Skip to content

Commit b657c84

Browse files
committed
Change constants to user upper case names
Signed-off-by: Elijah Zupancic <[email protected]>
1 parent 77a4f8f commit b657c84

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

common/etc/nginx/include/s3gateway.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,57 @@ const fs = require('fs');
3030
* about signature generation will be logged.
3131
* @type {boolean}
3232
*/
33-
const debug = _parseBoolean(process.env['S3_DEBUG']);
34-
const allow_listing = _parseBoolean(process.env['ALLOW_DIRECTORY_LIST']);
35-
const provide_index_page = _parseBoolean(process.env['PROVIDE_INDEX_PAGE']);
36-
const append_slash = _parseBoolean(process.env['APPEND_SLASH_FOR_POSSIBLE_DIRECTORY']);
33+
const DEBUG = _parseBoolean(process.env['S3_DEBUG']);
34+
const ALLOW_LISTING = _parseBoolean(process.env['ALLOW_DIRECTORY_LIST']);
35+
const PROVIDE_INDEX_PAGE = _parseBoolean(process.env['PROVIDE_INDEX_PAGE']);
36+
const APPEND_SLASH = _parseBoolean(process.env['APPEND_SLASH_FOR_POSSIBLE_DIRECTORY']);
3737

38-
const s3_style = process.env['S3_STYLE'];
38+
const S3_STYLE = process.env['S3_STYLE'];
3939

40+
/**
41+
* Default filename for index pages to be read off of the backing object store.
42+
* @type {string}
43+
*/
4044
const INDEX_PAGE = "index.html";
4145

4246
/**
4347
* The current moment as a timestamp. This timestamp will be used across
4448
* functions in order for there to be no variations in signatures.
4549
* @type {Date}
4650
*/
47-
const now = new Date();
51+
const NOW = new Date();
4852

4953
/**
5054
* Constant defining the service requests are being signed for.
5155
* @type {string}
5256
*/
53-
const service = 's3';
57+
const SERVICE = 's3';
5458

5559
/**
5660
* Constant checksum for an empty HTTP body.
5761
* @type {string}
5862
*/
59-
const emptyPayloadHash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
63+
const EMPTY_PAYLOAD_HASH = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
6064

6165
/**
6266
* Constant defining the headers being signed.
6367
* @type {string}
6468
*/
65-
const defaultSignedHeaders = 'host;x-amz-content-sha256;x-amz-date';
69+
const DEFAULT_SIGNED_HEADERS = 'host;x-amz-content-sha256;x-amz-date';
6670

6771
/**
6872
* Constant base URI to fetch credentials together with the credentials relative URI, see
6973
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html for more details.
7074
* @type {string}
7175
*/
72-
const ecsCredentialsBaseUri = 'http://169.254.170.2';
76+
const ECS_CREDENTIAL_BASE_URI = 'http://169.254.170.2';
7377

7478
/**
7579
* @type {string}
7680
*/
77-
const ec2ImdsTokenEndpoint = 'http://169.254.169.254/latest/api/token';
81+
const EC2_IMDS_TOKEN_ENDPOINT = 'http://169.254.169.254/latest/api/token';
7882

79-
const ec2ImdsSecurityCredentialsEndpoint = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/';
83+
const EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/';
8084

8185
/**
8286
* Transform the headers returned from S3 such that there isn't information
@@ -120,7 +124,7 @@ function editAmzHeaders(r) {
120124
* @returns {string} RFC2616 timestamp
121125
*/
122126
function s3date(r) {
123-
return now.toUTCString();
127+
return NOW.toUTCString();
124128
}
125129

126130
/**
@@ -133,7 +137,7 @@ function s3date(r) {
133137
* @returns {string} ISO 8601 timestamp
134138
*/
135139
function awsHeaderDate(r) {
136-
return _amzDatetime(now, _eightDigitDate(now));
140+
return _amzDatetime(NOW, _eightDigitDate(NOW));
137141
}
138142

