From 1102231dfd4257e525c0b1b5eaae9200679744fb Mon Sep 17 00:00:00 2001 From: Guillermo Rojo Date: Thu, 8 Oct 2026 15:33:25 +0200 Subject: [PATCH] fix: operations hardening, second pass - fail2ban: web and Odoo bans use the tool's ufw-odoo-web action (ports 80/443), which works on 0.11.2 (whose ufw[application=...] passes the profile name unquoted) as on 1.0.2 and 1.1.0; backend is set per jail, never in [DEFAULT]; sshd reads the journal where auth.log is absent (Debian 12), with python3-systemd; durations and ignore networks are validated, the admin's SSH address proposed; the regex test is read-only. - Staged writes and the nginx switch back up every file first, check each write, and restore on failure or INT/TERM. - Certificates: Let's Encrypt names /etc/letsencrypt/live//, untouched names the files the vhost names; missing files or a cancelled pick write no HTTPS vhost. - Update configuration: role password, login check with the new values, then write and restart; no restart for a remote new password; the backup path is printed even on failure. - UFW: enabling allows SSH first, deleting an SSH rule asks, the PostgreSQL source is one address. - logrotate policies are staged and validated with logrotate -d; the size threshold is validated. - verify_ops_configs runs fail2ban 1.1.0, 1.0.2 and 0.11.2 (with a negative control), staged-write failures, logrotate rollback, and Let's Encrypt / unknown-version vhosts. --- .github/workflows/ci.yml | 7 +- CHANGELOG.md | 17 ++ CONTRIBUTING.md | 5 +- docs/installation.md | 4 +- docs/operations/instance-management.md | 5 +- docs/operations/log-rotation.md | 4 +- docs/security/firewall.md | 13 +- docs/security/security-fail2ban.md | 14 +- instance_manager/i18n.py | 12 ++ instance_manager/models.py | 6 + instance_manager/planners.py | 181 +++++++++++------- instance_manager/workflows/backup_restore.py | 6 +- instance_manager/workflows/fail2ban.py | 53 ++++- instance_manager/workflows/firewall.py | 28 ++- instance_manager/workflows/install.py | 91 +++++++-- instance_manager/workflows/logrotate.py | 8 +- instance_manager/workflows/manage.py | 15 +- .../2026-10-08-ops-hardening-2/proposal.md | 50 +++++ .../specs/fail2ban-protection/spec.md | 81 ++++++++ .../specs/firewall-ufw/spec.md | 61 ++++++ .../specs/instance-configuration/spec.md | 44 +++++ .../specs/log-rotation/spec.md | 21 ++ .../specs/web-proxy-tls/spec.md | 24 +++ .../2026-10-08-ops-hardening-2/tasks.md | 10 + openspec/specs/fail2ban-protection/spec.md | 23 ++- openspec/specs/firewall-ufw/spec.md | 14 +- openspec/specs/instance-configuration/spec.md | 7 +- openspec/specs/log-rotation/spec.md | 4 +- openspec/specs/web-proxy-tls/spec.md | 13 +- tests/test_ops_configs.py | 26 ++- tests/test_ops_hardening.py | 100 ++++++++++ tools/verify_ops_configs.py | 164 ++++++++++++++-- 32 files changed, 965 insertions(+), 146 deletions(-) create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/proposal.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/specs/fail2ban-protection/spec.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/specs/firewall-ufw/spec.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/specs/instance-configuration/spec.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/specs/log-rotation/spec.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/specs/web-proxy-tls/spec.md create mode 100644 openspec/changes/archive/2026-10-08-ops-hardening-2/tasks.md create mode 100644 tests/test_ops_hardening.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df1506e..78071ab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,12 @@ jobs: - name: Set up Python uses: actions/setup-python@v6 with: - python-version: "3.12" + # 3.9 and 3.11 run the older fail2ban releases verify_ops_configs checks; + # the last one listed is the default `python`. + python-version: | + 3.9 + 3.11 + 3.12 - name: Install project (with dev deps) run: python -m pip install --upgrade pip && pip install -e ".[dev]" diff --git a/CHANGELOG.md b/CHANGELOG.md index 72d4478..51a8f42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,23 @@ All notable changes to this project are documented here. The format is based on ### Fixed +- **fail2ban web and Odoo bans never applied on Ubuntu 22.04 and Debian 11.** fail2ban 0.11.2 split the + `Nginx Full` profile name, and ufw refused it. They now use the tool's own `ufw-odoo-web` action (ports 80 + and 443), checked on fail2ban 0.11.2, 1.0.2 and 1.1.0. +- **The fail2ban base setup was rolled back on Debian 12.** The `backend` set for every jail sent `sshd` to an + `auth.log` that does not exist; `sshd` now reads the journal there. +- **Let's Encrypt and "leave certificates untouched"** pointed the vhost at files that may not exist, and a + certbot-managed vhost was swapped back to self-signed. Both now name the files that are there. A missing + certificate or a cancelled file pick writes no HTTPS vhost. +- **Update configuration** now checks the new values before writing anything. With a new password for a remote + role it no longer restarts the service before that role has the password. +- **Safer configuration changes.** + - Staged writes of fail2ban, logrotate and nginx configuration can no longer leave half a configuration after + a failed write or an interruption. + - Enabling UFW allows SSH first, and deleting the SSH rule asks first. + - Durations, networks and the logrotate size are validated, and "Test the Odoo regex" no longer writes the + filter. + - **Installs that failed on supported hosts.** - **Python.** Odoo 15 on Debian 12 (Python 3.11) and Odoo 14 on a host Python 3.10 hit the `gevent==21.8.0` pin, which has no wheel there and no longer builds; they now get uv's interpreter. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 67a634d..3355c8a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -88,7 +88,10 @@ check and hand-over commands, and has Odoo itself try to send a mail from the co `tools/verify_ops_configs.py` downloads the distribution's nginx package (`apt-get download`, extracted, never installed) and fail2ban's source release, then runs `nginx -t`, `fail2ban-regex` and `fail2ban-client -t` on the -generated vhosts, filter and jails, including their rollbacks. Run it after changing those planners. +generated vhosts, filter and jails, including their rollbacks. It runs fail2ban 1.1.0, 1.0.2 and 0.11.2 (the +older two on a Python 3.11/3.10 and 3.9/3.8 found on PATH or through uv, converted with lib2to3 as the +distributions do), and has each ban a stub ufw. It also runs the staged writes' failure modes and logrotate's +rollback. Run it after changing those planners. CI (`.github/workflows/ci.yml`) runs ruff, pytest, a byte-compile, `openspec validate`, and the eunomai `docs-check` / `provenance-check` gates on every push and PR to `main`. diff --git a/docs/installation.md b/docs/installation.md index c795884..b98e0a1 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -136,9 +136,9 @@ For HTTPS you pick a certificate strategy: | Strategy | Behavior | |----------|----------| -| **Leave certificates untouched** | Adds no certificate commands. | +| **Leave certificates untouched** | Adds no certificate commands; the vhost names the certificate the current vhost names (certbot's included), else the tool's own. HTTPS is not configured when those files are missing. | | **Self-signed** | Reuses an existing key/fullchain or generates a 2048-bit self-signed cert for the domain. | -| **Let's Encrypt (managed externally)** | Adds no certificate commands — you manage LE outside the tool. | +| **Let's Encrypt (managed externally)** | Adds no certificate commands; the vhost names `/etc/letsencrypt/live//fullchain.pem` and `privkey.pem`, which must exist — obtain the certificate first. | | **Copy your own certificates** | Copies your CRT/KEY (+ optional intermediate) beside the live files, builds the fullchain, and **checks that the key matches the certificate** before they replace the current ones (kept as `.previous`); a wrong file never reaches Nginx. | ## The instance name must be free diff --git a/docs/operations/instance-management.md b/docs/operations/instance-management.md index 44d2044..35c1c37 100644 --- a/docs/operations/instance-management.md +++ b/docs/operations/instance-management.md @@ -60,7 +60,10 @@ Rewrites the instance's `odoo.conf` and systemd unit with new values, and option - **Nothing is reinstalled** — no apt, clone or pip run — so the venv's pinned setuptools stays as it is. - The Odoo version is read from the instance's checkout and the domain from its vhost. - Passwords are kept unless you choose to set new ones; a new DB password is also set on the local role. -- A running service is **restarted**, so the change is live when the plan ends. Autostart is left as it was. +- A new DB password is set on the local role first; then the login is checked with the new values, and only + then are the files written — a typo leaves the running configuration as it was. +- A running service is **restarted**, so the change is live when the plan ends. Autostart is left as it was. With + a new password for a **remote** role the service is not restarted: set the password there, then restart it. To go back, copy the files from the `config_preupdate/` directory and restart the service. diff --git a/docs/operations/log-rotation.md b/docs/operations/log-rotation.md index 013d2dd..892bf6b 100644 --- a/docs/operations/log-rotation.md +++ b/docs/operations/log-rotation.md @@ -23,8 +23,8 @@ policy for an instance's Odoo log (`/var/log/odoo/.log`). The policy uses **`copytruncate`**, so Odoo keeps writing to the same file and does **not** need a restart. `/var/log/odoo` belongs to root and each instance owns only its own log, so root rotates it with no `su` -directive. The plan installs `logrotate` if missing and validates the file with `logrotate -d` before -finishing. +directive. The plan installs `logrotate` if missing and keeps the policy only if `logrotate -d` accepts it; the +previous one is put back otherwise. The size threshold takes a number with `k`, `M` or `G`. Log rotation is also offered **at install time** (recommended, default yes), so a fresh instance starts with a rotation policy in place. diff --git a/docs/security/firewall.md b/docs/security/firewall.md index b1cc81d..d89cfde 100644 --- a/docs/security/firewall.md +++ b/docs/security/firewall.md @@ -31,13 +31,18 @@ Applies a secure baseline: - **Allow a port** — `ufw allow /`. - **Delete a rule (by number)** — lists the numbered rules and deletes one only if that number still shows the - same rule when the plan runs: fail2ban adds its bans at the top of the list, which shifts the numbers. -- **Enable / Disable UFW**. + same rule when the plan runs: fail2ban adds its bans at the top of the list, which shifts the numbers. A rule + that lets SSH in (its port, or the `OpenSSH` profile) is deleted only after you confirm the risk. +- **Enable / Disable UFW** — enabling allows the ports SSH listens on first; when none is detected, you confirm + before it goes up. + +The PostgreSQL rule takes one address (the app server's), never a network or `any`. ## Relationship with Fail2ban -[Fail2ban](security-fail2ban.md) is configured with `banaction = ufw`, so its bans only take effect when UFW -is installed and active — this menu is how you make that true. +[Fail2ban](security-fail2ban.md) bans through UFW (`ufw` for SSH and recidive, the tool's `ufw-odoo-web` for +the web ports), so its bans only take effect when UFW is installed and active — this menu is how you make that +true. ## Related diff --git a/docs/security/security-fail2ban.md b/docs/security/security-fail2ban.md index cf349dc..bb27f87 100644 --- a/docs/security/security-fail2ban.md +++ b/docs/security/security-fail2ban.md @@ -30,7 +30,7 @@ reloaded, and the plan waits for the socket. ```mermaid flowchart LR ssh["sshd · recidive"] --> all(["all ports
of that address"]) - web["nginx-http-auth · nginx-botsearch
odoo-auth (per instance)"] --> webports(["web ports only
ufw profile Nginx Full"]) + web["nginx-http-auth · nginx-botsearch
odoo-auth (per instance)"] --> webports(["ports 80 and 443 only
action ufw-odoo-web"]) ignore[("ignore list
loopback + your admin networks")] -.-> never["never banned"] classDef step fill:#dbeafe,stroke:#2563eb,color:#1e3a8a classDef guard fill:#dcfce7,stroke:#16a34a,color:#14532d @@ -42,8 +42,16 @@ flowchart LR class ignore data ``` -A failed Odoo or web login therefore never blocks SSH. The `Nginx Full` profile comes with the nginx package; -without it, fail2ban's ufw action blocks every port. +A failed Odoo or web login therefore never blocks SSH. The web bans use the tool's own `ufw-odoo-web` action +(`/etc/fail2ban/action.d/ufw-odoo-web.conf`), which rejects the address on ports 80 and 443. fail2ban's own +`ufw[application="Nginx Full"]` is not used. On fail2ban 0.11.2 (Ubuntu 22.04, Debian 11) it passes the profile +name unquoted, so ufw refuses every ban. On 1.0 and later it bans every port when the profile is missing. + +The base jails set `backend` per jail, never for all of them. Where `/var/log/auth.log` does not exist (Debian 12, +or a host without rsyslog), `sshd` reads the journal, and the plan installs `python3-systemd` for it. Durations +use fail2ban's units (`10m`, `1h`, `1d`). The ignore list takes addresses or networks and starts with the address +your SSH session comes from. Every write is staged, and a refused configuration, a failed write or an +interruption puts the previous files back. > **UFW prerequisite:** the ban action is `ufw`, so bans only take effect while **UFW is installed and > active**. This menu does not install UFW: set it up from **Firewall (UFW)**, which allows SSH before enabling diff --git a/instance_manager/i18n.py b/instance_manager/i18n.py index 4fcda5d..998f265 100644 --- a/instance_manager/i18n.py +++ b/instance_manager/i18n.py @@ -990,4 +990,16 @@ def tf(template: str, *args: object, **kwargs: object) -> str: 'Database manager disabled (list_db = False). Create the first database as the instance user: sudo -u /venv/bin/python /odoo/odoo-bin -c -d -i base --stop-after-init, or re-enable the manager for a while.': 'Gestor de bases de datos desactivado (list_db = False). Crea la primera base de datos como el usuario de la instancia: sudo -u /venv/bin/python /odoo/odoo-bin -c -d -i base --stop-after-init, o reactiva el gestor un momento.', 'verify-full checks the server against a CA: place it at {}/.postgresql/root.crt, readable by the instance user, before the install runs.': 'verify-full comprueba el servidor contra una CA: colócala en {}/.postgresql/root.crt, legible por el usuario de la instancia, antes de ejecutar la instalación.', 'The gevent port must differ from the HTTP port.': 'El puerto gevent debe ser distinto del puerto HTTP.', + 'Use a duration such as 10m, 1h, 1d or 1w.': 'Usa una duración como 10m, 1h, 1d o 1w.', + 'Not an IP address or network: {}': 'No es una dirección ni una red IP: {}', + 'Install the journal reader fail2ban needs (python3-systemd)': 'Instalar el lector del journal que necesita fail2ban (python3-systemd)', + 'This rule lets SSH in: deleting it can lock you out of the server. Delete it anyway?': 'Esta regla deja entrar por SSH: borrarla puede dejarte fuera del servidor. ¿Borrarla igualmente?', + 'Could not detect the SSH port: enabling UFW may lock you out. Enable anyway?': 'No se pudo detectar el puerto SSH: activar UFW puede dejarte fuera. ¿Activarlo igualmente?', + 'Write {} and keep it only if logrotate accepts it': 'Escribir {} y conservarlo solo si logrotate lo acepta', + 'Use a size such as 500k, 50M or 1G.': 'Usa un tamaño como 500k, 50M o 1G.', + "Test the fail2ban regex (the tool's odoo-auth filter, not installed)": 'Probar la regex de fail2ban (el filtro odoo-auth de la herramienta, sin instalar)', + 'Certificate file not found: {}. {}': 'Certificado no encontrado: {}. {}', + 'Choose a mode that provides a certificate.': 'Elige un modo que aporte un certificado.', + 'Obtain it first (for example: certbot certonly --webroot or --standalone for the domain).': 'Obténlo antes (por ejemplo: certbot certonly --webroot o --standalone para el dominio).', + 'The DB is remote: the service is not restarted. Set the same password on its role there, then restart the service.': 'La BD es remota: no se reinicia el servicio. Pon la misma contraseña a su rol allí y luego reinicia el servicio.', } diff --git a/instance_manager/models.py b/instance_manager/models.py index 8065bf1..232bbd6 100644 --- a/instance_manager/models.py +++ b/instance_manager/models.py @@ -135,6 +135,12 @@ class InstanceConfig: app_server_ip: str = "127.0.0.1" odoo_admin_passwd: str = "" + # The certificate and key an HTTPS vhost names when they are not the tool's own + # (Let's Encrypt, or the ones an existing vhost already names). Empty: the files + # under nginx_ssl_dir. + tls_cert: str = "" + tls_key: str = "" + # Production-posture settings (rendered into odoo.conf). list_db: bool = False dbfilter: str = "" diff --git a/instance_manager/planners.py b/instance_manager/planners.py index f2e6d7d..cf5a1a6 100644 --- a/instance_manager/planners.py +++ b/instance_manager/planners.py @@ -195,10 +195,26 @@ def plan_update_instance_config( """Rewrite an existing instance's ``odoo.conf`` (merged) and unit, and apply them. Nothing is reinstalled — no apt, clone or pip, which could move setuptools under - an Odoo that needs it pinned. A new DB password is set on the local role too, or - the next start could not connect. The service is restarted when it runs, since - ``systemctl start`` on a running unit loads nothing.""" - commands = write_text_file_command( + an Odoo that needs it pinned. A new DB password is set on the local role first, + then the instance's login is checked with the new values (host, port, user, + password, SSL mode): only then is anything written, so a typo leaves the running + configuration as it was. The service is restarted when it runs, since + ``systemctl start`` on a running unit loads nothing — except with a new password + for a remote role, which only that server's administrator can set: the restart + waits for it.""" + remote_password = new_db_password and config.is_remote_db_host + commands: list[Command] = [] + if new_db_password and not config.is_remote_db_host: + sql = f"{SCRAM}ALTER ROLE \"{config.db_user}\" WITH PASSWORD '{_sql_literal(config.db_password)}';" + commands.append( + _psql_stdin_command( + f"{_local_psql(config)} -q -v ON_ERROR_STOP=1", sql, (config.db_password,), + 'Set the new password on the local PostgreSQL role', + ) + ) + if not remote_password: + commands.append(_db_connectivity_check(config)) + commands += write_text_file_command( config.odoo_conf_file, render_merged_odoo_conf(config, existing, other_sections), "640", secrets=(config.odoo_admin_passwd, config.db_password, *(value for key, value in existing.items() if "pass" in key or "secret" in key)), @@ -209,15 +225,7 @@ def plan_update_instance_config( service_path = f"/etc/systemd/system/{config.odoo_service}.service" commands += write_text_file_command(service_path, _systemd_content(config), "644") commands.append(Command('Reload systemd', "systemctl daemon-reload")) - if new_db_password and not config.is_remote_db_host: - sql = f"{SCRAM}ALTER ROLE \"{config.db_user}\" WITH PASSWORD '{_sql_literal(config.db_password)}';" - commands.append( - _psql_stdin_command( - f"{_local_psql(config)} -q -v ON_ERROR_STOP=1", sql, (config.db_password,), - 'Set the new password on the local PostgreSQL role', - ) - ) - if restart: + if restart and not remote_password: commands.append( Command('Restart the Odoo service to load the configuration', f"systemctl restart {shlex.quote(config.odoo_service)}") ) @@ -395,8 +403,8 @@ def _nginx_https_content( client_max_body_size 2048m; - ssl_certificate {config.ssl_fullchain_file}; - ssl_certificate_key {config.ssl_key_file}; + ssl_certificate {config.tls_cert or config.ssl_fullchain_file}; + ssl_certificate_key {config.tls_key or config.ssl_key_file}; ssl_session_timeout 30m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers {_ODOO_SSL_CIPHERS}; @@ -428,26 +436,38 @@ def staged_files_command(files: list[tuple[str, str, str]], validate: str) -> st """Put ``(path, content, mode)`` files in place and keep them only if ``validate`` succeeds; otherwise every file is restored to what it was (or removed when it is new) and the command fails. A configuration a service would - refuse at its next restart is never left enabled.""" - lines = ['backup=$(mktemp -d)', 'restore() {'] + refuse at its next restart is never left enabled. + + Every file is backed up before the first is written, any failed write or an + interruption restores them all, and nothing is touched when no backup + directory can be made.""" + lines = [ + "backup=$(mktemp -d) || exit 1", + "restore() {", + ] for index, (path, _content, _mode) in enumerate(files): q = shlex.quote(path) - lines.append(f' if [ -e "$backup/{index}" ]; then cp -a "$backup/{index}" {q}; else rm -f {q}; fi') - lines.append('}') - for index, (path, content, mode) in enumerate(files): + lines.append(f' if [ -e "$backup/{index}" ]; then cp -a "$backup/{index}" {q}; ' + f'elif [ -e "$backup/{index}.absent" ]; then rm -f {q}; fi') + lines += [ + "}", + 'fail() { restore; rm -rf "$backup"; echo "[ERROR] $1: the previous configuration was restored." >&2; exit 1; }', + "trap 'fail Interrupted' INT TERM", + ] + for index, (path, _content, _mode) in enumerate(files): q = shlex.quote(path) + lines.append(f'if [ -e {q} ]; then cp -a {q} "$backup/{index}" || fail "Backup failed"; ' + f'else touch "$backup/{index}.absent"; fi') + for path, content, mode in files: + q, new = shlex.quote(path), shlex.quote(path + ".oim-new") delimiter = _heredoc_delimiter(content) lines += [ - f'if [ -e {q} ]; then cp -a {q} "$backup/{index}"; fi', - f"cat > {shlex.quote(path + '.oim-new')} <<'{delimiter}'", + f"cat > {new} <<'{delimiter}' && chmod {mode} {new} && mv -f {new} {q} || fail \"Writing {path} failed\"", content.rstrip("\n"), delimiter, - f"chmod {mode} {shlex.quote(path + '.oim-new')}", - f"mv -f {shlex.quote(path + '.oim-new')} {q}", ] lines += [ - f"if ! {{ {validate}; }}; then restore; rm -rf \"$backup\"; " - "echo '[ERROR] Validation failed: the previous configuration was restored.' >&2; exit 1; fi", + f'{{ {validate}; }} || fail "Validation failed"', 'rm -rf "$backup"', ] return "\n".join(lines) @@ -564,13 +584,16 @@ def plan_logrotate_config( "command -v logrotate >/dev/null 2>&1 || (apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold -o DPkg::Lock::Timeout=600 install logrotate)", ), ] - commands.extend( - write_text_file_command(config.logrotate_config_file, content, "644") - ) + # Kept only if logrotate's dry run accepts it: a policy it refuses stops the + # daily run at that file. The previous policy is put back otherwise. + path = shlex.quote(config.logrotate_config_file) commands.append( Command( - 'Validate logrotate configuration (dry-run)', - f"logrotate -d {shlex.quote(config.logrotate_config_file)}", + tf('Write {} and keep it only if logrotate accepts it', config.logrotate_config_file), + staged_files_command( + [(config.logrotate_config_file, content, "644")], + f'out=$(logrotate -d {path} 2>&1) || {{ printf "%s\\n" "$out" | grep -i error >&2; false; }}', + ), ) ) if remove_obsolete_odoo_key: @@ -601,9 +624,25 @@ def _safe_token(value: str) -> str: "Login failed for login:admin from 203.0.113.7 4 0.012 0.003", ) # Ports a web ban covers: ufw's application profile installed by the nginx package. -# Without it, fail2ban's ufw action bans every port, SSH included. UFW_WEB_APPLICATION = "Nginx Full" +# The action web and Odoo bans use: ufw rejecting the address on the web ports only. +# fail2ban's own `ufw[application=…]` cannot be used: 0.11.2 (Ubuntu 22.04, Debian +# 11) passes the name unquoted (`app Nginx Full`, which ufw refuses, so no ban +# applies), and 1.0+ bans every port, SSH included, when the profile is missing. +FAIL2BAN_WEB_ACTION = "ufw-odoo-web" +_FAIL2BAN_WEB_ACTION_PATH = f"/etc/fail2ban/action.d/{FAIL2BAN_WEB_ACTION}.conf" + + +def _fail2ban_web_action_content() -> str: + return """[Definition] +actionstart = +actionstop = +actioncheck = +actionban = ufw prepend reject from to any port 80,443 proto tcp +actionunban = ufw delete reject from to any port 80,443 proto tcp +""" + def _fail2ban_base_content( ignore_ips: str, @@ -612,16 +651,20 @@ def _fail2ban_base_content( maxretry: int, recidive_bantime: str, nginx_logs: bool = True, + sshd_systemd: bool = False, ) -> str: """The base jails. sshd bans every port (it is SSH); the web jails ban only the web ports; recidive uses ufw too. The nginx jails are written only when nginx logs exist: fail2ban refuses to start with a jail whose log file is missing, - and takes the sshd jail down with it.""" - web = f'ufw[application="{UFW_WEB_APPLICATION}"]' + and takes the sshd jail down with it. + + ``backend`` is set per jail, never in ``[DEFAULT]``, which every packaged jail + inherits: sshd reads the journal (``sshd_systemd``) on a host with no + ``/var/log/auth.log`` (Debian 12, a system without rsyslog), the file jails + ``auto``.""" content = f"""[DEFAULT] banaction = ufw banaction_allports = ufw -backend = auto ignoreip = {ignore_ips} bantime = {bantime} findtime = {findtime} @@ -630,15 +673,19 @@ def _fail2ban_base_content( [sshd] enabled = true """ + if sshd_systemd: + content += "backend = systemd\n" if nginx_logs: content += f""" [nginx-http-auth] enabled = true -banaction = {web} +backend = auto +banaction = {FAIL2BAN_WEB_ACTION} [nginx-botsearch] enabled = true -banaction = {web} +backend = auto +banaction = {FAIL2BAN_WEB_ACTION} """ content += f""" [recidive] @@ -678,7 +725,7 @@ def _fail2ban_odoo_jail_content( logpath = {log_path} backend = auto port = http,https -banaction = ufw[application="{UFW_WEB_APPLICATION}"] +banaction = {FAIL2BAN_WEB_ACTION} bantime = {bantime} findtime = {findtime} maxretry = {maxretry} @@ -725,15 +772,25 @@ def plan_fail2ban_base_setup( maxretry: int = 8, recidive_bantime: str = "24h", nginx_logs: bool = True, + sshd_systemd: bool = False, ) -> list[Command]: jail_base_path = "/etc/fail2ban/jail.d/odoo-instance-manager.local" - content = _fail2ban_base_content(ignore_ips, bantime, findtime, maxretry, recidive_bantime, nginx_logs) + content = _fail2ban_base_content(ignore_ips, bantime, findtime, maxretry, recidive_bantime, nginx_logs, + sshd_systemd) + files = [(_FAIL2BAN_WEB_ACTION_PATH, _fail2ban_web_action_content(), "644"), (jail_base_path, content, "644")] + journal = ( + [Command('Install the journal reader fail2ban needs (python3-systemd)', + "DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confold " + "-o DPkg::Lock::Timeout=600 install python3-systemd")] + if sshd_systemd else [] + ) return [ Command('Ensure fail2ban is installed', _FAIL2BAN_INSTALL), - Command('Create /etc/fail2ban/jail.d', "mkdir -p /etc/fail2ban/jail.d"), + *journal, + Command('Create fail2ban directories', "mkdir -p /etc/fail2ban/jail.d /etc/fail2ban/action.d"), Command( tf('Write {} and keep it only if fail2ban accepts it', jail_base_path), - staged_files_command([(jail_base_path, content, "644")], "fail2ban-client -t"), + staged_files_command(files, "fail2ban-client -t"), ), *_fail2ban_apply_commands(), ] @@ -750,11 +807,13 @@ def plan_fail2ban_enable_odoo_instance( jail_path = f"/etc/fail2ban/jail.d/{jail_name}.local" files = [ (_FAIL2BAN_FILTER, _fail2ban_odoo_filter_content(), "644"), + (_FAIL2BAN_WEB_ACTION_PATH, _fail2ban_web_action_content(), "644"), (jail_path, _fail2ban_odoo_jail_content(jail_name, log_path, bantime, findtime, maxretry), "644"), ] return [ Command('Ensure fail2ban is installed', _FAIL2BAN_INSTALL), - Command('Create fail2ban directories', "mkdir -p /etc/fail2ban/filter.d /etc/fail2ban/jail.d"), + Command('Create fail2ban directories', + "mkdir -p /etc/fail2ban/filter.d /etc/fail2ban/jail.d /etc/fail2ban/action.d"), Command('Validate instance log', f"test -f {shlex.quote(log_path)}"), Command( tf('Write the odoo-auth filter and the {} jail; keep them only if fail2ban accepts them', jail_name), @@ -765,18 +824,6 @@ def plan_fail2ban_enable_odoo_instance( ] -def plan_fail2ban_ensure_odoo_filter() -> list[Command]: - return [ - Command('Create fail2ban filters directory', "mkdir -p /etc/fail2ban/filter.d"), - Command( - 'Write the odoo-auth filter and keep it only if fail2ban accepts it', - staged_files_command([(_FAIL2BAN_FILTER, _fail2ban_odoo_filter_content(), "644")], - "fail2ban-client -t"), - ), - _filter_self_test(), - ] - - def plan_odoo_log_dir(config: InstanceConfig) -> list[Command]: """The shared ``/var/log/odoo`` belongs to root (755) and each instance owns only its own log (640): every instance writes its log, none can touch another's, and @@ -1115,30 +1162,30 @@ def plan_ensure_db_role(config: InstanceConfig, reset_password: bool = False) -> def _nginx_switch_command(available: str, content: str, enable: str, disable: str) -> str: """Write the vhost, enable it and disable its sibling, then keep the change only - if ``nginx -t`` accepts the whole configuration; otherwise the vhost file and - both links are put back as they were. A broken vhost left enabled would stop - nginx — every instance on the host — at its next restart.""" + if ``nginx -t`` accepts the whole configuration; otherwise — or on a failed + write, or an interruption — the vhost file and both links are put back as they + were. A broken vhost left enabled would stop nginx — every instance on the host + — at its next restart.""" q = shlex.quote delimiter = _heredoc_delimiter(content) return "\n".join([ - "backup=$(mktemp -d)", - f'if [ -e {q(available)} ]; then cp -a {q(available)} "$backup/available"; fi', - f'if [ -L {q(enable)} ]; then cp -a {q(enable)} "$backup/enable"; fi', - f'if [ -L {q(disable)} ]; then cp -a {q(disable)} "$backup/disable"; fi', + "backup=$(mktemp -d) || exit 1", + f'if [ -e {q(available)} ]; then cp -a {q(available)} "$backup/available" || exit 1; fi', + f'if [ -L {q(enable)} ]; then cp -a {q(enable)} "$backup/enable" || exit 1; fi', + f'if [ -L {q(disable)} ]; then cp -a {q(disable)} "$backup/disable" || exit 1; fi', "restore() {", f' if [ -e "$backup/available" ]; then cp -a "$backup/available" {q(available)}; else rm -f {q(available)}; fi', f' rm -f {q(enable)} {q(disable)}', f' if [ -L "$backup/enable" ]; then cp -a "$backup/enable" {q(enable)}; fi', f' if [ -L "$backup/disable" ]; then cp -a "$backup/disable" {q(disable)}; fi', "}", - f"cat > {q(available)} <<'{delimiter}'", + 'fail() { restore; rm -rf "$backup"; echo "[ERROR] $1: the previous configuration was restored." >&2; exit 1; }', + "trap 'fail Interrupted' INT TERM", + f"cat > {q(available)} <<'{delimiter}' && chmod 644 {q(available)} || fail 'Writing the vhost failed'", content.rstrip("\n"), delimiter, - f"chmod 644 {q(available)}", - f"rm -f {q(disable)}", - f"ln -sf {q(available)} {q(enable)}", - 'if ! nginx -t; then restore; rm -rf "$backup"; ' - "echo '[ERROR] nginx refused the configuration: the previous one was restored.' >&2; exit 1; fi", + f"rm -f {q(disable)} && ln -sf {q(available)} {q(enable)} || fail 'Enabling the vhost failed'", + "nginx -t || fail 'nginx refused the configuration'", 'rm -rf "$backup"', ]) diff --git a/instance_manager/workflows/backup_restore.py b/instance_manager/workflows/backup_restore.py index 8758403..0011379 100644 --- a/instance_manager/workflows/backup_restore.py +++ b/instance_manager/workflows/backup_restore.py @@ -946,7 +946,11 @@ def _plan_replica_target( if nginx_mode == 'Configure HTTP': commands.extend(plan_nginx_http(target_config, nginx_version)) elif nginx_mode == 'Configure HTTPS': - commands.extend(_maybe_plan_certs(target_config)) + certs = _maybe_plan_certs(target_config) + if certs is None: + print(level_text("INFO", 'Operation cancelled.')) + return None + commands.extend(certs) commands.extend(plan_nginx_https(target_config, nginx_version)) return commands diff --git a/instance_manager/workflows/fail2ban.py b/instance_manager/workflows/fail2ban.py index 384da64..e8df64e 100644 --- a/instance_manager/workflows/fail2ban.py +++ b/instance_manager/workflows/fail2ban.py @@ -8,9 +8,9 @@ from ..i18n import tf from ..planners import ( + _fail2ban_odoo_filter_content, plan_fail2ban_base_setup, plan_fail2ban_enable_odoo_instance, - plan_fail2ban_ensure_odoo_filter, ) from ..prompts import ask_bool, ask_int, ask_text, choose from ..system import ( @@ -21,6 +21,28 @@ from ..ui import level_text, render_table, title from .common import _execute_plan, _quote, _select_existing_instance +# fail2ban's time abbreviations (a number, optionally s/m/h/d/w/mo/y), or -1 for a +# permanent ban. Anything else lands in a jail fail2ban refuses — or, with a newline, +# in another section of it. +_DURATION_RE = re.compile(r"-1|\d{1,9}(s|m|h|d|w|mo|y)?") + + +def _duration_error(value: str) -> str | None: + if _DURATION_RE.fullmatch(value): + return None + return 'Use a duration such as 10m, 1h, 1d or 1w.' + + +def _networks_error(value: str) -> str | None: + """Addresses or networks separated by spaces or commas, each one ufw and + fail2ban accept.""" + for token in value.replace(",", " ").split(): + try: + ipaddress.ip_network(token, strict=False) + except ValueError: + return tf('Not an IP address or network: {}', token) + return None + def _fail2ban_jail_name_for_instance(instance: str) -> str: token = re.sub(r"[^a-zA-Z0-9_-]+", "_", (instance or "").strip()) @@ -183,8 +205,10 @@ def manage_fail2ban() -> None: if action == 'Install / configure secure baseline': extra_ignore = ask_text( 'Admin IPs/networks to exclude (space/comma separated)', - "", + # The address this session comes from, so the operator is not banned. + (os.environ.get("SSH_CLIENT", "").split() or [""])[0], required=False, + validate=_networks_error, ) extra_tokens = [ token.strip() @@ -192,10 +216,10 @@ def manage_fail2ban() -> None: if token.strip() ] ignore_ips = " ".join(["127.0.0.1/8", "::1", *extra_tokens]) - bantime = ask_text('bantime', "1h", required=True) - findtime = ask_text('findtime', "10m", required=True) + bantime = ask_text('bantime', "1h", required=True, validate=_duration_error) + findtime = ask_text('findtime', "10m", required=True, validate=_duration_error) maxretry = ask_int('maxretry', 8, min_value=1, max_value=1000) - recidive_bantime = ask_text('recidive bantime', "24h", required=True) + recidive_bantime = ask_text('recidive bantime', "24h", required=True, validate=_duration_error) commands = plan_fail2ban_base_setup( ignore_ips=ignore_ips, @@ -204,6 +228,8 @@ def manage_fail2ban() -> None: maxretry=maxretry, recidive_bantime=recidive_bantime, nginx_logs=os.path.isdir("/var/log/nginx"), + # No auth.log (Debian 12, a host without rsyslog): sshd from the journal. + sshd_systemd=not os.path.exists("/var/log/auth.log"), ) _execute_plan(commands) continue @@ -230,8 +256,8 @@ def manage_fail2ban() -> None: else: print(level_text("WARN", ip_message)) - bantime = ask_text('instance bantime', "1h", required=True) - findtime = ask_text('instance findtime', "10m", required=True) + bantime = ask_text('instance bantime', "1h", required=True, validate=_duration_error) + findtime = ask_text('instance findtime', "10m", required=True, validate=_duration_error) maxretry = ask_int('instance maxretry', 8, min_value=1, max_value=1000) commands = plan_fail2ban_enable_odoo_instance( @@ -350,8 +376,17 @@ def manage_fail2ban() -> None: commands: list[Command] = [ Command('Validate the log file', f"test -f {_quote(log_path)}"), ] - if filter_path == "/etc/fail2ban/filter.d/odoo-auth.conf": - commands.extend(plan_fail2ban_ensure_odoo_filter()) + # A test changes nothing: without the filter on disk, the tool's own is + # tested from a temporary copy. + if filter_path == "/etc/fail2ban/filter.d/odoo-auth.conf" and not os.path.exists(filter_path): + content = _fail2ban_odoo_filter_content() + commands.append(Command( + "Test the fail2ban regex (the tool's odoo-auth filter, not installed)", + "tmp=$(mktemp) && trap 'rm -f \"$tmp\"' EXIT && " + f"printf '%s' {_quote(content)} > \"$tmp\" && fail2ban-regex {_quote(log_path)} \"$tmp\"", + )) + _execute_plan(commands) + continue commands.extend( [ Command('Validate the filter file', f"test -f {_quote(filter_path)}"), diff --git a/instance_manager/workflows/firewall.py b/instance_manager/workflows/firewall.py index 67e698e..96b831e 100644 --- a/instance_manager/workflows/firewall.py +++ b/instance_manager/workflows/firewall.py @@ -5,6 +5,7 @@ import re from ..i18n import tf +from ..models import ip_error from ..planners import plan_ufw_allow_port, plan_ufw_base_setup, plan_ufw_delete_rule from ..prompts import ask_bool, ask_int, ask_text, choose from ..system import Command, detect_ssh_ports, run @@ -45,7 +46,8 @@ def _configure_base(config_hint_ip: str = "") -> None: allow_https = ask_bool('Allow HTTPS (443)?', True) pg_from_ip = "" if ask_bool('Allow PostgreSQL (5432) from a specific IP (app server)?', False): - pg_from_ip = ask_text('IP allowed for PostgreSQL', config_hint_ip or "", required=True) + # One address: `any` or a network would open PostgreSQL to more than the app server. + pg_from_ip = ask_text('IP allowed for PostgreSQL', config_hint_ip or "", required=True, validate=ip_error) commands = plan_ufw_base_setup( ssh_port=ssh_port, @@ -73,6 +75,15 @@ def _numbered_rule_line(status: str, number: int) -> str | None: return None +def _allows_ssh(line: str, ssh_ports: list[int]) -> bool: + """Whether a ``ufw status numbered`` line allows one of SSH's ports.""" + if "ALLOW" not in line: + return False + if "OpenSSH" in line: + return True + return any(re.search(rf"(^|[\s\]]){port}(/tcp)?(\s|$)", line) for port in ssh_ports) + + def _delete_rule() -> None: result = run("ufw status numbered 2>&1", check=False) print(f"\n{title('UFW rules')}\n{result.stdout.strip() or '(no rules)'}") @@ -84,12 +95,25 @@ def _delete_rule() -> None: if expected is None: print(level_text("ERROR", tf('There is no rule #{}.', number))) return + if _allows_ssh(expected, detect_ssh_ports() or [22]) and not ask_bool( + 'This rule lets SSH in: deleting it can lock you out of the server. Delete it anyway?', False + ): + return _execute_plan(plan_ufw_delete_rule(number, expected)) def _toggle(enable: bool) -> None: verb = "enable" if enable else "disable" - _execute_plan([Command(tf('UFW {}', verb), f"ufw --force {verb}")]) + commands = [Command(tf('UFW {}', verb), f"ufw --force {verb}")] + if enable: + # As the baseline does: SSH is allowed before the firewall goes up. + ports = detect_ssh_ports() + if not ports and not ask_bool( + 'Could not detect the SSH port: enabling UFW may lock you out. Enable anyway?', False + ): + return + commands = [Command(tf('Allow SSH (port {})', port), f"ufw allow {int(port)}/tcp") for port in ports] + commands + _execute_plan(commands) def manage_firewall() -> None: diff --git a/instance_manager/workflows/install.py b/instance_manager/workflows/install.py index 00372bb..4d7138b 100644 --- a/instance_manager/workflows/install.py +++ b/instance_manager/workflows/install.py @@ -480,7 +480,42 @@ def _maybe_plan_logrotate(config: InstanceConfig) -> list[Command]: return plan_logrotate_config(config, frequency="weekly", rotate_count=14, compress=True) -def _maybe_plan_certs(config: InstanceConfig) -> list[Command]: +_CERT_PATH_RE = re.compile(r"/[A-Za-z0-9/._@+-]{1,250}") + + +def _vhost_certificate(config: InstanceConfig) -> tuple[str, str] | None: + """The certificate and key the instance's current HTTPS vhost names (certbot + rewrites them there), when both are plain absolute paths.""" + found: dict[str, str] = {} + try: + with open(f"/etc/nginx/sites-available/{config.nginx_https_name}", encoding="utf-8", + errors="replace") as handle: + for line in handle: + match = re.match(r"\s*(ssl_certificate|ssl_certificate_key)\s+(\S+?);", line) + if match and _CERT_PATH_RE.fullmatch(match.group(2)): + found.setdefault(match.group(1), match.group(2)) + except OSError: + return None + if "ssl_certificate" in found and "ssl_certificate_key" in found: + return found["ssl_certificate"], found["ssl_certificate_key"] + return None + + +def _use_existing_certificate(config: InstanceConfig, cert: str, key: str, hint: str) -> bool: + """Point the vhost at ``cert``/``key`` if both exist; otherwise say why HTTPS + cannot be configured with this mode (nginx -t would refuse the vhost).""" + missing = [path for path in (cert, key) if not path_exists(path)] + if missing: + print(level_text("ERROR", tf('Certificate file not found: {}. {}', ", ".join(missing), t(hint)))) + return False + config.tls_cert, config.tls_key = cert, key + return True + + +def _maybe_plan_certs(config: InstanceConfig) -> list[Command] | None: + """The certificate steps for an HTTPS vhost, and where the vhost finds the + files. None when there is no certificate to use or the operator cancelled: the + vhost is then not written.""" cert_mode = choose( 'HTTPS certificate management', [ @@ -491,35 +526,43 @@ def _maybe_plan_certs(config: InstanceConfig) -> list[Command]: ], default_index=None, ) + if cert_mode == "": + return None - if cert_mode in {"", 'Leave certificates untouched', "Let's Encrypt (managed externally)"}: - return [] + if cert_mode == 'Leave certificates untouched': + # The ones the vhost names now (certbot's, for instance), else the tool's own. + cert, key = _vhost_certificate(config) or (config.ssl_fullchain_file, config.ssl_key_file) + ok = _use_existing_certificate(config, cert, key, 'Choose a mode that provides a certificate.') + return [] if ok else None + + if cert_mode == "Let's Encrypt (managed externally)": + live = f"/etc/letsencrypt/live/{config.domain}" + ok = _use_existing_certificate( + config, f"{live}/fullchain.pem", f"{live}/privkey.pem", + 'Obtain it first (for example: certbot certonly --webroot or --standalone for the domain).', + ) + return [] if ok else None if cert_mode == 'Self-signed (detect or generate automatically)': return plan_ensure_self_signed_certs(config) print(level_text("INFO", 'Select the public certificate (server.crt)')) - cert_src = select_file_path( - ".", - 'Public certificate (CRT)', - (".crt", ".pem", ".cer"), - ) + cert_src = select_file_path(".", 'Public certificate (CRT)', (".crt", ".pem", ".cer")) + if not cert_src: + return None print(level_text("INFO", 'Select the private key (server.key)')) - key_src = select_file_path( - ".", - 'Private key (KEY)', - (".key", ".pem"), - ) - use_intermediate = ask_bool('Do you have an intermediate file?', True) + key_src = select_file_path(".", 'Private key (KEY)', (".key", ".pem")) + if not key_src: + return None intermediate_src = None - if use_intermediate: + if ask_bool('Do you have an intermediate file?', True): print(level_text("INFO", 'Select the intermediate chain / CA bundle')) intermediate_src = select_file_path( - ".", - 'Intermediate chain (CA bundle / intermediate)', + ".", 'Intermediate chain (CA bundle / intermediate)', (".crt", ".pem", ".cer", ".bundle", ".ca-bundle"), ) - + if not intermediate_src: + return None return plan_copy_custom_certs(config, cert_src, key_src, intermediate_src) @@ -681,7 +724,11 @@ def install_odoo_only() -> None: if nginx_mode == 'Configure HTTP': commands.extend(plan_nginx_http(config, nginx_version)) elif nginx_mode == 'Configure HTTPS': - commands.extend(_maybe_plan_certs(config)) + certs = _maybe_plan_certs(config) + if certs is None: + print(level_text("INFO", 'Operation cancelled.')) + return + commands.extend(certs) commands.extend(plan_nginx_https(config, nginx_version)) commands.extend(_maybe_plan_logrotate(config)) @@ -730,7 +777,11 @@ def install_odoo_and_db() -> None: if nginx_mode == 'Configure HTTP': commands.extend(plan_nginx_http(config, nginx_version)) elif nginx_mode == 'Configure HTTPS': - commands.extend(_maybe_plan_certs(config)) + certs = _maybe_plan_certs(config) + if certs is None: + print(level_text("INFO", 'Operation cancelled.')) + return + commands.extend(certs) commands.extend(plan_nginx_https(config, nginx_version)) commands.extend(_maybe_plan_logrotate(config)) diff --git a/instance_manager/workflows/logrotate.py b/instance_manager/workflows/logrotate.py index 05e11f3..85c9d45 100644 --- a/instance_manager/workflows/logrotate.py +++ b/instance_manager/workflows/logrotate.py @@ -2,6 +2,8 @@ from __future__ import annotations +import re + from ..i18n import tf from ..models import InstanceConfig from ..planners import plan_logrotate_config @@ -89,7 +91,11 @@ def _configure_log_rotation(config: InstanceConfig) -> None: compress = ask_bool('Compress rotated logs?', True) maxsize = "" if ask_bool('Also rotate when a size is exceeded?', False): - maxsize = ask_text('Maximum size (e.g. 50M, 1G)', "50M", required=True) + maxsize = ask_text( + 'Maximum size (e.g. 50M, 1G)', "50M", required=True, + validate=lambda value: None if re.fullmatch(r"[1-9][0-9]{0,8}[kMG]?", value) + else 'Use a size such as 500k, 50M or 1G.', + ) conf_values = read_odoo_conf(config.odoo_conf_file) remove_obsolete = False diff --git a/instance_manager/workflows/manage.py b/instance_manager/workflows/manage.py index d5b8777..390a5b2 100644 --- a/instance_manager/workflows/manage.py +++ b/instance_manager/workflows/manage.py @@ -497,7 +497,7 @@ def update_existing_configs(instance: str) -> None: ) ) if new_db_password and config.is_remote_db_host: - print(level_text("WARN", 'The DB is remote: set the same password on its role there before the next restart.')) + print(level_text("WARN", 'The DB is remote: the service is not restarted. Set the same password on its role there, then restart the service.')) nginx_version = detect_nginx_version() nginx_mode = choose( @@ -508,11 +508,18 @@ def update_existing_configs(instance: str) -> None: if nginx_mode == 'Yes - HTTP': commands.extend(plan_nginx_http(config, nginx_version)) elif nginx_mode == 'Yes - HTTPS': - commands.extend(_maybe_plan_certs(config)) + certs = _maybe_plan_certs(config) + if certs is None: + print(level_text("INFO", 'Operation cancelled.')) + return + commands.extend(certs) commands.extend(plan_nginx_https(config, nginx_version)) - _execute_plan(commands) - print(level_text("INFO", tf('Configuration backup (if the plan ran) at: {}', backup_dest))) + try: + _execute_plan(commands) + finally: + # Also when a step fails: the files to go back to are there. + print(level_text("INFO", tf('Configuration backup (if the plan ran) at: {}', backup_dest))) def _delete_instance( diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/proposal.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/proposal.md new file mode 100644 index 0000000..055e25e --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/proposal.md @@ -0,0 +1,50 @@ +# Operations hardening, second pass + +## Why + +The second audit ran the operations against real fail2ban (0.11.2, 1.0.2, 1.1.0), nginx and logrotate: + +- **fail2ban 0.11.2** (Ubuntu 22.04, Debian 11) passes `ufw[application="Nginx Full"]`'s name unquoted. ufw + refuses it, so no web or Odoo ban ever applied. A negative control reproduces this. +- **Debian 12** has no `auth.log`. Our `[DEFAULT] backend = auto` made `sshd` look for that file, so + `fail2ban-client -t` failed and the base setup was rolled back. +- **Staged writes** wrote, chmodded and moved without checking each step. A failed write, a failed `mktemp` or + an interruption could leave half a configuration. +- **Certificates.** + - "Let's Encrypt" and "leave untouched" pointed the vhost at the tool's own files, which may not exist. + - A certbot-managed vhost was swapped back to self-signed. + - A cancelled file pick still built the plan. +- **Update configuration** wrote `odoo.conf` before checking the new values and before setting the role's + password. With a new password for a remote role, it restarted at once, before that role had it. +- **UFW.** Enable and Delete skipped the SSH guard, and the PostgreSQL source took any value. +- **logrotate and fail2ban inputs.** The logrotate policy was written before validation, with a free-text size. + fail2ban's durations and ignore list were not checked. +- **"Test the Odoo regex"** rewrote the filter. + +## What changes + +- **fail2ban.** + - A `ufw-odoo-web` action bans on 80/443 on every version. + - `backend` is set per jail; `sshd` uses the journal (with `python3-systemd`) where `auth.log` is absent. + - Validated durations and networks; the admin's SSH address is proposed for the ignore list. + - The regex test is read-only. +- **Staged writes.** Back up all files first, then check each write, with an INT/TERM trap. Nothing is touched + without a backup directory. +- **Certificates.** "Let's Encrypt" names `/etc/letsencrypt/live//`, and "untouched" names the files the + current vhost names. Missing files or a cancelled pick write no HTTPS vhost. +- **Update.** Order: role password, login check with the new values, write, restart. No restart for a remote + new password, and the backup path is shown even on failure. +- **UFW.** Enabling allows SSH first, deleting an SSH rule asks first, and the PostgreSQL source is one address. +- **logrotate.** Staged, validated with `logrotate -d`, and the size is validated. + +## Impact + +- Specs: `fail2ban-protection`, `firewall-ufw`, `web-proxy-tls`, `log-rotation`, `instance-configuration`. +- Code: `planners.py`, `models.py`, `workflows/{fail2ban,firewall,install,manage,logrotate,backup_restore}.py`. +- Tests: `tests/test_ops_hardening.py`. +- Verification: `tools/verify_ops_configs.py` (CI) now covers: + - fail2ban 1.1.0, 1.0.2 and 0.11.2, each banning a stub ufw rendered by fail2ban itself, plus the 0.11.2 + negative control; + - staged-write failures; + - logrotate rollback; + - Let's Encrypt and unknown-version vhosts under `nginx -t`. diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/fail2ban-protection/spec.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/fail2ban-protection/spec.md new file mode 100644 index 0000000..dac2359 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/fail2ban-protection/spec.md @@ -0,0 +1,81 @@ +## MODIFIED Requirements + +### Requirement: Secure base setup + +The tool SHALL install Fail2ban when missing and write a base jail configuration enabling `sshd` and +`recidive`, and `nginx-http-auth` and `nginx-botsearch` only when the host has nginx logs (fail2ban refuses the +whole configuration when a jail's log is missing), with `ufw` as the ban action (`banaction_allports` too, so +`recidive` uses it) and operator-tunable timing parameters, each checked (durations in fail2ban's units, ignore entries as addresses or +networks; the operator's own SSH client address is proposed). The web jails SHALL ban only the web ports, through +the tool's own `ufw-odoo-web` action (`ufw prepend reject … port 80,443 proto tcp`): fail2ban 0.11.2 passes +`ufw[application="Nginx Full"]`'s name unquoted, which ufw refuses, and 1.0+ bans every port when that profile is +missing. `backend` SHALL be set per jail, never in `[DEFAULT]` (which every packaged jail inherits); `sshd` SHALL +read the journal where `/var/log/auth.log` is absent (Debian 12, hosts without rsyslog), installing +`python3-systemd`. The configuration SHALL be kept only if `fail2ban-client -t` accepts it; otherwise — or on a +failed write, or an interruption — the previous one is restored. Because the ban action is `ufw`, effective banning SHALL require UFW to +be installed and active on the host; the tool does not install UFW. + +#### Scenario: Base jails are configured and the service is verified ready + +- **WHEN** the operator runs the base setup +- **THEN** the plan installs Fail2ban if missing, writes the base config (including operator-supplied ignore IPs + plus loopback) through a staged step validated with `fail2ban-client -t`, enables the service, reloads it, and + waits for the Fail2ban socket to respond before succeeding + +#### Scenario: UFW is a runtime prerequisite for banning + +- **WHEN** the base setup completes on a host without UFW +- **THEN** the jails validate and run, but bans do not take effect until UFW is installed and active (a + documented prerequisite, not installed by the tool) + +#### Scenario: No nginx jail without nginx logs + +- **WHEN** the host has no `/var/log/nginx` +- **THEN** the base configuration enables no nginx jail + +#### Scenario: A refused configuration is rolled back + +- **WHEN** `fail2ban-client -t` refuses the written configuration, a write fails, or the step is interrupted +- **THEN** the previous files are restored (new ones removed) and the step fails + +#### Scenario: Web bans work on every supported fail2ban + +- **WHEN** a web or Odoo jail bans an address on fail2ban 0.11.2, 1.0.2 or 1.1.0 +- **THEN** ufw rejects that address on ports 80 and 443 only + +### Requirement: Per-instance Odoo jail + +The tool SHALL enable a dedicated `odoo-auth-` jail bound to the instance log, installing the shared +Odoo auth filter, keeping both only if `fail2ban-client -t` accepts them, and testing the filter against Odoo's +login-failure line of every supported version (12-18 `Login failed for db: login: from `, 19 +`Login failed for login: from `, both from the `odoo.addons.base.models.res_users` logger and +followed by the request's perf info) — a test that fails unless each line matches. The jail SHALL ban only the +web ports (the `ufw-odoo-web` action), so an Odoo login failure never bans SSH. + +#### Scenario: Instance jail is created and filter-tested + +- **WHEN** the operator activates protection for an instance and supplies its log path +- **THEN** the plan verifies the log exists, writes the `odoo-auth` filter and the `odoo-auth-` jail + through a validated staged step, runs `fail2ban-regex` on Odoo's line of 12-18 and of 19 requiring one match + each, and reloads Fail2ban + +### Requirement: Fail2ban operations + +The tool SHALL provide operational actions over jails: show status/jails, show a +jail's detail, unban an IP, and test the Odoo regex against a log. + +#### Scenario: Unban lists banned IPs then removes the chosen one + +- **WHEN** the operator unbans an IP for a jail +- **THEN** the currently banned IPs are listed for selection (or manual entry), and the plan runs `fail2ban-client set unbanip ` + +#### Scenario: Regex test ensures the filter then runs fail2ban-regex + +- **WHEN** the operator tests the Odoo regex against a log +- **THEN** the plan validates the log and runs `fail2ban-regex` with the filter on disk or, when the default + `odoo-auth` filter is not installed, with the tool's filter from a temporary file — a test writes nothing + +#### Scenario: Status tolerates a not-yet-ready socket + +- **WHEN** the service is active but the socket has not yet come up +- **THEN** the status view reports a waiting state rather than an error diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/firewall-ufw/spec.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/firewall-ufw/spec.md new file mode 100644 index 0000000..2a7b0dd --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/firewall-ufw/spec.md @@ -0,0 +1,61 @@ +## MODIFIED Requirements + +### Requirement: UFW secure baseline + +The tool SHALL install UFW when missing and apply a secure baseline — default deny incoming, allow outgoing, +allow SSH before enabling (to avoid lock-out), and allow HTTP/HTTPS — optionally allowing PostgreSQL from a +single app-server IP (one address, never a network or `any`), then enable UFW. The SSH port SHALL default to the one SSH listens on (`sshd -T`, and +`ssh.socket` on socket-activated hosts), and a port SSH does not listen on SHALL require an explicit +confirmation. + +#### Scenario: Baseline is applied in a lock-out-safe order + +- **WHEN** the operator applies the UFW baseline with an SSH port +- **THEN** the plan installs UFW if missing, sets default deny-incoming / allow-outgoing, allows the SSH port, + allows HTTP (80) and HTTPS (443) when chosen, allows PostgreSQL (5432) from the given IP when provided, and + enables UFW last — with the SSH allow ordered before the enable + +#### Scenario: Optional rules are omitted when declined + +- **WHEN** HTTP, HTTPS, or the PostgreSQL allow are declined +- **THEN** the corresponding rules are not added + +#### Scenario: The detected SSH port is the default + +- **WHEN** SSH listens on port 2222 +- **THEN** the baseline proposes 2222, and allowing 22 instead requires confirming that new SSH sessions would + be locked out + +### Requirement: UFW operations + +The tool SHALL provide read-only status and operations to allow a port, delete a rule by number, and enable or +disable UFW, each mutating action through the preview/confirm/apply flow. A delete SHALL run only if the rule at +that number is still the line the operator was shown (fail2ban prepends its bans, shifting the numbers). +Enabling UFW SHALL allow the ports SSH listens on first, asking for confirmation when none is detected, and +deleting a rule that lets SSH in SHALL require confirmation. + +#### Scenario: Enabling never shuts SSH out + +- **WHEN** the operator enables UFW +- **THEN** the plan allows the detected SSH ports before enabling it + +#### Scenario: Deleting the SSH rule is confirmed + +- **WHEN** the numbered rule allows SSH (its port, or the `OpenSSH` profile) +- **THEN** the tool warns that it can lock the operator out and deletes it only on confirmation + +#### Scenario: Status is read-only + +- **WHEN** the operator views UFW status +- **THEN** the tool shows `ufw status verbose` without changing anything + +#### Scenario: A rule can be allowed and deleted + +- **WHEN** the operator allows a port or deletes a numbered rule +- **THEN** the plan runs the matching `ufw allow /`, or checks that the numbered line is unchanged + and then runs `ufw --force delete `, after preview and confirmation + +#### Scenario: A shifted list deletes nothing + +- **WHEN** the rule list changed between the listing and the delete +- **THEN** the step fails without deleting any rule diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/instance-configuration/spec.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/instance-configuration/spec.md new file mode 100644 index 0000000..7919fa8 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/instance-configuration/spec.md @@ -0,0 +1,44 @@ +## MODIFIED Requirements + +### Requirement: Configuration update merges and applies + +Updating an existing instance's configuration SHALL first back up the current config, systemd unit, and Nginx +vhosts into a single private timestamped directory, then rewrite `odoo.conf` **merged** with the current one and +rewrite the unit — without reinstalling anything — and optionally regenerate the Nginx vhost. The keys an +operator tunes by hand (`addons_path`, `data_dir`, `logfile`, `http_interface`, `without_demo`) SHALL keep their +current value, and keys the tool does not write SHALL be carried over. The current file SHALL be read as Odoo +reads it (`[options]` with keys lower-cased and a continued value kept whole), and its other sections (a +module's own, such as `[queue_job]`) SHALL follow unchanged. The Odoo version SHALL be read from the +instance's checkout and the domain from its vhost. A new DB password SHALL be set on the local role first, then +the login SHALL be checked with the new values, and only then are the files written; a running service SHALL be +restarted so the change is live — except with a new password for a remote role, which only that server can set: +the tool then leaves the service as it is and says so. The backup's location SHALL be shown even when a step +fails. + +#### Scenario: Existing files are backed up before regeneration + +- **WHEN** the operator updates an instance configuration +- **THEN** the plan copies the current config, unit, and Nginx vhosts into a single + `/var/backups//config_preupdate//` directory (mode `700`) before writing new versions + +#### Scenario: The current configuration is merged, not replaced + +- **WHEN** the current `odoo.conf` holds `data_dir`, extra `addons_path` entries (over several lines), `smtp_*` + or other keys, keys written in another case, or other sections +- **THEN** the rewritten file keeps them, with each key once, and Odoo reads it + +#### Scenario: Nothing is reinstalled + +- **WHEN** the configuration update is applied +- **THEN** the plan runs no package install, clone or `pip install` + +#### Scenario: The change is live when the plan ends + +- **WHEN** the service is running +- **THEN** the plan restarts it after writing the configuration; and when a new DB password was entered for a + local database, the plan sets it on the role before the restart + +#### Scenario: Autostart state is preserved across update + +- **WHEN** the configuration is regenerated +- **THEN** the service's enabled-at-boot state is left as it was diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/log-rotation/spec.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/log-rotation/spec.md new file mode 100644 index 0000000..9bb0185 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/log-rotation/spec.md @@ -0,0 +1,21 @@ +## MODIFIED Requirements + +### Requirement: Configure system log rotation + +The tool SHALL configure a system `logrotate` policy for an instance's Odoo log at +`/etc/logrotate.d/odoo-`, with operator-chosen frequency, retention count, optional compression, and +optional size threshold (a number with an optional `k`, `M` or `G`), using `copytruncate` so the running service +is not restarted. The policy SHALL be kept only if `logrotate -d` accepts it; otherwise the previous one is put +back. + +#### Scenario: Rotation policy is written and validated + +- **WHEN** the operator configures log rotation for an instance +- **THEN** the plan ensures `logrotate` is installed, writes `/etc/logrotate.d/odoo-` for + `/var/log/odoo/.log` with the chosen frequency/retention/compression and `copytruncate`, with no + `su` directive (`/var/log/odoo` is root's, so root rotates), and validates it with `logrotate -d` + +#### Scenario: Size threshold is honored when requested + +- **WHEN** the operator opts to also rotate on a size threshold +- **THEN** the generated policy includes a `maxsize` directive with the chosen value diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/web-proxy-tls/spec.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/web-proxy-tls/spec.md new file mode 100644 index 0000000..d0fabf2 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/specs/web-proxy-tls/spec.md @@ -0,0 +1,24 @@ +## MODIFIED Requirements + +### Requirement: TLS certificate modes + +For HTTPS, the tool SHALL offer four certificate strategies: do not touch +certificates, self-signed (detect or generate), Let's Encrypt (externally +managed), and copy operator-supplied CRT/KEY with an optional intermediate. + +#### Scenario: Self-signed is reused or generated + +- **WHEN** the self-signed strategy is chosen +- **THEN** the plan reuses an existing key/fullchain if present, otherwise generates a 2048-bit self-signed certificate for the domain, and sets `root:www-data` ownership with `640`/`644` permissions + +#### Scenario: Let's Encrypt and untouched use the files that are there + +- **WHEN** the Let's Encrypt strategy is chosen +- **THEN** the plan adds no certificate commands and the vhost names `/etc/letsencrypt/live//fullchain.pem` + and `privkey.pem`; **WHEN** do-not-touch is chosen, it names the files the current vhost names (certbot's + included), or else the tool's own + +#### Scenario: No certificate, no HTTPS vhost + +- **WHEN** the files a strategy names do not exist, or a certificate file pick is cancelled +- **THEN** the tool says which file is missing and writes no HTTPS vhost (`nginx -t` would refuse it) diff --git a/openspec/changes/archive/2026-10-08-ops-hardening-2/tasks.md b/openspec/changes/archive/2026-10-08-ops-hardening-2/tasks.md new file mode 100644 index 0000000..784025b --- /dev/null +++ b/openspec/changes/archive/2026-10-08-ops-hardening-2/tasks.md @@ -0,0 +1,10 @@ +# Tasks + +- [x] 1. Spec deltas; openspec 1.4.1 validates. +- [x] 2. fail2ban: ufw-odoo-web action, per-jail backend, journal for sshd, validated inputs, read-only regex test. +- [x] 3. Staged writes and the nginx switch: backups first, checked writes, INT/TERM trap. +- [x] 4. Certificate modes: Let's Encrypt and untouched name existing files; cancel and missing files stop. +- [x] 5. Update order; remote password without restart; backup path on failure. +- [x] 6. UFW SSH guard on enable and delete; one PostgreSQL source address. +- [x] 7. logrotate staged with logrotate -d; maxsize validated. +- [x] 8. Tests; verify_ops_configs across fail2ban 0.11.2, 1.0.2 and 1.1.0 (CI gets 3.9 and 3.11). diff --git a/openspec/specs/fail2ban-protection/spec.md b/openspec/specs/fail2ban-protection/spec.md index 8f398c5..79079a2 100644 --- a/openspec/specs/fail2ban-protection/spec.md +++ b/openspec/specs/fail2ban-protection/spec.md @@ -14,9 +14,14 @@ of whether the Odoo log carries the real client IP, and operational actions The tool SHALL install Fail2ban when missing and write a base jail configuration enabling `sshd` and `recidive`, and `nginx-http-auth` and `nginx-botsearch` only when the host has nginx logs (fail2ban refuses the whole configuration when a jail's log is missing), with `ufw` as the ban action (`banaction_allports` too, so -`recidive` uses it) and operator-tunable timing parameters. The web jails SHALL ban only the web ports, through -ufw's `Nginx Full` application profile. The configuration SHALL be kept only if `fail2ban-client -t` accepts it; -otherwise the previous one is restored. Because the ban action is `ufw`, effective banning SHALL require UFW to +`recidive` uses it) and operator-tunable timing parameters, each checked (durations in fail2ban's units, ignore entries as addresses or +networks; the operator's own SSH client address is proposed). The web jails SHALL ban only the web ports, through +the tool's own `ufw-odoo-web` action (`ufw prepend reject … port 80,443 proto tcp`): fail2ban 0.11.2 passes +`ufw[application="Nginx Full"]`'s name unquoted, which ufw refuses, and 1.0+ bans every port when that profile is +missing. `backend` SHALL be set per jail, never in `[DEFAULT]` (which every packaged jail inherits); `sshd` SHALL +read the journal where `/var/log/auth.log` is absent (Debian 12, hosts without rsyslog), installing +`python3-systemd`. The configuration SHALL be kept only if `fail2ban-client -t` accepts it; otherwise — or on a +failed write, or an interruption — the previous one is restored. Because the ban action is `ufw`, effective banning SHALL require UFW to be installed and active on the host; the tool does not install UFW. #### Scenario: Base jails are configured and the service is verified ready @@ -39,9 +44,14 @@ be installed and active on the host; the tool does not install UFW. #### Scenario: A refused configuration is rolled back -- **WHEN** `fail2ban-client -t` refuses the written configuration +- **WHEN** `fail2ban-client -t` refuses the written configuration, a write fails, or the step is interrupted - **THEN** the previous files are restored (new ones removed) and the step fails +#### Scenario: Web bans work on every supported fail2ban + +- **WHEN** a web or Odoo jail bans an address on fail2ban 0.11.2, 1.0.2 or 1.1.0 +- **THEN** ufw rejects that address on ports 80 and 443 only + ### Requirement: Per-instance Odoo jail The tool SHALL enable a dedicated `odoo-auth-` jail bound to the instance log, installing the shared @@ -49,7 +59,7 @@ Odoo auth filter, keeping both only if `fail2ban-client -t` accepts them, and te login-failure line of every supported version (12-18 `Login failed for db: login: from `, 19 `Login failed for login: from `, both from the `odoo.addons.base.models.res_users` logger and followed by the request's perf info) — a test that fails unless each line matches. The jail SHALL ban only the -web ports (ufw's `Nginx Full` profile), so an Odoo login failure never bans SSH. +web ports (the `ufw-odoo-web` action), so an Odoo login failure never bans SSH. #### Scenario: Instance jail is created and filter-tested @@ -93,7 +103,8 @@ jail's detail, unban an IP, and test the Odoo regex against a log. #### Scenario: Regex test ensures the filter then runs fail2ban-regex - **WHEN** the operator tests the Odoo regex against a log -- **THEN** the plan ensures the default `odoo-auth` filter exists when targeted, validates the log and filter files, and runs `fail2ban-regex` +- **THEN** the plan validates the log and runs `fail2ban-regex` with the filter on disk or, when the default + `odoo-auth` filter is not installed, with the tool's filter from a temporary file — a test writes nothing #### Scenario: Status tolerates a not-yet-ready socket diff --git a/openspec/specs/firewall-ufw/spec.md b/openspec/specs/firewall-ufw/spec.md index 8e14238..112ae23 100644 --- a/openspec/specs/firewall-ufw/spec.md +++ b/openspec/specs/firewall-ufw/spec.md @@ -9,7 +9,7 @@ Put a UFW baseline on the host without locking the operator out (SSH allowed on The tool SHALL install UFW when missing and apply a secure baseline — default deny incoming, allow outgoing, allow SSH before enabling (to avoid lock-out), and allow HTTP/HTTPS — optionally allowing PostgreSQL from a -single app-server IP, then enable UFW. The SSH port SHALL default to the one SSH listens on (`sshd -T`, and +single app-server IP (one address, never a network or `any`), then enable UFW. The SSH port SHALL default to the one SSH listens on (`sshd -T`, and `ssh.socket` on socket-activated hosts), and a port SSH does not listen on SHALL require an explicit confirmation. @@ -36,6 +36,18 @@ confirmation. The tool SHALL provide read-only status and operations to allow a port, delete a rule by number, and enable or disable UFW, each mutating action through the preview/confirm/apply flow. A delete SHALL run only if the rule at that number is still the line the operator was shown (fail2ban prepends its bans, shifting the numbers). +Enabling UFW SHALL allow the ports SSH listens on first, asking for confirmation when none is detected, and +deleting a rule that lets SSH in SHALL require confirmation. + +#### Scenario: Enabling never shuts SSH out + +- **WHEN** the operator enables UFW +- **THEN** the plan allows the detected SSH ports before enabling it + +#### Scenario: Deleting the SSH rule is confirmed + +- **WHEN** the numbered rule allows SSH (its port, or the `OpenSSH` profile) +- **THEN** the tool warns that it can lock the operator out and deletes it only on confirmation #### Scenario: Status is read-only diff --git a/openspec/specs/instance-configuration/spec.md b/openspec/specs/instance-configuration/spec.md index 5034792..85c14fd 100644 --- a/openspec/specs/instance-configuration/spec.md +++ b/openspec/specs/instance-configuration/spec.md @@ -154,8 +154,11 @@ operator tunes by hand (`addons_path`, `data_dir`, `logfile`, `http_interface`, current value, and keys the tool does not write SHALL be carried over. The current file SHALL be read as Odoo reads it (`[options]` with keys lower-cased and a continued value kept whole), and its other sections (a module's own, such as `[queue_job]`) SHALL follow unchanged. The Odoo version SHALL be read from the -instance's checkout and the domain from its vhost. A new DB password SHALL be set on the local role as well; a -running service SHALL be restarted so the change is live. +instance's checkout and the domain from its vhost. A new DB password SHALL be set on the local role first, then +the login SHALL be checked with the new values, and only then are the files written; a running service SHALL be +restarted so the change is live — except with a new password for a remote role, which only that server can set: +the tool then leaves the service as it is and says so. The backup's location SHALL be shown even when a step +fails. #### Scenario: Existing files are backed up before regeneration diff --git a/openspec/specs/log-rotation/spec.md b/openspec/specs/log-rotation/spec.md index babca7b..23b23b8 100644 --- a/openspec/specs/log-rotation/spec.md +++ b/openspec/specs/log-rotation/spec.md @@ -7,7 +7,9 @@ Keep an instance's Odoo log, and optionally its nginx logs, bounded with a syste The tool SHALL configure a system `logrotate` policy for an instance's Odoo log at `/etc/logrotate.d/odoo-`, with operator-chosen frequency, retention count, optional compression, and -optional size threshold, using `copytruncate` so the running service is not restarted. +optional size threshold (a number with an optional `k`, `M` or `G`), using `copytruncate` so the running service +is not restarted. The policy SHALL be kept only if `logrotate -d` accepts it; otherwise the previous one is put +back. #### Scenario: Rotation policy is written and validated diff --git a/openspec/specs/web-proxy-tls/spec.md b/openspec/specs/web-proxy-tls/spec.md index c21de1f..2f288c7 100644 --- a/openspec/specs/web-proxy-tls/spec.md +++ b/openspec/specs/web-proxy-tls/spec.md @@ -87,10 +87,17 @@ managed), and copy operator-supplied CRT/KEY with an optional intermediate. - **WHEN** the self-signed strategy is chosen - **THEN** the plan reuses an existing key/fullchain if present, otherwise generates a 2048-bit self-signed certificate for the domain, and sets `root:www-data` ownership with `640`/`644` permissions -#### Scenario: Let's Encrypt and untouched defer certificate work +#### Scenario: Let's Encrypt and untouched use the files that are there -- **WHEN** the Let's Encrypt or do-not-touch strategy is chosen -- **THEN** the plan adds no certificate commands, leaving certificate provisioning to the external tooling +- **WHEN** the Let's Encrypt strategy is chosen +- **THEN** the plan adds no certificate commands and the vhost names `/etc/letsencrypt/live//fullchain.pem` + and `privkey.pem`; **WHEN** do-not-touch is chosen, it names the files the current vhost names (certbot's + included), or else the tool's own + +#### Scenario: No certificate, no HTTPS vhost + +- **WHEN** the files a strategy names do not exist, or a certificate file pick is cancelled +- **THEN** the tool says which file is missing and writes no HTTPS vhost (`nginx -t` would refuse it) ### Requirement: Custom certificate validation diff --git a/tests/test_ops_configs.py b/tests/test_ops_configs.py index 93f5705..d6889eb 100644 --- a/tests/test_ops_configs.py +++ b/tests/test_ops_configs.py @@ -39,7 +39,8 @@ def test_switch_validates_before_keeping(self) -> None: commands = planners.plan_nginx_https(_config(), (1, 24, 0)) script = next(c.command for c in commands if "nginx -t" in c.command) self.assertLess(script.index("ln -sf"), script.index("nginx -t")) - self.assertIn("restore", script.split("nginx -t")[1]) + self.assertIn("fail 'nginx refused", script.split("nginx -t")[1]) + self.assertIn("trap 'fail Interrupted' INT TERM", script) self.assertFalse(any(c.command == "nginx -t" for c in commands)) @@ -53,9 +54,28 @@ def test_nginx_jails_only_with_nginx_logs(self) -> None: def test_web_bans_are_scoped_and_recidive_uses_ufw(self) -> None: text = planners._fail2ban_base_content("127.0.0.1/8", "1h", "10m", 8, "24h", True) self.assertIn("banaction_allports = ufw", text) - self.assertEqual(text.count('banaction = ufw[application="Nginx Full"]'), 2) + self.assertEqual(text.count("banaction = ufw-odoo-web"), 2) jail = planners._fail2ban_odoo_jail_content("odoo-auth-shop", "/var/log/odoo/shop.log", "1h", "10m", 8) - self.assertIn('banaction = ufw[application="Nginx Full"]', jail) + self.assertIn("banaction = ufw-odoo-web", jail) + # Only the web ports, with nothing fail2ban 0.11.2 would leave unquoted. + self.assertIn("actionban = ufw prepend reject from to any port 80,443 proto tcp", + planners._fail2ban_web_action_content()) + + def test_backend_is_never_set_for_every_jail(self) -> None: + text = planners._fail2ban_base_content("127.0.0.1/8", "1h", "10m", 8, "24h", True) + self.assertNotIn("backend", text.split("[sshd]")[0]) + self.assertNotIn("backend = systemd", text) + journal = planners._fail2ban_base_content("127.0.0.1/8", "1h", "10m", 8, "24h", True, sshd_systemd=True) + self.assertIn("[sshd]\nenabled = true\nbackend = systemd", journal) + + def test_typed_durations_and_networks(self) -> None: + from instance_manager.workflows.fail2ban import _duration_error, _networks_error + for good in ("10m", "1h", "1d", "2w", "1mo", "-1", "600"): + self.assertIsNone(_duration_error(good), good) + for bad in ("1 h", "1h\nenabled = false", "forever"): + self.assertIsNotNone(_duration_error(bad), bad) + self.assertIsNone(_networks_error("203.0.113.7, 10.0.0.0/8 2001:db8::/32")) + self.assertIsNotNone(_networks_error("203.0.113.7\nignoreip = 0.0.0.0/0")) def test_filter_is_tested_on_every_versions_line(self) -> None: commands = planners.plan_fail2ban_enable_odoo_instance("shop", "/var/log/odoo/shop.log") diff --git a/tests/test_ops_hardening.py b/tests/test_ops_hardening.py new file mode 100644 index 0000000..5c0ea1e --- /dev/null +++ b/tests/test_ops_hardening.py @@ -0,0 +1,100 @@ +"""Operations: the update's order, certificate modes, the SSH guard of UFW, and the +staged writes.""" + +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +from instance_manager import planners +from instance_manager.models import InstanceConfig +from instance_manager.workflows import install +from instance_manager.workflows.firewall import _allows_ssh + + +def _config(**values) -> InstanceConfig: + config = InstanceConfig(instance="shop", **values) + config.normalize_defaults() + config.ensure_strong_secrets() + return config + + +class UpdateOrderTests(unittest.TestCase): + def test_password_then_login_check_then_writes(self) -> None: + steps = [c.description for c in planners.plan_update_instance_config( + _config(), {}, new_db_password=True, restart=True)] + alter = steps.index('Set the new password on the local PostgreSQL role') + check = steps.index('Validate DB user login') + first_write = next(i for i, d in enumerate(steps) if "/etc/odoo/shop/shop.conf" in d) + self.assertLess(alter, check) + self.assertLess(check, first_write) + self.assertEqual(steps[-1], 'Restart the Odoo service to load the configuration') + + def test_a_remote_new_password_waits_for_its_server(self) -> None: + steps = [c.description for c in planners.plan_update_instance_config( + _config(db_host="db.example.com"), {}, new_db_password=True, restart=True)] + self.assertNotIn('Validate DB user login', steps) + self.assertNotIn('Restart the Odoo service to load the configuration', steps) + + +class CertificateModeTests(unittest.TestCase): + def test_lets_encrypt_names_its_live_files_when_they_exist(self) -> None: + config = _config(domain="erp.example.com") + with mock.patch.object(install, "choose", return_value="Let's Encrypt (managed externally)"), \ + mock.patch.object(install, "path_exists", return_value=True): + self.assertEqual(install._maybe_plan_certs(config), []) + self.assertEqual(config.tls_cert, "/etc/letsencrypt/live/erp.example.com/fullchain.pem") + self.assertIn("ssl_certificate /etc/letsencrypt/live/erp.example.com/fullchain.pem;", + planners._nginx_https_content(config, (1, 24, 0))) + + def test_no_certificate_means_no_https_plan(self) -> None: + for mode in ("Let's Encrypt (managed externally)", 'Leave certificates untouched', ""): + with mock.patch.object(install, "choose", return_value=mode), \ + mock.patch.object(install, "path_exists", return_value=False): + self.assertIsNone(install._maybe_plan_certs(_config()), mode) + + def test_untouched_keeps_the_files_the_vhost_names(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + vhost = Path(tmp) / "shop-https.conf" + vhost.write_text("server {\n ssl_certificate /etc/letsencrypt/live/x/fullchain.pem;\n" + " ssl_certificate_key /etc/letsencrypt/live/x/privkey.pem;\n}\n", encoding="utf-8") + config = _config() + real_open = open + with mock.patch("builtins.open", lambda path, *a, **k: real_open( + vhost if str(path).endswith("shop-https.conf") else path, *a, **k)), \ + mock.patch.object(install, "choose", return_value='Leave certificates untouched'), \ + mock.patch.object(install, "path_exists", return_value=True): + self.assertEqual(install._maybe_plan_certs(config), []) + self.assertEqual((config.tls_cert, config.tls_key), + ("/etc/letsencrypt/live/x/fullchain.pem", "/etc/letsencrypt/live/x/privkey.pem")) + + def test_a_cancelled_file_pick_cancels(self) -> None: + with mock.patch.object(install, "choose", return_value='Copy your own certificates (CRT/KEY[/Intermediate])'), \ + mock.patch.object(install, "select_file_path", return_value=""): + self.assertIsNone(install._maybe_plan_certs(_config())) + + +class UfwGuardTests(unittest.TestCase): + def test_ssh_rules_are_recognised(self) -> None: + self.assertTrue(_allows_ssh("[ 1] 22/tcp ALLOW IN Anywhere", [22])) + self.assertTrue(_allows_ssh("[ 3] OpenSSH ALLOW IN Anywhere", [2222])) + self.assertTrue(_allows_ssh("[ 2] 2222/tcp (v6) ALLOW IN Anywhere (v6)", [2222])) + self.assertFalse(_allows_ssh("[ 4] 443/tcp ALLOW IN Anywhere", [22])) + self.assertFalse(_allows_ssh("[ 5] Anywhere REJECT IN 203.0.113.7", [22])) + + +class StagedWriteTests(unittest.TestCase): + def test_every_file_is_backed_up_before_any_is_written(self) -> None: + script = planners.staged_files_command([("/a", "x", "644"), ("/b", "y", "644")], "true") + lines = script.splitlines() + last_backup = max(i for i, line in enumerate(lines) if '"$backup/' in line and line.startswith("if [ -e")) + first_write = min(i for i, line in enumerate(lines) if line.startswith("cat > ")) + self.assertLess(last_backup, first_write) + self.assertEqual(lines[0], "backup=$(mktemp -d) || exit 1") + self.assertIn("trap 'fail Interrupted' INT TERM", script) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/verify_ops_configs.py b/tools/verify_ops_configs.py index e22546a..8288450 100644 --- a/tools/verify_ops_configs.py +++ b/tools/verify_ops_configs.py @@ -21,10 +21,12 @@ import argparse import glob import os +import re import shutil import subprocess import sys import tempfile +import time import urllib.request from pathlib import Path from unittest import mock @@ -35,7 +37,27 @@ from instance_manager.models import InstanceConfig # noqa: E402 FAILURES: list[str] = [] -FAIL2BAN_VERSION = "1.1.0" +# 1.1.0 (Ubuntu 24.04), 1.0.2 (Debian 12), 0.11.2 (Ubuntu 22.04, Debian 11). +FAIL2BAN_VERSIONS = ("1.1.0", "1.0.2", "0.11.2") + + +def _python_for(version: str) -> str | None: + """An interpreter the release runs on: before 1.1 it imports asynchat (gone in + 3.12), and 0.11.2 also collections.MutableMapping (gone in 3.10); the + distributions patch their packages, the source release is not.""" + wanted = {"1.0.2": ("3.11", "3.10"), "0.11.2": ("3.9", "3.8")}.get(version) + if wanted is None: + return sys.executable + for minor in wanted: + found = shutil.which(f"python{minor}") + if found: + return found + uv = shutil.which("uv") + for minor in wanted if uv else (): + result = subprocess.run([uv, "python", "find", minor], capture_output=True, text=True) + if result.returncode == 0 and result.stdout.strip(): + return result.stdout.strip() + return None def check(name: str, ok: bool, detail: str = "") -> None: @@ -126,6 +148,22 @@ def where(config: InstanceConfig, kind: str) -> str: for leftover in (prefix / "sites-enabled").iterdir(): leftover.unlink() + # Let's Encrypt's files (or the ones a vhost already names), and an nginx + # whose version is unknown. + live = prefix / "live" / "le.example.com" + live.mkdir(parents=True) + le = InstanceConfig(instance="le", http_port=8269, gevent_port=8272, domain="le.example.com", + tls_cert=str(live / "fullchain.pem"), tls_key=str(live / "privkey.pem")) + _run(["openssl", "req", "-x509", "-nodes", "-newkey", "rsa:2048", "-days", "1", "-subj", + f"/CN={le.domain}", "-keyout", le.tls_key, "-out", le.tls_cert]) + for label, nginx_version in (("its version", version), ("an unknown version", None)): + (prefix / "sites-enabled" / "le.conf").write_text( + _unprivileged(planners._nginx_https_content(le, nginx_version))) + result = _run([str(stub), "-t"], env=env) + check(f"nginx -t accepts a vhost naming Let's Encrypt's files, written for {label}", + result.returncode == 0, result.stderr) + (prefix / "sites-enabled" / "le.conf").unlink() + shop = configs[0] avail = lambda name: str(prefix / "sites-available" / name) # noqa: E731 enabled = lambda name: str(prefix / "sites-enabled" / name) # noqa: E731 @@ -155,10 +193,10 @@ def where(config: InstanceConfig, kind: str) -> str: # --- fail2ban -------------------------------------------------------------------- -def _fail2ban_src(root: Path, given: str | None) -> Path | None: +def _fail2ban_src(root: Path, given: str | None, version: str) -> Path | None: if given: return Path(given) - url = f"https://github.com/fail2ban/fail2ban/archive/refs/tags/{FAIL2BAN_VERSION}.tar.gz" + url = f"https://github.com/fail2ban/fail2ban/archive/refs/tags/{version}.tar.gz" archive = root / "f2b.tgz" try: urllib.request.urlretrieve(url, archive) # noqa: S310 - fixed https URL @@ -167,21 +205,31 @@ def _fail2ban_src(root: Path, given: str | None) -> Path | None: print(f"skip could not download fail2ban: {error}") return None _run(["tar", "-xzf", str(archive), "-C", str(root)]) - return root / f"fail2ban-{FAIL2BAN_VERSION}" + return root / f"fail2ban-{version}" -def _fail2ban(root: Path, given: str | None) -> None: - src = _fail2ban_src(root, given) +def _fail2ban(root: Path, given: str | None, version: str) -> None: + python = _python_for(version) + if python is None: + check(f"fail2ban {version}: an interpreter it runs on is available", False) + return + src = _fail2ban_src(root, given, version) if src is None: return + print(f"info fail2ban {version} on {python}", flush=True) + if version in {"0.11.2", "1.0.2"}: + # Their sources are converted at packaging time (2to3), as the distributions do. + _run([python, "-m", "lib2to3", "-w", "-n", "--no-diffs", "bin", "fail2ban"], cwd=src) env = {"PYTHONPATH": str(src), "PATH": "/usr/bin:/bin"} - regex = [sys.executable, str(src / "bin" / "fail2ban-regex")] - client = [sys.executable, str(src / "bin" / "fail2ban-client")] + regex = [python, str(src / "bin" / "fail2ban-regex")] + client = [python, str(src / "bin" / "fail2ban-client")] conf = root / "conf" shutil.copytree(src / "config", conf) (conf / "jail.d").mkdir(exist_ok=True) filter_path = conf / "filter.d" / "odoo-auth.conf" filter_path.write_text(planners._fail2ban_odoo_filter_content(), encoding="utf-8") + (conf / "action.d" / f"{planners.FAIL2BAN_WEB_ACTION}.conf").write_text( + planners._fail2ban_web_action_content(), encoding="utf-8") for sample in planners.ODOO_LOGIN_FAILED_SAMPLES: result = _run([*regex, "--out", "ip", sample, str(filter_path)], env=env) @@ -229,8 +277,37 @@ def base_content(nginx: bool) -> str: (logs / "nginx" / "shop.error.log").write_text("", encoding="utf-8") base.write_text(base_content(True), encoding="utf-8") result = _run(test, env=env) - check("with nginx logs, the base jails (web bans scoped to Nginx Full) pass", result.returncode == 0, - result.stdout + result.stderr) + check(f"fail2ban {version}: with nginx logs, the base jails (web bans on the web ports) pass", + result.returncode == 0, result.stdout + result.stderr) + # The ban fail2ban would run, rendered by fail2ban itself, then run against a + # stub ufw: one address, the web ports, nothing split or left unquoted. + dump = _run([*client, "-c", str(conf), "-d"], env=env).stdout + bans = re.findall(r"'actionban', '(ufw [^']*)'", dump) + stub = root / "stub" + stub.mkdir(exist_ok=True) + (stub / "ufw").write_text('#!/bin/sh\nprintf "%s|" "$@"\n', encoding="utf-8") + (stub / "ufw").chmod(0o755) + ran = [_run(ban.replace("", "203.0.113.7"), env={"PATH": f"{stub}:/usr/bin:/bin"}).stdout for ban in bans] + check(f"fail2ban {version}: a web ban rejects the address on 80 and 443 only", + bool(bans) and all(r == "prepend|reject|from|203.0.113.7|to|any|port|80,443|proto|tcp|" for r in ran), + f"{bans} {ran}") + sshd = planners._fail2ban_base_content("127.0.0.1/8 ::1", "1h", "10m", 8, "24h", True).replace( + "logpath = /var/log/fail2ban.log", f"logpath = {logs}/fail2ban.log") + check(f"fail2ban {version}: no backend is forced on every jail", "backend" not in sshd.split("[sshd]")[0]) + # Negative control: fail2ban's own ufw action with the application profile. + old = conf / "jail.d" / "old-web.local" + old.write_text(f'[nginx-http-auth]\nbanaction = ufw[application="{planners.UFW_WEB_APPLICATION}"]\n', + encoding="utf-8") + dump = _run([*client, "-c", str(conf), "-d"], env=env).stdout + old.unlink() + old_bans = re.findall(r"'actionban', '([^']*ufw[^']*)'", dump) + ran = [_run(ban.replace("", "203.0.113.7").replace("\\n", "\n"), env={"PATH": f"{stub}:/usr/bin:/bin"}).stdout + for ban in old_bans] + split = any("|Nginx|Full|" in r for r in ran) + print(f"info fail2ban {version}: its own ufw[application=…] action passes the name " + f"{'split in two (ufw refuses it)' if split else 'as one word'}", flush=True) + if version == "0.11.2": + check("negative control: on 0.11.2 fail2ban's own action splits the application name", split, str(ran)) jail = conf / "jail.d" / "odoo-auth-shop.local" content = planners._fail2ban_odoo_jail_content("odoo-auth-shop", str(logs / "odoo.log"), "1h", "10m", 8) @@ -249,6 +326,64 @@ def base_content(nginx: bool) -> str: check("the staged writes leave no temporary file", not leftovers, ", ".join(leftovers)) +def _staged_failures(root: Path) -> None: + """The staged write restores everything on a failed write or an interruption, and + touches nothing when it cannot make its backup directory.""" + first, second = root / "a.conf", root / "sub" / "b.conf" + second.parent.mkdir() + first.write_text("old a\n", encoding="utf-8") + second.write_text("old b\n", encoding="utf-8") + files = [(str(first), "new a\n", "644"), (str(second), "new b\n", "644")] + second.parent.chmod(0o555) # the second write fails + try: + result = _run(planners.staged_files_command(files, "true")) + finally: + second.parent.chmod(0o755) + check("a failed write fails the step", result.returncode != 0) + check("and puts back the file already written", first.read_text(encoding="utf-8") == "old a\n", + first.read_text(encoding="utf-8")) + + result = subprocess.run(["bash", "-c", planners.staged_files_command(files, "true")], capture_output=True, + text=True, env={**os.environ, "TMPDIR": str(root / "missing")}) + check("without a backup directory nothing is written", + result.returncode != 0 and first.read_text(encoding="utf-8") == "old a\n") + + process = subprocess.Popen(["bash", "-c", planners.staged_files_command(files, "sleep 5")]) + time.sleep(1) + process.terminate() + process.wait(timeout=10) + check("an interruption during validation restores every file", + first.read_text(encoding="utf-8") == "old a\n" and second.read_text(encoding="utf-8") == "old b\n") + + +def _logrotate(root: Path) -> None: + """The logrotate policy is kept only if logrotate's dry run accepts it.""" + binary = shutil.which("logrotate") or ("/usr/sbin/logrotate" if Path("/usr/sbin/logrotate").exists() else None) + if binary is None: + check("logrotate is available", not os.environ.get("CI")) + print("skip logrotate not found") + return + from instance_manager.models import InstanceConfig + config = InstanceConfig(instance="shop") + log = root / "shop.log" + log.write_text("", encoding="utf-8") + policy = root / "odoo-shop" + + def staged(maxsize: str) -> str: + [step] = [c for c in planners.plan_logrotate_config(config, maxsize=maxsize) + if "logrotate -d" in c.command] + return (step.command.replace(config.logrotate_config_file, str(policy)) + .replace(config.odoo_log_file, str(log)) + .replace("logrotate -d", f"{binary} -s {root}/state -d")) + + result = _run(staged("50M")) + check("a logrotate policy logrotate accepts is kept", + result.returncode == 0 and "maxsize 50M" in policy.read_text(encoding="utf-8"), result.stderr) + result = _run(staged("10Q")) + check("one it refuses is rolled back to the previous policy", + result.returncode != 0 and "maxsize 50M" in policy.read_text(encoding="utf-8"), result.stderr) + + def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--nginx", help="an nginx binary to use instead of the downloaded package") @@ -257,9 +392,14 @@ def main() -> int: with tempfile.TemporaryDirectory(prefix="oim-verify-ops-") as tmp: root = Path(tmp) (root / "n").mkdir() - (root / "f").mkdir() + (root / "s").mkdir() + _staged_failures(root / "s") + (root / "l").mkdir() + _logrotate(root / "l") _nginx(root / "n", args.nginx) - _fail2ban(root / "f", args.fail2ban_src) + for version in FAIL2BAN_VERSIONS if not args.fail2ban_src else ("given",): + (root / "f" / version).mkdir(parents=True) + _fail2ban(root / "f" / version, args.fail2ban_src, version) print(f"\n{len(FAILURES)} failure(s)" if FAILURES else "\nall checks passed") return 1 if FAILURES else 0