@@ -254,11 +254,24 @@ in the following order:
254
254
The following code snippets show how to specify the authentication mechanism,
255
255
using the following placeholders:
256
256
257
- * ``username `` - value of your ``AWS_ACCESS_KEY_ID``.
258
- * ``password `` - value of your ``AWS_SECRET_ACCESS_KEY``.
257
+ * ``awsKeyId `` - value of your ``AWS_ACCESS_KEY_ID``.
258
+ * ``awsSecretKey `` - value of your ``AWS_SECRET_ACCESS_KEY``.
259
259
* ``atlasUri`` - network address of your MongoDB Atlas instance.
260
260
* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
261
261
262
+ .. important:: URL-encode Your Credentials
263
+
264
+ Make sure to URL-encode your credentials to prevent backslash or other
265
+ characters from causing parsing errors. The following code example
266
+ shows you how to URL-encode a sample string, represented by the placeholder
267
+ ``fieldValue``:
268
+
269
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
270
+ :language: java
271
+ :dedent:
272
+ :start-after: start urlEncode
273
+ :end-before: end urlEncode
274
+
262
275
Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
263
276
tab below for instructions and sample code for specifying this authentication
264
277
mechanism:
@@ -270,22 +283,25 @@ mechanism:
270
283
271
284
To specify the ``MONGODB-AWS`` authentication mechanism using a
272
285
connection string, assign the ``authMechanism`` parameter the value
273
- ``MONGODB-AWS`` in your connection string. Your code to instantiate
286
+ ``" MONGODB-AWS" `` in your connection string. Your code to instantiate
274
287
a ``MongoClient`` should look something like this:
275
288
276
- .. code-block:: java
277
-
278
- MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>?authMechanism=MONGODB-AWS");
289
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
290
+ :language: java
291
+ :dedent:
292
+ :start-after: start connectionString
293
+ :end-before: end connectionString
279
294
280
295
If you need to specify an AWS session token, include it in the
281
296
``authMechanismProperties`` parameter as follows using the format
282
297
``AWS_SESSION_TOKEN:<awsSessionToken>``. Your code to instantiate
283
298
a ``MongoClient`` with a session token should look something like this:
284
299
285
- .. code-block:: java
286
-
287
- MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
288
-
300
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
301
+ :language: java
302
+ :dedent:
303
+ :start-after: start connectionStringSessionToken
304
+ :end-before: end connectionStringSessionToken
289
305
290
306
.. tab::
291
307
:tabid: MongoCredential
@@ -295,7 +311,11 @@ mechanism:
295
311
`createAwsCredential() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#createAwsCredential(java.lang.String,char%5B%5D)>`__
296
312
method. Your code to instantiate a ``MongoClient`` should look something like this:
297
313
298
- .. include:: /includes/fundamentals/code-snippets/auth-credentials-aws.rst
314
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
315
+ :language: java
316
+ :dedent:
317
+ :start-after: start mongoCredential
318
+ :end-before: end mongoCredential
299
319
300
320
If you need to specify an AWS session token, you can add it using
301
321
one of the following choices:
@@ -310,7 +330,11 @@ mechanism:
310
330
`applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
311
331
method as follows:
312
332
313
- .. include:: /includes/fundamentals/code-snippets/auth-credentials-aws-session.rst
333
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
334
+ :language: java
335
+ :dedent:
336
+ :start-after: start mongoCredentialSessionTokenConnString
337
+ :end-before: end mongoCredentialSessionTokenConnString
314
338
315
339
- **Specify your AWS session token in a MongoCredential.**
316
340
@@ -319,15 +343,17 @@ mechanism:
319
343
`withMechanismProperty() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#withMechanismProperty(java.lang.String,T)>`__
320
344
method as shown below:
321
345
322
- .. code-block:: java
323
-
324
- MongoCredential.createAwsCredential("<username>", "<password>".toCharArray()) .withMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>");
346
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
347
+ :language: java
348
+ :dedent:
349
+ :start-after: start mongoCredentialSessionTokenCredential
350
+ :end-before: end mongoCredentialSessionTokenCredential
325
351
326
352
- **Specify your AWS session token in an environment variable.**
327
353
328
354
In your client execution environment, set an environment variable
329
355
called ``AWS_SESSION_TOKEN`` and assign your token to it. The value is
330
- automatically picked up by your MongoClient when you specify the
356
+ automatically picked up by your `` MongoClient`` when you specify the
331
357
``MONGODB-AWS`` authentication mechanism.
332
358
333
359
Refresh Credentials
@@ -336,24 +362,12 @@ Refresh Credentials
336
362
The driver supports refreshing credentials for cases such as assuming roles
337
363
or using `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
338
364
339
-
340
- .. code-block:: java
341
- :emphasize-lines: 3-4, 8
342
-
343
- Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
344
- // Code to fetch fresh credentials, such as assuming a role using the AWS SDK.
345
- // Ensure you return the temporary credentials.
346
- return new AwsCredential("<accessKeyId>", "<secretAccessKey>", "<sessionToken>");
347
- };
348
-
349
- MongoCredential credential = MongoCredential.createAwsCredential(null, null)
350
- .withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
351
- MongoClient mongoClient = MongoClients.create(
352
- MongoClientSettings.builder()
353
- .applyToClusterSettings(builder ->
354
- builder.hosts(Collections.singletonList(new ServerAddress("<hostname>", 27017))))
355
- .credential(credential)
356
- .build());
365
+ .. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
366
+ :language: java
367
+ :dedent:
368
+ :start-after: start refreshCredentials
369
+ :end-before: end refreshCredentials
370
+ :emphasize-lines: 4-5, 9
357
371
358
372
.. _x509-auth-mechanism:
359
373
0 commit comments