Skip to content

Commit 462dafd

Browse files
authored
OIDC Updates (#85)
1 parent 480a960 commit 462dafd

File tree

6 files changed

+263
-257
lines changed

6 files changed

+263
-257
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
44

55
# define callback, properties, and MongoClient
6-
audience = "<percent-encoded application or service that the OIDC access token is intended for>"
7-
client_id = "<Azure identity client ID>"
6+
audience = "<audience configured on the MongoDB deployment>"
7+
client_id = "<Azure client ID>"
88
class MyCallback(OIDCCallback):
99
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
1010
credential = DefaultAzureCredential(managed_identity_client_id=client_id)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# define URI and MongoClient
44
uri = ("mongodb://<hostname>:<port>/?"
5-
"username=<Azure identity client ID>"
5+
"username=<Azure client ID or application ID>"
66
"&authMechanism=MONGODB-OIDC"
77
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<audience>")
88
client = MongoClient(uri)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
55
client = MongoClient(
66
"mongodb://<hostname>:<port>",
7-
username="<Azure identity client ID>",
7+
username="<Azure client ID or application ID>",
88
authMechanism="MONGODB-OIDC",
99
authMechanismProperties=properties
1010
)

source/security.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ To learn more about authenticating with PLAIN SASL, see
431431
MONGODB-OIDC
432432
------------
433433

434+
.. note:: MongoDB Enterprise Only
435+
436+
MONGODB-OIDC authentication is available only in MongoDB Enterprise.
437+
434438
Azure IMDS
435439
~~~~~~~~~~
436440

source/security/authentication.txt

Lines changed: 1 addition & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -532,255 +532,10 @@ You can set this option in two ways: by passing an argument to the
532532
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
533533
client = pymongo.MongoClient(uri)
534534

535-
.. _pymongo-mongodb-oidc:
536-
537-
MONGODB-OIDC
538-
------------
539-
540-
.. important::
541-
542-
The MONGODB-OIDC authentication mechanism requires MongoDB v7.0 or later running
543-
on a Linux platform.
544-
545-
{+driver-short+} supports OIDC authentication for **workload identities**. A workload
546-
identity is an identity you assign to a software workload, such as an application,
547-
service, script, or container, to authenticate and access other services and resources.
548-
549-
The following sections describe how to use the MONGODB-OIDC authentication mechanism to
550-
authenticate to various platforms.
551-
552-
For more information about the MONGODB-OIDC authentication mechanism, see
553-
:manual:`OpenID Connect Authentication </core/security-oidc/>` in the MongoDB Server
554-
manual.
555-
556-
.. _pymongo-mongodb-oidc-azure-imds:
557-
558-
Azure IMDS
559-
~~~~~~~~~~
560-
561-
If your application runs on an Azure VM, or otherwise uses the
562-
`Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__
563-
(IMDS), you can authenticate to MongoDB by using {+driver-short+}'s built-in Azure
564-
support.
565-
566-
You can configure OIDC for Azure IMDS in two ways: by passing arguments to the
567-
``MongoClient`` constructor or through parameters in your connection string.
568-
569-
.. tabs::
570-
571-
.. tab:: MongoClient
572-
:tabid: mongoclient
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-
594-
.. literalinclude:: /includes/authentication/azure-imds-mongoclient.py
595-
:language: python
596-
:copyable: true
597-
:emphasize-lines: 5-10
598-
599-
.. tab:: Connection String
600-
:tabid: connectionstring
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-
612-
.. literalinclude:: /includes/authentication/azure-imds-connection-string.py
613-
:language: python
614-
:copyable: true
615-
:emphasize-lines: 4-7
616-
617-
.. tip::
618-
619-
If your application is running on an Azure VM, and only one managed identity is
620-
associated with the VM, you can omit the ``username`` connection option.
621-
622-
.. _pymongo-mongodb-oidc-gcp-imds:
623-
624-
GCP IMDS
625-
~~~~~~~~
626-
627-
If your application runs on a GCP VM, or otherwise uses the
628-
`GCP Instance Metadata Service <https://cloud.google.com/compute/docs/metadata/querying-metadata>`__,
629-
you can authenticate to MongoDB by using {+driver-short+}'s built-in GCP
630-
support.
631-
632-
You can configure OIDC for GCP IMDS in two ways: by passing arguments to the
633-
``MongoClient`` constructor or through parameters in your connection string.
634-
635-
.. tabs::
636-
637-
.. tab:: MongoClient
638-
:tabid: mongoclient
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-
660-
.. literalinclude:: /includes/authentication/gcp-imds-mongoclient.py
661-
:language: python
662-
:copyable: true
663-
:emphasize-lines: 5-10
664-
665-
.. tab:: Connection String
666-
:tabid: connectionstring
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-
678-
.. literalinclude:: /includes/authentication/gcp-imds-connection-string.py
679-
:language: python
680-
:copyable: true
681-
:emphasize-lines: 4-7
682-
683-
.. _pymongo-mongodb-oidc-azure-envs:
684-
685-
Other Azure Environments
686-
~~~~~~~~~~~~~~~~~~~~~~~~
687-
688-
If your application runs on Azure Functions, App Service Environment (ASE), or Azure
689-
Kubernetes Service (AKS), you can use the
690-
`azure-identity <https://pypi.org/project/azure-identity/>`__ package to fetch
691-
authentication credentials.
692-
693-
First, use pip to install the ``azure-identity`` library, as shown in the
694-
following example:
695-
696-
.. code-block:: sh
697-
698-
python3 -m pip install azure-identity
699-
700-
Next, define a class that inherits from the ``OIDCCallback`` class. This class must
701-
implement a ``fetch()`` method, which returns the OIDC token in the form of an
702-
``OIDCCallbackResult`` object.
703-
704-
The following example shows how to define a callback class named ``MyCallback``. This class
705-
includes a ``fetch()`` method that retrieves an OIDC token from a file in the standard
706-
service-account token-file location.
707-
708-
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
709-
:language: python
710-
:copyable: true
711-
:start-after: # define callback, properties, and MongoClient
712-
:end-before: properties = {"OIDC_CALLBACK": MyCallback()}
713-
714-
After you define your callback class, create a Python dictionary that contains one key,
715-
``"OIDC_CALLBACK"``, whose value is an instance of your custom callback class:
716-
717-
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
718-
:language: python
719-
:copyable: true
720-
:start-after: return OIDCCallbackResult(access_token=token)
721-
:end-before: client = MongoClient(
722-
723-
Finally, set the following connection options by passing arguments to the ``MongoClient``
724-
constructor:
725-
726-
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
727-
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created in the
728-
previous step.
729-
730-
.. literalinclude:: /includes/authentication/azure-envs-mongoclient.py
731-
:language: python
732-
:copyable: true
733-
:emphasize-lines: 14-18
734-
735-
.. _pymongo-mongodb-oidc-gcp-gke:
736-
737-
GCP GKE
738-
~~~~~~~
739-
740-
If your application runs on a GCP Google Kubernetes Engine (GKE) cluster with a
741-
`configured service account <https://cloud.google.com/kubernetes-engine/docs/how-to/service-accounts>`__,
742-
you can read the OIDC token from the standard service-account token-file location.
743-
744-
First, define a class that inherits from the ``OIDCCallback`` class. This class must
745-
implement a ``fetch()`` method, which returns the OIDC token in the form of an
746-
``OIDCCallbackResult`` object.
747-
748-
The following example shows how to define a callback class named ``MyCallback``. This class
749-
includes a ``fetch()`` method that retrieves an OIDC token from a file in the standard
750-
service-account token-file location.
751-
752-
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
753-
:language: python
754-
:copyable: true
755-
:start-after: # define callback, properties, and MongoClient
756-
:end-before: properties = {"OIDC_CALLBACK": MyCallback()}
757-
758-
After you define your callback class, create a Python dictionary that contains one key,
759-
``"OIDC_CALLBACK"``, whose value is an instance of your custom callback class:
760-
761-
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
762-
:language: python
763-
:copyable: true
764-
:start-after: return OIDCCallbackResult(access_token=token)
765-
:end-before: client = MongoClient(
766-
767-
Finally, set the following connection options by passing arguments to the ``MongoClient``
768-
constructor:
769-
770-
- ``authMechanism``: Set to ``"MONGODB-OIDC"``.
771-
- ``authMechanismProperties``: Set to the ``properties`` dictionary that you created
772-
in the previous step.
773-
774-
.. literalinclude:: /includes/authentication/gcp-gke-mongoclient.py
775-
:language: python
776-
:copyable: true
777-
:emphasize-lines: 11-15
778-
779535
API Documentation
780536
-----------------
781537

782538
To learn more about authenticating your application in {+driver-short+},
783539
see the following API documentation:
784540

785-
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__
786-
- `OIDCCallback <{+api-root+}pymongo/auth_oidc.html#pymongo.auth_oidc.OIDCCallback>`__
541+
- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__

0 commit comments

Comments
 (0)