Throw a warning if custom secret key was specified but not given (#13128)

* Throw a warning if custom secret key was specified but not given

* Fixing unit tests
This commit is contained in:
John Westcott IV 2022-12-17 12:15:27 -05:00 committed by GitHub
parent e87e041a2a
commit b7f2825909
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -32,8 +32,14 @@ class Command(BaseCommand):
def handle(self, **options):
self.old_key = settings.SECRET_KEY
custom_key = os.environ.get("TOWER_SECRET_KEY")
if options.get("use_custom_key") and custom_key:
self.new_key = custom_key
if options.get("use_custom_key"):
if custom_key:
self.new_key = custom_key
else:
print("Use custom key was specified but the env var TOWER_SECRET_KEY was not available")
import sys
sys.exit(1)
else:
self.new_key = base64.encodebytes(os.urandom(33)).decode().rstrip()
self._notification_templates()

View File

@ -171,13 +171,17 @@ class TestKeyRegeneration:
def test_use_custom_key_with_empty_tower_secret_key_env_var(self):
os.environ['TOWER_SECRET_KEY'] = ''
new_key = call_command('regenerate_secret_key', '--use-custom-key')
assert settings.SECRET_KEY != new_key
with pytest.raises(SystemExit) as e:
call_command('regenerate_secret_key', '--use-custom-key')
assert e.type == SystemExit
assert e.value.code == 1
def test_use_custom_key_with_no_tower_secret_key_env_var(self):
os.environ.pop('TOWER_SECRET_KEY', None)
new_key = call_command('regenerate_secret_key', '--use-custom-key')
assert settings.SECRET_KEY != new_key
with pytest.raises(SystemExit) as e:
call_command('regenerate_secret_key', '--use-custom-key')
assert e.type == SystemExit
assert e.value.code == 1
def test_with_tower_secret_key_env_var(self):
custom_key = 'MXSq9uqcwezBOChl/UfmbW1k4op+bC+FQtwPqgJ1u9XV'