Skip to content

Commit 843c1c3

Browse files
authored
DOCSP-47923: kubernetes oidc (#205)
1 parent 6a174b5 commit 843c1c3

File tree

5 files changed

+87
-1
lines changed

5 files changed

+87
-1
lines changed

examples/src/test/kotlin/EnterpriseAuthTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,32 @@ internal class EnterpriseAuthTest {
171171
// :snippet-end:
172172
}
173173

174+
fun oidcKubernetesConnectionString() = runBlocking {
175+
// :snippet-start: oidc-k8s-connection-string
176+
val connectionString = ConnectionString(
177+
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
178+
"authMechanism=MONGODB-OIDC" +
179+
"&authMechanismProperties=ENVIRONMENT:k8s,TOKEN_RESOURCE:<percent-encoded audience>")
180+
val mongoClient = MongoClient.create(connectionString)
181+
// :snippet-end:
182+
}
183+
184+
fun oidcKubernetesCredential() = runBlocking {
185+
// :snippet-start: oidc-k8s-credential
186+
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
187+
.withMechanismProperty("ENVIRONMENT", "k8s")
188+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
189+
190+
val mongoClient = MongoClient.create(
191+
MongoClientSettings.builder()
192+
.applyToClusterSettings { builder ->
193+
builder.hosts(listOf(ServerAddress("<hostname>", PORT)))
194+
}
195+
.credential(credential)
196+
.build())
197+
// :snippet-end:
198+
}
199+
174200
fun oidcCallback() = runBlocking {
175201
// :snippet-start: oidc-callback
176202
val credential = MongoCredential.createOidcCredential(null)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val connectionString = ConnectionString(
2+
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
3+
"authMechanism=MONGODB-OIDC" +
4+
"&authMechanismProperties=ENVIRONMENT:k8s,TOKEN_RESOURCE:<percent-encoded audience>")
5+
val mongoClient = MongoClient.create(connectionString)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
2+
.withMechanismProperty("ENVIRONMENT", "k8s")
3+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
4+
5+
val mongoClient = MongoClient.create(
6+
MongoClientSettings.builder()
7+
.applyToClusterSettings { builder ->
8+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
9+
}
10+
.credential(credential)
11+
.build())

source/fundamentals/enterprise-auth.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ to improve performance.
224224
- `JDK-6722928 <https://bugs.openjdk.java.net/browse/JDK-6722928>`__
225225
- `SO 23427343 <https://stackoverflow.com/questions/23427343/cannot-retrieve-tgt-despite-allowtgtsessionkey-registry-entry>`__
226226

227-
228227
.. _plain-auth-mechanism:
229228

230229
LDAP (PLAIN)
@@ -396,6 +395,46 @@ see the corresponding syntax.
396395
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-gcp-credential.kt
397396
:language: kotlin
398397

398+
.. _kotlin-auth-kubernetes:
399+
400+
Kubernetes
401+
++++++++++
402+
403+
If your application runs on a Kubernetes cluster, you can authenticate
404+
to MongoDB by using the {+driver-short+}'s built-in Kubernetes support.
405+
406+
Select from the :guilabel:`Connection String` or
407+
:guilabel:`MongoCredential` tabs to see the corresponding syntax.
408+
409+
.. tabs::
410+
411+
.. tab:: Connection String
412+
:tabid: mongodb-kubernetes-connection-string
413+
414+
To specify Kubernetes OIDC as the authentication mechanism, set the following
415+
options in your connection string:
416+
417+
- ``authMechanism``: Set to ``MONGODB-OIDC``.
418+
- ``authMechanismProperties``: Set to ``ENVIRONMENT:k8s``.
419+
420+
Replace the ``<percent-encoded audience>`` placeholder in the
421+
following code with the percent-encoded value of the audience server
422+
parameter configured on your MongoDB deployment.
423+
424+
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-k8s-connection-string.kt
425+
:language: kotlin
426+
427+
.. tab:: MongoCredential
428+
:tabid: mongodb-kubernetes-mongo-credential
429+
430+
Replace the ``hostname`` and ``port`` with the network address and port
431+
number of your MongoDB deployment. Also, replace the
432+
``<audience>`` placeholder with the value of the ``audience``
433+
server parameter configured on your MongoDB deployment.
434+
435+
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-k8s-credential.kt
436+
:language: kotlin
437+
399438
Custom Callback
400439
+++++++++++++++
401440

source/whats-new.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ and features:
3939
:ref:`kotlin-client-bulk-write-replace` sections of the Bulk
4040
Operations guide
4141

42+
.. replacement:: k8s-link
43+
44+
the :ref:`MONGODB-OIDC: Kubernetes <kotlin-auth-kubernetes>`
45+
section of the Enterprise Authentication Mechanisms guide
46+
4247
.. _kotlin-coroutine-version-5.3:
4348

4449
What's New in 5.3

0 commit comments

Comments
 (0)