fix(aws): keep the resolved endpoint when building a Config from credentials - #1885
Open
ffernandezcast wants to merge 1 commit into
Open
Conversation
…entials NewAuthenticatedSessionFromRoleContext, CreateAwsSessionWithCredsContext and CreateAwsSessionWithMfaContext each returned a freshly built aws.Config that carried only Region and Credentials. Everything else the SDK had resolved was dropped, including Config.BaseEndpoint. That matters for anyone pointing the SDK at an AWS emulator such as LocalStack or Moto. config.LoadDefaultConfig resolves AWS_ENDPOINT_URL (and the per-service AWS_ENDPOINT_URL_<SERVICE> form) into BaseEndpoint, and service clients inherit it through NewFromConfig, so NewAuthenticatedSessionContext honours it. The three helpers above silently did not: with TERRATEST_IAM_ROLE set, requests went to real AWS even though an override was configured. Silent misrouting rather than a visible failure. NewAuthenticatedSessionFromRoleContext already had a fully resolved config in hand, so it now swaps the assumed-role credentials into it instead of discarding it. The other two gain a small helper that loads the standard configuration for the region and overrides only the credentials. Behaviour change worth noting for reviewers: CreateAwsSessionWithCredsContext previously never consulted the environment and could not fail. It now resolves ambient configuration, so it honours AWS_ENDPOINT_URL and can return an error if that configuration is unreadable. Its ctx argument, documented as unused, is now actually used. Credentials passed in explicitly still take precedence. Tests stub the STS endpoint over httptest, so they run offline with no AWS account and no credentials.
ffernandezcast
requested review from
denis256,
james00012 and
yhakbar
as code owners
August 17, 2026 15:26
|
Caution Review failedAn error occurred during the review process. Please try again later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
AWS_ENDPOINT_URLis honoured byNewAuthenticatedSessionContextbut silently discarded by three sibling helpers, so a suite pointed at an emulator such as LocalStack or Moto can end up talking to real AWS.The problem
config.LoadDefaultConfigresolvesAWS_ENDPOINT_URL(and the per-serviceAWS_ENDPOINT_URL_<SERVICE>form) intoConfig.BaseEndpoint, which service clients inherit throughNewFromConfig. Three helpers return a freshly builtaws.Configcarrying onlyRegionandCredentials, dropping it:NewAuthenticatedSessionFromDefaultCredentialsContextNewAuthenticatedSessionFromRoleContextCreateAwsSessionWithCredsContextCreateAwsSessionWithMfaContextThis is not limited to callers of those three.
NewAuthenticatedSessionContextdelegates to the role helper wheneverTERRATEST_IAM_ROLEis set, so the standard entry point loses the override too.Nothing errors when this happens — requests are simply sent to AWS instead of the emulator.
The change
NewAuthenticatedSessionFromRoleContextalready holds a fully resolved config, so it swaps the assumed-role credentials into that config instead of rebuilding one.Two of the three call sites get shorter as a result.
One behaviour change to flag:
CreateAwsSessionWithCredsContextpreviously never consulted the environment and could not fail. It now resolves ambient configuration, so it honoursAWS_ENDPOINT_URLand can return an error if that configuration is unreadable. Itsctx, documented as "accepted for API consistency but not currently used", is now used. Explicitly supplied credentials still take precedence.Tests stub STS over
httptestwith cannedAssumeRoleandGetSessionTokenresponses, so they run offline with no AWS account.I could not find an open issue covering this. Related, both closed: #494, #453.
TODOs
Read the Gruntwork contribution guidelines.
Unticked deliberately: no docs change seemed warranted for restoring existing documented behaviour, though I will add one if you disagree; and I am not a maintainer, so I cannot own the release.
Release Notes (draft)
Fixed
NewAuthenticatedSessionFromRoleContext,CreateAwsSessionWithCredsContextandCreateAwsSessionWithMfaContextdiscarding the resolved endpoint, soAWS_ENDPOINT_URLis honoured when assuming a role or supplying explicit credentials.Migration Guide
No signature changes. Two behavioural notes:
CreateAwsSessionWithCredsContextcan now return a non-nil error where it previously always returnednil. Callers already handle the error return.AWS_ENDPOINT_URLin the returnedConfig. If you set that variable but relied on these helpers ignoring it, unset it or scope it per service withAWS_ENDPOINT_URL_<SERVICE>.