Skip to content

Conversation

maastha
Copy link
Collaborator

@maastha maastha commented Sep 26, 2025

Description

Fixes STS region resolution when using cross-region authentication in the MongoDB Atlas Terraform provider. The fix addresses an issue where the provider wasn't correctly handling STS endpoint configuration when AWS credentials are in one region but the STS endpoint is in another region.

Link to any related issue(s): CLOUDP-347906

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@maastha maastha changed the title chore: Updates provider to infer AWS region from sts_endpoint fix: Fixes STS region resolution when using cross-region authentication Sep 26, 2025
@github-actions github-actions bot added the bug label Sep 26, 2025
@maastha maastha marked this pull request as ready for review September 26, 2025 20:59
@maastha maastha requested review from a team as code owners September 26, 2025 20:59
@Copilot Copilot AI review requested due to automatic review settings September 26, 2025 20:59
Copy link
Contributor

APIx bot: a message has been sent to Docs Slack channel

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes STS region resolution when using cross-region authentication in the MongoDB Atlas Terraform provider. The fix addresses an issue where the provider wasn't correctly handling STS endpoint configuration when AWS credentials are in one region but the STS endpoint is in another region.

  • Refactors STS endpoint resolution logic to properly derive signing regions from endpoint URLs
  • Adds utility functions for parsing STS endpoints and resolving regional configurations
  • Introduces comprehensive test coverage for cross-region authentication scenarios

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
internal/provider/credentials.go Implements new STS endpoint resolution logic with proper region parsing
internal/provider/credentials_test.go Adds comprehensive test coverage for STS endpoint resolution functions
.github/workflows/acceptance-tests-runner.yml Updates CI to test cross-region authentication scenarios with matrix strategy
.changelog/3718.txt Adds changelog entry documenting the bug fix

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@maastha
Copy link
Collaborator Author

maastha commented Sep 26, 2025

will hold off on merging this until we hear back from the customer

fail-fast: false
matrix:
include:
# Same region
Copy link
Member

@lantoli lantoli Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the name makes it clear, no need for the comments

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the comment more clear and specific in 06a0b97. I think it's useful

# Cross-region
- name: cross-sts-us-east-1-secret-eu-north-1
aws_region: EU_NORTH_1
sts_endpoint: https://sts.us-east-1.amazonaws.com/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although the test group list is "polluted" instead of having only one entry for assume_role, i think it's good use case for matrix.

also we'll need to merge with changes in SA dev branch, we might need to remove the matrix approach if we want to keep all authentication tests together, but that's ok. cc @oarbusi

no need for action about this comment.

Image


return endpoints.ResolvedEndpoint{
URL: ep,
SigningRegion: signingRegion,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind to check if SigningRegion is really needed or URL is enough so we don't need to calculate the sts region?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, can we actually check how AWS does it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, SigningRegion is needed. URL alone isn’t sufficient.

  • The AWS signer uses the client’s region when SigningRegion is not set. In this case, the client region is the Secrets Manager region, which may not match the STS endpoint’s region.

  • For the global endpoint sts.amazonaws.com, requests must be signed with us-east-1; without setting SigningRegion, signatures will be computed with the client region and can fail.

  • For regional STS endpoints, the signature must match that region as well.

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test coverage!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 !!

if err != nil {
return DefaultRegionSTS
}
host := u.Hostname() // valid values: sts.us-west-2.amazonaws.com or sts.amazonaws.com
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes the port if provided, correct? Not sure if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, "Hostname returns u.Host, stripping any valid port number if present.". It's not strictly necessary but with it we make sure at this point we have either sts.us-west-2.amazonaws.com or sts.amazonaws.com

ep = fmt.Sprintf("https://sts.%s.amazonaws.com/", r)
}

signingRegion := DeriveSTSRegionFromEndpoint(ep)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the endpoint is "", no need to derive the region since we know it already.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if endpoint is "" the region can be secretsRegion or default region. I think we can keep this as is to avoid having repeated logic(e.g. if r == ""). WDYT?

Copy link
Collaborator

@manupedrozo manupedrozo Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking something along the lines of:

ep := stsEndpoint
var signingRegion string
if ep == "" {
	signingRegion = secretsRegion
	if signingRegion == "" {			
       signingRegion = DefaultRegionSTS
	}
	ep = fmt.Sprintf("https://sts.%s.amazonaws.com/", signingRegion)
} else {
	signingRegion = DeriveSTSRegionFromEndpoint(ep)
}

But ok either way

@@ -0,0 +1,3 @@
```release-note:bug
provider: Fixes STS region resolution when using cross-region authentication
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it'd have been better to create an associated cloudp ticket

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, thank you Leo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created CLOUDP-347906

Copy link
Collaborator

@marcosuma marcosuma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one

@@ -0,0 +1,3 @@
```release-note:bug
provider: Fixes STS region resolution when using cross-region authentication
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, thank you Leo

include:
# Same region
- name: same-region-us-east-1
aws_region: US_EAST_1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could we lease a comment to explain what this aws_region is used for?

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 !!


return endpoints.ResolvedEndpoint{
URL: ep,
SigningRegion: signingRegion,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, can we actually check how AWS does it?

sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(awsAccessKeyID, awsSecretAccessKey, awsSessionToken),
STSRegionalEndpoint: ep,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why we don't need this/

return *cfg, nil
}

func DeriveSTSRegionFromEndpoint(ep string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance we could easily unit test this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet, you've done it- ignore me

Copy link
Contributor

@carriecwk carriecwk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@oarbusi oarbusi merged commit bcc21de into master Sep 30, 2025
45 checks passed
@oarbusi oarbusi deleted the update-sts-region branch September 30, 2025 07:26
svc-apix-Bot added a commit that referenced this pull request Sep 30, 2025
oarbusi added a commit that referenced this pull request Oct 1, 2025
…on (#3718)

* update provider to infer region from sts_endpoint

* changelog

* fmt

* nit

* nit

* fmt

* unit tests

* test

* nit

* nit

* nit

* improve clarity of comment

* use asserts in unit test

* use const instead of magic number

---------

Co-authored-by: Oriol Arbusi Abadal <oriol.abadal@mongodb.com>
oarbusi added a commit that referenced this pull request Oct 1, 2025
… authentication (#3718)" (#3731)

* fix: Fixes STS region resolution when using cross-region authentication (#3718)

* update provider to infer region from sts_endpoint

* changelog

* fmt

* nit

* nit

* fmt

* unit tests

* test

* nit

* nit

* nit

* improve clarity of comment

* use asserts in unit test

* use const instead of magic number

---------

Co-authored-by: Oriol Arbusi Abadal <oriol.abadal@mongodb.com>

* rename changelog file

---------

Co-authored-by: maastha <122359335+maastha@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants