Skip to content

Commit e699450

Browse files
authored
DOCSP-48166: Async examples for Security pages (#237)
1 parent c1bf88f commit e699450

24 files changed

+928
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pymongo import AsyncMongoClient
2+
from azure.identity import DefaultAzureCredential
3+
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
4+
5+
# define callback, properties, and MongoClient
6+
audience = "<audience>"
7+
client_id = "<Azure ID>"
8+
class MyCallback(OIDCCallback):
9+
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
10+
credential = DefaultAzureCredential(managed_identity_client_id=client_id)
11+
token = credential.get_token(f"{audience}/.default").token
12+
return OIDCCallbackResult(access_token=token)
13+
properties = {"OIDC_CALLBACK": MyCallback()}
14+
client = AsyncMongoClient(
15+
"mongodb[+srv]://<hostname>:<port>",
16+
authMechanism="MONGODB-OIDC",
17+
authMechanismProperties=properties
18+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pymongo import AsyncMongoClient
2+
3+
# define URI and MongoClient
4+
uri = ("mongodb[+srv]://<hostname>:<port>/?"
5+
"username=<username>"
6+
"&authMechanism=MONGODB-OIDC"
7+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
8+
client = AsyncMongoClient(uri)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pymongo import AsyncMongoClient
2+
3+
# define properties and MongoClient
4+
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
5+
client = AsyncMongoClient(
6+
"mongodb[+srv]://<hostname>:<port>",
7+
username="<Azure ID>",
8+
authMechanism="MONGODB-OIDC",
9+
authMechanismProperties=properties
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pymongo import AsyncMongoClient
2+
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
3+
4+
# define callback, properties, and MongoClient
5+
class MyCallback(OIDCCallback):
6+
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
7+
with open("/var/run/secrets/kubernetes.io/serviceaccount/token") as fid:
8+
token = fid.read()
9+
return OIDCCallbackResult(access_token=token)
10+
properties = {"OIDC_CALLBACK": MyCallback()}
11+
client = AsyncMongoClient(
12+
"mongodb[+srv]://<hostname>:<port>",
13+
authMechanism="MONGODB-OIDC",
14+
authMechanismProperties=properties
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pymongo import AsyncMongoClient
2+
3+
# define URI and MongoClient
4+
uri = ("mongodb[+srv]://<hostname>:<port>/?"
5+
"&authMechanism=MONGODB-OIDC"
6+
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
7+
client = AsyncMongoClient(uri)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pymongo import AsyncMongoClient
2+
3+
# define properties and MongoClient
4+
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
5+
client = AsyncMongoClient(
6+
"mongodb[+srv]://<hostname>:<port>",
7+
authMechanism="MONGODB-OIDC",
8+
authMechanismProperties=properties
9+
)
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
from pymongo import MongoClient
1+
from pymongo import MongoClient, AsyncMongoClient
22

33
# start-kubernetes-connection-string
44
uri = ("mongodb[+srv]://<hostname>:<port>/?"
55
"authMechanism=MONGODB-OIDC"
66
"&authMechanismProperties=ENVIRONMENT:k8s")
77
client = MongoClient(uri)
8-
# end-kubernetes-connection-string
8+
# end-kubernetes-connection-string
9+
10+
# start-kubernetes-connection-string-async
11+
uri = ("mongodb[+srv]://<hostname>:<port>/?"
12+
"authMechanism=MONGODB-OIDC"
13+
"&authMechanismProperties=ENVIRONMENT:k8s")
14+
client = AsyncMongoClient(uri)
15+
# end-kubernetes-connection-string-async
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pymongo import MongoClient
1+
from pymongo import MongoClient, AsyncMongoClient
22

33
# start-kubernetes-mongoclient
44
properties = {"ENVIRONMENT": "k8s"}
@@ -7,4 +7,13 @@
77
authMechanism="MONGODB-OIDC",
88
authMechanismProperties=properties
99
)
10-
# end-kubernetes-mongoclient
10+
# end-kubernetes-mongoclient
11+
12+
# start-kubernetes-mongoclient-async
13+
properties = {"ENVIRONMENT": "k8s"}
14+
client = AsyncMongoClient(
15+
"mongodb[+srv]://<hostname>:<port>",
16+
authMechanism="MONGODB-OIDC",
17+
authMechanismProperties=properties
18+
)
19+
# end-kubernetes-mongoclient-async

source/includes/connect/ca-file-tabs.rst

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,21 @@
1515
.. code-block:: python
1616
1717
uri = "mongodb://<db_username>:<db_password>@<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem"
18-
client = pymongo.MongoClient(uri)
18+
client = pymongo.MongoClient(uri)
19+
20+
.. tab:: MongoClient (Asynchronous)
21+
:tabid: mongoclient-async
22+
23+
.. code-block:: python
24+
25+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
26+
tls=True,
27+
tlsCAFile="/path/to/ca.pem")
28+
29+
.. tab:: Connection String (Asynchronous)
30+
:tabid: connectionstring-async
31+
32+
.. code-block:: python
33+
34+
uri = "mongodb://<db_username>:<db_password@<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem"
35+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/client-cert-tabs.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@
1717
uri = ("mongodb://<db_username>:<db_password>@<hostname:<port>/?"
1818
"tls=true"
1919
"&tlsCertificateKeyFile=path/to/client.pem")
20-
client = pymongo.MongoClient(uri)
20+
client = pymongo.MongoClient(uri)
21+
22+
.. tab:: MongoClient (Asynchronous)
23+
:tabid: mongoclient-async
24+
25+
.. code-block:: python
26+
27+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
28+
tls=True,
29+
tlsCertificateKeyFile='/path/to/client.pem')
30+
31+
.. tab:: Connection String (Asynchronous)
32+
:tabid: connectionstring-async
33+
34+
.. code-block:: python
35+
36+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
37+
"tls=true"
38+
"&tlsCertificateKeyFile=path/to/client.pem")
39+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/crl-tabs.rst

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,21 @@
1515
.. code-block:: python
1616
1717
uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem"
18-
client = pymongo.MongoClient(uri)
18+
client = pymongo.MongoClient(uri)
19+
20+
.. tab:: MongoClient (Asynchronous)
21+
:tabid: mongoclient-async
22+
23+
.. code-block:: python
24+
25+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
26+
tls=True,
27+
tlsCRLFile="/path/to/crl.pem")
28+
29+
.. tab:: Connection String (Asynchronous)
30+
:tabid: connectionstring-async
31+
32+
.. code-block:: python
33+
34+
uri = "mongodb://example.com/?tls=true&tlsCRLFile=/path/to/crl.pem"
35+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/disable-cert-validation-tabs.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@
1717
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
1818
"tls=true"
1919
"&tlsAllowInvalidCertificates=true")
20-
client = pymongo.MongoClient(uri)
20+
client = pymongo.MongoClient(uri)
21+
22+
.. tab:: MongoClient (Asynchronous)
23+
:tabid: mongoclient-async
24+
25+
.. code-block:: python
26+
27+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
28+
tls=True,
29+
tlsAllowInvalidCertificates=True)
30+
31+
.. tab:: Connection String (Asynchronous)
32+
:tabid: connectionstring-async
33+
34+
.. code-block:: python
35+
36+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
37+
"tls=true"
38+
"&tlsAllowInvalidCertificates=true")
39+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/disable-host-verification-tabs.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@
1717
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
1818
"tls=true"
1919
"&tlsAllowInvalidHostnames=true")
20-
client = pymongo.MongoClient(uri)
20+
client = pymongo.MongoClient(uri)
21+
22+
.. tab:: MongoClient (Asynchronous)
23+
:tabid: mongoclient-async
24+
25+
.. code-block:: python
26+
27+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
28+
tls=True,
29+
tlsAllowInvalidHostnames=True)
30+
31+
.. tab:: Connection String (Asynchronous)
32+
:tabid: connectionstring-async
33+
34+
.. code-block:: python
35+
36+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
37+
"tls=true"
38+
"&tlsAllowInvalidHostnames=true")
39+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/insecure-tls-tabs.rst

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@
1717
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
1818
"tls=true"
1919
"&tlsInsecure=true")
20-
client = pymongo.MongoClient(uri)
20+
client = pymongo.MongoClient(uri)
21+
22+
.. tab:: MongoClient (Asynchronous)
23+
:tabid: mongoclient-async
24+
25+
.. code-block:: python
26+
27+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
28+
tls=True,
29+
tlsInsecure=True)
30+
31+
.. tab:: Connection String (Asynchronous)
32+
:tabid: connectionstring-async
33+
34+
.. code-block:: python
35+
36+
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?"
37+
"tls=true"
38+
"&tlsInsecure=true")
39+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/key-file-password.rst

