Skip to content

Commit 9c33cd5

Browse files
committed
Rename TOMTOOLKIT_FIELD_ENCRYPTION_KEY to TOMTOOLKIT_DEK_ENCRYPTION_KEY
Naming Rationale: The new name is more descriptive because in the "envelope encryption" scheme, the TOMTOOLKIT_DEK_ENCRYPTION_KEY is the encryption key that encrypts and decrypts the user's DEK (data encryption key). So it's the user-DEK encryptiion key for TOM Toolkit.
1 parent d4b1064 commit 9c33cd5

7 files changed

Lines changed: 18 additions & 18 deletions

File tree

tom_base/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
# Encryption key for protecting sensitive user data (API keys, credentials) at rest.
3535
# This is a Fernet key — a 44-character URL-safe base64 string encoding 32 random bytes.
3636
# Treat this like SECRET_KEY. See the TOM Toolkit encryption documentation.
37-
TOMTOOLKIT_FIELD_ENCRYPTION_KEY = os.getenv(
38-
'TOMTOOLKIT_FIELD_ENCRYPTION_KEY',
37+
TOMTOOLKIT_DEK_ENCRYPTION_KEY = os.getenv(
38+
'TOMTOOLKIT_DEK_ENCRYPTION_KEY',
3939
'UlUYyKsGzQVwjpTbvhtgCihKaj07H1voc-V4pmb7NN4=') # 44-char URL-safe base64 string
4040

4141
ALLOWED_HOSTS = ['']

tom_common/management/commands/rotate_field_encryption_key.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Management command to rotate the TOMTOOLKIT_FIELD_ENCRYPTION_KEY.
1+
"""Management command to rotate the TOMTOOLKIT_DEK_ENCRYPTION_KEY.
22
33
This is a thin CLI wrapper around ``session_utils.rotate_master_key()``.
44
See that function for the actual rotation logic.
@@ -21,7 +21,7 @@
2121
class Command(BaseCommand):
2222
help = (
2323
'Re-encrypts all per-user Data Encryption Keys (DEKs) with a new master key. '
24-
'Run this when rotating TOMTOOLKIT_FIELD_ENCRYPTION_KEY.'
24+
'Run this when rotating TOMTOOLKIT_DEK_ENCRYPTION_KEY.'
2525
)
2626

2727
def add_arguments(self, parser) -> None:
@@ -68,6 +68,6 @@ def handle(self, *args, **options) -> None:
6868

6969
self.stdout.write("")
7070
self.stdout.write(self.style.WARNING(
71-
"IMPORTANT: Update TOMTOOLKIT_FIELD_ENCRYPTION_KEY in your environment / "
71+
"IMPORTANT: Update TOMTOOLKIT_DEK_ENCRYPTION_KEY in your environment / "
7272
"settings.py with the new key, then restart the server."
7373
))

tom_common/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
TOM Toolkit uses envelope encryption to protect sensitive user data (API keys,
66
observatory credentials) at rest in the database. The scheme has two layers:
77
8-
1. A server-side **master key** (``TOMTOOLKIT_FIELD_ENCRYPTION_KEY``) is stored in the
8+
1. A server-side **master key** (``TOMTOOLKIT_DEK_ENCRYPTION_KEY``) is stored in the
99
environment, never in the database. It is a Fernet key used to encrypt
1010
per-user keys.
1111

tom_common/session_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
``docs/design/encryption_architecture_redesign.md``; here is a brief summary
66
of how the pieces fit together:
77
8-
**Master key** (``TOMTOOLKIT_FIELD_ENCRYPTION_KEY`` in settings / environment):
8+
**Master key** (``TOMTOOLKIT_DEK_ENCRYPTION_KEY`` in settings / environment):
99
A Fernet key that never touches the database. It encrypts each user's
1010
Data Encryption Key so that database access alone cannot reveal user data.
1111
@@ -54,19 +54,19 @@
5454
def _get_master_cipher() -> Fernet:
5555
"""Return a Fernet cipher built from the server-side master key.
5656
57-
The master key (``TOMTOOLKIT_FIELD_ENCRYPTION_KEY``) lives in the server
57+
The master key (``TOMTOOLKIT_DEK_ENCRYPTION_KEY``) lives in the server
5858
environment, not in the database. It is used only to encrypt and decrypt
5959
per-user DEKs — never to encrypt user data directly.
6060
6161
Raises:
6262
django.core.exceptions.ImproperlyConfigured: If the setting is missing
6363
or empty.
6464
"""
65-
key = getattr(settings, 'TOMTOOLKIT_FIELD_ENCRYPTION_KEY', '')
65+
key = getattr(settings, 'TOMTOOLKIT_DEK_ENCRYPTION_KEY', '')
6666
if not key:
6767
from django.core.exceptions import ImproperlyConfigured
6868
raise ImproperlyConfigured(
69-
"TOMTOOLKIT_FIELD_ENCRYPTION_KEY is not set. This setting is required for "
69+
"TOMTOOLKIT_DEK_ENCRYPTION_KEY is not set. This setting is required for "
7070
"encrypting sensitive user data at rest. Generate one with:\n"
7171
" python -c \"from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())\"\n"
7272
"Then add it to your environment or settings.py."
@@ -261,13 +261,13 @@ def rotate_master_key(new_key: str) -> RotationResult:
261261
"""Re-encrypt all per-user DEKs with a new master key.
262262
263263
Each Profile's ``encrypted_dek`` is decrypted with the current master key
264-
(from ``TOMTOOLKIT_FIELD_ENCRYPTION_KEY``) and re-encrypted with
264+
(from ``TOMTOOLKIT_DEK_ENCRYPTION_KEY``) and re-encrypted with
265265
``new_key``. The user Profile's plaintext DEK is unchanged — only its
266266
encryption layer (i.e. `encrypted_dek`) is replaced. The actual encrypted
267267
data is not touched.
268268
269269
After this function completes successfully, the server's
270-
``TOMTOOLKIT_FIELD_ENCRYPTION_KEY`` must be updated to ``new_key`` and the
270+
``TOMTOOLKIT_DEK_ENCRYPTION_KEY`` must be updated to ``new_key`` and the
271271
server restarted. Until that happens, the re-encrypted DEKs cannot be
272272
decrypted.
273273

tom_common/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ class TestEncryptionKeyManagement(TestCase):
381381
- Users get an encrypted DEK on creation (via signal)
382382
- The DEK can be decrypted and used to encrypt/decrypt data
383383
- Password changes do not affect the encryption key
384-
- The master key (TOMTOOLKIT_FIELD_ENCRYPTION_KEY) is required for decryption
384+
- The master key (TOMTOOLKIT_DEK_ENCRYPTION_KEY) is required for decryption
385385
"""
386386
def setUp(self):
387387
self.user = User.objects.create_user(
@@ -476,7 +476,7 @@ def test_create_encrypted_dek_produces_valid_encrypted_key(self):
476476

477477
def test_master_key_required_for_decryption(self):
478478
"""Decrypting with a different master key should fail, proving
479-
that the encrypted DEK is bound to TOMTOOLKIT_FIELD_ENCRYPTION_KEY."""
479+
that the encrypted DEK is bound to TOMTOOLKIT_DEK_ENCRYPTION_KEY."""
480480
profile = Profile.objects.get(user=self.user)
481481
wrong_key = Fernet.generate_key()
482482
wrong_cipher = Fernet(wrong_key)

tom_setup/management/commands/tom_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def generate_secret_key(self):
216216

217217
def generate_field_encryption_key(self):
218218
self.status('Generating field encryption key... ')
219-
self.context['TOMTOOLKIT_FIELD_ENCRYPTION_KEY'] = Fernet.generate_key().decode()
219+
self.context['TOMTOOLKIT_DEK_ENCRYPTION_KEY'] = Fernet.generate_key().decode()
220220
self.ok()
221221

222222
def generate_config(self):

tom_setup/templates/tom_setup/settings.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ SECRET_KEY = '{{ SECRET_KEY }}'
2828
# Encryption key for protecting sensitive user data (API keys, credentials) at rest.
2929
# This is a Fernet key — a 44-character URL-safe base64 string encoding 32 random bytes.
3030
# Treat this like SECRET_KEY. See the TOM Toolkit encryption documentation.
31-
TOMTOOLKIT_FIELD_ENCRYPTION_KEY = os.getenv(
32-
'TOMTOOLKIT_FIELD_ENCRYPTION_KEY',
33-
'{{ TOMTOOLKIT_FIELD_ENCRYPTION_KEY }}') # 44-char URL-safe base64 string
31+
TOMTOOLKIT_DEK_ENCRYPTION_KEY = os.getenv(
32+
'TOMTOOLKIT_DEK_ENCRYPTION_KEY',
33+
'{{ TOMTOOLKIT_DEK_ENCRYPTION_KEY }}') # 44-char URL-safe base64 string
3434

3535
# SECURITY WARNING: don't run with debug turned on in production!
3636
DEBUG = True

0 commit comments

Comments
 (0)