3232# include < aws/core/auth/AWSCredentialsProvider.h>
3333# include < aws/core/auth/AWSCredentialsProviderChain.h>
3434# include < aws/core/client/ClientConfiguration.h>
35+ # include < aws/core/config/ConfigAndCredentialsCacheManager.h>
3536# include < aws/core/http/standard/StandardHttpRequest.h>
37+ # include < aws/core/platform/Environment.h>
3638# include < aws/core/utils/HashingUtils.h>
3739
3840# include " iceberg/catalog/rest/auth/auth_managers.h"
@@ -170,7 +172,11 @@ SigV4AuthSession::SigV4AuthSession(
170172 signer_(std::make_unique<RestSigV4Signer>(
171173 credentials_provider_, signing_name_.c_str(), signing_region_.c_str())) {}
172174
173- SigV4AuthSession::~SigV4AuthSession () { AwsSdkLifecycle::Instance ().UnregisterSession (); }
175+ SigV4AuthSession::~SigV4AuthSession () {
176+ if (owns_sdk_registration_) {
177+ AwsSdkLifecycle::Instance ().UnregisterSession ();
178+ }
179+ }
174180
175181Result<HttpRequest> SigV4AuthSession::Authenticate (const HttpRequest& request) {
176182 ICEBERG_ASSIGN_OR_RAISE (auto delegate_request, delegate_->Authenticate (request));
@@ -338,18 +344,36 @@ SigV4AuthManager::MakeCredentialsProvider(
338344 return std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>();
339345}
340346
341- std::string SigV4AuthManager::ResolveSigningRegion (
347+ Result< std::string> SigV4AuthManager::ResolveSigningRegion (
342348 const std::unordered_map<std::string, std::string>& properties) {
343349 if (auto it = properties.find (AuthProperties::kSigV4SigningRegion );
344350 it != properties.end () && !it->second .empty ()) {
345351 return it->second ;
346352 }
347- // ClientConfiguration() walks env / profile / IMDS / us-east-1; the IMDS
348- // step can block for seconds on non-EC2 hosts. Resolve once per process
349- // (set AWS_EC2_METADATA_DISABLED=true to skip IMDS).
350- static const std::string kSdkResolvedRegion =
351- std::string (Aws::Client::ClientConfiguration ().region .c_str ());
352- return kSdkResolvedRegion ;
353+ // Resolve from env then the shared config profile (skip IMDS — it can block
354+ // on non-EC2 hosts), and fail rather than silently defaulting to us-east-1.
355+ // Resolved once per process.
356+ static const std::string kResolvedRegion = []() -> std::string {
357+ Aws::String region = Aws::Environment::GetEnv (" AWS_REGION" );
358+ if (region.empty ()) {
359+ region = Aws::Environment::GetEnv (" AWS_DEFAULT_REGION" );
360+ }
361+ if (region.empty ()) {
362+ const auto & profiles = Aws::Config::GetCachedConfigProfiles ();
363+ if (auto it = profiles.find (Aws::Auth::GetConfigProfileName ());
364+ it != profiles.end ()) {
365+ region = it->second .GetRegion ();
366+ }
367+ }
368+ return std::string (region.c_str ());
369+ }();
370+ if (kResolvedRegion .empty ()) {
371+ return InvalidArgument (
372+ " SigV4: could not resolve a signing region; set the '{}' property or the "
373+ " AWS_REGION environment variable" ,
374+ AuthProperties::kSigV4SigningRegion );
375+ }
376+ return kResolvedRegion ;
353377}
354378
355379std::string SigV4AuthManager::ResolveSigningName (
@@ -392,7 +416,7 @@ Result<std::shared_ptr<AuthSession>> SigV4AuthManager::WrapSession(
392416 const std::unordered_map<std::string, std::string>& properties,
393417 std::shared_ptr<Aws::Auth::AWSCredentialsProvider> reuse_credentials) {
394418 ICEBERG_ASSIGN_OR_RAISE (auto slot, SessionSlot::Reserve ());
395- auto region = ResolveSigningRegion (properties);
419+ ICEBERG_ASSIGN_OR_RAISE ( auto region, ResolveSigningRegion (properties) );
396420 auto service = ResolveSigningName (properties);
397421
398422 // Reuse the parent's provider unless properties override keys, avoiding a
@@ -409,6 +433,8 @@ Result<std::shared_ptr<AuthSession>> SigV4AuthManager::WrapSession(
409433 auto session =
410434 std::make_shared<SigV4AuthSession>(std::move (delegate_session), std::move (region),
411435 std::move (service), std::move (credentials));
436+ // The reserved slot's unregister responsibility now belongs to the session.
437+ session->owns_sdk_registration_ = true ;
412438 slot.Release ();
413439 return session;
414440}
0 commit comments