Skip to content

Conversation

durran
Copy link
Member

@durran durran commented Sep 9, 2025

Description

Updates AWS auth to use a custom credential provider first after URI/MongoClient credentials if present.

Summary of Changes

  • Custom credential provider now takes precedence over everything except URI, MongoClient credentials.
  • Adds additional prose tests.

What is the motivation for this change?

NODE-7047

Release Highlight

Custom AWS Credential Provider Takes Highest Precedence

When providing a custom AWS credential provider via the auth mechanism property AWS_CREDENTIAL_PROVIDER, it will now take the highest precedence over any other AWS auth mechanism.

Double check the following

  • Lint is passing (npm run check:lint)
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@durran durran force-pushed the NODE-7047 branch 2 times, most recently from 1a59a5a to 6e98b5d Compare September 10, 2025 12:44
@durran durran marked this pull request as ready for review September 10, 2025 12:51
@durran durran requested a review from a team as a code owner September 10, 2025 12:51
@dariakp dariakp changed the title feat(NODE-7047): use custom credential provider first feat(NODE-7047)!: use custom credential provider first Sep 17, 2025
@durran durran force-pushed the NODE-7047 branch 3 times, most recently from 7281459 to 8271c1f Compare September 19, 2025 11:54
@durran durran changed the title feat(NODE-7047)!: use custom credential provider first feat(NODE-7047)!: use custom credential provider first after URI Sep 19, 2025
@baileympearson baileympearson self-assigned this Sep 19, 2025
@baileympearson baileympearson added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Sep 19, 2025
let provider;

beforeEach(function () {
console.log(client?.options);
Copy link
Contributor

Choose a reason for hiding this comment

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

stray debugging console log here and in L222?

expect(providerCount).to.be.greaterThan(0);
beforeEach(function () {
if (client?.options.credentials.username || !process.env.AWS_ACCESS_KEY_ID) {
this.skipReason = 'Test only runs when credentials are present in the environment';
Copy link
Contributor

Choose a reason for hiding this comment

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

"... and not the URI"? Is that what client?.options.credentials.username is testing for? If so, should there be a similar check in the case above to make sure that process.env.AWS_ACCESS_KEY_ID is not set?

Copy link
Contributor

Choose a reason for hiding this comment

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

"... and not the URI"? Is that what client?.options.credentials.username is testing for?

Yup

If so, should there be a similar check in the case above to make sure that process.env.AWS_ACCESS_KEY_ID is not set?

No - URI takes precedence over everything, so it should run both when there are no credentials in the environment and when there are credentials in the environment.


expect(client).to.have.nested.property('s.authProviders');
const provider = client.s.authProviders.getOrCreateProvider('MONGODB-AWS');
const provider = client.s.authProviders.getOrCreateProvider('MONGODB-AWS', {});
Copy link
Contributor

Choose a reason for hiding this comment

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

what prompted this change?

this.mechanism = options.mechanism || AuthMechanism.MONGODB_DEFAULT;
this.mechanismProperties = options.mechanismProperties || {};

if (this.mechanism.match(/MONGODB-AWS/i)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work because we outsource the env credential reading to the sdk as of NODE-6988 and previously we were overriding the natural order by explicitly reading in the env vars?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, reading credentials from the environment in addition to using the SDK has always been a bug (https://jira.mongodb.org/browse/NODE-6987)

AWS_CREDENTIAL_PROVIDER: provider
}
context('2. Custom Credential Provider Authentication Precedence', function () {
context('Case 1: Credentials in URI Take Precedence', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

For all the prose tests, we usually copy the language of the prose test in the comments, so that it's easier to tell how the test implementation maps to the prose and and also to identify cases of divergence between the spec prose and the implementation (if either the prose or the implementation are updated in the future).

Copy link
Contributor

@baileympearson baileympearson left a comment

Choose a reason for hiding this comment

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

Just a small comment in addition to Daria's comment

});

it('authenticates with a user provided credentials provider', async function () {
console.log(process.env);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
console.log(process.env);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Primary Review In Review with primary reviewer, not yet ready for team's eyes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants