@@ -3,7 +3,7 @@ import * as http from 'http';
3
3
import { performance } from 'perf_hooks' ;
4
4
import * as sinon from 'sinon' ;
5
5
6
- import { MongoAWSError , type MongoClient , MongoServerError } from '../../mongodb' ;
6
+ import { MongoAWSError , type MongoClient , MongoDBAWS , MongoServerError } from '../../mongodb' ;
7
7
8
8
describe ( 'MONGODB-AWS' , function ( ) {
9
9
let client : MongoClient ;
@@ -88,4 +88,152 @@ describe('MONGODB-AWS', function () {
88
88
expect ( timeTaken ) . to . be . below ( 12000 ) ;
89
89
} ) ;
90
90
} ) ;
91
+
92
+ describe ( 'when using AssumeRoleWithWebIdentity' , ( ) => {
93
+ const tests = [
94
+ {
95
+ ctx : 'when no AWS region settings are set' ,
96
+ title : 'uses the default region' ,
97
+ env : {
98
+ AWS_STS_REGIONAL_ENDPOINTS : undefined ,
99
+ AWS_REGION : undefined
100
+ } ,
101
+ calledWith : [ ]
102
+ } ,
103
+ {
104
+ ctx : 'when only AWS_STS_REGIONAL_ENDPOINTS is set' ,
105
+ title : 'uses the default region' ,
106
+ env : {
107
+ AWS_STS_REGIONAL_ENDPOINTS : 'regional' ,
108
+ AWS_REGION : undefined
109
+ } ,
110
+ calledWith : [ ]
111
+ } ,
112
+ {
113
+ ctx : 'when only AWS_REGION is set' ,
114
+ title : 'uses the default region' ,
115
+ env : {
116
+ AWS_STS_REGIONAL_ENDPOINTS : undefined ,
117
+ AWS_REGION : 'us-west-2'
118
+ } ,
119
+ calledWith : [ ]
120
+ } ,
121
+
122
+ {
123
+ ctx : 'when AWS_STS_REGIONAL_ENDPOINTS is set to regional and region is legacy' ,
124
+ title : 'uses the region from the environment' ,
125
+ env : {
126
+ AWS_STS_REGIONAL_ENDPOINTS : 'regional' ,
127
+ AWS_REGION : 'us-west-2'
128
+ } ,
129
+ calledWith : [ { clientConfig : { region : 'us-west-2' } } ]
130
+ } ,
131
+ {
132
+ ctx : 'when AWS_STS_REGIONAL_ENDPOINTS is set to regional and region is new' ,
133
+ title : 'uses the region from the environment' ,
134
+ env : {
135
+ AWS_STS_REGIONAL_ENDPOINTS : 'regional' ,
136
+ AWS_REGION : 'sa-east-1'
137
+ } ,
138
+ calledWith : [ { clientConfig : { region : 'sa-east-1' } } ]
139
+ } ,
140
+
141
+ {
142
+ ctx : 'when AWS_STS_REGIONAL_ENDPOINTS is set to legacy and region is legacy' ,
143
+ title : 'uses the region from the environment' ,
144
+ env : {
145
+ AWS_STS_REGIONAL_ENDPOINTS : 'legacy' ,
146
+ AWS_REGION : 'us-west-2'
147
+ } ,
148
+ calledWith : [ ]
149
+ } ,
150
+ {
151
+ ctx : 'when AWS_STS_REGIONAL_ENDPOINTS is set to legacy and region is new' ,
152
+ title : 'uses the default region' ,
153
+ env : {
154
+ AWS_STS_REGIONAL_ENDPOINTS : 'legacy' ,
155
+ AWS_REGION : 'sa-east-1'
156
+ } ,
157
+ calledWith : [ ]
158
+ }
159
+ ] ;
160
+
161
+ for ( const test of tests ) {
162
+ context ( test . ctx , ( ) => {
163
+ let credentialProvider ;
164
+ let storedEnv ;
165
+ let calledArguments ;
166
+ let shouldSkip = false ;
167
+
168
+ const envCheck = ( ) => {
169
+ const { AWS_WEB_IDENTITY_TOKEN_FILE = '' } = process . env ;
170
+ credentialProvider = ( ( ) => {
171
+ try {
172
+ return require ( '@aws-sdk/credential-providers' ) ;
173
+ } catch {
174
+ return null ;
175
+ }
176
+ } ) ( ) ;
177
+ return AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 || credentialProvider == null ;
178
+ } ;
179
+
180
+ beforeEach ( function ( ) {
181
+ shouldSkip = envCheck ( ) ;
182
+ if ( shouldSkip ) {
183
+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
184
+ return this . skip ( ) ;
185
+ }
186
+
187
+ client = this . configuration . newClient ( process . env . MONGODB_URI ) ;
188
+
189
+ storedEnv = process . env ;
190
+ if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
191
+ delete process . env . AWS_STS_REGIONAL_ENDPOINTS ;
192
+ } else {
193
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = test . env . AWS_STS_REGIONAL_ENDPOINTS ;
194
+ }
195
+ if ( test . env . AWS_REGION === undefined ) {
196
+ delete process . env . AWS_REGION ;
197
+ } else {
198
+ process . env . AWS_REGION = test . env . AWS_REGION ;
199
+ }
200
+
201
+ calledArguments = [ ] ;
202
+ MongoDBAWS . credentialProvider = {
203
+ fromNodeProviderChain ( ...args ) {
204
+ calledArguments = args ;
205
+ return credentialProvider . fromNodeProviderChain ( ...args ) ;
206
+ }
207
+ } ;
208
+ } ) ;
209
+
210
+ afterEach ( ( ) => {
211
+ if ( shouldSkip ) {
212
+ return ;
213
+ }
214
+ if ( typeof storedEnv . AWS_STS_REGIONAL_ENDPOINTS === 'string' ) {
215
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = storedEnv . AWS_STS_REGIONAL_ENDPOINTS ;
216
+ }
217
+ if ( typeof storedEnv . AWS_STS_REGIONAL_ENDPOINTS === 'string' ) {
218
+ process . env . AWS_REGION = storedEnv . AWS_REGION ;
219
+ }
220
+ MongoDBAWS . credentialProvider = credentialProvider ;
221
+ calledArguments = [ ] ;
222
+ } ) ;
223
+
224
+ it ( test . title , async function ( ) {
225
+ const result = await client
226
+ . db ( 'aws' )
227
+ . collection ( 'aws_test' )
228
+ . estimatedDocumentCount ( )
229
+ . catch ( error => error ) ;
230
+
231
+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
232
+ expect ( result ) . to . be . a ( 'number' ) ;
233
+
234
+ expect ( calledArguments ) . to . deep . equal ( test . calledWith ) ;
235
+ } ) ;
236
+ } ) ;
237
+ }
238
+ } ) ;
91
239
} ) ;
0 commit comments