Skip to content

Commit a1ec3d1

Browse files
authored
DOCSP-34479 - OIDC (#537)
1 parent 43ab2e3 commit a1ec3d1

File tree

2 files changed

+166
-2
lines changed

2 files changed

+166
-2
lines changed

source/fundamentals/auth.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,4 +574,4 @@ mechanism:
574574

575575
For additional information on configuring your application to use
576576
certificates as well as TLS/SSL options, see our
577-
:doc:`TLS/SSL guide </fundamentals/connection/tls>`.
577+
:doc:`TLS/SSL guide </fundamentals/connection/tls>`.

source/fundamentals/enterprise-auth.txt

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ Enterprise Authentication Mechanisms
1010
:depth: 2
1111
:class: singlecol
1212

13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: ldap, encryption, principal, tls
19+
1320
Overview
1421
--------
1522

@@ -22,13 +29,13 @@ Enterprise Edition:
2229

2330
- :ref:`Kerberos (GSSAPI) <gssapi-auth-mechanism>`
2431
- :ref:`LDAP (PLAIN) <plain-auth-mechanism>`
32+
- :ref:`MONGODB-OIDC <mongodb-oidc>`
2533

2634
To authenticate using another mechanism, see the
2735
:doc:`Authentication Mechanisms guide </fundamentals/auth>`. For more
2836
information on establishing a connection to your MongoDB cluster, read our
2937
:doc:`Connection Guide </fundamentals/connection>`.
3038

31-
3239
Specify an Authentication Mechanism
3340
-----------------------------------
3441

@@ -280,3 +287,160 @@ mechanism:
280287
method. The code to instantiate a ``MongoClient`` resembles the following:
281288

282289
.. include:: /includes/fundamentals/code-snippets/auth-credentials-ldap.rst
290+
291+
.. _mongodb-oidc:
292+
293+
MONGODB-OIDC
294+
~~~~~~~~~~~~
295+
296+
.. important::
297+
298+
The MONGODB-OIDC authentication mechanism requires {+mdb-server+} v7.0 or later running
299+
on a Linux platform.
300+
301+
The following sections describe how to use the MONGODB-OIDC authentication mechanism to
302+
authenticate to various platforms.
303+
304+
For more information about the MONGODB-OIDC authentication mechanism, see
305+
:manual:`OpenID Connect Authentication </core/security-oidc/>` and
306+
:manual:`MongoDB Server Parameters </reference/parameters/#mongodb-parameter-param.oidcIdentityProviders>`
307+
in the MongoDB Server manual.
308+
309+
.. _java-mongodb-oidc-azure-imds:
310+
311+
Azure IMDS
312+
++++++++++
313+
314+
If your application runs on an Azure VM, or otherwise uses the
315+
`Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__
316+
(IMDS), you can authenticate to MongoDB by using the {+driver-short+}'s built-in Azure
317+
support.
318+
319+
You can specify Azure IMDS OIDC authentication either by
320+
using a ``MongoCredential`` or as part of the connection string. Select the
321+
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
322+
see the corresponding syntax.
323+
324+
.. tabs::
325+
326+
.. tab:: Connection String
327+
:tabid: mongodb-azure-imds-connection-string
328+
329+
Replace the ``<percent-encoded audience>`` placeholder with the percent-encoded
330+
value of the ``audience`` server parameter configured on your MongoDB deployment.
331+
332+
.. code-block:: java
333+
334+
MongoClient mongoClient = MongoClients.create(
335+
"mongodb://<username>@<hostname>:<port>/?" +
336+
"?authMechanism=MONGODB-OIDC" +
337+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>");
338+
339+
.. tab:: MongoCredential
340+
:tabid: mongodb-azure-mongo-credential
341+
342+
Replace the ``<username>`` placeholder with the client ID or application ID of the
343+
Azure managed identity or enterprise application. Replace the ``<audience>``
344+
placeholder with the value of the
345+
``audience`` server parameter configured on your MongoDB deployment.
346+
347+
.. code-block:: java
348+
349+
MongoCredential credential = MongoCredential.createOidcCredential("<username>")
350+
.withMechanismProperty("ENVIRONMENT", "azure")
351+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>");
352+
353+
MongoClient mongoClient = MongoClients.create(
354+
MongoClientSettings.builder()
355+
.applyToClusterSettings(builder ->
356+
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
357+
.credential(credential)
358+
.build());
359+
360+
.. _java-mongodb-oidc-gcp-imds:
361+
362+
GCP IMDS
363+
++++++++++
364+
365+
If your application runs on a GCP VM, or otherwise uses the
366+
`GCP Instance Metadata Service <https://cloud.google.com/compute/docs/metadata/querying-metadata>`__,
367+
you can authenticate to MongoDB by using {+driver-short+}'s built-in GCP
368+
support.
369+
370+
You can specify GCP IMDS OIDC authentication either by
371+
using a ``MongoCredential`` or as part of the connection string. Select the
372+
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
373+
see the corresponding syntax.
374+
375+
.. tabs::
376+
377+
.. tab:: Connection String
378+
:tabid: mongodb-gcp-imds-connection-string
379+
380+
Replace the ``<percent-encoded audience>`` placeholder with the percent-encoded
381+
value of the ``audience`` server parameter configured on your MongoDB deployment.
382+
383+
.. code-block:: java
384+
385+
MongoClient mongoClient = MongoClients.create(
386+
"mongodb://<hostname>:<port>/?" +
387+
"authMechanism=MONGODB-OIDC" +
388+
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>");
389+
390+
.. tab:: MongoCredential
391+
:tabid: mongodb-gcp-mongo-credential
392+
393+
Replace the ``<audience>`` placeholder with the value of the
394+
``audience`` server parameter configured on your MongoDB deployment.
395+
396+
.. code-block:: java
397+
398+
MongoCredential credential = MongoCredential.createOidcCredential()
399+
.withMechanismProperty("ENVIRONMENT", "gcp")
400+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>");
401+
402+
MongoClient mongoClient = MongoClients.create(
403+
MongoClientSettings.builder()
404+
.applyToClusterSettings(builder ->
405+
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
406+
.credential(credential)
407+
.build());
408+
409+
Custom Callback
410+
+++++++++++++++
411+
412+
The {+driver-short+} doesn't offer built-in support for all platforms, including
413+
Azure Functions and Azure Kubernetes Service (AKS). Instead, you
414+
must define a custom callback to use OIDC to authenticate from these platforms.
415+
To do so, use the ``"OIDC_CALLBACK"`` authentication property, as shown in the following
416+
code example:
417+
418+
.. code-block:: java
419+
420+
MongoCredential credential = MongoCredential.createOidcCredential(null)
421+
.withMechanismProperty("OIDC_CALLBACK", (context) -> {
422+
String accessToken = ...
423+
return new OidcCallbackResult(accessToken);
424+
});
425+
426+
The value of the ``"OIDC_CALLBACK"`` property must be a lambda or other implementation
427+
of the ``OidcCallback`` functional interface that accepts an ``OidcCallbackContext``
428+
as a parameter and returns an ``OidcCallbackResult``.
429+
430+
The following example uses an example callback to retrieve an OIDC token from a file
431+
named ``"access-token.dat"`` in the local file system:
432+
433+
.. code-block:: java
434+
435+
MongoCredential credential = MongoCredential.createOidcCredential(null)
436+
.withMechanismProperty("OIDC_CALLBACK", (context) -> {
437+
string accessToken = new String(Files.readAllBytes(Paths.get("access-token.dat"));
438+
return new OidcCallbackResult(accessToken);
439+
});
440+
441+
MongoClient mongoClient = MongoClients.create(
442+
MongoClientSettings.builder()
443+
.applyToClusterSettings(builder ->
444+
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
445+
.credential(credential)
446+
.build());

0 commit comments

Comments
 (0)