This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/security/secret.py
2019-11-12 11:09:47 -05:00

28 lines
762 B
Python

import itertools
import uuid
def convert_secret_key(config_secret_key):
""" Converts the secret key from the app config into a secret key that is usable by AES
Cipher. """
secret_key = None
# First try parsing the key as an int.
try:
big_int = int(config_secret_key)
secret_key = str(bytearray.fromhex('{:02x}'.format(big_int)))
except ValueError:
pass
# Next try parsing it as an UUID.
if secret_key is None:
try:
secret_key = uuid.UUID(config_secret_key).bytes
except ValueError:
pass
if secret_key is None:
secret_key = str(bytearray(map(ord, config_secret_key)))
# Otherwise, use the bytes directly.
assert len(secret_key)
return ''.join(itertools.islice(itertools.cycle(secret_key), 32))