@@ -8,6 +8,8 @@ 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+ AWSCredentialProvider ,
12+ AWSCredentials ,
1113 AWSTemporaryCredentialProvider ,
1214 type CommandOptions ,
1315 Connection ,
@@ -138,27 +140,43 @@ describe('MONGODB-AWS', function () {
138140
139141 context ( 'when user supplies a credentials provider' , function ( ) {
140142 let providerCount = 0 ;
143+ let provider ;
144+
145+ before ( function ( ) {
146+ const credentials = client . options . credentials ;
147+ if ( credentials . username ) {
148+ // There are 2 variants in our tests that remove the environment variables
149+ // and put the credentials in the URI. In those cases we need a custom
150+ // provider that returns the correct variables by extracting them out.
151+ const awsCredentials : AWSCredentials = {
152+ accessKeyId : credentials . username ,
153+ secretAccessKey : credentials . password
154+ } ;
155+ if ( credentials . mechanismProperties . AWS_SESSION_TOKEN ) {
156+ awsCredentials . sessionToken = credentials . mechanismProperties . AWS_SESSION_TOKEN ;
157+ }
158+ provider = async ( ) => {
159+ providerCount ++ ;
160+ return awsCredentials ;
161+ } ;
162+ } else {
163+ // @ts -expect-error We intentionally access a protected variable.
164+ const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
165+ provider = async ( ) => {
166+ providerCount ++ ;
167+ return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
168+ } ;
169+ }
170+ } ) ;
141171
142172 beforeEach ( function ( ) {
143173 if ( ! awsSdkPresent ) {
144174 this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
145175 return this . skip ( ) ;
146176 }
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- }
153177 } ) ;
154178
155179 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- } ;
162180 client = this . configuration . newClient ( process . env . MONGODB_URI , {
163181 authMechanismProperties : {
164182 AWS_CREDENTIAL_PROVIDER : provider
0 commit comments