Skip to content

Commit 48a216c

Browse files
authored
aws credential refreshing (#171)
* credential refreshing * highlight correct line * nits * addressing nits * nits * nits * line highlighting
1 parent 2461239 commit 48a216c

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

source/fundamentals/auth.txt

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,33 @@ connect using ``MONGODB-CR``.
228228
~~~~~~~~~~~~~~~
229229

230230
.. 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.
233234

234235
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
235236
Identity and Access Management (AWS IAM) credentials to authenticate your
236237
user.
237238

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+
238252
The following code snippets show how to specify the authentication mechanism,
239253
using the following placeholders:
240254

241255
* ``username`` - value of your ``AWS_ACCESS_KEY_ID``.
242256
* ``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.
248258
* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
249259

250260
Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
@@ -263,7 +273,7 @@ mechanism:
263273

264274
.. code-block:: java
265275

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");
267277

268278
If you need to specify an AWS session token, include it in the
269279
``authMechanismProperties`` parameter as follows using the format
@@ -272,7 +282,7 @@ mechanism:
272282

273283
.. code-block:: java
274284

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>");
276286

277287

278288
.. tab::
@@ -291,10 +301,10 @@ mechanism:
291301
- **Specify your AWS session token in a connection string.**
292302

293303
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
296306
``authMechanismProperties`` parameter. Then, add it to your
297-
``MongoClientSettings`` by calling the
307+
``MongoClientSettings`` by calling the
298308
`applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
299309
method as follows:
300310

@@ -318,6 +328,31 @@ mechanism:
318328
automatically picked up by your MongoClient when you specify the
319329
``MONGODB-AWS`` authentication mechanism.
320330

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+
321356
.. _x509-auth-mechanism:
322357

323358
``X.509``

source/includes/fundamentals/code-snippets/auth-credentials-aws.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
MongoClient mongoClient = MongoClients.create(
66
MongoClientSettings.builder()
77
.applyToClusterSettings(builder ->
8-
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", "<port>"))))
8+
builder.hosts(Arrays.asList(new ServerAddress("<atlasUri>"))))
99
.credential(credential)
1010
.build());
1111

0 commit comments

Comments
 (0)