From 3a32e524f8cef87367ad07773c0966dbfce1f707 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Mon, 26 Nov 2018 14:50:14 +0100 Subject: [PATCH] Storage/S3: Truncate bucket hash if necessary --- storage_s3/indico_storage_s3/storage.py | 2 +- storage_s3/tests/plugin_test.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/storage_s3/indico_storage_s3/storage.py b/storage_s3/indico_storage_s3/storage.py index 181d771..bc7ad5e 100644 --- a/storage_s3/indico_storage_s3/storage.py +++ b/storage_s3/indico_storage_s3/storage.py @@ -223,7 +223,7 @@ class DynamicS3Storage(S3StorageBase): def _get_bucket_name(self, date): name = self._replace_bucket_placeholders(self.bucket_name_template, date) token = hmac.new(self.bucket_secret.encode('utf-8'), name, hashlib.md5).hexdigest() - return '{}-{}'.format(name, token) + return '{}-{}'.format(name, token)[:63] def _replace_bucket_placeholders(self, name, date): name = name.replace('', date.strftime('%Y')) diff --git a/storage_s3/tests/plugin_test.py b/storage_s3/tests/plugin_test.py index 9928db5..994e147 100644 --- a/storage_s3/tests/plugin_test.py +++ b/storage_s3/tests/plugin_test.py @@ -102,8 +102,10 @@ def test_static_bucket_name_too_long(): def test_dynamic_bucket_name_too_long(): - DynamicS3Storage('bucket_secret=secret,bucket_template=test-' + 'x'*37) - DynamicS3Storage('bucket_secret=secret,bucket_template=test--' + 'x'*34) + s = DynamicS3Storage('bucket_secret=secret,bucket_template=test-' + 'x'*37) + assert len(s._get_current_bucket_name()) == 63 + s = DynamicS3Storage('bucket_secret=secret,bucket_template=test--' + 'x'*34) + assert len(s._get_current_bucket_name()) == 63 with pytest.raises(StorageError): DynamicS3Storage('bucket_secret=secret,bucket_template=test-' + 'x' * 38) with pytest.raises(StorageError):