Skip to content

Commit 50022c1

Browse files
author
Chris Cho
authored
DOCSP-9407: Add MONGODB-AWS auth mechanism (#82)
* DOCSP-9407: Add MONGODB-AWS auth mechanism
1 parent fba8188 commit 50022c1

File tree

7 files changed

+83
-25
lines changed

7 files changed

+83
-25
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ignored first line
2+
const { MongoClient } = require("mongodb");
3+
4+
// Replace the following with values for your environment.
5+
const accessKeyId = encodeURIComponent("<AWS_ACCESS_KEY_ID>");
6+
const secretAccessKey = encodeURIComponent("<AWS_SECRET_ACCESS_KEY>");
7+
const clusterUrl = "<MongoDB cluster url>";
8+
9+
const authMechanism = "MONGODB-AWS";
10+
11+
// Replace the following with your MongoDB deployment's connection string.
12+
const uri =
13+
`mongodb+srv://${accessKeyId}:${secretAccessKey}@${clusterUrl}/?authMechanism=${authMechanism}`;
14+
15+
// Create a new MongoClient
16+
const client = new MongoClient(uri);
17+
18+
// Function to connect to the server
19+
async function run() {
20+
try {
21+
// Connect the client to the server
22+
await client.connect();
23+
24+
// Establish and verify connection
25+
await client.db("admin").command({ ping: 1 });
26+
console.log("Connected successfully to server");
27+
} finally {
28+
// Ensures that the client will close when you finish/error
29+
await client.close();
30+
}
31+
}
32+
run().catch(console.dir);

source/code-snippets/authentication/cr.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
// ignored first line
22
const { MongoClient } = require("mongodb");
3-
const fs = require("fs");
43

5-
// specify the placeholder values for your environment in the following lines
4+
// Replace the following with values for your environment.
65
const username = encodeURIComponent("<username>");
76
const password = encodeURIComponent("<password>");
87
const clusterUrl = "<MongoDB cluster url>";
98

10-
11-
// Replace the following with your MongoDB deployment's connection
12-
// string.
13-
const uri =
9+
// Replace the following with your MongoDB deployment's connection string.
10+
const uri =
1411
`mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}&tls=true&tlsCertificateKeyFile=${clientPEMFile}`;
1512

1613
// Create a new MongoClient

source/code-snippets/authentication/default.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// ignored first line
22
const { MongoClient } = require("mongodb");
33

4-
// specify the placeholder values for your environment in the following lines
4+
// Replace the following with values for your environment.
55
const username = encodeURIComponent("<username>");
66
const password = encodeURIComponent("<password>");
77
const clusterUrl = "<MongoDB cluster url>";
88

99
const authMechanism = "DEFAULT";
1010

11-
// Replace the following with your MongoDB deployment's connection
12-
// string.
13-
const uri =
11+
// Replace the following with your MongoDB deployment's connection string.
12+
const uri =
1413
`mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`;
1514

1615
// Create a new MongoClient

source/code-snippets/authentication/sha1.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// ignored first line
22
const { MongoClient } = require("mongodb");
33

4-
// specify the placeholder values for your environment in the following lines
4+
// Replace the following with values for your environment.
55
const username = encodeURIComponent("<username>");
66
const password = encodeURIComponent("<password>");
77
const clusterUrl = "<MongoDB cluster url>";
88

99
const authMechanism = "SCRAM-SHA-1";
1010

11-
// Replace the following with your MongoDB deployment's connection
12-
// string.
13-
const uri =
11+
// Replace the following with your MongoDB deployment's connection string.
12+
const uri =
1413
`mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`;
1514

1615
// Create a new MongoClient

source/code-snippets/authentication/sha256.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// ignored first line
22
const { MongoClient } = require("mongodb");
33

4-
// specify the placeholder values for your environment in the following lines
4+
// Replace the following with values for your environment.
55
const username = encodeURIComponent("<username>");
66
const password = encodeURIComponent("<password>");
77
const clusterUrl = "<MongoDB cluster url>";
88

99
const authMechanism = "SCRAM-SHA-256";
1010

11-
// Replace the following with your MongoDB deployment's connection
12-
// string.
13-
const uri =
11+
// Replace the following with your MongoDB deployment's connection string.
12+
const uri =
1413
`mongodb+srv://${username}:${password}@${clusterUrl}/?authMechanism=${authMechanism}`;
1514

1615
// Create a new MongoClient

source/code-snippets/authentication/x509.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// ignored first line
22
const { MongoClient } = require("mongodb");
3-
const fs = require("fs");
43

4+
// Replace the following with values for your environment.
55
const username = encodeURIComponent("<client certificate distinguished name>");
66
const clusterUrl = "<MongoDB cluster url>";
7-
const clientPEMFile = encodeURIComponent(
8-
"<path to the client pem certificate file>",
9-
);
7+
const clientPEMFile = encodeURIComponent("<path to the client pem certificate file>");
8+
109
const authMechanism = "MONGODB-X509";
1110

12-
// Replace the following with your MongoDB deployment's connection
13-
// string.
14-
const uri =
11+
// Replace the following with your MongoDB deployment's connection string.
12+
const uri =
1513
`mongodb+srv://${username}@${clusterUrl}/?authMechanism=${authMechanism}&tls=true&tlsCertificateKeyFile=${clientPEMFile}`;
1614

1715
// Create a new MongoClient

source/fundamentals/authentication/mechanisms.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,40 @@ in the following sample code.
125125
SCRAM </release-notes/3.0-scram/>`, any ``MONGODB-CR`` user
126126
authentication requests fail.
127127

128+
129+
``MONGODB-AWS``
130+
---------------
131+
132+
.. note::
133+
The MONGODB-AWS authentication mechanism is only available in MongoDB
134+
versions 4.4 and later.
135+
136+
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
137+
Identity and Access Management (AWS IAM) credentials to authenticate your
138+
user.
139+
140+
To connect to a MongoDB instance with ``MONGODB-AWS`` authentication
141+
enabled, specify the ``MONGODB-AWS`` authentication mechanism and pass
142+
your ``AWS_ACCESS_KEY_ID`` and ``AWS_SECRET_ACCESS_KEY`` credentials to the
143+
driver when you attempt to connect. If your AWS login requires a session
144+
token, you must include your ``AWS_SESSION_TOKEN`` as well.
145+
146+
The driver checks the following sources for your credentials in order:
147+
148+
1. Connection string
149+
2. Environment variables
150+
3. AWS ECS endpoint specified in ``AWS_CONTAINER_CREDENTIALS_RELATIVE_URI``
151+
4. AWS EC2 endpoint. For more information, see `IAM Roles for Tasks
152+
<https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`_.
153+
154+
The following code shows an example of specifying the ``MONGODB-AWS``
155+
authentication mechanism and credentials in the connection string.
156+
157+
.. literalinclude:: /code-snippets/authentication/aws.js
158+
:language: javascript
159+
:dedent: 4
160+
161+
128162
``X.509``
129163
---------
130164

0 commit comments

Comments
 (0)