Enable OAuth 2.0/OIDC authentication for Aiven for Apache Kafka® Schema Registry
Use OAuth 2.0/OpenID Connect (OIDC) to authenticate requests to Karapace Schema Registry with JSON Web Tokens (JWTs) issued by your identity provider.
You can also enable role-based authorization to control which Schema Registry operations clients can perform.
OAuth 2.0/OIDC token handling
Karapace Schema Registry validates JWTs sent with the Bearer authentication scheme. It checks tokens against the OIDC provider settings for the Aiven for Apache Kafka service.
This differs from the Karapace REST proxy, where Apache Kafka validates the bearer token. Schema Registry also validates the token.
When you enable OIDC authentication, clients can use a bearer token or basic authentication. Enabling OIDC authentication does not turn off basic authentication.
Keep basic authentication on while you migrate clients to JWT authentication. After all clients use JWT authentication, turn off basic authentication.
Authorization enforcement
By default, any client with a valid token can access Schema Registry. To restrict access, enable role-based authorization.
Karapace extracts roles from the JWT using a configured claim path. It checks those roles against the roles allowed for the requested HTTP method.
Enable OIDC authentication before you enable role-based authorization.
Prerequisites
Before you begin, make sure you have:
- An Aiven for Apache Kafka® service with Schema Registry enabled
- Karapace version 6.2.1 or later
- Access to an OIDC-compliant identity provider
- OIDC provider settings configured for your Aiven for Apache Kafka service,
including
kafka.sasl_oauthbearer_jwks_endpoint_url,kafka.sasl_oauthbearer_expected_issuer, andkafka.sasl_oauthbearer_expected_audience
Schema Registry uses the same OIDC provider settings as Apache Kafka. Those settings include the JWKS endpoint, the expected issuer, and the expected audience.
The Aiven Console does not require the issuer and audience. Schema Registry uses them to validate tokens.
For more information about configuring these settings, see Enable OAuth 2.0/OIDC authentication for Apache Kafka®.
If your service runs a Karapace version earlier than 6.2.1, apply the available maintenance update first. For more information, see Set the Karapace version.
Enable OIDC authentication
- Console
- CLI
- In the Aiven Console, select your project and choose your Aiven for Apache Kafka service.
- Click Service settings.
- Click Advanced configuration > Configure.
- Click Add configuration options.
- Add
schema_registry_config.sasl_oauthbearer_authentication_enabled. - Set the option to Enabled.
- Click Save configuration.
Run the following command:
avn service update SERVICE_NAME \
-c schema_registry_config.sasl_oauthbearer_authentication_enabled=true
Replace SERVICE_NAME with the name of your Aiven for Apache Kafka service.
Enable role-based authorization
Enable role-based authorization to restrict Schema Registry operations based on roles in the JWT.
Before you enable authorization, set
schema_registry_config.sasl_oauthbearer_authentication_enabled to
Enabled.
You can customize how Karapace reads and applies roles:
schema_registry_config.sasl_oauthbearer_roles_claim_path: Claim path used to extract roles from the JWT. The default isresource_access.karapace.roles. Set this option if your identity provider stores roles at a different path.schema_registry_config.sasl_oauthbearer_method_roles: Maps HTTP methods to the roles allowed to use them. Set this option to customize access forGET,POST,PUT, andDELETErequests.
Role names use the karapace. prefix, for example
karapace.schema:read.
Default HTTP method roles
If you do not set schema_registry_config.sasl_oauthbearer_method_roles,
Karapace allows only read access.
| Action | HTTP method | Default roles |
|---|---|---|
| Read schemas | GET | karapace.schema:read, karapace.subject:read |
| Register or update schemas | POST, PUT | None |
| Delete schemas | DELETE | None |
An empty array ([]) means no role can use that method.
Karapace uses the following default mapping:
{
"GET": [
"karapace.schema:read",
"karapace.subject:read"
],
"POST": [],
"PUT": [],
"DELETE": []
}
To allow write access, set
schema_registry_config.sasl_oauthbearer_method_roles.
Configure authorization
- Console
- CLI
- In the Aiven Console, select your project and choose your Aiven for Apache Kafka service.
- Click Service settings.
- Click Advanced configuration > Configure.
- Make sure
schema_registry_config.sasl_oauthbearer_authentication_enabledis set to Enabled. - Click Add configuration options.
- Add
schema_registry_config.sasl_oauthbearer_authorization_enabledand set it to Enabled. - Optional: Add
schema_registry_config.sasl_oauthbearer_roles_claim_pathif your JWT stores roles somewhere other thanresource_access.karapace.roles. - Optional: Add
schema_registry_config.sasl_oauthbearer_method_rolesto customize which roles can use each HTTP method. - Click Save configuration.
To enable role-based authorization using the default roles claim path and default HTTP method roles, run:
avn service update SERVICE_NAME \
-c schema_registry_config.sasl_oauthbearer_authorization_enabled=true
Replace SERVICE_NAME with the name of your Aiven for Apache Kafka service.
To customize the HTTP method roles, include
schema_registry_config.sasl_oauthbearer_method_roles. For example:
avn service update SERVICE_NAME \
-c schema_registry_config.sasl_oauthbearer_authorization_enabled=true \
-c 'schema_registry_config.sasl_oauthbearer_method_roles={"GET":["karapace.schema:read","karapace.schema:write"],"POST":["karapace.schema:write"],"PUT":["karapace.schema:write"],"DELETE":["karapace.schema:write"]}'
Configure roles for HTTP methods
Set schema_registry_config.sasl_oauthbearer_method_roles to JSON that maps
each HTTP method to the roles that can use it.
Clients with karapace.schema:read can read schemas.
Clients with karapace.schema:write can read and write schemas.
| Role | Allowed actions |
|---|---|
karapace.schema:read | Read schemas (GET) |
karapace.schema:write | Read and write schemas (GET, POST, PUT, DELETE) |
Each key in the JSON is an HTTP method. Each value is the list of roles allowed for that method:
{
"GET": [
"karapace.schema:read",
"karapace.schema:write"
],
"POST": [
"karapace.schema:write"
],
"PUT": [
"karapace.schema:write"
],
"DELETE": [
"karapace.schema:write"
]
}
When you set this option, include GET, POST, PUT, and DELETE.
To block a method, set its value to [].
Send a request to Schema Registry
Send the JWT in the Authorization header of each Schema Registry request.
You can use curl or any HTTP client that supports bearer tokens.
On the service Overview page, open Connection information and copy the Schema Registry URL.
The following example lists subjects:
curl \
--header "Authorization: Bearer ACCESS_TOKEN" \
"SCHEMA_REGISTRY_URL/subjects"
Replace the following:
ACCESS_TOKEN: a valid JWT from your identity providerSCHEMA_REGISTRY_URL: the Schema Registry URL from Connection information
This example uses GET, so it works with the default read roles.
If authorization is on, a POST, PUT, or DELETE request needs a write role.
Disable OAuth 2.0/OIDC authentication
To turn off OIDC authentication and authorization, set both options to Disabled. This does not turn off basic authentication.
- Console
- CLI
- In the Aiven Console, select your project and choose your Aiven for Apache Kafka service.
- Click Service settings.
- Click Advanced configuration > Configure.
- Set
schema_registry_config.sasl_oauthbearer_authorization_enabledto Disabled. - Set
schema_registry_config.sasl_oauthbearer_authentication_enabledto Disabled. - Click Save configuration.
Run the following command:
avn service update SERVICE_NAME \
-c schema_registry_config.sasl_oauthbearer_authorization_enabled=false \
-c schema_registry_config.sasl_oauthbearer_authentication_enabled=false
Replace SERVICE_NAME with the name of your Aiven for Apache Kafka service.
Related pages