@@ -563,31 +563,34 @@ If your application runs on an Azure VM, or otherwise uses the
563
563
(IMDS), you can authenticate to MongoDB by using {+driver-short+}'s built-in Azure
564
564
support.
565
565
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
584
567
``MongoClient`` constructor or through parameters in your connection string.
585
568
586
569
.. tabs::
587
570
588
571
.. tab:: MongoClient
589
572
:tabid: mongoclient
590
573
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
+
591
594
.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
592
595
:language: python
593
596
:copyable: true
@@ -596,10 +599,20 @@ You can set these options in two ways: by passing arguments to the
596
599
.. tab:: Connection String
597
600
:tabid: connectionstring
598
601
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
+
599
612
.. literalinclude:: /includes/authentication/azure-imds-connection-string.py
600
613
:language: python
601
614
:copyable: true
602
- :emphasize-lines: 5-8
615
+ :emphasize-lines: 4-7
603
616
604
617
.. tip::
605
618
@@ -616,31 +629,34 @@ If your application runs on a GCP VM, or otherwise uses the
616
629
you can authenticate to MongoDB by using {+driver-short+}'s built-in GCP
617
630
support.
618
631
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
637
633
``MongoClient`` constructor or through parameters in your connection string.
638
634
639
635
.. tabs::
640
636
641
637
.. tab:: MongoClient
642
638
:tabid: mongoclient
643
639
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
+
644
660
.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
645
661
:language: python
646
662
:copyable: true
@@ -649,10 +665,20 @@ You can set these options in two ways: by passing arguments to the
649
665
.. tab:: Connection String
650
666
:tabid: connectionstring
651
667
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
+
652
678
.. literalinclude:: /includes/authentication/gcp-imds-connection-string.py
653
679
:language: python
654
680
:copyable: true
655
- :emphasize-lines: 5-8
681
+ :emphasize-lines: 4-7
656
682
657
683
.. _pymongo-mongodb-oidc-azure-envs:
658
684
@@ -694,32 +720,17 @@ After you define your callback class, create a Python dictionary that contains o
694
720
:start-after: return OIDCCallbackResult(access_token=token)
695
721
:end-before: client = MongoClient(
696
722
697
- Finally, set the following connection options:
723
+ Finally, set the following connection options by passing arguments to the ``MongoClient``
724
+ constructor:
698
725
699
726
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
700
727
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created in the
701
728
previous step.
702
729
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
723
734
724
735
.. _pymongo-mongodb-oidc-gcp-gke:
725
736
@@ -753,32 +764,17 @@ After you define your callback class, create a Python dictionary that contains o
753
764
:start-after: return OIDCCallbackResult(access_token=token)
754
765
:end-before: client = MongoClient(
755
766
756
- Finally, set the following connection options:
767
+ Finally, set the following connection options by passing arguments to the ``MongoClient``
768
+ constructor:
757
769
758
770
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
759
771
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created
760
772
in the previous step.
761
773
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
782
778
783
779
API Documentation
784
780
-----------------
0 commit comments