139143
/**
@@ -295,7 +299,7 @@ function s3auth(r) {
295299
if (sigver == '2') {
296300
signature = signatureV2(r, bucket, credentials);
297301
} else {
298-
signature = signatureV4(r, now, bucket, region, server, credentials);
302+
signature = signatureV4(r, NOW, bucket, region, server, credentials);
299303
}
300304

301305
return signature;
@@ -511,7 +515,7 @@ function filterListResponse(r, data, flags) {
511515
* @returns {string} semicolon delimited string of the headers needed for signing
512516
*/
513517
function signedHeaders(sessionToken) {
514-
let headers = defaultSignedHeaders;
518+
let headers = DEFAULT_SIGNED_HEADERS;
515519
if (sessionToken) {
516520
headers += ';x-amz-security-token';
517521
}
@@ -535,7 +539,7 @@ function signatureV4(r, timestamp, bucket, region, server, credentials) {
535539
const amzDatetime = _amzDatetime(timestamp, eightDigitDate);
536540
const signature = _buildSignatureV4(r, amzDatetime, eightDigitDate, credentials, bucket, region, server);
537541
const authHeader = 'AWS4-HMAC-SHA256 Credential='
538-
.concat(credentials.accessKeyId, '/', eightDigitDate, '/', region, '/', service, '/aws4_request,',
542+
.concat(credentials.accessKeyId, '/', eightDigitDate, '/', region, '/', SERVICE, '/aws4_request,',
539543
'SignedHeaders=', signedHeaders(credentials.sessionToken), ',Signature=', signature);
540544

541545
_debug_log(r, 'AWS v4 Auth header: [' + authHeader + ']');
@@ -614,13 +618,13 @@ function _buildSignatureV4(r, amzDatetime, eightDigitDate, creds, bucket, region
614618
kSigningHash = Buffer.from(JSON.parse(fields[1]));
615619
// Otherwise, generate a new signing key hash and store it in the cache
616620
} else {
617-
kSigningHash = _buildSigningKeyHash(creds.secretAccessKey, eightDigitDate, service, region);
621+
kSigningHash = _buildSigningKeyHash(creds.secretAccessKey, eightDigitDate, SERVICE, region);
618622
_debug_log(r, 'Writing key: ' + eightDigitDate + ':' + kSigningHash.toString('hex'));
619623
r.variables.signing_key_hash = eightDigitDate + ':' + JSON.stringify(kSigningHash);
620624
}
621625
// Otherwise, don't use caching at all (like when we are using NGINX OSS)
622626
} else {
623-
kSigningHash = _buildSigningKeyHash(creds.secretAccessKey, eightDigitDate, service, region);
627+
kSigningHash = _buildSigningKeyHash(creds.secretAccessKey, eightDigitDate, SERVICE, region);
624628
}
625629

626630
_debug_log(r, 'AWS v4 Signing Key Hash: [' + kSigningHash.toString('hex') + ']');
@@ -690,7 +694,7 @@ function _buildStringToSign(amzDatetime, eightDigitDate, region, canonicalReques
690694
*/
691695
function _buildCanonicalRequest(method, uri, queryParams, host, amzDatetime, sessionToken) {
692696
let canonicalHeaders = 'host:' + host + '\n' +
693-
'x-amz-content-sha256:' + emptyPayloadHash + '\n' +
697+
'x-amz-content-sha256:' + EMPTY_PAYLOAD_HASH + '\n' +
694698
'x-amz-date:' + amzDatetime + '\n';
695699

696700
if (sessionToken) {
@@ -702,7 +706,7 @@ function _buildCanonicalRequest(method, uri, queryParams, host, amzDatetime, ses
702706
canonicalRequest += queryParams + '\n';
703707
canonicalRequest += canonicalHeaders + '\n';
704708
canonicalRequest += signedHeaders(sessionToken) + '\n';
705-
canonicalRequest += emptyPayloadHash;
709+
canonicalRequest += EMPTY_PAYLOAD_HASH;
706710

707711
return canonicalRequest;
708712
}
@@ -938,7 +942,7 @@ async function fetchCredentials(r) {
938942
if (current) {
939943
// AWS returns Unix timestamps in seconds, but in Date constructor we should provide timestamp in milliseconds
940944
const exp = new Date(current.expiration * 1000).getTime() - maxValidityOffsetMs;
941-
if (now.getTime() < exp) {
945+
if (NOW.getTime() < exp) {
942946
r.return(200);
943947
return;
944948
}
@@ -949,7 +953,7 @@ async function fetchCredentials(r) {
949953
_debug_log(r, 'Cached credentials are expired or not present, requesting new ones');
950954

951955
if (process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']) {
952-
const uri = ecsCredentialsBaseUri + process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
956+
const uri = ECS_CREDENTIAL_BASE_URI + process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
953957
try {
954958
credentials = await _fetchEcsRoleCredentials(uri);
955959
} catch (e) {
@@ -1016,14 +1020,14 @@ async function _fetchEcsRoleCredentials(credentialsUri) {
10161020
* @private
10171021
*/
10181022
async function _fetchEC2RoleCredentials() {
1019-
const tokenResp = await ngx.fetch(ec2ImdsTokenEndpoint, {
1023+
const tokenResp = await ngx.fetch(EC2_IMDS_TOKEN_ENDPOINT, {
10201024
headers: {
10211025
'x-aws-ec2-metadata-token-ttl-seconds': '21600',
10221026
},
10231027
method: 'PUT',
10241028
});
10251029
const token = await tokenResp.text();
1026-
let resp = await ngx.fetch(ec2ImdsSecurityCredentialsEndpoint, {
1030+
let resp = await ngx.fetch(EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT, {
10271031
headers: {
10281032
'x-aws-ec2-metadata-token': token,
10291033
},
@@ -1036,7 +1040,7 @@ async function _fetchEC2RoleCredentials() {
10361040
if (credName === "") {
10371041
throw 'No credentials available for EC2 instance';
10381042
}
1039-
resp = await ngx.fetch(ec2ImdsSecurityCredentialsEndpoint + credName, {
1043+
resp = await ngx.fetch(EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT + credName, {
10401044
headers: {
10411045
'x-aws-ec2-metadata-token': token,
10421046
},

0 commit comments

Comments
 (0)