+23-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,26 @@
1919
"tls=true"
2020
"&tlsCertificateKeyFile=path/to/client.pem"
2121
"&tlsCertificateKeyFilePassword=<passphrase>")
22-
client = pymongo.MongoClient(uri)
22+
client = pymongo.MongoClient(uri)
23+
24+
.. tab:: MongoClient (Asynchronous)
25+
:tabid: mongoclient-async
26+
27+
.. code-block:: python
28+
29+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
30+
tls=True,
31+
tlsCertificateKeyFile='/path/to/client.pem',
32+
tlsCertificateKeyFilePassword=<passphrase>)
33+
34+
.. tab:: Connection String (Asynchronous)
35+
:tabid: connectionstring-async
36+
37+
.. code-block:: python
38+
39+
uri = ("mongodb://<db_username>:<db_password"
40+
"@<hostname>:<port>/?"
41+
"tls=true"
42+
"&tlsCertificateKeyFile=path/to/client.pem"
43+
"&tlsCertificateKeyFilePassword=<passphrase>")
44+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/ocsp-tabs.rst

+18-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,21 @@
1515
.. code-block:: python
1616
1717
uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true"
18-
client = pymongo.MongoClient(uri)
18+
client = pymongo.MongoClient(uri)
19+
20+
.. tab:: MongoClient (Asynchronous)
21+
:tabid: mongoclient-async
22+
23+
.. code-block:: python
24+
25+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>",
26+
tls=True,
27+
tlsDisableOCSPEndpointCheck=True)
28+
29+
.. tab:: Connection String (Asynchronous)
30+
:tabid: connectionstring-async
31+
32+
.. code-block:: python
33+
34+
uri = "mongodb://example.com/?tls=true&tlsDisableOCSPEndpointCheck=true"
35+
client = pymongo.AsyncMongoClient(uri)

source/includes/connect/tls-tabs.rst

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@
1212

1313
.. code-block:: python
1414
15-
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")
15+
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")
16+
17+
.. tab:: MongoClient (Asynchronous)
18+
:tabid: mongoclient-async
19+
20+
.. code-block:: python
21+
22+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname:<port>", tls=True)
23+
24+
.. tab:: Connection String (Asynchronous)
25+
:tabid: connectionstring-async
26+
27+
.. code-block:: python
28+
29+
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>?tls=true")

0 commit comments

Comments
 (0)