Repository navigation
Add AWS MSK IAM authentication for Kafka table engine - #96100
Conversation
2f50232 to
27167b7
Compare
27167b7 to
bbf19ed
Compare
|
@antaljanosbenjamin for your review, sorry decided open new PR hope this won't trouble you. |
|
Workflow [PR], commit [ca5c1a6] Summary: ✅
AI ReviewSummaryThis PR adds Missing context / blind spots
Findings
Tests
Final VerdictStatus: Minimum required actions: add direct signed-token coverage for |
|
I updated the PR description. Next time please keep the format, otherwise our CI cannot pick up the necessary information. |
| std::mutex g_contexts_mutex; | ||
| std::unordered_map<OAuthBearerTokenRefreshContext*, std::shared_ptr<OAuthBearerTokenRefreshContext>> g_contexts; |
There was a problem hiding this comment.
We don't need a global container for this. Check how set_assignment_callback and oauthBearerTokenRefreshCallback is used. We have to do similar here.
There was a problem hiding this comment.
Thanks for the review!
The current implementation no longer uses a global container. We now use ensureOAuthContext() on the storage object — each StorageKafka/StorageKafka2 instance holds a single shared_ptr<OAuthBearerTokenRefreshContext> guarded by a mutex, and the librdkafka callback captures that shared_ptr by value.
This is consistent with how set_assignment_callback and the interceptor callbacks are structured: the context lives on the storage, and the lambda captures a reference to it. The only difference here is we use a shared_ptr (rather than a raw ref) because librdkafka can invoke the OAUTHBEARER callback from an internal thread after the storage has started shutting down — the shared_ptr ensures the CredentialsProvider outlives those calls safely.
9aed2d6 to
85b74bc
Compare
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
85b74bc to
92c445e
Compare
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
92c445e to
215336b
Compare
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
215336b to
1c7bb15
Compare
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
1c7bb15 to
83429b8
Compare
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
83429b8 to
adfe9fe
Compare
|
I couldn't spend time on this PR recently, will look into it again. |
| catch (...) // NOLINT(bugprone-empty-catch) | ||
| { | ||
| // Ok: non-setup exceptions (e.g. missing AWS credentials) are acceptable here. | ||
| } |
There was a problem hiding this comment.
If an exception is thrown in setupAuthentication, then the following two checks won't be checked. So I think we should ensure no exception is thrown.
| { | ||
| // Ok: non-region exceptions (e.g. missing AWS credentials) are acceptable here. |
There was a problem hiding this comment.
What if the function throws before the region check is done in setupAuthentication?
| String token = generateAWSMSKToken(context->region, credentials); | ||
|
|
||
| auto now = std::chrono::system_clock::now(); |
There was a problem hiding this comment.
Let's be conservative on the expiry time.
| String token = generateAWSMSKToken(context->region, credentials); | |
| auto now = std::chrono::system_clock::now(); | |
| auto now = std::chrono::system_clock::now(); | |
| String token = generateAWSMSKToken(context->region, credentials); |
| && !boost::iequals(kafka_config.get("security.protocol"), "SASL_SSL")) | ||
| LOG_WARNING( | ||
| params.log, | ||
| "kafka_security_protocol='{}' will be overridden to 'SASL_SSL' — AWS MSK IAM requires SASL_SSL.", |
There was a problem hiding this comment.
| "kafka_security_protocol='{}' will be overridden to 'SASL_SSL' — AWS MSK IAM requires SASL_SSL.", | |
| "kafka_security_protocol='{}' will be overridden to 'SASL_SSL' - AWS MSK IAM requires SASL_SSL.", |
| "AWS MSK IAM: cached OAuth context uses region '{}' but broker list '{}' resolves to region '{}'. " | ||
| "Consumer and producer must connect to MSK clusters in the same AWS region.", | ||
| context_holder->region, broker_list, effective_region); |
There was a problem hiding this comment.
| "AWS MSK IAM: cached OAuth context uses region '{}' but broker list '{}' resolves to region '{}'. " | |
| "Consumer and producer must connect to MSK clusters in the same AWS region.", | |
| context_holder->region, broker_list, effective_region); | |
| "AWS MSK IAM: Different regions used: already configured region is '{}' but got another region '{}'. " | |
| "Consumer and producer of the same table must connect to MSK clusters in the same AWS region.", | |
| context_holder->region, effective_region); |
- Fix UBSan: guard LOG_DEBUG against null logger - Fix loop condition: start <= size() -> start < size() - Support China partition broker hostnames (.amazonaws.com.cn) - Move timestamp before token generation for conservative expiry - Replace em-dash with ASCII hyphen in log message - Improve region mismatch error message wording - Strengthen test assertions (move checks outside try/catch) Co-Authored-By: Claude Opus 4.8 <[email protected]>
antaljanosbenjamin
left a comment
There was a problem hiding this comment.
Once the unit tests are passing, I think we are good to merge this.
| constexpr std::chrono::seconds TOKEN_LIFETIME{300}; | ||
| constexpr std::chrono::seconds PRESIGNED_URL_EXPIRY{900}; | ||
|
|
||
| String generateAWSMSKToken( |
There was a problem hiding this comment.
This is the only code that proves the new AWS_MSK_IAM mechanism can produce a token MSK will accept, but none of the added tests exercise generateAWSMSKToken or the OAUTH callback. The current tests only cover region parsing and config rewriting, so a regression in the signed URL shape (Action, service scope, User-Agent, base64url encoding, or expiry) would still pass while the feature fails at runtime.
Please add a deterministic unit test with static dummy AWS credentials that invokes this token generation path, decodes the token, and asserts the expected host/query parameters and expiry. Making generateAWSMSKToken test-visible is enough; no live AWS or Kafka dependency is needed.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 232/257 (90.27%) | Lost baseline coverage: none · Uncovered code |
|
@antaljanosbenjamin |
|
@kalavt, I've opened a PR against your branch to try to improve test coverage for the authentication piece :) |
|
Sorry, this flew under my radar. Merging it. |
…rowsOnZeroChecksum The test deliberately exercises the zero-checksum guard in `getBackupDataFileName`, which throws `DB::Exception(LOGICAL_ERROR, ...)`. On non-debug, non-sanitizer builds (e.g. `Unit tests (amd_llvm_coverage)`) the test passes, but `Exception::~Exception()` then logs the exception through `ForcedCriticalErrorsLogger` because `LOGICAL_ERROR` is treated as an "important" error code unless `markAsLogged` has been called. The resulting `Code: 49. DB::Exception: Backup checksum should not be zero (test.bin). (LOGICAL_ERROR)` line plus its full stack trace ends up on the gtest binary's stderr after the test's `[ OK ]` line. When an unrelated test in the same `unit_tests_dbms` binary later hangs (e.g. `SchedulerWorkloadResourceManager.DropNotEmptyQueueLong` per the report linked below), the wrapping `gdb -batch ... -ex run` runner times out, kills the binary mid-suite, and `gtest.json` is never written. The Praktika `from_gtest` parser then has nothing to parse and falls back to log-scraping, which surfaces the leaked `Backup checksum should not be zero` stack trace as a fake `-FunctionsStress` failure in CIDB. Sixteen hits across `Unit tests (amd_llvm_coverage)` on PRs ClickHouse#100177, ClickHouse#105784, ClickHouse#96100 over the past 60 days follow this pattern; the underlying contributor is the chronic `SchedulerWorkloadResourceManager` / `FunctionsStress` family hang, not the backup test. Replace `EXPECT_THROW(..., Exception)` with an equivalent `try` block that calls `markAsLogged` on the caught exception, mirroring the pattern already used in `executeQuery.cpp` (commit 64a4caa "Mark fuzzer exceptions as logged to prevent ForcedCriticalErrorsLogger") to suppress the stray stack trace. Verified locally: built `unit_tests_dbms` in release mode (`-O3 -DNDEBUG`, `ENABLE_TESTS=1`, no sanitizer) and confirmed the pre-fix binary prints a 17-frame stack trace to stderr on this test, while the post-fix binary runs the test silently with `[ OK ]` and no trace. Related: ClickHouse#100177 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Implements SASL_SSL/OAUTHBEARER-based authentication for AWS MSK IAM
using SigV4-presigned URLs as tokens, as required by the MSK IAM auth
protocol.
New setting: kafka_sasl_mechanism = 'AWS_MSK_IAM'
New setting: kafka_aws_region (auto-detected from broker hostname if not set)
Implementation:
- AWSMSKIAMAuth.{h,cpp}: token generation via AWSAuthV4Signer::PresignRequest,
callback registered with librdkafka via rd_kafka_conf_enable_sasl_queue +
set_oauthbearer_token_refresh_callback
- KafkaConfigLoader: skips writing sasl.mechanism for AWS_MSK_IAM (librdkafka
does not know this value); sets SASL_SSL and OAUTHBEARER inside setupAuthentication
- StorageKafka / StorageKafka2: ensureOAuthContext() ensures a single shared
S3CredentialsProviderChain across all consumers (thread-safe, mutex-guarded)
- cppkafka submodule updated to release-0.4.1 for set_oauthbearer_token_refresh_callback
Closes ClickHouse#96100
Skip `use_environment_credentials` in `setKafkaConfigValue` (not a librdkafka property). Add `NOLINT` and `Ok` comment to suppress `bugprone-empty-catch` in the gtest. ClickHouse#96100
- Remove case-insensitive flag from region regex; `(?i)` could return uppercase regions that then fail `isValidAWSRegion`'s lowercase check - Scan all brokers for region auto-detection instead of only the first, so configs with a custom alias as the first broker still work ClickHouse#96100
…uth_v2 Add AWS MSK IAM authentication for Kafka table engine
…rowsOnZeroChecksum The test deliberately exercises the zero-checksum guard in `getBackupDataFileName`, which throws `DB::Exception(LOGICAL_ERROR, ...)`. On non-debug, non-sanitizer builds (e.g. `Unit tests (amd_llvm_coverage)`) the test passes, but `Exception::~Exception()` then logs the exception through `ForcedCriticalErrorsLogger` because `LOGICAL_ERROR` is treated as an "important" error code unless `markAsLogged` has been called. The resulting `Code: 49. DB::Exception: Backup checksum should not be zero (test.bin). (LOGICAL_ERROR)` line plus its full stack trace ends up on the gtest binary's stderr after the test's `[ OK ]` line. When an unrelated test in the same `unit_tests_dbms` binary later hangs (e.g. `SchedulerWorkloadResourceManager.DropNotEmptyQueueLong` per the report linked below), the wrapping `gdb -batch ... -ex run` runner times out, kills the binary mid-suite, and `gtest.json` is never written. The Praktika `from_gtest` parser then has nothing to parse and falls back to log-scraping, which surfaces the leaked `Backup checksum should not be zero` stack trace as a fake `-FunctionsStress` failure in CIDB. Sixteen hits across `Unit tests (amd_llvm_coverage)` on PRs ClickHouse#100177, ClickHouse#105784, ClickHouse#96100 over the past 60 days follow this pattern; the underlying contributor is the chronic `SchedulerWorkloadResourceManager` / `FunctionsStress` family hang, not the backup test. Replace `EXPECT_THROW(..., Exception)` with an equivalent `try` block that calls `markAsLogged` on the caught exception, mirroring the pattern already used in `executeQuery.cpp` (commit 64a4caa "Mark fuzzer exceptions as logged to prevent ForcedCriticalErrorsLogger") to suppress the stray stack trace. Verified locally: built `unit_tests_dbms` in release mode (`-O3 -DNDEBUG`, `ENABLE_TESTS=1`, no sanitizer) and confirmed the pre-fix binary prints a 17-frame stack trace to stderr on this test, while the post-fix binary runs the test silently with `[ OK ]` and no trace. Related: ClickHouse#100177 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added
AWS_MSK_IAMas a supported value forkafka_sasl_mechanism, enabling ClickHouse to authenticate with Amazon MSK using IAM roles without managing SASL/SCRAM credentials.Documentation entry for user-facing changes
Description
Implements AWS MSK IAM authentication for the Kafka table engine. ClickHouse can now authenticate with Amazon Managed Streaming for Apache Kafka (MSK) using IAM roles instead of managing SASL/SCRAM credentials or mTLS certificates.
Motivation
Users running ClickHouse on AWS commonly connect to MSK clusters. MSK IAM authentication is the preferred approach — it integrates with existing IAM role policies, requires no credential rotation, and works with instance profiles, ECS task roles, and EKS service accounts out of the box.
Changes
src/Storages/Kafka/AWSMSKIAMAuth.{h,cpp}: token generation viaAWSAuthV4Signer::PresignRequest, callback registered with librdkafka viard_kafka_conf_enable_sasl_queue+set_oauthbearer_token_refresh_callbackKafkaConfigLoader: skips writingsasl.mechanismforAWS_MSK_IAM(value unknown to librdkafka); setsSASL_SSL+OAUTHBEARERinsidesetupAuthenticationStorageKafka/StorageKafka2: addedensureOAuthContext()to share a singleS3CredentialsProviderChainacross all consumers (mutex-guarded)KafkaSettings: new settingskafka_sasl_mechanism = 'AWS_MSK_IAM'andkafka_aws_region(auto-detected from broker hostname if not set)contrib/cppkafka: updated torelease-0.4.1forset_oauthbearer_token_refresh_callbacksupportNew settings
kafka_sasl_mechanismAWS_MSK_IAMas a supported valuekafka_aws_region""Example
Requires
USE_AWS_S3=1(enabled by default in official builds).Version info
26.7.1.241(included in26.7and later)