@@ -3,12 +3,17 @@ import com.mongodb.ConnectionString
3
3
import com.mongodb.KerberosSubjectProvider
4
4
import com.mongodb.MongoClientSettings
5
5
import com.mongodb.MongoCredential
6
+ import com.mongodb.MongoCredential.OidcCallbackResult
6
7
import com.mongodb.ServerAddress
7
8
import com.mongodb.kotlin.client.coroutine.MongoClient
8
9
import kotlinx.coroutines.runBlocking
10
+ import java.nio.file.Files
11
+ import java.nio.file.Paths
12
+ import javax.naming.Context
9
13
import javax.security.auth.Subject
10
14
import javax.security.auth.login.LoginContext
11
15
import kotlin.test.Ignore
16
+
12
17
// :replace-start: {
13
18
// "terms": {
14
19
// "PORT": "<port>",
@@ -32,7 +37,7 @@ internal class EnterpriseAuthTest {
32
37
33
38
fun createGSSAPICred () = runBlocking {
34
39
// :snippet-start: auth-creds-gssapi
35
- val credential = MongoCredential .createGSSAPICredential(" <username >" )
40
+ val credential = MongoCredential .createGSSAPICredential(" <Kerberos principal >" )
36
41
37
42
val settings = MongoClientSettings .builder()
38
43
.applyToClusterSettings { builder ->
@@ -47,7 +52,7 @@ internal class EnterpriseAuthTest {
47
52
48
53
fun serviceNameKey () = runBlocking {
49
54
// :snippet-start: service-name-key
50
- val credential = MongoCredential .createGSSAPICredential(" <username >" )
55
+ val credential = MongoCredential .createGSSAPICredential(" <Kerberos principal >" )
51
56
.withMechanismProperty(MongoCredential .SERVICE_NAME_KEY , " myService" )
52
57
// :snippet-end:
53
58
}
@@ -58,7 +63,7 @@ internal class EnterpriseAuthTest {
58
63
loginContext.login()
59
64
val subject: Subject = loginContext.subject
60
65
61
- val credential = MongoCredential .createGSSAPICredential(" <username >" )
66
+ val credential = MongoCredential .createGSSAPICredential(" <Kerberos principal >" )
62
67
.withMechanismProperty(MongoCredential .JAVA_SUBJECT_KEY , subject)
63
68
// :snippet-end:
64
69
}
@@ -70,7 +75,7 @@ internal class EnterpriseAuthTest {
70
75
val myLoginContext = " myContext"
71
76
/* Login context defaults to "com.sun.security.jgss.krb5.initiate"
72
77
if unspecified in KerberosSubjectProvider */
73
- val credential = MongoCredential .createGSSAPICredential(" <username >" )
78
+ val credential = MongoCredential .createGSSAPICredential(" <Kerberos principal >" )
74
79
.withMechanismProperty(
75
80
MongoCredential .JAVA_SUBJECT_PROVIDER_KEY ,
76
81
KerberosSubjectProvider (myLoginContext)
@@ -80,7 +85,7 @@ internal class EnterpriseAuthTest {
80
85
81
86
fun ldapCredential () = runBlocking {
82
87
// :snippet-start: ldap-mongo-credential
83
- val credential = MongoCredential .createPlainCredential(" <username>" , " $external " , " <password>" .toCharArray())
88
+ val credential = MongoCredential .createPlainCredential(" <LDAP username>" , " $external " , " <password>" .toCharArray())
84
89
85
90
val settings = MongoClientSettings .builder()
86
91
.applyToClusterSettings { builder ->
@@ -95,24 +100,105 @@ internal class EnterpriseAuthTest {
95
100
96
101
fun gssapiConnectionString () = runBlocking {
97
102
// :snippet-start: gssapi-connection-string
98
- val connectionString = ConnectionString (" <username >@<hostname>:<port>/?authSource=$external &authMechanism=GSSAPI" )
103
+ val connectionString = ConnectionString (" <Kerberos principal >@<hostname>:<port>/?authSource=$external &authMechanism=GSSAPI" )
99
104
val mongoClient = MongoClient .create(connectionString)
100
105
// :snippet-end:
101
106
}
102
107
103
108
fun gssapiPropertiesConnectionString () = runBlocking {
104
109
// :snippet-start: gssapi-properties-connection-string
105
- val connectionString = ConnectionString (" <username >@<hostname>:<port>/?authSource=$external &authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService" )
110
+ val connectionString = ConnectionString (" <Kerberos principal >@<hostname>:<port>/?authSource=$external &authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService" )
106
111
val mongoClient = MongoClient .create(connectionString)
107
112
// :snippet-end:
108
113
}
109
114
110
115
fun ldapConnectionString () = runBlocking {
111
116
// :snippet-start: ldap-connection-string
112
- val connectionString = ConnectionString (" <username>:<password>@<hostname>:<port>/?authSource=$external &authMechanism=PLAIN" )
117
+ val connectionString = ConnectionString (" <LDAP username>:<password>@<hostname>:<port>/?authSource=$external &authMechanism=PLAIN" )
118
+ val mongoClient = MongoClient .create(connectionString)
119
+ // :snippet-end:
120
+ }
121
+
122
+ fun oidcAzureConnectionString () = runBlocking {
123
+ // :snippet-start: oidc-azure-connection-string
124
+ val connectionString = ConnectionString (
125
+ " mongodb://<OIDC principal>@<hostname>:<port>/?" +
126
+ " ?authMechanism=MONGODB-OIDC" +
127
+ " &authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>" )
128
+ val mongoClient = MongoClient .create(connectionString)
129
+ // :snippet-end:
130
+ }
131
+
132
+ fun oidcAzureCredential () = runBlocking {
133
+ // :snippet-start: oidc-azure-credential
134
+ val credential = MongoCredential .createOidcCredential(" <OIDC principal>" )
135
+ .withMechanismProperty(" ENVIRONMENT" , " azure" )
136
+ .withMechanismProperty(" TOKEN_RESOURCE" , " <audience>" )
137
+
138
+ val mongoClient = MongoClient .create(
139
+ MongoClientSettings .builder()
140
+ .applyToClusterSettings { builder ->
141
+ builder.hosts(listOf (ServerAddress (" <hostname>" , PORT )))
142
+ }
143
+ .credential(credential)
144
+ .build())
145
+ // :snippet-end:
146
+ }
147
+
148
+ fun oidcGCPConnectionString () = runBlocking {
149
+ // :snippet-start: oidc-gcp-connection-string
150
+ val connectionString = ConnectionString (
151
+ " mongodb://<OIDC principal>@<hostname>:<port>/?" +
152
+ " authMechanism=MONGODB-OIDC" +
153
+ " &authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>" )
113
154
val mongoClient = MongoClient .create(connectionString)
114
155
// :snippet-end:
115
156
}
157
+
158
+ fun oidcGCPCredential () = runBlocking {
159
+ // :snippet-start: oidc-gcp-credential
160
+ val credential = MongoCredential .createOidcCredential(" <OIDC principal>" )
161
+ .withMechanismProperty(" ENVIRONMENT" , " gcp" )
162
+ .withMechanismProperty(" TOKEN_RESOURCE" , " <audience>" )
163
+
164
+ val mongoClient = MongoClient .create(
165
+ MongoClientSettings .builder()
166
+ .applyToClusterSettings { builder ->
167
+ builder.hosts(listOf (ServerAddress (" <hostname>" , PORT )))
168
+ }
169
+ .credential(credential)
170
+ .build())
171
+ // :snippet-end:
172
+ }
173
+
174
+ fun oidcCallback () = runBlocking {
175
+ // :snippet-start: oidc-callback
176
+ val credential = MongoCredential .createOidcCredential(null )
177
+ .withMechanismProperty(" OIDC_CALLBACK" ) { context: Context ->
178
+ val accessToken = " ..."
179
+ OidcCallbackResult (accessToken)
180
+ }
181
+ // :snippet-end:
182
+ }
183
+
184
+ fun oidcCallbackFile () = runBlocking {
185
+ // :snippet-start: oidc-callback-file
186
+ val credential = MongoCredential .createOidcCredential(null )
187
+ .withMechanismProperty(" OIDC_CALLBACK" ) { context: Context ->
188
+ val accessToken = String (Files .readAllBytes(Paths .get(" access-token.dat" )))
189
+ OidcCallbackResult (accessToken)
190
+ }
191
+
192
+ val mongoClient = MongoClient .create(
193
+ MongoClientSettings .builder()
194
+ .applyToClusterSettings { builder ->
195
+ builder.hosts(listOf (ServerAddress (" <hostname>" , PORT )))
196
+ }
197
+ .credential(credential)
198
+ .build()
199
+ )
200
+ // :snippet-end:
201
+ }
116
202
}
117
203
// :replace-end:
118
204
0 commit comments