@@ -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,154 @@ 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
+ outcome : null
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
+ outcome : null
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
+ outcome : null
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
+ outcome : '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
+ outcome : '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
+ outcome : null
149
+ } ,
150
+ {
151
+ ctx : 'when AWS_STS_REGIONAL_ENDPOINTS is set to legacy and region is new' ,
152
+ title : 'uses the region from the environment' ,
153
+ env : {
154
+ AWS_STS_REGIONAL_ENDPOINTS : 'legacy' ,
155
+ AWS_REGION : 'sa-east-1'
156
+ } ,
157
+ outcome : null
158
+ }
159
+ ] ;
160
+
161
+ for ( const test of tests ) {
162
+ context ( test . ctx , ( ) => {
163
+ let credentialProvider ;
164
+ let storedEnv ;
165
+ let calledArguments ;
166
+
167
+ const shouldSkip = ( ) => {
168
+ const { AWS_WEB_IDENTITY_TOKEN_FILE = '' } = process . env ;
169
+ credentialProvider = ( ( ) => {
170
+ try {
171
+ return require ( '@aws-sdk/credential-providers' ) ;
172
+ } catch {
173
+ return null ;
174
+ }
175
+ } ) ( ) ;
176
+ return AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 || credentialProvider == null ;
177
+ } ;
178
+
179
+ beforeEach ( function ( ) {
180
+ if ( shouldSkip ( ) ) {
181
+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
182
+ return this . skip ( ) ;
183
+ }
184
+
185
+ client = this . configuration . newClient ( process . env . MONGODB_URI ) ;
186
+
187
+ storedEnv = process . env ;
188
+ if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
189
+ delete process . env . AWS_STS_REGIONAL_ENDPOINTS ;
190
+ } else {
191
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = test . env . AWS_STS_REGIONAL_ENDPOINTS ;
192
+ }
193
+ if ( test . env . AWS_REGION === undefined ) {
194
+ delete process . env . AWS_REGION ;
195
+ } else {
196
+ process . env . AWS_REGION = test . env . AWS_REGION ;
197
+ }
198
+
199
+ calledArguments = [ ] ;
200
+ MongoDBAWS . credentialProvider = {
201
+ fromNodeProviderChain ( ...args ) {
202
+ calledArguments = args ;
203
+ return credentialProvider . fromNodeProviderChain ( ...args ) ;
204
+ }
205
+ } ;
206
+ } ) ;
207
+
208
+ afterEach ( ( ) => {
209
+ if ( shouldSkip ( ) ) {
210
+ return ;
211
+ }
212
+ if ( typeof storedEnv . AWS_STS_REGIONAL_ENDPOINTS === 'string' ) {
213
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = storedEnv . AWS_STS_REGIONAL_ENDPOINTS ;
214
+ }
215
+ if ( typeof storedEnv . AWS_STS_REGIONAL_ENDPOINTS === 'string' ) {
216
+ process . env . AWS_REGION = storedEnv . AWS_REGION ;
217
+ }
218
+ MongoDBAWS . credentialProvider = credentialProvider ;
219
+ calledArguments = [ ] ;
220
+ } ) ;
221
+
222
+ it ( test . title , async function ( ) {
223
+ const result = await client
224
+ . db ( 'aws' )
225
+ . collection ( 'aws_test' )
226
+ . estimatedDocumentCount ( )
227
+ . catch ( error => error ) ;
228
+
229
+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
230
+ expect ( result ) . to . be . a ( 'number' ) ;
231
+
232
+ if ( test . outcome != null ) {
233
+ expect ( calledArguments ) . to . deep . equal ( [ { clientConfig : { region : test . outcome } } ] ) ;
234
+ } else {
235
+ expect ( calledArguments ) . to . deep . equal ( [ ] ) ;
236
+ }
237
+ } ) ;
238
+ } ) ;
239
+ }
240
+ } ) ;
91
241
} ) ;
0 commit comments