@@ -5,6 +5,7 @@ import * as http from 'http';
55import { performance } from 'perf_hooks' ;
66import * as sinon from 'sinon' ;
77
8+ import { KMSCredentialProvider } from '../../../src/client-side-encryption/providers' ;
89import {
910 AWSTemporaryCredentialProvider ,
1011 MongoAWSError ,
@@ -14,14 +15,6 @@ import {
1415 MongoServerError
1516} from '../../mongodb' ;
1617
17- function awsSdk ( ) {
18- try {
19- return require ( '@aws-sdk/credential-providers' ) ;
20- } catch {
21- return null ;
22- }
23- }
24-
2518describe ( 'MONGODB-AWS' , function ( ) {
2619 let awsSdkPresent ;
2720 let client : MongoClient ;
@@ -39,7 +32,7 @@ describe('MONGODB-AWS', function () {
3932 `Always inform the AWS tests if they run with or without the SDK (MONGODB_AWS_SDK=${ MONGODB_AWS_SDK } )`
4033 ) . to . include ( MONGODB_AWS_SDK ) ;
4134
42- awsSdkPresent = ! ! awsSdk ( ) ;
35+ awsSdkPresent = AWSTemporaryCredentialProvider . isAWSSDKInstalled ;
4336 expect (
4437 awsSdkPresent ,
4538 MONGODB_AWS_SDK === 'true'
@@ -244,8 +237,10 @@ describe('MONGODB-AWS', function () {
244237
245238 const envCheck = ( ) => {
246239 const { AWS_WEB_IDENTITY_TOKEN_FILE = '' } = process . env ;
247- credentialProvider = awsSdk ( ) ;
248- return AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 || credentialProvider == null ;
240+ return (
241+ AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 ||
242+ ! AWSTemporaryCredentialProvider . isAWSSDKInstalled
243+ ) ;
249244 } ;
250245
251246 beforeEach ( function ( ) {
@@ -255,6 +250,9 @@ describe('MONGODB-AWS', function () {
255250 return this . skip ( ) ;
256251 }
257252
253+ // @ts -expect-error We intentionally access a protected variable.
254+ credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
255+
258256 storedEnv = process . env ;
259257 if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
260258 delete process . env . AWS_STS_REGIONAL_ENDPOINTS ;
@@ -324,3 +322,37 @@ describe('MONGODB-AWS', function () {
324322 }
325323 } ) ;
326324} ) ;
325+
326+ describe ( 'AWS KMS Credential Fetching' , function ( ) {
327+ context ( 'when the AWS SDK is not installed' , function ( ) {
328+ beforeEach ( function ( ) {
329+ if ( AWSTemporaryCredentialProvider . isAWSSDKInstalled ) {
330+ this . currentTest . skipReason =
331+ 'This test must run in an environment where the AWS SDK is not installed.' ;
332+ this . skip ( ) ;
333+ }
334+ } ) ;
335+ it ( 'fetching AWS KMS credentials throws an error' , async function ( ) {
336+ const error = await new KMSCredentialProvider ( { aws : { } } ) . refreshCredentials ( ) . catch ( e => e ) ;
337+
338+ expect ( error ) . to . be . instanceOf ( MongoAWSError ) ;
339+ } ) ;
340+ } ) ;
341+
342+ context ( 'when the AWS SDK is installed' , function ( ) {
343+ beforeEach ( function ( ) {
344+ if ( ! AWSTemporaryCredentialProvider . isAWSSDKInstalled ) {
345+ this . currentTest . skipReason =
346+ 'This test must run in an environment where the AWS SDK is installed.' ;
347+ this . skip ( ) ;
348+ }
349+ } ) ;
350+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
351+ const { aws } = await new KMSCredentialProvider ( { aws : { } } ) . refreshCredentials ( ) ;
352+
353+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
354+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
355+ expect ( aws ) . to . have . property ( 'sessionToken' ) ;
356+ } ) ;
357+ } ) ;
358+ } ) ;
0 commit comments