Skip to content

Commit 1a59a5a

Browse files
committed
feat(NODE-7047): use custom credential provider first
1 parent 1634d19 commit 1a59a5a

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/cmap/auth/mongodb_aws.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export class MongoDBAWS extends AuthProvider {
6363
);
6464
}
6565

66-
if (!authContext.credentials.username) {
66+
// If a custom credential provider is present we will use that first.
67+
if (this.credentialProvider || !authContext.credentials.username) {
6768
authContext.credentials = await makeTempCredentials(
6869
authContext.credentials,
6970
this.credentialFetcher

test/integration/auth/mongodb_aws.test.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as sinon from 'sinon';
88
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
99
import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers';
1010
import {
11+
AWSCredentialProvider,
12+
AWSCredentials,
1113
AWSTemporaryCredentialProvider,
1214
type CommandOptions,
1315
Connection,
@@ -138,27 +140,43 @@ describe('MONGODB-AWS', function () {
138140

139141
context('when user supplies a credentials provider', function () {
140142
let providerCount = 0;
143+
let provider;
144+
145+
before(function () {
146+
const credentials = client.options.credentials;
147+
if (credentials.username) {
148+
// There are 2 variants in our tests that remove the environment variables
149+
// and put the credentials in the URI. In those cases we need a custom
150+
// provider that returns the correct variables by extracting them out.
151+
const awsCredentials: AWSCredentials = {
152+
accessKeyId: credentials.username,
153+
secretAccessKey: credentials.password
154+
};
155+
if (credentials.mechanismProperties.AWS_SESSION_TOKEN) {
156+
awsCredentials.sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN;
157+
}
158+
provider = async () => {
159+
providerCount++;
160+
return awsCredentials;
161+
};
162+
} else {
163+
// @ts-expect-error We intentionally access a protected variable.
164+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
165+
provider = async () => {
166+
providerCount++;
167+
return await credentialProvider.fromNodeProviderChain().apply();
168+
};
169+
}
170+
});
141171

142172
beforeEach(function () {
143173
if (!awsSdkPresent) {
144174
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
145175
return this.skip();
146176
}
147-
// If we have a username the credentials have been set from the URI, options, or environment
148-
// variables per the auth spec stated order.
149-
if (client.options.credentials.username) {
150-
this.skipReason = 'Credentials in the URI on env variables will not use custom provider.';
151-
return this.skip();
152-
}
153177
});
154178

155179
it('authenticates with a user provided credentials provider', async function () {
156-
// @ts-expect-error We intentionally access a protected variable.
157-
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
158-
const provider = async () => {
159-
providerCount++;
160-
return await credentialProvider.fromNodeProviderChain().apply();
161-
};
162180
client = this.configuration.newClient(process.env.MONGODB_URI, {
163181
authMechanismProperties: {
164182
AWS_CREDENTIAL_PROVIDER: provider

0 commit comments

Comments
 (0)