Skip to content

DOCSP-48166: Async examples for Security pages #237

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 4 commits into from
Mar 26, 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
18 changes: 18 additions & 0 deletions source/includes/authentication/azure-envs-mongoclient-async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pymongo import AsyncMongoClient
from azure.identity import DefaultAzureCredential
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult

# define callback, properties, and MongoClient
audience = "<audience>"
client_id = "<Azure 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 = AsyncMongoClient(
"mongodb[+srv]://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pymongo import AsyncMongoClient

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

# define properties and MongoClient
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
client = AsyncMongoClient(
"mongodb[+srv]://<hostname>:<port>",
username="<Azure ID>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
15 changes: 15 additions & 0 deletions source/includes/authentication/gcp-gke-mongoclient-async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pymongo import AsyncMongoClient
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 = AsyncMongoClient(
"mongodb[+srv]://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pymongo import AsyncMongoClient

# define URI and MongoClient
uri = ("mongodb[+srv]://<hostname>:<port>/?"
"&authMechanism=MONGODB-OIDC"
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
client = AsyncMongoClient(uri)
9 changes: 9 additions & 0 deletions source/includes/authentication/gcp-imds-mongoclient-async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pymongo import AsyncMongoClient

# define properties and MongoClient
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
client = AsyncMongoClient(
"mongodb[+srv]://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
11 changes: 9 additions & 2 deletions source/includes/authentication/kubernetes-connection-string.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from pymongo import MongoClient
from pymongo import MongoClient, AsyncMongoClient

# start-kubernetes-connection-string
uri = ("mongodb[+srv]://<hostname>:<port>/?"
"authMechanism=MONGODB-OIDC"
"&authMechanismProperties=ENVIRONMENT:k8s")
client = MongoClient(uri)
# end-kubernetes-connection-string
# end-kubernetes-connection-string

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

# start-kubernetes-mongoclient
properties = {"ENVIRONMENT": "k8s"}
Expand All @@ -7,4 +7,13 @@
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
# end-kubernetes-mongoclient
# end-kubernetes-mongoclient

# start-kubernetes-mongoclient-async
properties = {"ENVIRONMENT": "k8s"}
client = AsyncMongoClient(
"mongodb[+srv]://<hostname>:<port>",
authMechanism="MONGODB-OIDC",
authMechanismProperties=properties
)
# end-kubernetes-mongoclient-async
19 changes: 18 additions & 1 deletion source/includes/connect/ca-file-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@
.. code-block:: python

uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem"
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCAFile="/path/to/ca.pem")

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = "mongodb://<db_username>:<db_password@<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem"
client = pymongo.AsyncMongoClient(uri)
21 changes: 20 additions & 1 deletion source/includes/connect/client-cert-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@
uri = ("mongodb://<db_username>:<db_password>@<hostname:<port>/?"
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem")
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCertificateKeyFile='/path/to/client.pem')

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem")
client = pymongo.AsyncMongoClient(uri)
19 changes: 18 additions & 1 deletion source/includes/connect/crl-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@
.. code-block:: python

uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem"
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCRLFile="/path/to/crl.pem")

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem"
client = pymongo.AsyncMongoClient(uri)
21 changes: 20 additions & 1 deletion source/includes/connect/disable-cert-validation-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidCertificates=true")
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsAllowInvalidCertificates=True)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidCertificates=true")
client = pymongo.AsyncMongoClient(uri)
21 changes: 20 additions & 1 deletion source/includes/connect/disable-host-verification-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidHostnames=true")
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsAllowInvalidHostnames=True)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsAllowInvalidHostnames=true")
client = pymongo.AsyncMongoClient(uri)
21 changes: 20 additions & 1 deletion source/includes/connect/insecure-tls-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,23 @@
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsInsecure=true")
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsInsecure=True)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
"tls=true"
"&tlsInsecure=true")
client = pymongo.AsyncMongoClient(uri)
24 changes: 23 additions & 1 deletion source/includes/connect/key-file-password.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,26 @@
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem"
"&tlsCertificateKeyFilePassword=<passphrase>")
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsCertificateKeyFile='/path/to/client.pem',
tlsCertificateKeyFilePassword=<passphrase>)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = ("mongodb://<db_username>:<db_password"
"@<hostname>:<port>/?"
"tls=true"
"&tlsCertificateKeyFile=path/to/client.pem"
"&tlsCertificateKeyFilePassword=<passphrase>")
client = pymongo.AsyncMongoClient(uri)
19 changes: 18 additions & 1 deletion source/includes/connect/ocsp-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@
.. code-block:: python

uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true"
client = pymongo.MongoClient(uri)
client = pymongo.MongoClient(uri)

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
tls=True,
tlsDisableOCSPEndpointCheck=True)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true"
client = pymongo.AsyncMongoClient(uri)
16 changes: 15 additions & 1 deletion source/includes/connect/tls-tabs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@

.. code-block:: python

client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")

.. tab:: MongoClient (Asynchronous)
:tabid: mongoclient-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>", tls=True)

.. tab:: Connection String (Asynchronous)
:tabid: connectionstring-async

.. code-block:: python

client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")
Loading
Loading