Auto-generate key file if it does not exist!

This commit is contained in:
Oliver Walters 2021-04-10 19:01:02 +10:00
parent 9086c8a3bf
commit 5e54b0f5cf
2 changed files with 13 additions and 14 deletions

View File

@ -138,24 +138,22 @@ else:
key_file = os.getenv("INVENTREE_SECRET_KEY_FILE") key_file = os.getenv("INVENTREE_SECRET_KEY_FILE")
if key_file: if key_file:
if os.path.isfile(key_file): key_file = os.path.abspath(key_file)
logger.info("SECRET_KEY loaded by INVENTREE_SECRET_KEY_FILE")
else:
logger.error(f"Secret key file '{key_file}'' not found")
exit(-1)
else: else:
# default secret key location # default secret key location
key_file = os.path.join(BASE_DIR, "secret_key.txt") key_file = os.path.join(BASE_DIR, "secret_key.txt")
key_file = os.path.abspath(key_file)
if not os.path.exists(key_file): if not os.path.exists(key_file):
logger.info("Creating key file 'secret_key.txt'") logger.info(f"Generating random key file at '{key_file}'")
# Create a random key file # Create a random key file
with open(key_file, 'w') as f: with open(key_file, 'w') as f:
options = string.digits + string.ascii_letters + string.punctuation options = string.digits + string.ascii_letters + string.punctuation
key = ''.join([random.choice(options) for i in range(50)]) key = ''.join([random.choice(options) for i in range(100)])
f.write(key) f.write(key)
logger.info(f"SECRET_KEY loaded from {key_file}") logger.info(f"Loading SECRET_KEY from '{key_file}'")
try: try:
SECRET_KEY = open(key_file, "r").read().strip() SECRET_KEY = open(key_file, "r").read().strip()
except Exception: except Exception:

View File

@ -20,11 +20,12 @@ ENV INVENTREE_SRC_DIR="${INVENTREE_HOME}/src"
ENV INVENTREE_MNG_DIR="${INVENTREE_SRC_DIR}/InvenTree" ENV INVENTREE_MNG_DIR="${INVENTREE_SRC_DIR}/InvenTree"
ENV INVENTREE_DATA_DIR="${INVENTREE_HOME}/data" ENV INVENTREE_DATA_DIR="${INVENTREE_HOME}/data"
ENV INVENTREE_STATIC_ROOT="${INVENTREE_DATA_DIR}/static" ENV INVENTREE_STATIC_ROOT="${INVENTREE_DATA_DIR}/static"
ENV INVENTREE_MEDIA_ROOT="${INVENTREE_MEDIA_DIR}/media" ENV INVENTREE_MEDIA_ROOT="${INVENTREE_DATA_DIR}/media"
ENV INVENTREE_BACKUP_DIR="${INVENTREE_DATA_DIR}/backup" ENV INVENTREE_BACKUP_DIR="${INVENTREE_DATA_DIR}/backup"
ENV INVENTREE_VENV="${INVENTREE_HOME}/env" ENV INVENTREE_VENV="${INVENTREE_HOME}/env"
ENV INVENTREE_CONFIG_FILE="${INVENTREE_DATA_DIR}/config.yaml" ENV INVENTREE_CONFIG_FILE="${INVENTREE_DATA_DIR}/config.yaml"
ENV INVENTREE_SECRET_KEY_FILE="${INVENTREE_DATA_DIR}/secret_key.txt"
# Pass DB configuration through as environment variables # Pass DB configuration through as environment variables
ENV INVENTREE_DB_ENGINE="${INVENTREE_DB_ENGINE}" ENV INVENTREE_DB_ENGINE="${INVENTREE_DB_ENGINE}"