Skip to content

Commit 01d8d97

Browse files
author
Chris Cho
authored
DOCSP-25962: MONGODB-AWS aws sdk info (#298)
* DOCSP-25962: AWS SDK for MONGODB-AWS auth
1 parent 013a7b5 commit 01d8d97

File tree

2 files changed

+240
-108
lines changed

2 files changed

+240
-108
lines changed

source/fundamentals/auth.txt

Lines changed: 207 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ mechanism:
209209

210210
.. include:: /includes/fundamentals/code-snippets/auth-credentials-sha1.rst
211211

212-
213212
.. _mongodb-cr-auth-mechanism:
214213

215214
``MONGODB-CR``
@@ -229,145 +228,253 @@ connect using ``MONGODB-CR``.
229228
``MONGODB-AWS``
230229
~~~~~~~~~~~~~~~
231230

231+
..
232+
The MONGODB-AWS section structure was updated for v4.8 of the driver. Avoid
233+
backporting any documentation that might not apply to a prior version.
234+
232235
.. note::
233236

234-
The MONGODB-AWS authentication mechanism is available in MongoDB
235-
Atlas.
237+
The MONGODB-AWS authentication mechanism is available for MongoDB
238+
deployments on MongoDB Atlas.
236239

237240
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
238241
Identity and Access Management (AWS IAM) credentials to authenticate your
239-
user.
242+
user. To learn more about configuring MongoDB Atlas, see the
243+
:atlas:`Set Up Passwordless Authentication with AWS IAM Roles </security/passwordless-authentication/#set-up-passwordless-authentication-with-aws-iam-roles>`
244+
guide.
240245

241-
You can store your AWS credentials as environment variables, or insert
242-
them inline like the examples below. The driver checks for your credentials
243-
in the following order:
246+
To instruct the driver to use this authentication mechanism, you can specify
247+
``MONGODB-AWS`` either as a parameter in the connection string or by using
248+
the ``MongoCredential.createAwsCredential()`` factory method.
244249

245-
1. Supplied values in a ``MongoCredential`` object or the provided connection string.
246-
2. Your environment variables. (``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``,
247-
and optionally ``AWS_SESSION_TOKEN``)
248-
3. The AWS EC2 endpoint specified in the ``AWS_CONTAINER_CREDENTIALS_RELATIVE_URI``
249-
environment variable.
250-
4. The default AWS EC2 endpoint. For more information, see `IAM Roles for Tasks
251-
<https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`__
250+
Learn how to specify this authentication mechanism and the various ways to
251+
provide your AWS IAM credentials in the next sections.
252252

253+
These sections contain code examples that use the following placeholders:
253254

254-
The following code snippets show how to specify the authentication mechanism,
255-
using the following placeholders:
255+
* ``awsKeyId`` - value of your AWS access key ID
256+
* ``awsSecretKey`` - value of your AWS secret access key
257+
* ``atlasUri`` - network address of your MongoDB Atlas deployment
258+
* ``hostname`` - hostname of your MongoDB Atlas deployment
259+
* ``port`` - port of your MongoDB Atlas deployment
260+
* ``awsSessionToken`` - value of your AWS session token
256261

257-
* ``awsKeyId`` - value of your ``AWS_ACCESS_KEY_ID``.
258-
* ``awsSecretKey`` - value of your ``AWS_SECRET_ACCESS_KEY``.
259-
* ``atlasUri`` - network address of your MongoDB Atlas instance.
260-
* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
262+
.. _java-mongodb-aws-sdk:
261263

262-
.. important:: URL-encode Your Credentials
264+
AWS SDK for Java
265+
++++++++++++++++
263266

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``:
267+
.. versionadded:: v4.8
268268

269-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
270-
:language: java
271-
:dedent:
272-
:start-after: start urlEncode
273-
:end-before: end urlEncode
269+
You can use one of the AWS SDK for Java v1 or v2 to specify your credentials.
270+
This method offers the following features:
274271

275-
Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
276-
tab below for instructions and sample code for specifying this authentication
277-
mechanism:
272+
- Multiple options for obtaining credentials
273+
- Credential caching which helps your application avoid rate limiting
274+
- Credential provider management for use with the `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
278275

279-
.. tabs::
276+
To use the AWS SDK for Java for ``MONGODB-AWS`` authentication, you must
277+
perform the following:
280278

281-
.. tab::
282-
:tabid: Connection String
279+
1. Specify the authentication mechanism
280+
#. Add the SDK as a dependency to your project
281+
#. Supply your credentials using one of the methods in the credential
282+
provider chain
283283

284-
To specify the ``MONGODB-AWS`` authentication mechanism using a
285-
connection string, assign the ``authMechanism`` parameter the value
286-
``"MONGODB-AWS"`` in your connection string. Your code to instantiate
287-
a ``MongoClient`` should look something like this:
284+
.. important::
288285

289-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
290-
:language: java
291-
:dedent:
292-
:start-after: start connectionString
293-
:end-before: end connectionString
286+
This method of providing ``MONGODB-AWS`` credentials is available
287+
only in the {+driver-long+} v4.8 and later.
294288

295-
If you need to specify an AWS session token, include it in the
296-
``authMechanismProperties`` parameter as follows using the format
297-
``AWS_SESSION_TOKEN:<awsSessionToken>``. Your code to instantiate
298-
a ``MongoClient`` with a session token should look something like this:
289+
To specify the authentication mechanism by using a ``MongoCredential``,
290+
use the ``MongoCredential.createAwsCredential()`` factory method
291+
and add the ``MongoCredential`` instance to your ``MongoClient`` as shown
292+
in the following example:
299293

300-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
301-
:language: java
302-
:dedent:
303-
:start-after: start connectionStringSessionToken
304-
:end-before: end connectionStringSessionToken
294+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
295+
:language: java
296+
:dedent:
297+
:start-after: start mechOnlyMongoCredential
298+
:end-before: end mechOnlyMongoCredential
299+
:emphasize-lines: 1,7
305300

306-
.. tab::
307-
:tabid: MongoCredential
301+
To specify the authentication mechanism in the connection string, add
302+
it as a parameter as shown in the following example:
308303

309-
To specify the ``MONGODB-AWS`` authentication mechanism using the
310-
``MongoCredential`` class, use the
311-
`createAwsCredential() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#createAwsCredential(java.lang.String,char%5B%5D)>`__
312-
method. Your code to instantiate a ``MongoClient`` should look something like this:
304+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
305+
:language: java
306+
:dedent:
307+
:start-after: start mechOnlyConnectionString
308+
:end-before: end mechOnlyConnectionString
309+
310+
To add the AWS SDK as a dependency to your project, see the following
311+
AWS documentation for the version you need:
312+
313+
- For the **AWS SDK for Java v2**, see the `Setting Up <https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html>`__
314+
guide.
315+
- For the **AWS SDK for Java v1**, see the `Getting Started <https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/getting-started.html>`__
316+
guide.
317+
318+
.. note::
319+
320+
For the AWS SDK for Java v2, the Java driver currently tests using the
321+
``software.amazon.awssdk:auth:2.18.9`` dependency.
322+
323+
For the AWS SDK for Java v1, the Java driver currently tests using the
324+
``com.amazonaws:aws-java-sdk-core:1.12.337`` dependency.
325+
326+
To supply your credentials, see the following AWS documentation for the
327+
version you need:
328+
329+
- To learn more about the **AWS SDK for Java v2** class the driver uses to
330+
get the credentials, see the `DefaultCredentialsProvider <https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/auth/credentials/DefaultCredentialsProvider.html>`__
331+
API documentation.
332+
333+
Learn how to supply your credentials to this class from the
334+
`Use the default credential provider chain <https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html#credentials-chain>`__
335+
section.
336+
337+
- To learn more about the **AWS SDK for Java v1** class the driver uses to
338+
get the credentials, see the `DefaultAWSCredentialsProviderChain <https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html>`__
339+
API documentation.
340+
341+
Learn how to supply your credentials to this class from the
342+
`Using the Default Credential Provider Chain <https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default>`__
343+
section.
344+
345+
.. note::
313346

314-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
315-
:language: java
316-
:dedent:
317-
:start-after: start mongoCredential
318-
:end-before: end mongoCredential
347+
If you include both v1 and v2 of the AWS SDK for Java in your project,
348+
you must use the v2 methods to supply your credentials.
319349

320-
If you need to specify an AWS session token, you can add it using
321-
one of the following choices:
350+
.. _java-mongodb-aws-env-variables:
322351

323-
- **Specify your AWS session token in a connection string.**
352+
Specify Your Credentials in the Environment
353+
+++++++++++++++++++++++++++++++++++++++++++
324354

325-
If you prefer to pass the AWS session token in the connection string
326-
alongside your ``MongoCredential``, specify your authentication mechanism
327-
in the ``authMechanism`` parameter and your session token in the
328-
``authMechanismProperties`` parameter. Then, add it to your
329-
``MongoClientSettings`` by calling the
330-
`applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
331-
method as follows:
355+
You can provide your AWS IAM credentials by instructing the driver to
356+
use the ``MONGODB-AWS`` authentication mechanism and by setting the
357+
appropriate environment variables.
332358

333-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
334-
:language: java
335-
:dedent:
336-
:start-after: start mongoCredentialSessionTokenConnString
337-
:end-before: end mongoCredentialSessionTokenConnString
359+
To use the environment variables to supply your credentials, you must perform
360+
the following:
338361

339-
- **Specify your AWS session token in a MongoCredential.**
362+
1. Specify the authentication mechanism
363+
#. Add the appropriate environment variables
340364

341-
You can include your AWS session token in your ``MongoCredential``
342-
instance by specifying it in a call to the
343-
`withMechanismProperty() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#withMechanismProperty(java.lang.String,T)>`__
344-
method as shown below:
365+
You can specify the authentication mechanism by using a ``MongoCredential``
366+
or on the connection string.
345367

346-
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
347-
:language: java
348-
:dedent:
349-
:start-after: start mongoCredentialSessionTokenCredential
350-
:end-before: end mongoCredentialSessionTokenCredential
368+
To specify the authentication mechanism by using a ``MongoCredential``,
369+
use the ``MongoCredential.createAwsCredential()`` factory method and add the
370+
``MongoCredential`` instance to your ``MongoClient`` as shown in the following
371+
example:
351372

352-
- **Specify your AWS session token in an environment variable.**
373+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
374+
:language: java
375+
:dedent:
376+
:start-after: start mechOnlyMongoCredential
377+
:end-before: end mechOnlyMongoCredential
378+
:emphasize-lines: 1,7
379+
380+
To specify the authentication mechanism in the connection string, add it as a
381+
parameter as shown in the following example:
382+
383+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
384+
:language: java
385+
:dedent:
386+
:start-after: start mechOnlyConnectionString
387+
:end-before: end mechOnlyConnectionString
388+
389+
The next examples show how to provide your credentials by setting environment
390+
variables for the following types of authentication:
391+
392+
- Programmatic access keys
393+
- ECS container credentials
394+
- EC2 container credentials
395+
396+
The following examples shows how you can set your **programmatic access keys**
397+
in environment variables by using ``bash`` or a similar shell:
398+
399+
.. code-block:: bash
400+
401+
export AWS_ACCESS_KEY_ID=<awsKeyId>
402+
export AWS_SECRET_ACCESS_KEY=<awsSecretKey>
403+
export AWS_SESSION_TOKEN=<awsSessionToken>
404+
405+
Omit the line containing ``AWS_SESSION_TOKEN`` if you don't need an AWS
406+
session token for that role.
407+
408+
To authenticate by using **ECS container credentials**, set the ECS
409+
endpoint relative URI in an environment variable by using ``bash`` or
410+
a similar shell as shown in the following example:
411+
412+
.. code-block:: bash
413+
414+
export AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=<your ECS endpoint>
415+
416+
To authenticate using **EC2 container credentials**, make sure none of the
417+
aforementioned environment variables are set. The driver obtains the
418+
credentials from the default IPv4 EC2 instance metadata endpoint.
353419

354-
In your client execution environment, set an environment variable
355-
called ``AWS_SESSION_TOKEN`` and assign your token to it. The value is
356-
automatically picked up by your ``MongoClient`` when you specify the
357-
``MONGODB-AWS`` authentication mechanism.
420+
.. _java-mongodb-aws-mongoclient-configuration:
358421

359-
Refresh Credentials
360-
+++++++++++++++++++
422+
Specify Your Credentials in a MongoCredential
423+
+++++++++++++++++++++++++++++++++++++++++++++
361424

362-
The driver supports refreshing credentials for cases such as assuming roles
363-
or using `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
425+
You can supply your AWS IAM credentials to a ``MongoClient`` by using a
426+
a ``MongoCredential`` instance. To construct the ``MongoCredential`` instance
427+
for ``MONGODB-AWS`` authentication, use the `createAwsCredential() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#createAwsCredential(java.lang.String,char%5B%5D)>`__
428+
factory method.
429+
430+
You can supply only programmatic access keys to the
431+
``MongoCredential.createAwsCredential()`` method. If you need to supply ECS
432+
or EC2 container credentials, use the instructions in
433+
:ref:`<java-mongodb-aws-env-variables>` or :ref:`<java-mongodb-aws-sdk>`.
434+
435+
To use the the ``MongoCredential`` for ``MONGODB-AWS`` authentication, you
436+
must perform the following:
437+
438+
1. Specify the authentication mechanism
439+
#. Supply the credentials
440+
441+
To specify the authentication mechanism by using a ``MongoCredential``,
442+
use the ``MongoCredential.createAwsCredential()`` factory method
443+
and add the ``MongoCredential`` instance to your ``MongoClient`` as shown
444+
in the following example:
445+
446+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
447+
:language: java
448+
:dedent:
449+
:start-after: start mongoCredential
450+
:end-before: end mongoCredential
451+
:emphasize-lines: 1
452+
453+
If you need to specify an AWS session token, pass it to the
454+
`withMechanismProperty() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#withMechanismProperty(java.lang.String,T)>`__
455+
method as shown in the following example:
456+
457+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
458+
:language: java
459+
:dedent:
460+
:start-after: start mongoCredentialSessionTokenCredential
461+
:end-before: end mongoCredentialSessionTokenCredential
462+
:emphasize-lines: 1,7
463+
464+
To refresh your credentials, you can declare a ``Supplier`` lambda expression
465+
that returns new credentials as shown in the following example:
364466

365467
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
366468
:language: java
367469
:dedent:
368470
:start-after: start refreshCredentials
369471
:end-before: end refreshCredentials
370-
:emphasize-lines: 4-5, 9
472+
:emphasize-lines: 4-5,9
473+
474+
.. note::
475+
476+
If you must provide AWS IAM credentials in a connection string, refer to
477+
a previous release of the `MONGODB-AWS driver documentation <https://www.mongodb.com/docs/drivers/java/sync/v4.7/fundamentals/auth/#mongodb-aws>`__.
371478

372479
.. _x509-auth-mechanism:
373480

0 commit comments

Comments
 (0)