From 8d3b9e2ca4fd6137660a04e70a9804bb5d48adbf Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 2 Apr 2021 00:06:17 +1100 Subject: [PATCH] Updates to settings.py - Create secret_key.txt if it does not exist - Copy default settings file if it does not exist --- InvenTree/InvenTree/settings.py | 31 ++++++++++++++++++++++++++++--- tasks.py | 32 +++++++------------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index eee69c0780..3f705ae70c 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -13,6 +13,9 @@ database setup in this file. import logging import os +import random +import string +import shutil import sys import tempfile from datetime import datetime @@ -55,8 +58,10 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) cfg_filename = os.path.join(BASE_DIR, 'config.yaml') if not os.path.exists(cfg_filename): - print("Error: config.yaml not found") - sys.exit(-1) + print("InvenTree configuration file 'config.yaml' not found - creating default file") + + cfg_template = os.path.join(BASE_DIR, "config_template.yaml") + shutil.copyfile(cfg_template, cfg_filename) with open(cfg_filename, 'r') as cfg: CONFIG = yaml.safe_load(cfg) @@ -99,6 +104,17 @@ LOGGING = { # Get a logger instance for this setup file logger = logging.getLogger(__name__) +""" +Specify a secret key to be used by django. + +Following options are tested, in descending order of preference: + +A) Check for environment variable INVENTREE_SECRET_KEY => Use raw key data +B) Check for environment variable INVENTREE_SECRET_KEY_FILE => Load key data from file +C) Look for default key file "secret_key.txt" +d) Create "secret_key.txt" if it does not exist +""" + if os.getenv("INVENTREE_SECRET_KEY"): # Secret key passed in directly SECRET_KEY = os.getenv("INVENTREE_SECRET_KEY").strip() @@ -111,11 +127,20 @@ else: 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") + logger.error(f"Secret key file '{key_file}'' not found") exit(-1) else: # default secret key location key_file = os.path.join(BASE_DIR, "secret_key.txt") + + 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) + logger.info(f"SECRET_KEY loaded from {key_file}") try: SECRET_KEY = open(key_file, "r").read().strip() diff --git a/tasks.py b/tasks.py index 2d7d395d10..83a99949f3 100644 --- a/tasks.py +++ b/tasks.py @@ -3,11 +3,10 @@ from invoke import task from shutil import copyfile -import random -import string import os import sys + def apps(): """ Returns a list of installed apps @@ -27,6 +26,7 @@ def apps(): 'users', ] + def localDir(): """ Returns the directory of *THIS* file. @@ -35,6 +35,7 @@ def localDir(): """ return os.path.dirname(os.path.abspath(__file__)) + def managePyDir(): """ Returns the directory of the manage.py file @@ -42,6 +43,7 @@ def managePyDir(): return os.path.join(localDir(), 'InvenTree') + def managePyPath(): """ Return the path of the manage.py file @@ -49,6 +51,7 @@ def managePyPath(): return os.path.join(managePyDir(), 'manage.py') + def manage(c, cmd, pty=False): """ Runs a given command against django's "manage.py" script. @@ -63,32 +66,11 @@ def manage(c, cmd, pty=False): cmd=cmd ), pty=pty) -@task(help={'length': 'Length of secret key (default=50)'}) -def key(c, length=50, force=False): - """ - Generates a SECRET_KEY file which InvenTree uses for generating security hashes - """ - SECRET_KEY_FILE = os.path.join(localDir(), 'InvenTree', 'secret_key.txt') - - # If a SECRET_KEY file does not exist, generate a new one! - if force or not os.path.exists(SECRET_KEY_FILE): - print("Generating SECRET_KEY file - " + SECRET_KEY_FILE) - with open(SECRET_KEY_FILE, 'w') as key_file: - options = string.digits + string.ascii_letters + string.punctuation - - key = ''.join([random.choice(options) for i in range(length)]) - - key_file.write(key) - - else: - print("SECRET_KEY file already exists - skipping") - - -@task(post=[key]) +@task def install(c): """ - Installs required python packages, and runs initial setup functions. + Installs required python packages """ # Install required Python packages with PIP