@@ -30,53 +30,57 @@ const fs = require('fs');
30
30
* about signature generation will be logged.
31
31
* @type {boolean }
32
32
*/
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' ] ) ;
37
37
38
- const s3_style = process . env [ 'S3_STYLE' ] ;
38
+ const S3_STYLE = process . env [ 'S3_STYLE' ] ;
39
39
40
+ /**
41
+ * Default filename for index pages to be read off of the backing object store.
42
+ * @type {string }
43
+ */
40
44
const INDEX_PAGE = "index.html" ;
41
45
42
46
/**
43
47
* The current moment as a timestamp. This timestamp will be used across
44
48
* functions in order for there to be no variations in signatures.
45
49
* @type {Date }
46
50
*/
47
- const now = new Date ( ) ;
51
+ const NOW = new Date ( ) ;
48
52
49
53
/**
50
54
* Constant defining the service requests are being signed for.
51
55
* @type {string }
52
56
*/
53
- const service = 's3' ;
57
+ const SERVICE = 's3' ;
54
58
55
59
/**
56
60
* Constant checksum for an empty HTTP body.
57
61
* @type {string }
58
62
*/
59
- const emptyPayloadHash = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' ;
63
+ const EMPTY_PAYLOAD_HASH = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' ;
60
64
61
65
/**
62
66
* Constant defining the headers being signed.
63
67
* @type {string }
64
68
*/
65
- const defaultSignedHeaders = 'host;x-amz-content-sha256;x-amz-date' ;
69
+ const DEFAULT_SIGNED_HEADERS = 'host;x-amz-content-sha256;x-amz-date' ;
66
70
67
71
/**
68
72
* Constant base URI to fetch credentials together with the credentials relative URI, see
69
73
* https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html for more details.
70
74
* @type {string }
71
75
*/
72
- const ecsCredentialsBaseUri = 'http://169.254.170.2' ;
76
+ const ECS_CREDENTIAL_BASE_URI = 'http://169.254.170.2' ;
73
77
74
78
/**
75
79
* @type {string }
76
80
*/
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' ;
78
82
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/' ;
80
84
81
85
/**
82
86
* Transform the headers returned from S3 such that there isn't information
@@ -120,7 +124,7 @@ function editAmzHeaders(r) {
120
124
* @returns {string } RFC2616 timestamp
121
125
*/
122
126
function s3date ( r ) {
123
- return now . toUTCString ( ) ;
127
+ return NOW . toUTCString ( ) ;
124
128
}
125
129
126
130
/**
@@ -133,7 +137,7 @@ function s3date(r) {
133
137
* @returns {string } ISO 8601 timestamp
134
138
*/
135
139
function awsHeaderDate ( r ) {
136
- return _amzDatetime ( now , _eightDigitDate ( now ) ) ;
140
+ return _amzDatetime ( NOW , _eightDigitDate ( NOW ) ) ;
137
141
}
138
142
139
143
/**
@@ -295,7 +299,7 @@ function s3auth(r) {
295
299
if ( sigver == '2' ) {
296
300
signature = signatureV2 ( r , bucket , credentials ) ;
297
301
} else {
298
- signature = signatureV4 ( r , now , bucket , region , server , credentials ) ;
302
+ signature = signatureV4 ( r , NOW , bucket , region , server , credentials ) ;
299
303
}
300
304
301
305
return signature ;
@@ -511,7 +515,7 @@ function filterListResponse(r, data, flags) {
511
515
* @returns {string } semicolon delimited string of the headers needed for signing
512
516
*/
513
517
function signedHeaders ( sessionToken ) {
514
- let headers = defaultSignedHeaders ;
518
+ let headers = DEFAULT_SIGNED_HEADERS ;
515
519
if ( sessionToken ) {
516
520
headers += ';x-amz-security-token' ;
517
521
}
@@ -535,7 +539,7 @@ function signatureV4(r, timestamp, bucket, region, server, credentials) {
535
539
const amzDatetime = _amzDatetime ( timestamp , eightDigitDate ) ;
536
540
const signature = _buildSignatureV4 ( r , amzDatetime , eightDigitDate , credentials , bucket , region , server ) ;
537
541
const authHeader = 'AWS4-HMAC-SHA256 Credential='
538
- . concat ( credentials . accessKeyId , '/' , eightDigitDate , '/' , region , '/' , service , '/aws4_request,' ,
542
+ . concat ( credentials . accessKeyId , '/' , eightDigitDate , '/' , region , '/' , SERVICE , '/aws4_request,' ,
539
543
'SignedHeaders=' , signedHeaders ( credentials . sessionToken ) , ',Signature=' , signature ) ;
540
544
541
545
_debug_log ( r , 'AWS v4 Auth header: [' + authHeader + ']' ) ;
@@ -614,13 +618,13 @@ function _buildSignatureV4(r, amzDatetime, eightDigitDate, creds, bucket, region
614
618
kSigningHash = Buffer . from ( JSON . parse ( fields [ 1 ] ) ) ;
615
619
// Otherwise, generate a new signing key hash and store it in the cache
616
620
} else {
617
- kSigningHash = _buildSigningKeyHash ( creds . secretAccessKey , eightDigitDate , service , region ) ;
621
+ kSigningHash = _buildSigningKeyHash ( creds . secretAccessKey , eightDigitDate , SERVICE , region ) ;
618
622
_debug_log ( r , 'Writing key: ' + eightDigitDate + ':' + kSigningHash . toString ( 'hex' ) ) ;
619
623
r . variables . signing_key_hash = eightDigitDate + ':' + JSON . stringify ( kSigningHash ) ;
620
624
}
621
625
// Otherwise, don't use caching at all (like when we are using NGINX OSS)
622
626
} else {
623
- kSigningHash = _buildSigningKeyHash ( creds . secretAccessKey , eightDigitDate , service , region ) ;
627
+ kSigningHash = _buildSigningKeyHash ( creds . secretAccessKey , eightDigitDate , SERVICE , region ) ;
624
628
}
625
629
626
630
_debug_log ( r , 'AWS v4 Signing Key Hash: [' + kSigningHash . toString ( 'hex' ) + ']' ) ;
@@ -690,7 +694,7 @@ function _buildStringToSign(amzDatetime, eightDigitDate, region, canonicalReques
690
694
*/
691
695
function _buildCanonicalRequest ( method , uri , queryParams , host , amzDatetime , sessionToken ) {
692
696
let canonicalHeaders = 'host:' + host + '\n' +
693
- 'x-amz-content-sha256:' + emptyPayloadHash + '\n' +
697
+ 'x-amz-content-sha256:' + EMPTY_PAYLOAD_HASH + '\n' +
694
698
'x-amz-date:' + amzDatetime + '\n' ;
695
699
696
700
if ( sessionToken ) {
@@ -702,7 +706,7 @@ function _buildCanonicalRequest(method, uri, queryParams, host, amzDatetime, ses
702
706
canonicalRequest += queryParams + '\n' ;
703
707
canonicalRequest += canonicalHeaders + '\n' ;
704
708
canonicalRequest += signedHeaders ( sessionToken ) + '\n' ;
705
- canonicalRequest += emptyPayloadHash ;
709
+ canonicalRequest += EMPTY_PAYLOAD_HASH ;
706
710
707
711
return canonicalRequest ;
708
712
}
@@ -938,7 +942,7 @@ async function fetchCredentials(r) {
938
942
if ( current ) {
939
943
// AWS returns Unix timestamps in seconds, but in Date constructor we should provide timestamp in milliseconds
940
944
const exp = new Date ( current . expiration * 1000 ) . getTime ( ) - maxValidityOffsetMs ;
941
- if ( now . getTime ( ) < exp ) {
945
+ if ( NOW . getTime ( ) < exp ) {
942
946
r . return ( 200 ) ;
943
947
return ;
944
948
}
@@ -949,7 +953,7 @@ async function fetchCredentials(r) {
949
953
_debug_log ( r , 'Cached credentials are expired or not present, requesting new ones' ) ;
950
954
951
955
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' ] ;
953
957
try {
954
958
credentials = await _fetchEcsRoleCredentials ( uri ) ;
955
959
} catch ( e ) {
@@ -1016,14 +1020,14 @@ async function _fetchEcsRoleCredentials(credentialsUri) {
1016
1020
* @private
1017
1021
*/
1018
1022
async function _fetchEC2RoleCredentials ( ) {
1019
- const tokenResp = await ngx . fetch ( ec2ImdsTokenEndpoint , {
1023
+ const tokenResp = await ngx . fetch ( EC2_IMDS_TOKEN_ENDPOINT , {
1020
1024
headers : {
1021
1025
'x-aws-ec2-metadata-token-ttl-seconds' : '21600' ,
1022
1026
} ,
1023
1027
method : 'PUT' ,
1024
1028
} ) ;
1025
1029
const token = await tokenResp . text ( ) ;
1026
- let resp = await ngx . fetch ( ec2ImdsSecurityCredentialsEndpoint , {
1030
+ let resp = await ngx . fetch ( EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT , {
1027
1031
headers : {
1028
1032
'x-aws-ec2-metadata-token' : token ,
1029
1033
} ,
@@ -1036,7 +1040,7 @@ async function _fetchEC2RoleCredentials() {
1036
1040
if ( credName === "" ) {
1037
1041
throw 'No credentials available for EC2 instance' ;
1038
1042
}
1039
- resp = await ngx . fetch ( ec2ImdsSecurityCredentialsEndpoint + credName , {
1043
+ resp = await ngx . fetch ( EC2_IMDS_SECURITY_CREDENTIALS_ENDPOINT + credName , {
1040
1044
headers : {
1041
1045
'x-aws-ec2-metadata-token' : token ,
1042
1046
} ,
0 commit comments