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 11// ignored first line
22const { 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.
65const username = encodeURIComponent ( "<username>" ) ;
76const password = encodeURIComponent ( "<password>" ) ;
87const 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
Original file line number Diff line number Diff line change 11// ignored first line
22const { 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.
55const username = encodeURIComponent ( "<username>" ) ;
66const password = encodeURIComponent ( "<password>" ) ;
77const clusterUrl = "<MongoDB cluster url>" ;
88
99const 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
Original file line number Diff line number Diff line change 11// ignored first line
22const { 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.
55const username = encodeURIComponent ( "<username>" ) ;
66const password = encodeURIComponent ( "<password>" ) ;
77const clusterUrl = "<MongoDB cluster url>" ;
88
99const 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
Original file line number Diff line number Diff line change 11// ignored first line
22const { 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.
55const username = encodeURIComponent ( "<username>" ) ;
66const password = encodeURIComponent ( "<password>" ) ;
77const clusterUrl = "<MongoDB cluster url>" ;
88
99const 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
Original file line number Diff line number Diff line change 11// ignored first line
22const { MongoClient } = require ( "mongodb" ) ;
3- const fs = require ( "fs" ) ;
43
4+ // Replace the following with values for your environment.
55const username = encodeURIComponent ( "<client certificate distinguished name>" ) ;
66const 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+
109const 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments