Skip to content

Commit 7281459

Browse files
committed
test: 2 last prose tests
1 parent 6d3dc2d commit 7281459

File tree

2 files changed

+95
-30
lines changed

2 files changed

+95
-30
lines changed

src/cmap/auth/mongodb_aws.ts

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

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

test/integration/auth/mongodb_aws.test.ts

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,42 +137,24 @@ describe('MONGODB-AWS', function () {
137137
});
138138
});
139139

140-
context('when user supplies a credentials provider', function () {
140+
context('1. Custom Credential Provider Authenticates', function () {
141141
let providerCount = 0;
142142
let provider;
143143

144-
before(function () {
145-
if (client?.options.credentials.username) {
146-
const credentials = client?.options.credentials;
147-
// There are 2 variants in our tests that remove the environment variables
148-
// and put the credentials in the URI. In those cases we need a custom
149-
// provider that returns the correct variables by extracting them out.
150-
const awsCredentials: AWSCredentials = {
151-
accessKeyId: credentials.username,
152-
secretAccessKey: credentials.password
153-
};
154-
if (credentials.mechanismProperties.AWS_SESSION_TOKEN) {
155-
awsCredentials.sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN;
156-
}
157-
provider = async () => {
158-
providerCount++;
159-
return awsCredentials;
160-
};
161-
} else {
162-
// @ts-expect-error We intentionally access a protected variable.
163-
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
164-
provider = async () => {
165-
providerCount++;
166-
return await credentialProvider.fromNodeProviderChain().apply();
167-
};
168-
}
169-
});
170-
171144
beforeEach(function () {
172145
if (!awsSdkPresent) {
173146
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
174147
return this.skip();
175148
}
149+
if (client?.options.credentials.username) {
150+
this.skipReason = 'only relevant when no credentials are in the URI';
151+
return this.skip();
152+
}
153+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
154+
provider = async () => {
155+
providerCount++;
156+
return await credentialProvider.fromNodeProviderChain().apply();
157+
};
176158
});
177159

178160
it('authenticates with a user provided credentials provider', async function () {
@@ -194,6 +176,90 @@ describe('MONGODB-AWS', function () {
194176
});
195177
});
196178

179+
context('2. Custom Credential Provider Authentication Precedence', function () {
180+
context('Case 1: Credentials in URI Take Precedence', function () {
181+
let providerCount = 0;
182+
let provider;
183+
184+
beforeEach(function () {
185+
if (!awsSdkPresent) {
186+
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
187+
return this.skip();
188+
}
189+
console.log(client?.options);
190+
if (!client?.options.credentials.username) {
191+
this.skipReason = 'Test only runs when credentials are present in the URI';
192+
return this.skip();
193+
}
194+
// @ts-expect-error We intentionally access a protected variable.
195+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
196+
provider = async () => {
197+
providerCount++;
198+
return await credentialProvider.fromNodeProviderChain().apply();
199+
};
200+
});
201+
202+
it('authenticates with a user provided credentials provider', async function () {
203+
console.log(process.env);
204+
client = this.configuration.newClient(process.env.MONGODB_URI, {
205+
authMechanismProperties: {
206+
AWS_CREDENTIAL_PROVIDER: provider
207+
}
208+
});
209+
210+
const result = await client
211+
.db('aws')
212+
.collection('aws_test')
213+
.estimatedDocumentCount()
214+
.catch(error => error);
215+
216+
expect(result).to.not.be.instanceOf(MongoServerError);
217+
expect(result).to.be.a('number');
218+
expect(providerCount).to.equal(0);
219+
});
220+
});
221+
222+
context('Case 2: Custom Provider Takes Precedence Over Environment Variables', function () {
223+
let providerCount = 0;
224+
let provider;
225+
226+
beforeEach(function () {
227+
if (!awsSdkPresent) {
228+
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
229+
return this.skip();
230+
}
231+
if (client?.options.credentials.username || !process.env.AWS_ACCESS_KEY_ID) {
232+
this.skipReason = 'Test only runs when credentials are present in the environment';
233+
return this.skip();
234+
}
235+
// @ts-expect-error We intentionally access a protected variable.
236+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
237+
provider = async () => {
238+
providerCount++;
239+
return await credentialProvider.fromNodeProviderChain().apply();
240+
};
241+
});
242+
243+
it('authenticates with a user provided credentials provider', async function () {
244+
client = this.configuration.newClient(process.env.MONGODB_URI, {
245+
authMechanismProperties: {
246+
AWS_CREDENTIAL_PROVIDER: provider
247+
}
248+
});
249+
250+
const result = await client
251+
.db('aws')
252+
.collection('aws_test')
253+
.estimatedDocumentCount()
254+
.catch(error => error);
255+
256+
expect(result).to.not.be.instanceOf(MongoServerError);
257+
expect(result).to.be.a('number');
258+
expect(providerCount).to.be.greaterThan(0);
259+
});
260+
});
261+
});
262+
197263
it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function () {
198264
client = this.configuration.newClient(this.configuration.url(), {
199265
authMechanismProperties: { AWS_SESSION_TOKEN: '' }

0 commit comments

Comments
 (0)