From bcba5d349a6cd2ae71fb4ec03e73dfddcb965c27 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Nov 2026 05:24:16 +0100 Subject: [PATCH] security(monitoring): reset the Alerta admin password on every start The entrypoint creates admin users only when they are missing, so an admin user created before Alerta had a password of its own keeps the Grafana password, which stays valid after an operator rotates Grafana's. The respin runs a step right after user creation that sets every admin user's password to ADMIN_PASSWORD, and stops the container if it fails. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- ...ecrets.bats => alerta-patch-upstream.bats} | 11 ++++++- .../monitoring/images/alerta/Containerfile | 11 +++---- .../{redact-secrets.sh => patch-upstream.sh} | 30 +++++++++++++------ .../images/alerta/sync-admin-password.py | 29 ++++++++++++++++++ 4 files changed, 66 insertions(+), 15 deletions(-) rename hack/{alerta-redact-secrets.bats => alerta-patch-upstream.bats} (84%) rename packages/system/monitoring/images/alerta/{redact-secrets.sh => patch-upstream.sh} (58%) create mode 100644 packages/system/monitoring/images/alerta/sync-admin-password.py diff --git a/hack/alerta-redact-secrets.bats b/hack/alerta-patch-upstream.bats similarity index 84% rename from hack/alerta-redact-secrets.bats rename to hack/alerta-patch-upstream.bats index e4a4123b2..9131102d4 100644 --- a/hack/alerta-redact-secrets.bats +++ b/hack/alerta-patch-upstream.bats @@ -4,7 +4,7 @@ load test_helper # The fixtures reproduce the lines of the upstream alerta-web 9.1.0 # docker-entrypoint.sh and nginx.conf.j2 that the respin edits. -SCRIPT="packages/system/monitoring/images/alerta/redact-secrets.sh" +SCRIPT="packages/system/monitoring/images/alerta/patch-upstream.sh" write_fixtures() { cat >"${BATS_TEST_TMPDIR}/docker-entrypoint.sh" <<'EOF' @@ -55,6 +55,15 @@ EOF [ -x "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" ] } +@test "the entrypoint syncs the admin password right after creating admin users" { + write_fixtures + sh "${SCRIPT}" "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" "${BATS_TEST_TMPDIR}/nginx.conf.j2" + + run grep -A1 -x ' alertad user --all --password "${ADMIN_PASSWORD}" || true' "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" + [ "${status}" -eq 0 ] + [ "${lines[1]}" = ' /venv/bin/python /app/sync-admin-password.py' ] +} + @test "nginx logs the request line with the api-key parameter masked" { write_fixtures sh "${SCRIPT}" "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" "${BATS_TEST_TMPDIR}/nginx.conf.j2" diff --git a/packages/system/monitoring/images/alerta/Containerfile b/packages/system/monitoring/images/alerta/Containerfile index 96e28b068..82ac16654 100644 --- a/packages/system/monitoring/images/alerta/Containerfile +++ b/packages/system/monitoring/images/alerta/Containerfile @@ -22,11 +22,12 @@ RUN apt-get update \ && /venv/bin/pip check \ && pip install --no-cache-dir --upgrade virtualenv 'filelock>=4.2.1' -# The upstream entrypoint and nginx config write Alerta's credentials to the -# container log; see redact-secrets.sh. -COPY redact-secrets.sh /tmp/redact-secrets.sh -RUN sh /tmp/redact-secrets.sh /usr/local/bin/docker-entrypoint.sh /app/nginx.conf.j2 \ - && rm /tmp/redact-secrets.sh +# Keeps Alerta's credentials out of the container log and the admin password in +# step with ADMIN_PASSWORD; see patch-upstream.sh. +COPY sync-admin-password.py /app/sync-admin-password.py +COPY patch-upstream.sh /tmp/patch-upstream.sh +RUN sh /tmp/patch-upstream.sh /usr/local/bin/docker-entrypoint.sh /app/nginx.conf.j2 \ + && rm /tmp/patch-upstream.sh # A purge on top of the upstream layers hides files without removing them, so # the result is copied into an empty image. That drops the image config with diff --git a/packages/system/monitoring/images/alerta/redact-secrets.sh b/packages/system/monitoring/images/alerta/patch-upstream.sh similarity index 58% rename from packages/system/monitoring/images/alerta/redact-secrets.sh rename to packages/system/monitoring/images/alerta/patch-upstream.sh index f8721f159..0dc3e72a9 100644 --- a/packages/system/monitoring/images/alerta/redact-secrets.sh +++ b/packages/system/monitoring/images/alerta/patch-upstream.sh @@ -1,21 +1,27 @@ #!/bin/sh -# Keeps Alerta's credentials out of the container log, which tenant users who -# may only view their namespace can still read. +# Adapts the upstream Alerta entrypoint and nginx template. # -# The upstream entrypoint prints the whole environment (ADMIN_PASSWORD, -# ADMIN_KEY, ALERTA_API_KEY) and the admin API keys it creates, and nginx logs -# the request line, where Alertmanager and the Telegram webhook pass the key as -# ?api-key=. Every edit is checked, so an upstream change to either file fails -# the image build instead of silently bringing the output back. +# Credentials out of the container log, which tenant users who may only view +# their namespace can still read: the upstream entrypoint prints the whole +# environment (ADMIN_PASSWORD, ADMIN_KEY, ALERTA_API_KEY) and the admin API +# keys it creates, and nginx logs the request line, where Alertmanager and the +# Telegram webhook pass the key as ?api-key=. # -# Usage: redact-secrets.sh +# Admin password follows ADMIN_PASSWORD: `alertad user --all` only creates +# missing admin users, so an existing one would keep the password it was +# created with. The entrypoint runs sync-admin-password.py right after it. +# +# Every edit is checked, so an upstream change to either file fails the image +# build instead of silently bringing the old behaviour back. +# +# Usage: patch-upstream.sh set -eu entrypoint=$1 nginx_conf=$2 fail() { - echo "redact-secrets: $*" >&2 + echo "patch-upstream: $*" >&2 exit 1 } @@ -31,11 +37,16 @@ edit() { grep -qx 'env | sort' "$entrypoint" || fail "env dump not found in $entrypoint" grep -qx ' alertad key --all' "$entrypoint" || fail "'alertad key --all' not found in $entrypoint" +grep -qx ' alertad user --all --password "${ADMIN_PASSWORD}" || true' "$entrypoint" \ + || fail "admin user creation not found in $entrypoint" grep -q '^ alertad key --username "${ADMIN_USER}" --key "${ADMIN_KEY}" --duration "${MAXAGE}" || true$' "$entrypoint" \ || fail "user-defined key creation not found in $entrypoint" edit "$entrypoint" \ -e '/^env | sort$/d' \ + -e '/^ alertad user --all --password "${ADMIN_PASSWORD}" || true$/a\ + /venv/bin/python /app/sync-admin-password.py +' \ -e 's/^ alertad key --all$/ alertad key --all >\/dev\/null/' \ -e 's/^\( alertad key --username "${ADMIN_USER}" --key "${ADMIN_KEY}" --duration "${MAXAGE}"\) || true$/\1 >\/dev\/null || true/' @@ -43,6 +54,7 @@ if grep -q '^env' "$entrypoint"; then fail "env dump still present in $entrypoint" fi [ "$(grep -c '^ *alertad key .*>/dev/null' "$entrypoint")" -eq 2 ] || fail "key creation output is not silenced in $entrypoint" +grep -qx ' /venv/bin/python /app/sync-admin-password.py' "$entrypoint" || fail "admin password sync not added to $entrypoint" grep -q "^ log_format main " "$nginx_conf" || fail "log_format main not found in $nginx_conf" [ "$(grep -c '"\$request"' "$nginx_conf")" -eq 1 ] || fail "expected exactly one \$request in $nginx_conf" diff --git a/packages/system/monitoring/images/alerta/sync-admin-password.py b/packages/system/monitoring/images/alerta/sync-admin-password.py new file mode 100644 index 000000000..21ce71258 --- /dev/null +++ b/packages/system/monitoring/images/alerta/sync-admin-password.py @@ -0,0 +1,29 @@ +"""Set the password of every Alerta admin user to ADMIN_PASSWORD. + +`alertad user --all` only creates admin users that are missing, so an admin +user that already exists keeps the password it was created with, which on +installations that predate the dedicated Alerta password is Grafana's. The +entrypoint runs this right after it, on every start. It never prints the +password, and a failure stops the container rather than leaving the old +password in place. +""" +import os +import sys + +from alerta.app import create_app + +password = os.environ.get('ADMIN_PASSWORD') +if not password: + sys.exit('ADMIN_PASSWORD is not set') + +app = create_app() +# Imported after the app exists: importing the model first runs into a +# circular import through alerta.auth. +from alerta.models.user import User # noqa: E402 + +with app.app_context(): + for login in app.config['ADMIN_USERS']: + user = User.find_by_username(login) + if user is not None and not user.verify_password(password): + user.update(password=password) + print(f'# Reset the password of admin user {login}.')