Skip to content

Commit bc988bb

Browse files
authored
DRIVERS-2882 Add Kubernetes Support for OIDC (#1622)
1 parent 067ce16 commit bc988bb

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ repos:
7676
rev: "v2.3.0"
7777
hooks:
7878
- id: codespell
79-
args: ["-L", "fle,re-use,merchantibility,synching,crate,nin,infinit,te,checkin"]
79+
args: ["-L", "fle,re-use,merchantibility,synching,crate,nin,infinit,te,checkin,aks"]
8080
exclude: |
8181
(?x)^(.*\.rst
8282
)$

source/auth/auth.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,8 +1266,8 @@ in the MONGODB-OIDC specification, including sections or blocks that specificall
12661266
- ENVIRONMENT
12671267
12681268
Drivers MUST allow the user to specify the name of a built-in OIDC application environment integration to use to
1269-
obtain credentials. If provided, the value MUST be one of `["test", "azure", "gcp"]`. If both `ENVIRONMENT` and an
1270-
[OIDC Callback](#oidc-callback) or [OIDC Human Callback](#oidc-human-callback) are provided for the same
1269+
obtain credentials. If provided, the value MUST be one of `["test", "azure", "gcp", "k8s"]`. If both `ENVIRONMENT`
1270+
and an [OIDC Callback](#oidc-callback) or [OIDC Human Callback](#oidc-human-callback) are provided for the same
12711271
`MongoClient`, the driver MUST raise an error.
12721272
12731273
- TOKEN_RESOURCE
@@ -1428,6 +1428,35 @@ callback had been provided by the user.
14281428
For details on test environment setup, see the README in
14291429
[Drivers-Evergreen-Tools](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/auth_oidc/gcp/README.md).
14301430
1431+
***Kubernetes***
1432+
1433+
The Kubernetes integration is enabled by setting auth mechanism property `ENVIRONMENT:k8s`. In this configuration, the
1434+
driver is expected to be running inside a Kubernetes environment with a configured
1435+
[ServiceAccount](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#bound-service-account-token-volume).
1436+
1437+
If enabled, drivers MUST read the contents of the token from the local file path found using the following algorithm:
1438+
1439+
```python
1440+
if 'AZURE_FEDERATED_TOKEN_FILE' in os.environ:
1441+
fname = os.environ['AZURE_FEDERATED_TOKEN_FILE']
1442+
elif 'AWS_WEB_IDENTITY_TOKEN_FILE' in os.environ:
1443+
fname = os.environ['AWS_WEB_IDENTITY_TOKEN_FILE']
1444+
else:
1445+
fname = '/var/run/secrets/kubernetes.io/serviceaccount/token'
1446+
```
1447+
1448+
Where `AZURE_FEDERATED_TOKEN_FILE` contains the file path on Azure Kubernetes Service (AKS),
1449+
`AWS_WEB_IDENTITY_TOKEN_FILE` contains the file path on Elastic Kubernetes Service (EKS), and
1450+
`/var/run/secrets/kubernetes.io/serviceaccount/token` is the default path for a Kubernetes
1451+
[ServiceAccount token](https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#serviceaccount-admission-controller),
1452+
which is used by Google Kubernetes Engine (GKE).
1453+
1454+
The callback itself MUST not perform any caching, and the driver MUST cache its tokens in the same way as if a custom
1455+
callback had been provided by the user.
1456+
1457+
For details on test environment setup, see the README in
1458+
[Drivers-Evergreen-Tools](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/auth_oidc/k8s/README.md).
1459+
14311460
#### OIDC Callback
14321461
14331462
Drivers MUST allow users to provide a callback that returns an OIDC access token. The purpose of the callback is to
@@ -2105,6 +2134,8 @@ practice to avoid this. (See
21052134

21062135
## Changelog
21072136

2137+
- 2024-10-02: Add Kubernetes built-in OIDC provider integration.
2138+
21082139
- 2024-08-19: Clarify Reauthentication and Speculative Authentication combination behavior.
21092140

21102141
- 2024-05-29: Disallow comma character when `TOKEN_RESOURCE` is given in a connection string.

source/auth/tests/legacy/connection-string.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/auth/tests/legacy/connection-string.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,18 @@ tests:
454454
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp
455455
valid: false
456456
credential: null
457+
- description: should recognise the mechanism with k8s provider (MONGODB-OIDC)
458+
uri: mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s
459+
valid: true
460+
credential:
461+
username: null
462+
password: null
463+
source: $external
464+
mechanism: MONGODB-OIDC
465+
mechanism_properties:
466+
ENVIRONMENT: k8s
467+
- description: should throw an error for a username and password with k8s provider
468+
(MONGODB-OIDC)
469+
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s
470+
valid: false
471+
credential: null

source/auth/tests/mongodb-oidc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ______________________________________________________________________
2121
Drivers MUST run the machine prose tests when `OIDC_TOKEN_DIR` is set. Drivers can either set the `ENVIRONMENT:test`
2222
auth mechanism property, or use a custom callback that also reads the file.
2323

24-
Drivers can also choose to run the machine prose tests on GCP or Azure VMs.
24+
Drivers can also choose to run the machine prose tests on GCP or Azure VMs, or on the Kubernetes clusters.
2525

2626
Drivers MUST implement all prose tests in this section. Unless otherwise noted, all `MongoClient` instances MUST be
2727
configured with `retryReads=false`.

0 commit comments

Comments
 (0)