Skip to content

Commit 5902b4c

Browse files
authored
fix(NODE-3487): check for nullish aws mechanism property (#2957)
1 parent 54f5c2d commit 5902b4c

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

lib/core/auth/mongo_credentials.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ class MongoCredentials {
5858
this.password = process.env.AWS_SECRET_ACCESS_KEY;
5959
}
6060

61-
if (!this.mechanismProperties.AWS_SESSION_TOKEN && process.env.AWS_SESSION_TOKEN) {
61+
if (
62+
this.mechanismProperties.AWS_SESSION_TOKEN == null &&
63+
process.env.AWS_SESSION_TOKEN != null
64+
) {
6265
this.mechanismProperties.AWS_SESSION_TOKEN = process.env.AWS_SESSION_TOKEN;
6366
}
6467
}

lib/core/auth/mongodb_aws.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@ class MongoDBAWS extends AuthProvider {
5151
return;
5252
}
5353

54-
const username = credentials.username;
55-
const password = credentials.password;
5654
const db = credentials.source;
57-
const token = credentials.mechanismProperties.AWS_SESSION_TOKEN;
5855
const bson = this.bson;
5956

57+
const accessKeyId = credentials.username;
58+
const secretAccessKey = credentials.password;
59+
const sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN;
60+
61+
// If all three defined, include sessionToken, else include username and pass, else no credentials
62+
const awsCredentials =
63+
accessKeyId && secretAccessKey && sessionToken
64+
? { accessKeyId, secretAccessKey, sessionToken }
65+
: accessKeyId && secretAccessKey
66+
? { accessKeyId, secretAccessKey }
67+
: undefined;
68+
6069
crypto.randomBytes(32, (err, nonce) => {
6170
if (err) {
6271
callback(err);
@@ -109,18 +118,14 @@ class MongoDBAWS extends AuthProvider {
109118
path: '/',
110119
body
111120
},
112-
{
113-
accessKeyId: username,
114-
secretAccessKey: password,
115-
token
116-
}
121+
awsCredentials
117122
);
118123

119124
const authorization = options.headers.Authorization;
120125
const date = options.headers['X-Amz-Date'];
121126
const payload = { a: authorization, d: date };
122-
if (token) {
123-
payload.t = token;
127+
if (sessionToken) {
128+
payload.t = sessionToken;
124129
}
125130

126131
const saslContinue = {
@@ -164,6 +169,7 @@ function makeTempCredentials(credentials, callback) {
164169
if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
165170
request(
166171
`${AWS_RELATIVE_URI}${process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`,
172+
undefined,
167173
(err, res) => {
168174
if (err) return callback(err);
169175
done(res);
@@ -215,11 +221,6 @@ function deriveRegion(host) {
215221
}
216222

217223
function request(uri, options, callback) {
218-
if (typeof options === 'function') {
219-
callback = options;
220-
options = {};
221-
}
222-
223224
options = Object.assign(
224225
{
225226
method: 'GET',

test/functional/mongodb_aws.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ describe('MONGODB-AWS', function() {
4040
});
4141
});
4242
});
43+
44+
it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function() {
45+
const client = this.configuration.newClient(this.configuration.url(), {
46+
authMechanismProperties: { AWS_SESSION_TOKEN: '' }
47+
});
48+
expect(client)
49+
.to.have.nested.property('options.credentials.mechanismProperties.AWS_SESSION_TOKEN')
50+
.that.equals('');
51+
});
4352
});

0 commit comments

Comments
 (0)