-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-34479 - OIDC #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOCSP-34479 - OIDC #537
Changes from 7 commits
0def0b4
b0e5bb0
34bc74d
5955ac5
ff6e000
f13c406
f4b472f
b0e5131
2933fe6
a28e861
fa3cff5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,13 @@ Enterprise Authentication Mechanisms | |
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: ldap, encryption, principal, tls | ||
|
||
Overview | ||
-------- | ||
|
||
|
@@ -22,13 +29,13 @@ Enterprise Edition: | |
|
||
- :ref:`Kerberos (GSSAPI) <gssapi-auth-mechanism>` | ||
- :ref:`LDAP (PLAIN) <plain-auth-mechanism>` | ||
- :ref:`MONGODB-OIDC <mongodb-oidc>` | ||
|
||
To authenticate using another mechanism, see the | ||
:doc:`Authentication Mechanisms guide </fundamentals/auth>`. For more | ||
information on establishing a connection to your MongoDB cluster, read our | ||
:doc:`Connection Guide </fundamentals/connection>`. | ||
|
||
|
||
Specify an Authentication Mechanism | ||
----------------------------------- | ||
|
||
|
@@ -280,3 +287,154 @@ mechanism: | |
method. The code to instantiate a ``MongoClient`` resembles the following: | ||
|
||
.. include:: /includes/fundamentals/code-snippets/auth-credentials-ldap.rst | ||
|
||
.. _mongodb-oidc: | ||
|
||
MONGODB-OIDC | ||
~~~~~~~~~~~~ | ||
|
||
.. important:: | ||
|
||
The MONGODB-OIDC authentication mechanism requires {+mdb-server+} v7.0 or later running | ||
on a Linux platform. | ||
|
||
The following sections describe how to use the MONGODB-OIDC authentication mechanism to | ||
authenticate to various platforms. | ||
|
||
For more information about the MONGODB-OIDC authentication mechanism, see | ||
:manual:`OpenID Connect Authentication </core/security-oidc/>` in the MongoDB Server | ||
manual. | ||
|
||
.. _java-mongodb-oidc-azure-imds: | ||
|
||
Azure IMDS | ||
++++++++++ | ||
|
||
If your application runs on an Azure VM, or otherwise uses the | ||
`Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__ | ||
(IMDS), you can authenticate to MongoDB by using the {+driver-short+}'s built-in Azure | ||
support. | ||
|
||
You can specify Azure IMDS OIDC authentication either by | ||
using a ``MongoCredential`` object or as part of the connection string. Select the | ||
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to | ||
see the corresponding syntax. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Connection String | ||
:tabid: mongodb-azure-imds-connection-string | ||
|
||
Replace the ``<resource>`` placeholder with the percent-encoded application or | ||
service that the OIDC access token is intended for. | ||
|
||
.. code-block:: java | ||
|
||
MongoClient mongoClient = MongoClients.create("mongodb://<username>@<hostname>:<port>/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<resource>"); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. tab:: MongoCredential | ||
:tabid: mongodb-azure-mongo-credential | ||
|
||
Replace the ``<username>`` placeholder with the client ID or application ID of the | ||
Azure managed identity or enterprise application. Replace the ``<resource>`` | ||
placeholder with the application or service that the OIDC access token is intended for. | ||
You must percent-encode the resource value | ||
if it contains a comma (``,``), plus sign (``+``), or ampersand (``&``). | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: java | ||
|
||
MongoCredential credential = MongoCredential.createOidcCredential("<username>") | ||
.withMechanismProperty("TOKEN_RESOURCE", "<resource>") | ||
.withMechanismProperty("ENVIRONMENT", "azure"); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
MongoClient mongoClient = MongoClients.create( | ||
MongoClientSettings.builder() | ||
.applyToClusterSettings(builder -> | ||
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) | ||
.credential(credential) | ||
.build()); | ||
|
||
.. _java-mongodb-oidc-gcp-imds: | ||
|
||
GCP IMDS | ||
++++++++++ | ||
|
||
If your application runs on a GCP VM, or otherwise uses the | ||
`GCP Instance Metadata Service <https://cloud.google.com/compute/docs/metadata/querying-metadata>`__, | ||
you can authenticate to MongoDB by using {+driver-short+}'s built-in GCP | ||
support. | ||
|
||
You can specify GCP IMDS OIDC authentication either by | ||
using a ``MongoCredential`` object or as part of the connection string. Select the | ||
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to | ||
see the corresponding syntax. | ||
|
||
.. tabs:: | ||
|
||
.. tab:: Connection String | ||
:tabid: mongodb-gcp-imds-connection-string | ||
|
||
Replace the ``<resource>`` placeholder with the percent-encoded application or | ||
service that the OIDC access token is intended for. | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: java | ||
|
||
MongoClient mongoClient = MongoClients.create("mongodb://<username>@<hostname>:<port>/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<resource>"); | ||
|
||
.. tab:: MongoCredential | ||
:tabid: mongodb-gcp-mongo-credential | ||
|
||
Replace the ``<username>`` placeholder with the client ID of the GCP managed | ||
identity. Replace the ``<resource>`` placeholder with the | ||
application or service that the OIDC access token is intended for. | ||
You must percent-encode the resource value | ||
if it contains a comma (``,``), plus sign (``+``), or ampersand (``&``). | ||
|
||
.. code-block:: java | ||
|
||
MongoCredential credential = MongoCredential.createOidcCredential("<username>") | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.withMechanismProperty("TOKEN_RESOURCE", "<resource>") | ||
.withMechanismProperty("ENVIRONMENT", "gcp"); | ||
|
||
MongoClient mongoClient = MongoClients.create( | ||
MongoClientSettings.builder() | ||
.applyToClusterSettings(builder -> | ||
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) | ||
.credential(credential) | ||
.build()); | ||
|
||
Custom Callback | ||
+++++++++++++++ | ||
|
||
On some platforms, such as Azure Functions and Azure Kubernetes Service (AKS), you | ||
must define a custom callback method to use OIDC to authenticate. To use a custom | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
callback to retrieve an OIDC token, call the ``withMechanismProperty()`` method on | ||
your ``MongoCredential`` object to set the following options: | ||
|
||
- ``"ALLOWED_HOSTS"``: The list of allowed hostnames or IP addresses for MongoDB. | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The hostnames may include ``"*."`` as a leading wildcard, which allows for | ||
matching subdomains. By default, this property includes ``"*.mongodb.net"``, | ||
``"*.mongodb-qa.net"``, ``"*.mongodb-dev.net"``, ``"*.mongodbgov.net"``, ``"localhost"``, | ||
``"127.0.0.1"``, and ``"::1"``. | ||
- ``"OIDC_CALLBACK"``: The method that retrieves the OIDC token. This method must accept | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's an authentication mechanism property that works with the OIDC auth mechanism. The whole thing is like a key-value pair, where "OIDC_CALLBACK" is the key and the callback method is the value. |
||
a ``context`` object as a parameter and return an ``OidcCallbackResult`` object. | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following code example shows how to include | ||
a callback method in your ``MongoCredential`` object. In this example, the callback | ||
retrieves an OIDC token from a file named ``"access-token.dat"`` in the local file system. | ||
|
||
.. code-block:: java | ||
|
||
MongoCredential credential = new Credential() | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.withMechanismProperty("ALLOWED_HOSTS", allowedHosts) | ||
.withMechanismProperty("OIDC_CALLBACK", (context) -> { | ||
string accessToken = new String(Files.readAllBytes(Paths.get("access-token.dat")); | ||
return new OidcCallbackResult(accessToken, 0); | ||
katcharov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
MongoClient mongoClient = MongoClients.create( | ||
MongoClientSettings.builder() | ||
.applyToClusterSettings(builder -> | ||
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>)))) | ||
.credential(credential) | ||
.build()); |
Uh oh!
There was an error while loading. Please reload this page.