From cf0255d74f790fc33a6345b360246edf48b25f83 Mon Sep 17 00:00:00 2001 From: Guillermo Rojo Date: Thu, 8 Oct 2026 14:26:17 +0200 Subject: [PATCH] fix: guard the paths that remove or overwrite - Install: a new instance needs a free name (no system account or service, nothing on the host under it); Ctrl+C or cancel at the confirmation runs nothing; a failure while applying previews the undo of what that run made and asks first. A pre-existing data dir or role is kept. - Purge: databases by owner or exact name only, superuser roles refused, databases dropped before their filestores, a shared data dir loses only the selected filestores, a role another instance uses is kept, the Linux user goes only when the tool made it, logrotate policy removed. - /var/log/odoo is root's; each instance owns its log, earlier logs get their owner back, logrotate policies lose su. - Delete: owner-checked dropdb before the config goes, data dir from both config locations, nginx reloaded only if installed, logrotate policy removed. - Copies: scoped chown, existing target filestore moved aside, copy into the target after checking the source, REPLACE for a refresh, the data dir of the config being built wins. - Backups: dedicated directories only; retention by name; valid database names from file names. --- CHANGELOG.md | 25 +++ docs/architecture.md | 12 +- docs/installation.md | 19 +- docs/operations/disk-usage.md | 5 +- docs/operations/instance-management.md | 34 +-- docs/operations/log-rotation.md | 7 +- instance_manager/i18n.py | 36 ++++ instance_manager/models.py | 24 +++ instance_manager/planners.py | 47 +++- instance_manager/system.py | 9 + instance_manager/workflows/backup_restore.py | 92 ++++---- instance_manager/workflows/common.py | 112 +++++++++- instance_manager/workflows/diskusage.py | 10 +- instance_manager/workflows/install.py | 114 +++++++--- instance_manager/workflows/manage.py | 52 +++-- instance_manager/workflows/purge.py | 182 +++++++++++----- .../workflows/scheduled_backup.py | 9 +- .../proposal.md | 49 +++++ .../specs/data-backup-restore/spec.md | 193 +++++++++++++++++ .../specs/disk-usage/spec.md | 29 +++ .../specs/execution-safety/spec.md | 43 ++++ .../specs/instance-provisioning/spec.md | 61 ++++++ .../specs/instance-removal/spec.md | 125 +++++++++++ .../specs/log-rotation/spec.md | 19 ++ .../specs/scheduled-backups/spec.md | 36 ++++ .../tasks.md | 26 +++ openspec/specs/data-backup-restore/spec.md | 34 ++- openspec/specs/disk-usage/spec.md | 4 +- openspec/specs/execution-safety/spec.md | 43 ++-- openspec/specs/instance-provisioning/spec.md | 30 ++- openspec/specs/instance-removal/spec.md | 87 +++++--- openspec/specs/log-rotation/spec.md | 4 +- openspec/specs/scheduled-backups/spec.md | 4 +- tests/test_backup_restore.py | 28 ++- tests/test_destructive_guards.py | 203 ++++++++++++++++++ tests/test_planners.py | 3 +- tools/verify_data_safety.py | 193 ++++++++++++++++- 37 files changed, 1732 insertions(+), 271 deletions(-) create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/proposal.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/data-backup-restore/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/disk-usage/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/execution-safety/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-provisioning/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-removal/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/log-rotation/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/scheduled-backups/spec.md create mode 100644 openspec/changes/archive/2026-10-08-guard-destructive-paths/tasks.md create mode 100644 tests/test_destructive_guards.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 33004d0..0baacd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,31 @@ All notable changes to this project are documented here. The format is based on ### Fixed +- **Installing over an existing instance removed it.** The install never checked the name was free, and its + cleanup ran on a failure — or on Ctrl+C at the confirmation prompt — removing that instance's home, config, + unit and vhosts. A new instance now needs a free name (no system account or service such as `backup`, whose + home is `/var/backups`), Ctrl+C at the confirmation changes nothing, and a failure while running previews the + undo of what that run made and asks first; a data dir or role that was already there is kept. +- **The purge dropped other instances' databases.** Every filestore folder selected a database, with no owner + check, so on a shared `/var/lib/odoo` it dropped the others' databases and removed their filestores; a role + named after the instance was dropped even when another instance used it, and `postgres` was accepted as the + instance role. Databases are now selected by owner or exact name, superusers are refused, a shared data dir + loses only the selected filestores, and the Linux user is removed only when the tool made it. +- **Every install took `/var/log/odoo`** (`chown -R`), so the other instances could not write their logs, their + fail2ban jails saw nothing and logrotate's `su` failed. The directory is root's, each instance owns its log, + earlier logs get their owner back, and the logrotate policies lose `su`. +- **Delete instance** without nginx never dropped the database (`nginx -t` failed first); it now drops only a + database its role owns, reloads nginx only if installed, reads the data dir from the legacy config location + too, and removes the logrotate policy, as the purge does. +- **Copies:** a duplicate chowned the whole data dir (other instances' files when shared), nested the copy in an + existing target, and a refresh deleted the target filestore and asked for a phrase naming the source. The copy + now lands in the target, an existing one is moved aside, ownership is scoped, and a refresh asks for + `REPLACE `. A replica's filestore went to `~/.local/share/Odoo` while its config named + `/var/lib/odoo/`. +- **Backups:** a shared directory (`/tmp`, `/`) was accepted and made `700`; retention kept the newest by mtime, + so an old backup copied back in outlived newer ones. Backup directories must be dedicated and retention keeps + the newest names. + - **A failed install could drop a PostgreSQL role it had not created.** The cleanup of the PostgreSQL and Odoo + PostgreSQL installs dropped the instance's role even when it existed before; it now drops it only when this run created it. diff --git a/docs/architecture.md b/docs/architecture.md index 7a6a712..0ee5289 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -73,15 +73,15 @@ flowchart LR confirm -- no --> menu confirm -- yes --> apply["Apply in order
(root only)"] apply -- a step fails --> stop(["Stop at that step
report it by name"]) - stop -- install --> cleanup["Clean the install's residues"] - cleanup --> menu + stop -- install --> cleanup{"Undo what
this run made?"} + cleanup -- yes or no --> menu classDef step fill:#dbeafe,stroke:#2563eb,color:#1e3a8a classDef ask fill:#fef3c7,stroke:#d97706,color:#78350f classDef guard fill:#dcfce7,stroke:#16a34a,color:#14532d classDef stop fill:#fee2e2,stroke:#dc2626,color:#7f1d1d class collect,build,apply step - class phrase,confirm ask - class preview,cleanup guard + class phrase,confirm,cleanup ask + class preview guard class stop,menu stop ``` @@ -92,8 +92,8 @@ flowchart LR - Destructive actions first ask for `confirm_with_phrase` — the operator types an exact phrase naming the operation and instance — and only then show the plan. - `preview_commands` renders the whole plan before anything runs; `apply_commands` runs it in order and stops - at the first failing step. An install that fails, or is interrupted with Ctrl+C, then removes what it created - and returns to the menu. + at the first failing step. An install that fails, or is interrupted with Ctrl+C while it runs, previews the + steps that undo what it made and asks before running them; then it returns to the menu. ## Diagram conventions diff --git a/docs/installation.md b/docs/installation.md index 44d631e..85bd55e 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -40,8 +40,9 @@ derived path): 1. Installs OS build dependencies and the PostgreSQL client (apt runs unattended and waits for the dpkg lock). 2. Creates the system user `` and the directory layout under `/opt/odoo/` - (`odoo`, `addons-oca`, `addons-custom`) plus `/etc/odoo/`, `/var/log/odoo` and the data dir - `/var/lib/odoo/` (filestores and sessions, outside the home). + (`odoo`, `addons-oca`, `addons-custom`) plus `/etc/odoo/` and the data dir + `/var/lib/odoo/` (filestores and sessions, outside the home). `/var/log/odoo` stays root's; the + instance owns only its own log, `/var/log/odoo/.log`. 3. Clones the core you chose — **Odoo** (official) or **OCB** (OCA's backports, same branches) — at the requested branch (only if absent). 4. Builds the virtualenv with the Python interpreter the version needs (see below), installs pip, wheel and the @@ -140,12 +141,18 @@ For HTTPS you pick a certificate strategy: | **Let's Encrypt (managed externally)** | Adds no certificate commands — you manage LE outside the tool. | | **Copy your own certificates** | Installs your CRT/KEY (+ optional intermediate), builds the fullchain, and **validates that the key matches the certificate** before Nginx is reconfigured. | +## The instance name must be free + +The name becomes the instance's Linux user, systemd unit, home and log, so an install refuses a system account +or service name (`backup`, `nginx`, `postgres`, …) and a name the host already has a home, config, unit, vhost, +SSL directory or user for. To change an existing instance, use **Manage instances**. + ## If an install fails -If an install plan fails partway, or you interrupt it with Ctrl+C, the tool runs a **best-effort cleanup** of -that instance's residues — service, config, home, Nginx vhosts, SSL dir, the new data dir while it holds no -filestore, and the DB role when this run created it (a role that already existed is kept) — and returns to the -menu, so you can retry cleanly. +Cancelling, or pressing Ctrl+C, at the plan confirmation changes nothing. If the plan fails partway, or you +interrupt it while it runs, the tool shows the steps that undo what that run made — service, config, home, +Nginx vhosts, SSL dir, log, logrotate policy, Linux user, the data dir if the run created it, and the DB role if +the run created it — and asks before running them. Then it returns to the menu, so you can retry cleanly. ## Related diff --git a/docs/operations/disk-usage.md b/docs/operations/disk-usage.md index ed4b30b..5464799 100644 --- a/docs/operations/disk-usage.md +++ b/docs/operations/disk-usage.md @@ -24,8 +24,9 @@ Removes the oldest backups, **keeping the N most recent of each kind per databas filestore archives (`.filestore.tar.gz`) are counted separately. Only names of the exact form `----` are matched, so backups of another instance whose name starts the same way (`shop_eu` next to `shop`) are never touched. Backups written before the database was part of the name -(`_`) are pruned as one more group. You choose N; the plan is previewed before it runs. A -missing backup directory is a no-op. +(`_`) are pruned as one more group. The most recent are the latest timestamps in the +names, so a backup copied back in later does not count as new. You choose N; the plan is previewed before it +runs. A missing backup directory is a no-op. ## Related diff --git a/docs/operations/instance-management.md b/docs/operations/instance-management.md index f527d37..bb75a8f 100644 --- a/docs/operations/instance-management.md +++ b/docs/operations/instance-management.md @@ -79,7 +79,8 @@ To go back, copy the files from the `config_preupdate/` directory and ### Backups -**Create backup** exports the database and/or filestore into the backup directory you choose: +**Create backup** exports the database and/or filestore into the backup directory you choose — a dedicated one, +such as `/var/backups/`; a shared directory (`/tmp`, `/var/backups`, …) is refused: - Database → `pg_dump -Fc` → `----.dump` - Filestore → gzipped tar → `----.filestore.tar.gz` @@ -92,7 +93,8 @@ by root only: a dump holds password hashes, API keys and mail or payment secrets ### Restore and duplicate — copied vs moved **Restore backup**, **Duplicate database** and **Duplicate instance** ask how the copy relates to its source, -and each asks for its confirmation phrase (`RESTORE ` / `DUPLICATE `): +and each asks for its confirmation phrase (`RESTORE `, `DUPLICATE `, or `REPLACE ` +for a refresh in place): - **Copied (new UUID on target)** — the target gets its own `database.uuid`, `database.secret` and creation date, as Odoo's own copy does. @@ -140,7 +142,8 @@ what in a database can still act on the outside, rule by rule, and whether the m Restore refuses to overwrite an existing target **database**. An existing target **filestore** needs an explicit overwrite, and is then moved aside to `.replaced-`, not deleted; the restored files are -handed to the instance user. +handed to the instance user. When the data dir may be shared with other instances (a custom `data_dir`), only +the restored database's filestore changes owner. ### Duplicate instance — replica or refresh @@ -158,7 +161,9 @@ differs from the source database's owner. - **Target exists → refresh in place.** The tool stops the target service, replaces its database and filestore from the source, applies the semantics, and restarts it, without recreating its config or service. The target's database is dropped only when it belongs to the target's own role (from its `odoo.conf`) and you - confirm the overwrite; the source instance or database can never be the target. + confirm the overwrite with `REPLACE `; its previous filestore is moved aside, not deleted. The source + instance or database can never be the target, and a name that is a system account or a half-removed + instance is neither refreshed nor provisioned. Every seeded database is **restricted to its owner** (`CONNECT` revoked from `PUBLIC`, granted to the owning role), and the target's data dir is owned by the target user. @@ -181,10 +186,10 @@ Two levels, both phrase-gated and asked before the plan is shown: | Action | Removes | Phrase | |--------|---------|--------| -| **Delete instance** (in *Manage instances*) | Service, backup timer, config, home, Nginx vhosts, SSL; optionally the database and one database's filestore | `DELETE ` | -| **Remove instances** (main menu → total purge) | Everything above **plus** the Linux user, Odoo and Nginx logs, the fail2ban jail, the filestore root, the instance's databases, and its PostgreSQL roles | `DELETE-ALL ` | +| **Delete instance** (in *Manage instances*) | Service, backup timer, config, logrotate policy, home, Nginx vhosts, SSL; optionally the database (if its owner is the instance's role) and one database's filestore | `DELETE ` | +| **Remove instances** (main menu → total purge) | Everything above **plus** the Linux user, Odoo and Nginx logs, the fail2ban jail, the instance's filestores, its databases, and its PostgreSQL roles | `DELETE-ALL ` | -Neither removes the instance's logrotate policy (`/etc/logrotate.d/odoo-`). +Both reload Nginx only when it is installed, and go on if `nginx -t` fails because of another site. **Delete instance keeps the filestores you did not ask to delete.** An instance installed with `data_dir = /var/lib/odoo/` keeps its data dir where it is. An older instance without a `data_dir` @@ -192,12 +197,15 @@ keeps its filestores inside the home, so the plan first moves its data dir to `/var/backups//kept-data-dir-` and only then removes the home. The **total purge** selects the databases the instance's DB role owns and the one named exactly like the -instance, plus those found in its filestore root and any you add. Databases whose name merely starts with the -instance name (`shop2`, `shop_eu` when purging `shop`) are listed as **not selected**; add them yourself if they -belong to it. When another instance connects with the same DB role, the purge says so, selects nothing by owner -and keeps the role. Without admin DB access it performs the local cleanup only. It removes the filestore root -(`/filestore`), not the rest of the data dir. The full contract is the -[removal spec](../../openspec/specs/instance-removal/spec.md). +instance, plus any you add. A filestore folder, or a name that merely starts with the instance name (`shop2`, +`shop_eu` when purging `shop`), is listed as **not selected**; add those yourself if they belong to it. The role +cannot be `postgres` or another superuser. When another instance connects with the same DB role, the purge says +so, selects nothing by owner and keeps the role. Without admin DB access it performs the local cleanup only. + +The databases are dropped first, then their files. The instance's own data dir (`/var/lib/odoo/`) is +removed whole; from any other data dir, which other instances may share, only the selected databases' +filestores are removed. The Linux user is removed only when its home is `/opt/odoo/`. The full +contract is the [removal spec](../../openspec/specs/instance-removal/spec.md). ## Related diff --git a/docs/operations/log-rotation.md b/docs/operations/log-rotation.md index 718b970..013d2dd 100644 --- a/docs/operations/log-rotation.md +++ b/docs/operations/log-rotation.md @@ -21,9 +21,10 @@ policy for an instance's Odoo log (`/var/log/odoo/.log`). - **Compression** — gzip the rotated files (adds `compress` + `delaycompress`). - **Size threshold** (optional) — also rotate when the log exceeds a size (e.g. `50M`, `1G`). -The policy uses **`copytruncate`**, so Odoo keeps writing to the same file and does **not** need a restart. An -`su ` directive lets logrotate rotate the instance-owned log safely. The plan installs -`logrotate` if missing and validates the file with `logrotate -d` before finishing. +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. 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/instance_manager/i18n.py b/instance_manager/i18n.py index b6a0af3..fa5362d 100644 --- a/instance_manager/i18n.py +++ b/instance_manager/i18n.py @@ -933,4 +933,40 @@ def tf(template: str, *args: object, **kwargs: object) -> str: 'certificate not found': 'certificado no encontrado', 'Custom': 'Personalizado', 'Incomplete TLS': 'TLS incompleto', + 'Make /var/log/odoo root-owned (755)': 'Hacer /var/log/odoo propiedad de root (755)', + 'Drop su from the Odoo logrotate policies (root rotates them)': 'Quitar su de las políticas logrotate de Odoo (las rota root)', + 'Give each instance its own log back ({} included)': 'Devolver a cada instancia su propio log ({} incluido)', + 'Check the source filestore {} exists': 'Comprobar que existe el filestore origen {}', + 'Move a previous target filestore aside to {}': 'Apartar el filestore destino anterior a {}', + '{} is a shared directory: use a dedicated one, such as /var/backups/.': '{} es un directorio compartido: usa uno dedicado, como /var/backups/.', + 'Own the filestore of {} by the instance user': 'Dar el filestore de {} al usuario de la instancia', + 'Something named {} already exists on this host ({}). Update or delete it from Manage instances, or choose another name.': 'Ya existe algo llamado {} en este host ({}). Actualízalo o bórralo desde Gestionar instancias, o elige otro nombre.', + '[Cleanup] Remove the instance log and logrotate policy': '[Limpieza] Eliminar el log de la instancia y su política logrotate', + '[Cleanup] Remove the instance Linux user': '[Limpieza] Eliminar el usuario Linux de la instancia', + 'Undo what this installation made (the steps above)?': '¿Deshacer lo que ha creado esta instalación (los pasos de arriba)?', + 'Cleanup finished. Review the messages above.': 'Limpieza terminada. Revisa los mensajes de arriba.', + 'Nothing was undone: the steps above list what the installation may have left.': 'No se ha deshecho nada: los pasos de arriba indican lo que la instalación puede haber dejado.', + '[Cleanup] Remove the new data dir': '[Limpieza] Eliminar el data dir nuevo', + 'The installation {}.': 'La instalación {}.', + 'Remove the logrotate policy': 'Eliminar la política logrotate', + 'Validate and reload Nginx (if installed)': 'Validar y recargar Nginx (si está instalado)', + 'Database {} belongs to role {}, not to this instance ({}): it is not deleted.': 'La base de datos {} pertenece al rol {}, no a esta instancia ({}): no se borra.', + '{} is an administrator role, not an instance role.': '{} es un rol de administración, no de una instancia.', + 'Could not check role {}: {}': 'No se pudo comprobar el rol {}: {}', + '{} is a superuser role, not an instance role.': '{} es un rol superusuario, no de una instancia.', + 'Remove the instance Odoo/Nginx logs and logrotate policy': 'Eliminar los logs Odoo/Nginx de la instancia y su política logrotate', + '[INFO] Not selected — a filestore folder exists but role {} does not own the database: {}. Add any that belong to this instance below.': '[INFO] No seleccionadas — existe la carpeta de filestore pero el rol {} no es propietario de la base de datos: {}. Añade abajo las que sean de esta instancia.', + 'Remove the instance data dir {}': 'Eliminar el data dir de la instancia {}', + 'Remove the filestore {}': 'Eliminar el filestore {}', + 'Linux user {} kept: its home is not {}': 'Usuario Linux {} conservado: su home no es {}', + 'Role {} kept: it still owns objects or is in use.': 'Rol {} conservado: aún es propietario de objetos o está en uso.', + 'home': 'home', + 'config': 'configuración', + 'systemd unit': 'unidad systemd', + 'Nginx vhost': 'vhost de Nginx', + 'SSL directory': 'directorio SSL', + 'Linux user': 'usuario Linux', + 'That name is a system account or service name. Choose another instance name.': 'Ese nombre es de una cuenta o un servicio del sistema. Elige otro nombre de instancia.', + 'Data dirs': 'Data dirs', + 'Filestore folders': 'Carpetas de filestore', } diff --git a/instance_manager/models.py b/instance_manager/models.py index 222e728..8065bf1 100644 --- a/instance_manager/models.py +++ b/instance_manager/models.py @@ -29,6 +29,30 @@ ) +# Names a new instance must not take: the instance name is also its Linux user, its +# systemd unit, its home and log names, so a system account or service name would +# hand the tool an existing account (``userdel -r backup`` removes /var/backups) or +# shadow a distribution unit (``/etc/systemd/system/nginx.service``). +RESERVED_INSTANCE_NAMES = frozenset({ + # Debian/Ubuntu base accounts and common service accounts + "root", "daemon", "bin", "sys", "sync", "games", "man", "lp", "mail", "news", "uucp", + "proxy", "backup", "list", "irc", "gnats", "nobody", "messagebus", "syslog", "sshd", + "uuidd", "tcpdump", "tss", "landscape", "pollinate", "lxd", "usbmux", "dnsmasq", + "polkitd", "postfix", "ntp", "chrony", "ubuntu", "debian", "admin", "www_data", + # services this tool configures or relies on + "postgres", "postgresql", "nginx", "ssh", "cron", "fail2ban", "ufw", "rsyslog", + "logrotate", "certbot", "systemd", "networking", "docker", "snapd", "dbus", "odoo", + # directory names the tool keeps beside the instances + "ssl", "default", "filestore", "sessions", +}) + + +def reserved_name_error(name: str) -> str | None: + if name in RESERVED_INSTANCE_NAMES: + return "That name is a system account or service name. Choose another instance name." + return None + + def is_valid_db_name(name: str) -> bool: """True if ``name`` is a database name Odoo accepts and every command can take.""" return bool(DB_NAME_RE.fullmatch(name or "")) diff --git a/instance_manager/planners.py b/instance_manager/planners.py index 4243837..02064ea 100644 --- a/instance_manager/planners.py +++ b/instance_manager/planners.py @@ -458,7 +458,6 @@ def _logrotate_content( " missingok", " notifempty", " copytruncate", - f" su {config.odoo_user} {config.odoo_user}", ] if compress: lines.append(" compress") @@ -738,6 +737,39 @@ def plan_fail2ban_ensure_odoo_filter() -> list[Command]: ] +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 + logrotate (as root) rotates them with no ``su``. + + Earlier installs gave the whole directory to the newest instance; the logs of + the other tool-made instances (user home ``/opt/odoo/``) get their owner + back, and their logrotate policies lose the ``su`` that root rotation no longer + needs.""" + log = shlex.quote(config.odoo_log_file) + owner = f"{shlex.quote(config.odoo_user)}:{shlex.quote(config.odoo_user)}" + return [ + Command( + 'Make /var/log/odoo root-owned (755)', + "install -d -m 755 /var/log/odoo && chown root:root /var/log/odoo && chmod 755 /var/log/odoo", + ), + Command( + tf('Give each instance its own log back ({} included)', config.odoo_log_file), + f"touch {log} && chown {owner} {log} && chmod 640 {log} && " + "for f in /var/log/odoo/*.log; do [ -f \"$f\" ] || continue; " + 'u=$(basename "$f" .log); ' + '[ "$(getent passwd "$u" | cut -d: -f6)" = "/opt/odoo/$u" ] && chown "$u:$u" "$f"; ' + "done; true", + ), + Command( + 'Drop su from the Odoo logrotate policies (root rotates them)', + "for f in /etc/logrotate.d/odoo-*; do [ -f \"$f\" ] || continue; " + "grep -q '^/var/log/odoo/' \"$f\" || continue; " + "sed -i '/^[[:space:]]*su [a-z_][a-z0-9_]* [a-z_][a-z0-9_]*$/d' \"$f\"; done; true", + ), + ] + + def plan_odoo_base_setup( config: InstanceConfig, service_autostart: bool = True, start_now: bool = True ) -> list[Command]: @@ -759,12 +791,13 @@ def plan_odoo_base_setup( ), Command( 'Create instance directories', - f"mkdir -p '{config.odoo_home}/odoo' '{config.odoo_home}/addons-oca' '{config.odoo_home}/addons-custom' '{config.odoo_conf_dir}' /var/log/odoo", + f"mkdir -p '{config.odoo_home}/odoo' '{config.odoo_home}/addons-oca' '{config.odoo_home}/addons-custom' '{config.odoo_conf_dir}'", ), Command( 'Adjust base ownership', - f"chown -R '{config.odoo_user}:{config.odoo_user}' '{config.odoo_home}' /var/log/odoo", + f"chown -R '{config.odoo_user}:{config.odoo_user}' '{config.odoo_home}'", ), + *plan_odoo_log_dir(config), Command('Permissions on config folder', f"chmod 750 '{config.odoo_conf_dir}'"), ] if config.data_dir: @@ -1245,11 +1278,13 @@ def _prune_command(dir_word: str, glob_word: str, keep: int) -> str: Both arguments are shell words the caller has already quoted (a literal path, or a quoted ``"$VAR"`` in a script). ``find`` exits 0 when nothing matches, so the command is safe under ``set -euo pipefail``; the glob is exact (prefix + - timestamp + suffix), so it never reaches another instance's or database's files.""" + timestamp + suffix), so it never reaches another instance's or database's files. + The newest are the latest *names*: the timestamp in the name is when the backup + was taken, while a copied or restored file's mtime is not.""" return ( f"find {dir_word} -maxdepth 1 -type f -name {glob_word} " - "-printf '%T@ %p\\n' | sort -rn | tail -n +" + str(int(keep) + 1) + " " - "| cut -d' ' -f2- | xargs -r -d '\\n' rm -f --" + "-printf '%p\\n' | sort -r | tail -n +" + str(int(keep) + 1) + " " + "| xargs -r -d '\\n' rm -f --" ) diff --git a/instance_manager/system.py b/instance_manager/system.py index 94324a7..2addd64 100644 --- a/instance_manager/system.py +++ b/instance_manager/system.py @@ -150,6 +150,15 @@ def db_role_exists(role_name: str) -> bool: return _local_psql_value(f"SELECT 1 FROM pg_roles WHERE rolname = {sql_literal(role_name)}") == "1" +def db_role_absent(role_name: str) -> bool: + """True only when the role certainly does not exist yet: the local server says + so, or there is no local PostgreSQL at all. A role an install may drop on failure + must be one it created, so "cannot tell" is False.""" + if not user_exists("postgres"): + return True + return _local_psql_value(f"SELECT 1 FROM pg_roles WHERE rolname = {sql_literal(role_name)}") == "" + + def database_exists(db_name: str) -> bool: return _local_psql_value(f"SELECT 1 FROM pg_database WHERE datname = {sql_literal(db_name)}") == "1" diff --git a/instance_manager/workflows/backup_restore.py b/instance_manager/workflows/backup_restore.py index 4a2d686..58df6f5 100644 --- a/instance_manager/workflows/backup_restore.py +++ b/instance_manager/workflows/backup_restore.py @@ -43,22 +43,24 @@ read_odoo_conf, run, service_active, - service_exists, ) from ..ui import level_text, render_table from .common import ( DbCredentials, _ask_db_credentials, + _backup_dir_error, _execute_plan, _filestore_path, + _odoo_conf_candidates, + _own_filestore_commands, _pick_db_name, _quote, - _resolve_data_dir, ) from .install import ( _choose_nginx_mode, _maybe_plan_certs, _maybe_plan_wkhtmltopdf, + _new_instance_name_error, _plan_runtime, _prompt_production_hardening, _prompt_secret, @@ -270,7 +272,8 @@ def _backup_instance( db_host, db_port, db_user, db_password = creds.host, creds.port, creds.user, creds.password backup_dir = ask_text( - 'Backup destination directory', f"/var/backups/{config.instance}", required=True + 'Backup destination directory', f"/var/backups/{config.instance}", required=True, + validate=_backup_dir_error, ) backup_mode = choose( 'Backup type', @@ -443,13 +446,7 @@ def _restore_backup( f"tar --no-same-owner -xzf {_quote(filestore_backup)} -C {_quote(target_filestore)}", ) ) - commands.append( - Command( - 'Own the data dir by the instance user (filestore, sessions, …)', - f"chown -R {_quote(config.odoo_user)}:{_quote(config.odoo_user)} " - f"{_quote(_resolve_data_dir(config))}", - ) - ) + commands.extend(_own_filestore_commands(config, target_db)) if not confirm_with_phrase( 'Sensitive restore action detected.', @@ -514,37 +511,33 @@ def _filestore_copy_commands( source_db: str, target_config: InstanceConfig, target_db: str, - overwrite: bool, ) -> list[Command]: - """Copy the source filestore into the **target** instance's data dir, then hand - the **whole data dir** to the target user. + """Copy the source filestore into the **target** instance's data dir and give it + to the target user. - ``mkdir``/``cp`` run as root, so the created ``.local/share/Odoo`` tree would be - root-owned and Odoo (running as the target user) could not create its - ``sessions``/``filestore`` entries. Chowning the entire resolved data dir fixes - that, not just the copied filestore subdirectory.""" + A filestore already at the target is moved aside, never deleted: attachments + created after the copy was taken are still referenced by its database. The copy + lands *in* the target (``src/.``), never nested inside an existing one, and runs + only once the source is known to be there.""" source_filestore = _filestore_path(source_config, source_db) target_filestore = _filestore_path(target_config, target_db) - target_parent = target_filestore.rsplit("/", 1)[0] - target_data_dir = _resolve_data_dir(target_config) - owner = f"{_quote(target_config.odoo_user)}:{_quote(target_config.odoo_user)}" - commands = [ - Command('Create the target filestore base path', f"mkdir -p {_quote(target_parent)}") - ] - if overwrite: - commands.append( - Command('Remove the previous target filestore', f"rm -rf {_quote(target_filestore)}") - ) - commands.append( - Command('Duplicate the filestore', f"cp -a {_quote(source_filestore)} {_quote(target_filestore)}") - ) - commands.append( + kept = f"{target_filestore}.replaced-{datetime.datetime.now():%Y%m%d_%H%M%S}" + return [ Command( - 'Own the target data dir (filestore, sessions, …)', - f"chown -R {owner} {_quote(target_data_dir)}", - ) - ) - return commands + tf('Check the source filestore {} exists', source_filestore), + f"test -d {_quote(source_filestore)}", + ), + Command( + tf('Move a previous target filestore aside to {}', kept), + f"if [ -e {_quote(target_filestore)} ]; then mv -- {_quote(target_filestore)} {_quote(kept)}; fi", + ), + Command( + 'Duplicate the filestore', + f"mkdir -p {_quote(target_filestore)} && " + f"cp -a {_quote(source_filestore + '/.')} {_quote(target_filestore + '/')}", + ), + *_own_filestore_commands(target_config, target_db), + ] def _replicate_venv_packages_command( @@ -648,7 +641,7 @@ def _plan_refresh_target( commands.extend(_hand_over_commands(target_db, target_owner)) if duplicate_filestore: commands.extend( - _filestore_copy_commands(source_config, source_db, target_config, target_db, overwrite=True) + _filestore_copy_commands(source_config, source_db, target_config, target_db) ) commands.append( Command('Start the target Odoo service', f"systemctl start {_quote(target_config.odoo_service)}") @@ -757,7 +750,7 @@ def _plan_replica_target( commands.append(_replicate_venv_packages_command(source_config, target_config)) if duplicate_filestore: commands.extend( - _filestore_copy_commands(source_config, source_db, target_config, target_db, overwrite=False) + _filestore_copy_commands(source_config, source_db, target_config, target_db) ) commands.extend(_post_db_mode_commands( _psql_target_local(target_db), migration_mode, neutralize, _local_url(target_config) @@ -855,7 +848,7 @@ def _duplicate_database( commands.extend(_hand_over_commands(target_db, target_owner)) if duplicate_filestore: commands.extend( - _filestore_copy_commands(config, source_db, config, target_db, overwrite=overwrite) + _filestore_copy_commands(config, source_db, config, target_db) ) if not confirm_with_phrase( @@ -911,11 +904,17 @@ def _duplicate_instance( print(level_text("ERROR", str(error))) return creds - target_exists = ( - service_exists(target_instance) - or path_exists(target_config.odoo_home) - or path_exists(target_config.odoo_conf_file) + # A refresh needs a whole instance of this tool (home and odoo.conf); anything + # less under that name — a system service or account, a half-removed instance — + # is neither refreshed nor provisioned over. + target_exists = path_exists(target_config.odoo_home) and any( + path_exists(conf) for conf in _odoo_conf_candidates(target_instance) ) + if not target_exists: + error = _new_instance_name_error(target_instance) + if error: + print(level_text("ERROR", error)) + return creds method_choice = choose( 'Database copy method', @@ -952,10 +951,9 @@ def _duplicate_instance( if commands is None: return creds - if not confirm_with_phrase( - 'Sensitive duplication action detected.', - f"DUPLICATE {config.instance}", - ): + # A refresh replaces the target's data, so its phrase names the target. + phrase = f"REPLACE {target_config.instance}" if target_exists else f"DUPLICATE {config.instance}" + if not confirm_with_phrase('Sensitive duplication action detected.', phrase): print(level_text("INFO", 'Invalid confirmation. Operation cancelled.')) return creds diff --git a/instance_manager/workflows/common.py b/instance_manager/workflows/common.py index e05d2fb..8dc2529 100644 --- a/instance_manager/workflows/common.py +++ b/instance_manager/workflows/common.py @@ -26,6 +26,8 @@ read_odoo_conf, require_root_for_apply, run, + service_exists, + user_exists, ) from ..ui import level_text @@ -61,18 +63,23 @@ def _read_text_file(path: str) -> str: return "" -def _execute_plan(commands: list[Command]) -> None: +def _confirm_plan(commands: list[Command]) -> bool: + """Show the plan and ask for confirmation; True only on an explicit yes. Nothing + runs here, so an interruption at this prompt has changed nothing.""" if not commands: print(level_text("INFO", 'There are no actions to run.')) - return - + return False preview_commands(commands) mode = choose( 'Confirm action', ['Cancel', 'Confirm plan and run'], default_index=None, ) - if mode in {"", 'Cancel'}: + return mode == 'Confirm plan and run' + + +def _execute_plan(commands: list[Command]) -> None: + if not _confirm_plan(commands): return require_root_for_apply() apply_commands(commands) @@ -175,13 +182,80 @@ def _probe_databases_for_management(instance: str) -> tuple[str, str | None, lis def _resolve_data_dir(config: InstanceConfig) -> str: - values = read_odoo_conf(config.odoo_conf_file) - data_dir = values.get("data_dir", "").strip() - if data_dir: - return data_dir + """The instance's data dir: the one the config being built names (a new instance + has no odoo.conf on disk yet), else ``data_dir`` from its odoo.conf (current or + legacy location), else Odoo's default under the home.""" + if config.data_dir: + return config.data_dir + for conf_path in _odoo_conf_candidates(config.instance): + data_dir = read_odoo_conf(conf_path).get("data_dir", "").strip() + if data_dir: + return data_dir return f"{config.odoo_home}/.local/share/Odoo" +def _data_dir_is_private(config: InstanceConfig, data_dir: str) -> bool: + """True when ``data_dir`` can only be this instance's: the managed + ``/var/lib/odoo/`` or a directory inside its home. A custom data dir + elsewhere may be shared with other instances, so nothing is done to it as a whole.""" + return os.path.normpath(data_dir) == os.path.normpath(config.managed_data_dir) or _is_inside( + data_dir, config.odoo_home + ) + + +def _own_filestore_commands(config: InstanceConfig, db_name: str) -> list[Command]: + """Give a restored or copied filestore to the instance user. A private data dir is + handed over whole (Odoo creates ``sessions`` and ``filestore`` in it); a data dir + that may be shared gets only this database's filestore.""" + data_dir = _resolve_data_dir(config) + owner = f"{_quote(config.odoo_user)}:{_quote(config.odoo_user)}" + if _data_dir_is_private(config, data_dir): + return [Command( + 'Own the data dir by the instance user (filestore, sessions, …)', + f"chown -R {owner} {_quote(data_dir)}", + )] + return [Command( + tf('Own the filestore of {} by the instance user', db_name), + f"chown -R {owner} {_quote(_filestore_path(config, db_name))}", + )] + + +# Directories a backup must not be written into directly: making them private +# (chmod 700) or pruning in them would affect everything else they hold. +_SHARED_DIRS = frozenset({ + "/", "/tmp", "/var", "/var/tmp", "/var/backups", "/var/lib", "/var/log", "/etc", "/usr", + "/home", "/root", "/opt", "/opt/odoo", "/srv", "/mnt", "/media", "/boot", "/run", "/dev", +}) + + +def _backup_dir_error(path: str) -> str | None: + """An absolute, dedicated directory: the tool makes it private (700) and prunes + in it, so a shared system directory is refused.""" + if not path.startswith("/") or "\n" in path or ".." in path.split("/"): + return 'Use an absolute directory path.' + if os.path.normpath(path) in _SHARED_DIRS: + return tf('{} is a shared directory: use a dedicated one, such as /var/backups/.', path) + return None + + +def _existing_instance_artifacts(config: InstanceConfig) -> list[str]: + """What already exists under this instance's names: an install must not take + them over, and its cleanup could otherwise remove them.""" + found = [] + for label, present in ( + ('home', path_exists(config.odoo_home)), + ('config', path_exists(config.odoo_conf_dir)), + ('systemd unit', service_exists(config.odoo_service)), + ('Nginx vhost', path_exists(f"/etc/nginx/sites-available/{config.nginx_http_name}") + or path_exists(f"/etc/nginx/sites-available/{config.nginx_https_name}")), + ('SSL directory', path_exists(config.nginx_ssl_dir)), + ('Linux user', user_exists(config.odoo_user)), + ): + if present: + found.append(label) + return found + + def _filestore_path(config: InstanceConfig, db_name: str) -> str: data_dir = _resolve_data_dir(config) return f"{data_dir}/filestore/{db_name}" @@ -282,6 +356,28 @@ def _database_exists(creds: DbCredentials, db_name: str) -> bool: return result.returncode == 0 and result.stdout.strip() == "1" +def _instance_db_user(config: InstanceConfig) -> str: + for conf_path in _odoo_conf_candidates(config.instance): + db_user = read_odoo_conf(conf_path).get("db_user", "").strip() + if db_user: + return db_user + return config.db_user or config.instance + + +def _database_owner(creds: DbCredentials, db_name: str) -> str: + """The role owning ``db_name``, or "" when it is absent or cannot be read.""" + sql = ( + "SELECT r.rolname FROM pg_database d JOIN pg_roles r ON r.oid = d.datdba " + "WHERE d.datname = '" + db_name.replace("'", "''") + "'" + ) + cmd = ( + f"psql -X -h {shlex.quote(creds.host)} -p {int(creds.port)} -U {shlex.quote(creds.user)} " + f"-d postgres -tAc {shlex.quote(sql)}" + ) + result = run(cmd, check=False, env=pg_env(creds.password)) + return result.stdout.strip() if result.returncode == 0 else "" + + def _is_self_signed_certificate(cert_path: str) -> bool: if not cert_path or not path_exists(cert_path): return False diff --git a/instance_manager/workflows/diskusage.py b/instance_manager/workflows/diskusage.py index 11739db..ac1b08b 100644 --- a/instance_manager/workflows/diskusage.py +++ b/instance_manager/workflows/diskusage.py @@ -6,12 +6,12 @@ import re from ..i18n import tf -from ..models import InstanceConfig +from ..models import InstanceConfig, is_valid_db_name from ..planners import BACKUP_DUMP_SUFFIX, BACKUP_FILESTORE_SUFFIX, plan_backup_retention from ..prompts import ask_int, ask_text, choose from ..system import path_exists, run from ..ui import level_text, render_table, title -from .common import _execute_plan, _quote, _resolve_data_dir +from .common import _backup_dir_error, _execute_plan, _quote, _resolve_data_dir def _size_of(path: str) -> str: @@ -64,7 +64,8 @@ def _backed_up_databases(config: InstanceConfig, backup_dir: str) -> list[str]: names = os.listdir(backup_dir) except OSError: return [] - return sorted({m.group("db") for m in map(pattern.match, names) if m}) + # A file name is not a checked database name: one that is not valid is skipped. + return sorted({m.group("db") for m in map(pattern.match, names) if m and is_valid_db_name(m.group("db"))}) def _cleanup_old_backups(config: InstanceConfig, backup_dir: str) -> None: @@ -80,7 +81,8 @@ def _cleanup_old_backups(config: InstanceConfig, backup_dir: str) -> None: def manage_disk_usage(config: InstanceConfig) -> None: backup_dir = ask_text( - 'Backup directory to review', f"/var/backups/{config.instance}", required=True + 'Backup directory to review', f"/var/backups/{config.instance}", required=True, + validate=_backup_dir_error, ) while True: action = choose( diff --git a/instance_manager/workflows/install.py b/instance_manager/workflows/install.py index cb230fd..9e6c85d 100644 --- a/instance_manager/workflows/install.py +++ b/instance_manager/workflows/install.py @@ -8,6 +8,7 @@ from .. import support from ..i18n import t, tf from ..models import ( + INSTANCE_NAME_RE, InstanceConfig, branch_error, db_host_error, @@ -15,6 +16,7 @@ generate_secret, ip_error, is_valid_db_name, + reserved_name_error, version_error, ) from ..planners import ( @@ -36,7 +38,7 @@ from ..system import ( Command, apply_commands, - db_role_exists, + db_role_absent, detect_arch, detect_cpu_count, detect_host_python, @@ -44,11 +46,19 @@ detect_os_release, detect_total_ram_bytes, list_instances, + path_exists, + preview_commands, read_odoo_conf, + require_root_for_apply, run, ) from ..ui import level_text -from .common import _execute_plan, _odoo_conf_candidates, _quote +from .common import ( + _confirm_plan, + _existing_instance_artifacts, + _odoo_conf_candidates, + _quote, +) def _supported_version_error(version: str) -> str | None: @@ -240,9 +250,32 @@ def _maybe_plan_wkhtmltopdf() -> list[Command]: return [] +def _new_instance_name_error(name: str, with_odoo: bool = True) -> str | None: + """A new instance takes a free name: not a system account or service, and with + Odoo, nothing on the host under it yet — its install would take those over, and + the cleanup of a failed install would remove them.""" + if not INSTANCE_NAME_RE.fullmatch(name): + return 'invalid instance. Use the format: start with a lowercase letter and only [a-z0-9_] (max 32).' + if not with_odoo: + return None + error = reserved_name_error(name) + if error: + return error + found = _existing_instance_artifacts(InstanceConfig(instance=name)) + if found: + return tf( + 'Something named {} already exists on this host ({}). Update or delete it from Manage instances, or choose another name.', + name, ", ".join(t(item) for item in found), + ) + return None + + def _collect_instance_config(with_odoo: bool = True) -> InstanceConfig: while True: - instance = ask_text('Instance name', "odoo18", required=True) + instance = ask_text( + 'Instance name', "odoo18", required=True, + validate=lambda value: _new_instance_name_error(value, with_odoo), + ) config = InstanceConfig(instance=instance) config.version = ask_text( 'Odoo version', config.version, required=True, validate=_supported_version_error @@ -307,9 +340,13 @@ def _collect_instance_config(with_odoo: bool = True) -> InstanceConfig: def _build_partial_install_cleanup( - config: InstanceConfig, cleanup_db_role: bool + config: InstanceConfig, cleanup_db_role: bool, data_dir_is_new: bool = False, + with_odoo: bool = True, ) -> list[Command]: - commands: list[Command] = [ + """Undo a failed install. The name was free when the install started (see + ``_new_instance_name_error``), so everything under it was made by this run; the + data dir only when it did not exist before, and the role only when it is new.""" + commands: list[Command] = [] if not with_odoo else [ Command( '[Cleanup] Stop the Odoo service', f"systemctl stop {_quote(config.odoo_service)} || true", @@ -330,15 +367,17 @@ def _build_partial_install_cleanup( Command( '[Cleanup] Remove instance home', f"rm -rf {_quote(config.odoo_home)}" ), - # Only while it holds no filestore: a data dir kept from an earlier delete - # of the same instance may be reused, and must not be removed with it. *( - [Command( - '[Cleanup] Remove the new data dir (if it holds no filestore)', - f'[ -n "$(ls -A {_quote(config.data_dir + "/filestore")} 2>/dev/null)" ] || ' - f"rm -rf {_quote(config.data_dir)}", - )] - if config.data_dir else [] + [Command('[Cleanup] Remove the new data dir', f"rm -rf {_quote(config.data_dir)}")] + if config.data_dir and data_dir_is_new else [] + ), + Command( + '[Cleanup] Remove the instance log and logrotate policy', + f"rm -f {_quote(config.odoo_log_file)} {_quote(config.logrotate_config_file)}", + ), + Command( + '[Cleanup] Remove the instance Linux user', + f"if id -u {_quote(config.odoo_user)} >/dev/null 2>&1; then userdel {_quote(config.odoo_user)}; fi", ), Command( '[Cleanup] Remove Nginx HTTP', @@ -359,7 +398,8 @@ def _build_partial_install_cleanup( commands.append( Command( '[Cleanup] Remove the instance PostgreSQL role (if it exists)', - f'sudo -u postgres psql -v ON_ERROR_STOP=1 -c "DROP ROLE IF EXISTS {config.db_user};" || true', + "sudo -u postgres psql -X -v ON_ERROR_STOP=1 -c " + + _quote('DROP ROLE IF EXISTS "' + config.db_user + '";') + " || true", ) ) @@ -370,30 +410,44 @@ def _execute_install_with_cleanup( commands: list[Command], config: InstanceConfig, cleanup_db_role: bool, + with_odoo: bool = True, ) -> None: + """Confirm and run an install; if it fails or is interrupted while running, offer + to undo what it made. Cancelling or interrupting at the confirmation changes + nothing, so there is nothing to undo.""" + # Probed before anything runs: a data dir that is already there is not this run's. + data_dir_is_new = bool(config.data_dir) and not path_exists(config.data_dir) + if not _confirm_plan(commands): + return + require_root_for_apply() try: - _execute_plan(commands) + apply_commands(commands) except (RuntimeError, KeyboardInterrupt) as error: reason = ( 'was interrupted (Ctrl+C)' if isinstance(error, KeyboardInterrupt) else 'failed' ) + print(f"\n{level_text('WARN', tf('The installation {}.', t(reason)))}") cleanup_commands = _build_partial_install_cleanup( - config, cleanup_db_role=cleanup_db_role - ) - print( - f"\n{level_text('WARN', tf('The installation {}. Running automatic cleanup of the instance residues...', t(reason)))}" + config, cleanup_db_role=cleanup_db_role, data_dir_is_new=data_dir_is_new, + with_odoo=with_odoo, ) + if not cleanup_commands: + return + preview_commands(cleanup_commands) + try: + undo = ask_bool('Undo what this installation made (the steps above)?', True) + except KeyboardInterrupt: + undo = False + if not undo: + print(level_text("WARN", 'Nothing was undone: the steps above list what the installation may have left.')) + return apply_commands(cleanup_commands, stop_on_error=False) - print( - level_text( - "WARN", - 'Automatic cleanup finished. Review the messages above.', - ) - ) + print(level_text("WARN", 'Cleanup finished. Review the messages above.')) # Return to the menu instead of crashing the CLI with an uncaught raise. return + print(f"\n{level_text('OK', 'Plan executed successfully.')}") def _choose_nginx_mode() -> str: @@ -591,7 +645,9 @@ def install_odoo_only() -> None: commands.extend(_maybe_plan_logrotate(config)) - _execute_install_with_cleanup(commands, config, cleanup_db_role=False) + # The role is dropped on failure only if this run creates it (local DB only). + role_is_new = not config.is_remote_db_host and db_role_absent(config.db_user) + _execute_install_with_cleanup(commands, config, cleanup_db_role=role_is_new) def install_db_only() -> None: @@ -600,9 +656,9 @@ def install_db_only() -> None: 'Configure listen_addresses and pg_hba for remote access?', True ) # Dropped on failure only if this run creates it: an existing role is reused. - role_is_new = not db_role_exists(config.db_user) + role_is_new = db_role_absent(config.db_user) commands = plan_db_setup(config, ensure_remote_access=ensure_remote_access) - _execute_install_with_cleanup(commands, config, cleanup_db_role=role_is_new) + _execute_install_with_cleanup(commands, config, cleanup_db_role=role_is_new, with_odoo=False) def install_odoo_and_db() -> None: @@ -611,7 +667,7 @@ def install_odoo_and_db() -> None: 'Enable the Odoo service to start on boot?', True, ) - role_is_new = not db_role_exists(config.db_user) + role_is_new = db_role_absent(config.db_user) commands: list[Command] = [] commands.extend(_plan_runtime(config)) # Odoo and PostgreSQL on one host talk over loopback: nothing is opened to the diff --git a/instance_manager/workflows/manage.py b/instance_manager/workflows/manage.py index 279b0d4..85913da 100644 --- a/instance_manager/workflows/manage.py +++ b/instance_manager/workflows/manage.py @@ -48,9 +48,10 @@ from .common import ( DbCredentials, _ask_db_credentials, - _database_exists, + _database_owner, _execute_plan, _filestore_path, + _instance_db_user, _is_self_signed_certificate, _keep_data_dir_commands, _probe_databases_for_management, @@ -503,10 +504,21 @@ def _delete_instance( creds = cached if drop_db: - db_name = ask_text('DB to delete', config.instance, required=True) + db_name = ask_text( + 'DB to delete', config.instance, required=True, + validate=lambda value: None if is_valid_db_name(value) else 'Invalid database name.', + ) creds = _ask_db_credentials(config.instance, cached) db_host, db_port, db_user, db_password = creds.host, creds.port, creds.user, creds.password - if not _database_exists(creds, db_name): + owner = _database_owner(creds, db_name) + instance_role = _instance_db_user(config) + if owner and owner != instance_role: + print(level_text("ERROR", tf( + 'Database {} belongs to role {}, not to this instance ({}): it is not deleted.', + db_name, owner, instance_role, + ))) + drop_db = False + elif not owner: print( level_text( "WARN", @@ -530,9 +542,6 @@ def _delete_instance( ), Command('Reload systemd', "systemctl daemon-reload"), *plan_remove_scheduled_backup(config), - Command( - 'Remove Odoo configuration', f"rm -rf {_quote(config.odoo_conf_dir)}" - ), ] data_dir = _resolve_data_dir(config) if remove_store: @@ -552,7 +561,19 @@ def _delete_instance( # that is Odoo's whole data dir — every database's filestore — so it is moved # out first: only the filestore the operator named above is deleted. commands.extend(_keep_data_dir_commands(config, data_dir)) + if drop_db and db_name: + commands.append( + Command( + 'Delete DB', + f"dropdb --if-exists -h {_quote(db_host)} -p {int(db_port)} -U {_quote(db_user)} {_quote(db_name)}", + env=pg_env(db_password), + ) + ) commands.extend([ + Command( + 'Remove Odoo configuration', f"rm -rf {_quote(config.odoo_conf_dir)}" + ), + Command('Remove the logrotate policy', f"rm -f {_quote(config.logrotate_config_file)}"), Command('Remove instance home', f"rm -rf {_quote(config.odoo_home)}"), Command( 'Remove Nginx HTTP', @@ -563,19 +584,16 @@ def _delete_instance( f"rm -f {_quote(f'/etc/nginx/sites-available/{config.nginx_https_name}')} {_quote(f'/etc/nginx/sites-enabled/{config.nginx_https_name}')}", ), Command('Remove instance SSL', f"rm -rf {_quote(config.nginx_ssl_dir)}"), - Command('Validate Nginx', "nginx -t"), - Command('Reload Nginx', "systemctl reload nginx || true"), + # Best effort: a host without nginx, or whose config another site broke, still + # gets the instance removed. + Command( + 'Validate and reload Nginx (if installed)', + "if command -v nginx >/dev/null 2>&1; then " + "if nginx -t; then systemctl reload nginx || true; " + "else echo 'nginx -t failed: Nginx was not reloaded' >&2; fi; fi", + ), ]) - if drop_db and db_name: - commands.append( - Command( - 'Delete DB', - f"dropdb --if-exists -h {_quote(db_host)} -p {int(db_port)} -U {_quote(db_user)} {_quote(db_name)}", - env=pg_env(db_password), - ) - ) - if not confirm_with_phrase( 'Destructive action detected.', f"DELETE {config.instance}", diff --git a/instance_manager/workflows/purge.py b/instance_manager/workflows/purge.py index cd696e3..014bccb 100644 --- a/instance_manager/workflows/purge.py +++ b/instance_manager/workflows/purge.py @@ -24,9 +24,11 @@ run, service_exists, ) -from ..ui import render_table, title +from ..ui import level_text, render_table, title from .common import ( _execute_plan, + _instance_db_user, + _is_inside, _odoo_conf_candidates, _quote, _resolve_data_dir, @@ -184,19 +186,91 @@ def _list_instance_databases( return _query_names(session, _instance_databases_sql(instance, db_user, by_owner)) -def _list_filestore_databases(config: InstanceConfig) -> tuple[list[str], str]: - filestore_root = f"{_resolve_data_dir(config)}/filestore" - if not os.path.isdir(filestore_root): - return [], filestore_root +def _instance_data_dirs(config: InstanceConfig) -> list[str]: + """The data dirs that may hold this instance's filestores: the one its odoo.conf + names (or Odoo's default in the home) and the managed ``/var/lib/odoo/``, + which outlives a Delete instance that removed the odoo.conf.""" + dirs = [_resolve_data_dir(config)] + if config.managed_data_dir not in dirs and os.path.isdir(config.managed_data_dir): + dirs.append(config.managed_data_dir) + return dirs + + +def _list_filestore_databases(config: InstanceConfig) -> list[str]: + """Folder names under the instance's ``filestore`` roots: candidates only. A + shared data dir holds other instances' filestores too, so a folder alone never + selects a database.""" + names: set[str] = set() + for data_dir in _instance_data_dirs(config): + root = f"{data_dir}/filestore" + if os.path.isdir(root): + names.update( + entry for entry in os.listdir(root) + if os.path.isdir(os.path.join(root, entry)) and not entry.startswith(".") + ) + return sorted(names) + - db_names = sorted( - [ - entry - for entry in os.listdir(filestore_root) - if os.path.isdir(os.path.join(filestore_root, entry)) and not entry.startswith(".") - ] +def _remove_data_commands(config: InstanceConfig, db_names: list[str]) -> list[Command]: + """Remove the instance's filestores. A data dir that is the instance's own (the + managed ``/var/lib/odoo/``; one inside the home goes with the home) is + removed whole; any other may be shared, so only the selected databases' + filestores are removed from it.""" + commands: list[Command] = [] + for data_dir in _instance_data_dirs(config): + if _is_inside(data_dir, config.odoo_home): + continue + if os.path.normpath(data_dir) == os.path.normpath(config.managed_data_dir): + commands.append(Command( + tf('Remove the instance data dir {}', data_dir), f"rm -rf {_quote(data_dir)}", + )) + continue + for db_name in db_names: + store = f"{data_dir}/filestore/{db_name}" + commands.append(Command(tf('Remove the filestore {}', store), f"rm -rf {_quote(store)}")) + return commands + + +def _superuser_role_error(session: DbAdminSession, db_user: str) -> str | None: + """A superuser owns, or can reach, every database on the server: selecting + "the databases it owns" would select the server's.""" + if db_user in {"postgres", session.admin_user}: + return tf('{} is an administrator role, not an instance role.', db_user) + rows, error = _query_names( + session, f"SELECT rolsuper FROM pg_roles WHERE rolname = '{_sql_literal(db_user)}';" ) - return db_names, filestore_root + if error: + return tf('Could not check role {}: {}', db_user, error) + if rows == ["t"]: + return tf('{} is a superuser role, not an instance role.', db_user) + return None + + +def _drop_database_commands(session: DbAdminSession | None, db_names: list[str]) -> list[Command]: + commands: list[Command] = [] + if session is None: + return commands + for db_name in db_names: + terminate_sql = ( + "SELECT pg_terminate_backend(pid) " + "FROM pg_stat_activity " + f"WHERE datname = '{_sql_literal(db_name)}' AND pid <> pg_backend_pid();" + ) + commands.append( + Command( + tf('Close active connections of DB {}', db_name), + _db_admin_psql_command(session, terminate_sql) + " || true", + env=_admin_env(session), + ) + ) + commands.append( + Command( + tf('Delete DB {}', db_name), + _db_admin_dropdb_command(session, db_name), + env=_admin_env(session), + ) + ) + return commands def purge_instance_superuser() -> None: @@ -209,11 +283,11 @@ def purge_instance_superuser() -> None: if config is None: return - filestore_dbs, filestore_root = _list_filestore_databases(config) + filestore_dbs = _list_filestore_databases(config) db_user = ask_text( 'Instance DB user (to find associated databases)', - instance, + _instance_db_user(config), required=True, validate=lambda value: None if POSTGRES_IDENTIFIER_RE.fullmatch(value) @@ -228,18 +302,26 @@ def purge_instance_superuser() -> None: )) session = _resolve_db_admin_access() - db_names: list[str] = list(filestore_dbs) + db_names: list[str] = [] dbs_by_prefix: list[str] = [] db_error: str | None = None if session: + role_error = _superuser_role_error(session, db_user) + if role_error: + print(level_text("ERROR", role_error)) + return dbs_by_prefix, db_error = _list_instance_databases( instance, session, db_user, by_owner=not sharing ) if db_error: print(tf("[WARN] Could not detect the databases of '{}': {}", instance, db_error)) - for db_name in dbs_by_prefix: - if db_name not in db_names: - db_names.append(db_name) + db_names = list(dbs_by_prefix) + unowned = [name for name in filestore_dbs if name not in db_names] + if unowned: + print(tf( + "[INFO] Not selected — a filestore folder exists but role {} does not own the database: {}. Add any that belong to this instance below.", + db_user, ", ".join(unowned), + )) others, _error = _query_names(session, _prefix_only_databases_sql(instance, db_user)) if others: print(tf( @@ -273,6 +355,8 @@ def purge_instance_superuser() -> None: else: print(t('[INFO] No databases detected/specified for deletion.')) + # The service stops first and the databases go before their filestores: a + # database never outlives the files it points at. commands: list[Command] = [ Command( 'Stop the Odoo service', @@ -282,6 +366,7 @@ def purge_instance_superuser() -> None: 'Disable the Odoo service', f"systemctl disable {_quote(config.odoo_service)} || true", ), + *_drop_database_commands(session, db_names), Command( 'Remove unit file', f"rm -f {_quote(f'/etc/systemd/system/{config.odoo_service}.service')}", @@ -292,13 +377,19 @@ def purge_instance_superuser() -> None: 'Remove Odoo configuration', f"rm -rf {_quote(config.odoo_conf_dir)}" ), Command('Remove instance home', f"rm -rf {_quote(config.odoo_home)}"), + # Only an account this tool made (home /opt/odoo/); its home is + # already gone, so no `userdel -r`, which would follow another home. Command( 'Remove the instance Linux user', - f"id -u {_quote(config.odoo_user)} >/dev/null 2>&1 && userdel -r {_quote(config.odoo_user)} || true", + f"if id -u {_quote(config.odoo_user)} >/dev/null 2>&1; then " + f'if [ "$(getent passwd {_quote(config.odoo_user)} | cut -d: -f6)" = {_quote(config.odoo_home)} ]; ' + f"then userdel {_quote(config.odoo_user)}; " + f"else echo {_quote(tf('Linux user {} kept: its home is not {}', config.odoo_user, config.odoo_home))}; fi; fi", ), Command( - 'Remove the instance Odoo/Nginx logs', - f"rm -f {_quote(f'/var/log/odoo/{config.instance}.log')} {_quote(f'/var/log/nginx/{config.instance}.access.log')} {_quote(f'/var/log/nginx/{config.instance}.error.log')}", + 'Remove the instance Odoo/Nginx logs and logrotate policy', + f"rm -f {_quote(config.odoo_log_file)} {_quote(config.nginx_access_log)} " + f"{_quote(config.nginx_error_log)} {_quote(config.logrotate_config_file)}", ), Command( 'Remove Nginx HTTP', @@ -316,47 +407,26 @@ def purge_instance_superuser() -> None: f"rm -f {_quote(f'/etc/fail2ban/jail.d/odoo-auth-{_safe_token(config.instance)}.local')} && " "{ ! systemctl is-active --quiet fail2ban || systemctl reload fail2ban; }", ), - Command('Remove the instance filestore root', f"rm -rf {_quote(filestore_root)}"), - Command('Validate/reload Nginx (best effort)', "nginx -t && systemctl reload nginx || true"), + *_remove_data_commands(config, db_names), + Command( + 'Validate/reload Nginx (best effort)', + "if command -v nginx >/dev/null 2>&1; then nginx -t && systemctl reload nginx || true; fi", + ), ] if session: - for db_name in db_names: - db_literal = _sql_literal(db_name) - terminate_sql = ( - "SELECT pg_terminate_backend(pid) " - "FROM pg_stat_activity " - f"WHERE datname = '{db_literal}' AND pid <> pg_backend_pid();" - ) - commands.append( - Command( - tf('Close active connections of DB {}', db_name), - _db_admin_psql_command(session, terminate_sql) + " || true", - env=_admin_env(session), - ) - ) - commands.append( - Command( - tf('Delete DB {}', db_name), - _db_admin_dropdb_command(session, db_name), - env=_admin_env(session), - ) - ) - + # A role another instance connects as stays; so does one named after the + # instance that another instance uses. role_candidates = [] if sharing else [db_user] - if config.instance not in _instances_sharing_role(instance, config.instance): + if not _instances_sharing_role(instance, config.instance): role_candidates.append(config.instance) - unique_roles: list[str] = [] - for role in role_candidates: - if role and role not in unique_roles: - unique_roles.append(role) - - for role in unique_roles: - drop_role_sql = f"DROP ROLE IF EXISTS {role};" + for role in dict.fromkeys(r for r in role_candidates if r): + drop_role_sql = f'DROP ROLE IF EXISTS "{role}";' commands.append( Command( tf('Delete PostgreSQL role {} (if it exists)', role), - _db_admin_psql_command(session, drop_role_sql) + " || true", + _db_admin_psql_command(session, drop_role_sql) + + " || echo " + _quote(tf('Role {} kept: it still owns objects or is in use.', role)), env=_admin_env(session), ) ) @@ -370,8 +440,8 @@ def purge_instance_superuser() -> None: ['Nginx HTTP detected', "yes" if path_exists(f"/etc/nginx/sites-available/{config.nginx_http_name}") else "no"], ['Nginx HTTPS detected', "yes" if path_exists(f"/etc/nginx/sites-available/{config.nginx_https_name}") else "no"], ['SSL detected', "yes" if path_exists(config.nginx_ssl_dir) else "no"], - ['Filestore root detected', f"{'yes' if path_exists(filestore_root) else 'no'} ({filestore_root})"], - ['DBs by filestore', ", ".join(filestore_dbs) if filestore_dbs else '(none)'], + ['Data dirs', ", ".join(_instance_data_dirs(config))], + ['Filestore folders', ", ".join(filestore_dbs) if filestore_dbs else '(none)'], ['DBs by owner/name', ", ".join(dbs_by_prefix) if dbs_by_prefix else '(none)'], ['DB admin access', session.mode if session else 'unavailable (local cleanup only)'], ['DBs to remove', ", ".join(db_names) if db_names else '(none detected)'], diff --git a/instance_manager/workflows/scheduled_backup.py b/instance_manager/workflows/scheduled_backup.py index 2f01270..1ce1bc1 100644 --- a/instance_manager/workflows/scheduled_backup.py +++ b/instance_manager/workflows/scheduled_backup.py @@ -8,7 +8,7 @@ from ..prompts import ask_bool, ask_int, ask_text, choose from ..system import run from ..ui import level_text, title -from .common import _execute_plan, _filestore_path +from .common import _backup_dir_error, _execute_plan, _filestore_path _ONCALENDAR = { 'Daily (02:30)': "*-*-* 02:30:00", @@ -17,13 +17,6 @@ } -def _backup_dir_error(path: str) -> str | None: - """An absolute directory path: it is written into a root-run script.""" - if path.startswith("/") and "\n" not in path and ".." not in path.split("/"): - return None - return 'Use an absolute directory path.' - - def _configure_schedule(config: InstanceConfig) -> None: print(f"\n{title('Configure scheduled backup')}") print( diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/proposal.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/proposal.md new file mode 100644 index 0000000..11fd111 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/proposal.md @@ -0,0 +1,49 @@ +# Guard the paths that remove or overwrite + +## Why + +A second audit, run by executing the plans, found paths where the preview and the confirmation phrase show a +plan that is itself wrong: + +- **Install** never checked that the instance name was free. Installing over an existing instance, or pressing + Ctrl+C at the confirmation prompt, ran a cleanup that removed that instance's home, config, unit and vhosts. + System names were accepted: an instance named `backup` reuses the account whose home is `/var/backups`. +- **Purge** dropped every database that had a folder in the filestore root, with no owner check. On a data dir + shared by several instances (`/var/lib/odoo`), it removed the other instances' filestores and dropped their + databases. A role named after the instance was dropped even when another instance used it, and `postgres` was + accepted as the instance role. +- **Every install** ran `chown -R /var/log/odoo`: the newest instance owned every other instance's + log, those instances logged to stdout, their fail2ban jails saw nothing, and logrotate's `su` failed. +- **Delete** ran a bare `nginx -t` before dropping the database (no nginx means no drop), read the data dir + only from the current `odoo.conf` location, and dropped a database without checking its owner. +- **Copies** chowned the whole resolved data dir (another instance's files when shared), `cp -a` nested the copy + inside an existing target, a refresh removed the target filestore with `rm -rf`, and its phrase named the + source. A replica's filestore went to `~/.local/share/Odoo` while its `odoo.conf` named `/var/lib/odoo/`. +- **Backups** accepted `/tmp` or `/` as their directory, then made it `700`; retention kept the newest by mtime, + so a copied old backup outlived newer ones. + +## What changes + +- A new instance needs a free name: no system account or service name, and nothing on the host under it. +- Install confirms outside the failure handling; a failure while applying previews the cleanup of what that run + made and asks before applying it. The data dir and the role are removed only when the run created them. +- Purge selects databases by owner or exact name only, refuses administrator roles, drops databases before their + filestores, removes a shared data dir's filestores one by one, keeps a role another instance uses, removes the + Linux user only when the tool made it, and removes the logrotate policy. +- `/var/log/odoo` is root's (755); each instance owns only its log, earlier instances get theirs back, and the + logrotate policies lose `su`. +- Delete checks the database's owner, drops it before the config goes, resolves the data dir from both config + locations, reloads nginx only if installed, and removes the logrotate policy. +- Copies chown only what is the target's, move an existing target filestore aside, copy into it, and check the + source first; a refresh asks for `REPLACE `; the data dir of the config being built wins. +- Backup directories must be dedicated; retention keeps the newest names, and file names yield only valid + database names. + +## Impact + +- Specs: `execution-safety`, `instance-provisioning`, `instance-removal`, `data-backup-restore`, `disk-usage`, + `scheduled-backups`, `log-rotation`. +- Code: `models.py`, `planners.py`, `system.py`, `workflows/{common,install,purge,manage,backup_restore, + diskusage,scheduled_backup}.py`, `i18n.py`. +- Tests: `tests/test_destructive_guards.py`; `tools/verify_data_safety.py` runs the new steps against stubs and + a PostgreSQL of its own. diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/data-backup-restore/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/data-backup-restore/spec.md new file mode 100644 index 0000000..ca6226d --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/data-backup-restore/spec.md @@ -0,0 +1,193 @@ +## MODIFIED Requirements + +### Requirement: Backup + +The tool SHALL back up an instance's database (as a compressed custom-format dump) and/or its filestore (as a +gzipped tar) into the chosen backup directory, using a **single timestamp for the whole operation**, naming +each artifact `----`, and writing each artifact **atomically** (a temporary file +promoted only on success). The backup directory SHALL be private to root (`700`) and every artifact SHALL be +written private (`umask 077`): a dump holds password hashes, API keys and mail/payment secrets. The backup +directory SHALL be an absolute, dedicated directory: a shared system directory (`/`, `/tmp`, `/var/backups`, +`/etc`, `/opt/odoo`, …) SHALL be refused, since the tool makes the directory private and prunes in it. + +#### Scenario: Database backup produces an atomic timestamped custom dump + +- **WHEN** the operator selects a backup that includes the database +- **THEN** the plan runs `pg_dump -Fc` to a temporary file and renames it to + `/----.dump` only on success, removing the temporary file and failing + the step otherwise + +#### Scenario: Filestore backup archives the resolved filestore path atomically + +- **WHEN** the operator selects a backup that includes the filestore +- **THEN** the plan tars the resolved filestore directory to a temporary file and renames it to + `/----.filestore.tar.gz` only on success; a file that changed while + being read (GNU tar exit 1) does not fail the archive, any other tar failure does + +#### Scenario: DB dump and filestore archive share one timestamp + +- **WHEN** the operator selects a "DB + Filestore" backup +- **THEN** the `.dump` and `.filestore.tar.gz` names carry the **same** timestamp so the pair can be matched + +#### Scenario: Backups are private + +- **WHEN** a backup is written +- **THEN** the backup directory is mode `700` and the artifacts are readable by root only + +#### Scenario: A shared directory is not a backup directory + +- **WHEN** the operator gives `/tmp` or `/var/backups` as the backup directory +- **THEN** the tool refuses it and asks for a dedicated one, such as `/var/backups/` + +### Requirement: Restore + +Restoring SHALL create the target database from a selected dump and/or restore a filestore archive, refusing to +clobber an existing target database, and require phrase confirmation before applying. The pre-restore +existence check for the target database is evaluated against the **local** PostgreSQL server. An existing +target filestore SHALL be moved aside, never deleted, and the restored files SHALL belong to the instance user. + +#### Scenario: Existing target database blocks restore + +- **WHEN** the restore includes the database and the target database already exists +- **THEN** the operation reports the conflict and stops without executing + +#### Scenario: Target-database existence check is local-only + +- **WHEN** the operator targets a remote database host for the restore +- **THEN** the pre-restore existence check runs against the local server and may not detect a remote + collision; the subsequent `createdb` still fails safely if the remote target exists + +#### Scenario: Existing target filestore requires explicit overwrite + +- **WHEN** the restore includes the filestore and the target filestore exists +- **THEN** the operator must confirm overwrite; on confirmation the previous filestore is moved to + `.replaced-` before extracting, otherwise the restore is cancelled + +#### Scenario: Restored files belong to the instance user + +- **WHEN** a filestore archive is extracted +- **THEN** tar does not restore the archive's owners (`--no-same-owner`) and the plan gives the files to the + instance user: the whole data dir when it is the instance's own (`/var/lib/odoo/` or inside its + home), only that database's filestore when the data dir may be shared + +#### Scenario: Restore is phrase-confirmed + +- **WHEN** the restore plan is assembled +- **THEN** it executes only after the operator types the exact `RESTORE ` phrase + +### Requirement: Duplication + +The tool SHALL duplicate a source instance's database — and optionally its filestore — into a target, +end-to-end and existence-aware, requiring phrase confirmation and validating the source and target database +names as safe before using them in SQL. The target instance SHALL differ from the source instance, and the +target database SHALL differ from the source database. + +The operator SHALL choose the database copy method: a fast PostgreSQL **template** copy, or a robust +**`pg_dump | pg_restore --no-owner`** that reassigns ownership to the target role. A template copy keeps the +source's role as owner of every table, so the tool SHALL refuse it when the target role differs from the source +database's owner (always the case for a new replica). The template copy SHALL refuse new connections to the +source, terminate its sessions, copy, and reopen the source whatever the outcome. + +When the **target instance does not exist**, duplication SHALL provision it fully before seeding: create the +target role, run the base setup (system user, home, Odoo checkout at the source's version, virtualenv, +`odoo.conf`, systemd unit) **without starting the service**, optionally configure Nginx, seed the database and +filestore, then start the service. The replica SHALL follow the **same production-hardening prompts as a fresh +install** (secrets with informed choice, `list_db`, `dbfilter`, worker sizing, `db_sslmode`, wkhtmltopdf) with +auto-suggested non-colliding internal ports, and — when it fronts Nginx — SHALL require a domain that is **not +already served by another vhost** (or the replica would be unreachable behind a shared `server_name`). It SHALL +also offer to **replicate the source venv's Python packages** into the target venv, so addon dependencies the +source installed beyond `requirements.txt` are present in the replica. + +When the **target instance already exists**, duplication SHALL refresh it in place: stop the target service, +drop and recreate its database from the source and replace its filestore, apply the migration semantics, and +restart — **without** recreating the target's config, service, or system user. It SHALL drop an existing +target database only when that database belongs to the target's own role (read from its `odoo.conf`) and the +operator explicitly confirms the overwrite. + +Migration semantics (copied vs moved, neutralize) SHALL apply in both cases, and the duplicated filestore SHALL +be placed under the **target** instance's data directory — the one its configuration names, also for a replica +whose `odoo.conf` is not written yet — and owned by the target system user (the whole data dir when it is the +target's own, only the copied filestore otherwise). A filestore already at the target SHALL be moved aside to +`.replaced-`, never deleted, and the copy SHALL run only when the source filestore exists. +A target that is not a whole instance of this tool (home and `odoo.conf`) SHALL be provisioned only under a free +name, and never refreshed. Every database the tool seeds SHALL have its access +**restricted to its owner** — `CONNECT` revoked from `PUBLIC` and granted to the owning role — so an instance's +database role cannot reach other instances' databases. Local drops SHALL use `dropdb --force`, which closes the +database's sessions and refuses new ones in one statement. + +#### Scenario: Replica replicates the source venv Python packages + +- **WHEN** a replica is provisioned and the operator opts to replicate packages +- **THEN** the plan installs the source venv's packages (a filtered `pip freeze`) into the target venv, so the + replica has the same addon Python dependencies as the source + +#### Scenario: New target is provisioned and seeded as a replica + +- **WHEN** the target instance does not exist +- **THEN** the plan creates the target role, runs the same production-hardening prompts as a fresh install with + auto-suggested non-colliding ports, provisions the target (base setup without starting, optional Nginx), + seeds the database and filestore from the source with the `pg_dump` copy, and then starts the target service + +#### Scenario: Replica domain must not collide with another vhost + +- **WHEN** the replica is configured to front Nginx and the chosen domain is already a `server_name` in an + enabled vhost +- **THEN** the tool rejects it and re-prompts for a different domain, so the replica is reachable + +#### Scenario: Existing target is refreshed in place + +- **WHEN** the target instance already exists +- **THEN** the plan stops the target service, drops and recreates its database from the source, replaces its + filestore, applies the migration semantics, and restarts the service without recreating its config or unit + +#### Scenario: A database that is not the target's is never dropped + +- **WHEN** the target database exists and belongs to a role other than the target's, or the target equals the + source instance or the source database +- **THEN** the tool refuses with a descriptive error and builds no plan + +#### Scenario: Overwriting the target's database is confirmed + +- **WHEN** the target database exists and belongs to the target's role +- **THEN** the operator must explicitly confirm that it will be dropped and replaced before the plan is built + +#### Scenario: Operator selects the database copy method + +- **WHEN** the duplication plan is assembled +- **THEN** the operator chooses a template copy or a `pg_dump | pg_restore --no-owner` copy, and the plan uses + the selected method, refusing a template copy across roles + +#### Scenario: Template copy frees the source; dump copy leaves it untouched + +- **WHEN** the template method is chosen +- **THEN** the plan blocks and terminates the source's sessions before the copy and re-enables them afterward + even on failure; **WHEN** the dump method is chosen, the source is read live without terminating its sessions + +#### Scenario: Migration semantics and phrase confirmation + +- **WHEN** duplication runs +- **THEN** copied/moved and neutralize semantics are applied to the target and execution proceeds only after + the operator types the exact `DUPLICATE ` phrase for a replica, or `REPLACE ` for a refresh, + whose data it replaces + +#### Scenario: Seeded database is restricted to its owner + +- **WHEN** the tool seeds a target database +- **THEN** the plan revokes `CONNECT` on that database from `PUBLIC` and grants it to the owning role, so other + instances' roles cannot connect to it + +#### Scenario: Target data dir is owned by the target user + +- **WHEN** the filestore is copied into the target data directory (created as root) +- **THEN** the plan chowns the target's own data directory to the target system user, so Odoo can create its + `sessions` and `filestore` entries; in a data directory that may be shared, only the copied filestore + +#### Scenario: A previous target filestore is moved aside + +- **WHEN** the target filestore already exists +- **THEN** it is moved to `.replaced-` and the copy lands in the target, not nested in it + +#### Scenario: Unsafe database name is rejected + +- **WHEN** the source or target database name is not a safe name +- **THEN** the plan is not built and the operation reports the problem instead of interpolating it into SQL diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/disk-usage/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/disk-usage/spec.md new file mode 100644 index 0000000..0dab7b7 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/disk-usage/spec.md @@ -0,0 +1,29 @@ +## MODIFIED Requirements + +### Requirement: Backup retention cleanup + +The tool SHALL remove an instance's oldest backup artifacts while keeping the operator-chosen number of most +recent ones **per database** and kind (DB dumps and filestore archives), through the standard +preview/confirm/apply flow. Backups SHALL be matched by their exact name pattern +(`----` plus the kind's suffix), so another instance's or another database's backups +are never removed. Backups named before the database was part of the name (`_`) SHALL be +pruned as one more group, matched exactly. The newest backups SHALL be the latest timestamps in their names, not +the latest modification times (a copied or restored file gets a new one), and a database name read from a file +name SHALL be used only when it is a valid database name. + +#### Scenario: Retention keeps the N newest of each kind + +- **WHEN** the operator runs retention cleanup keeping N backups +- **THEN** for each database the instance has backups of, the plan keeps the N newest dumps and the N newest + filestore archives and removes the older ones, previewed before applying + +#### Scenario: Look-alike backups are never pruned + +- **WHEN** the backup directory also holds backups of an instance whose name starts with this instance's name + (`shop_eu` next to `shop`) or of another database of this instance +- **THEN** none of them is removed by this instance's or this database's retention + +#### Scenario: Missing backup directory is a no-op + +- **WHEN** the backup directory does not exist +- **THEN** the tool reports it and performs no cleanup diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/execution-safety/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/execution-safety/spec.md new file mode 100644 index 0000000..a7dcc6f --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/execution-safety/spec.md @@ -0,0 +1,43 @@ +## MODIFIED Requirements + +### Requirement: Automatic cleanup on failed install + +When a provisioning run fails **or is interrupted while applying**, the tool SHALL preview a cleanup of what that +run made, ask whether to apply it (default yes), and return control to the menu rather than terminate the +program. Cancelling or interrupting at the plan confirmation SHALL change nothing and run no cleanup. Because a +new instance takes a name with nothing on the host under it, the cleanup removes the instance's unit, config, +home, Nginx vhosts, SSL dir, log, logrotate policy and Linux user; it SHALL remove the data dir only when the +run created it and SHALL drop the DB role only when the role did not exist before the run (unknown counts as +existing). A PostgreSQL-only install SHALL clean up its role only. + +#### Scenario: Failed install offers the cleanup of what it made + +- **WHEN** applying an install plan raises an error partway through +- **THEN** the tool previews the cleanup steps, asks whether to apply them, and applies them with stop-on-error + disabled when the operator agrees + +#### Scenario: Interrupted install offers the same cleanup + +- **WHEN** applying an install plan is interrupted (e.g. Ctrl+C) +- **THEN** the same cleanup is previewed and offered before control returns to the menu + +#### Scenario: Nothing is undone before anything ran + +- **WHEN** the operator cancels, or presses Ctrl+C, at the plan confirmation +- **THEN** no command runs, no cleanup runs, and the host is left as it was + +#### Scenario: What existed before the run is kept + +- **WHEN** the data dir or the DB role already existed when the install started, or the role's existence could + not be checked +- **THEN** the cleanup neither removes that data dir nor drops that role + +#### Scenario: A PostgreSQL-only install touches no Odoo artifact + +- **WHEN** a database-only install fails +- **THEN** the cleanup drops only the role it created, and touches no unit, home, config or vhost + +#### Scenario: Cleanup returns to the menu + +- **WHEN** cleanup finishes, or is declined, after a failed or interrupted install +- **THEN** control returns to the menu and the program does not crash with an uncaught exception diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-provisioning/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-provisioning/spec.md new file mode 100644 index 0000000..c25e3e1 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-provisioning/spec.md @@ -0,0 +1,61 @@ +## ADDED Requirements + +### Requirement: A new instance takes a free name + +An install with Odoo, and a replica, SHALL refuse an instance name that is a system account or service name +(`backup`, `nginx`, `postgres`, `root`, …) or under which the host already has a home, config dir, systemd unit, +Nginx vhost, SSL dir or Linux user, and SHALL ask for the name again, pointing to Manage instances for an +existing instance. The name becomes the instance's Linux user, unit, home and log: taking an existing one would +hand the tool another account or service, and the cleanup of a failed install would remove it. + +#### Scenario: An existing instance is not installed over + +- **WHEN** the operator types the name of an instance that already exists on the host +- **THEN** the tool reports what exists under that name and asks for another name before any plan is built + +#### Scenario: A system name is refused + +- **WHEN** the operator types `backup` (whose home is `/var/backups`) or `nginx` as the instance name +- **THEN** the tool refuses it and asks for another name + +## MODIFIED Requirements + +### Requirement: Odoo base setup + +The Odoo base setup SHALL install OS dependencies, create the instance system user and directory layout, clone +the chosen core (official Odoo or OCB) at the requested branch, build a virtualenv with the interpreter chosen +for the version, install pip, wheel, the setuptools the version needs and its requirements, write the instance +config and systemd unit, and register the service. + +#### Scenario: Instance directories and user are created + +- **WHEN** the base setup runs for a new instance +- **THEN** it creates the system user (if missing), the `/opt/odoo/` home with `odoo`, `addons-oca`, + `addons-custom` subdirs, the `/etc/odoo/` config dir, and the data dir `/var/lib/odoo/` + (mode `750`, owned by the instance user), with correct ownership and a `750` config dir + +#### Scenario: Each instance owns only its own log + +- **WHEN** the base setup prepares the logs +- **THEN** `/var/log/odoo` is `root:root` mode `755`, the instance owns only `/var/log/odoo/.log` + (mode `640`), the logs of earlier instances (accounts whose home is `/opt/odoo/`) get their own owner + back, and the `su` directive leaves this tool's logrotate policies for `/var/log/odoo`, which root rotates + +#### Scenario: Odoo repo and venv are prepared + +- **WHEN** the base setup runs +- **THEN** it clones the chosen core at the requested branch only if absent, creates the venv with the chosen + interpreter, installs pip, wheel and the version's setuptools requirement, and installs `requirements.txt` + (with the version's substitutes) + +#### Scenario: Config and unit files are written with restrictive permissions + +- **WHEN** the base setup writes the instance config and systemd unit +- **THEN** `.conf` is written mode `640` owned `root:`, and the systemd unit is written + mode `644`, followed by a systemd daemon-reload + +#### Scenario: Service autostart is operator-controlled + +- **WHEN** the operator opts into service autostart +- **THEN** the plan enables and starts the service; otherwise it explicitly disables autostart and then starts + the service (running now but not enabled at boot) diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-removal/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-removal/spec.md new file mode 100644 index 0000000..eb4f060 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/instance-removal/spec.md @@ -0,0 +1,125 @@ +## MODIFIED Requirements + +### Requirement: Scoped instance delete + +The tool SHALL remove an instance's systemd service, its scheduled-backup timer, Odoo config, logrotate policy, +home, Nginx vhosts (available and enabled), and SSL directory, and MAY additionally drop the database and remove +one database's filestore when the operator opts in. Removing the home SHALL NOT remove filestores the operator +did not ask to remove: when the instance's data dir (read from its `odoo.conf`, current or legacy location) lies +inside its home, the plan SHALL first move it to a private `/var/backups//kept-data-dir-`. +A database SHALL be dropped only when it exists and belongs to the instance's role (from its `odoo.conf`); the +tool SHALL warn and skip the drop otherwise rather than fail the operation. Nginx SHALL be validated and +reloaded only when it is installed, and its failure SHALL NOT stop the removal. + +#### Scenario: System residues are removed and Nginx reloaded + +- **WHEN** the operator deletes an instance +- **THEN** the plan stops and disables the service, removes the unit and reloads systemd, removes the backup + timer, the config dir, the logrotate policy, home, both Nginx vhosts, and the SSL dir, then validates and + reloads Nginx when it is installed + +#### Scenario: The data dir survives the removal of the home + +- **WHEN** the instance's data dir lies inside its home +- **THEN** the plan moves it to `/var/backups//kept-data-dir-` before removing the home, + so every filestore the operator did not ask to remove is kept + +#### Scenario: Optional database and filestore removal + +- **WHEN** the operator opts to drop the database and/or remove a filestore +- **THEN** the plan adds a `dropdb --if-exists` for the named database (a name Odoo accepts, owned by the + instance's role) before the config is removed, and/or an `rm -rf` of that database's resolved filestore path + +#### Scenario: Another role's database is not dropped + +- **WHEN** the database to drop belongs to a role other than the instance's +- **THEN** the tool reports it and omits the drop + +#### Scenario: Non-existent database is skipped with a warning + +- **WHEN** the operator requests dropping a database that is not found with the given credentials (absent or + unreachable) +- **THEN** the tool warns that the database was not found and omits the drop, continuing with the rest of the + removal instead of crashing + +#### Scenario: A host without nginx still removes the instance + +- **WHEN** nginx is not installed, or `nginx -t` fails because of another site +- **THEN** the removal completes and nginx is not reloaded + +#### Scenario: Delete is phrase-confirmed + +- **WHEN** the delete plan is assembled +- **THEN** it executes only after the operator types the exact `DELETE ` phrase + +### Requirement: Total superuser purge + +The tool SHALL provide a total purge that, in addition to the scoped removal, deletes the instance's Linux +user, its Odoo/Nginx logs, its logrotate policy, its fail2ban jail, its backup timer, its filestores, the +instance's databases, and the instance's PostgreSQL roles. Database discovery SHALL select a database only when +the instance's DB role (read from its `odoo.conf`, prompted and validated) owns it or its name equals the +instance name. A filestore folder alone SHALL NOT select a database, and databases whose name merely starts with +the instance name SHALL be reported and SHALL NOT be selected; the operator may add any of them as an extra. +The instance role SHALL NOT be `postgres`, the admin user, or a superuser. The databases SHALL be dropped before +their filestores are removed. A data dir that is the instance's own (`/var/lib/odoo/`, or one inside +the home) SHALL be removed whole; any other data dir may be shared, so only the selected databases' filestores +SHALL be removed from it. The Linux user SHALL be removed only when its home is `/opt/odoo/`, without +`userdel -r`. + +#### Scenario: Databases are selected by owner or exact name + +- **WHEN** the purge collects databases to remove +- **THEN** it selects, when admin DB access is available, the databases owned by the instance's DB role or + named exactly like the instance, plus any operator-supplied extras that are valid database names; filestore + folders of databases it did not select are listed as not selected + +#### Scenario: Look-alike databases are reported, not selected + +- **WHEN** a database's name starts with the instance name (with `_` and `%` matched literally) but the + instance's role does not own it +- **THEN** the tool lists it as not selected, and it is dropped only if the operator adds it as an extra + +#### Scenario: An administrator role is refused + +- **WHEN** the role given for the instance is `postgres`, the admin user, or a superuser, or cannot be checked +- **THEN** the tool refuses and builds no plan + +#### Scenario: A role shared with another instance selects nothing and is kept + +- **WHEN** another instance's `odoo.conf` connects as the same DB role, or as the role named after the instance +- **THEN** the tool warns, selects no database by owner (only the one named like the instance), and does not + drop that role + +#### Scenario: A shared data dir keeps other instances' filestores + +- **WHEN** the instance's data dir is not its own (for example `/var/lib/odoo` or a custom path) +- **THEN** the plan removes only `filestore/` of the selected databases from it + +#### Scenario: The managed data dir is found after a delete + +- **WHEN** `/var/lib/odoo/` exists but the instance's `odoo.conf` is gone +- **THEN** the purge still lists its filestores and removes it + +#### Scenario: Admin DB access enables role and database deletion + +- **WHEN** admin PostgreSQL access is resolved (local `sudo -u postgres` or validated remote admin credentials) +- **THEN** the plan drops each selected database (locally with `dropdb --force`; remotely after terminating + its sessions) right after stopping the service, and drops the role the operator named and the instance-named + role, reporting a role it could not drop; without admin access it warns and performs local cleanup only + +#### Scenario: An account the tool did not make is kept + +- **WHEN** the instance's Linux user exists but its home is not `/opt/odoo/` +- **THEN** the purge keeps it and says so + +#### Scenario: The fail2ban jail goes with the log + +- **WHEN** the purge removes the instance's Odoo log +- **THEN** it also removes the instance's fail2ban jail and reloads fail2ban when it is running, since fail2ban + refuses to start with a jail whose log file is missing + +#### Scenario: Purge shows a summary and is phrase-confirmed + +- **WHEN** the purge plan is assembled +- **THEN** it presents a summary of detected resources and executes only after the operator types the exact + `DELETE-ALL ` phrase diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/log-rotation/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/log-rotation/spec.md new file mode 100644 index 0000000..e27d5ea --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/log-rotation/spec.md @@ -0,0 +1,19 @@ +## 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, using `copytruncate` so the running service is not restarted. + +#### 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-guard-destructive-paths/specs/scheduled-backups/spec.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/scheduled-backups/spec.md new file mode 100644 index 0000000..94c67c2 --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/specs/scheduled-backups/spec.md @@ -0,0 +1,36 @@ +## MODIFIED Requirements + +### Requirement: Configure a scheduled backup + +The tool SHALL install a systemd service and timer that back up an instance's database (via local +`sudo -u postgres pg_dump`) and, optionally, its filestore, applying retention, on an operator-chosen schedule. +The script SHALL take every value shell-quoted, SHALL write private files (`umask 077`, directory `700`, unit +`UMask=0077`), SHALL leave no partial file behind, and SHALL exit non-zero when the dump fails, when the dump has +no readable table of contents (`pg_restore --list`), when the filestore directory is missing, or when the +archive fails — so the unit shows failed instead of reporting a backup that is not there. The destination +SHALL be an absolute, dedicated directory (a shared system directory such as `/` or `/tmp` is refused), and +retention SHALL keep the newest backups by the timestamp in their names. + +#### Scenario: A timer and backup script are installed + +- **WHEN** the operator configures a scheduled backup with a database, destination, schedule, and retention +- **THEN** the plan writes `/usr/local/sbin/odoo-backup-.sh`, an `odoo-backup-.service`, and + an `odoo-backup-.timer` with the chosen `OnCalendar`, reloads systemd, and enables and starts the + timer + +#### Scenario: A failed dump fails the run + +- **WHEN** `pg_dump` fails or produces a dump `pg_restore --list` cannot read +- **THEN** the script exits non-zero, removes the partial file, and writes no `.dump` + +#### Scenario: Filestore is optional + +- **WHEN** the operator declines including the filestore +- **THEN** the backup script dumps only the database (no filestore archive) + +#### Scenario: Retention prunes old backups + +- **WHEN** the scheduled backup runs +- **THEN** it keeps the chosen number of newest dumps (and filestore archives) named + `----` and removes the older ones, never touching other instances' or other + databases' backups diff --git a/openspec/changes/archive/2026-10-08-guard-destructive-paths/tasks.md b/openspec/changes/archive/2026-10-08-guard-destructive-paths/tasks.md new file mode 100644 index 0000000..6b11c6a --- /dev/null +++ b/openspec/changes/archive/2026-10-08-guard-destructive-paths/tasks.md @@ -0,0 +1,26 @@ +# Tasks + +## 1. Spec deltas + +- [x] 1.1 execution-safety, instance-provisioning, instance-removal, data-backup-restore, disk-usage, + scheduled-backups, log-rotation. +- [x] 1.2 `openspec validate --specs` passes. + +## 2. Code + +- [x] 2.1 `models.py`: reserved instance names; `system.db_role_absent`. +- [x] 2.2 Install: free-name check at the prompt; confirm outside the try; previewed, asked cleanup of what the + run made; database-only cleanup drops the new role only. +- [x] 2.3 Purge: owner/name selection, administrator roles refused, order, per-filestore removal on a shared + data dir, role condition, guarded `userdel`, logrotate policy. +- [x] 2.4 `/var/log/odoo` root-owned, per-instance logs, `su` dropped from logrotate. +- [x] 2.5 Delete: owner check, order, data dir from both config locations, best-effort nginx, logrotate policy. +- [x] 2.6 Copies: scoped chown, move aside, copy into the target, source check, `REPLACE `. +- [x] 2.7 Backup directory validation; retention by name; valid database names from file names. +- [x] 2.8 i18n entries. + +## 3. Verification + +- [x] 3.1 Unit tests (`tests/test_destructive_guards.py`). +- [x] 3.2 `tools/verify_data_safety.py` executes the copy, retention, log directory, install cleanup, purge + account removal, delete without nginx, and the superuser refusal against a PostgreSQL of its own. diff --git a/openspec/specs/data-backup-restore/spec.md b/openspec/specs/data-backup-restore/spec.md index 0b87657..e497d11 100644 --- a/openspec/specs/data-backup-restore/spec.md +++ b/openspec/specs/data-backup-restore/spec.md @@ -16,7 +16,9 @@ The tool SHALL back up an instance's database (as a compressed custom-format dum gzipped tar) into the chosen backup directory, using a **single timestamp for the whole operation**, naming each artifact `----`, and writing each artifact **atomically** (a temporary file promoted only on success). The backup directory SHALL be private to root (`700`) and every artifact SHALL be -written private (`umask 077`): a dump holds password hashes, API keys and mail/payment secrets. +written private (`umask 077`): a dump holds password hashes, API keys and mail/payment secrets. The backup +directory SHALL be an absolute, dedicated directory: a shared system directory (`/`, `/tmp`, `/var/backups`, +`/etc`, `/opt/odoo`, …) SHALL be refused, since the tool makes the directory private and prunes in it. #### Scenario: Database backup produces an atomic timestamped custom dump @@ -42,6 +44,11 @@ written private (`umask 077`): a dump holds password hashes, API keys and mail/p - **WHEN** a backup is written - **THEN** the backup directory is mode `700` and the artifacts are readable by root only +#### Scenario: A shared directory is not a backup directory + +- **WHEN** the operator gives `/tmp` or `/var/backups` as the backup directory +- **THEN** the tool refuses it and asks for a dedicated one, such as `/var/backups/` + ### Requirement: Restore Restoring SHALL create the target database from a selected dump and/or restore a filestore archive, refusing to @@ -69,8 +76,9 @@ target filestore SHALL be moved aside, never deleted, and the restored files SHA #### Scenario: Restored files belong to the instance user - **WHEN** a filestore archive is extracted -- **THEN** tar does not restore the archive's owners (`--no-same-owner`) and the plan chowns the instance's data - dir to the instance user, so Odoo can write attachments and sessions +- **THEN** tar does not restore the archive's owners (`--no-same-owner`) and the plan gives the files to the + instance user: the whole data dir when it is the instance's own (`/var/lib/odoo/` or inside its + home), only that database's filestore when the data dir may be shared #### Scenario: Restore is phrase-confirmed @@ -107,8 +115,12 @@ target database only when that database belongs to the target's own role (read f operator explicitly confirms the overwrite. Migration semantics (copied vs moved, neutralize) SHALL apply in both cases, and the duplicated filestore SHALL -be placed under the **target** instance's data directory, which SHALL be owned by the target system user (so -Odoo can create its `sessions`/`filestore` entries). Every database the tool seeds SHALL have its access +be placed under the **target** instance's data directory — the one its configuration names, also for a replica +whose `odoo.conf` is not written yet — and owned by the target system user (the whole data dir when it is the +target's own, only the copied filestore otherwise). A filestore already at the target SHALL be moved aside to +`.replaced-`, never deleted, and the copy SHALL run only when the source filestore exists. +A target that is not a whole instance of this tool (home and `odoo.conf`) SHALL be provisioned only under a free +name, and never refreshed. Every database the tool seeds SHALL have its access **restricted to its owner** — `CONNECT` revoked from `PUBLIC` and granted to the owning role — so an instance's database role cannot reach other instances' databases. Local drops SHALL use `dropdb --force`, which closes the database's sessions and refuses new ones in one statement. @@ -165,7 +177,8 @@ database's sessions and refuses new ones in one statement. - **WHEN** duplication runs - **THEN** copied/moved and neutralize semantics are applied to the target and execution proceeds only after - the operator types the exact `DUPLICATE ` phrase + the operator types the exact `DUPLICATE ` phrase for a replica, or `REPLACE ` for a refresh, + whose data it replaces #### Scenario: Seeded database is restricted to its owner @@ -176,8 +189,13 @@ database's sessions and refuses new ones in one statement. #### Scenario: Target data dir is owned by the target user - **WHEN** the filestore is copied into the target data directory (created as root) -- **THEN** the plan chowns the whole target data directory to the target system user, so Odoo can create its - `sessions` and `filestore` entries +- **THEN** the plan chowns the target's own data directory to the target system user, so Odoo can create its + `sessions` and `filestore` entries; in a data directory that may be shared, only the copied filestore + +#### Scenario: A previous target filestore is moved aside + +- **WHEN** the target filestore already exists +- **THEN** it is moved to `.replaced-` and the copy lands in the target, not nested in it #### Scenario: Unsafe database name is rejected diff --git a/openspec/specs/disk-usage/spec.md b/openspec/specs/disk-usage/spec.md index 7e336e8..aa1c3e9 100644 --- a/openspec/specs/disk-usage/spec.md +++ b/openspec/specs/disk-usage/spec.md @@ -23,7 +23,9 @@ recent ones **per database** and kind (DB dumps and filestore archives), through preview/confirm/apply flow. Backups SHALL be matched by their exact name pattern (`----` plus the kind's suffix), so another instance's or another database's backups are never removed. Backups named before the database was part of the name (`_`) SHALL be -pruned as one more group, matched exactly. +pruned as one more group, matched exactly. The newest backups SHALL be the latest timestamps in their names, not +the latest modification times (a copied or restored file gets a new one), and a database name read from a file +name SHALL be used only when it is a valid database name. #### Scenario: Retention keeps the N newest of each kind diff --git a/openspec/specs/execution-safety/spec.md b/openspec/specs/execution-safety/spec.md index 4f705a5..4c4f5bb 100644 --- a/openspec/specs/execution-safety/spec.md +++ b/openspec/specs/execution-safety/spec.md @@ -126,31 +126,44 @@ configs, or existing Nginx vhosts. ### Requirement: Automatic cleanup on failed install -When a provisioning run fails **or is interrupted** while applying, the tool SHALL run a best-effort cleanup of -that instance's residues so the operation can be retried cleanly, and SHALL return control to the menu rather -than terminating the program. Whether the cleanup drops the instance DB role is determined by the **install -mode**, not by whether the role was newly created. +When a provisioning run fails **or is interrupted while applying**, the tool SHALL preview a cleanup of what that +run made, ask whether to apply it (default yes), and return control to the menu rather than terminate the +program. Cancelling or interrupting at the plan confirmation SHALL change nothing and run no cleanup. Because a +new instance takes a name with nothing on the host under it, the cleanup removes the instance's unit, config, +home, Nginx vhosts, SSL dir, log, logrotate policy and Linux user; it SHALL remove the data dir only when the +run created it and SHALL drop the DB role only when the role did not exist before the run (unknown counts as +existing). A PostgreSQL-only install SHALL clean up its role only. -#### Scenario: Failed install triggers cleanup +#### Scenario: Failed install offers the cleanup of what it made - **WHEN** applying an install plan raises an error partway through -- **THEN** the tool runs cleanup commands (stop/disable/remove service, remove config, home, Nginx vhosts, SSL - dir, and — in the PostgreSQL-install modes — the instance DB role, even if it pre-existed) with stop-on-error - disabled +- **THEN** the tool previews the cleanup steps, asks whether to apply them, and applies them with stop-on-error + disabled when the operator agrees -#### Scenario: Odoo-only install never drops the DB role on cleanup +#### Scenario: Interrupted install offers the same cleanup -- **WHEN** an Odoo-only install (no PostgreSQL) fails and cleanup runs -- **THEN** the cleanup does not drop the instance DB role, since that mode does not own it +- **WHEN** applying an install plan is interrupted (e.g. Ctrl+C) +- **THEN** the same cleanup is previewed and offered before control returns to the menu -#### Scenario: Interrupted install triggers cleanup +#### Scenario: Nothing is undone before anything ran -- **WHEN** applying an install plan is interrupted (e.g. Ctrl+C) -- **THEN** the same best-effort cleanup runs before control returns to the menu +- **WHEN** the operator cancels, or presses Ctrl+C, at the plan confirmation +- **THEN** no command runs, no cleanup runs, and the host is left as it was + +#### Scenario: What existed before the run is kept + +- **WHEN** the data dir or the DB role already existed when the install started, or the role's existence could + not be checked +- **THEN** the cleanup neither removes that data dir nor drops that role + +#### Scenario: A PostgreSQL-only install touches no Odoo artifact + +- **WHEN** a database-only install fails +- **THEN** the cleanup drops only the role it created, and touches no unit, home, config or vhost #### Scenario: Cleanup returns to the menu -- **WHEN** cleanup finishes after a failed or interrupted install +- **WHEN** cleanup finishes, or is declined, after a failed or interrupted install - **THEN** control returns to the menu and the program does not crash with an uncaught exception ### Requirement: Secret input is not echoed diff --git a/openspec/specs/instance-provisioning/spec.md b/openspec/specs/instance-provisioning/spec.md index 84c2581..eaea1be 100644 --- a/openspec/specs/instance-provisioning/spec.md +++ b/openspec/specs/instance-provisioning/spec.md @@ -31,6 +31,24 @@ loopback and the tool SHALL NOT open PostgreSQL to the network. - **THEN** the plan performs the DB setup without remote access (no `listen_addresses` change, no `pg_hba` rule) followed by the Odoo base setup, and optionally configures Nginx +### Requirement: A new instance takes a free name + +An install with Odoo, and a replica, SHALL refuse an instance name that is a system account or service name +(`backup`, `nginx`, `postgres`, `root`, …) or under which the host already has a home, config dir, systemd unit, +Nginx vhost, SSL dir or Linux user, and SHALL ask for the name again, pointing to Manage instances for an +existing instance. The name becomes the instance's Linux user, unit, home and log: taking an existing one would +hand the tool another account or service, and the cleanup of a failed install would remove it. + +#### Scenario: An existing instance is not installed over + +- **WHEN** the operator types the name of an instance that already exists on the host +- **THEN** the tool reports what exists under that name and asks for another name before any plan is built + +#### Scenario: A system name is refused + +- **WHEN** the operator types `backup` (whose home is `/var/backups`) or `nginx` as the instance name +- **THEN** the tool refuses it and asks for another name + ### Requirement: Odoo base setup The Odoo base setup SHALL install OS dependencies, create the instance system user and directory layout, clone @@ -42,9 +60,15 @@ config and systemd unit, and register the service. - **WHEN** the base setup runs for a new instance - **THEN** it creates the system user (if missing), the `/opt/odoo/` home with `odoo`, `addons-oca`, - `addons-custom` subdirs, the `/etc/odoo/` config dir, `/var/log/odoo`, and the data dir - `/var/lib/odoo/` (mode `750`, owned by the instance user), with correct ownership and a `750` - config dir + `addons-custom` subdirs, the `/etc/odoo/` config dir, and the data dir `/var/lib/odoo/` + (mode `750`, owned by the instance user), with correct ownership and a `750` config dir + +#### Scenario: Each instance owns only its own log + +- **WHEN** the base setup prepares the logs +- **THEN** `/var/log/odoo` is `root:root` mode `755`, the instance owns only `/var/log/odoo/.log` + (mode `640`), the logs of earlier instances (accounts whose home is `/opt/odoo/`) get their own owner + back, and the `su` directive leaves this tool's logrotate policies for `/var/log/odoo`, which root rotates #### Scenario: Odoo repo and venv are prepared diff --git a/openspec/specs/instance-removal/spec.md b/openspec/specs/instance-removal/spec.md index 2ee4370..610e3bd 100644 --- a/openspec/specs/instance-removal/spec.md +++ b/openspec/specs/instance-removal/spec.md @@ -5,26 +5,28 @@ Remove an instance and its residues. Two levels are supported: a scoped delete of an instance's service, config, home, Nginx vhosts, and SSL (optionally its database and filestore), and a total superuser purge that additionally removes -the Linux user, logs, all databases matching the instance, and PostgreSQL roles. +the Linux user, logs, the databases its role owns or named like it, and PostgreSQL roles. Both are gated by an exact confirmation phrase. ## Requirements ### Requirement: Scoped instance delete -The tool SHALL remove an instance's systemd service, its scheduled-backup timer, Odoo config, home, Nginx -vhosts (available and enabled), and SSL directory, and MAY additionally drop the database and remove one -database's filestore when the operator opts in. Removing the home SHALL NOT remove filestores the operator did -not ask to remove: when the instance's data dir lies inside its home (no `data_dir` outside it in `odoo.conf`), -the plan SHALL first move the data dir to a private `/var/backups//kept-data-dir-`. When a -database drop is requested for a database that does not exist, the tool SHALL warn and skip the drop rather -than fail the operation. +The tool SHALL remove an instance's systemd service, its scheduled-backup timer, Odoo config, logrotate policy, +home, Nginx vhosts (available and enabled), and SSL directory, and MAY additionally drop the database and remove +one database's filestore when the operator opts in. Removing the home SHALL NOT remove filestores the operator +did not ask to remove: when the instance's data dir (read from its `odoo.conf`, current or legacy location) lies +inside its home, the plan SHALL first move it to a private `/var/backups//kept-data-dir-`. +A database SHALL be dropped only when it exists and belongs to the instance's role (from its `odoo.conf`); the +tool SHALL warn and skip the drop otherwise rather than fail the operation. Nginx SHALL be validated and +reloaded only when it is installed, and its failure SHALL NOT stop the removal. #### Scenario: System residues are removed and Nginx reloaded - **WHEN** the operator deletes an instance - **THEN** the plan stops and disables the service, removes the unit and reloads systemd, removes the backup - timer, the config dir, home, both Nginx vhosts, and the SSL dir, then validates and reloads Nginx + timer, the config dir, the logrotate policy, home, both Nginx vhosts, and the SSL dir, then validates and + reloads Nginx when it is installed #### Scenario: The data dir survives the removal of the home @@ -35,8 +37,13 @@ than fail the operation. #### Scenario: Optional database and filestore removal - **WHEN** the operator opts to drop the database and/or remove a filestore -- **THEN** the plan adds a `dropdb --if-exists` for the named database and/or an `rm -rf` of that database's - resolved filestore path (a name Odoo accepts as a database name), before the data dir is moved +- **THEN** the plan adds a `dropdb --if-exists` for the named database (a name Odoo accepts, owned by the + instance's role) before the config is removed, and/or an `rm -rf` of that database's resolved filestore path + +#### Scenario: Another role's database is not dropped + +- **WHEN** the database to drop belongs to a role other than the instance's +- **THEN** the tool reports it and omits the drop #### Scenario: Non-existent database is skipped with a warning @@ -45,6 +52,11 @@ than fail the operation. - **THEN** the tool warns that the database was not found and omits the drop, continuing with the rest of the removal instead of crashing +#### Scenario: A host without nginx still removes the instance + +- **WHEN** nginx is not installed, or `nginx -t` fails because of another site +- **THEN** the removal completes and nginx is not reloaded + #### Scenario: Delete is phrase-confirmed - **WHEN** the delete plan is assembled @@ -53,18 +65,23 @@ than fail the operation. ### Requirement: Total superuser purge The tool SHALL provide a total purge that, in addition to the scoped removal, deletes the instance's Linux -user, its Odoo/Nginx logs, its fail2ban jail, its backup timer, the filestore root, the instance's databases, -and the instance's PostgreSQL roles. Database discovery SHALL select a database only when the instance's DB -role (which the tool prompts for and validates) owns it or its name equals the instance name. Databases whose -name merely starts with the instance name SHALL be reported to the operator and SHALL NOT be selected; the -operator may add any of them as an extra. - -#### Scenario: Databases are discovered from filestore, by prefix, and by owner +user, its Odoo/Nginx logs, its logrotate policy, its fail2ban jail, its backup timer, its filestores, the +instance's databases, and the instance's PostgreSQL roles. Database discovery SHALL select a database only when +the instance's DB role (read from its `odoo.conf`, prompted and validated) owns it or its name equals the +instance name. A filestore folder alone SHALL NOT select a database, and databases whose name merely starts with +the instance name SHALL be reported and SHALL NOT be selected; the operator may add any of them as an extra. +The instance role SHALL NOT be `postgres`, the admin user, or a superuser. The databases SHALL be dropped before +their filestores are removed. A data dir that is the instance's own (`/var/lib/odoo/`, or one inside +the home) SHALL be removed whole; any other data dir may be shared, so only the selected databases' filestores +SHALL be removed from it. The Linux user SHALL be removed only when its home is `/opt/odoo/`, without +`userdel -r`. + +#### Scenario: Databases are selected by owner or exact name - **WHEN** the purge collects databases to remove -- **THEN** it gathers filestore-derived database names and, when admin DB access is available, the databases - owned by the instance's DB role or named exactly like the instance, plus any operator-supplied extras that are - valid database names +- **THEN** it selects, when admin DB access is available, the databases owned by the instance's DB role or + named exactly like the instance, plus any operator-supplied extras that are valid database names; filestore + folders of databases it did not select are listed as not selected #### Scenario: Look-alike databases are reported, not selected @@ -72,18 +89,38 @@ operator may add any of them as an extra. instance's role does not own it - **THEN** the tool lists it as not selected, and it is dropped only if the operator adds it as an extra +#### Scenario: An administrator role is refused + +- **WHEN** the role given for the instance is `postgres`, the admin user, or a superuser, or cannot be checked +- **THEN** the tool refuses and builds no plan + #### Scenario: A role shared with another instance selects nothing and is kept -- **WHEN** another instance's `odoo.conf` connects as the same DB role +- **WHEN** another instance's `odoo.conf` connects as the same DB role, or as the role named after the instance - **THEN** the tool warns, selects no database by owner (only the one named like the instance), and does not drop that role +#### Scenario: A shared data dir keeps other instances' filestores + +- **WHEN** the instance's data dir is not its own (for example `/var/lib/odoo` or a custom path) +- **THEN** the plan removes only `filestore/` of the selected databases from it + +#### Scenario: The managed data dir is found after a delete + +- **WHEN** `/var/lib/odoo/` exists but the instance's `odoo.conf` is gone +- **THEN** the purge still lists its filestores and removes it + #### Scenario: Admin DB access enables role and database deletion - **WHEN** admin PostgreSQL access is resolved (local `sudo -u postgres` or validated remote admin credentials) -- **THEN** the plan drops each candidate database (locally with `dropdb --force`; remotely after terminating - its sessions), and drops the role the operator named and the instance-named role; without admin access it - warns and performs local cleanup only +- **THEN** the plan drops each selected database (locally with `dropdb --force`; remotely after terminating + its sessions) right after stopping the service, and drops the role the operator named and the instance-named + role, reporting a role it could not drop; without admin access it warns and performs local cleanup only + +#### Scenario: An account the tool did not make is kept + +- **WHEN** the instance's Linux user exists but its home is not `/opt/odoo/` +- **THEN** the purge keeps it and says so #### Scenario: The fail2ban jail goes with the log diff --git a/openspec/specs/log-rotation/spec.md b/openspec/specs/log-rotation/spec.md index fb813cb..babca7b 100644 --- a/openspec/specs/log-rotation/spec.md +++ b/openspec/specs/log-rotation/spec.md @@ -13,8 +13,8 @@ optional size threshold, using `copytruncate` so the running service is not rest - **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` plus an - `su ` directive), and validates it with `logrotate -d` + `/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 diff --git a/openspec/specs/scheduled-backups/spec.md b/openspec/specs/scheduled-backups/spec.md index dff60f1..e8e5524 100644 --- a/openspec/specs/scheduled-backups/spec.md +++ b/openspec/specs/scheduled-backups/spec.md @@ -12,7 +12,9 @@ The tool SHALL install a systemd service and timer that back up an instance's da The script SHALL take every value shell-quoted, SHALL write private files (`umask 077`, directory `700`, unit `UMask=0077`), SHALL leave no partial file behind, and SHALL exit non-zero when the dump fails, when the dump has no readable table of contents (`pg_restore --list`), when the filestore directory is missing, or when the -archive fails — so the unit shows failed instead of reporting a backup that is not there. +archive fails — so the unit shows failed instead of reporting a backup that is not there. The destination +SHALL be an absolute, dedicated directory (a shared system directory such as `/` or `/tmp` is refused), and +retention SHALL keep the newest backups by the timestamp in their names. #### Scenario: A timer and backup script are installed diff --git a/tests/test_backup_restore.py b/tests/test_backup_restore.py index 12aa51a..e2c113c 100644 --- a/tests/test_backup_restore.py +++ b/tests/test_backup_restore.py @@ -147,20 +147,32 @@ class FilestoreCopyTests(unittest.TestCase): def test_copies_into_target_and_owns_it(self) -> None: source = InstanceConfig(instance="prod") target = InstanceConfig(instance="dev") - cmds = [c.command for c in _filestore_copy_commands(source, "prod", target, "dev", overwrite=False)] + cmds = [c.command for c in _filestore_copy_commands(source, "prod", target, "dev")] joined = "\n".join(cmds) - self.assertIn("cp -a", joined) - self.assertIn("/opt/odoo/prod/.local/share/Odoo/filestore/prod", joined) - self.assertIn("/opt/odoo/dev/.local/share/Odoo/filestore/dev", joined) - # The whole data dir is chowned (so Odoo can create sessions/), not just filestore. + self.assertIn("test -d /opt/odoo/prod/.local/share/Odoo/filestore/prod", joined) + # Into the target, never nested inside an existing one. + self.assertIn( + "cp -a /opt/odoo/prod/.local/share/Odoo/filestore/prod/. /opt/odoo/dev/.local/share/Odoo/filestore/dev/", + joined, + ) + # A data dir inside the home is the instance's own: it is handed over whole. self.assertIn("chown -R dev:dev /opt/odoo/dev/.local/share/Odoo", joined) self.assertNotIn("rm -rf", joined) - def test_overwrite_removes_previous_target(self) -> None: + def test_a_previous_target_is_moved_aside_not_removed(self) -> None: source = InstanceConfig(instance="prod") target = InstanceConfig(instance="dev") - cmds = [c.command for c in _filestore_copy_commands(source, "prod", target, "dev", overwrite=True)] - self.assertTrue(any("rm -rf" in c and "filestore/dev" in c for c in cmds)) + cmds = [c.command for c in _filestore_copy_commands(source, "prod", target, "dev")] + self.assertTrue(any("mv -- /opt/odoo/dev/.local/share/Odoo/filestore/dev " in c and ".replaced-" in c + for c in cmds)) + self.assertFalse(any("rm -rf" in c for c in cmds)) + + def test_a_shared_data_dir_gets_only_this_filestore_chowned(self) -> None: + source = InstanceConfig(instance="prod") + target = InstanceConfig(instance="dev", data_dir="/srv/odoo-data") + cmds = [c.command for c in _filestore_copy_commands(source, "prod", target, "dev")] + self.assertIn("chown -R dev:dev /srv/odoo-data/filestore/dev", cmds) + self.assertNotIn("chown -R dev:dev /srv/odoo-data", cmds) class NginxServerNameInUseTests(unittest.TestCase): diff --git a/tests/test_destructive_guards.py b/tests/test_destructive_guards.py new file mode 100644 index 0000000..2c85166 --- /dev/null +++ b/tests/test_destructive_guards.py @@ -0,0 +1,203 @@ +"""Guards on the paths that remove or overwrite: install over an existing instance, +the cleanup of a failed install, purge, delete, backup directories and retention.""" + +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, reserved_name_error +from instance_manager.workflows import common, install, purge +from instance_manager.workflows.diskusage import _backed_up_databases + + +def _no_host_artifacts(): + return mock.patch.multiple( + common, path_exists=mock.DEFAULT, service_exists=mock.DEFAULT, user_exists=mock.DEFAULT, + ) + + +class ReservedNameTests(unittest.TestCase): + def test_system_accounts_and_services_are_refused(self) -> None: + for name in ("backup", "nginx", "postgres", "ssh", "root", "ubuntu", "cron"): + self.assertIsNotNone(reserved_name_error(name), name) + + def test_an_instance_name_is_accepted(self) -> None: + self.assertIsNone(reserved_name_error("shop")) + + +class NewInstanceNameTests(unittest.TestCase): + def test_a_free_name_is_accepted(self) -> None: + with _no_host_artifacts() as probes: + for probe in probes.values(): + probe.return_value = False + self.assertIsNone(install._new_instance_name_error("shop")) + + def test_an_existing_home_unit_or_user_is_refused(self) -> None: + for present in ("path_exists", "service_exists", "user_exists"): + with _no_host_artifacts() as probes: + for name, probe in probes.items(): + probe.return_value = name == present + self.assertIsNotNone(install._new_instance_name_error("shop"), present) + + def test_a_reserved_name_is_refused_before_any_probe(self) -> None: + with _no_host_artifacts() as probes: + self.assertIsNotNone(install._new_instance_name_error("backup")) + for probe in probes.values(): + probe.assert_not_called() + + def test_a_db_only_install_checks_the_format_only(self) -> None: + self.assertIsNone(install._new_instance_name_error("backup", with_odoo=False)) + self.assertIsNotNone(install._new_instance_name_error("Bad-Name", with_odoo=False)) + + +class InstallCleanupTests(unittest.TestCase): + def _config(self) -> InstanceConfig: + config = InstanceConfig(instance="shop", data_dir="/var/lib/odoo/shop") + config.normalize_defaults() + return config + + def test_a_data_dir_that_was_there_is_kept(self) -> None: + cmds = install._build_partial_install_cleanup(self._config(), cleanup_db_role=False) + self.assertFalse(any("/var/lib/odoo/shop" in c.command for c in cmds)) + + def test_a_data_dir_this_run_made_is_removed(self) -> None: + cmds = install._build_partial_install_cleanup( + self._config(), cleanup_db_role=False, data_dir_is_new=True + ) + self.assertIn("rm -rf /var/lib/odoo/shop", [c.command for c in cmds]) + + def test_the_user_log_and_logrotate_policy_are_removed(self) -> None: + joined = "\n".join(c.command for c in install._build_partial_install_cleanup(self._config(), False)) + self.assertIn("userdel shop", joined) + self.assertNotIn("userdel -r", joined) + self.assertIn("/etc/logrotate.d/odoo-shop", joined) + + def test_a_db_only_cleanup_drops_only_the_new_role(self) -> None: + cmds = install._build_partial_install_cleanup(self._config(), True, with_odoo=False) + self.assertEqual(len(cmds), 1) + self.assertIn('DROP ROLE IF EXISTS "shop";', cmds[0].command) + + def test_cancel_or_ctrl_c_at_the_confirmation_runs_nothing(self) -> None: + config = self._config() + with mock.patch.object(install, "apply_commands") as apply, \ + mock.patch.object(install, "_confirm_plan", return_value=False): + install._execute_install_with_cleanup([mock.Mock()], config, cleanup_db_role=True) + apply.assert_not_called() + with mock.patch.object(install, "apply_commands") as apply, \ + mock.patch.object(install, "_confirm_plan", side_effect=KeyboardInterrupt): + with self.assertRaises(KeyboardInterrupt): + install._execute_install_with_cleanup([mock.Mock()], config, cleanup_db_role=True) + apply.assert_not_called() + + def test_a_failure_while_running_offers_the_previewed_cleanup(self) -> None: + config = self._config() + with mock.patch.object(install, "_confirm_plan", return_value=True), \ + mock.patch.object(install, "require_root_for_apply"), \ + mock.patch.object(install, "path_exists", return_value=True), \ + mock.patch.object(install, "preview_commands") as preview, \ + mock.patch.object(install, "ask_bool", return_value=True), \ + mock.patch.object(install, "apply_commands", side_effect=[RuntimeError("x"), None]) as apply: + install._execute_install_with_cleanup([mock.Mock()], config, cleanup_db_role=False) + preview.assert_called_once() + cleanup = apply.call_args_list[1].args[0] + # The data dir existed before the run: the cleanup leaves it. + self.assertFalse(any("/var/lib/odoo/shop" in c.command for c in cleanup)) + + +class DataDirTests(unittest.TestCase): + def test_the_config_being_built_wins_over_the_disk(self) -> None: + config = InstanceConfig(instance="dev", data_dir="/var/lib/odoo/dev") + with mock.patch.object(common, "read_odoo_conf") as read: + self.assertEqual(common._resolve_data_dir(config), "/var/lib/odoo/dev") + read.assert_not_called() + + def test_the_legacy_conf_location_is_read(self) -> None: + config = InstanceConfig(instance="old") + confs = {"/etc/old/odoo.conf": {"data_dir": "/srv/old-data"}} + with mock.patch.object(common, "read_odoo_conf", side_effect=lambda p: confs.get(p, {})): + self.assertEqual(common._resolve_data_dir(config), "/srv/old-data") + + def test_private_and_shared_data_dirs(self) -> None: + config = InstanceConfig(instance="shop") + self.assertTrue(common._data_dir_is_private(config, "/var/lib/odoo/shop")) + self.assertTrue(common._data_dir_is_private(config, "/opt/odoo/shop/.local/share/Odoo")) + self.assertFalse(common._data_dir_is_private(config, "/var/lib/odoo")) + self.assertFalse(common._data_dir_is_private(config, "/opt/odoo/shop2/data")) + + +class BackupDirTests(unittest.TestCase): + def test_shared_or_relative_directories_are_refused(self) -> None: + for path in ("/", "/tmp", "/tmp/", "/var/backups", "/etc", "/opt/odoo", "backups", "/a/../tmp"): + self.assertIsNotNone(common._backup_dir_error(path), path) + + def test_a_dedicated_directory_is_accepted(self) -> None: + self.assertIsNone(common._backup_dir_error("/var/backups/shop")) + self.assertIsNone(common._backup_dir_error("/srv/backups/odoo/shop")) + + +class RetentionTests(unittest.TestCase): + def test_newest_by_name_not_by_mtime(self) -> None: + command = planners._prune_command("/b", "'x--*'", 3) + self.assertNotIn("%T@", command) + self.assertIn("sort -r |", command) + self.assertIn("tail -n +4", command) + + def test_database_names_from_file_names_are_checked(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + for name in ("shop--prod--20261001_020000.dump", "shop--$(x)--20261001_020000.dump", + "shop---x--20261001_020000.dump"): + (Path(tmp) / name).touch() + self.assertEqual(_backed_up_databases(InstanceConfig(instance="shop"), tmp), ["prod"]) + + +class PurgeTests(unittest.TestCase): + session = purge.DbAdminSession("local", "127.0.0.1", 5432, "", "") + + def test_administrator_roles_are_refused(self) -> None: + self.assertIsNotNone(purge._superuser_role_error(self.session, "postgres")) + with mock.patch.object(purge, "_query_names", return_value=(["t"], None)): + self.assertIsNotNone(purge._superuser_role_error(self.session, "admin_role")) + with mock.patch.object(purge, "_query_names", return_value=(["f"], None)): + self.assertIsNone(purge._superuser_role_error(self.session, "shop")) + with mock.patch.object(purge, "_query_names", return_value=([], "no server")): + self.assertIsNotNone(purge._superuser_role_error(self.session, "shop")) + + def _remove(self, data_dir: str, managed_exists: bool = False) -> list[str]: + config = InstanceConfig(instance="shop") + with mock.patch.object(purge, "_resolve_data_dir", return_value=data_dir), \ + mock.patch.object(purge.os.path, "isdir", return_value=managed_exists): + return [c.command for c in purge._remove_data_commands(config, ["shop", "shop_test"])] + + def test_a_shared_data_dir_loses_only_the_selected_filestores(self) -> None: + cmds = self._remove("/var/lib/odoo") + self.assertEqual(cmds, [ + "rm -rf /var/lib/odoo/filestore/shop", "rm -rf /var/lib/odoo/filestore/shop_test", + ]) + + def test_the_managed_data_dir_is_removed_whole(self) -> None: + self.assertEqual(self._remove("/var/lib/odoo/shop"), ["rm -rf /var/lib/odoo/shop"]) + + def test_a_data_dir_in_the_home_goes_with_the_home(self) -> None: + self.assertEqual(self._remove("/opt/odoo/shop/.local/share/Odoo"), []) + + def test_after_a_delete_the_managed_data_dir_is_still_found(self) -> None: + cmds = self._remove("/opt/odoo/shop/.local/share/Odoo", managed_exists=True) + self.assertEqual(cmds, ["rm -rf /var/lib/odoo/shop"]) + + +class LogDirTests(unittest.TestCase): + def test_no_instance_takes_the_shared_log_directory(self) -> None: + config = InstanceConfig(instance="shop") + config.normalize_defaults() + joined = "\n".join(c.command for c in planners.plan_odoo_base_setup(config)) + self.assertNotRegex(joined, r"chown -R [^\n]*/var/log/odoo( |$)") + self.assertIn("chown root:root /var/log/odoo", joined) + self.assertIn("chown shop:shop /var/log/odoo/shop.log && chmod 640 /var/log/odoo/shop.log", joined) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_planners.py b/tests/test_planners.py index bbf3d60..3bd9c85 100644 --- a/tests/test_planners.py +++ b/tests/test_planners.py @@ -154,7 +154,8 @@ def test_includes_core_directives(self) -> None: self.assertIn(" weekly", content) self.assertIn(" rotate 14", content) self.assertIn(" copytruncate", content) - self.assertIn(" su odoo18 odoo18", content) + # /var/log/odoo is root's: root rotates, with no `su`. + self.assertNotIn(" su ", content) self.assertIn(" compress", content) self.assertIn(" delaycompress", content) self.assertTrue(content.rstrip().endswith("}")) diff --git a/tools/verify_data_safety.py b/tools/verify_data_safety.py index bd93597..eda0633 100644 --- a/tools/verify_data_safety.py +++ b/tools/verify_data_safety.py @@ -29,7 +29,7 @@ from instance_manager import planners, system # noqa: E402 from instance_manager.models import InstanceConfig # noqa: E402 -from instance_manager.workflows import backup_restore, common, purge # noqa: E402 +from instance_manager.workflows import backup_restore, common, install, manage, purge # noqa: E402 FAILURES: list[str] = [] @@ -180,6 +180,180 @@ def _keep_data_dir(root: Path) -> None: check("the home itself is gone", not Path(config.odoo_home).exists()) +def _retention_by_name(root: Path) -> None: + """A copied or restored backup gets a new mtime: the newest is the latest name.""" + backup_dir = root / "byname" + backup_dir.mkdir() + names = [f"shop--acme--2025010{day}_000000.dump" for day in (1, 2, 3)] + for age, name in enumerate(names): + path = backup_dir / name + path.write_text("x", encoding="utf-8") + stamp = time.time() - 3600 * age # the oldest name has the newest mtime + os.utime(path, (stamp, stamp)) + for command in planners.plan_backup_retention(InstanceConfig(instance="shop"), str(backup_dir), 1, ["acme"]): + _run(command.command, dict(os.environ)) + left = sorted(p.name for p in backup_dir.iterdir()) + check("retention keeps the newest name, whatever the mtimes", left == [names[-1]], str(left)) + + +# Host paths the plans name, moved under a temp root so their shell can run. +_HOST_DIRS = ("/var/log/odoo", "/etc/logrotate.d", "/opt/odoo", "/etc/odoo", "/etc/systemd/system", + "/etc/nginx", "/var/lib/odoo", "/var/log/nginx", "/etc/fail2ban") + + +def _rebase(command: str, root: Path) -> str: + for directory in _HOST_DIRS: + command = command.replace(directory, f"{root}{directory}") + return command + + +def _host_stubs(root: Path, passwd: dict[str, str]) -> dict[str, str]: + """chown/userdel/systemctl record their calls; id/getent answer from ``passwd``.""" + bindir = root / "hoststubs" + bindir.mkdir() + calls = root / "calls" + for name in ("chown", "userdel", "systemctl"): + _write_exe(bindir / name, f'echo "{name} $*" >> {calls}\n') + entries = "\n".join(f"{user}:x:999:999::{home}:/usr/sbin/nologin" for user, home in passwd.items()) + _write_exe(bindir / "getent", f"grep \"^$2:\" <<'EOF'\n{entries}\nEOF\n") + _write_exe(bindir / "id", 'getent passwd "${@: -1}" >/dev/null\n') + return dict(os.environ, PATH=f"{bindir}:{os.environ['PATH']}") + + +def _calls(root: Path) -> str: + path = root / "calls" + return path.read_text(encoding="utf-8") if path.exists() else "" + + +def _filestore_copy(root: Path) -> None: + with mock.patch.object(InstanceConfig, "base_instances_dir", str(root / "opt")): + source, target = InstanceConfig(instance="prod"), InstanceConfig(instance="dev") + src = Path(common._filestore_path(source, "prod")) + dst = Path(common._filestore_path(target, "dev")) + (src / "ab").mkdir(parents=True) + (src / "ab" / "new").write_text("new", encoding="utf-8") + (dst / "cd").mkdir(parents=True) + (dst / "cd" / "old").write_text("old", encoding="utf-8") + env = _host_stubs(root, {}) + commands = backup_restore._filestore_copy_commands(source, "prod", target, "dev") + failed = [c.description for c in commands if _run(c.command, env).returncode != 0] + check("the filestore copy runs", not failed, str(failed)) + check("it lands in the target, not nested in it", (dst / "ab" / "new").is_file() and not (dst / "prod").exists(), + str(list(dst.iterdir()))) + aside = [p for p in dst.parent.iterdir() if p.name.startswith("dev.replaced-")] + check("the previous target filestore is moved aside, not removed", + len(aside) == 1 and (aside[0] / "cd" / "old").is_file(), str(list(dst.parent.iterdir()))) + check("the private data dir is handed to the target user", + f"chown -R dev:dev {Path(common._resolve_data_dir(target))}" in _calls(root), _calls(root)) + + shutil.rmtree(src) + before = sorted(p.name for p in dst.parent.iterdir()) + results = [] + for command in backup_restore._filestore_copy_commands(source, "prod", target, "dev"): + results.append(_run(command.command, env).returncode) + if results[-1]: + break + check("a missing source stops the copy before the target is touched", + results == [1] and sorted(p.name for p in dst.parent.iterdir()) == before, str(results)) + + +def _log_dir(root: Path) -> None: + env = _host_stubs(root, {"shop": f"{root}/opt/odoo/shop", "other": f"{root}/opt/odoo/other", + "stranger": "/home/stranger"}) + logs = root / "var/log/odoo" + logs.mkdir(parents=True) + for name in ("other", "stranger"): + (logs / f"{name}.log").write_text("x", encoding="utf-8") + rotate = root / "etc/logrotate.d" + rotate.mkdir(parents=True) + (rotate / "odoo-other").write_text( + f"{root}/var/log/odoo/other.log {{\n weekly\n copytruncate\n su other other\n}}\n", encoding="utf-8") + foreign = "/srv/app/app.log {\n su app app\n}\n" + (rotate / "odoo-foreign").write_text(foreign, encoding="utf-8") + config = InstanceConfig(instance="shop") + failed = [c.description for c in planners.plan_odoo_log_dir(config) + if _run(_rebase(c.command, root), env).returncode != 0] + calls = _calls(root) + check("the log directory steps run", not failed, str(failed)) + check("the directory goes to root", f"chown root:root {logs}" in calls, calls) + check("the new instance gets its own log", f"chown shop:shop {logs}/shop.log" in calls, calls) + check("an earlier instance gets its log back", f"chown other:other {logs}/other.log" in calls, calls) + check("a log of an account that is not an instance is left alone", "stranger" not in calls, calls) + check("su leaves the tool's logrotate policies", + " su " not in (rotate / "odoo-other").read_text(encoding="utf-8") + and "copytruncate" in (rotate / "odoo-other").read_text(encoding="utf-8")) + check("a policy for other logs is not edited", (rotate / "odoo-foreign").read_text(encoding="utf-8") == foreign) + + +def _install_cleanup(root: Path) -> None: + config = InstanceConfig(instance="shop", data_dir="/var/lib/odoo/shop") + config.normalize_defaults() + env = _host_stubs(root, {"shop": f"{root}/opt/odoo/shop"}) + made = [root / "opt/odoo/shop/odoo", root / "etc/odoo/shop", root / "var/lib/odoo/shop/filestore/kept"] + for directory in made: + directory.mkdir(parents=True) + for kept_data_dir in (True, False): + cleanup = install._build_partial_install_cleanup(config, False, data_dir_is_new=not kept_data_dir) + failed = [c.description for c in cleanup if _run(_rebase(c.command, root), env).returncode != 0] + check("the install cleanup runs", not failed, str(failed)) + check("it removes what the install made", + not (root / "opt/odoo/shop").exists() and not (root / "etc/odoo/shop").exists()) + check(f"a data dir {'that was there is kept' if kept_data_dir else 'the run made is removed'}", + (root / "var/lib/odoo/shop").exists() == kept_data_dir) + check("the account it made is removed", "userdel shop" in _calls(root), _calls(root)) + + +def _purge_user(root: Path) -> None: + for home, removed in ((f"{root}/opt/odoo/shop", True), ("/var/backups", False)): + case = root / ("own" if removed else "system") + case.mkdir() + env = _host_stubs(case, {"shop": home}) + with mock.patch.object(InstanceConfig, "base_instances_dir", str(root / "opt/odoo")): + config = InstanceConfig(instance="shop") + step = next(c for c in _purge_commands(config) if c.description == "Remove the instance Linux user") + result = _run(step.command, env) + check(f"purge {'removes the account it made' if removed else 'keeps an account with another home'}", + result.returncode == 0 and ("userdel shop" in _calls(case)) == removed, _calls(case) + result.stderr) + check("never with userdel -r", "-r" not in _calls(case)) + + +def _purge_commands(config: InstanceConfig) -> list[system.Command]: + """The purge's local steps, built through its flow with the prompts answered.""" + execute = mock.Mock() + with mock.patch.multiple( + purge, _select_existing_instance=mock.Mock(return_value=config.instance), + _validate_instance_or_abort=mock.Mock(return_value=config), + _resolve_db_admin_access=mock.Mock(return_value=None), + ask_text=mock.Mock(side_effect=lambda label, default="", **_: default), + confirm_with_phrase=mock.Mock(return_value=True), _execute_plan=execute, + path_exists=mock.Mock(return_value=False), service_exists=mock.Mock(return_value=False), + list_instances=mock.Mock(return_value=[]), print=mock.Mock(), create=True, + ): + purge.purge_instance_superuser() + return execute.call_args.args[0] + + +def _delete_without_nginx(root: Path) -> None: + config = InstanceConfig(instance="shop") + execute = mock.Mock() + with mock.patch.multiple( + manage, ask_bool=mock.Mock(return_value=False), confirm_with_phrase=mock.Mock(return_value=True), + _execute_plan=execute, + ): + manage._delete_instance(config) + commands = execute.call_args.args[0] + step = next(c for c in commands if "nginx" in c.description.lower() and "Validate" in c.description) + bare = root / "bare" + bare.mkdir() + for name in ("bash", "true"): + (bare / name).symlink_to(shutil.which(name)) + result = subprocess.run(["bash", "-c", step.command], capture_output=True, text=True, env={"PATH": str(bare)}) + check("delete goes on without nginx installed", result.returncode == 0, result.stderr) + _write_exe(bare / "nginx", "exit 1\n") + result = subprocess.run(["bash", "-c", step.command], capture_output=True, text=True, env={"PATH": str(bare)}) + check("and when nginx -t fails", result.returncode == 0, result.stderr) + + # --- against a PostgreSQL of our own ------------------------------------------ @@ -299,6 +473,14 @@ def _against_a_server(root: Path) -> None: others = cluster.value(purge._prefix_only_databases_sql("shop", "shop")).split() check("the prefix report lists look-alikes it did not select", sorted(others) == ["shop2", "shopXeu", "shop_eu"], str(others)) + + cluster.value("CREATE ROLE boss LOGIN SUPERUSER") + admin = purge.DbAdminSession("local", "127.0.0.1", cluster.port, "", "") + with mock.patch.dict(os.environ, env): + check("purge refuses postgres as the instance role", + purge._superuser_role_error(admin, "postgres") is not None) + check("purge refuses a superuser role", purge._superuser_role_error(admin, "boss") is not None) + check("purge accepts an instance role", purge._superuser_role_error(admin, "shop") is None) finally: cluster.stop() @@ -306,11 +488,18 @@ def _against_a_server(root: Path) -> None: def main() -> int: with tempfile.TemporaryDirectory(prefix="oim-verify-data-") as tmp: root = Path(tmp) - for part in ("sched", "manual", "keep", "pg"): + parts = ("sched", "manual", "byname", "keep", "copy", "logs", "cleanup", "purge", "delete", "pg") + for part in parts: (root / part).mkdir() _scheduled_backup(root / "sched") _manual_retention(root / "manual") + _retention_by_name(root / "byname") _keep_data_dir(root / "keep") + _filestore_copy(root / "copy") + _log_dir(root / "logs") + _install_cleanup(root / "cleanup") + _purge_user(root / "purge") + _delete_without_nginx(root / "delete") _against_a_server(root / "pg") print(f"\n{len(FAILURES)} failure(s)" if FAILURES else "\nall checks passed") return 1 if FAILURES else 0