@@ -10,6 +10,13 @@ Enterprise Authentication Mechanisms
10
10
:depth: 2
11
11
:class: singlecol
12
12
13
+ .. facet::
14
+ :name: genre
15
+ :values: reference
16
+
17
+ .. meta::
18
+ :keywords: ldap, encryption, principal, tls
19
+
13
20
Overview
14
21
--------
15
22
@@ -22,13 +29,13 @@ Enterprise Edition:
22
29
23
30
- :ref:`Kerberos (GSSAPI) <gssapi-auth-mechanism>`
24
31
- :ref:`LDAP (PLAIN) <plain-auth-mechanism>`
32
+ - :ref:`MONGODB-OIDC <mongodb-oidc>`
25
33
26
34
To authenticate using another mechanism, see the
27
35
:doc:`Authentication Mechanisms guide </fundamentals/auth>`. For more
28
36
information on establishing a connection to your MongoDB cluster, read our
29
37
:doc:`Connection Guide </fundamentals/connection>`.
30
38
31
-
32
39
Specify an Authentication Mechanism
33
40
-----------------------------------
34
41
@@ -280,3 +287,160 @@ mechanism:
280
287
method. The code to instantiate a ``MongoClient`` resembles the following:
281
288
282
289
.. 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