Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions test/auth_aws/test_auth_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import sys
import unittest
from unittest.mock import patch

sys.path[0:0] = [""]

Expand Down Expand Up @@ -111,6 +112,63 @@ def test_poisoned_cache(self):
client.get_database().test.find_one()
self.assertNotEqual(auth.get_cached_credentials(), None)

def test_environment_variables_ignored(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this behavior outlined in the spec? Shouldn't the env vars take precedence even if we already have a previous cached credential?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in person, this behavior happens because we rely on aws for the credential lookup. The code is like:

if cached_creds:
    return cached_creds
creds = get_aws_creds()
if should_cache(creds):
    cached_creds = creds
return creds

So if the app is always using env vars then there will never be a cached credential and dynamic env var changes will propagate immediately. However, if the app is using temp creds then later tries to switch to dynamic env var creds, then changes won't propagate until the cached creds are cleared.

creds = self.setup_cache()
self.assertIsNotNone(creds)
prev = os.environ.copy()

client = MongoClient(self.uri)
self.addCleanup(client.close)

client.get_database().test.find_one()

self.assertIsNotNone(auth.get_cached_credentials())

mock_env = dict(
AWS_ACCESS_KEY_ID="foo", AWS_SECRET_ACCESS_KEY="bar", AWS_SESSION_TOKEN="baz"
)

with patch.dict("os.environ", mock_env):
self.assertEqual(os.environ["AWS_ACCESS_KEY_ID"], "foo")
client.get_database().test.find_one()

auth.set_cached_credentials(None)

client2 = MongoClient(self.uri)
self.addCleanup(client2.close)

with patch.dict("os.environ", mock_env):
self.assertEqual(os.environ["AWS_ACCESS_KEY_ID"], "foo")
with self.assertRaises(OperationFailure):
client2.get_database().test.find_one()

def test_no_cache_environment_variables(self):
creds = self.setup_cache()
self.assertIsNotNone(creds)
auth.set_cached_credentials(None)

mock_env = dict(AWS_ACCESS_KEY_ID=creds.username, AWS_SECRET_ACCESS_KEY=creds.password)
if creds.token:
mock_env["AWS_SESSION_TOKEN"] = creds.token

client = MongoClient(self.uri)
self.addCleanup(client.close)

with patch.dict(os.environ, mock_env):
self.assertEqual(os.environ["AWS_ACCESS_KEY_ID"], creds.username)
client.get_database().test.find_one()

self.assertIsNone(auth.get_cached_credentials())

mock_env["AWS_ACCESS_KEY_ID"] = "foo"

client2 = MongoClient(self.uri)
self.addCleanup(client2.close)

with patch.dict("os.environ", mock_env), self.assertRaises(OperationFailure):
self.assertEqual(os.environ["AWS_ACCESS_KEY_ID"], "foo")
client2.get_database().test.find_one()


class TestAWSLambdaExamples(unittest.TestCase):
def test_shared_client(self):
Expand Down