@@ -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,144 @@ 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
+ beforeEach ( function ( ) {
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
+
177
+ if ( AWS_WEB_IDENTITY_TOKEN_FILE . length === 0 || credentialProvider == null ) {
178
+ this . skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed' ;
179
+ return this . skip ( ) ;
180
+ }
181
+
182
+ client = this . configuration . newClient ( process . env . MONGODB_URI ) ;
183
+
184
+ storedEnv = process . env ;
185
+ if ( test . env . AWS_STS_REGIONAL_ENDPOINTS === undefined ) {
186
+ delete process . env . AWS_STS_REGIONAL_ENDPOINTS ;
187
+ } else {
188
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = test . env . AWS_STS_REGIONAL_ENDPOINTS ;
189
+ }
190
+ if ( test . env . AWS_REGION === undefined ) {
191
+ delete process . env . AWS_REGION ;
192
+ } else {
193
+ process . env . AWS_REGION = test . env . AWS_REGION ;
194
+ }
195
+
196
+ calledArguments = [ ] ;
197
+ MongoDBAWS . credentialProvider = {
198
+ fromNodeProviderChain ( ...args ) {
199
+ calledArguments = args ;
200
+ return credentialProvider . fromNodeProviderChain ( ...args ) ;
201
+ }
202
+ } ;
203
+ } ) ;
204
+
205
+ afterEach ( ( ) => {
206
+ process . env . AWS_STS_REGIONAL_ENDPOINTS = storedEnv . AWS_STS_REGIONAL_ENDPOINTS ;
207
+ process . env . AWS_REGION = storedEnv . AWS_REGION ;
208
+ MongoDBAWS . credentialProvider = credentialProvider ;
209
+ calledArguments = [ ] ;
210
+ } ) ;
211
+
212
+ it ( test . title , async function ( ) {
213
+ const result = await client
214
+ . db ( 'aws' )
215
+ . collection ( 'aws_test' )
216
+ . estimatedDocumentCount ( )
217
+ . catch ( error => error ) ;
218
+
219
+ expect ( result ) . to . not . be . instanceOf ( MongoServerError ) ;
220
+ expect ( result ) . to . be . a ( 'number' ) ;
221
+
222
+ if ( test . outcome != null ) {
223
+ expect ( calledArguments ) . to . deep . equal ( [ { clientConfig : { region : test . outcome } } ] ) ;
224
+ } else {
225
+ expect ( calledArguments ) . to . deep . equal ( [ ] ) ;
226
+ }
227
+ } ) ;
228
+ } ) ;
229
+ }
230
+ } ) ;
91
231
} ) ;
0 commit comments