From d99a1a063e63b521db2aa0e29b5f7cdb10f594af Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Nov 2026 01:26:22 +0100 Subject: [PATCH 1/4] fix(monitoring): give Alerta a real signing key and its own admin password Alerta signs its session JWTs with SECRET_KEY, but the upstream image never wires a real value into it: the entrypoint renders the generated key into alertad.conf under the name SECRET (which the application never reads) and unsets SECRET_KEY before exec, so at runtime the built-in default from alerta.settings ("changeme") signs and verifies every token. Anyone can mint a valid admin token without credentials. Alerta's admin account also reused Grafana's credentials (grafana-admin-password), and Alerta exposes the whole process environment via /api/management/properties to any admin-scoped caller, so admin on Alerta yielded Grafana's admin password. Generate a strong SECRET_KEY and a dedicated admin password, persist both in the alerta Secret so they survive upgrades and restarts, feed the key to the application through a mounted alertad.conf under the name it actually reads, and stop referencing grafana-admin-password. Add a unittest covering the key, the mount and the credential decoupling. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- .../monitoring/templates/alerta/alerta.yaml | 48 +++++++++-- .../tests/alerta_secret_key_test.yaml | 83 +++++++++++++++++++ 2 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 packages/system/monitoring/tests/alerta_secret_key_test.yaml diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 3199c9b69..01e4cddc6 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -115,9 +115,33 @@ {{- end }} {{- $apiKey := randAlphaNum 32 }} +{{- /* + Alerta signs its session JWTs with SECRET_KEY. The upstream image never + wires a real value into it: the entrypoint renders the generated key into + alertad.conf under the name SECRET (which the application never reads) and + unsets SECRET_KEY before exec, so at runtime the built-in default from + alerta.settings ("changeme") is used to sign and verify every token. We + generate a strong key here, persist it in this Secret so it survives + upgrades and pod restarts, and feed it to the application as a mounted + alertad.conf under the name the application actually reads. +*/}} +{{- $secretKey := randAlphaNum 64 }} +{{- /* + Alerta's admin account must not share Grafana's credentials: Alerta exposes + the whole process environment through /api/management/properties to any + admin-scoped caller, so a shared password would hand Grafana's admin over + with it. Give Alerta its own password, persisted the same way. +*/}} +{{- $adminPassword := randAlphaNum 32 }} {{- $existingSecret := lookup "v1" "Secret" .Release.Namespace "alerta" }} {{- if $existingSecret }} {{- $apiKey = index $existingSecret.data "alerta-api-key" | b64dec }} +{{- if index $existingSecret.data "secret-key" }} +{{- $secretKey = index $existingSecret.data "secret-key" | b64dec }} +{{- end }} +{{- if index $existingSecret.data "admin-password" }} +{{- $adminPassword = index $existingSecret.data "admin-password" | b64dec }} +{{- end }} {{- end }} --- apiVersion: v1 @@ -129,6 +153,12 @@ metadata: type: Opaque data: alerta-api-key: {{ $apiKey | b64enc }} + secret-key: {{ $secretKey | b64enc }} + admin-password: {{ $adminPassword | b64enc }} +stringData: + alertad.conf: | + DEBUG = False + SECRET_KEY = {{ $secretKey | quote }} --- apiVersion: v1 kind: ConfigMap @@ -189,15 +219,12 @@ spec: resources: {{- toYaml .Values.alerta.resources | nindent 12 }} env: - name: ADMIN_USERS - valueFrom: - secretKeyRef: - key: user - name: grafana-admin-password + value: "admin" - name: ADMIN_PASSWORD valueFrom: secretKeyRef: - key: password - name: grafana-admin-password + name: alerta + key: admin-password - name: ADMIN_KEY valueFrom: secretKeyRef: @@ -279,10 +306,19 @@ spec: - name: alerta-config mountPath: /app/config.js subPath: config.js + - name: alerta-server-config + mountPath: /app/alertad.conf + subPath: alertad.conf volumes: - name: alerta-config configMap: name: alerta + - name: alerta-server-config + secret: + secretName: alerta + items: + - key: alertad.conf + path: alertad.conf {{- if ne $gatewayEnabled "true" }} --- apiVersion: networking.k8s.io/v1 diff --git a/packages/system/monitoring/tests/alerta_secret_key_test.yaml b/packages/system/monitoring/tests/alerta_secret_key_test.yaml new file mode 100644 index 000000000..5a26f1943 --- /dev/null +++ b/packages/system/monitoring/tests/alerta_secret_key_test.yaml @@ -0,0 +1,83 @@ +suite: alerta is given its own signing key and admin password, decoupled from Grafana +templates: + - templates/alerta/alerta.yaml + +release: + name: monitoring + namespace: tenant-root + +set: + _cluster: + issuer-name: letsencrypt-prod + solver: http01 + gateway-enabled: "false" + _namespace: + host: example.org + ingress: tenant-root + +tests: + - it: renders a 64-char SECRET_KEY for Alerta into a mounted alertad.conf + documentIndex: 0 + asserts: + - isKind: + of: Secret + - equal: + path: metadata.name + value: alerta + - matchRegex: + path: stringData["alertad.conf"] + pattern: 'SECRET_KEY = "[A-Za-z0-9]{64}"' + + - it: persists the signing key and admin password in the alerta Secret + documentIndex: 0 + asserts: + - equal: + path: metadata.name + value: alerta + - isNotNullOrEmpty: + path: data.secret-key + - isNotNullOrEmpty: + path: data.admin-password + + - it: mounts the server config so Alerta reads the generated key at runtime + documentSelector: + path: kind + value: Deployment + asserts: + - contains: + path: spec.template.spec.containers[0].volumeMounts + content: + name: alerta-server-config + mountPath: /app/alertad.conf + subPath: alertad.conf + - contains: + path: spec.template.spec.volumes + content: + name: alerta-server-config + secret: + secretName: alerta + items: + - key: alertad.conf + path: alertad.conf + + - it: gives Alerta its own admin password instead of reusing Grafana's + documentSelector: + path: kind + value: Deployment + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: ADMIN_PASSWORD + valueFrom: + secretKeyRef: + name: alerta + key: admin-password + - notContains: + path: spec.template.spec.containers[0].env + content: + name: ADMIN_PASSWORD + valueFrom: + secretKeyRef: + name: grafana-admin-password + key: password From 8942c7dfcc4b85be240a3a9e708d1dec8c4b6b4b Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Nov 2026 04:49:30 +0100 Subject: [PATCH 2/4] security(monitoring): keep Alerta credentials out of its container log The upstream Alerta entrypoint prints the whole environment, including the admin password and API key, and echoes the admin API keys it creates, while nginx logs the request line where Alertmanager passes the key as a query parameter. Pod logs are readable at lower tenant tiers than the credentials themselves. The respin drops the environment dump, silences key creation and masks the api-key parameter in the access log. Each edit is checked during the build, so an upstream change to either file fails the image build rather than bringing the output back. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- hack/alerta-redact-secrets.bats | 76 +++++++++++++++++++ packages/system/monitoring/Makefile | 2 +- .../monitoring/images/alerta/Containerfile | 6 ++ .../images/alerta/redact-secrets.sh | 60 +++++++++++++++ 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 hack/alerta-redact-secrets.bats create mode 100644 packages/system/monitoring/images/alerta/redact-secrets.sh diff --git a/hack/alerta-redact-secrets.bats b/hack/alerta-redact-secrets.bats new file mode 100644 index 000000000..e4a4123b2 --- /dev/null +++ b/hack/alerta-redact-secrets.bats @@ -0,0 +1,76 @@ +#!/usr/bin/env bats + +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" + +write_fixtures() { + cat >"${BATS_TEST_TMPDIR}/docker-entrypoint.sh" <<'EOF' +#!/bin/bash +set -e + +ADMIN_USER=${ADMIN_USERS%%,*} +ADMIN_PASSWORD=${ADMIN_PASSWORD:-alerta} + +env | sort + +if [ -n "${ADMIN_USERS}" ]; then + echo "# Create admin users." + alertad user --all --password "${ADMIN_PASSWORD}" || true + echo "# Create admin API keys." + alertad key --all + + if [ -n "${ADMIN_KEY}" ]; then + echo "# Create user-defined admin API key." + alertad key --username "${ADMIN_USER}" --key "${ADMIN_KEY}" --duration "${MAXAGE}" || true + fi +fi + +exec "$@" +EOF + chmod 0755 "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" + cat >"${BATS_TEST_TMPDIR}/nginx.conf.j2" <<'EOF' +http { + log_format main '$remote_addr - $remote_user [$time_local] ' + '"$request" $status $body_bytes_sent ' + '"$http_referer" "$http_user_agent" - $request_time'; + + server { + access_log /dev/stdout main; + } +} +EOF +} + +@test "the entrypoint no longer prints the environment or the admin API keys" { + write_fixtures + sh "${SCRIPT}" "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" "${BATS_TEST_TMPDIR}/nginx.conf.j2" + + assert_file_lacks_pattern '^env' "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" + grep -qx ' alertad key --all >/dev/null' "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" + grep -qx ' alertad key --username "${ADMIN_USER}" --key "${ADMIN_KEY}" --duration "${MAXAGE}" >/dev/null || true' \ + "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" + [ -x "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" ] +} + +@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" + + grep -qF '"$request_redacted" $status' "${BATS_TEST_TMPDIR}/nginx.conf.j2" + grep -qF 'map $request $request_redacted {' "${BATS_TEST_TMPDIR}/nginx.conf.j2" + grep -qF '"~*^(?.*[?&]api-key=)[^&\s]*(?.*)$" "${redact_pre}REDACTED${redact_post}";' \ + "${BATS_TEST_TMPDIR}/nginx.conf.j2" + assert_file_lacks_pattern '"[$]request"' "${BATS_TEST_TMPDIR}/nginx.conf.j2" +} + +@test "an upstream file that no longer matches fails the build instead of passing unedited" { + write_fixtures + sed 's/^env | sort$/printenv/' "${BATS_TEST_TMPDIR}/docker-entrypoint.sh" >"${BATS_TEST_TMPDIR}/changed.sh" + + run sh "${SCRIPT}" "${BATS_TEST_TMPDIR}/changed.sh" "${BATS_TEST_TMPDIR}/nginx.conf.j2" + [ "${status}" -ne 0 ] + [[ "${output}" == *"env dump not found"* ]] +} diff --git a/packages/system/monitoring/Makefile b/packages/system/monitoring/Makefile index fa5e1dfd6..86c32fee4 100644 --- a/packages/system/monitoring/Makefile +++ b/packages/system/monitoring/Makefile @@ -19,7 +19,7 @@ COMPONENT_VERSION = v$(OTELCOL_VERSION)-cozystack.$(COZYSTACK_REV) # Alerta is versioned by the upstream release it respins, pinned in the # Containerfile; bump ALERTA_COZYSTACK_REV for a respin on the same release. ALERTA_VERSION = $(shell awk -F= '/^ARG VERSION=/{print $$2}' images/alerta/Containerfile) -ALERTA_COZYSTACK_REV = 0 +ALERTA_COZYSTACK_REV = 1 ALERTA_COMPONENT_VERSION = v$(ALERTA_VERSION)-cozystack.$(ALERTA_COZYSTACK_REV) image: image-otelcol-tracing image-alerta diff --git a/packages/system/monitoring/images/alerta/Containerfile b/packages/system/monitoring/images/alerta/Containerfile index 0c4108b6d..96e28b068 100644 --- a/packages/system/monitoring/images/alerta/Containerfile +++ b/packages/system/monitoring/images/alerta/Containerfile @@ -22,6 +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 + # 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 # the layers; it is restated below exactly as the upstream image declares it, diff --git a/packages/system/monitoring/images/alerta/redact-secrets.sh b/packages/system/monitoring/images/alerta/redact-secrets.sh new file mode 100644 index 000000000..f8721f159 --- /dev/null +++ b/packages/system/monitoring/images/alerta/redact-secrets.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# Keeps Alerta's 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=. Every edit is checked, so an upstream change to either file fails +# the image build instead of silently bringing the output back. +# +# Usage: redact-secrets.sh +set -eu + +entrypoint=$1 +nginx_conf=$2 + +fail() { + echo "redact-secrets: $*" >&2 + exit 1 +} + +# sed -i differs between GNU and BSD; rewrite in place through a temporary +# file, keeping the target's mode. +edit() { + file=$1 + shift + sed "$@" "$file" >"$file.redact" + cat "$file.redact" >"$file" + rm -f "$file.redact" +} + +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 -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 '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/' + +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 -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" + +# The map rewrites the first api-key parameter; a request line carries one. +edit "$nginx_conf" \ + -e '/^ log_format main /i\ + map $request $request_redacted {\ + "~*^(?.*[?\&]api-key=)[^\&\\s]*(?.*)$" "${redact_pre}REDACTED${redact_post}";\ + default $request;\ + }\ +' \ + -e 's/"\$request"/"$request_redacted"/' + +grep -q '"\$request_redacted"' "$nginx_conf" || fail "log_format still logs the raw request line in $nginx_conf" From 68b0e80d1771c18065f97c88da992696b95c5a25 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Nov 2026 05:24:15 +0100 Subject: [PATCH 3/4] security(monitoring): show tenants Alerta's login apart from its keys Tenants logged in to the Alerta UI with the Grafana credentials, the only Secret of the release they are shown. With Alerta on a password of its own, that password moves to a Secret that holds nothing but the login, listed for tenants next to Grafana's, so the signing key and the API key in the alerta Secret stay hidden. The alerta Secret is now rendered under data alone. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- .../monitoring-rd/cozyrds/monitoring.yaml | 1 + .../monitoring/templates/alerta/alerta.yaml | 39 ++++++++++----- .../tests/alerta_secret_key_test.yaml | 47 +++++++++++++------ 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/packages/system/monitoring-rd/cozyrds/monitoring.yaml b/packages/system/monitoring-rd/cozyrds/monitoring.yaml index 2ef708196..134761f11 100644 --- a/packages/system/monitoring-rd/cozyrds/monitoring.yaml +++ b/packages/system/monitoring-rd/cozyrds/monitoring.yaml @@ -44,6 +44,7 @@ spec: include: - resourceNames: - grafana-admin-password + - alerta-admin-password services: exclude: [] include: diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 01e4cddc6..c7cab4270 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -130,18 +130,23 @@ Alerta's admin account must not share Grafana's credentials: Alerta exposes the whole process environment through /api/management/properties to any admin-scoped caller, so a shared password would hand Grafana's admin over - with it. Give Alerta its own password, persisted the same way. + with it. Its own login lives in a Secret apart from this one, so tenants can + be shown it (monitoring-rd lists it) without the signing key and API key. + The entrypoint resets the admin user's password to it on every start, which + also replaces the Grafana password on installations that predate this. */}} {{- $adminPassword := randAlphaNum 32 }} +{{- with lookup "v1" "Secret" .Release.Namespace "alerta-admin-password" }} +{{- with index .data "password" }} +{{- $adminPassword = b64dec . }} +{{- end }} +{{- end }} {{- $existingSecret := lookup "v1" "Secret" .Release.Namespace "alerta" }} {{- if $existingSecret }} {{- $apiKey = index $existingSecret.data "alerta-api-key" | b64dec }} {{- if index $existingSecret.data "secret-key" }} {{- $secretKey = index $existingSecret.data "secret-key" | b64dec }} {{- end }} -{{- if index $existingSecret.data "admin-password" }} -{{- $adminPassword = index $existingSecret.data "admin-password" | b64dec }} -{{- end }} {{- end }} --- apiVersion: v1 @@ -154,11 +159,18 @@ type: Opaque data: alerta-api-key: {{ $apiKey | b64enc }} secret-key: {{ $secretKey | b64enc }} - admin-password: {{ $adminPassword | b64enc }} -stringData: - alertad.conf: | - DEBUG = False - SECRET_KEY = {{ $secretKey | quote }} + alertad.conf: {{ printf "DEBUG = False\nSECRET_KEY = %s\n" ($secretKey | quote) | b64enc }} +--- +apiVersion: v1 +kind: Secret +metadata: + name: alerta-admin-password + labels: + app: alerta +type: Opaque +data: + user: {{ b64enc "admin" }} + password: {{ $adminPassword | b64enc }} --- apiVersion: v1 kind: ConfigMap @@ -219,12 +231,15 @@ spec: resources: {{- toYaml .Values.alerta.resources | nindent 12 }} env: - name: ADMIN_USERS - value: "admin" + valueFrom: + secretKeyRef: + name: alerta-admin-password + key: user - name: ADMIN_PASSWORD valueFrom: secretKeyRef: - name: alerta - key: admin-password + name: alerta-admin-password + key: password - name: ADMIN_KEY valueFrom: secretKeyRef: diff --git a/packages/system/monitoring/tests/alerta_secret_key_test.yaml b/packages/system/monitoring/tests/alerta_secret_key_test.yaml index 5a26f1943..bcb36ab27 100644 --- a/packages/system/monitoring/tests/alerta_secret_key_test.yaml +++ b/packages/system/monitoring/tests/alerta_secret_key_test.yaml @@ -1,4 +1,4 @@ -suite: alerta is given its own signing key and admin password, decoupled from Grafana +suite: alerta is given its own signing key and admin login, decoupled from Grafana templates: - templates/alerta/alerta.yaml @@ -21,24 +21,33 @@ tests: asserts: - isKind: of: Secret - - equal: - path: metadata.name - value: alerta - - matchRegex: - path: stringData["alertad.conf"] - pattern: 'SECRET_KEY = "[A-Za-z0-9]{64}"' - - - it: persists the signing key and admin password in the alerta Secret - documentIndex: 0 - asserts: - equal: path: metadata.name value: alerta - isNotNullOrEmpty: path: data.secret-key - - isNotNullOrEmpty: + - notExists: + path: stringData + - notExists: path: data.admin-password + - it: keeps Alerta's login in its own Secret, apart from the signing key + documentSelector: + path: metadata.name + value: alerta-admin-password + asserts: + - isKind: + of: Secret + - equal: + path: data.user + value: YWRtaW4= + - isNotNullOrEmpty: + path: data.password + - notExists: + path: data.secret-key + - notExists: + path: data.alerta-api-key + - it: mounts the server config so Alerta reads the generated key at runtime documentSelector: path: kind @@ -60,7 +69,7 @@ tests: - key: alertad.conf path: alertad.conf - - it: gives Alerta its own admin password instead of reusing Grafana's + - it: gives Alerta its own admin login instead of reusing Grafana's documentSelector: path: kind value: Deployment @@ -71,8 +80,16 @@ tests: name: ADMIN_PASSWORD valueFrom: secretKeyRef: - name: alerta - key: admin-password + name: alerta-admin-password + key: password + - contains: + path: spec.template.spec.containers[0].env + content: + name: ADMIN_USERS + valueFrom: + secretKeyRef: + name: alerta-admin-password + key: user - notContains: path: spec.template.spec.containers[0].env content: From bcba5d349a6cd2ae71fb4ec03e73dfddcb965c27 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Fri, 27 Nov 2026 05:24:16 +0100 Subject: [PATCH 4/4] 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}.')