Skip to content

fix(aws): keep the resolved endpoint when building a Config from credentials - #1885

Open
ffernandezcast wants to merge 1 commit into
gruntwork-io:mainfrom
ffernandezcast:ffernandezcast/aws-preserve-base-endpoint
Open

fix(aws): keep the resolved endpoint when building a Config from credentials#1885
ffernandezcast wants to merge 1 commit into
gruntwork-io:mainfrom
ffernandezcast:ffernandezcast/aws-preserve-base-endpoint

Conversation

@ffernandezcast

@ffernandezcast ffernandezcast commented Aug 17, 2026

Copy link
Copy Markdown

Description

AWS_ENDPOINT_URL is honoured by NewAuthenticatedSessionContext but 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.LoadDefaultConfig resolves AWS_ENDPOINT_URL (and the per-service AWS_ENDPOINT_URL_<SERVICE> form) into Config.BaseEndpoint, which service clients inherit through NewFromConfig. Three helpers return a freshly built aws.Config carrying only Region and Credentials, dropping it:

helper endpoint preserved
NewAuthenticatedSessionFromDefaultCredentialsContext yes
NewAuthenticatedSessionFromRoleContext no
CreateAwsSessionWithCredsContext no
CreateAwsSessionWithMfaContext no

This is not limited to callers of those three. NewAuthenticatedSessionContext delegates to the role helper whenever TERRATEST_IAM_ROLE is 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

  • NewAuthenticatedSessionFromRoleContext already holds a fully resolved config, so it swaps the assumed-role credentials into that config instead of rebuilding one.
  • The other two share a small helper that loads the standard configuration for the region and overrides only the credentials.

Two of the three call sites get shorter as a result.

One behaviour change to flag: 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, documented as "accepted for API consistency but not currently used", is now used. Explicitly supplied credentials still take precedence.

Tests stub STS over httptest with canned AssumeRole and GetSessionToken responses, 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.

  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • Include release notes. If this PR is backward incompatible, include a migration guide.
  • Make a plan for release of the functionality in this PR. If it delivers value to an end user, you are responsible for ensuring it is released promptly, and correctly. If you are not a maintainer, you are responsible for finding a maintainer to do this for you.

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, CreateAwsSessionWithCredsContext and CreateAwsSessionWithMfaContext discarding the resolved endpoint, so AWS_ENDPOINT_URL is honoured when assuming a role or supplying explicit credentials.

Migration Guide

No signature changes. Two behavioural notes:

  • CreateAwsSessionWithCredsContext can now return a non-nil error where it previously always returned nil. Callers already handle the error return.
  • These helpers now reflect AWS_ENDPOINT_URL in the returned Config. If you set that variable but relied on these helpers ignoring it, unset it or scope it per service with AWS_ENDPOINT_URL_<SERVICE>.

…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.
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown

Caution

Review failed

An 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assert failing if you are using local configuration for testing (i.e: localstack locally)

1 participant