@@ -8,7 +8,7 @@ import * as sinon from 'sinon';
88// eslint-disable-next-line @typescript-eslint/no-restricted-imports
99import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers' ;
1010import {
11- AWSTemporaryCredentialProvider ,
11+ AWSSDKCredentialProvider ,
1212 type CommandOptions ,
1313 Connection ,
1414 type Document ,
@@ -41,7 +41,7 @@ describe('MONGODB-AWS', function () {
4141
4242 context ( 'when the AWS SDK is not present' , function ( ) {
4343 beforeEach ( function ( ) {
44- AWSTemporaryCredentialProvider . awsSDK [ 'kModuleError' ] = new MongoMissingDependencyError (
44+ AWSSDKCredentialProvider . awsSDK [ 'kModuleError' ] = new MongoMissingDependencyError (
4545 'Missing dependency @aws-sdk/credential-providers' ,
4646 {
4747 cause : new Error ( ) ,
@@ -51,7 +51,7 @@ describe('MONGODB-AWS', function () {
5151 } ) ;
5252
5353 afterEach ( function ( ) {
54- delete AWSTemporaryCredentialProvider . awsSDK [ 'kModuleError' ] ;
54+ delete AWSSDKCredentialProvider . awsSDK [ 'kModuleError' ] ;
5555 } ) ;
5656
5757 describe ( 'when attempting AWS auth' , function ( ) {
@@ -176,7 +176,7 @@ describe('MONGODB-AWS', function () {
176176 } ) ;
177177
178178 it ( 'authenticates with a user provided credentials provider' , async function ( ) {
179- const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
179+ const credentialProvider = AWSSDKCredentialProvider . awsSDK ;
180180 const provider = async ( ) => {
181181 providerCount ++ ;
182182 return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
@@ -389,7 +389,7 @@ describe('MONGODB-AWS', function () {
389389 return this . skip ( ) ;
390390 }
391391
392- credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
392+ credentialProvider = AWSSDKCredentialProvider . awsSDK ;
393393
394394 storedEnv = process . env ;
395395 if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
@@ -461,46 +461,78 @@ describe('MONGODB-AWS', function () {
461461 } ) ;
462462
463463 describe ( 'AWS KMS Credential Fetching' , function ( ) {
464- context ( 'when a credential provider is not provided' , function ( ) {
465- it ( 'KMS credentials are successfully fetched.' , async function ( ) {
466- const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
464+ context ( 'when the AWS SDK is not installed' , function ( ) {
465+ beforeEach ( function ( ) {
466+ AWSSDKCredentialProvider . awsSDK [ 'kModuleError' ] = new MongoMissingDependencyError (
467+ 'Missing dependency @aws-sdk/credential-providers' ,
468+ {
469+ cause : new Error ( ) ,
470+ dependencyName : '@aws-sdk/credential-providers'
471+ }
472+ ) ;
473+ } ) ;
467474
468- expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
469- expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
475+ afterEach ( function ( ) {
476+ delete AWSSDKCredentialProvider . awsSDK [ 'kModuleError' ] ;
477+ } ) ;
478+
479+ it ( 'fetching AWS KMS credentials throws an error' , async function ( ) {
480+ const result = await refreshKMSCredentials ( { aws : { } } ) . catch ( e => e ) ;
481+
482+ // TODO(NODE-7046): Remove branch when removing support for AWS credentials in URI.
483+ // The drivers tools scripts put the credentials in the URI currently for some environments,
484+ // this will need to change when doing the DRIVERS-3131 work.
485+ if ( ! client . options . credentials . username ) {
486+ expect ( result ) . to . be . instanceof ( MongoAWSError ) ;
487+ expect ( result . message ) . to . match ( / c r e d e n t i a l - p r o v i d e r s / ) ;
488+ } else {
489+ expect ( result ) . to . equal ( 0 ) ;
490+ }
470491 } ) ;
471492 } ) ;
472493
473- context ( 'when a credential provider is provided' , function ( ) {
474- let credentialProvider ;
475- let providerCount = 0 ;
494+ context ( 'when the AWS SDK is installed' , function ( ) {
495+ context ( 'when a credential provider is not provided' , function ( ) {
496+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
497+ const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
476498
477- beforeEach ( function ( ) {
478- const provider = AWSTemporaryCredentialProvider . awsSDK ;
479- credentialProvider = async ( ) => {
480- providerCount ++ ;
481- return await provider . fromNodeProviderChain ( ) . apply ( ) ;
482- } ;
499+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
500+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
501+ } ) ;
483502 } ) ;
484503
485- it ( 'KMS credentials are successfully fetched.' , async function ( ) {
486- const { aws } = await refreshKMSCredentials ( { aws : { } } , { aws : credentialProvider } ) ;
504+ context ( 'when a credential provider is provided' , function ( ) {
505+ let credentialProvider ;
506+ let providerCount = 0 ;
487507
488- expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
489- expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
490- expect ( providerCount ) . to . be . greaterThan ( 0 ) ;
508+ beforeEach ( function ( ) {
509+ const provider = AWSSDKCredentialProvider . awsSDK ;
510+ credentialProvider = async ( ) => {
511+ providerCount ++ ;
512+ return await provider . fromNodeProviderChain ( ) . apply ( ) ;
513+ } ;
514+ } ) ;
515+
516+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
517+ const { aws } = await refreshKMSCredentials ( { aws : { } } , { aws : credentialProvider } ) ;
518+
519+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
520+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
521+ expect ( providerCount ) . to . be . greaterThan ( 0 ) ;
522+ } ) ;
491523 } ) ;
492- } ) ;
493524
494- it ( 'does not return any extra keys for the `aws` credential provider' , async function ( ) {
495- const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
525+ it ( 'does not return any extra keys for the `aws` credential provider' , async function ( ) {
526+ const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
496527
497- const keys = new Set ( Object . keys ( aws ?? { } ) ) ;
498- const allowedKeys = [ 'accessKeyId' , 'secretAccessKey' , 'sessionToken' ] ;
528+ const keys = new Set ( Object . keys ( aws ?? { } ) ) ;
529+ const allowedKeys = [ 'accessKeyId' , 'secretAccessKey' , 'sessionToken' ] ;
499530
500- expect (
501- Array . from ( setDifference ( keys , allowedKeys ) ) ,
502- 'received an unexpected key in the response refreshing KMS credentials'
503- ) . to . deep . equal ( [ ] ) ;
531+ expect (
532+ Array . from ( setDifference ( keys , allowedKeys ) ) ,
533+ 'received an unexpected key in the response refreshing KMS credentials'
534+ ) . to . deep . equal ( [ ] ) ;
535+ } ) ;
504536 } ) ;
505537 } ) ;
506538} ) ;
0 commit comments