Skip to content

Commit 480a960

Browse files
authored
Fix OIDC Examples (#83)
1 parent d0a68ae commit 480a960

10 files changed

+90
-151
lines changed

source/includes/authentication/azure-envs-connection-string.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

source/includes/authentication/azure-envs-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
1414
client = MongoClient(
1515
"mongodb://<hostname>:<port>",
1616
authMechanism="MONGODB-OIDC",
17-
authMechanismProperties=properties,
17+
authMechanismProperties=properties
1818
)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from pymongo import MongoClient
22

3-
# define properties, URI, and MongoClient
4-
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
3+
# define URI and MongoClient
54
uri = ("mongodb://<hostname>:<port>/?"
65
"username=<Azure identity client ID>"
76
"&authMechanism=MONGODB-OIDC"
8-
"&authMechanismProperties=properties")
7+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<audience>")
98
client = MongoClient(uri)

source/includes/authentication/azure-imds-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"mongodb://<hostname>:<port>",
77
username="<Azure identity client ID>",
88
authMechanism="MONGODB-OIDC",
9-
authMechanismProperties=properties,
9+
authMechanismProperties=properties
1010
)

source/includes/authentication/gcp-gke-connection-string.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

source/includes/authentication/gcp-gke-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
1111
client = MongoClient(
1212
"mongodb://<hostname>:<port>",
1313
authMechanism="MONGODB-OIDC",
14-
authMechanismProperties=properties,
14+
authMechanismProperties=properties
1515
)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from pymongo import MongoClient
22

3-
# define properties, URI, and MongoClient
4-
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
3+
# define URI and MongoClient
54
uri = ("mongodb://<hostname>:<port>/?"
65
"username=<GCP identity client ID>"
76
"&authMechanism=MONGODB-OIDC"
8-
"&authMechanismProperties=properties")
7+
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<audience>")
98
client = MongoClient(uri)

source/includes/authentication/gcp-imds-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"mongodb://<hostname>:<port>",
77
username="<GCP identity client ID>",
88
authMechanism="MONGODB-OIDC",
9-
authMechanismProperties=properties,
9+
authMechanismProperties=properties
1010
)

source/security.txt

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -478,43 +478,19 @@ To learn more about authenticating with OIDC, see
478478
Other Azure Environments
479479
~~~~~~~~~~~~~~~~~~~~~~~~
480480

481-
.. tabs::
482-
483-
.. tab:: MongoClient
484-
:tabid: mongoclient
485-
486-
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
487-
:language: python
488-
:copyable: true
489-
490-
.. tab:: Connection String
491-
:tabid: connectionstring
492-
493-
.. literalinclude:: /includes/authentication/azure-envs-connection-string.py
494-
:language: python
495-
:copyable: true
481+
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
482+
:language: python
483+
:copyable: true
496484

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

500488
GCP GKE
501489
~~~~~~~
502490

503-
.. tabs::
504-
505-
.. tab:: MongoClient
506-
:tabid: mongoclient
507-
508-
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
509-
:language: python
510-
:copyable: true
511-
512-
.. tab:: Connection String
513-
:tabid: connectionstring
514-
515-
.. literalinclude:: /includes/authentication/gcp-gke-connection-string.py
516-
:language: python
517-
:copyable: true
491+
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
492+
:language: python
493+
:copyable: true
518494

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

source/security/authentication.txt

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -563,31 +563,34 @@ If your application runs on an Azure VM, or otherwise uses the
563563
(IMDS), you can authenticate to MongoDB by using {+driver-short+}'s built-in Azure
564564
support.
565565

