From f83a33b9729225a4f3115f81ace8e82050105ea7 Mon Sep 17 00:00:00 2001 From: AnsibleGuy Date: Sun, 3 Mar 2024 09:59:34 +0100 Subject: [PATCH] allowing to change listen address --- CHANGELOG.md | 1 + docs/source/usage/4_config.rst | 5 +++ src/ansibleguy-webui/aw/config/defaults.py | 1 + src/ansibleguy-webui/aw/config/environment.py | 1 + .../aw/migrations/0003_v0_0_14.py | 38 +++++++++++++++++++ src/ansibleguy-webui/aw/settings.py | 9 +++++ src/ansibleguy-webui/webserver.py | 3 +- 7 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/ansibleguy-webui/aw/migrations/0003_v0_0_14.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f93959..c54b02a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * SQLite connection optimizations * Database version-upgrade enhancements +* Allow to change listen address ---- diff --git a/docs/source/usage/4_config.rst b/docs/source/usage/4_config.rst index 9348754..28b8ecf 100644 --- a/docs/source/usage/4_config.rst +++ b/docs/source/usage/4_config.rst @@ -151,6 +151,11 @@ Some settings are only available as environmental variables. Port to listen on. Default: :code:`8000` +* **AW_LISTEN** + + IP Address to listen on. Default: :code:`127.0.0.1` + + * **AW_SSL_CERT** Optionally provide the path to a ssl certificate to use. Use a (full-)chain if not self-signed. diff --git a/src/ansibleguy-webui/aw/config/defaults.py b/src/ansibleguy-webui/aw/config/defaults.py index bfdc370..aa70df3 100644 --- a/src/ansibleguy-webui/aw/config/defaults.py +++ b/src/ansibleguy-webui/aw/config/defaults.py @@ -36,6 +36,7 @@ def _get_defaults_docker(var: str) -> any: # need to be referenced multiple times without import dependencies CONFIG_DEFAULTS = { 'port': 8000, + 'address': '127.0.0.1', 'run_timeout': 3600, 'path_run': '/tmp/ansible-webui', 'path_play': getcwd(), diff --git a/src/ansibleguy-webui/aw/config/environment.py b/src/ansibleguy-webui/aw/config/environment.py index 3042271..a5dbfb1 100644 --- a/src/ansibleguy-webui/aw/config/environment.py +++ b/src/ansibleguy-webui/aw/config/environment.py @@ -5,6 +5,7 @@ AW_ENV_VARS = { 'port': ['AW_PORT'], + 'address': ['AW_LISTEN', 'AW_LISTEN_ADDRESS'], 'timezone': ['AW_TIMEZONE'], 'secret': ['AW_SECRET'], 'path_run': ['AW_PATH_RUN'], diff --git a/src/ansibleguy-webui/aw/migrations/0003_v0_0_14.py b/src/ansibleguy-webui/aw/migrations/0003_v0_0_14.py new file mode 100644 index 0000000..6f87267 --- /dev/null +++ b/src/ansibleguy-webui/aw/migrations/0003_v0_0_14.py @@ -0,0 +1,38 @@ +# Generated by Django 5.0.2 on 2024-03-03 08:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("aw", "0002_v0_0_13"), + ] + + operations = [ + migrations.CreateModel( + name="SchemaMetadata", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created", models.DateTimeField(auto_now_add=True)), + ("updated", models.DateTimeField(auto_now=True)), + ("schema_version", models.CharField(max_length=50)), + ( + "schema_version_prev", + models.CharField( + blank=True, default=None, max_length=50, null=True + ), + ), + ], + options={ + "abstract": False, + }, + ), + ] diff --git a/src/ansibleguy-webui/aw/settings.py b/src/ansibleguy-webui/aw/settings.py index 0dc3554..ee24e98 100644 --- a/src/ansibleguy-webui/aw/settings.py +++ b/src/ansibleguy-webui/aw/settings.py @@ -130,12 +130,21 @@ def debug_mode() -> bool: # WEB BASICS PORT_WEB = get_aw_env_var_or_default('port') +LISTEN_ADDRESS = get_aw_env_var_or_default('address') CSRF_TRUSTED_ORIGINS = [ 'http://localhost', f'http://localhost:{PORT_WEB}', 'http://127.0.0.1', f'http://127.0.0.1:{PORT_WEB}', ] +if LISTEN_ADDRESS != '127.0.0.1': + CSRF_TRUSTED_ORIGINS.extend([ + f'http://{LISTEN_ADDRESS}' + f'http://{LISTEN_ADDRESS}:{PORT_WEB}' + f'https://{LISTEN_ADDRESS}' + f'https://{LISTEN_ADDRESS}:{PORT_WEB}' + ]) + if 'AW_PROXY' in environ: SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') USE_X_FORWARDED_HOST = True diff --git a/src/ansibleguy-webui/webserver.py b/src/ansibleguy-webui/webserver.py index dbe0f9d..d437841 100644 --- a/src/ansibleguy-webui/webserver.py +++ b/src/ansibleguy-webui/webserver.py @@ -12,6 +12,7 @@ from aw.config.environment import get_aw_env_var_or_default PORT_WEB = get_aw_env_var_or_default('port') +LISTEN_ADDRESS = get_aw_env_var_or_default('address') # https://docs.gunicorn.org/en/stable/settings.html OPTIONS_DEV = { @@ -20,7 +21,7 @@ 'workers': 2, } OPTIONS_PROD = { - 'bind': f'127.0.0.1:{PORT_WEB}', + 'bind': f'{LISTEN_ADDRESS}:{PORT_WEB}', 'reload': False, 'loglevel': 'warning', }