Skip to content

Commit 57fd616

Browse files
authored
PYTHON-4330 Add Kubernetes Support for OIDC (mongodb#1759)
1 parent a9caaf0 commit 57fd616

File tree

10 files changed

+190
-85
lines changed

10 files changed

+190
-85
lines changed

.evergreen/config.yml

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,18 @@ functions:
520520
args:
521521
- .evergreen/run-mongodb-oidc-test.sh
522522

523+
"run oidc k8s auth test":
524+
- command: subprocess.exec
525+
type: test
526+
params:
527+
binary: bash
528+
working_dir: src
529+
env:
530+
OIDC_ENV: k8s
531+
include_expansions_in_env: ["DRIVERS_TOOLS", "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "K8S_VARIANT"]
532+
args:
533+
- ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-oidc-remote-test.sh
534+
523535
"run aws auth test with aws credentials as environment variables":
524536
- command: shell.exec
525537
type: test
@@ -873,6 +885,32 @@ task_groups:
873885
tasks:
874886
- oidc-auth-test-gcp
875887

888+
- name: testk8soidc_task_group
889+
setup_group:
890+
- func: fetch source
891+
- func: prepare resources
892+
- func: fix absolute paths
893+
- func: make files executable
894+
- command: ec2.assume_role
895+
params:
896+
role_arn: ${aws_test_secrets_role}
897+
duration_seconds: 1800
898+
- command: subprocess.exec
899+
params:
900+
binary: bash
901+
args:
902+
- ${DRIVERS_TOOLS}/.evergreen/auth_oidc/k8s/setup.sh
903+
teardown_task:
904+
- command: subprocess.exec
905+
params:
906+
binary: bash
907+
args:
908+
- ${DRIVERS_TOOLS}/.evergreen/auth_oidc/k8s/teardown.sh
909+
setup_group_can_fail_task: true
910+
setup_group_timeout_secs: 1800
911+
tasks:
912+
- oidc-auth-test-k8s
913+
876914
- name: testoidc_task_group
877915
setup_group:
878916
- func: fetch source
@@ -1548,40 +1586,41 @@ tasks:
15481586

15491587
- name: "oidc-auth-test-azure"
15501588
commands:
1551-
- command: shell.exec
1589+
- command: subprocess.exec
15521590
type: test
15531591
params:
1554-
shell: bash
1555-
script: |-
1556-
set -o errexit
1557-
. src/.evergreen/scripts/env.sh
1558-
cd src
1559-
git add .
1560-
git commit -m "add files"
1561-
export AZUREOIDC_DRIVERS_TAR_FILE=/tmp/mongo-python-driver.tgz
1562-
git archive -o $AZUREOIDC_DRIVERS_TAR_FILE HEAD
1563-
export AZUREOIDC_TEST_CMD="OIDC_ENV=azure ./.evergreen/run-mongodb-oidc-test.sh"
1564-
bash $DRIVERS_TOOLS/.evergreen/auth_oidc/azure/run-driver-test.sh
1592+
binary: bash
1593+
working_dir: src
1594+
env:
1595+
OIDC_ENV: azure
1596+
include_expansions_in_env: ["DRIVERS_TOOLS"]
1597+
args:
1598+
- ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-oidc-remote-test.sh
15651599

15661600
- name: "oidc-auth-test-gcp"
15671601
commands:
1568-
- command: shell.exec
1602+
- command: subprocess.exec
15691603
type: test
15701604
params:
1571-
shell: bash
1572-
script: |-
1573-
set -o errexit
1574-
. src/.evergreen/scripts/env.sh
1575-
cd src
1576-
git add .
1577-
git commit -m "add files"
1578-
export GCPOIDC_DRIVERS_TAR_FILE=/tmp/mongo-python-driver.tgz
1579-
git archive -o $GCPOIDC_DRIVERS_TAR_FILE HEAD
1580-
# Define the command to run on the VM.
1581-
# Ensure that we source the environment file created for us, set up any other variables we need,
1582-
# and then run our test suite on the vm.
1583-
export GCPOIDC_TEST_CMD="OIDC_ENV=gcp ./.evergreen/run-mongodb-oidc-test.sh"
1584-
bash $DRIVERS_TOOLS/.evergreen/auth_oidc/gcp/run-driver-test.sh
1605+
binary: bash
1606+
working_dir: src
1607+
env:
1608+
OIDC_ENV: gcp
1609+
include_expansions_in_env: ["DRIVERS_TOOLS"]
1610+
args:
1611+
- ${PROJECT_DIRECTORY}/.evergreen/run-mongodb-oidc-remote-test.sh
1612+
1613+
- name: "oidc-auth-test-k8s"
1614+
commands:
1615+
- func: "run oidc k8s auth test"
1616+
vars:
1617+
K8S_VARIANT: eks
1618+
- func: "run oidc k8s auth test"
1619+
vars:
1620+
K8S_VARIANT: gke
1621+
- func: "run oidc k8s auth test"
1622+
vars:
1623+
K8S_VARIANT: aks
15851624
# }}}
15861625
- name: "coverage-report"
15871626
tags: ["coverage"]
@@ -1740,20 +1779,6 @@ buildvariants:
17401779
tasks:
17411780
- name: "coverage-report"
17421781

1743-
- name: testazureoidc-variant
1744-
display_name: "OIDC Auth Azure"
1745-
run_on: ubuntu2204-small
1746-
tasks:
1747-
- name: testazureoidc_task_group
1748-
batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README
1749-
1750-
- name: testgcpoidc-variant
1751-
display_name: "OIDC Auth GCP"
1752-
run_on: ubuntu2204-small
1753-
tasks:
1754-
- name: testgcpoidc_task_group
1755-
batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README
1756-
17571782
- name: testgcpkms-variant
17581783
display_name: "GCP KMS"
17591784
run_on:

.evergreen/generated_configs/variants.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,15 @@ buildvariants:
955955
VERSION: "8.0"
956956

957957
# Oidc auth tests
958-
- name: oidc-auth-rhel8
958+
- name: oidc-auth-ubuntu-22
959959
tasks:
960960
- name: testoidc_task_group
961-
display_name: OIDC Auth RHEL8
961+
- name: testazureoidc_task_group
962+
- name: testgcpoidc_task_group
963+
- name: testk8soidc_task_group
964+
display_name: OIDC Auth Ubuntu-22
962965
run_on:
963-
- rhel87-small
966+
- ubuntu2204-small
964967
batchtime: 20160
965968
- name: oidc-auth-macos
966969
tasks:
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
set +x # Disable debug trace
4+
set -eu
5+
6+
echo "Running MONGODB-OIDC remote tests"
7+
8+
OIDC_ENV=${OIDC_ENV:-"test"}
9+
10+
# Make sure DRIVERS_TOOLS is set.
11+
if [ -z "$DRIVERS_TOOLS" ]; then
12+
echo "Must specify DRIVERS_TOOLS"
13+
exit 1
14+
fi
15+
16+
# Set up the remote files to test.
17+
git add .
18+
git commit -m "add files" || true
19+
export TEST_TAR_FILE=/tmp/mongo-python-driver.tgz
20+
git archive -o $TEST_TAR_FILE HEAD
21+
22+
pushd $DRIVERS_TOOLS
23+
24+
if [ $OIDC_ENV == "test" ]; then
25+
echo "Test OIDC environment does not support remote test!"
26+
exit 1
27+
28+
elif [ $OIDC_ENV == "azure" ]; then
29+
export AZUREOIDC_DRIVERS_TAR_FILE=$TEST_TAR_FILE
30+
export AZUREOIDC_TEST_CMD="OIDC_ENV=azure ./.evergreen/run-mongodb-oidc-test.sh"
31+
bash ./.evergreen/auth_oidc/azure/run-driver-test.sh
32+
33+
elif [ $OIDC_ENV == "gcp" ]; then
34+
export GCPOIDC_DRIVERS_TAR_FILE=$TEST_TAR_FILE
35+
export GCPOIDC_TEST_CMD="OIDC_ENV=gcp ./.evergreen/run-mongodb-oidc-test.sh"
36+
bash ./.evergreen/auth_oidc/gcp/run-driver-test.sh
37+
38+
elif [ $OIDC_ENV == "k8s" ]; then
39+
# Make sure K8S_VARIANT is set.
40+
if [ -z "$K8S_VARIANT" ]; then
41+
echo "Must specify K8S_VARIANT"
42+
popd
43+
exit 1
44+
fi
45+
46+
bash ./.evergreen/auth_oidc/k8s/setup-pod.sh
47+
bash ./.evergreen/auth_oidc/k8s/run-self-test.sh
48+
export K8S_DRIVERS_TAR_FILE=$TEST_TAR_FILE
49+
export K8S_TEST_CMD="OIDC_ENV=k8s ./.evergreen/run-mongodb-oidc-test.sh"
50+
source ./.evergreen/auth_oidc/k8s/secrets-export.sh # for MONGODB_URI
51+
bash ./.evergreen/auth_oidc/k8s/run-driver-test.sh
52+
bash ./.evergreen/auth_oidc/k8s/teardown-pod.sh
53+
54+
else
55+
echo "Unrecognized OIDC_ENV $OIDC_ENV"
56+
pod
57+
exit 1
58+
fi
59+
60+
popd

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ elif [ $OIDC_ENV == "azure" ]; then
2121
elif [ $OIDC_ENV == "gcp" ]; then
2222
source ./secrets-export.sh
2323

24+
elif [ $OIDC_ENV == "k8s" ]; then
25+
echo "Running oidc on k8s"
26+
2427
else
2528
echo "Unrecognized OIDC_ENV $OIDC_ENV"
2629
exit 1

.evergreen/scripts/generate_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,14 @@ def create_serverless_variants():
615615

616616
def create_oidc_auth_variants():
617617
variants = []
618-
for host in ["rhel8", "macos", "win64"]:
618+
other_tasks = ["testazureoidc_task_group", "testgcpoidc_task_group", "testk8soidc_task_group"]
619+
for host in ["ubuntu22", "macos", "win64"]:
620+
tasks = ["testoidc_task_group"]
621+
if host == "ubuntu22":
622+
tasks += other_tasks
619623
variants.append(
620624
create_variant(
621-
["testoidc_task_group"],
625+
tasks,
622626
get_display_name("OIDC Auth", host),
623627
host=host,
624628
batchtime=BATCHTIME_WEEK * 2,

pymongo/auth_oidc_shared.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,17 @@ def __init__(self, token_resource: str) -> None:
116116
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
117117
resp = _get_gcp_response(self.token_resource, context.timeout_seconds)
118118
return OIDCCallbackResult(access_token=resp["access_token"])
119+
120+
121+
class _OIDCK8SCallback(OIDCCallback):
122+
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
123+
return OIDCCallbackResult(access_token=_get_k8s_token())
124+
125+
126+
def _get_k8s_token() -> str:
127+
fname = "/var/run/secrets/kubernetes.io/serviceaccount/token"
128+
for key in ["AZURE_FEDERATED_TOKEN_FILE", "AWS_WEB_IDENTITY_TOKEN_FILE"]:
129+
if key in os.environ:
130+
fname = os.environ[key]
131+
with open(fname) as fid:
132+
return fid.read()

pymongo/auth_shared.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pymongo.auth_oidc_shared import (
2727
_OIDCAzureCallback,
2828
_OIDCGCPCallback,
29+
_OIDCK8SCallback,
2930
_OIDCProperties,
3031
_OIDCTestCallback,
3132
)
@@ -192,6 +193,9 @@ def _build_credentials_tuple(
192193
"GCP provider for MONGODB-OIDC requires a TOKEN_RESOURCE auth mechanism property"
193194
)
194195
callback = _OIDCGCPCallback(token_resource)
196+
elif environ == "k8s":
197+
passwd = None
198+
callback = _OIDCK8SCallback()
195199
else:
196200
raise ConfigurationError(f"unrecognized ENVIRONMENT for MONGODB-OIDC: {environ}")
197201
else:

test/auth/legacy/connection-string.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,26 @@
626626
"uri": "mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp",
627627
"valid": false,
628628
"credential": null
629+
},
630+
{
631+
"description": "should recognise the mechanism with k8s provider (MONGODB-OIDC)",
632+
"uri": "mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s",
633+
"valid": true,
634+
"credential": {
635+
"username": null,
636+
"password": null,
637+
"source": "$external",
638+
"mechanism": "MONGODB-OIDC",
639+
"mechanism_properties": {
640+
"ENVIRONMENT": "k8s"
641+
}
642+
}
643+
},
644+
{
645+
"description": "should throw an error for a username and password with k8s provider (MONGODB-OIDC)",
646+
"uri": "mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s",
647+
"valid": false,
648+
"credential": null
629649
}
630650
]
631651
}

0 commit comments

Comments
 (0)