Skip to content

Commit 364761d

Browse files
authored
DRIVERS-2333 Cache AWS Credentials Where Possible (#1281)
1 parent e780e91 commit 364761d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

source/auth/auth.rst

+24
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,29 @@ From the JSON response drivers
10981098
MUST obtain the ``access_key``, ``secret_key`` and ``security_token`` which will be used during the `Signature Version 4 Signing Process
10991099
<https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html?shortFooter=true>`_.
11001100

1101+
Caching Credentials
1102+
___________________
1103+
Credentials fetched by the driver using AWS endpoints MUST be cached and reused
1104+
to avoid hitting AWS rate limitations. AWS recommends using a suitable
1105+
Software Development Kit (SDK) for your langauge. If that SDK supports
1106+
credential fetch and automatic refresh/caching, then that mechanism can
1107+
be used in lieu of manual caching.
1108+
1109+
If using manual caching, the "Expiration" field MUST be stored
1110+
and used to determine when to clear the cache. Credentials are considered
1111+
valid if they are more than five minutes away from expiring; to the reduce the
1112+
chance of expiration before they are validated by the server.
1113+
1114+
If there are no current valid cached credentials, the driver MUST initiate a
1115+
credential request. To avoid adding a bottleneck that would override the
1116+
``maxConnecting`` setting, the driver MUST not place a lock on making a
1117+
request. The cache MUST be written atomically.
1118+
1119+
If AWS authentication fails for any reason, the cache MUST be cleared.
1120+
1121+
.. note::
1122+
Five minutes was chosen based on the AWS documentation for `IAM roles for EC2 <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html>`_ : "We make new credentials available at least five minutes before the expiration of the old credentials". The intent is to have some buffer between when the driver fetches the credentials and when the server verifies them.
1123+
11011124
-------------------------
11021125
Connection String Options
11031126
-------------------------
@@ -1334,6 +1357,7 @@ Q: Should drivers support accessing Amazon EC2 instance metadata in Amazon ECS?
13341357
Changelog
13351358
=========
13361359

1360+
:2022-10-07: Require caching of AWS credentials fetched by the driver.
13371361
:2022-10-05: Remove spec front matter and convert version history to changelog.
13381362
:2022-09-07: Add support for AWS AssumeRoleWithWebIdentity.
13391363
:2022-01-20: Require that timeouts be applied per the client-side operations timeout spec.

source/auth/tests/mongodb-aws.rst

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ There are 6 scenarios drivers MUST test:
1010
#. ``Assume Role``: Auth via temporary credentials obtained from an STS AssumeRole request
1111
#. ``Assume Role with Web Identity``: Auth via temporary credentials obtained from an STS AssumeRoleWithWebIdentity request
1212
#. ``AWS Lambda``: Auth via environment variables ``AWS_ACCESS_KEY_ID``, ``AWS_SECRET_ACCESS_KEY``, and ``AWS_SESSION_TOKEN``.
13+
#. Caching of AWS credentials fetched by the driver.
1314

1415
For brevity, this section gives the values ``<AccessKeyId>``, ``<SecretAccessKey>`` and ``<Token>`` in place of a valid access key ID, secret access key and session token (also known as a security token). Note that if these values are passed into the URI they MUST be URL encoded. Sample values are below.
1516

@@ -115,3 +116,26 @@ Sample URIs both with and without optional session tokens set are shown below. D
115116
URI="mongodb://localhost/?authMechanism=MONGODB-AWS"
116117
117118
.. note:: No username, password or session token is passed into the URI. Drivers MUST check the environment variables listed above for these values. If the session token is set Drivers MUST use it.
119+
120+
121+
Cached Credentials
122+
==================
123+
124+
Drivers MUST ensure that they are testing the ability to cache credentials.
125+
Drivers will need to be able to query and override the cached credentials to
126+
verify usage. To determine whether to run the cache tests, the driver can
127+
check for the absence of the AWS_ACCESS_KEY_ID and of credentials in the URI.
128+
129+
#. Clear the cache.
130+
#. Create a new client.
131+
#. Ensure that a ``find`` operation adds credentials to the cache.
132+
#. Override the cached credentials with an "Expiration" that is within one
133+
minute of the current UTC time.
134+
#. Create a new client.
135+
#. Ensure that a ``find`` operation updates the credentials in the cache.
136+
#. Poison the cache with an invalid access key id.
137+
#. Create a new client.
138+
#. Ensure that a ``find`` operation results in an error.
139+
#. Ensure that the cache has been cleared.
140+
#. Ensure that a subsequent ``find`` operation succeeds.
141+
#. Ensure that the cache has been set.

0 commit comments

Comments
 (0)