@@ -137,42 +137,24 @@ describe('MONGODB-AWS', function () {
137137 } ) ;
138138 } ) ;
139139
140- context ( 'when user supplies a credentials provider ' , function ( ) {
140+ context ( '1. Custom Credential Provider Authenticates ' , function ( ) {
141141 let providerCount = 0 ;
142142 let provider ;
143143
144- before ( function ( ) {
145- if ( client ?. options . credentials . username ) {
146- const credentials = client ?. options . credentials ;
147- // There are 2 variants in our tests that remove the environment variables
148- // and put the credentials in the URI. In those cases we need a custom
149- // provider that returns the correct variables by extracting them out.
150- const awsCredentials : AWSCredentials = {
151- accessKeyId : credentials . username ,
152- secretAccessKey : credentials . password
153- } ;
154- if ( credentials . mechanismProperties . AWS_SESSION_TOKEN ) {
155- awsCredentials . sessionToken = credentials . mechanismProperties . AWS_SESSION_TOKEN ;
156- }
157- provider = async ( ) => {
158- providerCount ++ ;
159- return awsCredentials ;
160- } ;
161- } else {
162- // @ts -expect-error We intentionally access a protected variable.
163- const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
164- provider = async ( ) => {
165- providerCount ++ ;
166- return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
167- } ;
168- }
169- } ) ;
170-
171144 beforeEach ( function ( ) {
172145 if ( ! awsSdkPresent ) {
173146 this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
174147 return this . skip ( ) ;
175148 }
149+ if ( client ?. options . credentials . username ) {
150+ this . skipReason = 'only relevant when no credentials are in the URI' ;
151+ return this . skip ( ) ;
152+ }
153+ const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
154+ provider = async ( ) => {
155+ providerCount ++ ;
156+ return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
157+ } ;
176158 } ) ;
177159
178160 it ( 'authenticates with a user provided credentials provider' , async function ( ) {
@@ -194,6 +176,90 @@ describe('MONGODB-AWS', function () {
194176 } ) ;
195177 } ) ;
196178
179+ context ( '2. Custom Credential Provider Authentication Precedence' , function ( ) {
180+ context ( 'Case 1: Credentials in URI Take Precedence' , function ( ) {
181+ let providerCount = 0 ;
182+ let provider ;
183+
184+ beforeEach ( function ( ) {
185+ if ( ! awsSdkPresent ) {
186+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
187+ return this . skip ( ) ;
188+ }
189+ console . log ( client ?. options ) ;
190+ if ( ! client ?. options . credentials . username ) {
191+ this . skipReason = 'Test only runs when credentials are present in the URI' ;
192+ return this . skip ( ) ;
193+ }
194+ // @ts -expect-error We intentionally access a protected variable.
195+ const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
196+ provider = async ( ) => {
197+ providerCount ++ ;
198+ return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
199+ } ;
200+ } ) ;
201+
202+ it ( 'authenticates with a user provided credentials provider' , async function ( ) {
203+ console . log ( process . env ) ;
204+ client = this . configuration . newClient ( process . env . MONGODB_URI , {
205+ authMechanismProperties : {
206+ AWS_CREDENTIAL_PROVIDER : provider
207+ }
208+ } ) ;
209+
210+ const result = await client
211+ . db ( 'aws' )
212+ . collection ( 'aws_test' )
213+ . estimatedDocumentCount ( )
214+ . catch ( error => error ) ;
215+
216+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
217+ expect ( result ) . to . be . a ( 'number' ) ;
218+ expect ( providerCount ) . to . equal ( 0 ) ;
219+ } ) ;
220+ } ) ;
221+
222+ context ( 'Case 2: Custom Provider Takes Precedence Over Environment Variables' , function ( ) {
223+ let providerCount = 0 ;
224+ let provider ;
225+
226+ beforeEach ( function ( ) {
227+ if ( ! awsSdkPresent ) {
228+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
229+ return this . skip ( ) ;
230+ }
231+ if ( client ?. options . credentials . username || ! process . env . AWS_ACCESS_KEY_ID ) {
232+ this . skipReason = 'Test only runs when credentials are present in the environment' ;
233+ return this . skip ( ) ;
234+ }
235+ // @ts -expect-error We intentionally access a protected variable.
236+ const credentialProvider = AWSTemporaryCredentialProvider . awsSDK ;
237+ provider = async ( ) => {
238+ providerCount ++ ;
239+ return await credentialProvider . fromNodeProviderChain ( ) . apply ( ) ;
240+ } ;
241+ } ) ;
242+
243+ it ( 'authenticates with a user provided credentials provider' , async function ( ) {
244+ client = this . configuration . newClient ( process . env . MONGODB_URI , {
245+ authMechanismProperties : {
246+ AWS_CREDENTIAL_PROVIDER : provider
247+ }
248+ } ) ;
249+
250+ const result = await client
251+ . db ( 'aws' )
252+ . collection ( 'aws_test' )
253+ . estimatedDocumentCount ( )
254+ . catch ( error => error ) ;
255+
256+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
257+ expect ( result ) . to . be . a ( 'number' ) ;
258+ expect ( providerCount ) . to . be . greaterThan ( 0 ) ;
259+ } ) ;
260+ } ) ;
261+ } ) ;
262+
197263 it ( 'should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable' , function ( ) {
198264 client = this . configuration . newClient ( this . configuration . url ( ) , {
199265 authMechanismProperties : { AWS_SESSION_TOKEN : '' }
0 commit comments