Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENT-7908] - Add management command to re-encrypt enterprise customer reporting configs #1944

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Change Log

Unreleased
----------
Nothing unreleased.
[4.7.3]
--------
feat: added management command to re-encrypt enterprise customer reporting configs

[4.7.2]
--------
Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Your project description goes here.
"""

__version__ = "4.7.2"
__version__ = "4.7.3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Django management command to reencrypt passwords in enterprise custom reporting configs.
"""
import logging

from django.core.management import BaseCommand

from enterprise.models import EnterpriseCustomerReportingConfiguration

LOGGER = logging.getLogger(__name__)


class Command(BaseCommand):
"""
Django management command to reencrypt passwords in enterprise custom reporting configs
It's useful when following encryption keys are rotated
- FERNET_KEYS
- LMS_FERNET_KEY

Example usage:
./manage.py lms reencrypt_enterprise_customer_reporting_config_passwords

"""
def handle(self, *args, **options):
try:
for config in EnterpriseCustomerReportingConfiguration.objects.all():
config.save() # resaving reencrypts all the encrypted columns
LOGGER.info('Enterprise customer reporting configuration passwords reencrypted succesfully!')
except Exception as e: # pylint: disable=broad-except
LOGGER.exception(f'Failed to reencrypt customer reporting configuration passwords. Error: {e}')
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Tests for the djagno management command `reencrypt_enterprise_customer_reporting_config_passwords`.
"""
from testfixtures import LogCapture

from django.core.management import call_command
from django.test import TestCase

from enterprise.models import EnterpriseCustomerReportingConfiguration
from test_utils import factories

LOGGER_NAME = 'enterprise.management.commands.reencrypt_enterprise_customer_reporting_config_passwords'


class ReencryptPasswordsTest(TestCase):
"""
Test command `reencrypt_enterprise_customer_reporting_config_passwords`.
"""
def test_reencrypt_command(self):
# Create an instance of EnterpriseCustomerReportingConfiguration
enterprise_customer = factories.EnterpriseCustomerFactory(name="GriffCo")
original_config = EnterpriseCustomerReportingConfiguration.objects.create(
enterprise_customer=enterprise_customer,
active=True,
delivery_method=EnterpriseCustomerReportingConfiguration.DELIVERY_METHOD_EMAIL,
email='test@edx.org',
decrypted_password='test_password',
day_of_month=1,
hour_of_day=1,
)

# Assert that the password has been encrypted
self.assertNotEqual(original_config.encrypted_password, 'test_password')

with LogCapture(LOGGER_NAME) as log:
call_command('reencrypt_enterprise_customer_reporting_config_passwords')
self.assertEqual(
'Enterprise customer reporting configuration passwords reencrypted succesfully!',
log.records[0].message
)
updated_config = EnterpriseCustomerReportingConfiguration.objects.get(pk=original_config.pk)
# Assert that the password has been reencrypted
self.assertNotEqual(updated_config.encrypted_password, updated_config.encrypted_password)
Loading