Skip to content

DOCSP-47923: kubernetes oidc #86

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

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions source/includes/security/enterprise-auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ val mongoClient = MongoClient.create(
.build())
// end-oidc-gcp-mongo-cred

// start-oidc-k8s-connect-str
val connectionString = ConnectionString(
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
"authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:k8s,TOKEN_RESOURCE:<percent-encoded audience>")
val mongoClient = MongoClient.create(connectionString)
// end-oidc-k8s-connect-str

// start-oidc-k8s-mongo-cred
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
.withMechanismProperty("ENVIRONMENT", "k8s")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")

val mongoClient = MongoClient.create(
MongoClientSettings.builder()
.applyToClusterSettings { builder ->
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
}
.credential(credential)
.build())
// end-oidc-k8s-mongo-cred

// start-oidc-custom-callback
val credential = MongoCredential.createOidcCredential(null)
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
Expand Down
46 changes: 46 additions & 0 deletions source/security/enterprise-auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,52 @@ see the corresponding syntax.
:start-after: start-oidc-gcp-mongo-cred
:end-before: end-oidc-gcp-mongo-cred

.. _kotlin-sync-auth-kubernetes:

Kubernetes
~~~~~~~~~~

If your application runs on a Kubernetes cluster, you can authenticate
to MongoDB by using the {+driver-short+}'s built-in Kubernetes support.

Select from the :guilabel:`Connection String` or
:guilabel:`MongoCredential` tabs to see the corresponding syntax.

.. tabs::

.. tab:: Connection String
:tabid: mongodb-kubernetes-connection-string

To specify Kubernetes OIDC as the authentication mechanism, set the following
options in your connection string:

- ``authMechanism``: Set to ``MONGODB-OIDC``.
- ``authMechanismProperties``: Set to ``ENVIRONMENT:k8s``.

Replace the ``<percent-encoded audience>`` placeholder in the
following code with the percent-encoded value of the audience server
parameter configured on your MongoDB deployment.

.. literalinclude:: /includes/security/enterprise-auth.kt
:language: kotlin
:dedent:
:start-after: start-oidc-k8s-connect-str
:end-before: end-oidc-k8s-connect-str

.. tab:: MongoCredential
:tabid: mongodb-kubernetes-mongo-credential

Replace the ``hostname`` and ``port`` with the network address and port
number of your MongoDB deployment. Also, replace the
``<audience>`` placeholder with the value of the ``audience``
server parameter configured on your MongoDB deployment.

.. literalinclude:: /includes/security/enterprise-auth.kt
:language: kotlin
:dedent:
:start-after: start-oidc-k8s-mongo-cred
:end-before: end-oidc-k8s-mongo-cred

Custom Callback
~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ and features:
:ref:`kotlin-sync-client-bulk-write-replace` sections of the Bulk Write
Operations guide

.. replacement:: k8s-link

the :ref:`MONGODB-OIDC: Kubernetes <kotlin-sync-auth-kubernetes>`
section of the Enterprise Authentication Mechanisms guide

.. _kotlin-sync-version-5.3:

What's New in 5.3
Expand Down
Loading