-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmongodb_aws.test.ts
325 lines (282 loc) · 10.1 KB
/
mongodb_aws.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import * as process from 'node:process';
import { expect } from 'chai';
import * as http from 'http';
import { performance } from 'perf_hooks';
import * as sinon from 'sinon';
import {
MongoAWSError,
type MongoClient,
MongoDBAWS,
MongoMissingCredentialsError,
MongoServerError
} from '../../mongodb';
function awsSdk() {
try {
return require('@aws-sdk/credential-providers');
} catch {
return null;
}
}
describe('MONGODB-AWS', function () {
let awsSdkPresent;
let client: MongoClient;
beforeEach(function () {
const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI || MONGODB_URI.indexOf('MONGODB-AWS') === -1) {
this.currentTest.skipReason = 'requires MONGODB_URI to contain MONGODB-AWS auth mechanism';
return this.skip();
}
const { MONGODB_AWS_SDK = 'unset' } = process.env;
expect(
['true', 'false'],
`Always inform the AWS tests if they run with or without the SDK (MONGODB_AWS_SDK=${MONGODB_AWS_SDK})`
).to.include(MONGODB_AWS_SDK);
awsSdkPresent = !!awsSdk();
expect(
awsSdkPresent,
MONGODB_AWS_SDK === 'true'
? 'expected aws sdk to be installed'
: 'expected aws sdk to not be installed'
).to.be[MONGODB_AWS_SDK];
});
afterEach(async () => {
await client?.close();
});
it('should authorize when successfully authenticated', async function () {
client = this.configuration.newClient(process.env.MONGODB_URI); // use the URI built by the test environment
const result = await client
.db('aws')
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);
expect(result).to.not.be.instanceOf(MongoServerError);
expect(result).to.be.a('number');
});
it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function () {
client = this.configuration.newClient(this.configuration.url(), {
authMechanismProperties: { AWS_SESSION_TOKEN: '' }
});
expect(client)
.to.have.nested.property('options.credentials.mechanismProperties.AWS_SESSION_TOKEN')
.that.equals('');
});
it('should store a MongoDBAWS provider instance per client', async function () {
client = this.configuration.newClient(process.env.MONGODB_URI);
await client
.db('aws')
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);
expect(client).to.have.nested.property('s.authProviders');
const provider = client.s.authProviders.getOrCreateProvider('MONGODB-AWS');
expect(provider).to.be.instanceOf(MongoDBAWS);
});
describe('with missing aws token', () => {
let awsSessionToken: string | undefined;
beforeEach(() => {
awsSessionToken = process.env.AWS_SESSION_TOKEN;
delete process.env.AWS_SESSION_TOKEN;
});
afterEach(() => {
if (awsSessionToken != null) {
process.env.AWS_SESSION_TOKEN = awsSessionToken;
}
});
it('should not throw an exception when aws token is missing', async function () {
client = this.configuration.newClient(process.env.MONGODB_URI);
const result = await client
.db('aws')
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);
// We check only for the MongoMissingCredentialsError
// and do check for the MongoServerError as the error or numeric result
// that can be returned depending on different types of environments
// getting credentials from different sources.
expect(result).to.not.be.instanceOf(MongoMissingCredentialsError);
});
});
describe('EC2 with missing credentials', () => {
let client;
beforeEach(function () {
if (!process.env.IS_EC2) {
this.currentTest.skipReason = 'requires an AWS EC2 environment';
this.skip();
}
sinon.stub(http, 'request').callsFake(function (...args) {
// We pass in a legacy object that has the same properties as a URL
// but it is not an instanceof URL.
expect(args[0]).to.be.an('object');
if (typeof args[0] === 'object') {
args[0].hostname = 'www.example.com';
args[0].port = '81';
}
return http.request.wrappedMethod.apply(this, args);
});
});
afterEach(async () => {
sinon.restore();
await client?.close();
});
it('should respect the default timeout of 10000ms', async function () {
const config = this.configuration;
client = config.newClient(process.env.MONGODB_URI, { authMechanism: 'MONGODB-AWS' }); // use the URI built by the test environment
const startTime = performance.now();
const caughtError = await client
.db()
.command({ ping: 1 })
.catch(error => error);
const endTime = performance.now();
const timeTaken = endTime - startTime;
expect(caughtError).to.be.instanceOf(MongoAWSError);
expect(caughtError)
.property('message')
.match(/(timed out after)|(Could not load credentials)/);
// Credentials provider from the SDK does not allow to configure the timeout
// and defaults to 2 seconds - so we ensure this timeout happens below 12s
// instead of the 10s-12s range previously.
expect(timeTaken).to.be.below(12000);
});
});
describe('when using AssumeRoleWithWebIdentity', () => {
const tests = [
{
ctx: 'when no AWS region settings are set',
title: 'uses the default region',
env: {
AWS_STS_REGIONAL_ENDPOINTS: undefined,
AWS_REGION: undefined
},
calledWith: []
},
{
ctx: 'when only AWS_STS_REGIONAL_ENDPOINTS is set',
title: 'uses the default region',
env: {
AWS_STS_REGIONAL_ENDPOINTS: 'regional',
AWS_REGION: undefined
},
calledWith: []
},
{
ctx: 'when only AWS_REGION is set',
title: 'uses the default region',
env: {
AWS_STS_REGIONAL_ENDPOINTS: undefined,
AWS_REGION: 'us-west-2'
},
calledWith: []
},
{
ctx: 'when AWS_STS_REGIONAL_ENDPOINTS is set to regional and region is legacy',
title: 'uses the region from the environment',
env: {
AWS_STS_REGIONAL_ENDPOINTS: 'regional',
AWS_REGION: 'us-west-2'
},
calledWith: [{ clientConfig: { region: 'us-west-2' } }]
},
{
ctx: 'when AWS_STS_REGIONAL_ENDPOINTS is set to regional and region is new',
title: 'uses the region from the environment',
env: {
AWS_STS_REGIONAL_ENDPOINTS: 'regional',
AWS_REGION: 'sa-east-1'
},
calledWith: [{ clientConfig: { region: 'sa-east-1' } }]
},
{
ctx: 'when AWS_STS_REGIONAL_ENDPOINTS is set to legacy and region is legacy',
title: 'uses the region from the environment',
env: {
AWS_STS_REGIONAL_ENDPOINTS: 'legacy',
AWS_REGION: 'us-west-2'
},
calledWith: []
},
{
ctx: 'when AWS_STS_REGIONAL_ENDPOINTS is set to legacy and region is new',
title: 'uses the default region',
env: {
AWS_STS_REGIONAL_ENDPOINTS: 'legacy',
AWS_REGION: 'sa-east-1'
},
calledWith: []
}
];
for (const test of tests) {
context(test.ctx, () => {
let credentialProvider;
let storedEnv;
let calledArguments;
let shouldSkip = false;
let numberOfFromNodeProviderChainCalls;
const envCheck = () => {
const { AWS_WEB_IDENTITY_TOKEN_FILE = '' } = process.env;
credentialProvider = awsSdk();
return AWS_WEB_IDENTITY_TOKEN_FILE.length === 0 || credentialProvider == null;
};
beforeEach(function () {
shouldSkip = envCheck();
if (shouldSkip) {
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
return this.skip();
}
storedEnv = process.env;
if (test.env.AWS_STS_REGIONAL_ENDPOINTS === undefined) {
delete process.env.AWS_STS_REGIONAL_ENDPOINTS;
} else {
process.env.AWS_STS_REGIONAL_ENDPOINTS = test.env.AWS_STS_REGIONAL_ENDPOINTS;
}
if (test.env.AWS_REGION === undefined) {
delete process.env.AWS_REGION;
} else {
process.env.AWS_REGION = test.env.AWS_REGION;
}
numberOfFromNodeProviderChainCalls = 0;
MongoDBAWS.credentialProvider = {
fromNodeProviderChain(...args) {
calledArguments = args;
numberOfFromNodeProviderChainCalls += 1;
return credentialProvider.fromNodeProviderChain(...args);
}
};
client = this.configuration.newClient(process.env.MONGODB_URI);
});
afterEach(() => {
if (shouldSkip) {
return;
}
if (typeof storedEnv.AWS_STS_REGIONAL_ENDPOINTS === 'string') {
process.env.AWS_STS_REGIONAL_ENDPOINTS = storedEnv.AWS_STS_REGIONAL_ENDPOINTS;
}
if (typeof storedEnv.AWS_STS_REGIONAL_ENDPOINTS === 'string') {
process.env.AWS_REGION = storedEnv.AWS_REGION;
}
MongoDBAWS.credentialProvider = credentialProvider;
calledArguments = [];
});
it(test.title, async function () {
const result = await client
.db('aws')
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);
expect(result).to.not.be.instanceOf(MongoServerError);
expect(result).to.be.a('number');
expect(calledArguments).to.deep.equal(test.calledWith);
});
it('fromNodeProviderChain called once', async function () {
await client.close();
await client.connect();
await client
.db('aws')
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);
expect(numberOfFromNodeProviderChainCalls).to.be.eql(1);
});
});
}
});
});