Skip to content

Commit 44d21d4

Browse files
committed
fixed reported problems VSC's eslint
- r.variables contains only strings, so r.variables.cache_instance_credentials_enabled == 1 may cause issues - updated typedef S3ReqParams and make properties optional - NjsStringOrBuffer has no indexOf, so explicitly invoke toString() - import Credentials typedefs where missed
1 parent d6fb9b0 commit 44d21d4

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

common/etc/nginx/include/awscredentials.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ function readCredentials(r) {
112112
expiration: null,
113113
}
114114
}
115-
if ('variables' in r && r.variables.cache_instance_credentials_enabled == 1) {
115+
if (
116+
'variables' in r &&
117+
r.variables.cache_instance_credentials_enabled == '1'
118+
) {
116119
return _readCredentialsFromKeyValStore(r)
117120
} else {
118121
return _readCredentialsFromFile()
@@ -157,7 +160,7 @@ function _readCredentialsFromFile() {
157160

158161
try {
159162
const creds = fs.readFileSync(credsFilePath)
160-
return JSON.parse(creds)
163+
return JSON.parse(creds.toString())
161164
} catch (e) {
162165
/* Do not throw an exception in the case of when the
163166
credentials file path is invalid in order to signal to
@@ -206,7 +209,10 @@ function writeCredentials(r, credentials) {
206209
throw `Cannot write invalid credentials: ${JSON.stringify(credentials)}`
207210
}
208211

209-
if ('variables' in r && r.variables.cache_instance_credentials_enabled == 1) {
212+
if (
213+
'variables' in r &&
214+
r.variables.cache_instance_credentials_enabled == '1'
215+
) {
210216
_writeCredentialsToKeyValStore(r, credentials)
211217
} else {
212218
_writeCredentialsToFile(credentials)
@@ -352,6 +358,8 @@ async function _fetchEcsRoleCredentials(credentialsUri) {
352358
if (!resp.ok) {
353359
throw 'Credentials endpoint response was not ok.'
354360
}
361+
362+
/** @type {any} */
355363
const creds = await resp.json()
356364

357365
return {
@@ -395,6 +403,7 @@ async function _fetchEC2RoleCredentials() {
395403
'x-aws-ec2-metadata-token': token,
396404
},
397405
})
406+
/** @type {any} */
398407
const creds = await resp.json()
399408

400409
return {
@@ -453,7 +462,9 @@ async function _fetchWebIdentityCredentials(_r) {
453462
method: 'GET',
454463
})
455464

465+
/** @type {any} */
456466
const resp = await response.json()
467+
457468
const creds =
458469
resp.AssumeRoleWithWebIdentityResponse.AssumeRoleWithWebIdentityResult
459470
.Credentials

common/etc/nginx/include/awssig2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/**
1919
* @module awssig2
2020
* @alias AwsSig2
21+
* @typedef {import('./awscredentials.js').Credentials} Credentials
2122
*/
2223

2324
import utils from './utils.js'

common/etc/nginx/include/awssig4.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/**
1919
* @module awssig4
2020
* @alias AwsSig4
21+
* @typedef {import('./awscredentials.js').Credentials} Credentials
2122
*/
2223

2324
import awscred from './awscredentials.js'
@@ -189,7 +190,7 @@ function _buildSignatureV4(
189190
* operations that have to be performed per incoming request. The signing
190191
* key expires every day, so our cache key can persist for 24 hours safely.
191192
*/
192-
if ('variables' in r && r.variables.cache_signing_key_enabled == 1) {
193+
if ('variables' in r && r.variables.cache_signing_key_enabled == '1') {
193194
// cached value is in the format: [eightDigitDate]:[signingKeyHash]
194195
const cached =
195196
'signing_key_hash' in r.variables ? r.variables.signing_key_hash : ''
@@ -306,7 +307,7 @@ function _signedHeaders(r, sessionToken) {
306307
* @param eightDigitDate {string} date in the form of 'YYYYMMDD'
307308
* @param region {string} region associated with server API
308309
* @param service {string} name of service that request is for e.g. s3, lambda
309-
* @returns {ArrayBuffer} signing HMAC
310+
* @returns {Buffer} signing HMAC
310311
* @private
311312
*/
312313
function _buildSigningKeyHash(kSecret, eightDigitDate, region, service) {
@@ -376,7 +377,7 @@ function awsHeaderDate(_r) {
376377
function awsHeaderPayloadHash(r) {
377378
const reqBody = r.variables.request_body ? r.variables.request_body : ''
378379
const payloadHash = mod_hmac
379-
.createHash('sha256', 'utf8')
380+
.createHash('sha256')
380381
.update(reqBody)
381382
.digest('hex')
382383
return payloadHash

common/etc/nginx/include/s3gateway.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
/**
2424
* @typedef {Object} S3ReqParams
2525
* @property {string} uri - URI to use for S3 request
26-
* @property {string|undefined} httpDate - RFC2616 timestamp used to sign the request
27-
* @property {string|undefined} host - S3 host to use for request
28-
* @property {string|undefined} queryParams - query parameters to use with S3 request
26+
* @property {string|undefined} [httpDate] - RFC2616 timestamp used to sign the request
27+
* @property {string|undefined} [host] - S3 host to use for request
28+
* @property {string|undefined} [queryParams] - query parameters to use with S3 request
2929
*/
3030

3131
import awscred from './awscredentials.js'
@@ -449,13 +449,13 @@ function filterListResponse(r, data, flags) {
449449
if (FOUR_O_FOUR_ON_EMPTY_BUCKET) {
450450
let indexIsEmpty = utils.parseBoolean(r.variables.indexIsEmpty)
451451

452-
if (indexIsEmpty && data.indexOf('<Contents') >= 0) {
453-
r.variables.indexIsEmpty = false
452+
if (indexIsEmpty && data.toString().indexOf('<Contents') >= 0) {
453+
r.variables.indexIsEmpty = 'false'
454454
indexIsEmpty = false
455455
}
456456

457-
if (indexIsEmpty && data.indexOf('<CommonPrefixes') >= 0) {
458-
r.variables.indexIsEmpty = false
457+
if (indexIsEmpty && data.toString().indexOf('<CommonPrefixes') >= 0) {
458+
r.variables.indexIsEmpty = 'false'
459459
indexIsEmpty = false
460460
}
461461

test/unit/awscredentials_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function testReadCredentialsFromFilePath() {
6565
printHeader('testReadCredentialsFromFilePath');
6666
let r = {
6767
variables: {
68-
cache_instance_credentials_enabled: 0
68+
cache_instance_credentials_enabled: '0'
6969
}
7070
};
7171

@@ -107,7 +107,7 @@ function testReadCredentialsFromNonexistentPath() {
107107
printHeader('testReadCredentialsFromNonexistentPath');
108108
let r = {
109109
variables: {
110-
cache_instance_credentials_enabled: 0
110+
cache_instance_credentials_enabled: '0'
111111
}
112112
};
113113
var originalCredentialPath = process.env['AWS_CREDENTIALS_TEMP_FILE'];
@@ -149,7 +149,7 @@ function testReadAndWriteCredentialsFromKeyValStore() {
149149
try {
150150
let r = {
151151
variables: {
152-
cache_instance_credentials_enabled: 1,
152+
cache_instance_credentials_enabled: '1',
153153
instance_credential_json: null
154154
}
155155
};
@@ -234,7 +234,7 @@ async function testEc2CredentialRetrieval() {
234234
delete process.env['AWS_ACCESS_KEY_ID'];
235235
}
236236
if ('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI' in process.env) {
237-
delete process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
237+
delete process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
238238
}
239239
globalThis.ngx.fetch = function (url, options) {
240240
if (url === 'http://169.254.169.254/latest/api/token' && options && options.method === 'PUT') {

test/unit/awssig4_test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ function _runSignatureV4(r) {
7171
queryParams : '',
7272
host: bucket.concat('.', server)
7373
}
74-
const canonicalRequest = awssig4._buildCanonicalRequest(r,
74+
const canonicalRequest = awssig4._buildCanonicalRequest(r,
7575
r.method, req.uri, req.queryParams, req.host, amzDatetime, creds.sessionToken);
7676

7777
var expected = '600721cacc21e3de14416de7517868381831f4709e5c5663bbf2b738e4d5abe4';
78-
var signature = awssig4._buildSignatureV4(r,
78+
var signature = awssig4._buildSignatureV4(r,
7979
amzDatetime, eightDigitDate, creds, region, service, canonicalRequest);
80-
80+
8181
if (signature !== expected) {
8282
throw 'V4 signature hash was not created correctly.\n' +
8383
'Actual: [' + signature + ']\n' +
@@ -144,7 +144,7 @@ function testSignatureV4Cache() {
144144
"foo" : "bar"
145145
},
146146
"variables": {
147-
"cache_signing_key_enabled": 1,
147+
"cache_signing_key_enabled": '1',
148148
"request_body": "",
149149
"uri_path": "/a/c/ramen.jpg"
150150
},

0 commit comments

Comments
 (0)