@@ -5,30 +5,26 @@ import * as http from 'http';
5
5
import { performance } from 'perf_hooks' ;
6
6
import * as sinon from 'sinon' ;
7
7
8
+ // eslint-disable-next-line @typescript-eslint/no-restricted-imports
9
+ import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers' ;
8
10
import {
9
11
AWSTemporaryCredentialProvider ,
10
12
MongoAWSError ,
11
13
type MongoClient ,
12
14
MongoDBAWS ,
13
15
MongoMissingCredentialsError ,
14
- MongoServerError
16
+ MongoServerError ,
17
+ setDifference
15
18
} from '../../mongodb' ;
16
19
17
- function awsSdk ( ) {
18
- try {
19
- return require ( '@aws-sdk/credential-providers' ) ;
20
- } catch {
21
- return null ;
22
- }
23
- }
20
+ const isMongoDBAWSAuthEnvironment = ( process . env . MONGODB_URI ?? '' ) . includes ( 'MONGODB-AWS' ) ;
24
21
25
22
describe ( 'MONGODB-AWS' , function ( ) {
26
23
let awsSdkPresent ;
27
24
let client : MongoClient ;
28
25
29
26
beforeEach ( function ( ) {
30
- const MONGODB_URI = process . env . MONGODB_URI ;
31
- if ( ! MONGODB_URI || MONGODB_URI . indexOf ( 'MONGODB-AWS' ) === - 1 ) {
27
+ if ( ! isMongoDBAWSAuthEnvironment ) {
32
28
this . currentTest . skipReason = 'requires MONGODB_URI to contain MONGODB-AWS auth mechanism' ;
33
29
return this . skip ( ) ;
34
30
}
@@ -39,7 +35,7 @@ describe('MONGODB-AWS', function () {
39
35
`Always inform the AWS tests if they run with or without the SDK (MONGODB_AWS_SDK=${ MONGODB_AWS_SDK } )`
40
36
) . to . include ( MONGODB_AWS_SDK ) ;
41
37
42
- awsSdkPresent = ! ! awsSdk ( ) ;
38
+ awsSdkPresent = AWSTemporaryCredentialProvider . isAWSSDKInstalled ;
43
39
expect (
44
40
awsSdkPresent ,
45
41
MONGODB_AWS_SDK === 'true'
@@ -244,8 +240,10 @@ describe('MONGODB-AWS', function () {
244
240
245
241
const envCheck = ( ) => {
246
242
const { AWS_WEB_IDENTITY_TOKEN_FILE = '' } = process . env ;
247
- credentialProvider = awsSdk ( ) ;
248
- return AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 || credentialProvider == null ;
243
+ return (
244
+ AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 ||
245
+ ! AWSTemporaryCredentialProvider . isAWSSDKInstalled
246
+ ) ;
249
247
} ;
250
248
251
249
beforeEach ( function ( ) {
@@ -255,6 +253,9 @@ describe('MONGODB-AWS', function () {
255
253
return this . skip ( ) ;
256
254
}
257
255
256
+ // @ts -expect-error We intentionally access a protected variable.
257
+ credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
258
+
258
259
storedEnv = process . env ;
259
260
if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
260
261
delete process . env . AWS_STS_REGIONAL_ENDPOINTS ;
@@ -324,3 +325,49 @@ describe('MONGODB-AWS', function () {
324
325
}
325
326
} ) ;
326
327
} ) ;
328
+
329
+ describe ( 'AWS KMS Credential Fetching' , function ( ) {
330
+ context ( 'when the AWS SDK is not installed' , function ( ) {
331
+ beforeEach ( function ( ) {
332
+ this . currentTest . skipReason = ! isMongoDBAWSAuthEnvironment
333
+ ? 'Test must run in an AWS auth testing environment'
334
+ : AWSTemporaryCredentialProvider . isAWSSDKInstalled
335
+ ? 'This test must run in an environment where the AWS SDK is not installed.'
336
+ : undefined ;
337
+ this . currentTest ?. skipReason && this . skip ( ) ;
338
+ } ) ;
339
+ it ( 'fetching AWS KMS credentials throws an error' , async function ( ) {
340
+ const error = await refreshKMSCredentials ( { aws : { } } ) . catch ( e => e ) ;
341
+ expect ( error ) . to . be . instanceOf ( MongoAWSError ) ;
342
+ } ) ;
343
+ } ) ;
344
+
345
+ context ( 'when the AWS SDK is installed' , function ( ) {
346
+ beforeEach ( function ( ) {
347
+ this . currentTest . skipReason = ! isMongoDBAWSAuthEnvironment
348
+ ? 'Test must run in an AWS auth testing environment'
349
+ : ! AWSTemporaryCredentialProvider . isAWSSDKInstalled
350
+ ? 'This test must run in an environment where the AWS SDK is installed.'
351
+ : undefined ;
352
+ this . currentTest ?. skipReason && this . skip ( ) ;
353
+ } ) ;
354
+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
355
+ const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
356
+
357
+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
358
+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
359
+ } ) ;
360
+
361
+ it ( 'does not return any extra keys for the `aws` credential provider' , async function ( ) {
362
+ const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
363
+
364
+ const keys = new Set ( Object . keys ( aws ?? { } ) ) ;
365
+ const allowedKeys = [ 'accessKeyId' , 'secretAccessKey' , 'sessionToken' ] ;
366
+
367
+ expect (
368
+ Array . from ( setDifference ( keys , allowedKeys ) ) ,
369
+ 'received an unexpected key in the response refreshing KMS credentials'
370
+ ) . to . deep . equal ( [ ] ) ;
371
+ } ) ;
372
+ } ) ;
373
+ } ) ;
0 commit comments