From 5a12535a4f727e6fb45d95542952c3b008c65c7b Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Wed, 8 Nov 2023 20:12:41 +0100 Subject: [PATCH] Fix `AWS_S3_*` environment variables lookup --- storages/backends/s3.py | 19 ++++++++++++++++--- storages/utils.py | 11 +++++++++++ tests/test_s3.py | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/storages/backends/s3.py b/storages/backends/s3.py index 466489c8..a63ea6e6 100644 --- a/storages/backends/s3.py +++ b/storages/backends/s3.py @@ -26,6 +26,7 @@ from storages.utils import clean_name from storages.utils import get_available_overwrite_name from storages.utils import is_seekable +from storages.utils import lookup_env from storages.utils import safe_join from storages.utils import setting from storages.utils import to_bytes @@ -352,14 +353,26 @@ def get_cloudfront_signer(self, key_id, key): def get_default_settings(self): return { - "access_key": setting("AWS_S3_ACCESS_KEY_ID", setting("AWS_ACCESS_KEY_ID")), + "access_key": setting( + "AWS_S3_ACCESS_KEY_ID", + setting( + "AWS_ACCESS_KEY_ID", + lookup_env(["AWS_S3_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID"]) + ) + ), "secret_key": setting( - "AWS_S3_SECRET_ACCESS_KEY", setting("AWS_SECRET_ACCESS_KEY") + "AWS_S3_SECRET_ACCESS_KEY", + setting( + "AWS_SECRET_ACCESS_KEY", + lookup_env(["AWS_S3_SECRET_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY"]) + ) ), "security_token": setting( "AWS_SESSION_TOKEN", setting("AWS_SECURITY_TOKEN") ), - "session_profile": setting("AWS_S3_SESSION_PROFILE"), + "session_profile": setting( + "AWS_S3_SESSION_PROFILE", lookup_env(["AWS_S3_SESSION_PROFILE"]) + ), "file_overwrite": setting("AWS_S3_FILE_OVERWRITE", True), "object_parameters": setting("AWS_S3_OBJECT_PARAMETERS", {}), "bucket_name": setting("AWS_STORAGE_BUCKET_NAME"), diff --git a/storages/utils.py b/storages/utils.py index 5eda4deb..cd3668e9 100644 --- a/storages/utils.py +++ b/storages/utils.py @@ -109,6 +109,17 @@ def check_location(storage): ) +def lookup_env(names): + """ + Look up for names in environment. Returns the first element + found. + """ + for name in names: + value = os.environ.get(name) + if value: + return value + + def get_available_overwrite_name(name, max_length): if max_length is None or len(name) <= max_length: return name diff --git a/tests/test_s3.py b/tests/test_s3.py index 36f75b32..a93062fa 100644 --- a/tests/test_s3.py +++ b/tests/test_s3.py @@ -1,6 +1,7 @@ import datetime import gzip import io +import os import pickle import threading from textwrap import dedent @@ -938,6 +939,26 @@ def test_auth_config(self): self.assertEqual(storage.access_key, "foo") self.assertEqual(storage.secret_key, "boo") + with mock.patch.dict( + os.environ, { + "AWS_S3_ACCESS_KEY_ID": "foo", + "AWS_S3_SECRET_ACCESS_KEY": "boo" + } + ): + storage = s3.S3Storage() + self.assertEqual(storage.access_key, "foo") + self.assertEqual(storage.secret_key, "boo") + + with mock.patch.dict( + os.environ, { + "AWS_ACCESS_KEY_ID": "foo", + "AWS_SECRET_ACCESS_KEY": "boo" + } + ): + storage = s3.S3Storage() + self.assertEqual(storage.access_key, "foo") + self.assertEqual(storage.secret_key, "boo") + storage = s3.S3Storage(access_key="foo", secret_key="boo") self.assertEqual(storage.access_key, "foo") self.assertEqual(storage.secret_key, "boo")