Skip to content

Commit d502f89

Browse files
DOCSP-18964 AWS session token (#263)
* added info about AWS session token
1 parent 81372eb commit d502f89

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
// Remember to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
4+
// credentials to your environment variables.
5+
const clusterUrl = "<MongoDB cluster url>";
6+
const authMechanism = "MONGODB-AWS";
7+
8+
let uri =
9+
`mongodb+srv://${clusterUrl}/?authSource=%24external&authMechanism=${authMechanism}`;
10+
11+
// Create a new MongoClient.
12+
const client = new MongoClient(uri);
13+
14+
async function run() {
15+
try {
16+
// Connect the client to the server.
17+
await client.connect();
18+
19+
// Establish and verify connection.
20+
await client.db("admin").command({ ping: 1 });
21+
console.log("Connected successfully to server.");
22+
} finally {
23+
// Ensure that the client closes when it finishes/errors.
24+
await client.close();
25+
}
26+
}
27+
run().catch(console.dir);

source/code-snippets/authentication/aws.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const clusterUrl = "<MongoDB cluster url>";
88
const authMechanism = "MONGODB-AWS";
99

1010
let uri =
11-
`mongodb+srv://${accessKeyId}:${secretAccessKey}@${clusterUrl}/?authMechanism=${authMechanism}`;
12-
11+
`mongodb+srv://${accessKeyId}:${secretAccessKey}@${clusterUrl}/?authSource=%24external&authMechanism=${authMechanism}`;
12+
1313
// Uncomment the following lines if your AWS authentication setup requires a session token.
1414
// const sessionToken = encodeURIComponent("<AWS_SESSION_TOKEN>");
15-
// uri = uri.concat(`&authMechanismProperties=${sessionToken}`);
15+
// uri = uri.concat(`&authMechanismProperties=AWS_SESSION_TOKEN:${sessionToken}`);
1616

1717
// Create a new MongoClient.
1818
const client = new MongoClient(uri);

source/fundamentals/authentication/mechanisms.txt

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,67 @@ in the following sample code.
133133

134134
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
135135
Identity and Access Management (AWS IAM) credentials to authenticate your
136-
user.
136+
user. If you do not already have the `AWS signature library
137+
<https://www.npmjs.com/package/aws4>`__, install it using the following
138+
``npm`` command:
139+
140+
.. code-block:: bash
141+
142+
npm install aws4
137143

138144
To connect to a MongoDB instance with ``MONGODB-AWS`` authentication
139-
enabled, specify the ``MONGODB-AWS`` authentication mechanism and pass
140-
your ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` credentials to the
141-
driver when you attempt to connect. If your AWS login requires a session
142-
token, you must include your ``AWS_SESSION_TOKEN`` as well.
145+
enabled, specify the ``MONGODB-AWS`` authentication mechanism.
143146

144-
The driver checks the following sources for your credentials in order:
147+
The driver checks for your credentials in the following sources in order:
145148

146149
1. Connection string
147150
2. Environment variables
148151
3. AWS ECS endpoint specified in ``AWS_CONTAINER_CREDENTIALS_RELATIVE_URI``
149152
4. AWS EC2 endpoint. For more information, see `IAM Roles for Tasks
150153
<https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`_.
151154

152-
If you do not already have the `AWS signature library <https://www.npmjs.com/package/aws4>`__,
153-
install it using the following ``npm`` command:
155+
.. tabs::
154156

155-
.. code-block:: console
157+
.. tab:: Environment Variables
158+
:tabid: environment variables
156159

157-
npm install aws4
160+
To connect to your MongoDB instance with environment variables,
161+
add your ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY``
162+
credentials to your environment variables. If your AWS
163+
login requires an ``AWS_SESSION_TOKEN``, add it to your
164+
environment variables as well.
158165

159-
The following code shows an example of specifying the ``MONGODB-AWS``
160-
authentication mechanism and credentials in the connection string:
166+
The following code shows an example of specifying the ``MONGODB-AWS``
167+
authentication mechanism with environment variables:
161168

162-
.. literalinclude:: /code-snippets/authentication/aws.js
163-
:language: javascript
169+
.. note::
170+
171+
You don't need to specify these credentials in your connection URI
172+
because the driver automatically retrieves them when you attempt
173+
to connect.
174+
175+
.. literalinclude:: /code-snippets/authentication/aws-env-variable.js
176+
:language: javascript
177+
178+
.. tab:: Connection String
179+
:tabid: connection string
180+
181+
To connect to your MongoDB instance with a connection string, pass
182+
your ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY``
183+
credentials to the driver when you attempt to connect. If your AWS
184+
login requires a session token, include your ``AWS_SESSION_TOKEN`` as well.
185+
186+
The following code shows an example of specifying the ``MONGODB-AWS``
187+
authentication mechanism and credentials with a connection string:
188+
189+
.. important::
190+
191+
Always **URI encode** the username and certificate file path using the
192+
``encodeURIComponent`` method to ensure they are correctly parsed.
164193

194+
.. literalinclude:: /code-snippets/authentication/aws.js
195+
:language: javascript
196+
165197
``X.509``
166198
---------
167199

0 commit comments

Comments
 (0)