forked from nginxinc/nginx-s3-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awscredentials.js
494 lines (448 loc) · 16.5 KB
/
awscredentials.js
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* Copyright 2023 F5, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @module awscredentials
* @alias AwsCredentials
*/
/**
* @typedef {Object} Credentials
* @property {string} accessKeyId - AWS access key ID
* @property {string} secretAccessKey - AWS secret access key
* @property {string | null} sessionToken - AWS session token
* @property {string | null} expiration - Expiration timestamp of the credentials
*/
import utils from "./utils.js";
const fs = require('fs');
/**
* The current moment as a timestamp. This timestamp will be used across
* functions in order for there to be no variations in signatures.
* @type {Date}
*/
const NOW = new Date();
/**
* Constant base URI to fetch credentials together with the credentials relative URI, see
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html for more details.
* @type {string}
*/
const ECS_CREDENTIAL_BASE_URI = 'http://169.254.170.2';
/**
* URL to EC2 Instance Metadata Service (IMDS) token endpoint
* @see {@link https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html| EC2 Instance Metadata Service}
* @type {string}
*/
const EC2_IMDS_TOKEN_ENDPOINT = 'http://169.254.169.254/latest/api/token';
/**
* URL to EC2 Instance Metadata Service (IMDS) security credentials endpoint
* @type {string}
*/
const EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/';
/**
* URL to EKS Pod Identity Agent credentials endpoint
* @type {string}
*/
const EKS_POD_IDENTITY_AGENT_CREDENTIALS_ENDPOINT = 'http://169.254.170.23/v1/credentials'
/**
* Offset to the expiration of credentials, when they should be considered expired and refreshed. The maximum
* time here can be 5 minutes, the IMDS and ECS credentials endpoint will make sure that each returned set of credentials
* is valid for at least another 5 minutes.
*
* To make sure we always refresh the credentials instead of retrieving the same again, keep credentials until 4:30 minutes
* before they really expire.
*
* @type {number}
*/
const maxValidityOffsetMs = 4.5 * 60 * 1000;
/**
* Get the current session token from either the instance profile credential
* cache or environment variables.
*
* @param r {NginxHTTPRequest} HTTP request object (not used, but required for NGINX configuration)
* @returns {string} current session token or empty string
*/
function sessionToken(r) {
const credentials = readCredentials(r);
if (credentials.sessionToken) {
return credentials.sessionToken;
}
return '';
}
/**
* Get the instance profile credentials needed to authenticate against S3 from
* a backend cache. If the credentials cannot be found, then return undefined.
* @param r {NginxHTTPRequest} HTTP request object (not used, but required for NGINX configuration)
* @returns {Credentials|undefined} AWS instance profile credentials or undefined
*/
function readCredentials(r) {
if ('AWS_ACCESS_KEY_ID' in process.env && 'AWS_SECRET_ACCESS_KEY' in process.env) {
let sessionToken = 'AWS_SESSION_TOKEN' in process.env ?
process.env['AWS_SESSION_TOKEN'] : null;
if (sessionToken !== null && sessionToken.length === 0) {
sessionToken = null;
}
return {
accessKeyId: process.env['AWS_ACCESS_KEY_ID'],
secretAccessKey: process.env['AWS_SECRET_ACCESS_KEY'],
sessionToken: sessionToken,
expiration: null
};
}
if ("variables" in r && r.variables.cache_instance_credentials_enabled == 1) {
return _readCredentialsFromKeyValStore(r);
} else {
return _readCredentialsFromFile();
}
}
/**
* Read credentials from the NGINX Keyval store. If it is not found, then
* return undefined.
*
* @param r {NginxHTTPRequest} HTTP request object (not used, but required for NGINX configuration)
* @returns {Credentials|undefined} AWS instance profile credentials or undefined
* @private
*/
function _readCredentialsFromKeyValStore(r) {
const cached = r.variables.instance_credential_json;
if (!cached) {
return undefined;
}
try {
return JSON.parse(cached);
} catch (e) {
utils.debug_log(r, `Error parsing JSON value from r.variables.instance_credential_json: ${e}`);
return undefined;
}
}
/**
* Read the contents of the credentials file into memory. If it is not
* found, then return undefined.
*
* @returns {Credentials|undefined} AWS instance profile credentials or undefined
* @private
*/
function _readCredentialsFromFile() {
const credsFilePath = _credentialsTempFile();
try {
const creds = fs.readFileSync(credsFilePath);
return JSON.parse(creds);
} catch (e) {
/* Do not throw an exception in the case of when the
credentials file path is invalid in order to signal to
the caller that such a file has not been created yet. */
if (e.code === 'ENOENT') {
return undefined;
}
throw e;
}
}
/**
* Returns the path to the credentials temporary cache file.
*
* @returns {string} path on the file system to credentials cache file
* @private
*/
function _credentialsTempFile() {
if (process.env['AWS_CREDENTIALS_TEMP_FILE']) {
return process.env['AWS_CREDENTIALS_TEMP_FILE'];
}
if (process.env['TMPDIR']) {
return `${process.env['TMPDIR']}/credentials.json`
}
return '/tmp/credentials.json';
}
/**
* Write the instance profile credentials to a caching backend.
*
* @param r {NginxHTTPRequest} HTTP request object (not used, but required for NGINX configuration)
* @param credentials {Credentials} AWS instance profile credentials
*/
function writeCredentials(r, credentials) {
/* Do not bother writing credentials if we are running in a mode where we
do not need instance credentials. */
if (process.env['AWS_ACCESS_KEY_ID'] && process.env['AWS_SECRET_ACCESS_KEY']) {
return;
}
if (!credentials) {
throw `Cannot write invalid credentials: ${JSON.stringify(credentials)}`;
}
if ("variables" in r && r.variables.cache_instance_credentials_enabled == 1) {
_writeCredentialsToKeyValStore(r, credentials);
} else {
_writeCredentialsToFile(credentials);
}
}
/**
* Write the instance profile credentials to the NGINX Keyval store.
*
* @param r {NginxHTTPRequest} HTTP request object (not used, but required for NGINX configuration)
* @param credentials {{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string), expiration: (string)}} AWS instance profile credentials
* @private
*/
function _writeCredentialsToKeyValStore(r, credentials) {
r.variables.instance_credential_json = JSON.stringify(credentials);
}
/**
* Write the instance profile credentials to a file on the file system. This
* file will be quite small and should end up in the file cache relatively
* quickly if it is repeatedly read.
*
* @param credentials {Credentials} AWS instance profile credentials
* @private
*/
function _writeCredentialsToFile(credentials) {
fs.writeFileSync(_credentialsTempFile(), JSON.stringify(credentials));
}
/**
* Get the credentials needed to create AWS signatures in order to authenticate
* to AWS service. If the gateway is being provided credentials via an instance
* profile credential as provided over the metadata endpoint, this function will:
* 1. Try to read the credentials from cache
* 2. Determine if the credentials are stale
* 3. If the cached credentials are missing or stale, it gets new credentials
* from the metadata endpoint.
* 4. If new credentials were pulled, it writes the credentials back to the
* cache.
*
* If the gateway is not using instance profile credentials, then this function
* quickly exits.
*
* @param r {NginxHTTPRequest} HTTP request object
* @returns {Promise<void>}
*/
async function fetchCredentials(r) {
/* If we are not using an AWS instance profile to set our credentials we
exit quickly and don't write a credentials file. */
if (utils.areAllEnvVarsSet(['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'])) {
r.return(200);
return;
}
let current;
try {
current = readCredentials(r);
} catch (e) {
utils.debug_log(r, `Could not read credentials: ${e}`);
r.return(500);
return;
}
if (current) {
// If AWS returns a Unix timestamp it will be in seconds, but in Date constructor we should provide timestamp in milliseconds
// In some situations (including EC2 and Fargate) current.expiration will be an RFC 3339 string - see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#instance-metadata-security-credentials
const expireAt = typeof current.expiration == 'number' ? current.expiration * 1000 : current.expiration
const exp = new Date(expireAt).getTime() - maxValidityOffsetMs;
if (NOW.getTime() < exp) {
r.return(200);
return;
}
}
let credentials;
utils.debug_log(r, 'Cached credentials are expired or not present, requesting new ones');
if (utils.areAllEnvVarsSet('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI')) {
const relative_uri = process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] || '';
const uri = ECS_CREDENTIAL_BASE_URI + relative_uri;
try {
credentials = await _fetchEcsRoleCredentials(uri);
} catch (e) {
utils.debug_log(r, 'Could not load ECS task role credentials: ' + JSON.stringify(e));
r.return(500);
return;
}
}
else if (utils.areAllEnvVarsSet('AWS_WEB_IDENTITY_TOKEN_FILE')) {
try {
credentials = await _fetchWebIdentityCredentials(r)
} catch (e) {
utils.debug_log(r, 'Could not assume role using web identity: ' + JSON.stringify(e));
r.return(500);
return;
}
}
else if (utils.areAllEnvVarsSet('AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE')) {
try {
credentials = await _fetchEKSPodIdentityCredentials(r)
} catch (e) {
utils.debug_log(r, 'Could not assume role using EKS pod identity: ' + JSON.stringify(e));
r.return(500);
return;
}
} else {
try {
credentials = await _fetchEC2RoleCredentials();
} catch (e) {
utils.debug_log(r, 'Could not load EC2 task role credentials: ' + JSON.stringify(e));
r.return(500);
return;
}
}
try {
writeCredentials(r, credentials);
} catch (e) {
utils.debug_log(r, `Could not write credentials: ${e}`);
r.return(500);
return;
}
r.return(200);
}
/**
* Get the credentials needed to generate AWS signatures from the ECS
* (Elastic Container Service) metadata endpoint.
*
* @param credentialsUri {string} endpoint to get credentials from
* @returns {Promise<Credentials>}
* @private
*/
async function _fetchEcsRoleCredentials(credentialsUri) {
const resp = await ngx.fetch(credentialsUri);
if (!resp.ok) {
throw 'Credentials endpoint response was not ok.';
}
const creds = await resp.json();
return {
accessKeyId: creds.AccessKeyId,
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.Token,
expiration: creds.Expiration,
};
}
/**
* Get the credentials needed to generate AWS signatures from the EC2
* metadata endpoint.
*
* @returns {Promise<Credentials>}
* @private
*/
async function _fetchEC2RoleCredentials() {
const tokenResp = await ngx.fetch(EC2_IMDS_TOKEN_ENDPOINT, {
headers: {
'x-aws-ec2-metadata-token-ttl-seconds': '21600',
},
method: 'PUT',
});
const token = await tokenResp.text();
let resp = await ngx.fetch(EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT, {
headers: {
'x-aws-ec2-metadata-token': token,
},
});
/* This _might_ get multiple possible roles in other scenarios, however,
EC2 supports attaching one role only.It should therefore be safe to take
the whole output, even given IMDS _might_ (?) be able to return multiple
roles. */
const credName = await resp.text();
if (credName === "") {
throw 'No credentials available for EC2 instance';
}
resp = await ngx.fetch(EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT + credName, {
headers: {
'x-aws-ec2-metadata-token': token,
},
});
const creds = await resp.json();
return {
accessKeyId: creds.AccessKeyId,
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.Token,
expiration: creds.Expiration,
};
}
/**
* Get the credentials needed to generate AWS signatures from the EKS Pod Identity Agent
* endpoint.
*
* @returns {Promise<Credentials>}
* @private
*/
async function _fetchEKSPodIdentityCredentials() {
const token = fs.readFileSync(process.env['AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE']);
let resp = await ngx.fetch(EKS_POD_IDENTITY_AGENT_CREDENTIALS_ENDPOINT, {
headers: {
'Authorization': token,
},
});
const creds = await resp.json();
return {
accessKeyId: creds.AccessKeyId,
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.Token,
expiration: creds.Expiration,
};
}
/**
* Get the credentials by assuming calling AssumeRoleWithWebIdentity with the environment variable
* values ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_SESSION_NAME
*
* @returns {Promise<Credentials>}
* @private
*/
async function _fetchWebIdentityCredentials(r) {
const arn = process.env['AWS_ROLE_ARN'];
const name = process.env['AWS_ROLE_SESSION_NAME'];
let sts_endpoint = process.env['STS_ENDPOINT'];
if (!sts_endpoint) {
/* On EKS, the ServiceAccount can be annotated with
'eks.amazonaws.com/sts-regional-endpoints' to control
the usage of regional endpoints. We are using the same standard
environment variable here as the AWS SDK. This is with the exception
of replacing the value `legacy` with `global` to match what EKS sets
the variable to.
See: https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html
See: https://docs.aws.amazon.com/eks/latest/userguide/configure-sts-endpoint.html */
const sts_regional = process.env['AWS_STS_REGIONAL_ENDPOINTS'] || 'global';
if (sts_regional === 'regional') {
/* STS regional endpoints can be derived from the region's name.
See: https://docs.aws.amazon.com/general/latest/gr/sts.html */
const region = process.env['AWS_REGION'];
if (region) {
sts_endpoint = `https://sts.${region}.amazonaws.com`;
} else {
throw 'Missing required AWS_REGION env variable';
}
} else {
// This is the default global endpoint
sts_endpoint = 'https://sts.amazonaws.com';
}
}
const token = fs.readFileSync(process.env['AWS_WEB_IDENTITY_TOKEN_FILE']);
const params = `Version=2011-06-15&Action=AssumeRoleWithWebIdentity&RoleArn=${arn}&RoleSessionName=${name}&WebIdentityToken=${token}`;
const response = await ngx.fetch(sts_endpoint + "?" + params, {
headers: {
"Accept": "application/json"
},
method: 'GET',
});
const resp = await response.json();
const creds = resp.AssumeRoleWithWebIdentityResponse.AssumeRoleWithWebIdentityResult.Credentials;
return {
accessKeyId: creds.AccessKeyId,
secretAccessKey: creds.SecretAccessKey,
sessionToken: creds.SessionToken,
expiration: creds.Expiration,
};
}
/**
* Get the current timestamp. This timestamp will be used across functions in
* order for there to be no variations in signatures.
*
* @returns {Date} The current moment as a timestamp
*/
function Now() {
return NOW;
}
export default {
Now,
fetchCredentials,
readCredentials,
sessionToken,
writeCredentials
}