Skip to content

Commit 4c8ecaa

Browse files
committed
fix(storages): use ssec storage at correct times
fix document clone storage usage
1 parent fea6b20 commit 4c8ecaa

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

alexandria/core/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.contrib.postgres.search import SearchVectorField
1010
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
1111
from django.core.files import File as DjangoFile
12+
from django.core.files.storage import storages
1213
from django.core.validators import RegexValidator
1314
from django.db import models, transaction
1415
from django.dispatch import receiver
@@ -172,7 +173,10 @@ def clone(self):
172173
self.pk = None
173174
self.save()
174175

175-
storage = File.content.field.storage
176+
storage_backend = settings.ALEXANDRIA_FILE_STORAGE
177+
if settings.ALEXANDRIA_ENABLE_AT_REST_ENCRYPTION:
178+
storage_backend = "alexandria.storages.backends.s3.SsecGlobalS3Storage"
179+
storage = storages.create_storage({"BACKEND": storage_backend})
176180
latest_original.pk = None
177181
latest_original.document = self
178182
if isinstance(storage, S3Storage):

alexandria/core/tests/test_models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ def test_document_no_files(
5454

5555

5656
def test_clone_document_s3(db, mocker, settings, file_factory):
57-
settings.ALEXANDRIA_FILE_STORAGE = (
58-
"alexandria.storages.backends.s3.SsecGlobalS3Storage"
59-
)
57+
settings.ALEXANDRIA_FILE_STORAGE = "alexandria.storages.backends.s3.S3Storage"
6058
settings.ALEXANDRIA_ENABLE_AT_REST_ENCRYPTION = True
6159
name = "name-of-the-file"
6260
mocker.patch("storages.backends.s3.S3Storage.save", return_value=name)

alexandria/storages/fields.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44
from django.db.models.fields.files import FieldFile
55
from storages.backends.s3 import S3Storage
66

7-
from alexandria.storages.backends.s3 import SsecGlobalS3Storage
8-
97

108
class DynamicStorageFieldFile(FieldFile):
119
def __init__(self, instance, field, name):
1210
super().__init__(instance, field, name)
13-
self.storage = storages.create_storage(
14-
{"BACKEND": settings.ALEXANDRIA_FILE_STORAGE}
15-
)
11+
storage_backend = settings.ALEXANDRIA_FILE_STORAGE
1612
if settings.ALEXANDRIA_ENABLE_AT_REST_ENCRYPTION:
1713
from alexandria.core.models import File
1814

1915
if instance.encryption_status == File.EncryptionStatus.SSEC_GLOBAL_KEY:
20-
self.storage = SsecGlobalS3Storage()
16+
storage_backend = "alexandria.storages.backends.s3.SsecGlobalS3Storage"
17+
self.storage = storages.create_storage({"BACKEND": storage_backend})
2118

2219

2320
class DynamicStorageFileField(models.FileField):
@@ -56,8 +53,9 @@ def pre_save(self, instance, add):
5653
"Set `ALEXANDRIA_FILE_STORAGE` to `alexandria.storages.s3.S3Storage`."
5754
)
5855
raise ImproperlyConfigured(msg)
59-
storage = SsecGlobalS3Storage()
6056
if instance.encryption_status == File.EncryptionStatus.SSEC_GLOBAL_KEY:
61-
self.storage = storage
57+
self.storage = storages.create_storage(
58+
{"BACKEND": "alexandria.storages.backends.s3.SsecGlobalS3Storage"}
59+
)
6260
_file = super().pre_save(instance, add)
6361
return _file

alexandria/storages/tests/test_dynamic_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_dynamic_storage_select_global_ssec(
2727
mocker.patch("alexandria.core.tasks.create_thumbnail.delay", side_effect=None)
2828
if raises is not None:
2929
with pytest.raises(raises):
30-
file_factory()
30+
file_factory(encryption_status=settings.ALEXANDRIA_ENCRYPTION_METHOD)
3131
return
3232
file_factory(encryption_status=settings.ALEXANDRIA_ENCRYPTION_METHOD)
3333
assert SsecGlobalS3Storage.save.called_once()

0 commit comments

Comments
 (0)