Skip to content

Commit 969e9b9

Browse files
authored
DOCSP-46858: Add custom AWS credential documentation (#1160)
1 parent 1751141 commit 969e9b9

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// start-custom-credentials
3+
const { MongoClient } = require('mongodb');
4+
const { fromNodeProviderChain } = require('@aws-sdk/credential-providers');
5+
6+
const client = new MongoClient('<cluster_url>?authMechanism=MONGODB-AWS', {
7+
authMechanismProperties: {
8+
AWS_CREDENTIAL_PROVIDER: fromNodeProviderChain()
9+
}
10+
});
11+
// end-custom-credentials
12+
}
13+
14+
{
15+
// start-custom-credentials-function
16+
const { MongoClient } = require('mongodb');
17+
18+
const client = new MongoClient('<cluster_url>?authMechanism=MONGODB-AWS', {
19+
authMechanismProperties: {
20+
AWS_CREDENTIAL_PROVIDER: async () => {
21+
return {
22+
accessKeyId: process.env.ACCESS_KEY_ID,
23+
secretAccessKey: process.env.SECRET_ACCESS_KEY
24+
}
25+
}
26+
}
27+
});
28+
// end-custom-credentials-function
29+
}

source/security/authentication/aws-iam.txt

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,38 @@ The driver checks for your credentials in the following sources in order:
153153
.. literalinclude:: /code-snippets/authentication/aws-env-variable.js
154154
:language: javascript
155155

156-
.. important:: Retrieval of AWS Credentials
157-
158-
Starting in MongoDB version 4.11, when you install the optional
159-
``aws-sdk/credential-providers`` dependency, the driver uses the AWS SDK
160-
to retrieve credentials from the environment. As a result, if you
161-
have a shared AWS credentials file or config file, the driver will
162-
use those credentials by default.
163-
164-
You can override this behavior by performing one of the following
165-
actions:
166-
167-
- Set ``AWS_SHARED_CREDENTIALS_FILE`` variable in your shell to point
168-
to your credentials file.
169-
- Set the equivalent environment variable in your application to point
170-
to your credentials file.
171-
- Create an AWS profile for your MongoDB credentials and set the
172-
``AWS_PROFILE`` environment variable to that profile name.
156+
Specifying AWS Credentials
157+
--------------------------
158+
159+
When you install the optional ``aws-sdk/credential-providers`` dependency, the driver
160+
retrieves credentials in a priority order defined by the AWS SDK. If you have a shared AWS
161+
credentials file or config file, the driver uses those credentials by default.
162+
163+
.. tip::
164+
165+
To learn more about how the ``aws-sdk/credential-providers`` dependency retrieves
166+
credentials, see the `AWS SDK documentation <https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-credential-providers/#fromnodeproviderchain>`__.
167+
168+
To manually specify the AWS credentials to retrieve, you can set the ``AWS_CREDENTIAL_PROVIDER``
169+
property to a defined credential provider from the AWS SDK. The following example passes a provider chain
170+
from the AWS SDK to the AWS authentication mechanism:
171+
172+
.. literalinclude:: /code-snippets/authentication/aws-custom-credentials.js
173+
:language: javascript
174+
:start-after: // start-custom-credentials
175+
:end-before: // end-custom-credentials
176+
:dedent:
177+
178+
To use a custom provider, you can pass any asynchronous function that returns your credentials
179+
to the ``AWS_CREDENTIAL_PROVIDER`` authentication mechanism property. The following example shows how to pass
180+
a custom provider function that fetches credentials from environment variables to the
181+
AWS authentication mechanism:
182+
183+
.. literalinclude:: /code-snippets/authentication/aws-custom-credentials.js
184+
:language: javascript
185+
:start-after: // start-custom-credentials-function
186+
:end-before: // end-custom-credentials-function
187+
:dedent:
173188

174189
API Documentation
175190
-----------------

0 commit comments

Comments
 (0)