From abf389aaa1b6aa4a800e76a48b6cf94514e312b1 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Mon, 15 Nov 2021 12:54:07 -0800 Subject: [PATCH] main: Drop initializing Django when listing dependencies - Avoid any potential problems with sqlite3 not being present. - Avoid any potential problems with secret key not being available. Avoid accidentally creating the secret key hardware/container images. Tests: - --list-dependencies works with and without secret key and plinth sqlite3. - --list-dependencies works with plinth and root users. - --list-dependencies does not create the sqlite3 database or the secret key. - Running app works in normal mode and creates the secret key. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/__main__.py | 3 --- plinth/web_framework.py | 9 +++------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/plinth/__main__.py b/plinth/__main__.py index 4ab44f5c0..500250a59 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -108,9 +108,6 @@ def main(): if arguments.list_dependencies is not False: log.default_level = 'ERROR' - web_framework.init(read_only=True) - module_loader.include_urls() - menu.init() module_loader.load_modules() module_loader.apps_init() list_dependencies(arguments.list_dependencies) diff --git a/plinth/web_framework.py b/plinth/web_framework.py index 678012688..9fc8b6433 100644 --- a/plinth/web_framework.py +++ b/plinth/web_framework.py @@ -20,7 +20,7 @@ from . import cfg, glib, log, module_loader, settings logger = logging.getLogger(__name__) -def init(read_only=False): +def init(): """Setup Django configuration in the absence of .settings file""" # Workaround for django-simple-captcha 0.5.6 not being compatible with # Django 3.2. 0.5.14 is almost there in Debian. Workaround only until then. @@ -40,7 +40,7 @@ def init(read_only=False): settings.LANGUAGES = get_languages() settings.LOGGING = log.get_configuration() settings.MESSAGE_TAGS = {message_constants.ERROR: 'danger'} - settings.SECRET_KEY = _get_secret_key(read_only) + settings.SECRET_KEY = _get_secret_key() settings.SESSION_FILE_PATH = os.path.join(cfg.data_dir, 'sessions') settings.STATIC_URL = '/'.join([cfg.server_dir, 'static/']).replace('//', '/') @@ -69,7 +69,7 @@ def post_init(): glib.schedule(24 * 3600, _cleanup_expired_sessions, in_thread=True) -def _get_secret_key(read_only=False): +def _get_secret_key(): """Retrieve or create a new Django secret key.""" secret_key_file = pathlib.Path(cfg.data_dir) / 'django-secret.key' if secret_key_file.exists(): @@ -77,9 +77,6 @@ def _get_secret_key(read_only=False): if len(secret_key) >= 128: return secret_key - if read_only: - return '' - secret_key = _generate_secret_key() # File should be created with permission 0o700 old_umask = os.umask(0o077)