Skip to content

Commit 8429a70

Browse files
committed
mongocredential section and edits
1 parent 9cc48e4 commit 8429a70

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

source/security/enterprise-auth.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,36 @@ Kubernetes
428428
If your application runs on a Kubernetes cluster, you can authenticate to MongoDB by using
429429
the {+driver-short+}'s built-in Kubernetes support.
430430

431-
To specify Kubernetes OIDC as the authentication mechanism, set the following
432-
options in your connection string:
431+
Select from the :guilabel:`Connection String` or :guilabel:`MongoCredential` tabs to
432+
see the corresponding syntax.
433433

434-
- ``authMechanism``: Set to ``MONGODB-OIDC``.
435-
- ``authMechanismProperties``: Set to ``ENVIRONMENT:k8s``.
434+
.. tabs::
436435

437-
The following code example shows how to set the preceding connection options:
436+
.. tab:: Connection String
437+
:tabid: mongodb-kubernetes-connection-string
438438

439-
.. code-block:: java
439+
.. code-block:: java
440+
441+
MongoClient mongoClient = MongoClients.create(
442+
"mongodb://<hostname>:<port>/" +
443+
"?authMechanism=MONGODB-OIDC" +
444+
"&authMechanismProperties=ENVIRONMENT:k8s");
445+
446+
.. tab:: MongoCredential
447+
:tabid: mongodb-kubernetes-mongo-credential
448+
449+
.. code-block:: java
450+
451+
MongoCredential credential = MongoCredential.createOidcCredential()
452+
.withMechanismProperty("ENVIRONMENT", "k8s");
453+
454+
MongoClient mongoClient = MongoClients.create(
455+
MongoClientSettings.builder()
456+
.applyToClusterSettings(builder ->
457+
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
458+
.credential(credential)
459+
.build());
440460

441-
String uri = "mongodb://<hostname>:<port>/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s";
442-
MongoClient mongoClient = MongoClients.create(uri)
443461

444462
Custom Callback
445463
+++++++++++++++

0 commit comments

Comments
 (0)