diff --git a/source/includes/authentication/azure-envs-mongoclient-async.py b/source/includes/authentication/azure-envs-mongoclient-async.py new file mode 100644 index 00000000..550d98d0 --- /dev/null +++ b/source/includes/authentication/azure-envs-mongoclient-async.py @@ -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 = "" +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 = AsyncMongoClient( + "mongodb[+srv]://:", + authMechanism="MONGODB-OIDC", + authMechanismProperties=properties +) \ No newline at end of file diff --git a/source/includes/authentication/azure-imds-connection-string-async.py b/source/includes/authentication/azure-imds-connection-string-async.py new file mode 100644 index 00000000..b2da2527 --- /dev/null +++ b/source/includes/authentication/azure-imds-connection-string-async.py @@ -0,0 +1,8 @@ +from pymongo import AsyncMongoClient + +# define URI and MongoClient +uri = ("mongodb[+srv]://:/?" + "username=" + "&authMechanism=MONGODB-OIDC" + "&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:") +client = AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/authentication/azure-imds-mongoclient-async.py b/source/includes/authentication/azure-imds-mongoclient-async.py new file mode 100644 index 00000000..fce7b32d --- /dev/null +++ b/source/includes/authentication/azure-imds-mongoclient-async.py @@ -0,0 +1,10 @@ +from pymongo import AsyncMongoClient + +# define properties and MongoClient +properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": ""} +client = AsyncMongoClient( + "mongodb[+srv]://:", + username="", + authMechanism="MONGODB-OIDC", + authMechanismProperties=properties +) \ No newline at end of file diff --git a/source/includes/authentication/gcp-gke-mongoclient-async.py b/source/includes/authentication/gcp-gke-mongoclient-async.py new file mode 100644 index 00000000..3182f7c7 --- /dev/null +++ b/source/includes/authentication/gcp-gke-mongoclient-async.py @@ -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]://:", + authMechanism="MONGODB-OIDC", + authMechanismProperties=properties +) \ No newline at end of file diff --git a/source/includes/authentication/gcp-imds-connection-string-async.py b/source/includes/authentication/gcp-imds-connection-string-async.py new file mode 100644 index 00000000..74f25016 --- /dev/null +++ b/source/includes/authentication/gcp-imds-connection-string-async.py @@ -0,0 +1,7 @@ +from pymongo import AsyncMongoClient + +# define URI and MongoClient +uri = ("mongodb[+srv]://:/?" + "&authMechanism=MONGODB-OIDC" + "&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:") +client = AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/authentication/gcp-imds-mongoclient-async.py b/source/includes/authentication/gcp-imds-mongoclient-async.py new file mode 100644 index 00000000..221df4cc --- /dev/null +++ b/source/includes/authentication/gcp-imds-mongoclient-async.py @@ -0,0 +1,9 @@ +from pymongo import AsyncMongoClient + +# define properties and MongoClient +properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": ""} +client = AsyncMongoClient( + "mongodb[+srv]://:", + authMechanism="MONGODB-OIDC", + authMechanismProperties=properties +) \ No newline at end of file diff --git a/source/includes/authentication/kubernetes-connection-string.py b/source/includes/authentication/kubernetes-connection-string.py index 14046f95..d0454e72 100644 --- a/source/includes/authentication/kubernetes-connection-string.py +++ b/source/includes/authentication/kubernetes-connection-string.py @@ -1,8 +1,15 @@ -from pymongo import MongoClient +from pymongo import MongoClient, AsyncMongoClient # start-kubernetes-connection-string uri = ("mongodb[+srv]://:/?" "authMechanism=MONGODB-OIDC" "&authMechanismProperties=ENVIRONMENT:k8s") client = MongoClient(uri) -# end-kubernetes-connection-string \ No newline at end of file +# end-kubernetes-connection-string + +# start-kubernetes-connection-string-async +uri = ("mongodb[+srv]://:/?" + "authMechanism=MONGODB-OIDC" + "&authMechanismProperties=ENVIRONMENT:k8s") +client = AsyncMongoClient(uri) +# end-kubernetes-connection-string-async \ No newline at end of file diff --git a/source/includes/authentication/kubernetes-mongoclient.py b/source/includes/authentication/kubernetes-mongoclient.py index 099d0aef..1b278077 100644 --- a/source/includes/authentication/kubernetes-mongoclient.py +++ b/source/includes/authentication/kubernetes-mongoclient.py @@ -1,4 +1,4 @@ -from pymongo import MongoClient +from pymongo import MongoClient, AsyncMongoClient # start-kubernetes-mongoclient properties = {"ENVIRONMENT": "k8s"} @@ -7,4 +7,13 @@ authMechanism="MONGODB-OIDC", authMechanismProperties=properties ) -# end-kubernetes-mongoclient \ No newline at end of file +# end-kubernetes-mongoclient + +# start-kubernetes-mongoclient-async +properties = {"ENVIRONMENT": "k8s"} +client = AsyncMongoClient( + "mongodb[+srv]://:", + authMechanism="MONGODB-OIDC", + authMechanismProperties=properties +) +# end-kubernetes-mongoclient-async \ No newline at end of file diff --git a/source/includes/connect/ca-file-tabs.rst b/source/includes/connect/ca-file-tabs.rst index 555d9b6e..b56a13fa 100644 --- a/source/includes/connect/ca-file-tabs.rst +++ b/source/includes/connect/ca-file-tabs.rst @@ -15,4 +15,21 @@ .. code-block:: python uri = "mongodb://:@:/?tls=true&tlsCAFile=/path/to/ca.pem" - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsCAFile="/path/to/ca.pem") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://::/?tls=true&tlsCAFile=/path/to/ca.pem" + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/client-cert-tabs.rst b/source/includes/connect/client-cert-tabs.rst index 60d2b922..94d4a73c 100644 --- a/source/includes/connect/client-cert-tabs.rst +++ b/source/includes/connect/client-cert-tabs.rst @@ -17,4 +17,23 @@ uri = ("mongodb://:@/?" "tls=true" "&tlsCertificateKeyFile=path/to/client.pem") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsCertificateKeyFile='/path/to/client.pem') + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "tls=true" + "&tlsCertificateKeyFile=path/to/client.pem") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/crl-tabs.rst b/source/includes/connect/crl-tabs.rst index 3aacff00..b3e7f868 100644 --- a/source/includes/connect/crl-tabs.rst +++ b/source/includes/connect/crl-tabs.rst @@ -15,4 +15,21 @@ .. code-block:: python uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem" - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + 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) \ No newline at end of file diff --git a/source/includes/connect/disable-cert-validation-tabs.rst b/source/includes/connect/disable-cert-validation-tabs.rst index bae3c9a5..2dadfe7a 100644 --- a/source/includes/connect/disable-cert-validation-tabs.rst +++ b/source/includes/connect/disable-cert-validation-tabs.rst @@ -17,4 +17,23 @@ uri = ("mongodb://:@:/?" "tls=true" "&tlsAllowInvalidCertificates=true") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsAllowInvalidCertificates=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "tls=true" + "&tlsAllowInvalidCertificates=true") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/disable-host-verification-tabs.rst b/source/includes/connect/disable-host-verification-tabs.rst index d600a306..d11d5831 100644 --- a/source/includes/connect/disable-host-verification-tabs.rst +++ b/source/includes/connect/disable-host-verification-tabs.rst @@ -17,4 +17,23 @@ uri = ("mongodb://:@:/?" "tls=true" "&tlsAllowInvalidHostnames=true") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsAllowInvalidHostnames=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "tls=true" + "&tlsAllowInvalidHostnames=true") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/insecure-tls-tabs.rst b/source/includes/connect/insecure-tls-tabs.rst index 503d0344..32bea4aa 100644 --- a/source/includes/connect/insecure-tls-tabs.rst +++ b/source/includes/connect/insecure-tls-tabs.rst @@ -17,4 +17,23 @@ uri = ("mongodb://:@:/?" "tls=true" "&tlsInsecure=true") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsInsecure=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "tls=true" + "&tlsInsecure=true") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/key-file-password.rst b/source/includes/connect/key-file-password.rst index 8c75a415..87f75ba4 100644 --- a/source/includes/connect/key-file-password.rst +++ b/source/includes/connect/key-file-password.rst @@ -19,4 +19,26 @@ "tls=true" "&tlsCertificateKeyFile=path/to/client.pem" "&tlsCertificateKeyFilePassword=") - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + tls=True, + tlsCertificateKeyFile='/path/to/client.pem', + tlsCertificateKeyFilePassword=) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://::/?" + "tls=true" + "&tlsCertificateKeyFile=path/to/client.pem" + "&tlsCertificateKeyFilePassword=") + client = pymongo.AsyncMongoClient(uri) \ No newline at end of file diff --git a/source/includes/connect/ocsp-tabs.rst b/source/includes/connect/ocsp-tabs.rst index 52695a56..786e498d 100644 --- a/source/includes/connect/ocsp-tabs.rst +++ b/source/includes/connect/ocsp-tabs.rst @@ -15,4 +15,21 @@ .. code-block:: python uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true" - client = pymongo.MongoClient(uri) \ No newline at end of file + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:", + 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) \ No newline at end of file diff --git a/source/includes/connect/tls-tabs.rst b/source/includes/connect/tls-tabs.rst index df592c65..d23ea5b9 100644 --- a/source/includes/connect/tls-tabs.rst +++ b/source/includes/connect/tls-tabs.rst @@ -12,4 +12,18 @@ .. code-block:: python - client = pymongo.MongoClient("mongodb://:@:?tls=true") \ No newline at end of file + client = pymongo.MongoClient("mongodb://:@:?tls=true") + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@", tls=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:@:?tls=true") \ No newline at end of file diff --git a/source/security.txt b/source/security.txt index cd5ff3ef..e257c5bb 100644 --- a/source/security.txt +++ b/source/security.txt @@ -46,11 +46,28 @@ the relevant values for your MongoDB deployment. .. include:: /includes/usage-examples/sample-app-intro.rst -.. literalinclude:: /includes/usage-examples/connect-sample-app.py - :language: python - :copyable: true - :linenos: - :emphasize-lines: 4-6 +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the +corresponding code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/usage-examples/connect-sample-app.py + :language: python + :copyable: true + :linenos: + :emphasize-lines: 4-6 + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/usage-examples/connect-sample-app-async.py + :language: python + :copyable: true + :linenos: + :emphasize-lines: 6-8 Transport Layer Security (TLS) ------------------------------ @@ -153,6 +170,28 @@ SCRAM-SHA-256 "authSource=" "&authMechanism=SCRAM-SHA-256") client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="", + password="", + authSource="", + authMechanism="SCRAM-SHA-256") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:" + "@:/?" + "authSource=" + "&authMechanism=SCRAM-SHA-256") + client = pymongo.AsyncMongoClient(uri) To learn more about SCRAM-SHA-256 authentication, see :ref:`pymongo-scram-sha-256` in the Authentication guide. @@ -183,6 +222,28 @@ SCRAM-SHA-1 "authSource=" "&authMechanism=SCRAM-SHA-1") client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="", + password="", + authSource="", + authMechanism="SCRAM-SHA-1") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:" + "@:/?" + "authSource=" + "&authMechanism=SCRAM-SHA-1") + client = pymongo.AsyncMongoClient(uri) To learn more about SCRAM-SHA-1 authentication, see :ref:`pymongo-scram-sha-1` in the Authentication guide. @@ -213,6 +274,27 @@ MONGODB-X509 "&authMechanism=MONGODB-X509") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + tls=True, + tlsCertificateKeyFile="/path/to/client.pem", + authMechanism="MONGODB-X509") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:/?" + "tls=true" + "&tlsCertificateKeyFile=path/to/client.pem" + "&authMechanism=MONGODB-X509") + client = pymongo.AsyncMongoClient(uri) + To learn more about MONGODB-X509 authentication, see :ref:`pymongo-mongodb-x509` in the Authentication guide. @@ -245,6 +327,27 @@ MONGODB-AWS "&authMechanism=MONGODB-AWS") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="", + password="", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:" + "" + "@:/?" + "&authMechanism=MONGODB-AWS") + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with AWS ``MongoClient`` credentials, see :ref:`pymongo-mongodb-aws-credentials` in the Authentication guide. @@ -269,6 +372,22 @@ Environment Variables uri = "mongodb://:/?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://:/?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with AWS environment variables, see :ref:`pymongo-mongodb-aws-env-vars` in the Authentication guide. @@ -293,6 +412,22 @@ Shared Credentials File uri = "mongodb://:/?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://:/?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with a shared AWS credentials file, see :ref:`pymongo-mongodb-aws-creds-file` in the Authentication guide. @@ -317,6 +452,21 @@ AWS Config File uri = "mongodb://:/?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://:/?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) To learn more about authenticating with an AWS config file, see :ref:`pymongo-mongodb-aws-config-file` in the Authentication guide. @@ -349,6 +499,29 @@ AssumeRole Request "&authMechanism=MONGODB-AWS") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://@:", + username="", + password="", + authMechanismProperties="AWS_SESSION_TOKEN:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:" + "" + "@:/?" + "authMechanismProperties=AWS_SESSION_TOKEN:" + "&authMechanism=MONGODB-AWS") + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with an ``AssumeRole`` request, see :ref:`pymongo-mongodb-aws-assume-role` in the Authentication guide. @@ -373,6 +546,22 @@ AssumeRoleWithWebIdentity uri = "mongodb://:/?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://:/?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with an ``AssumeRoleWithWebIdentity`` request, see :ref:`pymongo-mongodb-aws-oidc` in the Authentication guide. @@ -397,6 +586,22 @@ ECS Container or EC2 Instance uri = "mongodb://:/?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb://:/?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating from an ECS container, see :ref:`pymongo-mongodb-aws-ec` in the Authentication guide. @@ -432,6 +637,26 @@ Unix "&authMechanismProperties=SERVICE_NAME:") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="mongodbuser@EXAMPLE.COM", + authMechanism="GSSAPI", + authMechanismProperties="SERVICE_NAME:") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://mongodbuser%40EXAMPLE.COM@:/?" + "&authMechanism=GSSAPI" + "&authMechanismProperties=SERVICE_NAME:") + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with Kerberos, see :ref:`pymongo-kerberos` in the Enterprise Authentication guide. @@ -440,32 +665,59 @@ Windows .. tabs:: - .. tab:: MongoClient - :tabid: mongoclient + .. tab:: MongoClient + :tabid: mongoclient - .. code-block:: python + .. code-block:: python - client = pymongo.MongoClient("mongodb://:", - username="", - authMechanism="GSSAPI", - password="", - authMechanismProperties="SERVICE_NAME:, - CANONICALIZE_HOST_NAME:true, - SERVICE_REALM:") + client = pymongo.MongoClient("mongodb://:", + username="", + authMechanism="GSSAPI", + password="", + authMechanismProperties="SERVICE_NAME:, + CANONICALIZE_HOST_NAME:true, + SERVICE_REALM:") - .. tab:: Connection String - :tabid: connectionstring + .. tab:: Connection String + :tabid: connectionstring + + .. code-block:: python + + uri = ("mongodb://:" + "@:/?" + "&authMechanism=GSSAPI" + "&authMechanismProperties=" + "SERVICE_NAME:," + "CANONICALIZE_HOST_NAME:true," + "SERVICE_REALM:") + client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async - .. code-block:: python + .. code-block:: python - uri = ("mongodb://:" - "@:/?" - "&authMechanism=GSSAPI" - "&authMechanismProperties=" - "SERVICE_NAME:," - "CANONICALIZE_HOST_NAME:true," - "SERVICE_REALM:") - client = pymongo.MongoClient(uri) + client = pymongo.AsyncMongoClient("mongodb://:", + username="", + authMechanism="GSSAPI", + password="", + authMechanismProperties="SERVICE_NAME:, + CANONICALIZE_HOST_NAME:true, + SERVICE_REALM:") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:" + "@:/?" + "&authMechanism=GSSAPI" + "&authMechanismProperties=" + "SERVICE_NAME:," + "CANONICALIZE_HOST_NAME:true," + "SERVICE_REALM:") + client = pymongo.AsyncMongoClient(uri) To learn more about authenticating with Kerberos, see :ref:`pymongo-kerberos` in the Enterprise Authentication guide. @@ -500,6 +752,27 @@ PLAIN SASL "&tls=true") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="", + password="", + authMechanism="PLAIN", + tls=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://:@:/?" + "&authMechanism=PLAIN" + "&tls=true") + client = pymongo.AsyncMongoClient(uri) + To learn more about authenticating with PLAIN SASL, see :ref:`pymongo-sasl` in the Enterprise Authentication guide. @@ -519,15 +792,29 @@ Azure IMDS :tabid: mongoclient .. literalinclude:: /includes/authentication/azure-imds-mongoclient.py - :language: python - :copyable: true + :language: python + :copyable: true .. tab:: Connection String :tabid: connectionstring .. literalinclude:: /includes/authentication/azure-imds-connection-string.py - :language: python - :copyable: true + :language: python + :copyable: true + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. literalinclude:: /includes/authentication/azure-imds-mongoclient-async.py + :language: python + :copyable: true + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. literalinclude:: /includes/authentication/azure-imds-connection-string-async.py + :language: python + :copyable: true To learn more about authenticating with OIDC, see :ref:`pymongo-mongodb-oidc-azure-imds` in the Authentication guide. @@ -541,15 +828,29 @@ GCP IMDS :tabid: mongoclient .. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py - :language: python - :copyable: true + :language: python + :copyable: true .. tab:: Connection String :tabid: connectionstring .. literalinclude:: /includes/authentication/gcp-imds-connection-string.py - :language: python - :copyable: true + :language: python + :copyable: true + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. literalinclude:: /includes/authentication/gcp-imds-mongoclient-async.py + :language: python + :copyable: true + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. literalinclude:: /includes/authentication/gcp-imds-connection-string-async.py + :language: python + :copyable: true To learn more about authenticating with OIDC, see :ref:`pymongo-mongodb-oidc-gcp-imds` in the Authentication guide. @@ -557,9 +858,21 @@ To learn more about authenticating with OIDC, see Other Azure Environments ~~~~~~~~~~~~~~~~~~~~~~~~ -.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py - :language: python - :copyable: true +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/authentication/azure-envs-mongoclient.py + :language: python + :copyable: true + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/authentication/azure-envs-mongoclient-async.py + :language: python + :copyable: true To learn more about authenticating with OIDC, see :ref:`pymongo-mongodb-oidc-azure-envs` in the Authentication guide. @@ -567,9 +880,21 @@ To learn more about authenticating with OIDC, see GCP GKE ~~~~~~~ -.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py - :language: python - :copyable: true +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py + :language: python + :copyable: true + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/authentication/gcp-gke-mongoclient-async.py + :language: python + :copyable: true To learn more about authenticating with OIDC, see :ref:`pymongo-mongodb-oidc-gcp-gke` in the Authentication guide. \ No newline at end of file diff --git a/source/security/authentication/aws-iam.txt b/source/security/authentication/aws-iam.txt index e6dc0f07..fb1e6d2a 100644 --- a/source/security/authentication/aws-iam.txt +++ b/source/security/authentication/aws-iam.txt @@ -111,6 +111,27 @@ You can set these options in two ways: by passing arguments to the "&authMechanism=MONGODB-AWS") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + username="", + password="", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb+srv://:" + "" + "@/?" + "&authMechanism=MONGODB-AWS") + client = pymongo.AsyncMongoClient(uri) + .. _pymongo-mongodb-aws-env-vars: Environment Variables @@ -161,6 +182,22 @@ You can set this option in two ways: by passing an argument to the uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + .. tip:: AWS Lambda AWS Lambda runtimes can automatically set these environment variables during @@ -207,6 +244,22 @@ You can set this option in two ways: by passing an argument to the uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + .. tip:: To prevent {+driver-short+} from using a shared credentials file for authentication, @@ -257,6 +310,22 @@ You can set this option in two ways: by passing an argument to the uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + .. _pymongo-mongodb-aws-assume-role: AssumeRole Request @@ -314,6 +383,29 @@ You can set these options in two ways: by passing arguments to the "&authMechanism=MONGODB-AWS") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://@", + username="", + password="", + authMechanismProperties="AWS_SESSION_TOKEN:", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb+srv://:" + "" + "@/?" + "authMechanismProperties=AWS_SESSION_TOKEN:" + "&authMechanism=MONGODB-AWS") + client = pymongo.AsyncMongoClient(uri) + For more information about using the ``AssumeRole`` request to authenticate your application, see the following AWS documentation: @@ -366,6 +458,22 @@ You can set this option in two ways: by passing an argument to the uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + For more information about using an ``AssumeRoleWithWebIdentity`` request to authenticate your application, see the following AWS documentation: @@ -404,6 +512,22 @@ You can set this option in two ways: by passing an argument to the uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb+srv://", + authMechanism="MONGODB-AWS") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = "mongodb+srv:///?&authMechanism=MONGODB-AWS" + client = pymongo.AsyncMongoClient(uri) + API Documentation ----------------- diff --git a/source/security/authentication/kerberos.txt b/source/security/authentication/kerberos.txt index 15bb9238..23f1963a 100644 --- a/source/security/authentication/kerberos.txt +++ b/source/security/authentication/kerberos.txt @@ -109,6 +109,26 @@ to use Kerberos to authenticate. "&authMechanism=GSSAPI" "&authMechanismProperties=SERVICE_NAME:") client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="mongodbuser@EXAMPLE.COM", + authMechanism="GSSAPI", + authMechanismProperties="SERVICE_NAME:") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://mongodbuser%40EXAMPLE.COM@:/?" + "&authMechanism=GSSAPI" + "&authMechanismProperties=SERVICE_NAME:") + client = pymongo.AsyncMongoClient(uri) .. tab:: Windows (SSPI) :tabid: windows @@ -164,6 +184,33 @@ to use Kerberos to authenticate. "CANONICALIZE_HOST_NAME:true," "SERVICE_REALM:") client = pymongo.MongoClient(uri) + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb://:", + username="mongodbuser@EXAMPLE.COM", + authMechanism="GSSAPI", + password="", + authMechanismProperties="SERVICE_NAME:, + CANONICALIZE_HOST_NAME:true, + SERVICE_REALM:") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb://mongodbuser%40EXAMPLE.COM:" + "@:/?" + "&authMechanism=GSSAPI" + "&authMechanismProperties=" + "SERVICE_NAME:," + "CANONICALIZE_HOST_NAME:true," + "SERVICE_REALM:") + client = pymongo.AsyncMongoClient(uri) API Documentation ----------------- diff --git a/source/security/authentication/ldap.txt b/source/security/authentication/ldap.txt index 3378e43e..5cb0948b 100644 --- a/source/security/authentication/ldap.txt +++ b/source/security/authentication/ldap.txt @@ -92,6 +92,30 @@ You can set this option in two ways: by passing an argument to the "&tls=true") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb[+srv]://:", + username="", + password="", + authSource="", + authMechanism="PLAIN", + tls=True) + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb[+srv]://: + @:/?" + "authSource=" + "&authMechanism=PLAIN" + "&tls=true") + client = pymongo.AsyncMongoClient(uri) + API Documentation ----------------- diff --git a/source/security/authentication/oidc.txt b/source/security/authentication/oidc.txt index c68e4c2b..19c17228 100644 --- a/source/security/authentication/oidc.txt +++ b/source/security/authentication/oidc.txt @@ -238,18 +238,34 @@ After you define your callback class, create a Python dictionary that contains o :copyable: true :start-after: return OIDCCallbackResult(access_token=token) :end-before: client = MongoClient( - -Finally, set the following connection options by passing arguments to the ``MongoClient`` -constructor: + +Finally, set the following connection options by passing the followingarguments to the +``MongoClient`` constructor: - ``authMechanism``: Set to ``"MONGODB-OIDC"``. - ``authMechanismProperties``: Set to the ``properties`` dictionary that you created in the previous step. -.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py - :language: python - :copyable: true - :emphasize-lines: 14-18 +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/authentication/azure-envs-mongoclient.py + :language: python + :copyable: true + :start-after: properties = {"OIDC_CALLBACK": MyCallback()} + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/authentication/azure-envs-mongoclient-async.py + :language: python + :copyable: true + :start-after: properties = {"OIDC_CALLBACK": MyCallback()} .. _pymongo-mongodb-oidc-gcp-gke: @@ -282,18 +298,34 @@ After you define your callback class, create a Python dictionary that contains o :copyable: true :start-after: return OIDCCallbackResult(access_token=token) :end-before: client = MongoClient( - -Finally, set the following connection options by passing arguments to the ``MongoClient`` -constructor: + +Finally, set the following connection options by passing the following arguments to the +``MongoClient`` constructor: - ``authMechanism``: Set to ``"MONGODB-OIDC"``. - ``authMechanismProperties``: Set to the ``properties`` dictionary that you created in the previous step. -.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py - :language: python - :copyable: true - :emphasize-lines: 11-15 +Select the :guilabel:`Synchronous` or :guilabel:`Asynchronous` tab to see the corresponding +code: + +.. tabs:: + + .. tab:: Synchronous + :tabid: sync + + .. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py + :language: python + :copyable: true + :start-after: properties = {"OIDC_CALLBACK": MyCallback()} + + .. tab:: Asynchronous + :tabid: async + + .. literalinclude:: /includes/authentication/gcp-gke-mongoclient-async.py + :language: python + :copyable: true + :start-after: properties = {"OIDC_CALLBACK": MyCallback()} .. _pymongo-mongodb-oidc-kubernetes: @@ -326,3 +358,21 @@ the following tabs to see how to enable Kubernetes authentication for your appli :copyable: true :start-after: start-kubernetes-connection-string :end-before: end-kubernetes-connection-string + + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. literalinclude:: /includes/authentication/kubernetes-mongoclient.py + :language: python + :copyable: true + :start-after: start-kubernetes-mongoclient-async + :end-before: end-kubernetes-mongoclient-async + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. literalinclude:: /includes/authentication/kubernetes-connection-string.py + :language: python + :copyable: true + :start-after: start-kubernetes-connection-string-async + :end-before: end-kubernetes-connection-string-async \ No newline at end of file diff --git a/source/security/authentication/scram.txt b/source/security/authentication/scram.txt index 12324db6..000af75d 100644 --- a/source/security/authentication/scram.txt +++ b/source/security/authentication/scram.txt @@ -96,6 +96,28 @@ You can set this option in two ways: by passing an argument to the "&authMechanism=") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb[+srv]://:", + username="", + password="", + authSource="", + authMechanism="") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb[+srv]://:" + "@:/?" + "authSource=" + "&authMechanism=") + client = pymongo.AsyncMongoClient(uri) + API Documentation ----------------- diff --git a/source/security/authentication/x509.txt b/source/security/authentication/x509.txt index e40c6861..d03a63a8 100644 --- a/source/security/authentication/x509.txt +++ b/source/security/authentication/x509.txt @@ -84,6 +84,29 @@ You can set these options in two ways: by passing arguments to the "&authMechanism=MONGODB-X509") client = pymongo.MongoClient(uri) + .. tab:: MongoClient (Asynchronous) + :tabid: mongoclient-async + + .. code-block:: python + + client = pymongo.AsyncMongoClient("mongodb[+srv]://:", + tls=True, + tlsCertificateKeyFile="", + tlsCertificateKeyFilePassword="", + authMechanism="MONGODB-X509") + + .. tab:: Connection String (Asynchronous) + :tabid: connectionstring-async + + .. code-block:: python + + uri = ("mongodb[+srv]://:/?" + "tls=true" + "&tlsCertificateKeyFile=" + "&tlsCertificateKeyFilePassword=" + "&authMechanism=MONGODB-X509") + client = pymongo.AsyncMongoClient(uri) + API Documentation -----------------