Skip to content

Commit 142ff06

Browse files
authored
docsp-27247 - aws auth (#83)
* first draft * cc feedback * dl feedback
1 parent a69f8cf commit 142ff06

File tree

1 file changed

+176
-6
lines changed

1 file changed

+176
-6
lines changed

source/fundamentals/authentication.txt

Lines changed: 176 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ of {+mongo-community+}:
2323

2424
- :ref:`csharp-scram-sha-256`
2525
- :ref:`csharp-scram-sha-1`
26+
- :ref:`csharp-mongodb-aws`
2627
- :ref:`csharp-x509`
2728

2829
To authenticate using ``GSSAPI/Kerberos`` or ``LDAP``, see the
@@ -46,14 +47,14 @@ MongoDB using either of the following methods:
4647
Mechanisms
4748
----------
4849

49-
The following examples specify authentication mechanisms using the following
50+
The following examples contain code examples that use the following
5051
placeholders:
5152

52-
- ``<username>``: Your MongoDB username.
53-
- ``<password>``: Your MongoDB user's password.
54-
- ``<hostname>``: The network address of your MongoDB server, accessible by your client.
55-
- ``<port>``: The port number of your MongoDB server.
56-
- ``<authenticationDb>``: The MongoDB database that contains your user's authentication
53+
- ``<username>`` - MongoDB username.
54+
- ``<password>`` - MongoDB user's password.
55+
- ``<hostname>`` - network address of the MongoDB server, accessible by your client.
56+
- ``<port>`` - port number of the MongoDB server.
57+
- ``<authenticationDb>`` - MongoDB database that contains the user's authentication
5758
data. If you omit this parameter, the driver uses the default value ``admin``.
5859

5960
.. _csharp-authentication-default:
@@ -142,6 +143,175 @@ string as follow:
142143

143144
To learn more on specifying the default mechanism, see :ref:`csharp-authentication-default`.
144145

146+
.. _csharp-mongodb-aws:
147+
148+
MONGODB-AWS
149+
~~~~~~~~~~~
150+
151+
.. note::
152+
153+
The ``MONGODB-AWS`` authentication mechanism is available only for
154+
MongoDB deployments on MongoDB Atlas.
155+
156+
The ``MONGODB-AWS`` authentication mechanism uses your Amazon Web Services
157+
Identity and Access Management (AWS IAM) credentials to authenticate your
158+
user. You can either specify your credentials explicitly
159+
or instruct the driver to retrieve them automatically from an external source.
160+
161+
The following sections contain code examples that use the following placeholders:
162+
163+
- ``<awsKeyId>`` - value of the AWS access key ID
164+
- ``<awsSecretKey>`` - value of the AWS secret access key
165+
- ``<awsSessionToken>`` - value of the AWS session token
166+
167+
.. tip::
168+
169+
To learn more about configuring MongoDB Atlas with AWS IAM, see the
170+
:atlas:`Set Up Passwordless Authentication with AWS IAM Roles </security/passwordless-authentication/#set-up-passwordless-authentication-with-aws-iam-roles>` guide.
171+
172+
Specify Your AWS IAM Credentials
173+
++++++++++++++++++++++++++++++++
174+
175+
You can supply your AWS IAM credentials on a ``MongoClientSettings`` object either by
176+
using a ``MongoCredential`` object or as part of the connection string. Select the
177+
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
178+
see the corresponding syntax for specifying your credentials:
179+
180+
.. tabs::
181+
182+
.. tab:: Connection String
183+
:tabid: mongodb-aws-mongoclientsettings-connection-string
184+
185+
.. code-block:: csharp
186+
187+
var connectionString = "mongodb+srv://<awsKeyId>:<awsSecretKey>@<hostname>[:<port>]?authSource=$external&authMechanism=MONGODB-AWS";
188+
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
189+
var client = new MongoClient(mongoClientSettings);
190+
191+
If you're using an AWS session token, include the ``authMechanismProperties``
192+
parameter in the connection string as shown below:
193+
194+
.. code-block:: csharp
195+
196+
var connectionString = "mongodb+srv://<awsKeyId>:<awsSecretKey>@<hostname>[:<port>]?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>";
197+
198+
.. tab:: MongoCredential
199+
:tabid: mongodb-aws-mongoclientsettings-mongo-credential
200+
201+
.. code-block:: csharp
202+
203+
var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb+srv://<hostname>[:<port>]");
204+
mongoClientSettings.Credential = new MongoCredential("MONGODB-AWS", new MongoExternalIdentity("<awsKeyId>"), new PasswordEvidence("<awsSecretKey>"));
205+
var client = new MongoClient(mongoClientSettings);
206+
207+
If you're using an AWS session token, call the ``WithMechanismProperty()``
208+
method on your ``MongoCredential`` object as shown below:
209+
210+
.. code-block:: csharp
211+
212+
mongoClientSettings.Credential = new MongoCredential("MONGODB-AWS", new MongoExternalIdentity("<awsKeyId>"), new PasswordEvidence("<awsSecretKey>"))
213+
.WithMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>");
214+
215+
Retrieve Credentials Automatically
216+
++++++++++++++++++++++++++++++++++
217+
218+
Instead of specifying your AWS IAM credentials in ``MongoClientSettings``, you can
219+
instruct the {+driver-short+} to use the AWS SDK to automatically retrieve your
220+
credentials from an external source. To instruct the driver to
221+
retrieve your credentials, perform the following actions:
222+
223+
- Specify ``MONGODB-AWS`` as the authentication mechanism
224+
- Specify that the authentication source is external to MongoDB
225+
- Set your credentials in the appropriate location
226+
227+
You can specify the authentication mechanism and source either
228+
by using a ``MongoCredential`` object or as part of the connection string. Select the
229+
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
230+
see the corresponding syntax for specifying the ``MONGODB-AWS`` authentication mechanism
231+
and external authentication source:
232+
233+
.. tabs::
234+
235+
.. tab:: Connection String
236+
:tabid: mongodb-aws-automatic-connection-string
237+
238+
.. code-block:: csharp
239+
240+
var connectionString = "mongodb+srv://<hostname>[:<port>]?authMechanism=MONGODB-AWS&authSource=$external";
241+
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
242+
var client = new MongoClient(mongoClientSettings);
243+
244+
.. tab:: MongoCredential
245+
:tabid: mongodb-aws-automatic-mongo-credential
246+
247+
.. code-block:: csharp
248+
249+
var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb+srv://<hostname>[:<port>]");
250+
mongoClientSettings.Credential = new MongoCredential("MONGODB-AWS", new MongoExternalAwsIdentity(), new ExternalEvidence());
251+
var client = new MongoClient(mongoClientSettings);
252+
253+
After you specify the authentication mechanism and source, you must set
254+
your credentials in the location appropriate to the credential type. The {+driver-short+}
255+
checks for credentials in the following locations in the order listed here:
256+
257+
- Web identity provider
258+
- Shared AWS credentials file
259+
- Environment variables
260+
- ECS container credentials
261+
- EC2 container credentials
262+
263+
You can use an OpenID Connect (OIDC)-compatible **web identity provider** to authenticate
264+
to Amazon Elastic Kubernetes Service (EKS) or other services.
265+
To use a web identity provider, create a file that contains your
266+
OIDC token, then set the absolute path to this file in an environment variable by using
267+
``bash`` or a similar shell as shown in the following example:
268+
269+
.. code-block:: bash
270+
271+
export AWS_WEB_IDENTITY_TOKEN_FILE=<absolute path to file containing your OIDC token>
272+
273+
To authenticate by using a profile in a **shared AWS credentials file**, you can use a text
274+
editor, the AWS SDK for .NET, or the AWS CLI to create the appropriate credential file.
275+
276+
To retrieve credentials directly from **environment variables**, set the following
277+
environment variables by using ``bash`` or a similar shell:
278+
279+
.. code-block:: bash
280+
281+
export AWS_ACCESS_KEY_ID=<awsKeyId>
282+
export AWS_SECRET_ACCESS_KEY=<awsSecretKey>
283+
export AWS_SESSION_TOKEN=<awsSessionToken>
284+
285+
.. note::
286+
287+
Omit the line containing ``AWS_SESSION_TOKEN`` if you don't need an AWS
288+
session token for that role.
289+
290+
To authenticate by using **ECS container credentials**, set the URI of your ECS
291+
endpoint in an environment variable by using ``bash`` or a similar shell.
292+
Select the :guilabel:`Full ECS URI` or :guilabel:`Relative ECS URI` tab to
293+
see the syntax for specifying the corresponding environment variable:
294+
295+
.. tabs::
296+
297+
.. tab:: Full ECS URI
298+
:tabid: mongodb-aws-full-ecs-uri
299+
300+
.. code-block:: bash
301+
302+
export AWS_CONTAINER_CREDENTIALS_FULL_URI=<full ECS endpoint>
303+
304+
.. tab:: Relative ECS URI
305+
:tabid: mongodb-aws-relative-ecs-uri
306+
307+
.. code-block:: bash
308+
309+
export AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=<relative ECS endpoint>
310+
311+
To authenticate by using **EC2 container credentials**, make sure none of the
312+
environment variables mentioned earlier are set. The driver obtains the
313+
credentials from the default IPv4 EC2 instance metadata endpoint.
314+
145315
.. _csharp-x509:
146316

147317
X.509

0 commit comments

Comments
 (0)