Skip to content

Commit bce047d

Browse files
authored
PYTHON-4309 OIDC Atlas Testing Updates (#1567)
1 parent 6148891 commit bce047d

File tree

4 files changed

+55
-64
lines changed

4 files changed

+55
-64
lines changed

.evergreen/config.yml

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ functions:
527527
AWS_ROLE_SESSION_NAME="test" \
528528
.evergreen/run-mongodb-aws-test.sh web-identity
529529
530-
"run oidc auth test with aws credentials":
530+
"run oidc auth test with test credentials":
531531
- command: subprocess.exec
532532
type: test
533533
params:
@@ -991,6 +991,30 @@ task_groups:
991991
tasks:
992992
- oidc-auth-test-azure-latest
993993

994+
- name: testoidc_task_group
995+
setup_group:
996+
- func: fetch source
997+
- func: prepare resources
998+
- func: fix absolute paths
999+
- func: make files executable
1000+
- func: "assume ec2 role"
1001+
- command: subprocess.exec
1002+
params:
1003+
binary: bash
1004+
include_expansions_in_env: ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"]
1005+
args:
1006+
- ${DRIVERS_TOOLS}/.evergreen/auth_oidc/setup.sh
1007+
teardown_task:
1008+
- command: subprocess.exec
1009+
params:
1010+
binary: bash
1011+
args:
1012+
- ${DRIVERS_TOOLS}/.evergreen/auth_oidc/teardown.sh
1013+
setup_group_can_fail_task: true
1014+
setup_group_timeout_secs: 1800
1015+
tasks:
1016+
- oidc-auth-test-latest
1017+
9941018
- name: test_aws_lambda_task_group
9951019
setup_group:
9961020
- func: fetch source
@@ -1913,11 +1937,6 @@ tasks:
19131937
- func: "run aws auth test with aws web identity credentials"
19141938
- func: "run aws ECS auth test"
19151939

1916-
- name: "oidc-auth-test-latest"
1917-
commands:
1918-
- func: "assume ec2 role"
1919-
- func: "run oidc auth test with aws credentials"
1920-
19211940
- name: load-balancer-test
19221941
commands:
19231942
- func: "bootstrap mongo-orchestration"
@@ -1927,6 +1946,10 @@ tasks:
19271946
- func: "run load-balancer"
19281947
- func: "run tests"
19291948

1949+
- name: "oidc-auth-test-latest"
1950+
commands:
1951+
- func: "run oidc auth test with test credentials"
1952+
19301953
- name: "oidc-auth-test-azure-latest"
19311954
commands:
19321955
- command: shell.exec
@@ -1940,7 +1963,7 @@ tasks:
19401963
git commit -m "add files"
19411964
export AZUREOIDC_DRIVERS_TAR_FILE=/tmp/mongo-python-driver.tgz
19421965
git archive -o $AZUREOIDC_DRIVERS_TAR_FILE HEAD
1943-
export AZUREOIDC_TEST_CMD="source ./env.sh && export OIDC_ENV=azure && ./.evergreen/run-mongodb-oidc-test.sh"
1966+
export AZUREOIDC_TEST_CMD="OIDC_ENV=azure ./.evergreen/run-mongodb-oidc-test.sh"
19441967
bash $DRIVERS_TOOLS/.evergreen/auth_oidc/azure/run-driver-test.sh
19451968
19461969
- name: "test-fips-standalone"
@@ -2974,7 +2997,8 @@ buildvariants:
29742997
platform: [ rhel8, macos-1100, windows-64-vsMulti-small ]
29752998
display_name: "MONGODB-OIDC Auth ${platform}"
29762999
tasks:
2977-
- name: "oidc-auth-test-latest"
3000+
- name: testoidc_task_group
3001+
batchtime: 20160 # 14 days
29783002

29793003
- name: testazureoidc-variant
29803004
display_name: "Azure OIDC"

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

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,11 @@ if [ $OIDC_ENV == "test" ]; then
1313
echo "Must specify DRIVERS_TOOLS"
1414
exit 1
1515
fi
16-
17-
# Get the drivers secrets. Use an existing secrets file first.
18-
if [ ! -f "${DRIVERS_TOOLS}/.evergreen/auth_oidc/secrets-export.sh" ]; then
19-
. ${DRIVERS_TOOLS}/.evergreen/auth_oidc/setup-secrets.sh
20-
else
21-
source "${DRIVERS_TOOLS}/.evergreen/auth_oidc/secrets-export.sh"
22-
fi
23-
24-
# Make the OIDC tokens.
25-
set -x
26-
pushd ${DRIVERS_TOOLS}/.evergreen/auth_oidc
27-
. ./oidc_get_tokens.sh
28-
popd
29-
30-
# Set up variables and run the test.
31-
if [ -n "${LOCAL_OIDC_SERVER:-}" ]; then
32-
export MONGODB_URI=${MONGODB_URI:-"mongodb://localhost"}
33-
export MONGODB_URI_SINGLE="${MONGODB_URI}/?authMechanism=MONGODB-OIDC"
34-
export MONGODB_URI_MULTI="${MONGODB_URI}:27018/?authMechanism=MONGODB-OIDC&directConnection=true"
35-
else
36-
set +x # turn off xtrace for this portion
37-
export MONGODB_URI="$OIDC_ATLAS_URI_SINGLE"
38-
export MONGODB_URI_SINGLE="$OIDC_ATLAS_URI_SINGLE/?authMechanism=MONGODB-OIDC"
39-
export MONGODB_URI_MULTI="$OIDC_ATLAS_URI_MULTI/?authMechanism=MONGODB-OIDC"
40-
set -x
41-
fi
42-
export OIDC_TOKEN_FILE="$OIDC_TOKEN_DIR/test_user1"
43-
set +x # turn off xtrace for this portion
44-
export OIDC_ADMIN_USER=$OIDC_ATLAS_USER
45-
export OIDC_ADMIN_PWD=$OIDC_ATLAS_PASSWORD
46-
set -x
16+
source ${DRIVERS_TOOLS}/.evergreen/auth_oidc/secrets-export.sh
4717

4818
elif [ $OIDC_ENV == "azure" ]; then
49-
if [ -z "${AZUREOIDC_RESOURCE:-}" ]; then
50-
echo "Must specify an AZUREOIDC_RESOURCE"
51-
exit 1
52-
fi
53-
set +x # turn off xtrace for this portion
54-
export OIDC_ADMIN_USER=$AZUREOIDC_USERNAME
55-
export OIDC_ADMIN_PWD=pwd123
56-
set -x
57-
export MONGODB_URI=${MONGODB_URI:-"mongodb://localhost"}
58-
MONGODB_URI_SINGLE="${MONGODB_URI}/?authMechanism=MONGODB-OIDC"
59-
MONGODB_URI_SINGLE="${MONGODB_URI_SINGLE}&authMechanismProperties=ENVIRONMENT:azure"
60-
export MONGODB_URI_SINGLE="${MONGODB_URI_SINGLE},TOKEN_RESOURCE:${AZUREOIDC_RESOURCE}"
61-
export MONGODB_URI_MULTI=$MONGODB_URI_SINGLE
19+
source ./env.sh
20+
6221
else
6322
echo "Unrecognized OIDC_ENV $OIDC_ENV"
6423
exit 1

.evergreen/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fi
221221

222222
if [ -n "$TEST_AUTH_OIDC" ]; then
223223
python -m pip install ".[aws]"
224-
TEST_ARGS="test/auth_oidc/test_auth_oidc.py"
224+
TEST_ARGS="test/auth_oidc/test_auth_oidc.py $TEST_ARGS"
225225
fi
226226

227227
if [ -n "$PERF_TEST" ]; then

test/auth_oidc/test_auth_oidc.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
ROOT = Path(__file__).parent.parent.resolve()
4747
TEST_PATH = ROOT / "auth" / "unified"
4848
ENVIRON = os.environ.get("OIDC_ENV", "test")
49-
49+
DOMAIN = os.environ.get("OIDC_DOMAIN", "")
50+
TOKEN_DIR = os.environ.get("OIDC_TOKEN_DIR", "")
51+
TOKEN_FILE = os.environ.get("OIDC_TOKEN_FILE", "")
5052

5153
# Generate unified tests.
5254
globals().update(generate_test_classes(str(TEST_PATH), module=__name__))
@@ -56,7 +58,7 @@ class OIDCTestBase(unittest.TestCase):
5658
@classmethod
5759
def setUpClass(cls):
5860
cls.uri_single = os.environ["MONGODB_URI_SINGLE"]
59-
cls.uri_multiple = os.environ["MONGODB_URI_MULTI"]
61+
cls.uri_multiple = os.environ.get("MONGODB_URI_MULTI")
6062
cls.uri_admin = os.environ["MONGODB_URI"]
6163

6264
def setUp(self):
@@ -65,8 +67,10 @@ def setUp(self):
6567
def get_token(self, username=None):
6668
"""Get a token for the current provider."""
6769
if ENVIRON == "test":
68-
token_dir = os.environ["OIDC_TOKEN_DIR"]
69-
token_file = os.path.join(token_dir, username).replace(os.sep, "/")
70+
if username is None:
71+
token_file = TOKEN_FILE
72+
else:
73+
token_file = os.path.join(TOKEN_DIR, username)
7074
with open(token_file) as fid:
7175
return fid.read()
7276
elif ENVIRON == "azure":
@@ -95,6 +99,8 @@ class TestAuthOIDCHuman(OIDCTestBase):
9599
def setUpClass(cls):
96100
if ENVIRON != "test":
97101
raise unittest.SkipTest("Human workflows are only tested with the test environment")
102+
if DOMAIN is None:
103+
raise ValueError("Missing OIDC_DOMAIN")
98104
super().setUpClass()
99105

100106
def create_request_cb(self, username="test_user1", sleep=0):
@@ -121,11 +127,14 @@ def fetch(self, context):
121127

122128
def create_client(self, *args, **kwargs):
123129
username = kwargs.get("username", "test_user1")
130+
if kwargs.get("username"):
131+
kwargs["username"] = f"{username}@{DOMAIN}"
124132
request_cb = kwargs.pop("request_cb", self.create_request_cb(username=username))
125133
props = kwargs.pop("authmechanismproperties", {"OIDC_HUMAN_CALLBACK": request_cb})
126134
kwargs["retryReads"] = False
127135
if not len(args):
128136
args = [self.uri_single]
137+
129138
return MongoClient(*args, authmechanismproperties=props, **kwargs)
130139

131140
def test_1_1_single_principal_implicit_username(self):
@@ -145,6 +154,8 @@ def test_1_2_single_principal_explicit_username(self):
145154
client.close()
146155

147156
def test_1_3_multiple_principal_user_1(self):
157+
if not self.uri_multiple:
158+
raise unittest.SkipTest("Test Requires Server with Multiple Workflow IdPs")
148159
# Create a client with MONGODB_URI_MULTI, a username of test_user1, authMechanism=MONGODB-OIDC, and the OIDC human callback.
149160
client = self.create_client(self.uri_multiple, username="test_user1")
150161
# Perform a find operation that succeeds.
@@ -153,6 +164,8 @@ def test_1_3_multiple_principal_user_1(self):
153164
client.close()
154165

155166
def test_1_4_multiple_principal_user_2(self):
167+
if not self.uri_multiple:
168+
raise unittest.SkipTest("Test Requires Server with Multiple Workflow IdPs")
156169
# Create a human callback that reads in the generated test_user2 token file.
157170
# Create a client with MONGODB_URI_MULTI, a username of test_user2, authMechanism=MONGODB-OIDC, and the OIDC human callback.
158171
client = self.create_client(self.uri_multiple, username="test_user2")
@@ -162,6 +175,8 @@ def test_1_4_multiple_principal_user_2(self):
162175
client.close()
163176

164177
def test_1_5_multiple_principal_no_user(self):
178+
if not self.uri_multiple:
179+
raise unittest.SkipTest("Test Requires Server with Multiple Workflow IdPs")
165180
# Create a client with MONGODB_URI_MULTI, no username, authMechanism=MONGODB-OIDC, and the OIDC human callback.
166181
client = self.create_client(self.uri_multiple)
167182
# Assert that a find operation fails.
@@ -632,15 +647,8 @@ class TestAuthOIDCMachine(OIDCTestBase):
632647

633648
def setUp(self):
634649
self.request_called = 0
635-
if ENVIRON == "test":
636-
self.default_username = "test_user1"
637-
else:
638-
self.default_username = None
639650

640651
def create_request_cb(self, username=None, sleep=0):
641-
if username is None:
642-
username = self.default_username
643-
644652
def request_token(context):
645653
assert isinstance(context.timeout_seconds, int)
646654
assert context.version == 1

0 commit comments

Comments
 (0)