|
| 1 | +.. _ruby-authentication-aws: |
| 2 | + |
| 3 | +================================== |
| 4 | +AWS Identity and Access Management |
| 5 | +================================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 3 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: amazon web services, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +.. note:: |
| 24 | + |
| 25 | + AWS authentication is available only in the MongoDB Enterprise Edition for MongoDB 4.4 |
| 26 | + and later. |
| 27 | + |
| 28 | +The AWS authentication mechanism uses AWS `Identity and Access Management (IAM) |
| 29 | +<https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html>`_ |
| 30 | +and AWS `Security Token Service (STS) |
| 31 | +<https://docs.aws.amazon.com/STS/latest/APIReference/Welcome.html>`_ |
| 32 | +to prove the client's identity to a MongoDB deployment. The following steps describe the |
| 33 | +AWS authentication process: |
| 34 | + |
| 35 | +1. The client uses AWS IAM credentials to create a signature that is sent to |
| 36 | + the MongoDB deployment. |
| 37 | +2. The deployment uses the client's signature to send a request to AWS STS. |
| 38 | +3. If the request succeeds, STS returns the Amazon Resource Name (ARN) of |
| 39 | + the IAM user or role that corresponds to the client's credentials. |
| 40 | +4. The deployment uses the returned ARN to look up the user. The |
| 41 | + client is authenticated as this user. |
| 42 | + |
| 43 | +.. note:: |
| 44 | + |
| 45 | + The client and server use different usernames. The client uses the AWS access key ID, |
| 46 | + but the server uses the ARN of the IAM user or role corresponding to the access key ID. |
| 47 | + |
| 48 | +AWS credentials include the following components: |
| 49 | + |
| 50 | +- Access key ID |
| 51 | +- Secret access key |
| 52 | +- Optional session token |
| 53 | + |
| 54 | +Authentication with `AWS IAM credentials |
| 55 | +<https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html>`__ |
| 56 | +uses the access key ID and the secret access key. Authentication with |
| 57 | +`temporary AWS IAM credentials |
| 58 | +<https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html>`__ |
| 59 | +uses all three components. |
| 60 | + |
| 61 | +.. note:: |
| 62 | + |
| 63 | + The driver never sends the secret access key or the session token over |
| 64 | + the network. |
| 65 | + |
| 66 | +Temporary credentials are used with: |
| 67 | + |
| 68 | +- STS `Assume Role <https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-api.html>`__ |
| 69 | + requests. |
| 70 | +- `EC2 instance roles <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html>`__. |
| 71 | +- `ECS task roles <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`__. |
| 72 | +- `AWS Lambda environment <https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html>`__. |
| 73 | +- `IAM roles for service accounts <https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html>`__. |
| 74 | + |
| 75 | +Code Placeholders |
| 76 | +~~~~~~~~~~~~~~~~~ |
| 77 | + |
| 78 | +The code examples on this page use the following placeholders: |
| 79 | + |
| 80 | +- ``<hostname>``: The network address of your MongoDB deployment |
| 81 | +- ``<aws-access-key-id>``: The AWS access key ID |
| 82 | +- ``<aws_secret_access_key>``: The AWS secret access key |
| 83 | +- ``<aws_session_token>``: The AWS session token |
| 84 | + |
| 85 | +Using AWS IAM Authentication in Your Application |
| 86 | +------------------------------------------------ |
| 87 | + |
| 88 | +The following sections describe how to use the AWS IAM authentication mechanism in your |
| 89 | +application. |
| 90 | + |
| 91 | +Providing Credentials Explicitly |
| 92 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +You can provide regular (non-temporary) IAM credentials as client options or by using a URI. |
| 95 | +Select the :guilabel:`Connection String` or :guilabel:`Client Options` tab to |
| 96 | +see the corresponding syntax: |
| 97 | + |
| 98 | +.. tabs:: |
| 99 | + |
| 100 | + .. tab:: Connection String |
| 101 | + :tabid: default-connection-string |
| 102 | + |
| 103 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 104 | + :start-after: start-aws-connection-string |
| 105 | + :end-before: end-aws-connection-string |
| 106 | + :language: ruby |
| 107 | + :copyable: |
| 108 | + :dedent: |
| 109 | + |
| 110 | + .. tab:: Client Options |
| 111 | + :tabid: default-mongo-credential |
| 112 | + |
| 113 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 114 | + :start-after: start-aws |
| 115 | + :end-before: end-aws |
| 116 | + :language: ruby |
| 117 | + :copyable: |
| 118 | + :dedent: |
| 119 | + |
| 120 | +.. note:: |
| 121 | + |
| 122 | + If you provide credentials in a URI, you must percent-encode them. |
| 123 | + |
| 124 | +Providing Temporary Credentials |
| 125 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 126 | + |
| 127 | +To provide temporary credentials, specify the session token in the |
| 128 | +client options or by using a URI. Select the :guilabel:`Connection String` |
| 129 | +or :guilabel:`Client Options` tab to see the corresponding syntax: |
| 130 | + |
| 131 | +.. tabs:: |
| 132 | + |
| 133 | + .. tab:: Connection String |
| 134 | + :tabid: default-connection-string |
| 135 | + |
| 136 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 137 | + :start-after: start-aws-temp-connection-string |
| 138 | + :end-before: end-aws-temp-connection-string |
| 139 | + :language: ruby |
| 140 | + :copyable: |
| 141 | + :dedent: |
| 142 | + |
| 143 | + .. tab:: Client Options |
| 144 | + :tabid: default-mongo-credential |
| 145 | + |
| 146 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 147 | + :start-after: start-aws-temp |
| 148 | + :end-before: end-aws-temp |
| 149 | + :language: ruby |
| 150 | + :copyable: |
| 151 | + :dedent: |
| 152 | + |
| 153 | +Automatically Retrieving Credentials |
| 154 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 155 | + |
| 156 | +The client can retrieve credentials from the environment or from EC2 or ECS |
| 157 | +metadata endpoints. To retrieve credentials automatically, specify the |
| 158 | +AWS authentication mechanism but do not specify a username or a password. Select the |
| 159 | +:guilabel:`Connection String` or :guilabel:`Client Options` tab to see the corresponding syntax: |
| 160 | + |
| 161 | +.. tabs:: |
| 162 | + |
| 163 | + .. tab:: Connection String |
| 164 | + :tabid: default-connection-string |
| 165 | + |
| 166 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 167 | + :start-after: start-aws-automatic-retrieval-connection-string |
| 168 | + :end-before: end-aws-automatic-retrieval-connection-string |
| 169 | + :language: ruby |
| 170 | + :copyable: |
| 171 | + :dedent: |
| 172 | + |
| 173 | + .. tab:: Client Options |
| 174 | + :tabid: default-mongo-credential |
| 175 | + |
| 176 | + .. literalinclude:: /includes/authentication/aws-iam.rb |
| 177 | + :start-after: start-aws-automatic-retrieval |
| 178 | + :end-before: end-aws-automatic-retrieval |
| 179 | + :language: ruby |
| 180 | + :copyable: |
| 181 | + :dedent: |
| 182 | + |
| 183 | +The driver tries to obtain credentials from the following sources, in |
| 184 | +the specified order: |
| 185 | + |
| 186 | +- ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY`` and ``AWS_SESSION_TOKEN`` |
| 187 | + environment variables. These environment variables are recognized by |
| 188 | + a variety of AWS-related libraries and tools, such as the official |
| 189 | + AWS Ruby SDK and the AWS CLI. They are also defined when running in an |
| 190 | + AWS Lambda environment. |
| 191 | +- AWS STS `AssumeRoleWithWebIdentity action |
| 192 | + <https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html>`__. |
| 193 | + This mechanism returns credentials associated with the service account token, and |
| 194 | + requires the following environment variables to be set: |
| 195 | + |
| 196 | + - ``AWS_WEB_IDENTITY_TOKEN_FILE``: Path to a file containing the service |
| 197 | + account token. |
| 198 | + - ``AWS_ROLE_ARN``: The Amazon Resource Name (ARN) of the role that the |
| 199 | + caller is assuming. |
| 200 | + - ``AWS_ROLE_SESSION_NAME`` (optional): Identifier for the assumed role |
| 201 | + session. If this variable is empty, the driver generates a random identifier. |
| 202 | + |
| 203 | +- The AWS `ECS task metadata |
| 204 | + <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-iam-roles.html>`__ endpoint. |
| 205 | + This endpoint returns credentials associated with the ECS task role assigned to |
| 206 | + the container. |
| 207 | +- The AWS `EC2 instance metadata |
| 208 | + <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html>`__ endpoint. |
| 209 | + This endpoint returns credentials associated with the EC2 instance role assigned to |
| 210 | + the instance. |
| 211 | + |
| 212 | +.. important:: |
| 213 | + |
| 214 | + A credentials source must provide a complete |
| 215 | + set of credentials. For example, if your application uses the ``AWS_ACCESS_KEY_ID`` |
| 216 | + and ``AWS_SECRET_ACCESS_KEY`` environment variables, the driver raises an error if only |
| 217 | + one of these variables has a value. |
| 218 | + |
| 219 | +.. note:: |
| 220 | + |
| 221 | + If an application runs in an ECS container on an EC2 instance and |
| 222 | + the container is allowed access to the instance metadata, |
| 223 | + the driver attempts to retrieve AWS credentials from the EC2 instance metadata endpoint. |
| 224 | + If the driver retrieves credentials in this way, your application can authenticate as the IAM |
| 225 | + role assigned to the EC2 instance. |
| 226 | + |
| 227 | + To learn how to prevent containers from accessing EC2 instance metadata, |
| 228 | + see the `AWS documentation <https://aws.amazon.com/premiumsupport/knowledge-center/ecs-container-ec2-metadata>`__. |
| 229 | + |
| 230 | +API Documentation |
| 231 | +----------------- |
| 232 | + |
| 233 | +To learn more about any of the methods or types discussed on this |
| 234 | +page, see the following API documentation: |
| 235 | + |
| 236 | +- `Mongo::Client <{+api-root+}/Mongo/Client.html>`__ |
0 commit comments