Skip to content

Commit 381a5f3

Browse files
committed
feat(NODE-7047): use custom credential provider first
1 parent b7c6750 commit 381a5f3

File tree

2 files changed

+151
-28
lines changed

2 files changed

+151
-28
lines changed

test/integration/auth/mongodb_aws.test.ts

Lines changed: 106 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -163,39 +163,117 @@ describe('MONGODB-AWS', function () {
163163
});
164164
});
165165

166-
context('when user supplies a credentials provider', function () {
167-
let providerCount = 0;
166+
context('when using a custom credential provider', function () {
167+
context('1. Custom Credential Provider Authenticates', function () {
168+
let providerCount = 0;
168169

169-
beforeEach(function () {
170-
// If we have a username the credentials have been set from the URI, options, or environment
171-
// variables per the auth spec stated order.
172-
if (client.options.credentials.username) {
173-
this.skipReason = 'Credentials in the URI on env variables will not use custom provider.';
174-
return this.skip();
175-
}
170+
beforeEach(function () {
171+
// If we have a username the credentials have been set from the URI, options, or environment
172+
// variables per the auth spec stated order.
173+
if (client.options.credentials.username) {
174+
this.skipReason = 'Credentials in the URI will not use custom provider.';
175+
return this.skip();
176+
}
177+
});
178+
179+
it('authenticates with a user provided credentials provider', async function () {
180+
const credentialProvider = AWSSDKCredentialProvider.awsSDK;
181+
const provider = async () => {
182+
providerCount++;
183+
return await credentialProvider.fromNodeProviderChain().apply();
184+
};
185+
client = this.configuration.newClient(process.env.MONGODB_URI, {
186+
authMechanismProperties: {
187+
AWS_CREDENTIAL_PROVIDER: provider
188+
}
189+
});
190+
191+
const result = await client
192+
.db('aws')
193+
.collection('aws_test')
194+
.estimatedDocumentCount()
195+
.catch(error => error);
196+
197+
expect(result).to.not.be.instanceOf(MongoServerError);
198+
expect(result).to.be.a('number');
199+
expect(providerCount).to.be.greaterThan(0);
200+
});
176201
});
177202

178-
it('authenticates with a user provided credentials provider', async function () {
179-
const credentialProvider = AWSSDKCredentialProvider.awsSDK;
180-
const provider = async () => {
181-
providerCount++;
182-
return await credentialProvider.fromNodeProviderChain().apply();
183-
};
184-
client = this.configuration.newClient(process.env.MONGODB_URI, {
185-
authMechanismProperties: {
186-
AWS_CREDENTIAL_PROVIDER: provider
187-
}
203+
context('2. Custom Credential Provider Authentication Precedence', function () {
204+
context('Case 1: Credentials in URI Take Precedence', function () {
205+
let providerCount = 0;
206+
let provider;
207+
208+
beforeEach(function () {
209+
console.log(client?.options);
210+
if (!client?.options.credentials.username) {
211+
this.skipReason = 'Test only runs when credentials are present in the URI';
212+
return this.skip();
213+
}
214+
// @ts-expect-error We intentionally access a protected variable.
215+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
216+
provider = async () => {
217+
providerCount++;
218+
return await credentialProvider.fromNodeProviderChain().apply();
219+
};
220+
});
221+
222+
it('authenticates with a user provided credentials provider', async function () {
223+
console.log(process.env);
224+
client = this.configuration.newClient(process.env.MONGODB_URI, {
225+
authMechanismProperties: {
226+
AWS_CREDENTIAL_PROVIDER: provider
227+
}
228+
});
229+
230+
const result = await client
231+
.db('aws')
232+
.collection('aws_test')
233+
.estimatedDocumentCount()
234+
.catch(error => error);
235+
236+
expect(result).to.not.be.instanceOf(MongoServerError);
237+
expect(result).to.be.a('number');
238+
expect(providerCount).to.equal(0);
239+
});
188240
});
189241

190-
const result = await client
191-
.db('aws')
192-
.collection('aws_test')
193-
.estimatedDocumentCount()
194-
.catch(error => error);
242+
context('Case 2: Custom Provider Takes Precedence Over Environment Variables', function () {
243+
let providerCount = 0;
244+
let provider;
195245

196-
expect(result).to.not.be.instanceOf(MongoServerError);
197-
expect(result).to.be.a('number');
198-
expect(providerCount).to.be.greaterThan(0);
246+
beforeEach(function () {
247+
if (client?.options.credentials.username || !process.env.AWS_ACCESS_KEY_ID) {
248+
this.skipReason = 'Test only runs when credentials are present in the environment';
249+
return this.skip();
250+
}
251+
// @ts-expect-error We intentionally access a protected variable.
252+
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
253+
provider = async () => {
254+
providerCount++;
255+
return await credentialProvider.fromNodeProviderChain().apply();
256+
};
257+
});
258+
259+
it('authenticates with a user provided credentials provider', async function () {
260+
client = this.configuration.newClient(process.env.MONGODB_URI, {
261+
authMechanismProperties: {
262+
AWS_CREDENTIAL_PROVIDER: provider
263+
}
264+
});
265+
266+
const result = await client
267+
.db('aws')
268+
.collection('aws_test')
269+
.estimatedDocumentCount()
270+
.catch(error => error);
271+
272+
expect(result).to.not.be.instanceOf(MongoServerError);
273+
expect(result).to.be.a('number');
274+
expect(providerCount).to.be.greaterThan(0);
275+
});
276+
});
199277
});
200278
});
201279

@@ -218,7 +296,7 @@ describe('MONGODB-AWS', function () {
218296
.catch(error => error);
219297

220298
expect(client).to.have.nested.property('s.authProviders');
221-
const provider = client.s.authProviders.getOrCreateProvider('MONGODB-AWS');
299+
const provider = client.s.authProviders.getOrCreateProvider('MONGODB-AWS', {});
222300
expect(provider).to.be.instanceOf(MongoDBAWS);
223301
});
224302

test/integration/client-side-encryption/client_side_encryption.prose.26.custom_aws_credential_providers.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,49 @@ describe('26. Custom AWS Credential Providers', metadata, () => {
102102
});
103103
}
104104
);
105+
106+
context(
107+
'ClientEncryption with credentialProviders and valid environment variables',
108+
metadata,
109+
function () {
110+
let clientEncryption;
111+
let providerCount = 0;
112+
let previousAccessKey;
113+
let previousSecretKey;
114+
115+
beforeEach(function () {
116+
previousAccessKey = process.env.AWS_ACCESS_KEY_ID;
117+
previousSecretKey = process.env.AWS_SECRET_ACCESS_KEY;
118+
process.env.AWS_ACCESS_KEY_ID = process.env.FLE_AWS_KEY;
119+
process.env.AWS_SECRET_ACCESS_KEY = process.env.FLE_AWS_SECRET;
120+
121+
const options = {
122+
keyVaultNamespace: 'keyvault.datakeys',
123+
kmsProviders: { aws: {} },
124+
credentialProviders: {
125+
aws: async () => {
126+
providerCount++;
127+
return {
128+
accessKeyId: process.env.FLE_AWS_KEY,
129+
secretAccessKey: process.env.FLE_AWS_SECRET
130+
};
131+
}
132+
},
133+
extraOptions: getEncryptExtraOptions()
134+
};
135+
clientEncryption = new ClientEncryption(keyVaultClient, options);
136+
});
137+
138+
afterEach(function () {
139+
process.env.AWS_ACCESS_KEY_ID = previousAccessKey;
140+
process.env.AWS_SECRET_ACCESS_KEY = previousSecretKey;
141+
});
142+
143+
it('is successful', metadata, async function () {
144+
const dk = await clientEncryption.createDataKey('aws', { masterKey });
145+
expect(dk).to.be.instanceOf(Binary);
146+
expect(providerCount).to.be.greaterThan(0);
147+
});
148+
}
149+
);
105150
});

0 commit comments

Comments
 (0)