@@ -228,23 +228,33 @@ connect using ``MONGODB-CR``.
228
228
~~~~~~~~~~~~~~~
229
229
230
230
.. note::
231
- The MONGODB-AWS authentication mechanism is only available in MongoDB
232
- versions 4.4 and later.
231
+
232
+ The MONGODB-AWS authentication mechanism is available in MongoDB
233
+ Atlas.
233
234
234
235
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
235
236
Identity and Access Management (AWS IAM) credentials to authenticate your
236
237
user.
237
238
239
+ You can store your AWS credentials as environment variables, or insert
240
+ them inline like the examples below. The driver checks for your credentials
241
+ in the following order:
242
+
243
+ 1. Supplied values in a ``MongoCredential`` object or the provided connection string.
244
+ 2. Your environment variables. (``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``,
245
+ and optionally ``AWS_SESSION_TOKEN``)
246
+ 3. The AWS EC2 endpoint specified in the ``AWS_CONTAINER_CREDENTIALS_RELATIVE_URI``
247
+ environment variable.
248
+ 4. The default AWS EC2 endpoint. For more information, see `IAM Roles for Tasks
249
+ <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`__
250
+
251
+
238
252
The following code snippets show how to specify the authentication mechanism,
239
253
using the following placeholders:
240
254
241
255
* ``username`` - value of your ``AWS_ACCESS_KEY_ID``.
242
256
* ``password`` - value of your ``AWS_SECRET_ACCESS_KEY``.
243
- * ``hostname`` - network address of your MongoDB server, accessible by your client.
244
- * ``port`` - port number of your MongoDB server.
245
- * ``authenticationDb`` - MongoDB database that contains your user's
246
- authentication data. If you omit this parameter, the driver uses the
247
- default value ``admin``.
257
+ * ``atlasUri`` - network address of your MongoDB Atlas instance.
248
258
* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
249
259
250
260
Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
@@ -263,7 +273,7 @@ mechanism:
263
273
264
274
.. code-block:: java
265
275
266
- MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>& authMechanism=MONGODB-AWS");
276
+ MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>? authMechanism=MONGODB-AWS");
267
277
268
278
If you need to specify an AWS session token, include it in the
269
279
``authMechanismProperties`` parameter as follows using the format
@@ -272,7 +282,7 @@ mechanism:
272
282
273
283
.. code-block:: java
274
284
275
- MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>& authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
285
+ MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>? authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
276
286
277
287
278
288
.. tab::
@@ -291,10 +301,10 @@ mechanism:
291
301
- **Specify your AWS session token in a connection string.**
292
302
293
303
If you prefer to pass the AWS session token in the connection string
294
- alongside your MongoCredential specify your authentication mechanism
295
- in the ``authMechanism`` parameter and your session token in the
304
+ alongside your `` MongoCredential``, specify your authentication mechanism
305
+ in the ``authMechanism`` parameter and your session token in the
296
306
``authMechanismProperties`` parameter. Then, add it to your
297
- ``MongoClientSettings`` by calling the
307
+ ``MongoClientSettings`` by calling the
298
308
`applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
299
309
method as follows:
300
310
@@ -318,6 +328,31 @@ mechanism:
318
328
automatically picked up by your MongoClient when you specify the
319
329
``MONGODB-AWS`` authentication mechanism.
320
330
331
+ Refresh Credentials
332
+ +++++++++++++++++++
333
+
334
+ The driver supports refreshing credentials for cases such as assuming roles
335
+ or using `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
336
+
337
+
338
+ .. code-block:: java
339
+ :emphasize-lines: 3-4, 8
340
+
341
+ Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
342
+ // Code to fetch fresh credentials, such as assuming a role using the AWS SDK.
343
+ // Ensure you return the temporary credentials.
344
+ return new AwsCredential("<accessKeyId>", "<secretAccessKey>", "<sessionToken>");
345
+ };
346
+
347
+ MongoCredential credential = MongoCredential.createAwsCredential(null, null)
348
+ .withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
349
+ MongoClient mongoClient = MongoClients.create(
350
+ MongoClientSettings.builder()
351
+ .applyToClusterSettings(builder ->
352
+ builder.hosts(Collections.singletonList(new ServerAddress("<hostname>", 27017))))
353
+ .credential(credential)
354
+ .build());
355
+
321
356
.. _x509-auth-mechanism:
322
357
323
358
``X.509``
0 commit comments