Skip to content

Commit 21e82ca

Browse files
authored
Merge pull request #26 from shawnhankim/lib
chore: replace HOSTNAME with AWS_ROLE_SESSION_NAME, change now func and comment
2 parents 63b1821 + feee53e commit 21e82ca

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ js_set $awsDate awssig4.awsHeaderDate;
9090
js_set $awsPayloadHash awssig4.awsHeaderPayloadHash;
9191
js_set $awsSessionToken awscredentials.sessionToken;
9292
js_set $lambdaFunctionARNAuth lambdagateway.lambdaFunctionARNAuth;
93+
js_var $defaultHostName 'nginx-lambda-gateway';
9394
9495
map $request_uri $lambda_url {
9596
default https://lambda.us-east-1.amazonaws.com;
@@ -145,7 +146,7 @@ function lambdaFunctionARNAuth(r) {
145146
const credentials = awscred.readCredentials(r);
146147

147148
const signature = awssig4.signatureV4(
148-
r, awscred.getNow(), region, SERVICE,
149+
r, awscred.Now(), region, SERVICE,
149150
r.variables.request_uri, queryParams, host, credentials
150151
);
151152
return signature;

core/awscredentials.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ function sessionToken(r) {
6868
}
6969

7070
/**
71-
* Get the instance profile credentials needed to authenticate against Lambda from
72-
* a backend cache. If the credentials cannot be found, then return undefined.
71+
* Get the instance profile credentials needed to authenticate against services
72+
* in AWS such as S3 and Lambda from a backend cache. If the credentials cannot
73+
* be found, then return undefined.
7374
* @param r {Request} HTTP request object (not used, but required for NGINX configuration)
7475
* @returns {undefined|{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string|null), expiration: (string|null)}} AWS instance profile credentials or undefined
7576
*/
@@ -363,14 +364,14 @@ async function _fetchEC2RoleCredentials() {
363364

364365
/**
365366
* Get the credentials by assuming calling AssumeRoleWithWebIdentity with the environment variable
366-
* values ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE and HOSTNAME
367+
* values ROLE_ARN, AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_SESSION_NAME.
367368
*
368369
* @returns {Promise<{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string), expiration: (string)}>}
369370
* @private
370371
*/
371372
async function _fetchWebIdentityCredentials(r) {
372373
const arn = process.env['AWS_ROLE_ARN'];
373-
const name = process.env['HOSTNAME'] || 'nginx-lambda-gateway';
374+
const name = process.env['AWS_ROLE_SESSION_NAME'];
374375

375376
let sts_endpoint = process.env['STS_ENDPOINT'];
376377
if (!sts_endpoint) {
@@ -426,12 +427,12 @@ async function _fetchWebIdentityCredentials(r) {
426427
*
427428
* @returns {Date} The current moment as a timestamp
428429
*/
429-
function getNow() {
430+
function Now() {
430431
return NOW;
431432
}
432433

433434
export default {
434-
getNow,
435+
Now,
435436
fetchCredentials,
436437
readCredentials,
437438
sessionToken,

core/awssig4.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const EMPTY_PAYLOAD_HASH = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495
2929
* Constant defining the headers being signed.
3030
* @type {string}
3131
*/
32-
// const DEFAULT_SIGNED_HEADERS = 'host;x-amz-content-sha256;x-amz-date';
3332
const DEFAULT_SIGNED_HEADERS = 'host;x-amz-date';
3433

3534
/**
@@ -197,8 +196,7 @@ function _buildStringToSign(amzDatetime, eightDigitDate, region, service, canoni
197196
* @private
198197
*/
199198
function _signedHeaders(r, sessionToken) {
200-
let headers = '';
201-
headers += DEFAULT_SIGNED_HEADERS;
199+
let headers = DEFAULT_SIGNED_HEADERS;
202200
if (sessionToken && sessionToken.length > 0) {
203201
headers += ';x-amz-security-token';
204202
}
@@ -264,8 +262,8 @@ function _splitCachedValues(cached) {
264262
*/
265263
function awsHeaderDate(r) {
266264
return utils.getAmzDatetime(
267-
awscred.getNow(),
268-
utils.getEightDigitDate(awscred.getNow())
265+
awscred.Now(),
266+
utils.getEightDigitDate(awscred.Now())
269267
);
270268
}
271269

0 commit comments

Comments
 (0)