Skip to content

S3 credentials: use IMDSv2 token for availability-zone detection - #103549

Closed
ashrithb wants to merge 1 commit into
ClickHouse:masterfrom
ashrithb:fix/issue-81402-imdsv2-availability-zone
Closed

ashrithb wants to merge 1 commit into
ClickHouse:masterfrom
ashrithb:fix/issue-81402-imdsv2-availability-zone

Conversation

@ashrithb

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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)". getRunningAvailabilityZone now 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

  • Documentation is written (mandatory for new features)

Motivation

AWSEC2MetadataClient::getAvailabilityZoneOrException performed a plain
unauthenticated GET against the EC2 instance metadata service. On
instances configured for "IMDSv2 only" (the AWS-recommended hardening
setting), the metadata endpoint rejects unauthenticated GET requests
with 401 Unauthorized, so tryGetRunningAvailabilityZone failed and
ClickHouse / 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/token to obtain a token,
then GET with x-aws-ec2-metadata-token set. The AZ helper still used
the older code path.

Fix

Acquire an IMDSv2 session token first via PUT /latest/api/token with
the x-aws-ec2-metadata-token-ttl-seconds header (the constants and
header names already exist in Credentials.h), 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.

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 GET and returns 401 without
it. A new node_imds_v2 instance and test_placement_info_from_imdsv2_only
case in tests/integration/test_placement_info/test.py exercise the new
path end-to-end against the mock.

Closes #81402

`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
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9c5096a]

Summary: ❌

job_name test_name status info comment
Style check FAIL
catch_all FAIL
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED

AI Review

Summary

This 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
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Thanks for this fix, @ashrithb!

Since this PR was opened (2026-04-26), the same issue was independently fixed in master by #81323 (kafka_aws_az_aware, merged 2026-05-07). getAvailabilityZoneOrException now acquires an IMDSv2 session token via PUT /latest/api/token and sends it with the availability-zone GET, falling back to the token-less IMDSv1 request — the same approach proposed here, with some extras (availability-zone-id support and serialized token retrieval). So the production change in this PR is now redundant.

Your integration test, however, covers the "IMDSv2 only (token required)" scenario that master's test_placement_info was still missing, so it is worth keeping. I've rebased just the test (imdsv2_server.py + test_placement_info_from_imdsv2_only) onto current master and opened #107767 to add it, with you retained as Co-authored-by. It passes against master's implementation.

Closing this one as superseded by #81323. Thank you again for the contribution and the test!

huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Jun 17, 2026
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]>
@ashrithb

Copy link
Copy Markdown
Contributor Author

Sure, thanks for letting me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

avaliability zone detection (getRunningAvailabilityZone) may not work on aws sometimes

2 participants