Skip to content

Commit 3e977ef

Browse files
committed
DOCSP-48312 Kubernetes Auth Support
1 parent fe0aa5b commit 3e977ef

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

source/fundamentals/enterprise-auth.txt

+65
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,71 @@ callback function that you defined:
478478
:end-before: end-credential-callback
479479
:emphasize-lines: 6
480480

481+
Kubernetes
482+
~~~~~~~~~~
483+
484+
If your application runs on a Kubernetes cluster, you can authenticate to MongoDB
485+
by using the {+driver-short+}'s built-in Kubernetes support.
486+
487+
You can configure OIDC for Kubernetes in the following ways:
488+
489+
- By creating a ``Credential`` struct and passing it to the
490+
``SetAuth()`` method when creating a client
491+
- By setting parameters in your connection string
492+
493+
.. include:: /includes/authentication/auth-properties-commas.rst
494+
495+
.. tabs::
496+
497+
.. tab:: Credential
498+
:tabid: credential struct
499+
500+
First, create a map to store your authentication
501+
mechanism properties, as shown in the following example.
502+
503+
.. code-block:: go
504+
505+
props := map[string]string{
506+
"ENVIRONMENT": "k8s",
507+
}
508+
509+
Then, set the following ``Credential`` struct fields:
510+
511+
- ``AuthMechanism``: Set to ``"MONGODB-OIDC"``.
512+
- ``AuthMechanismProperties``: Set to the ``props`` map that you
513+
previously created.
514+
515+
The following code example shows how to set these options when creating a
516+
``Client``:
517+
518+
.. literalinclude:: /includes/authentication/kubernetes.go
519+
:language: go
520+
:dedent:
521+
:copyable: true
522+
:start-after: start-kubernetes
523+
:end-before: end-kubernetes
524+
525+
.. tab:: Connection String
526+
:tabid: connectionstring
527+
528+
Include the following connection options in your connection string:
529+
530+
- ``authMechanism``: Set to ``MONGODB-OIDC``.
531+
- ``authMechanismProperties``: Set to``ENVIRONMENT:k8s``.
532+
533+
The following code example shows how to set these options in your connection string:
534+
535+
.. code-block:: go
536+
537+
uri := "mongodb://<hostname>:<port>/?" +
538+
"&authMechanism=MONGODB-OIDC" +
539+
"&authMechanismProperties=ENVIRONMENT:k8s"
540+
541+
client, err := mongo.Connect(options.Client().ApplyURI(uri))
542+
if err != nil {
543+
panic(err)
544+
}
545+
481546
Additional Information
482547
----------------------
483548

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"go.mongodb.org/mongo-driver/v2/mongo"
5+
"go.mongodb.org/mongo-driver/v2/mongo/options"
6+
)
7+
8+
func main() {
9+
// start-kubernetes
10+
uri := "mongodb://<hostname>:<port>"
11+
props := map[string]string{
12+
"ENVIRONMENT": "k8s",
13+
}
14+
opts := options.Client().ApplyURI(uri)
15+
opts.SetAuth(
16+
options.Credential{
17+
Username: null,
18+
AuthMechanism: "MONGODB-OIDC",
19+
AuthMechanismProperties: props,
20+
},
21+
)
22+
client, err := mongo.Connect(opts)
23+
if err != nil {
24+
panic(err)
25+
}
26+
// end-kubernetes
27+
}

0 commit comments

Comments
 (0)