@@ -254,11 +254,24 @@ in the following order:
254254The following code snippets show how to specify the authentication mechanism,
255255using the following placeholders:
256256
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``.
259259* ``atlasUri`` - network address of your MongoDB Atlas instance.
260260* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
261261
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+
262275Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
263276tab below for instructions and sample code for specifying this authentication
264277mechanism:
@@ -270,22 +283,25 @@ mechanism:
270283
271284 To specify the ``MONGODB-AWS`` authentication mechanism using a
272285 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
274287 a ``MongoClient`` should look something like this:
275288
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
279294
280295 If you need to specify an AWS session token, include it in the
281296 ``authMechanismProperties`` parameter as follows using the format
282297 ``AWS_SESSION_TOKEN:<awsSessionToken>``. Your code to instantiate
283298 a ``MongoClient`` with a session token should look something like this:
284299
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
289305
290306 .. tab::
291307 :tabid: MongoCredential
@@ -295,7 +311,11 @@ mechanism:
295311 `createAwsCredential() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#createAwsCredential(java.lang.String,char%5B%5D)>`__
296312 method. Your code to instantiate a ``MongoClient`` should look something like this:
297313
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
299319
300320 If you need to specify an AWS session token, you can add it using
301321 one of the following choices:
@@ -310,7 +330,11 @@ mechanism:
310330 `applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
311331 method as follows:
312332
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
314338
315339 - **Specify your AWS session token in a MongoCredential.**
316340
@@ -319,15 +343,17 @@ mechanism:
319343 `withMechanismProperty() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#withMechanismProperty(java.lang.String,T)>`__
320344 method as shown below:
321345
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
325351
326352 - **Specify your AWS session token in an environment variable.**
327353
328354 In your client execution environment, set an environment variable
329355 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
331357 ``MONGODB-AWS`` authentication mechanism.
332358
333359Refresh Credentials
@@ -336,24 +362,12 @@ Refresh Credentials
336362The driver supports refreshing credentials for cases such as assuming roles
337363or using `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
338364
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
357371
358372.. _x509-auth-mechanism:
359373
0 commit comments