File tree Expand file tree Collapse file tree 7 files changed +83
-25
lines changed
code-snippets/authentication
fundamentals/authentication Expand file tree Collapse file tree 7 files changed +83
-25
lines changed Original file line number Diff line number Diff line change
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 ) ;
Original file line number Diff line number Diff line change 1
1
// ignored first line
2
2
const { MongoClient } = require ( "mongodb" ) ;
3
- const fs = require ( "fs" ) ;
4
3
5
- // specify the placeholder values for your environment in the following lines
4
+ // Replace the following with values for your environment.
6
5
const username = encodeURIComponent ( "<username>" ) ;
7
6
const password = encodeURIComponent ( "<password>" ) ;
8
7
const clusterUrl = "<MongoDB cluster url>" ;
9
8
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 =
14
11
`mongodb+srv://${ username } :${ password } @${ clusterUrl } /?authMechanism=${ authMechanism } &tls=true&tlsCertificateKeyFile=${ clientPEMFile } ` ;
15
12
16
13
// Create a new MongoClient
Original file line number Diff line number Diff line change 1
1
// ignored first line
2
2
const { MongoClient } = require ( "mongodb" ) ;
3
3
4
- // specify the placeholder values for your environment in the following lines
4
+ // Replace the following with values for your environment.
5
5
const username = encodeURIComponent ( "<username>" ) ;
6
6
const password = encodeURIComponent ( "<password>" ) ;
7
7
const clusterUrl = "<MongoDB cluster url>" ;
8
8
9
9
const authMechanism = "DEFAULT" ;
10
10
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 =
14
13
`mongodb+srv://${ username } :${ password } @${ clusterUrl } /?authMechanism=${ authMechanism } ` ;
15
14
16
15
// Create a new MongoClient
Original file line number Diff line number Diff line change 1
1
// ignored first line
2
2
const { MongoClient } = require ( "mongodb" ) ;
3
3
4
- // specify the placeholder values for your environment in the following lines
4
+ // Replace the following with values for your environment.
5
5
const username = encodeURIComponent ( "<username>" ) ;
6
6
const password = encodeURIComponent ( "<password>" ) ;
7
7
const clusterUrl = "<MongoDB cluster url>" ;
8
8
9
9
const authMechanism = "SCRAM-SHA-1" ;
10
10
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 =
14
13
`mongodb+srv://${ username } :${ password } @${ clusterUrl } /?authMechanism=${ authMechanism } ` ;
15
14
16
15
// Create a new MongoClient
Original file line number Diff line number Diff line change 1
1
// ignored first line
2
2
const { MongoClient } = require ( "mongodb" ) ;
3
3
4
- // specify the placeholder values for your environment in the following lines
4
+ // Replace the following with values for your environment.
5
5
const username = encodeURIComponent ( "<username>" ) ;
6
6
const password = encodeURIComponent ( "<password>" ) ;
7
7
const clusterUrl = "<MongoDB cluster url>" ;
8
8
9
9
const authMechanism = "SCRAM-SHA-256" ;
10
10
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 =
14
13
`mongodb+srv://${ username } :${ password } @${ clusterUrl } /?authMechanism=${ authMechanism } ` ;
15
14
16
15
// Create a new MongoClient
Original file line number Diff line number Diff line change 1
1
// ignored first line
2
2
const { MongoClient } = require ( "mongodb" ) ;
3
- const fs = require ( "fs" ) ;
4
3
4
+ // Replace the following with values for your environment.
5
5
const username = encodeURIComponent ( "<client certificate distinguished name>" ) ;
6
6
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
+
10
9
const authMechanism = "MONGODB-X509" ;
11
10
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 =
15
13
`mongodb+srv://${ username } @${ clusterUrl } /?authMechanism=${ authMechanism } &tls=true&tlsCertificateKeyFile=${ clientPEMFile } ` ;
16
14
17
15
// Create a new MongoClient
Original file line number Diff line number Diff line change @@ -125,6 +125,40 @@ in the following sample code.
125
125
SCRAM </release-notes/3.0-scram/>`, any ``MONGODB-CR`` user
126
126
authentication requests fail.
127
127
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
+
128
162
``X.509``
129
163
---------
130
164
You can’t perform that action at this time.
0 commit comments