Skip to content
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
17 changes: 17 additions & 0 deletions source/includes/authentication/azure-envs-connection-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pymongo import MongoClient
from azure.identity import DefaultAzureCredential
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult

# define callback, properties, URI, and MongoClient
audience = "<percent-encoded application or service that the OIDC access token is intended for>"
client_id = "<Azure identity client ID>"
class MyCallback(OIDCCallback):
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
credential = DefaultAzureCredential(managed_identity_client_id=client_id)
token = credential.get_token(f"{audience}/.default").token
return OIDCCallbackResult(access_token=token)
properties = {"OIDC_CALLBACK": MyCallback()}
uri = ("mongodb://<hostname>:<port>/?"
"&authMechanism=MONGODB-OIDC"
"&authMechanismProperties=properties")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pass properties in the connection string this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you cannot pass the callback in the connection string

client = MongoClient(uri)
18 changes: 18 additions & 0 deletions source/includes/authentication/azure-envs-mongoclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pymongo import MongoClient
from azure.identity import DefaultAzureCredential
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult

# define callback, properties, and MongoClient
audience = "<percent-encoded application or service that the OIDC access token is intended for>"
client_id = "<Azure identity client ID>"
class MyCallback(OIDCCallback):
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
credential = DefaultAzureCredential(managed_identity_client_id=client_id)
token = credential.get_token(f"{audience}/.default").token
return OIDCCallbackResult(access_token=token)
properties = {"OIDC_CALLBACK": MyCallback()}
client = MongoClient(
"mongodb://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pymongo import MongoClient

# define properties, URI, and MongoClient
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
uri = ("mongodb://<hostname>:<port>/?"
"username=<Azure identity client ID>"
"&authMechanism=MONGODB-OIDC"
"&authMechanismProperties=properties")
client = MongoClient(uri)
10 changes: 10 additions & 0 deletions source/includes/authentication/azure-imds-mongoclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pymongo import MongoClient

# define properties and MongoClient
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
client = MongoClient(
"mongodb://<hostname>:<port>",
Comment on lines +4 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: Space this out for readability since the example is focused on the client (applies to all code examples)

Suggested change
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
client = MongoClient(
"mongodb://<hostname>:<port>",
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
client = MongoClient(
"mongodb://<hostname>:<port>",

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added highlighting

username="<Azure identity client ID>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties,
)
14 changes: 14 additions & 0 deletions source/includes/authentication/gcp-gke-connection-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pymongo import MongoClient
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult

# define callback, properties, URI, and MongoClient
class MyCallback(OIDCCallback):
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as fid:
token = fid.read()
return OIDCCallbackResult(access_token=token)
properties = {"OIDC_CALLBACK": MyCallback()}
uri = ("mongodb://<hostname>:<port>/?"
"&authMechanism=MONGODB-OIDC"
"&authMechanismProperties=properties")
client = MongoClient(uri)
15 changes: 15 additions & 0 deletions source/includes/authentication/gcp-gke-mongoclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pymongo import MongoClient
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult

# define callback, properties, and MongoClient
class MyCallback(OIDCCallback):
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as fid:
token = fid.read()
return OIDCCallbackResult(access_token=token)
properties = {"OIDC_CALLBACK": MyCallback()}
client = MongoClient(
"mongodb://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties,
)
9 changes: 9 additions & 0 deletions source/includes/authentication/gcp-imds-connection-string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pymongo import MongoClient

# define properties, URI, and MongoClient
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
uri = ("mongodb://<hostname>:<port>/?"
"username=<GCP identity client ID>"
"&authMechanism=MONGODB-OIDC"
"&authMechanismProperties=properties")
client = MongoClient(uri)
10 changes: 10 additions & 0 deletions source/includes/authentication/gcp-imds-mongoclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pymongo import MongoClient

# define properties and MongoClient
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
client = MongoClient(
"mongodb://<hostname>:<port>",
username="<GCP identity client ID>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties,
)
97 changes: 94 additions & 3 deletions source/security.txt
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Unix
client = pymongo.MongoClient(uri)

To learn more about authenticating with Kerberos, see
:ref:`pymongo-kerberos` in the Authentication guide.
:ref:`pymongo-kerberos` in the Enterprise Authentication guide.

Windows
~~~~~~~
Expand Down Expand Up @@ -393,7 +393,7 @@ Windows
client = pymongo.MongoClient(uri)

To learn more about authenticating with Kerberos, see
:ref:`pymongo-kerberos` in the Authentication guide.
:ref:`pymongo-kerberos` in the Enterprise Authentication guide.

PLAIN SASL
----------
Expand Down Expand Up @@ -426,4 +426,95 @@ PLAIN SASL
client = pymongo.MongoClient(uri)

To learn more about authenticating with PLAIN SASL, see
:ref:`pymongo-sasl` in the Authentication guide.
:ref:`pymongo-sasl` in the Enterprise Authentication guide.

MONGODB-OIDC
------------

Azure IMDS
~~~~~~~~~~

.. tabs::

.. tab:: MongoClient
:tabid: mongoclient

.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
:language: python
:copyable: true

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/authentication/azure-imds-connection-string.py
:language: python
:copyable: true

To learn more about authenticating with OIDC, see
:ref:`pymongo-mongodb-oidc-azure-imds` in the Authentication guide.

GCP IMDS
~~~~~~~~

.. tabs::

.. tab:: MongoClient
:tabid: mongoclient

.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
:language: python
:copyable: true

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/authentication/gcp-imds-connection-string.py
:language: python
:copyable: true

To learn more about authenticating with OIDC, see
:ref:`pymongo-mongodb-oidc-gcp-imds` in the Authentication guide.

Other Azure Environments
~~~~~~~~~~~~~~~~~~~~~~~~

.. tabs::

.. tab:: MongoClient
:tabid: mongoclient

.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
:language: python
:copyable: true

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/authentication/azure-envs-connection-string.py
:language: python
:copyable: true

To learn more about authenticating with OIDC, see
:ref:`pymongo-mongodb-oidc-azure-envs` in the Authentication guide.

GCP GKE
~~~~~~~

.. tabs::

.. tab:: MongoClient
:tabid: mongoclient

.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
:language: python
:copyable: true

.. tab:: Connection String
:tabid: connectionstring

.. literalinclude:: /includes/authentication/gcp-gke-connection-string.py
:language: python
:copyable: true

To learn more about authenticating with OIDC, see
:ref:`pymongo-mongodb-oidc-gcp-gke` in the Authentication guide.
Loading