Skip to content

Commit efd48c9

Browse files
authored
DOCSP-39405: kotlin v5.1 content (#161)
* DOCSP-39405: kotlin v5.1 content * NR small fixes * RL tech review comments
1 parent f78b102 commit efd48c9

12 files changed

+292
-7
lines changed

examples/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
kotlin.code.style=official
2-
kotlin_mongodb_version=4.11.0
2+
kotlin_mongodb_version=5.1.0

examples/src/test/kotlin/EnterpriseAuthTest.kt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import com.mongodb.ConnectionString
33
import com.mongodb.KerberosSubjectProvider
44
import com.mongodb.MongoClientSettings
55
import com.mongodb.MongoCredential
6+
import com.mongodb.MongoCredential.OidcCallbackResult
67
import com.mongodb.ServerAddress
78
import com.mongodb.kotlin.client.coroutine.MongoClient
89
import kotlinx.coroutines.runBlocking
10+
import java.nio.file.Files
11+
import java.nio.file.Paths
12+
import javax.naming.Context
913
import javax.security.auth.Subject
1014
import javax.security.auth.login.LoginContext
1115
import kotlin.test.Ignore
@@ -113,6 +117,87 @@ internal class EnterpriseAuthTest {
113117
val mongoClient = MongoClient.create(connectionString)
114118
// :snippet-end:
115119
}
120+
121+
fun oidcAzureConnectionString() = runBlocking {
122+
// :snippet-start: oidc-azure-connection-string
123+
val connectionString = ConnectionString(
124+
"mongodb://<username>@<hostname>:<port>/?" +
125+
"?authMechanism=MONGODB-OIDC" +
126+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
127+
val mongoClient = MongoClient.create(connectionString)
128+
// :snippet-end:
129+
}
130+
131+
fun oidcAzureCredential() = runBlocking {
132+
// :snippet-start: oidc-azure-credential
133+
val credential = MongoCredential.createOidcCredential("<username>")
134+
.withMechanismProperty("ENVIRONMENT", "azure")
135+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
136+
137+
val mongoClient = MongoClient.create(
138+
MongoClientSettings.builder()
139+
.applyToClusterSettings { builder ->
140+
builder.hosts(listOf(ServerAddress("<hostname>", PORT)))
141+
}
142+
.credential(credential)
143+
.build())
144+
// :snippet-end:
145+
}
146+
147+
fun oidcGCPConnectionString() = runBlocking {
148+
// :snippet-start: oidc-gcp-connection-string
149+
val connectionString = ConnectionString(
150+
"mongodb://<hostname>:<port>/?" +
151+
"authMechanism=MONGODB-OIDC" +
152+
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
153+
val mongoClient = MongoClient.create(connectionString)
154+
// :snippet-end:
155+
}
156+
157+
fun oidcGCPCredential() = runBlocking {
158+
// :snippet-start: oidc-gcp-credential
159+
val credential = MongoCredential.createOidcCredential("<username>")
160+
.withMechanismProperty("ENVIRONMENT", "gcp")
161+
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")
162+
163+
val mongoClient = MongoClient.create(
164+
MongoClientSettings.builder()
165+
.applyToClusterSettings { builder ->
166+
builder.hosts(listOf(ServerAddress("<hostname>", PORT)))
167+
}
168+
.credential(credential)
169+
.build())
170+
// :snippet-end:
171+
}
172+
173+
fun oidcCallback() = runBlocking {
174+
// :snippet-start: oidc-callback
175+
val credential = MongoCredential.createOidcCredential(null)
176+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
177+
val accessToken = "..."
178+
OidcCallbackResult(accessToken)
179+
}
180+
// :snippet-end:
181+
}
182+
183+
fun oidcCallbackFile() = runBlocking {
184+
// :snippet-start: oidc-callback-file
185+
val credential = MongoCredential.createOidcCredential(null)
186+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
187+
val accessToken = String(Files.readAllBytes(Paths.get("access-token.dat")))
188+
OidcCallbackResult(accessToken)
189+
}
190+
191+
val mongoClient = MongoClient.create(
192+
MongoClientSettings.builder()
193+
.applyToClusterSettings { builder ->
194+
builder.hosts(listOf(ServerAddress("<hostname>", PORT)))
195+
}
196+
.credential(credential)
197+
.build()
198+
)
199+
// :snippet-end:
200+
}
116201
}
117202
// :replace-end:
118203

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val connectionString = ConnectionString(
2+
"mongodb://<username>@<hostname>:<port>/?" +
3+
"?authMechanism=MONGODB-OIDC" +
4+
"&authMechanismProperties=ENVIRONMENT:azure,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("<username>")
2+
.withMechanismProperty("ENVIRONMENT", "azure")
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())
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
val credential = MongoCredential.createOidcCredential(null)
2+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
3+
val accessToken = String(Files.readAllBytes(Paths.get("access-token.dat")))
4+
OidcCallbackResult(accessToken)
5+
}
6+
7+
val mongoClient = MongoClient.create(
8+
MongoClientSettings.builder()
9+
.applyToClusterSettings { builder ->
10+
builder.hosts(listOf(ServerAddress("<hostname>", <port>)))
11+
}
12+
.credential(credential)
13+
.build()
14+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val credential = MongoCredential.createOidcCredential(null)
2+
.withMechanismProperty("OIDC_CALLBACK") { context: Context ->
3+
val accessToken = "..."
4+
OidcCallbackResult(accessToken)
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
val connectionString = ConnectionString(
2+
"mongodb://<hostname>:<port>/?" +
3+
"authMechanism=MONGODB-OIDC" +
4+
"&authMechanismProperties=ENVIRONMENT:gcp,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("<username>")
2+
.withMechanismProperty("ENVIRONMENT", "gcp")
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/connection/connection-options.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ parameters of the connection URI to specify the behavior of the client.
261261

262262
| **Default**: ``true``
263263

264+
* - **serverMonitoringMode**
265+
- string
266+
- Specifies which server monitoring protocol the driver uses. When set to
267+
``auto``, the monitoring mode is determined by the environment in which
268+
the driver is running. The driver uses ``poll`` mode in function-as-a-service
269+
(FaaS) environments and ``stream`` mode in other environments.
270+
271+
| **Default**: ``auto``
272+
264273
* - **uuidRepresentation**
265274
- string
266275
- Specifies the UUID representation to use for read and write

source/fundamentals/connection/mongoclientsettings.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ settings to modify the driver's behavior:
433433
* - ``minHeartbeatFrequency()``
434434
- Sets the minimum interval for server monitoring checks.
435435

436+
* - ``serverMonitoringMode()``
437+
- Specifies which server monitoring protocol the driver uses.
438+
436439
Example
437440
~~~~~~~
438441

0 commit comments

Comments
 (0)