Skip to content

Commit 93c7361

Browse files
committed
enterprise auth ex
1 parent 3adcdb7 commit 93c7361

File tree

4 files changed

+212
-30
lines changed

4 files changed

+212
-30
lines changed

snooty.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
2424
driver-long = "MongoDB Kotlin Sync Driver"
2525
driver-short = "Kotlin Sync driver"
2626
language = "Kotlin"
27-
version-number = "5.1"
28-
full-version = "{+version-number+}.2"
27+
version-number = "5.2"
28+
full-version = "{+version-number+}.0"
2929
version = "v{+version-number+}"
3030
mdb-server = "MongoDB Server"
3131
stable-api = "Stable API"

source/includes/security/authentication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ val credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKe
160160

161161
val settings = MongoClientSettings.builder()
162162
.applyToClusterSettings { builder ->
163-
builder.hosts(listOf(ServerAddress("<hostname>", "<port>")))
163+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
164164
}
165165
.credential(credential)
166166
.build()
@@ -193,7 +193,7 @@ val credential = MongoCredential.createMongoX509Credential()
193193
val settings = MongoClientSettings.builder()
194194
.applyToClusterSettings { builder ->
195195
builder.hosts(listOf(
196-
ServerAddress("<hostname>", "<port>"))
196+
ServerAddress("<hostname>", <port>))
197197
)
198198
}
199199
.applyToSslSettings { builder ->

source/includes/security/enterprise-auth.kt

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,145 @@ import com.mongodb.kotlin.client.MongoClient
33
import org.bson.BsonInt64
44
import org.bson.Document
55

6+
// GSSAPI
7+
8+
// start-gssapi-connect-string
9+
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI")
10+
val mongoClient = MongoClient.create(connectionString)
11+
// end-gssapi-connect-string
12+
13+
// start-gssapi-mongo-cred
14+
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
15+
16+
val settings = MongoClientSettings.builder()
17+
.applyToClusterSettings { builder ->
18+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
19+
}
20+
.credential(credential)
21+
.build()
22+
23+
val mongoClient = MongoClient.create(settings)
24+
// end-gssapi-mongo-cred
25+
26+
// start-gssapi-properties-connect-string
27+
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService")
28+
val mongoClient = MongoClient.create(connectionString)
29+
// end-gssapi-properties-connect-string
30+
31+
// start-gssapi-service-name-key
32+
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
33+
.withMechanismProperty(MongoCredential.SERVICE_NAME_KEY, "myService")
34+
// end-gssapi-service-name-key
35+
36+
// start-gssapi-java-subject-key
37+
val loginContext = LoginContext("<LoginModule implementation from JAAS config>")
38+
loginContext.login()
39+
val subject: Subject = loginContext.subject
40+
41+
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
42+
.withMechanismProperty(MongoCredential.JAVA_SUBJECT_KEY, subject)
43+
// end-gssapi-java-subject-key
44+
45+
// start-gssapi-java-subject-provider
46+
/* All MongoClient instances sharing this instance of KerberosSubjectProvider
47+
will share a Kerberos ticket cache */
48+
val myLoginContext = "myContext"
49+
/* Login context defaults to "com.sun.security.jgss.krb5.initiate"
50+
if unspecified in KerberosSubjectProvider */
51+
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
52+
.withMechanismProperty(
53+
MongoCredential.JAVA_SUBJECT_PROVIDER_KEY,
54+
KerberosSubjectProvider(myLoginContext)
55+
)
56+
// end-gssapi-java-subject-provider
57+
58+
// LDAP
59+
60+
// start-ldap-connect-string
61+
val connectionString = ConnectionString("<LDAP username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
62+
val mongoClient = MongoClient.create(connectionString)
63+
// end-ldap-connect-string
64+
65+
// start-ldap-mongo-cred
66+
val credential = MongoCredential.createPlainCredential("<LDAP username>", "$external", "<password>".toCharArray())
67+
68+
val settings = MongoClientSettings.builder()
69+
.applyToClusterSettings { builder ->
70+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
71+
}
72+
.credential(credential)
73+
.build()
74+
75+
val mongoClient = MongoClient.create(settings)
76+
// end-ldap-mongo-cred
77+
78+
// OIDC
79+
80+
// start-oidc-azure-connect-str
81+
val connectionString = ConnectionString(
82+
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
83+
"?authMechanism=MONGODB-OIDC" +
84+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
85+
val mongoClient = MongoClient.create(connectionString)
86+
// end-oidc-azure-connect-str
87+
88+
// start-oidc-azure-mongo-cred
89+
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
90+
.withMechanismProperty("ENVIRONMENT", "azure")
91+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
92+
93+
val mongoClient = MongoClient.create(
94+
MongoClientSettings.builder()
95+
.applyToClusterSettings { builder ->
96+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
97+
}
98+
.credential(credential)
99+
.build())
100+
// end-oidc-azure-mongo-cred
101+
102+
// start-oidc-gcp-connect-str
103+
val connectionString = ConnectionString(
104+
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
105+
"authMechanism=MONGODB-OIDC" +
106+
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
107+
val mongoClient = MongoClient.create(connectionString)
108+
// end-oidc-gcp-connect-str
109+
110+
// start-oidc-gcp-mongo-cred
111+
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
112+
.withMechanismProperty("ENVIRONMENT", "gcp")
113+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
114+
115+
val mongoClient = MongoClient.create(
116+
MongoClientSettings.builder()
117+
.applyToClusterSettings { builder ->
118+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
119+
}
120+
.credential(credential)
121+
.build())
122+
// end-oidc-gcp-mongo-cred
123+
124+
// start-oidc-custom-callback
125+
val credential = MongoCredential.createOidcCredential(null)
126+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
127+
val accessToken = "..."
128+
OidcCallbackResult(accessToken)
129+
}
130+
// end-oidc-custom-callback
131+
132+
// start-oidc-custom-callback-ex
133+
val credential = MongoCredential.createOidcCredential(null)
134+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
135+
val accessToken = String(Files.readAllBytes(Paths.get("access-token.dat")))
136+
OidcCallbackResult(accessToken)
137+
}
138+
139+
val mongoClient = MongoClient.create(
140+
MongoClientSettings.builder()
141+
.applyToClusterSettings { builder ->
142+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
143+
}
144+
.credential(credential)
145+
.build()
146+
)
147+
// end-oidc-custom-callback-ex

source/security/enterprise-auth.txt

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ Enterprise Edition:
3232
- :ref:`LDAP (PLAIN) <plain-auth-mechanism>`
3333
- :ref:`MONGODB-OIDC <kotlin-oidc>`
3434

35-
For more
36-
information on establishing a connection to your MongoDB cluster, read our
37-
:doc:`Connection Guide </fundamentals/connection>`.
35+
For more information on establishing a connection to your MongoDB cluster, read our
36+
:ref:`Connection Guide <kotlin-sync-connect>`.
3837

3938

4039
Specify an Authentication Mechanism
@@ -67,10 +66,10 @@ authenticating using a ``MongoCredential``.
6766
For more information on these classes and methods, refer to the following API
6867
documentation:
6968

70-
- `MongoClient.create() <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-client/-factory/create.html>`__
71-
- `MongoClient <{+api+}/apidocs/mongodb-driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-client/index.html>`__
72-
- `MongoClientSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__
73-
- `MongoCredential <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html>`__
69+
- `MongoClient.create() <{+api+}/com.mongodb.kotlin.client/-mongo-client/-factory/index.html>`__
70+
- `MongoClient <{+api+}/com.mongodb.kotlin.client/-mongo-client/index.html>`__
71+
- `MongoClientSettings.Builder <{+java-api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__
72+
- `MongoCredential <{+java-api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html>`__
7473

7574
Mechanisms
7675
----------
@@ -120,8 +119,11 @@ mechanism:
120119

121120
Your code to instantiate a ``MongoClient`` should resemble the following:
122121

123-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.gssapi-connection-string.kt
122+
.. literalinclude:: /includes/security/enterprise-auth.kt
124123
:language: kotlin
124+
:dedent:
125+
:start-after: start-gssapi-connect-string
126+
:end-before: end-gssapi-connect-string
125127

126128
.. tab::
127129
:tabid: MongoCredential
@@ -130,8 +132,11 @@ mechanism:
130132
``MongoCredential`` class, use the ``createGSSAPICredential()``
131133
method. Your code to instantiate a ``MongoClient`` should resemble the following:
132134

133-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.auth-creds-gssapi.kt
135+
.. literalinclude:: /includes/security/enterprise-auth.kt
134136
:language: kotlin
137+
:dedent:
138+
:start-after: start-gssapi-mongo-cred
139+
:end-before: end-gssapi-mongo-cred
135140

136141
In order to acquire a
137142
`Kerberos ticket <https://docs.oracle.com/en/java/javase/11/docs/api/java.security.jgss/javax/security/auth/kerberos/KerberosTicket.html>`__,
@@ -177,8 +182,11 @@ You may need to specify one or more of the following additional
177182
Your code to instantiate a ``MongoClient`` using GSSAPI and additional
178183
properties might resemble the following:
179184

180-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.gssapi-properties-connection-string.kt
181-
:language: kotlin
185+
.. literalinclude:: /includes/security/enterprise-auth.kt
186+
:language: kotlin
187+
:dedent:
188+
:start-after: start-gssapi-properties-connect-string
189+
:end-before: end-gssapi-properties-connect-string
182190

183191
.. tab::
184192
:tabid: MongoCredential
@@ -203,14 +211,20 @@ You may need to specify one or more of the following additional
203211
.. tab::
204212
:tabid: SERVICE_NAME_KEY
205213

206-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.service-name-key.kt
207-
:language: kotlin
214+
.. literalinclude:: /includes/security/enterprise-auth.kt
215+
:language: kotlin
216+
:dedent:
217+
:start-after: start-gssapi-service-name-key
218+
:end-before: end-gssapi-service-name-key
208219

209220
.. tab::
210221
:tabid: JAVA_SUBJECT_KEY
211222

212-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.java-subject-key.kt
213-
:language: kotlin
223+
.. literalinclude:: /includes/security/enterprise-auth.kt
224+
:language: kotlin
225+
:dedent:
226+
:start-after: start-gssapi-java-subject-key
227+
:end-before: end-gssapi-java-subject-key
214228

215229
By default, the Kotlin driver caches Kerberos tickets by ``MongoClient`` instance.
216230
If your deployment needs to frequently create and destroy ``MongoClient`` instances,
@@ -237,8 +251,11 @@ to improve performance.
237251
in your ``MongoCredential`` instance. The code to configure the Kotlin driver to cache Kerberos tickets
238252
by process should resemble the following:
239253

240-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.kerberos-subject-provider.kt
241-
:language: kotlin
254+
.. literalinclude:: /includes/security/enterprise-auth.kt
255+
:language: kotlin
256+
:dedent:
257+
:start-after: start-gssapi-java-subject-provider
258+
:end-before: end-gssapi-java-subject-provider
242259

243260
.. note::
244261

@@ -252,7 +269,6 @@ to improve performance.
252269
- `JDK-6722928 <https://bugs.openjdk.java.net/browse/JDK-6722928>`__
253270
- `SO 23427343 <https://stackoverflow.com/questions/23427343/cannot-retrieve-tgt-despite-allowtgtsessionkey-registry-entry>`__
254271

255-
256272
.. _plain-auth-mechanism:
257273

258274
LDAP (PLAIN)
@@ -303,8 +319,11 @@ mechanism:
303319

304320
Your code to instantiate a ``MongoClient`` should resemble the following:
305321

306-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.ldap-connection-string.kt
322+
.. literalinclude:: /includes/security/enterprise-auth.kt
307323
:language: kotlin
324+
:dedent:
325+
:start-after: start-ldap-connect-string
326+
:end-before: end-ldap-connect-string
308327

309328
.. tab::
310329
:tabid: MongoCredential
@@ -313,8 +332,11 @@ mechanism:
313332
``MongoCredential`` class, use the ``createPlainCredential()``
314333
method. Your code to instantiate a ``MongoClient`` should resemble the following:
315334

316-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.ldap-mongo-credential.kt
335+
.. literalinclude:: /includes/security/enterprise-auth.kt
317336
:language: kotlin
337+
:dedent:
338+
:start-after: start-ldap-mongo-cred
339+
:end-before: end-ldap-mongo-cred
318340

319341
.. _kotlin-oidc:
320342

@@ -366,8 +388,11 @@ see the corresponding syntax.
366388
You must specify values that contain commas in a ``MongoCredential`` instance, as
367389
demonstrated in the :guilabel:`MongoCredential` tab.
368390

369-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-azure-connection-string.kt
391+
.. literalinclude:: /includes/security/enterprise-auth.kt
370392
:language: kotlin
393+
:dedent:
394+
:start-after: start-oidc-azure-connect-str
395+
:end-before: end-oidc-azure-connect-str
371396

372397
.. tab:: MongoCredential
373398
:tabid: mongodb-azure-mongo-credential
@@ -377,8 +402,11 @@ see the corresponding syntax.
377402
placeholder with the value of the
378403
``audience`` server parameter configured on your MongoDB deployment.
379404

380-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-azure-credential.kt
405+
.. literalinclude:: /includes/security/enterprise-auth.kt
381406
:language: kotlin
407+
:dedent:
408+
:start-after: start-oidc-azure-mongo-cred
409+
:end-before: end-oidc-azure-mongo-cred
382410

383411
.. _kotlin-mongodb-oidc-gcp-imds:
384412

@@ -412,17 +440,23 @@ see the corresponding syntax.
412440
You must specify values that contain commas in a ``MongoCredential`` instance, as
413441
demonstrated in the :guilabel:`MongoCredential` tab.
414442

415-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-gcp-connection-string.kt
443+
.. literalinclude:: /includes/security/enterprise-auth.kt
416444
:language: kotlin
445+
:dedent:
446+
:start-after: start-oidc-gcp-connect-str
447+
:end-before: end-oidc-gcp-connect-str
417448

418449
.. tab:: MongoCredential
419450
:tabid: mongodb-gcp-mongo-credential
420451

421452
Replace the ``<audience>`` placeholder with the value of the
422453
``audience`` server parameter configured on your MongoDB deployment.
423454

424-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-gcp-credential.kt
455+
.. literalinclude:: /includes/security/enterprise-auth.kt
425456
:language: kotlin
457+
:dedent:
458+
:start-after: start-oidc-gcp-mongo-cred
459+
:end-before: end-oidc-gcp-mongo-cred
426460

427461
Custom Callback
428462
+++++++++++++++
@@ -433,8 +467,11 @@ must define a custom callback to use OIDC to authenticate from these platforms.
433467
To do so, use the ``"OIDC_CALLBACK"`` authentication property, as shown in the following
434468
code example:
435469

436-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-callback.kt
470+
.. literalinclude:: /includes/security/enterprise-auth.kt
437471
:language: kotlin
472+
:dedent:
473+
:start-after: start-oidc-custom-callback
474+
:end-before: end-oidc-custom-callback
438475

439476
The value of the ``"OIDC_CALLBACK"`` property must be a lambda or other implementation
440477
of the ``OidcCallback`` functional interface that accepts an ``OidcCallbackContext``
@@ -443,5 +480,8 @@ as a parameter and returns an ``OidcCallbackResult``.
443480
The following example uses an example callback to retrieve an OIDC token from a file
444481
named ``"access-token.dat"`` in the local file system:
445482

446-
.. literalinclude:: /examples/generated/EnterpriseAuthTest.snippet.oidc-callback-file.kt
483+
.. literalinclude:: /includes/security/enterprise-auth.kt
447484
:language: kotlin
485+
:dedent:
486+
:start-after: start-oidc-custom-callback-ex
487+
:end-before: end-oidc-custom-callback-ex

0 commit comments

Comments
 (0)