Merge commit from fork

fix(monitoring): give Alerta a real signing key and its own admin password
This commit is contained in:
myasnikovdaniil authored and GitHub committed 2026-11-27 14:47:20 +05:00
commit fc2da5810f
8 files changed
+348 -3

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
#!/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/patch-upstream.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 "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"
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 '"~*^(?<redact_pre>.*[?&]api-key=)[^&\s]*(?<redact_post>.*)$" "${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"* ]]
}
+1
View File
@@ -44,6 +44,7 @@ spec:
include:
- resourceNames:
- grafana-admin-password
- alerta-admin-password
services:
exclude: []
include:
+1 -1
View File
@@ -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
@@ -22,6 +22,13 @@ RUN apt-get update \
&& /venv/bin/pip check \
&& pip install --no-cache-dir --upgrade virtualenv 'filelock>=4.2.1'
# 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
# the layers; it is restated below exactly as the upstream image declares it,
@@ -0,0 +1,72 @@
#!/bin/sh
# Adapts the upstream Alerta entrypoint and nginx template.
#
# 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=.
#
# 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 <docker-entrypoint.sh> <nginx.conf.j2>
set -eu
entrypoint=$1
nginx_conf=$2
fail() {
echo "patch-upstream: $*" >&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 -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/'
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"
# 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 {\
"~*^(?<redact_pre>.*[?\&]api-key=)[^\&\\s]*(?<redact_post>.*)$" "${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"
@@ -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}.')
@@ -115,9 +115,38 @@
{{- 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. 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 }}
{{- end }}
---
apiVersion: v1
@@ -129,6 +158,19 @@ metadata:
type: Opaque
data:
alerta-api-key: {{ $apiKey | b64enc }}
secret-key: {{ $secretKey | b64enc }}
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
@@ -191,13 +233,13 @@ spec:
- name: ADMIN_USERS
valueFrom:
secretKeyRef:
name: alerta-admin-password
key: user
name: grafana-admin-password
- name: ADMIN_PASSWORD
valueFrom:
secretKeyRef:
name: alerta-admin-password
key: password
name: grafana-admin-password
- name: ADMIN_KEY
valueFrom:
secretKeyRef:
@@ -279,10 +321,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
@@ -0,0 +1,100 @@
suite: alerta is given its own signing key and admin login, 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
- isNotNullOrEmpty:
path: data.secret-key
- 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
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 login 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-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:
name: ADMIN_PASSWORD
valueFrom:
secretKeyRef:
name: grafana-admin-password
key: password