Add sasl_oauth_token_provider_class for native OAUTHBEARER support#1226
Add sasl_oauth_token_provider_class for native OAUTHBEARER support#1226dylanbstorey wants to merge 5 commits intoAiven-Open:mainfrom
Conversation
|
@dylanbstorey can you pls rebase? |
d7d5268 to
3639f20
Compare
|
done |
|
@dylanbstorey there seems to be some lint errors |
|
done |
|
Hey — just a note on the 3.14 test failure: we've seen two different tests fail across two CI runs ( Our changes only add a new config field ( They’re also passing locally from the makefile tests-in-docker pattern you provide. Happy to re-trigger CI or investigate further if needed! |
|
@muralibasani |
f841f2f to
bbb508c
Compare
|
Rebased on main (includes merged #1225). All tests passing locally:
|
c532ef3 to
c699abc
Compare
|
@muralibasani , added some more retries and polling helpers to help with flakiness. |
Adds a new config option `sasl_oauth_token_provider_class` that accepts a Python import path to a class implementing the TokenWithExpiryProvider protocol (i.e. a `token_with_expiry` method). When configured, an instance is created and passed as `sasl_oauth_token_provider` to all Kafka client factories (admin, consumer, producer), including the schema reader's internal clients. This enables native OAUTHBEARER auth (e.g. AWS MSK IAM) without monkey-patching, configurable via env var: KARAPACE_SASL_OAUTH_TOKEN_PROVIDER_CLASS=mymodule:MyProvider Includes an example MSK IAM token provider implementation in examples/msk_iam_token_provider.py.
Documents how to configure sasl_oauth_token_provider_class, including how to load a provider class that lives outside the Karapace repo (pip install, PYTHONPATH, Docker mount).
Add missing copyright header to examples/msk_iam_token_provider.py and add return type annotations to _get_oauth_token_provider in both kafka_utils.py and schema_reader.py.
The test_schema_registry_oidc test was intermittently failing because it attempted write requests before the schema registry had elected a primary. This caused the node to forward the request to itself in a loop until the 60s timeout, resulting in a 500 Internal Server Error with text/plain response.
885ec91 to
bb52ed9
Compare
|
@muralibasani - looks like the recent merges to master have stabilized the tests and this is passing now. ready for a review for real this time. Thanks ! |
src/karapace/core/schema_reader.py
Outdated
| def _get_oauth_token_provider(config: Config) -> object | None: | ||
| """Instantiate the configured OAuth token provider, if any.""" | ||
| if config.sasl_oauth_token_provider_class is not None: | ||
| return config.sasl_oauth_token_provider_class() |
There was a problem hiding this comment.
can we check for token_with_expiry ?
muralibasani
left a comment
There was a problem hiding this comment.
@dylanbstorey thanks for the pr. Have a few comments left.
- Deduplicate _get_oauth_token_provider: single get_oauth_token_provider() in kafka_utils.py, imported by schema_reader.py - Cache provider instance as singleton to avoid re-instantiation per client - Validate that the provider implements token_with_expiry() on first use - Fix copyright years to 2026 in examples and tests - Add tests for validation error and caching behavior
|
@muralibasani , updated |
Summary
sasl_oauth_token_provider_classconfig option that accepts a Python import path (via pydanticImportString) to a class implementing theTokenWithExpiryProviderprotocolsasl_oauth_token_providerto all Kafka client factories — admin, consumer, and producer — including the schema reader's internal clientsProblem
kafka_utils.pyandschema_reader.pycreate Kafka clients but never passsasl_oauth_token_provider, even though_KafkaConfigMixinalready supports it viaKafkaClientParams→oauth_cb. Anyone deploying Karapace against AWS MSK with IAM auth must monkey-patch the client factories or build a custom wrapper.Solution
New config option configurable via env var:
The referenced class must implement:
Changes
src/karapace/core/config.py: Addsasl_oauth_token_provider_class: ImportString | Nonefieldsrc/karapace/core/kafka_utils.py: Instantiate provider and pass to all 3 factory functionssrc/karapace/core/schema_reader.py: Same for the 2 internal factory functionsexamples/msk_iam_token_provider.py: Example AWS MSK IAM token provider usingaws-msk-iam-sasl-signer-pythontests/unit/test_oauth_token_provider.py: 14 unit tests covering config, all 5 factories (with/without provider)Test plan
tests/unit/test_oauth_token_provider.py)