From 9a00239a86983c49aa731dcb88a35c911a73f3bb Mon Sep 17 00:00:00 2001 From: catborise Date: Mon, 9 Nov 2026 11:26:56 +0300 Subject: [PATCH] fix(settings): an on/off variable is 1 or 0; another value stops the start WEBVIRTCLOUD_DEBUG, WEBVIRTCLOUD_HTTPS, WEBVIRTCLOUD_SERIAL_CONSOLE and EMAIL_USE_TLS compared their value with "1": EMAIL_USE_TLS=true turned STARTTLS off, and WEBVIRTCLOUD_HTTPS=true left the cookies insecure, without a word. env_flag takes 1 or 0 (unset is the default, empty is 0 as before) and raises ImproperlyConfigured, naming the variable, on anything else. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- README.md | 4 +++- webvirtcloud/settings.py.template | 19 +++++++++++++++---- webvirtcloud/tests/test_settings_env.py | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5e95ceef..724c8057 100644 --- a/README.md +++ b/README.md @@ -343,11 +343,13 @@ If libvirt warns `Host SMBIOS information is not available`, install `dmidecode` | `WEBVIRTCLOUD_SERIAL_CONSOLE=1` | serial console, see [Serial console](#serial-console) | | `WEBVIRTCLOUD_DEBUG=1` | Django debug mode: error pages show the traceback and settings; for trying things out only, never on a panel others use | | `WEBVIRTCLOUD_DB_HOST` | use PostgreSQL instead of sqlite (new installs); `WEBVIRTCLOUD_DB_PORT`/`_NAME`/`_USER`/`_PASSWORD` default to 5432/webvirtcloud/webvirtcloud/empty | -| `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS` | SMTP for e-mail OTP | +| `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS` | SMTP for e-mail OTP; `EMAIL_USE_TLS` is 1 (STARTTLS, the default) or 0 | | `WEBVIRTCLOUD_LOG_FILE` | log file (default `data/webvirtcloud.log`, rotated at 10 MB × 5) | | `SECRET_KEY` | Django secret key (default: the `data/secret_key` file) | | `ADMIN_USERNAME`, `ADMIN_PASSWORD` | the first superuser, read by the first `migrate` only, see [First login](#first-login) | +The on/off variables take `1` or `0`; any other value, such as `true`, stops the panel at start with an error naming the variable. + Where to set them: - **Docker:** the `environment:` list of `docker-compose.override.yml`; every process in the container gets it. diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index a4b0e8ea..6504aac8 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -11,9 +11,20 @@ try: HAS_LDAP = True except ImportError: HAS_LDAP = False +from django.core.exceptions import ImproperlyConfigured from django.utils.translation import gettext_lazy as _ from pathlib import Path + +def env_flag(name, default="0"): + """An on/off environment variable: 1 or 0 (unset: the default; empty: 0, as + before). Another value stops the start, so that "true" does not quietly mean off.""" + value = os.getenv(name, default) or "0" + if value not in ("0", "1"): + raise ImproperlyConfigured(f"{name} must be 1 or 0, not {value!r}") + return value == "1" + + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -27,7 +38,7 @@ if not SECRET_KEY: pass # WEBVIRTCLOUD_DEBUG=1 shows error details on the pages: for trying things out only -DEBUG = os.getenv("WEBVIRTCLOUD_DEBUG") == "1" +DEBUG = env_flag("WEBVIRTCLOUD_DEBUG") MAC_OUI = '52:54:10' @@ -47,7 +58,7 @@ SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") # Production profile for a panel served over HTTPS: secure cookies, HTTP to # HTTPS redirect and HSTS. Off by default (plain HTTP). -if os.getenv("WEBVIRTCLOUD_HTTPS") == "1": +if env_flag("WEBVIRTCLOUD_HTTPS"): SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True SECURE_SSL_REDIRECT = True @@ -310,7 +321,7 @@ WS_HOST = "127.0.0.1" # Serial console (xterm.js over socketiod) for VMs without graphics. socketiod # accepts only the console page's signed token (as noVNC does); while this is # False it refuses every connection. Restart socketiod after changing it. -SERIAL_CONSOLE_ENABLED = os.getenv("WEBVIRTCLOUD_SERIAL_CONSOLE") == "1" +SERIAL_CONSOLE_ENABLED = env_flag("WEBVIRTCLOUD_SERIAL_CONSOLE") SOCKETIO_PORT = 6081 # Listen on localhost only; the console page reaches socketiod through the @@ -412,7 +423,7 @@ SPECTACULAR_SETTINGS = { # EMAIL Config (SMTP; override through the environment) EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = os.getenv("EMAIL_HOST", "smtp.gmail.com") -EMAIL_USE_TLS = os.getenv("EMAIL_USE_TLS", "1") == "1" +EMAIL_USE_TLS = env_flag("EMAIL_USE_TLS", "1") EMAIL_PORT = int(os.getenv("EMAIL_PORT", "587")) ## sender's email-id EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER", "") diff --git a/webvirtcloud/tests/test_settings_env.py b/webvirtcloud/tests/test_settings_env.py index f27d9afd..7005a36e 100644 --- a/webvirtcloud/tests/test_settings_env.py +++ b/webvirtcloud/tests/test_settings_env.py @@ -12,7 +12,7 @@ from django.test import SimpleTestCase, TestCase, override_settings # Env vars that change the settings; removed from the inherited environment so # every case starts from the defaults. CONFIG_VARS = [ - "WEBVIRTCLOUD_HTTPS", "WEBVIRTCLOUD_DEBUG", "ALLOWED_HOSTS", + "WEBVIRTCLOUD_HTTPS", "WEBVIRTCLOUD_DEBUG", "WEBVIRTCLOUD_SERIAL_CONSOLE", "ALLOWED_HOSTS", "EMAIL_HOST", "EMAIL_PORT", "EMAIL_HOST_USER", "EMAIL_HOST_PASSWORD", "EMAIL_USE_TLS", "WEBVIRTCLOUD_DB_HOST", "WEBVIRTCLOUD_DB_PORT", "WEBVIRTCLOUD_DB_NAME", "WEBVIRTCLOUD_DB_USER", "WEBVIRTCLOUD_DB_PASSWORD", @@ -142,3 +142,21 @@ class DebugEnvTestCase(SimpleTestCase): def test_debug_loads_the_urls(self): run_manage({"WEBVIRTCLOUD_DEBUG": "1"}, "check") + + +class EnvFlagTestCase(SimpleTestCase): + FLAGS = {"WEBVIRTCLOUD_DEBUG": "DEBUG", "WEBVIRTCLOUD_HTTPS": "SESSION_COOKIE_SECURE", + "WEBVIRTCLOUD_SERIAL_CONSOLE": "SERIAL_CONSOLE_ENABLED", "EMAIL_USE_TLS": "EMAIL_USE_TLS"} + + def test_a_flag_is_1_or_0_unset_is_its_default_and_empty_is_off(self): + for var, name in self.FLAGS.items(): + with self.subTest(var): + self.assertTrue(load_settings({var: "1"}, [name])[name]) + self.assertFalse(load_settings({var: "0"}, [name])[name]) + self.assertFalse(load_settings({var: ""}, [name])[name]) + self.assertEqual(load_settings({}, [name])[name], var == "EMAIL_USE_TLS") + + def test_another_value_stops_the_start(self): + for var in self.FLAGS: + with self.subTest(var), self.assertRaisesRegex(AssertionError, f"{var} must be 1 or 0, not 'true'"): + load_settings({var: "true"}, ["DEBUG"])