From 5e54b0f5cfc9b628451c23d30faaf89abb051eed Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 10 Apr 2021 19:01:02 +1000 Subject: [PATCH] Auto-generate key file if it does not exist! --- InvenTree/InvenTree/settings.py | 24 +++++++++++------------- docker/Dockerfile | 3 ++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 5e259a66e6..fe0164af94 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -138,24 +138,22 @@ else: key_file = os.getenv("INVENTREE_SECRET_KEY_FILE") if key_file: - if os.path.isfile(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) + key_file = os.path.abspath(key_file) else: # default secret key location key_file = os.path.join(BASE_DIR, "secret_key.txt") + key_file = os.path.abspath(key_file) - if not os.path.exists(key_file): - logger.info("Creating key file 'secret_key.txt'") - # Create a random key file - with open(key_file, 'w') as f: - options = string.digits + string.ascii_letters + string.punctuation - key = ''.join([random.choice(options) for i in range(50)]) - f.write(key) + if not os.path.exists(key_file): + logger.info(f"Generating random key file at '{key_file}'") + # Create a random key file + with open(key_file, 'w') as f: + options = string.digits + string.ascii_letters + string.punctuation + key = ''.join([random.choice(options) for i in range(100)]) + f.write(key) - logger.info(f"SECRET_KEY loaded from {key_file}") + logger.info(f"Loading SECRET_KEY from '{key_file}'") + try: SECRET_KEY = open(key_file, "r").read().strip() except Exception: diff --git a/docker/Dockerfile b/docker/Dockerfile index 89af08f835..1fd9329d69 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -20,11 +20,12 @@ ENV INVENTREE_SRC_DIR="${INVENTREE_HOME}/src" ENV INVENTREE_MNG_DIR="${INVENTREE_SRC_DIR}/InvenTree" ENV INVENTREE_DATA_DIR="${INVENTREE_HOME}/data" 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_VENV="${INVENTREE_HOME}/env" 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 ENV INVENTREE_DB_ENGINE="${INVENTREE_DB_ENGINE}"