@@ -51,12 +51,21 @@ class MongoDBAWS extends AuthProvider {
51
51
return ;
52
52
}
53
53
54
- const username = credentials . username ;
55
- const password = credentials . password ;
56
54
const db = credentials . source ;
57
- const token = credentials . mechanismProperties . AWS_SESSION_TOKEN ;
58
55
const bson = this . bson ;
59
56
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
+
60
69
crypto . randomBytes ( 32 , ( err , nonce ) => {
61
70
if ( err ) {
62
71
callback ( err ) ;
@@ -109,18 +118,14 @@ class MongoDBAWS extends AuthProvider {
109
118
path : '/' ,
110
119
body
111
120
} ,
112
- {
113
- accessKeyId : username ,
114
- secretAccessKey : password ,
115
- token
116
- }
121
+ awsCredentials
117
122
) ;
118
123
119
124
const authorization = options . headers . Authorization ;
120
125
const date = options . headers [ 'X-Amz-Date' ] ;
121
126
const payload = { a : authorization , d : date } ;
122
- if ( token ) {
123
- payload . t = token ;
127
+ if ( sessionToken ) {
128
+ payload . t = sessionToken ;
124
129
}
125
130
126
131
const saslContinue = {
@@ -164,6 +169,7 @@ function makeTempCredentials(credentials, callback) {
164
169
if ( process . env . AWS_CONTAINER_CREDENTIALS_RELATIVE_URI ) {
165
170
request (
166
171
`${ AWS_RELATIVE_URI } ${ process . env . AWS_CONTAINER_CREDENTIALS_RELATIVE_URI } ` ,
172
+ undefined ,
167
173
( err , res ) => {
168
174
if ( err ) return callback ( err ) ;
169
175
done ( res ) ;
@@ -215,11 +221,6 @@ function deriveRegion(host) {
215
221
}
216
222
217
223
function request ( uri , options , callback ) {
218
- if ( typeof options === 'function' ) {
219
- callback = options ;
220
- options = { } ;
221
- }
222
-
223
224
options = Object . assign (
224
225
{
225
226
method : 'GET' ,
0 commit comments