Skip to content

Commit

Permalink
[s3] Pull AWS_SESSION_TOKEN from the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
samdoolin committed May 9, 2024
1 parent d84ad92 commit 9fca46c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/backends/amazon-S3.rst
Expand Up @@ -50,8 +50,10 @@ searches for them:
#. ``session_profile`` or ``AWS_S3_SESSION_PROFILE``
#. ``access_key`` or ``AWS_S3_ACCESS_KEY_ID`` or ``AWS_S3_SECRET_ACCESS_KEY``
#. ``secret_key`` or ``AWS_ACCESS_KEY_ID`` or ``AWS_SECRET_ACCESS_KEY``
#. ``security_token`` or ``AWS_SESSION_TOKEN`` or ``AWS_SECURITY_TOKEN``
#. The environment variables AWS_S3_ACCESS_KEY_ID and AWS_S3_SECRET_ACCESS_KEY
#. The environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
#. The environment variables AWS_SESSION_TOKEN and AWS_SECURITY_TOKEN
#. Use Boto3's default session

Settings
Expand Down
6 changes: 5 additions & 1 deletion storages/backends/s3.py
Expand Up @@ -378,7 +378,11 @@ def get_default_settings(self):
),
),
"security_token": setting(
"AWS_SESSION_TOKEN", setting("AWS_SECURITY_TOKEN")
"AWS_SESSION_TOKEN",
setting(
"AWS_SECURITY_TOKEN",
lookup_env(["AWS_SESSION_TOKEN", "AWS_SECURITY_TOKEN"]),
),
),
"session_profile": setting(
"AWS_S3_SESSION_PROFILE", lookup_env(["AWS_S3_SESSION_PROFILE"])
Expand Down
23 changes: 23 additions & 0 deletions tests/test_s3.py
Expand Up @@ -982,6 +982,29 @@ def test_auth_config(self):
access_key="foo", secret_key="boo", session_profile="moo"
)

def test_security_token(self):
with override_settings(AWS_SESSION_TOKEN="baz"):
storage = s3.S3Storage()
self.assertEqual(storage.security_token, "baz")

with override_settings(AWS_SECURITY_TOKEN="baz"):
storage = s3.S3Storage()
self.assertEqual(storage.security_token, "baz")

with mock.patch.dict(
os.environ,
{"AWS_SESSION_TOKEN": "baz"},
):
storage = s3.S3Storage()
self.assertEqual(storage.security_token, "baz")

with mock.patch.dict(
os.environ,
{"AWS_SECURITY_TOKEN": "baz"},
):
storage = s3.S3Storage()
self.assertEqual(storage.security_token, "baz")


class S3StaticStorageTests(TestCase):
def setUp(self):
Expand Down

0 comments on commit 9fca46c

Please sign in to comment.