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 mounted entrypoint 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 <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil committed 2026-11-27 05:40:32 +01:00
1 parent cbdb4f64a5
commit 51acf0fcbb
4 files changed
+61 -3

No files matched your search

@@ -3,8 +3,9 @@
# (sha256:693845fb8a95a483c8c9d560786f661f5b6198e5ea0baa6ddf368a7e53c94f39),
# Apache-2.0, mounted over the image's copy by templates/alerta/alerta.yaml.
# Changed from upstream only so that Alerta's credentials stay out of the
# container log: the environment dump is removed and the output of the two
# `alertad key` calls is discarded.
# container log (the environment dump is removed and the output of the two
# `alertad key` calls is discarded), and so that existing admin users take
# ADMIN_PASSWORD on every start through sync-admin-password.py.
set -e
JINJA2="import os, sys, jinja2; sys.stdout.write(jinja2.Template(sys.stdin.read()).render(env=os.environ)+'\n')"
@@ -33,6 +34,7 @@ fi
if [ -n "${ADMIN_USERS}" ]; then
echo "# Create admin users."
alertad user --all --password "${ADMIN_PASSWORD}" || true
/venv/bin/python /app/sync-admin-password.py
echo "# Create admin API keys."
alertad key --all >/dev/null
@@ -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}.')
@@ -183,7 +183,11 @@ data:
it creates, and its nginx logs the api-key query parameter, so Alerta's
credentials end up in the container log, which is readable at lower tenant
tiers than the credentials themselves. Both files are mounted over the
image's copies with that output removed; see the headers in files/alerta/.
image's copies with that output removed. The entrypoint also runs
sync-admin-password.py after creating admin users, because it only creates
missing ones and an existing admin user would keep the password it was
created with, Grafana's on installations that predate Alerta's own. See the
headers in files/alerta/.
*/}}
apiVersion: v1
kind: ConfigMap
@@ -196,6 +200,8 @@ data:
{{ .Files.Get "files/alerta/docker-entrypoint.sh" | trimSuffix "\n" | indent 4 }}
nginx.conf.j2: |-
{{ .Files.Get "files/alerta/nginx.conf.j2" | trimSuffix "\n" | indent 4 }}
sync-admin-password.py: |-
{{ .Files.Get "files/alerta/sync-admin-password.py" | trimSuffix "\n" | indent 4 }}
---
apiVersion: v1
kind: Service
@@ -338,6 +344,9 @@ spec:
- name: alerta-entrypoint
mountPath: /app/nginx.conf.j2
subPath: nginx.conf.j2
- name: alerta-entrypoint
mountPath: /app/sync-admin-password.py
subPath: sync-admin-password.py
volumes:
- name: alerta-config
configMap:
@@ -33,6 +33,18 @@ tests:
path: data["docker-entrypoint.sh"]
pattern: '(?m)^ alertad key --username "\$\{ADMIN_USER\}" --key "\$\{ADMIN_KEY\}" --duration "\$\{MAXAGE\}" >/dev/null \|\| true$'
- it: resets the admin users' password to ADMIN_PASSWORD right after creating them
documentSelector:
path: metadata.name
value: alerta-entrypoint
asserts:
- matchRegex:
path: data["docker-entrypoint.sh"]
pattern: '(?m)^ alertad user --all --password "\$\{ADMIN_PASSWORD\}" \|\| true\n /venv/bin/python /app/sync-admin-password\.py$'
- matchRegex:
path: data["sync-admin-password.py"]
pattern: 'user\.update\(password=password\)'
- it: ships an nginx config that masks the api-key parameter in the access log
documentSelector:
path: metadata.name
@@ -65,6 +77,12 @@ tests:
name: alerta-entrypoint
mountPath: /app/nginx.conf.j2
subPath: nginx.conf.j2
- contains:
path: spec.template.spec.containers[0].volumeMounts
content:
name: alerta-entrypoint
mountPath: /app/sync-admin-password.py
subPath: sync-admin-password.py
- contains:
path: spec.template.spec.volumes
content: