@@ -136,6 +136,47 @@ describe('MONGODB-AWS', function () {
136
136
} ) ;
137
137
} ) ;
138
138
139
+ context ( 'when user supplies a credentials provider' , function ( ) {
140
+ let providerCount = 0 ;
141
+
142
+ beforeEach ( function ( ) {
143
+ if ( ! awsSdkPresent ) {
144
+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
145
+ return this . skip ( ) ;
146
+ }
147
+ // If we have a username the credentials have been set from the URI, options, or environment
148
+ // variables per the auth spec stated order.
149
+ if ( client . options . credentials . username ) {
150
+ this . skipReason = 'Credentials in the URI on env variables will not use custom provider.' ;
151
+ return this . skip ( ) ;
152
+ }
153
+ } ) ;
154
+
155
+ it ( 'authenticates with a user provided credentials provider' , async function ( ) {
156
+ // @ts -expect-error We intentionally access a protected variable.
157
+ const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
158
+ const provider = async ( ) => {
159
+ providerCount ++ ;
160
+ return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
161
+ } ;
162
+ client = this . configuration . newClient ( process . env . MONGODB_URI , {
163
+ authMechanismProperties : {
164
+ AWS_CREDENTIAL_PROVIDER : provider
165
+ }
166
+ } ) ;
167
+
168
+ const result = await client
169
+ . db ( 'aws' )
170
+ . collection ( 'aws_test' )
171
+ . estimatedDocumentCount ( )
172
+ . catch ( error => error ) ;
173
+
174
+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
175
+ expect ( result ) . to . be . a ( 'number' ) ;
176
+ expect ( providerCount ) . to . be . greaterThan ( 0 ) ;
177
+ } ) ;
178
+ } ) ;
179
+
139
180
it ( 'should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable' , function ( ) {
140
181
client = this . configuration . newClient ( this . configuration . url ( ) , {
141
182
authMechanismProperties : { AWS_SESSION_TOKEN : '' }
@@ -426,11 +467,36 @@ describe('AWS KMS Credential Fetching', function () {
426
467
: undefined ;
427
468
this . currentTest ?. skipReason && this . skip ( ) ;
428
469
} ) ;
429
- it ( 'KMS credentials are successfully fetched.' , async function ( ) {
430
- const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
431
470
432
- expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
433
- expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
471
+ context ( 'when a credential provider is not provided' , function ( ) {
472
+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
473
+ const { aws } = await refreshKMSCredentials ( { aws : { } } ) ;
474
+
475
+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
476
+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
477
+ } ) ;
478
+ } ) ;
479
+
480
+ context ( 'when a credential provider is provided' , function ( ) {
481
+ let credentialProvider ;
482
+ let providerCount = 0 ;
483
+
484
+ beforeEach ( function ( ) {
485
+ // @ts -expect-error We intentionally access a protected variable.
486
+ const provider = AWSTemporaryCredentialProvider . awsSDK ;
487
+ credentialProvider = async ( ) => {
488
+ providerCount ++ ;
489
+ return await provider . fromNodeProviderChain ( ) . apply ( ) ;
490
+ } ;
491
+ } ) ;
492
+
493
+ it ( 'KMS credentials are successfully fetched.' , async function ( ) {
494
+ const { aws } = await refreshKMSCredentials ( { aws : { } } , { aws : credentialProvider } ) ;
495
+
496
+ expect ( aws ) . to . have . property ( 'accessKeyId' ) ;
497
+ expect ( aws ) . to . have . property ( 'secretAccessKey' ) ;
498
+ expect ( providerCount ) . to . be . greaterThan ( 0 ) ;
499
+ } ) ;
434
500
} ) ;
435
501
436
502
it ( 'does not return any extra keys for the `aws` credential provider' , async function ( ) {
0 commit comments