Skip to content

Commit 2420025

Browse files
dekobonshawnhankim
authored andcommitted
feat: update from nginx/nginx-s3-gateway#119
1 parent 961ff9b commit 2420025

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ start-plus:
55
export NGINX_TYPE=plus && docker-compose -f tests/docker/docker-compose.yml up -d
66

77
test:
8-
bash tests/test.sh --type oss
8+
export NGINX_TYPE=oss && bash tests/test.sh --type oss
99

1010
test-plus:
11-
bash tests/test.sh --type plus
11+
export NGINX_TYPE=plus && bash tests/test.sh --type plus
1212

1313
down:
1414
docker-compose -f tests/docker/docker-compose.yml down

core/awscredentials.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ function readCredentials(r) {
8888
expiration: null
8989
};
9090
}
91-
9291
if ("variables" in r && r.variables.cache_instance_credentials_enabled == 1) {
9392
return _readCredentialsFromKeyValStore(r);
9493
} else {
@@ -228,7 +227,7 @@ function _writeCredentialsToFile(credentials) {
228227
async function fetchCredentials(r) {
229228
/* If we are not using an AWS instance profile to set our credentials we
230229
exit quickly and don't write a credentials file. */
231-
if (process.env['AWS_ACCESS_KEY_ID'] && process.env['AWS_SECRET_ACCESS_KEY']) {
230+
if (utils.areAllEnvVarsSet(['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'])) {
232231
r.return(200);
233232
return;
234233
}
@@ -258,8 +257,9 @@ async function fetchCredentials(r) {
258257

259258
utils.debug_log(r, 'Cached credentials are expired or not present, requesting new ones');
260259

261-
if (process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI']) {
262-
const uri = ECS_CREDENTIAL_BASE_URI + process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
260+
if (utils.areAllEnvVarsSet('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI')) {
261+
const relative_uri = process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] || '';
262+
const uri = ECS_CREDENTIAL_BASE_URI + relative_uri;
263263
try {
264264
credentials = await _fetchEcsRoleCredentials(uri);
265265
} catch (e) {
@@ -268,7 +268,7 @@ async function fetchCredentials(r) {
268268
return;
269269
}
270270
}
271-
else if (process.env['AWS_WEB_IDENTITY_TOKEN_FILE']) {
271+
else if (utils.areAllEnvVarsSet('AWS_WEB_IDENTITY_TOKEN_FILE')) {
272272
try {
273273
credentials = await _fetchWebIdentityCredentials(r)
274274
} catch (e) {

core/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
const DEBUG = parseBoolean(process.env['DEBUG']);
2323

2424

25+
/**
26+
* Checks to see if all of the elements of the passed array are present as keys
27+
* in the running process' environment variables. Alternatively, if a single
28+
* string is passed, it will check for the presence of that string.
29+
* @param envVars {array[string]|string} array of expected keys or single expected key
30+
* @returns {boolean} true if all keys are set as environment variables
31+
*/
32+
function areAllEnvVarsSet(envVars) {
33+
if (envVars instanceof Array) {
34+
const envVarsLen = envVars.length;
35+
for (let i = 0; i < envVarsLen; i++) {
36+
if (!process.env[envVars[i]]) {
37+
return false;
38+
}
39+
}
40+
return true;
41+
}
42+
return envVars in process.env;
43+
}
44+
2545
/**
2646
* Parses a string delimited by semicolons into an array of values
2747
* @param string {string|null} value representing a array of strings
@@ -143,6 +163,7 @@ function requireEnvVar(envVarName) {
143163
}
144164

145165
export default {
166+
areAllEnvVarsSet,
146167
debug_log,
147168
getAmzDatetime,
148169
getEightDigitDate,

tests/unit-test/awscredentials_test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ function testReadAndWriteCredentialsFromKeyValStore() {
180180

181181
async function testEcsCredentialRetrieval() {
182182
printHeader('testEcsCredentialRetrieval');
183-
process.env['AWS_ACCESS_KEY_ID'] = undefined;
183+
if ('AWS_ACCESS_KEY_ID' in process.env) {
184+
delete process.env['AWS_ACCESS_KEY_ID'];
185+
}
184186
process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] = '/example';
185187
globalThis.ngx.fetch = function (url) {
188+
console.log(' fetching mock credentials');
186189
globalThis.recordedUrl = url;
187190

188191
return Promise.resolve({
@@ -221,14 +224,18 @@ async function testEcsCredentialRetrieval() {
221224
await awscred.fetchCredentials(r);
222225

223226
if (globalThis.recordedUrl !== 'http://169.254.170.2/example') {
224-
throw 'No or wrong ECS credentials fetch URL recorded: ' + globalThis.recordedUrl;
227+
throw `No or wrong ECS credentials fetch URL recorded: ${globalThis.recordedUrl}`;
225228
}
226229
}
227230

228231
async function testEc2CredentialRetrieval() {
229232
printHeader('testEc2CredentialRetrieval');
230-
process.env['AWS_ACCESS_KEY_ID'] = undefined;
231-
process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'] = undefined;
233+
if ('AWS_ACCESS_KEY_ID' in process.env) {
234+
delete process.env['AWS_ACCESS_KEY_ID'];
235+
}
236+
if ('AWS_CONTAINER_CREDENTIALS_RELATIVE_URI' in process.env) {
237+
delete process.env['AWS_CONTAINER_CREDENTIALS_RELATIVE_URI'];
238+
}
232239
globalThis.ngx.fetch = function (url, options) {
233240
if (url === 'http://169.254.169.254/latest/api/token' && options && options.method === 'PUT') {
234241
return Promise.resolve({

tests/unit-test/utils_test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,72 @@ function testPad() {
122122
}
123123
}
124124

125+
function testAreAllEnvVarsSet() {
126+
function testAreAllEnvVarsSetStringFound() {
127+
console.log(' ## testAreAllEnvVarsSetStringFound');
128+
const key = 'TEST_ENV_VAR_KEY';
129+
process.env[key] = 'some value';
130+
const actual = utils.areAllEnvVarsSet(key);
131+
if (!actual) {
132+
throw 'Environment variable that was set not indicated as present';
133+
}
134+
}
135+
136+
function testAreAllEnvVarsSetStringNotFound() {
137+
console.log(' ## testAreAllEnvVarsSetStringNotFound');
138+
const actual = utils.areAllEnvVarsSet('UNKNOWN_ENV_VAR_KEY');
139+
if (actual) {
140+
throw 'Unknown environment variable indicated as being present';
141+
}
142+
}
143+
144+
function testAreAllEnvVarsSetStringArrayFound() {
145+
console.log(' ## testAreAllEnvVarsSetStringArrayFound');
146+
const keys = ['TEST_ENV_VAR_KEY_1', 'TEST_ENV_VAR_KEY_2', 'TEST_ENV_VAR_KEY_3'];
147+
for (let i = 0; i < keys.length; i++) {
148+
process.env[keys[i]] = 'something';
149+
}
150+
const actual = utils.areAllEnvVarsSet(keys);
151+
if (!actual) {
152+
throw 'Environment variables that were set not indicated as present';
153+
}
154+
}
155+
156+
function testAreAllEnvVarsSetStringArrayNotFound() {
157+
console.log(' ## testAreAllEnvVarsSetStringArrayNotFound');
158+
const keys = ['UNKNOWN_ENV_VAR_KEY_1', 'UNKNOWN_ENV_VAR_KEY_2', 'UNKNOWN_ENV_VAR_KEY_3'];
159+
const actual = utils.areAllEnvVarsSet(keys);
160+
if (actual) {
161+
throw 'Unknown environment variables that were not set indicated as present';
162+
}
163+
}
164+
165+
function testAreAllEnvVarsSetStringArrayWithSomeSet() {
166+
console.log(' ## testAreAllEnvVarsSetStringArrayWithSomeSet');
167+
const keys = ['TEST_ENV_VAR_KEY_1', 'UNKNOWN_ENV_VAR_KEY_2', 'UNKNOWN_ENV_VAR_KEY_3'];
168+
process.env[keys[0]] = 'something';
169+
170+
const actual = utils.areAllEnvVarsSet(keys);
171+
if (actual) {
172+
throw 'Unknown environment variables that were not set indicated as present';
173+
}
174+
}
175+
176+
printHeader('testAreAllEnvVarsSet');
177+
testAreAllEnvVarsSetStringFound();
178+
testAreAllEnvVarsSetStringNotFound();
179+
testAreAllEnvVarsSetStringArrayFound();
180+
testAreAllEnvVarsSetStringArrayNotFound();
181+
testAreAllEnvVarsSetStringArrayWithSomeSet();
182+
}
183+
184+
125185
async function test() {
126186
testAmzDatetime();
127187
testEightDigitDate();
128188
testPad();
129189
testParseArray();
190+
testAreAllEnvVarsSet();
130191
}
131192

132193
function printHeader(testName) {

0 commit comments

Comments
 (0)