Skip to content

Commit 30fb3cf

Browse files
committed
authentication v1
1 parent 14c04d9 commit 30fb3cf

File tree

3 files changed

+795
-0
lines changed

3 files changed

+795
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import com.mongodb.*
2+
import com.mongodb.kotlin.client.MongoClient
3+
import org.bson.BsonInt64
4+
import org.bson.Document
5+
6+
// SCRAM Authentication
7+
// start-default-cred-string
8+
val mongoClient =
9+
MongoClient.create("mongodb+srv://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>")
10+
// end-default-cred-string
11+
12+
// start-default-mongo-cred
13+
val credential = MongoCredential.createCredential(
14+
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
15+
)
16+
17+
val settings = MongoClientSettings.builder()
18+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
19+
builder.srvHost("<hostname>")
20+
}
21+
.credential(credential)
22+
.applyToSslSettings { builder ->
23+
builder.enabled(true)
24+
}
25+
.build()
26+
27+
val mongoClient = MongoClient.create(settings)
28+
// end-default-mongo-cred
29+
30+
// start-scramsha256-cred-string
31+
val mongoClient =
32+
MongoClient.create("mongodb+srv://<db_username>:<db_password>@<hostname>:<port>/?authSource=admin&authMechanism=SCRAM-SHA-256")
33+
// end-scramsha256-cred-string
34+
35+
// start-scramsha256-mongo-cred
36+
val credential = MongoCredential.createScramSha256Credential(
37+
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
38+
)
39+
40+
val settings = MongoClientSettings.builder()
41+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
42+
builder.srvHost("<hostname>")
43+
}
44+
.applyToSslSettings { builder ->
45+
builder.enabled(true)
46+
}
47+
.credential(credential)
48+
.build()
49+
50+
val mongoClient = MongoClient.create(settings)
51+
// end-scramsha256-mongo-cred
52+
53+
// start-scramsha1-cred-string
54+
val mongoClient =
55+
MongoClient.create("mongodb+srv://<db_username>:<db_password>@<hostname>:<port>/?authSource=admin&authMechanism=SCRAM-SHA-1")
56+
// end-scramsha1-cred-string
57+
58+
// start-scramsha1-mongo-cred
59+
val credential = MongoCredential.createScramSha1Credential(
60+
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
61+
)
62+
63+
val settings = MongoClientSettings.builder()
64+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
65+
builder.srvHost("<hostname>")
66+
}
67+
.applyToSslSettings { builder ->
68+
builder.enabled(true)
69+
}
70+
.credential(credential)
71+
.build()
72+
73+
val mongoClient = MongoClient.create(settings)
74+
// end-scramsha1-mongo-cred
75+
76+
// AWS Authentication
77+
78+
// start-aws-sdk-mcred
79+
val credential = MongoCredential.createAwsCredential(null, null)
80+
81+
val settings = MongoClientSettings.builder()
82+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
83+
builder.hosts(
84+
listOf(ServerAddress("<atlasUri>"))
85+
)
86+
}
87+
.credential(credential)
88+
.build()
89+
90+
val mongoClient = MongoClient.create(settings)
91+
// end-aws-sdk-mcred
92+
93+
// start-aws-sdk-cred-string
94+
val mongoClient =
95+
MongoClient.create("mongodb://<atlasUri>?authMechanism=MONGODB-AWS")
96+
// end-aws-sdk-cred-string
97+
98+
99+
// start-aws-env-mcred
100+
val credential = MongoCredential.createAwsCredential(null, null)
101+
102+
val settings = MongoClientSettings.builder()
103+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
104+
builder.hosts(
105+
listOf(ServerAddress("<atlasUri>"))
106+
)
107+
}
108+
.credential(credential)
109+
.build()
110+
111+
val mongoClient = MongoClient.create(settings)
112+
// end-aws-env-mcred
113+
114+
// start-aws-env-cred-string
115+
val mongoClient =
116+
MongoClient.create("mongodb://<atlasUri>?authMechanism=MONGODB-AWS")
117+
// end-aws-env-cred-string
118+
119+
// start-aws-mcred
120+
val credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray())
121+
122+
val settings = MongoClientSettings.builder()
123+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
124+
builder.hosts(
125+
listOf(ServerAddress("<atlasUri>"))
126+
)
127+
}
128+
.credential(credential)
129+
.build()
130+
131+
val mongoClient = MongoClient.create(settings)
132+
// end-aws-mcred
133+
134+
// start-aws-mcred-wmechprop
135+
val credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray())
136+
.withMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>")
137+
138+
val settings = MongoClientSettings.builder()
139+
.applyToClusterSettings { builder: ClusterSettings.Builder ->
140+
builder.hosts(
141+
listOf(ServerAddress("<atlasUri>"))
142+
)
143+
}
144+
.credential(credential)
145+
.build()
146+
147+
val mongoClient = MongoClient.create(settings)
148+
// end-aws-mcred-wmechprop
149+
150+
// start-aws-lambda-expression
151+
val awsFreshCredentialSupplier: Supplier<AwsCredential> = Supplier {
152+
// Add your code here to fetch new credentials
153+
154+
// Return the new credentials
155+
AwsCredential("<awsKeyId>", "<awsSecretKey>", "<awsSessionToken>")
156+
}
157+
158+
val credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray())
159+
.withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier)
160+
161+
val settings = MongoClientSettings.builder()
162+
.applyToClusterSettings { builder ->
163+
builder.hosts(listOf(ServerAddress("<hostname>", "<port>")))
164+
}
165+
.credential(credential)
166+
.build()
167+
168+
val mongoClient = MongoClient.create(settings)
169+
// end-aws-lambda-expression
170+
171+
// start-aws-apply-connect-string
172+
val credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray())
173+
val connectionString = ConnectionString("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>")
174+
175+
val settings = MongoClientSettings.builder()
176+
.applyConnectionString(connectionString)
177+
.credential(credential)
178+
.build()
179+
180+
val mongoClient = MongoClient.create(settings)
181+
// end-aws-apply-connect-string
182+
183+
// X.509
184+
185+
// start-x509-connect-string
186+
val mongoClient =
187+
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true")
188+
// end-x509-connect-string
189+
190+
// start-x509-mcred
191+
val credential = MongoCredential.createMongoX509Credential()
192+
193+
val settings = MongoClientSettings.builder()
194+
.applyToClusterSettings { builder ->
195+
builder.hosts(listOf(
196+
ServerAddress("<hostname>", "<port>"))
197+
)
198+
}
199+
.applyToSslSettings { builder ->
200+
builder.enabled(true)
201+
}
202+
.credential(credential)
203+
.build()
204+
205+
val mongoClient = MongoClient.create(settings)
206+
// end-x509-mcred

0 commit comments

Comments
 (0)