Files
computer-playbook/roles/svc-db-postgres/tasks/01_init.yml
T
kevinveenbirkenbachandClaude Opus 5 ffed838bc7 fix(roles): keep credentials out of the container exec argv, and lint for it
`container exec -e NAME=VALUE` puts VALUE in the argv of the host-side
process, where ps shows it to every local account for as long as the call runs.
no_log cannot mask a process table. The credentials that travel this way are
live: platform_administrator and every secrets.credentials.* value rotate
between the two passes of a matrix-deploy round.

tests/lint/ansible/shell/container/test_exec_env_passes_no_value.py rejects the
form for credential-bearing values, recognised by either the variable's name or
a credential source in the expression behind it. The second signal is what
keeps it from being a name denylist, which would be no protection on its own: a
secret handed over as -e FOO={{ lookup('users','x').password }} is caught by its
source although its name says nothing. Literal values pass; ports, paths and
model names are not worth moving.

The 24 sites it found now pass the name alone and bind the value in the task's
environment. Value-shaping filters stay, because the container needs the shaped
form; the shell-quoting | quote goes, and that is safe: shlex.quote builds
armour the host shell consumes while parsing argv, so what docker receives is
byte-identical either way, checked with a password of @, space, apostrophe
and $.

Three sites needed more than the pattern. set_postgres_superuser_password.sh
took the password as $2, so it stood in the argv of the script and of three
container exec calls; it now reads POSTGRES_SUPERUSER_PASSWORD, and both callers
follow. svc-db-elasticsearch built an exec prefix with the password inside it for
five tasks, so the binding moved to the two include sites and is inherited.
web-app-taiga had its upsert as a semicolon-chained one-liner inside the command;
it is a file read on stdin now, which took the name and the e-mail out of the
argv too.

What this does not do, stated because the docstring first claimed otherwise:
with ansible.builtin.script the value reaches no argv, but with shell and
command ansible-core still prefixes NAME='value' onto the module wrapper's own
line, from Shell.env_prefix in build_module_command. The window shrinks from the
whole container call, re-executed on every pass of an until: loop, to the
module's runtime. It does not vanish.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-12 22:54:33 +02:00

161 lines
6.6 KiB
YAML

---
- name: "✅ Assert correct context"
ansible.builtin.assert:
that:
- application_id == 'svc-db-postgres'
- database_consumer_id != 'svc-db-postgres'
- name: 📐 Normalize postgres credential strings for module args
ansible.builtin.set_fact:
postgres_login_password_string: "{{ POSTGRES_PASSWORD | string }}"
postgres_database_password_string: "{{ database_password | string }}"
changed_when: false
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🔍 Postgres | Resolve hosting node + container id"
ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}"
vars:
resolve_service_key: "{{ entity_name }}"
resolve_service_name: "{{ lookup('database', database_consumer_id, 'service_name') }}"
resolve_output_prefix: POSTGRES
resolve_compose_cid: "{{ lookup('database', database_consumer_id, 'container') }}"
- name: ⏳ Wait until postgres TCP auth works
block:
- name: ⏳ Wait for Postgres TCP auth
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
ansible.builtin.shell: > # nocheck: container-exec-resolver POSTGRES_LOCAL_CID resolved at delegate_to runtime
container exec -e PGPASSWORD {{ POSTGRES_LOCAL_CID | quote }} sh -lc
'psql -h "{{ POSTGRES_LOCAL_HOST }}" -p "{{ POSTGRES_PORT }}"
-U postgres -d postgres -c "SELECT 1"'
environment:
PGPASSWORD: "{{ POSTGRES_PASSWORD }}"
delegate_to: "{{ POSTGRES_HOST_NODE }}"
register: pg_ready
until: pg_ready.rc == 0
retries: 30
delay: 5
changed_when: false
rescue:
- name: "💾 Postgres | Dump pg_hba.conf into the diagnostics folder" # nocheck: rescue-extra-diagnostics pg_hba.conf is postgres-specific auth config the generic rescue script cannot capture, and it is what a failed TCP-auth wait needs
ansible.builtin.script: "{{ lookup('path_absolute', 'roles/svc-db-postgres/files/shell/dump_pg_hba.sh') }}"
environment:
POSTGRES_CONTAINER_ADDRESS: "{{ POSTGRES_LOCAL_CID }}"
PGPASSWORD: "{{ POSTGRES_PASSWORD }}"
delegate_to: "{{ POSTGRES_HOST_NODE }}"
changed_when: false
failed_when: false
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🛑 Postgres | fail after pg_hba dump"
ansible.builtin.fail:
msg: "Postgres TCP auth never became ready; pg_hba.conf was dumped into the diagnostics folder (the workflow-level rescue capture collects the full snapshot)"
- name: "📁 Create database: {{ database_name }}"
community.postgresql.postgresql_db:
name: "{{ database_name }}"
state: present
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
- name: "📁 Create database user: {{ database_username }}"
community.postgresql.postgresql_user:
name: "{{ database_username }}"
password: "{{ postgres_database_password_string }}"
login_db: "{{ database_name }}"
state: present
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
- name: "🟢 POSTGRES | Enable login for role {{ database_username }}"
# nocheck: database-query — svc-db-postgres bootstraps its own DB; the `database_query` module's `lookup('database', ...)` indirection isn't usable from inside the role that provides the lookup target.
community.postgresql.postgresql_query:
login_db: postgres
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
query: |
ALTER ROLE "{{ database_username }}"
WITH LOGIN;
register: postgres_enable_login
until: postgres_enable_login is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🔐 Grant ALL privileges on tables in public schema to {{ database_username }}"
community.postgresql.postgresql_privs:
login_db: "{{ database_name }}"
roles: "{{ database_username }}"
objs: ALL_IN_SCHEMA
privs: ALL
type: table
schema: public
state: present
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
- name: "🔐 Grant all privileges on {{ database_name }} to {{ database_username }}"
community.postgresql.postgresql_privs:
login_db: "{{ database_name }}"
roles: "{{ database_username }}"
type: database
privs: ALL
state: present
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
- name: "🔧 Set comprehensive schema privileges for {{ database_username }}"
# nocheck: database-query — svc-db-postgres bootstraps its own DB; the `database_query` module's `lookup('database', ...)` indirection isn't usable from inside the role that provides the lookup target.
community.postgresql.postgresql_query:
login_db: "{{ database_name }}"
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
query: "{{ lookup('template', 'sql/grant_schema_privileges.sql.j2') }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"
- name: "✅ Ensure consumer-specific extensions ({{ database_consumer_id }})"
community.postgresql.postgresql_ext:
login_db: "{{ database_name }}"
ext: "{{ item }}"
state: present
login_user: postgres
login_password: "{{ postgres_login_password_string }}"
login_host: "{{ POSTGRES_LOCAL_HOST }}"
login_port: "{{ POSTGRES_PORT }}"
loop: "{{ lookup('service_extensions', 'postgres')[database_consumer_id] | default([]) }}"
register: postgresql_result
until: postgresql_result is succeeded
retries: "{{ POSTGRES_RETRIES }}"
delay: "{{ POSTGRES_DELAY }}"