Repository navigation
Conversation
`AWSEC2MetadataClient::getAvailabilityZoneOrException` performed a plain unauthenticated `GET` against the EC2 instance metadata service. On instances configured for IMDSv2 only (the AWS-recommended setting), the metadata endpoint rejects unauthenticated `GET` requests with `401 Unauthorized`, so `tryGetRunningAvailabilityZone` failed and ClickHouse / Keeper logged `Failed to find the availability zone, tried AWS and GCP` even though the metadata service was reachable. Acquire an IMDSv2 session token first via `PUT /latest/api/token` with the `x-aws-ec2-metadata-token-ttl-seconds` header (the `getDefaultCredentialsSecurely` path in the same client already does this), then send the AZ `GET` with `x-aws-ec2-metadata-token` set. If the token `PUT` fails or returns a non-200, fall back to the unauthenticated `GET` so endpoints that do not implement IMDSv2 keep working as before. Adds an integration test using a new mock metadata server (`imdsv2_server.py`) that requires the IMDSv2 token on AZ `GET`, plus a new `node_imds_v2` instance and `test_placement_info_from_imdsv2_only` in `tests/integration/test_placement_info/test.py`. Closes ClickHouse#81402
|
Workflow [PR], commit [9c5096a] Summary: ❌
AI ReviewSummaryThis PR fixes EC2 availability-zone detection on IMDSv2-only instances by requesting an IMDSv2 token before querying the AZ endpoint, with fallback to unauthenticated IMDSv1 behavior when token acquisition is unavailable. The change is narrowly scoped, preserves backward compatibility, and is covered by an integration test that exercises the IMDSv2-only flow. ClickHouse Rules
Final Verdict
|
|
Thanks for this fix, @ashrithb! Since this PR was opened (2026-04-26), the same issue was independently fixed in master by #81323 ( Your integration test, however, covers the "IMDSv2 only (token required)" scenario that master's Closing this one as superseded by #81323. Thank you again for the contribution and the test! |
Issue ClickHouse#81402 reported that availability-zone detection failed on EC2 instances configured for "IMDSv2 only (token required)", because `getAvailabilityZoneOrException` issued a plain unauthenticated `GET` against the instance metadata service, which such instances reject with `401 Unauthorized`. The behavior was fixed in master by ClickHouse#81323 (`kafka_aws_az_aware`), which makes `getAvailabilityZoneOrException` acquire an IMDSv2 session token via `PUT /latest/api/token` and send it with the availability-zone `GET`, falling back to the token-less IMDSv1 request when the metadata service does not implement the token endpoint. That change did not add a test covering the IMDSv2-only scenario in `test_placement_info`. This adds that missing coverage: a new mock metadata server `imdsv2_server.py` that issues tokens on `PUT /latest/api/token` and rejects `GET` requests without a valid `x-aws-ec2-metadata-token` header (`401`), plus a `node_imds_v2` instance and a `test_placement_info_from_imdsv2_only` case that asserts the availability zone is still loaded against such an endpoint. The test originates from ClickHouse#103549, which proposed an equivalent fix before ClickHouse#81323 was merged; that pull request is now superseded for the production change, so only its regression test is kept here. Co-Authored-By: ashrithb <[email protected]> Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Sure, thanks for letting me know! |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix availability-zone detection on EC2 instances configured for "IMDSv2 only (token required)".
getRunningAvailabilityZonenow obtains an IMDSv2 session token before fetching the AZ resource, falling back to the legacy unauthenticated request when the metadata service does not implement the token endpoint.Documentation entry for user-facing changes
Motivation
AWSEC2MetadataClient::getAvailabilityZoneOrExceptionperformed a plainunauthenticated
GETagainst the EC2 instance metadata service. Oninstances configured for "IMDSv2 only" (the AWS-recommended hardening
setting), the metadata endpoint rejects unauthenticated
GETrequestswith
401 Unauthorized, sotryGetRunningAvailabilityZonefailed andClickHouse / Keeper logged
tryGetRunningAvailabilityZone: ... Failed to get AWS availability zone.
HTTP response code: HTTP_UNAUTHORIZED.
... Failed to find the availability zone, tried AWS and GCP.
even though the metadata service was reachable.
The credentials path in the same client (
getDefaultCredentialsSecurely)already does the IMDSv2 dance: PUT
/latest/api/tokento obtain a token,then GET with
x-aws-ec2-metadata-tokenset. The AZ helper still usedthe older code path.
Fix
Acquire an IMDSv2 session token first via
PUT /latest/api/tokenwiththe
x-aws-ec2-metadata-token-ttl-secondsheader (the constants andheader names already exist in
Credentials.h), then send the AZGETwith
x-aws-ec2-metadata-tokenset. If the tokenPUTfails or returnsa non-200, fall back to the unauthenticated
GETso endpoints that donot implement IMDSv2 keep working as before.
Test
Adds an integration test using a new mock metadata server,
tests/integration/test_placement_info/metadata_servers/imdsv2_server.py,which requires the IMDSv2 token on the AZ
GETand returns401withoutit. A new
node_imds_v2instance andtest_placement_info_from_imdsv2_onlycase in
tests/integration/test_placement_info/test.pyexercise the newpath end-to-end against the mock.
Closes #81402