566-
First, create a Python dictionary for your authentication mechanism properties, as shown
567-
in the following example. Replace the ``<audience>`` placeholder with
568-
the percent-encoded application or service that the OIDC access token is intended for.
569-
570-
.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
571-
:language: python
572-
:copyable: true
573-
:start-after: # define properties and MongoClient
574-
:end-before: client = MongoClient(
575-
576-
Then, set the following connection options:
577-
578-
- ``username``: The client ID of the Azure managed identity.
579-
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
580-
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you
581-
created in the previous step.
582-
583-
You can set these options in two ways: by passing arguments to the
566+
You can configure OIDC for Azure IMDS in two ways: by passing arguments to the
584567
``MongoClient`` constructor or through parameters in your connection string.
585568

586569
.. tabs::
587570

588571
.. tab:: MongoClient
589572
:tabid: mongoclient
590573

574+
First, create a Python dictionary for your authentication mechanism properties, as shown
575+
in the following example. Replace the ``<audience>`` placeholder with
576+
the percent-encoded application or service that the OIDC access token is intended for.
577+
578+
.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
579+
:language: python
580+
:copyable: true
581+
:start-after: # define properties and MongoClient
582+
:end-before: client = MongoClient(
583+
584+
Then, set the following connection options:
585+
586+
- ``username``: The client ID of the Azure managed identity.
587+
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
588+
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you
589+
created in the previous step.
590+
591+
The following code example shows how to set these options when creating a
592+
``MongoClient``:
593+
591594
.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
592595
:language: python
593596
:copyable: true
@@ -596,10 +599,20 @@ You can set these options in two ways: by passing arguments to the
596599
.. tab:: Connection String
597600
:tabid: connectionstring
598601

602+
Include the following connection options in your connection string:
603+
604+
- ``username``: The client ID of the Azure managed identity.
605+
- ``authMechanism``: Set to ``MONGODB-OIDC``.
606+
- ``authMechanismProperties``: Set to ``environment:azure,token_resource:<audience>``.
607+
Replace the ``<audience>`` placeholder with the percent-encoded application or
608+
service that the OIDC access token is intended for.
609+
610+
The following code example shows how to set these options in your connection string:
611+
599612
.. literalinclude:: /includes/authentication/azure-imds-connection-string.py
600613
:language: python
601614
:copyable: true
602-
:emphasize-lines: 5-8
615+
:emphasize-lines: 4-7
603616

604617
.. tip::
605618

@@ -616,31 +629,34 @@ If your application runs on a GCP VM, or otherwise uses the
616629
you can authenticate to MongoDB by using {+driver-short+}'s built-in GCP
617630
support.
618631

619-
First, create a Python dictionary for your authentication mechanism properties, as shown
620-
in the following example. Replace the ``<audience>`` placeholder with
621-
the percent-encoded application or service that the OIDC access token is intended for.
622-
623-
.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
624-
:language: python
625-
:copyable: true
626-
:start-after: # define properties and MongoClient
627-
:end-before: client = MongoClient(
628-
629-
Then, set the following connection options:
630-
631-
- ``username``: The client ID of the GCP managed identity.
632-
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
633-
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created
634-
in the previous step.
635-
636-
You can set these options in two ways: by passing arguments to the
632+
You can configure OIDC for GCP IMDS in two ways: by passing arguments to the
637633
``MongoClient`` constructor or through parameters in your connection string.
638634

639635
.. tabs::
640636

641637
.. tab:: MongoClient
642638
:tabid: mongoclient
643639

640+
First, create a Python dictionary for your authentication mechanism properties, as shown
641+
in the following example. Replace the ``<audience>`` placeholder with
642+
the percent-encoded application or service that the OIDC access token is intended for.
643+
644+
.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
645+
:language: python
646+
:copyable: true
647+
:start-after: # define properties and MongoClient
648+
:end-before: client = MongoClient(
649+
650+
Then, set the following connection options:
651+
652+
- ``username``: The client ID of the GCP managed identity.
653+
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
654+
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you
655+
created in the previous step.
656+
657+
The following code example shows how to set these options when creating a
658+
``MongoClient``:
659+
644660
.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
645661
:language: python
646662
:copyable: true
@@ -649,10 +665,20 @@ You can set these options in two ways: by passing arguments to the
649665
.. tab:: Connection String
650666
:tabid: connectionstring
651667

668+
Include the following connection options in your connection string:
669+
670+
- ``username``: The client ID of the GCP managed identity.
671+
- ``authMechanism``: Set to ``MONGODB-OIDC``.
672+
- ``authMechanismProperties``: Set to ``environment:gcp,token_resource:<audience>``.
673+
Replace the ``<audience>`` placeholder with the percent-encoded application or
674+
service that the OIDC access token is intended for.
675+
676+
The following code example shows how to set these options in your connection string:
677+
652678
.. literalinclude:: /includes/authentication/gcp-imds-connection-string.py
653679
:language: python
654680
:copyable: true
655-
:emphasize-lines: 5-8
681+
:emphasize-lines: 4-7
656682

657683
.. _pymongo-mongodb-oidc-azure-envs:
658684

@@ -694,32 +720,17 @@ After you define your callback class, create a Python dictionary that contains o
694720
:start-after: return OIDCCallbackResult(access_token=token)
695721
:end-before: client = MongoClient(
696722

697-
Finally, set the following connection options:
723+
Finally, set the following connection options by passing arguments to the ``MongoClient``
724+
constructor:
698725

699726
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
700727
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created in the
701728
previous step.
702729

703-
You can set these options in two ways: by passing arguments to the
704-
``MongoClient`` constructor or through parameters in your connection string.
705-
706-
.. tabs::
707-
708-
.. tab:: MongoClient
709-
:tabid: mongoclient
710-
711-
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
712-
:language: python
713-
:copyable: true
714-
:emphasize-lines: 14-18
715-
716-
.. tab:: Connection String
717-
:tabid: connectionstring
718-
719-
.. literalinclude:: /includes/authentication/azure-envs-connection-string.py
720-
:language: python
721-
:copyable: true
722-
:emphasize-lines: 14-16
730+
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
731+
:language: python
732+
:copyable: true
733+
:emphasize-lines: 14-18
723734

724735
.. _pymongo-mongodb-oidc-gcp-gke:
725736

@@ -753,32 +764,17 @@ After you define your callback class, create a Python dictionary that contains o
753764
:start-after: return OIDCCallbackResult(access_token=token)
754765
:end-before: client = MongoClient(
755766

756-
Finally, set the following connection options:
767+
Finally, set the following connection options by passing arguments to the ``MongoClient``
768+
constructor:
757769

758770
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
759771
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created
760772
in the previous step.
761773

762-
You can set these options in two ways: by passing arguments to the
763-
``MongoClient`` constructor or through parameters in your connection string.
764-
765-
.. tabs::
766-
767-
.. tab:: MongoClient
768-
:tabid: mongoclient
769-
770-
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
771-
:language: python
772-
:copyable: true
773-
:emphasize-lines: 11-15
774-
775-
.. tab:: Connection String
776-
:tabid: connectionstring
777-
778-
.. literalinclude:: /includes/authentication/gcp-gke-connection-string.py
779-
:language: python
780-
:copyable: true
781-
:emphasize-lines: 11-13
774+
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
775+
:language: python
776+
:copyable: true
777+
:emphasize-lines: 11-15
782778

783779
API Documentation
784780
-----------------

0 commit comments

Comments
 (0)