Skip to content

DOCSP-42014: Add OIDC Kubernetes support #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pymongo import MongoClient

# start-kubernetes-connection-string
uri = ("mongodb[+srv]://<hostname>:<port>/?"
"authMechanism=MONGODB-OIDC"
"&authMechanismProperties=ENVIRONMENT:k8s")
client = MongoClient(uri)
# end-kubernetes-connection-string
10 changes: 10 additions & 0 deletions source/includes/authentication/kubernetes-mongoclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pymongo import MongoClient

# start-kubernetes-mongoclient
properties = {"ENVIRONMENT": "k8s"}
client = MongoClient(
"mongodb[+srv]://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
# end-kubernetes-mongoclient
34 changes: 33 additions & 1 deletion source/security/authentication/oidc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,36 @@ constructor:
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
:language: python
:copyable: true
:emphasize-lines: 11-15
:emphasize-lines: 11-15

.. _pymongo-mongodb-oidc-kubernetes:

Kubernetes
~~~~~~~~~~

If your application runs on a Kubernetes cluster, you can authenticate to MongoDB by using
{+driver-short+}'s built-in Kubernetes support.

You can configure OIDC for Kubernetes in two ways: by passing arguments to the
``MongoClient`` constructor or through parameters in your connection string. Select from
the following tabs to see how to enable Kubernetes authentication for your application:

.. tabs::

.. tab:: MongoClient
:tabid: mongoclient

.. literalinclude:: /includes/authentication/kubernetes-mongoclient.py
:language: python
:copyable: true
:start-after: start-kubernetes-mongoclient
:end-before: end-kubernetes-mongoclient

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/authentication/kubernetes-connection-string.py
:language: python
:copyable: true
:start-after: start-kubernetes-connection-string
:end-before: end-kubernetes-connection-string
Loading