Skip to content

Commit 47d58be

Browse files
shawnhankimdekobon
authored andcommitted
fix: session token env in nginx-oss
chore: unit test for reading credentials fix: unused function fix: comment for title of unit tests
1 parent ad5fe25 commit 47d58be

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

standalone_ubuntu_oss_install.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ if [ $uses_iam_creds -eq 0 ]; then
186186
S3_ACCESS_KEY_ID=${S3_ACCESS_KEY_ID}
187187
# AWS Secret access key
188188
S3_SECRET_KEY=${S3_SECRET_KEY}
189+
EOF
190+
if [[ -v S3_SESSION_TOKEN ]]; then
191+
cat >> "/etc/nginx/environment" << EOF
189192
# AWS Session Token
190193
S3_SESSION_TOKEN=${S3_SESSION_TOKEN}
191194
EOF
195+
fi
192196
fi
193197

194198
set +o nounset # don't abort on unbound variable
@@ -287,6 +291,9 @@ if [ $uses_iam_creds -eq 0 ]; then
287291
cat >> "/etc/nginx/environment" << EOF
288292
env S3_ACCESS_KEY_ID;
289293
env S3_SECRET_KEY;
294+
EOF
295+
if [[ -v S3_SESSION_TOKEN ]]; then
296+
cat >> "/etc/nginx/environment" << EOF
290297
env S3_SESSION_TOKEN;
291298
EOF
292299
fi

test.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fi
265265

266266
### UNIT TESTS
267267

268-
p "Running unit tests in Docker image"
268+
p "Running unit tests with an access key ID and a secret key in Docker image"
269269
#MSYS_NO_PATHCONV=1 added to resolve automatic path conversion
270270
# https://github.com/docker/for-win/issues/6754#issuecomment-629702199
271271
MSYS_NO_PATHCONV=1 "${docker_cmd}" run \
@@ -286,6 +286,27 @@ MSYS_NO_PATHCONV=1 "${docker_cmd}" run \
286286
--entrypoint /usr/bin/njs \
287287
nginx-s3-gateway -t module -p '/etc/nginx' /var/tmp/s3gateway_test.js
288288

289+
p "Running unit tests with a session token in Docker image"
290+
#MSYS_NO_PATHCONV=1 added to resolve automatic path conversion
291+
# https://github.com/docker/for-win/issues/6754#issuecomment-629702199
292+
MSYS_NO_PATHCONV=1 "${docker_cmd}" run \
293+
--rm \
294+
-v "$(pwd)/test/unit:/var/tmp" \
295+
--workdir /var/tmp \
296+
-e "S3_DEBUG=true" \
297+
-e "S3_STYLE=virtual" \
298+
-e "S3_ACCESS_KEY_ID=unit_test" \
299+
-e "S3_SECRET_KEY=unit_test" \
300+
-e "S3_BUCKET_NAME=unit_test" \
301+
-e "S3_SERVER=unit_test" \
302+
-e "S3_SERVER_PROTO=https" \
303+
-e "S3_SERVER_PORT=443" \
304+
-e "S3_REGION=test-1" \
305+
-e "AWS_SIGS_VERSION=4" \
306+
--entrypoint /usr/bin/njs \
307+
nginx-s3-gateway -t module -p '/etc/nginx' /var/tmp/s3gateway_test.js
308+
309+
289310
### INTEGRATION TESTS
290311

291312
p "Testing API with AWS Signature V2 and allow directory listing off"

test/unit/s3gateway_test.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,14 @@ function testEscapeURIPathPreservesDoubleSlashes() {
368368
}
369369
}
370370

371-
function testReadCredentialsWithAccessAndSecretKeySet() {
372-
printHeader('testReadCredentialsWithAccessAndSecretKeySet');
371+
function testReadCredentialsWithAccessSecretKeyAndSessionTokenSet() {
372+
printHeader('testReadCredentialsWithAccessSecretKeyAndSessionTokenSet');
373373
let r = {};
374374
process.env['S3_ACCESS_KEY_ID'] = 'SOME_ACCESS_KEY';
375375
process.env['S3_SECRET_KEY'] = 'SOME_SECRET_KEY';
376-
process.env['S3_SESSION_TOKEN'] = 'SOME_SESSION_TOKEN';
376+
if ('S3_SESSION_TOKEN' in process.env) {
377+
process.env['S3_SESSION_TOKEN'] = 'SOME_SESSION_TOKEN';
378+
}
377379

378380
try {
379381
var credentials = s3gateway.readCredentials(r);
@@ -383,8 +385,14 @@ function testReadCredentialsWithAccessAndSecretKeySet() {
383385
if (credentials.secretAccessKey !== process.env['S3_SECRET_KEY']) {
384386
throw 'static credentials do not match returned value [secretAccessKey]';
385387
}
386-
if (credentials.sessionToken !== process.env['S3_SESSION_TOKEN']) {
387-
throw 'static credentials do not match returned value [sessionToken]';
388+
if ('S3_SESSION_TOKEN' in process.env) {
389+
if (credentials.sessionToken !== process.env['S3_SESSION_TOKEN']) {
390+
throw 'static credentials do not match returned value [sessionToken]';
391+
}
392+
} else {
393+
if (credentials.sessionToken !== null) {
394+
throw 'static credentials do not match returned value [sessionToken]';
395+
}
388396
}
389397
if (credentials.expiration !== null) {
390398
throw 'static credentials do not match returned value [expiration]';
@@ -710,7 +718,7 @@ async function test() {
710718
testEditHeaders();
711719
testEditHeadersHeadDirectory();
712720
testEscapeURIPathPreservesDoubleSlashes();
713-
testReadCredentialsWithAccessAndSecretKeySet();
721+
testReadCredentialsWithAccessSecretKeyAndSessionTokenSet();
714722
testReadCredentialsFromFilePath();
715723
testReadCredentialsFromNonexistentPath();
716724
testReadAndWriteCredentialsFromKeyValStore();

0 commit comments

Comments
 (0)