Skip to content

Commit a794f53

Browse files
author
iwysiu
authored
GODRIVER-1395 add MONGODB-AWS auth mechanism (mongodb#334)
1 parent 32aba96 commit a794f53

File tree

192 files changed

+40470
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

192 files changed

+40470
-323
lines changed

.evergreen/config.yml

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,212 @@ functions:
520520
-v \
521521
--fault revoked
522522
523+
add-aws-auth-variables-to-file:
524+
- command: shell.exec
525+
type: test
526+
params:
527+
working_dir: "src"
528+
silent: true
529+
script: |
530+
cat <<EOF > ${DRIVERS_TOOLS}/.evergreen/auth_aws/aws_e2e_setup.json
531+
{
532+
"iam_auth_ecs_account" : "${iam_auth_ecs_account}",
533+
"iam_auth_ecs_secret_access_key" : "${iam_auth_ecs_secret_access_key}",
534+
"iam_auth_ecs_account_arn": "arn:aws:iam::557821124784:user/authtest_fargate_user",
535+
"iam_auth_ecs_cluster": "${iam_auth_ecs_cluster}",
536+
"iam_auth_ecs_task_definition": "${iam_auth_ecs_task_definition}",
537+
"iam_auth_ecs_subnet_a": "${iam_auth_ecs_subnet_a}",
538+
"iam_auth_ecs_subnet_b": "${iam_auth_ecs_subnet_b}",
539+
"iam_auth_ecs_security_group": "${iam_auth_ecs_security_group}",
540+
"iam_auth_assume_aws_account" : "${iam_auth_assume_aws_account}",
541+
"iam_auth_assume_aws_secret_access_key" : "${iam_auth_assume_aws_secret_access_key}",
542+
"iam_auth_assume_role_name" : "${iam_auth_assume_role_name}",
543+
"iam_auth_ec2_instance_account" : "${iam_auth_ec2_instance_account}",
544+
"iam_auth_ec2_instance_secret_access_key" : "${iam_auth_ec2_instance_secret_access_key}",
545+
"iam_auth_ec2_instance_profile" : "${iam_auth_ec2_instance_profile}"
546+
}
547+
EOF
548+
549+
run-aws-auth-test-with-regular-aws-credentials:
550+
- command: shell.exec
551+
type: test
552+
params:
553+
working_dir: "src"
554+
script: |
555+
${PREPARE_SHELL}
556+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
557+
mongo aws_e2e_regular_aws.js
558+
- command: shell.exec
559+
type: test
560+
params:
561+
working_dir: "src"
562+
silent: true
563+
script: |
564+
cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
565+
alias urlencode='python -c "import sys, urllib as ul; sys.stdout.write(ul.quote_plus(sys.argv[1]))"'
566+
USER=$(urlencode ${iam_auth_ecs_account})
567+
PASS=$(urlencode ${iam_auth_ecs_secret_access_key})
568+
MONGODB_URI="mongodb://$USER:$PASS@localhost"
569+
EOF
570+
- command: shell.exec
571+
type: test
572+
params:
573+
working_dir: "src"
574+
script: |
575+
${PREPARE_SHELL}
576+
${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh
577+
578+
run-aws-auth-test-with-assume-role-credentials:
579+
- command: shell.exec
580+
type: test
581+
params:
582+
working_dir: "src"
583+
script: |
584+
${PREPARE_SHELL}
585+
# The aws_e2e_assume_role script requires python3 with boto3.
586+
virtualenv -p ${PYTHON3} mongovenv
587+
if [ "Windows_NT" = "$OS" ]; then
588+
. mongovenv/Scripts/activate
589+
else
590+
. mongovenv/bin/activate
591+
fi
592+
pip install boto3
593+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
594+
mongo aws_e2e_assume_role.js
595+
- command: shell.exec
596+
type: test
597+
params:
598+
working_dir: "src"
599+
silent: true
600+
script: |
601+
# DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
602+
cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
603+
alias urlencode='python -c "import sys, urllib as ul; sys.stdout.write(ul.quote_plus(sys.argv[1]))"'
604+
alias jsonkey='python -c "import json,sys;sys.stdout.write(json.load(sys.stdin)[sys.argv[1]])" < ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json'
605+
USER=$(jsonkey AccessKeyId)
606+
USER=$(urlencode $USER)
607+
PASS=$(jsonkey SecretAccessKey)
608+
PASS=$(urlencode $PASS)
609+
SESSION_TOKEN=$(jsonkey SessionToken)
610+
SESSION_TOKEN=$(urlencode $SESSION_TOKEN)
611+
MONGODB_URI="mongodb://$USER:$PASS@localhost"
612+
EOF
613+
- command: shell.exec
614+
type: test
615+
params:
616+
working_dir: "src"
617+
script: |
618+
${PREPARE_SHELL}
619+
${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh
620+
621+
run-aws-auth-test-with-aws-EC2-credentials:
622+
- command: shell.exec
623+
type: test
624+
params:
625+
working_dir: "src"
626+
script: |
627+
${PREPARE_SHELL}
628+
if [ "${SKIP_EC2_AUTH_TEST}" == "true" ]; then
629+
echo "This platform does not support the EC2 auth test, skipping..."
630+
exit 0
631+
fi
632+
# The mongovenv was created earlier in run-aws-auth-test-with-assume-role-credentials.
633+
if [ "Windows_NT" = "$OS" ]; then
634+
. mongovenv/Scripts/activate
635+
else
636+
. mongovenv/bin/activate
637+
fi
638+
cd ${DRIVERS_TOOLS}/.evergreen/auth_aws
639+
mongo aws_e2e_ec2.js
640+
- command: shell.exec
641+
type: test
642+
params:
643+
working_dir: "src"
644+
script: |
645+
${PREPARE_SHELL}
646+
if [ "${SKIP_EC2_AUTH_TEST}" == "true" ]; then
647+
exit 0
648+
fi
649+
${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh
650+
651+
run-aws-auth-test-with-aws-credentials-as-environment-variables:
652+
- command: shell.exec
653+
type: test
654+
params:
655+
working_dir: "src"
656+
silent: true
657+
script: |
658+
# DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
659+
cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
660+
export AWS_ACCESS_KEY_ID=${iam_auth_ecs_account}
661+
export AWS_SECRET_ACCESS_KEY=${iam_auth_ecs_secret_access_key}
662+
EOF
663+
- command: shell.exec
664+
type: test
665+
params:
666+
working_dir: "src"
667+
script: |
668+
${PREPARE_SHELL}
669+
PROJECT_DIRECTORY=${PROJECT_DIRECTORY} ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh
670+
671+
run-aws-auth-test-with-aws-credentials-and-session-token-as-environment-variables:
672+
- command: shell.exec
673+
type: test
674+
params:
675+
working_dir: "src"
676+
silent: true
677+
script: |
678+
# DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
679+
cat <<'EOF' > "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
680+
alias jsonkey='python -c "import json,sys;sys.stdout.write(json.load(sys.stdin)[sys.argv[1]])" < ${DRIVERS_TOOLS}/.evergreen/auth_aws/creds.json'
681+
export AWS_ACCESS_KEY_ID=$(jsonkey AccessKeyId)
682+
export AWS_SECRET_ACCESS_KEY=$(jsonkey SecretAccessKey)
683+
export AWS_SESSION_TOKEN=$(jsonkey SessionToken)
684+
EOF
685+
- command: shell.exec
686+
type: test
687+
params:
688+
working_dir: "src"
689+
script: |
690+
${PREPARE_SHELL}
691+
${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-test.sh
692+
693+
run-aws-ECS-auth-test:
694+
- command: shell.exec
695+
type: test
696+
params:
697+
working_dir: src/go.mongodb.org/mongo-driver
698+
script: |
699+
${PREPARE_SHELL}
700+
if [ "${SKIP_ECS_AUTH_TEST}" == "true" ]; then
701+
echo "This platform does not support the ECS auth test, skipping..."
702+
exit 0
703+
fi
704+
make build-aws-ecs-test
705+
- command: shell.exec
706+
type: test
707+
params:
708+
working_dir: "src"
709+
script: |
710+
${PREPARE_SHELL}
711+
if [ "${SKIP_ECS_AUTH_TEST}" == "true" ]; then
712+
exit 0
713+
fi
714+
AUTH_AWS_DIR=${DRIVERS_TOOLS}/.evergreen/auth_aws
715+
ECS_SRC_DIR=$AUTH_AWS_DIR/src
716+
# pack up project directory to ssh it to the container
717+
mkdir -p $ECS_SRC_DIR/.evergreen
718+
cp ${PROJECT_DIRECTORY}/main $ECS_SRC_DIR
719+
cp ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-aws-ecs-test.sh $ECS_SRC_DIR/.evergreen
720+
tar -czf $ECS_SRC_DIR/src.tgz -C $PROJECT_DIRECTORY .
721+
cd $AUTH_AWS_DIR
722+
cat <<EOF > setup.js
723+
const mongo_binaries = "$MONGODB_BINARIES";
724+
const project_dir = "$ECS_SRC_DIR";
725+
EOF
726+
cat setup.js
727+
mongo --nodb setup.js aws_e2e_ecs.js
728+
523729
pre:
524730
- func: fetch-source
525731
- func: prepare-resources
@@ -1236,6 +1442,21 @@ tasks:
12361442
commands:
12371443
- func: "run-atlas-test"
12381444

1445+
- name: "aws-auth-test"
1446+
commands:
1447+
- func: bootstrap-mongo-orchestration
1448+
vars:
1449+
AUTH: "auth"
1450+
ORCHESTRATION_FILE: "auth-aws.json"
1451+
TOPOLOGY: "server"
1452+
- func: add-aws-auth-variables-to-file
1453+
- func: run-aws-auth-test-with-regular-aws-credentials
1454+
- func: run-aws-auth-test-with-assume-role-credentials
1455+
- func: run-aws-auth-test-with-aws-credentials-as-environment-variables
1456+
- func: run-aws-auth-test-with-aws-credentials-and-session-token-as-environment-variables
1457+
- func: run-aws-auth-test-with-aws-EC2-credentials
1458+
- func: run-aws-ECS-auth-test
1459+
12391460
- name: go1.10-build-cse
12401461
tags: ["compile-check"]
12411462
commands:
@@ -1317,6 +1538,33 @@ axes:
13171538
variables:
13181539
GO_DIST: "/opt/golang/go1.12"
13191540

1541+
- id: os-aws-auth
1542+
display_name: OS
1543+
values:
1544+
- id: "windows-64-vsMulti-small-go-1-12"
1545+
display_name: "Windows 64-bit"
1546+
run_on:
1547+
- windows-64-vsMulti-small
1548+
variables:
1549+
GCC_PATH: "/cygdrive/c/mingw-w64/x86_64-4.9.1-posix-seh-rt_v3-rev1/mingw64/bin"
1550+
GO_DIST: "C:\\golang\\go1.12"
1551+
SKIP_ECS_AUTH_TEST: true
1552+
PYTHON3: "C:/python/Python38/python.exe"
1553+
- id: "ubuntu1804-64-go-1-12"
1554+
display_name: "Ubuntu 18.04"
1555+
run_on: ubuntu1804-test
1556+
variables:
1557+
GO_DIST: "/opt/golang/go1.12"
1558+
PYTHON3: python3
1559+
- id: "osx-go-1-12"
1560+
display_name: "MacOS 10.14"
1561+
run_on: macos-1014
1562+
variables:
1563+
GO_DIST: "/opt/golang/go1.12"
1564+
SKIP_ECS_AUTH_TEST: true
1565+
SKIP_EC2_AUTH_TEST: true
1566+
PYTHON3: python3
1567+
13201568
buildvariants:
13211569
- name: static-analysis
13221570
display_name: "Static Analysis"
@@ -1405,3 +1653,9 @@ buildvariants:
14051653
display_name: "Enterprise Auth - ${os-ssl-32}"
14061654
tasks:
14071655
- name: ".test .enterprise-auth"
1656+
1657+
- matrix_name: "aws-auth-test"
1658+
matrix_spec: { os-aws-auth: "*" }
1659+
display_name: "MONGODB-AWS Auth ${os-aws-auth}"
1660+
tasks:
1661+
- name: "aws-auth-test"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
set -o xtrace
4+
set -o errexit # Exit the script with error if any of the commands fail
5+
6+
############################################
7+
# Main Program #
8+
############################################
9+
10+
if [[ -z "$1" ]]; then
11+
echo "usage: $0 <MONGODB_URI>"
12+
exit 1
13+
fi
14+
export MONGODB_URI="$1"
15+
16+
echo "Running MONGODB-AWS ECS authentication tests"
17+
18+
if echo "$MONGODB_URI" | grep -q "@"; then
19+
echo "MONGODB_URI unexpectedly contains user credentials in ECS test!";
20+
exit 1
21+
fi
22+
23+
./src/main

.evergreen/run-mongodb-aws-test.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
set -o xtrace
4+
set -o errexit # Exit the script with error if any of the commands fail
5+
6+
############################################
7+
# Main Program #
8+
############################################
9+
10+
# Supported/used environment variables:
11+
# MONGODB_URI Set the URI, including an optional username/password to use
12+
# to connect to the server via MONGODB-AWS authentication
13+
# mechanism.
14+
15+
echo "Running MONGODB-AWS authentication tests"
16+
# ensure no secrets are printed in log files
17+
set +x
18+
19+
# load the script
20+
shopt -s expand_aliases # needed for `urlencode` alias
21+
[ -s "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh" ] && source "${PROJECT_DIRECTORY}/prepare_mongodb_aws.sh"
22+
23+
MONGODB_URI=${MONGODB_URI:-"mongodb://localhost"}
24+
MONGODB_URI="${MONGODB_URI}/aws?authMechanism=MONGODB-AWS"
25+
if [[ -n ${SESSION_TOKEN} ]]; then
26+
MONGODB_URI="${MONGODB_URI}&authMechanismProperties=AWS_SESSION_TOKEN:${SESSION_TOKEN}"
27+
fi
28+
29+
export MONGODB_URI="$MONGODB_URI"
30+
31+
# show test output
32+
set -x
33+
34+
go run "${PROJECT_DIRECTORY}/mongo/testaws/main.go"

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ evg-test-atlas:
138138
evg-test-ocsp:
139139
go test -v ./mongo -run TestOCSP $(OCSP_TLS_SHOULD_SUCCEED) >> test.suite
140140

141+
.PHONY: build-aws-ecs-test
142+
build-aws-ecs-test:
143+
go build $(BUILD_TAGS) ./mongo/testaws/main.go
144+
141145
# benchmark specific targets and support
142146
perf:driver-test-data.tar.gz
143147
tar -zxf $< $(if $(eq $(UNAME_S),Darwin),-s , --transform=s)/data/perf/

data/auth/connection-string.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,50 @@
355355
"description": "authSource without username is invalid (default mechanism)",
356356
"uri": "mongodb://localhost/?authSource=foo",
357357
"valid": false
358+
},
359+
{
360+
"description": "should recognise the mechanism (MONGODB-AWS)",
361+
"uri": "mongodb://localhost/?authMechanism=MONGODB-AWS",
362+
"valid": true,
363+
"credential": {
364+
"username": null,
365+
"password": null,
366+
"source": "$external",
367+
"mechanism": "MONGODB-AWS",
368+
"mechanism_properties": null
369+
}
370+
},
371+
{
372+
"description": "should throw an exception if username and no password (MONGODB-AWS)",
373+
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-AWS",
374+
"valid": false,
375+
"credential": null
376+
},
377+
{
378+
"description": "should use username and password if specified (MONGODB-AWS)",
379+
"uri": "mongodb://user%21%40%23%24%25%5E%26%2A%28%29_%2B:pass%21%40%23%24%25%5E%26%2A%28%29_%2B@localhost/?authMechanism=MONGODB-AWS",
380+
"valid": true,
381+
"credential": {
382+
"username": "user!@#$%^&*()_+",
383+
"password": "pass!@#$%^&*()_+",
384+
"source": "$external",
385+
"mechanism": "MONGODB-AWS",
386+
"mechanism_properties": null
387+
}
388+
},
389+
{
390+
"description": "should use username, password and session token if specified (MONGODB-AWS)",
391+
"uri": "mongodb://user:password@localhost/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:token%21%40%23%24%25%5E%26%2A%28%29_%2B",
392+
"valid": true,
393+
"credential": {
394+
"username": "user",
395+
"password": "password",
396+
"source": "$external",
397+
"mechanism": "MONGODB-AWS",
398+
"mechanism_properties": {
399+
"AWS_SESSION_TOKEN": "token!@#$%^&*()_+"
400+
}
401+
}
358402
}
359403
]
360404
}

0 commit comments

Comments
 (0)