Skip to content

Commit

Permalink
[s3] fix disabling cloudfront signing with cloudfront_signer=None
Browse files Browse the repository at this point in the history
This change fixes disabling cloudfront signing by specifying "None" (or
another falsy value). This allows multiple storage configs that are all
distributed via cloudfront, but only some of the configs need to be
signed.
  • Loading branch information
terencehonles committed Oct 16, 2023
1 parent a13b0ec commit 1d46c04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions storages/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ class S3Storage(CompressStorageMixin, BaseStorage):
config = None

def __init__(self, **settings):
self.cloudfront_signer = settings.pop("cloudfront_signer", None)
omitted = object()
self.cloudfront_signer = settings.pop("cloudfront_signer", omitted)

super().__init__(**settings)

Expand Down Expand Up @@ -333,7 +334,7 @@ def __init__(self, **settings):
if self.transfer_config is None:
self.transfer_config = TransferConfig(use_threads=self.use_threads)

if not self.cloudfront_signer:
if self.cloudfront_signer is omitted:
if self.cloudfront_key_id and self.cloudfront_key:
self.cloudfront_signer = self.get_cloudfront_signer(
self.cloudfront_key_id, self.cloudfront_key
Expand All @@ -343,6 +344,8 @@ def __init__(self, **settings):
"Both AWS_CLOUDFRONT_KEY_ID/cloudfront_key_id and "
"AWS_CLOUDFRONT_KEY/cloudfront_key must be provided together."
)
else:
self.cloudfront_signer = None

def get_cloudfront_signer(self, key_id, key):
return _cloud_front_signer_from_pem(key_id, key)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ def test_cloudfront_config(self):
storage = s3.S3Storage()
self.assertIsNotNone(storage.cloudfront_signer)

# allow disabling cloudfront signing
storage = s3.S3Storage(cloudfront_signer=None)
self.assertIsNone(storage.cloudfront_signer)

storage = s3.S3Storage(cloudfront_key_id=key_id, cloudfront_key=pem)
self.assertIsNotNone(storage.cloudfront_signer)

Expand Down

0 comments on commit 1d46c04

Please sign in to comment.