Merge commit from fork

[Backport release-1.6] security(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 45f3f74240
8 files changed
+512 -3

No files matched your search

@@ -17,6 +17,7 @@ rules:
- secrets
resourceNames:
- grafana-admin-password
- alerta-admin-password
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
@@ -44,6 +44,7 @@ spec:
include:
- resourceNames:
- grafana-admin-password
- alerta-admin-password
services:
exclude: []
include:
@@ -0,0 +1,109 @@
#!/bin/bash
# docker-entrypoint.sh of docker.io/alerta/alerta-web:9.1.0
# (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), 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')"
ALERTA_CONF_FILE=${ALERTA_CONF_FILE:-/app/alerta.conf}
ALERTA_SVR_CONF_FILE=${ALERTA_SVR_CONF_FILE:-/app/alertad.conf}
ALERTA_WEB_CONF_FILE=${ALERTA_WEB_CONF_FILE:-/web/config.json}
NGINX_CONF_FILE=/app/nginx.conf
UWSGI_CONF_FILE=/app/uwsgi.ini
SUPERVISORD_CONF_FILE=/app/supervisord.conf
ADMIN_USER=${ADMIN_USERS%%,*}
ADMIN_PASSWORD=${ADMIN_PASSWORD:-alerta}
MAXAGE=${ADMIN_KEY_MAXAGE:-315360000} # default=10 years
# Generate minimal server config, if not supplied
if [ ! -f "${ALERTA_SVR_CONF_FILE}" ]; then
echo "# Create server configuration file."
export SECRET_KEY=${SECRET_KEY:-$(< /dev/urandom tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= | head -c 32)}
python3 -c "${JINJA2}" < ${ALERTA_SVR_CONF_FILE}.j2 >${ALERTA_SVR_CONF_FILE}
unset SECRET_KEY
fi
# Init admin users and API keys
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
# Create user-defined API key, if required
if [ -n "${ADMIN_KEY}" ]; then
echo "# Create user-defined admin API key."
alertad key --username "${ADMIN_USER}" --key "${ADMIN_KEY}" --duration "${MAXAGE}" >/dev/null || true
fi
fi
# Generate minimal client config, if not supplied
if [ ! -f "${ALERTA_CONF_FILE}" ]; then
# Add API key to client config, if required
if [ "${AUTH_REQUIRED,,}" == "true" ]; then
echo "# Auth enabled; add admin API key to client configuration."
HOUSEKEEPING_SCOPES="--scope read --scope write:alerts --scope admin:management"
if grep -qE 'CUSTOMER_VIEWS.*=.*True' ${ALERTA_SVR_CONF_FILE};then
HOUSEKEEPING_SCOPES="--scope admin:alerts ${HOUSEKEEPING_SCOPES}"
fi
export API_KEY=$(alertad key \
--username "${ADMIN_USER}" \
${HOUSEKEEPING_SCOPES} \
--duration "${MAXAGE}" \
--text "Housekeeping")
fi
echo "# Create client configuration file."
python3 -c "${JINJA2}" < ${ALERTA_CONF_FILE}.j2 >${ALERTA_CONF_FILE}
fi
# Generate supervisord config, if not supplied
if [ ! -f "${SUPERVISORD_CONF_FILE}" ]; then
echo "# Create supervisord configuration file."
python3 -c "${JINJA2}" < ${SUPERVISORD_CONF_FILE}.j2 >${SUPERVISORD_CONF_FILE}
fi
# Generate nginx config, if not supplied.
if [ ! -f "${NGINX_CONF_FILE}" ]; then
echo "# Create nginx configuration file."
python3 -c "${JINJA2}" < ${NGINX_CONF_FILE}.j2 >${NGINX_CONF_FILE}
fi
nginx -t -c ${NGINX_CONF_FILE}
# Generate uWSGI config, if not supplied.
if [ ! -f "${UWSGI_CONF_FILE}" ]; then
echo "# Create uWSGI configuration file."
python3 -c "${JINJA2}" < ${UWSGI_CONF_FILE}.j2 >${UWSGI_CONF_FILE}
fi
# Generate web config, if not supplied.
if [ ! -f "${ALERTA_WEB_CONF_FILE}" ]; then
echo "# Create web configuration file."
python3 -c "${JINJA2}" < ${ALERTA_WEB_CONF_FILE}.j2 >${ALERTA_WEB_CONF_FILE}
fi
echo
echo '# Checking versions.'
echo Alerta Server ${SERVER_VERSION}
echo Alerta Client ${CLIENT_VERSION}
echo Alerta WebUI ${WEBUI_VERSION}
nginx -v
echo uwsgi $(uwsgi --version)
mongosh --version
psql --version
python3 --version
/venv/bin/pip list
echo
echo 'Alerta init process complete; ready for start up.'
echo
exec "$@"
@@ -0,0 +1,77 @@
{#- nginx.conf.j2 of docker.io/alerta/alerta-web:9.1.0
(sha256:693845fb8a95a483c8c9d560786f661f5b6198e5ea0baa6ddf368a7e53c94f39),
Apache-2.0, mounted over the image's copy by templates/alerta/alerta.yaml.
Changed from upstream only so that the access log masks the api-key query
parameter Alertmanager and the Telegram webhook authenticate with. #}
worker_processes {{ env.NGINX_WORKER_PROCESSES }};
pid /tmp/nginx.pid;
daemon off;
{%- if env.DEBUG %}
error_log /dev/stderr debug;
{%- else %}
error_log /dev/stderr {{ env.LOG_LEVEL|lower or 'warn' }};
{%- endif %}
events {
worker_connections {{ env.NGINX_WORKER_CONNECTIONS }};
}
http {
client_body_temp_path /tmp/client_body;
fastcgi_temp_path /tmp/fastcgi_temp;
proxy_temp_path /tmp/proxy_temp;
scgi_temp_path /tmp/scgi_temp;
uwsgi_temp_path /tmp/uwsgi_temp;
include /etc/nginx/mime.types;
server_tokens off;
gzip on;
gzip_disable "msie6";
client_max_body_size {{ env.NGINX_MAX_BODY_SIZE or '2M' }};
map $request $request_redacted {
"~*^(?<redact_pre>.*[?&]api-key=)[^&\s]*(?<redact_post>.*)$" "${redact_pre}REDACTED${redact_post}";
default $request;
}
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request_redacted" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" - $request_time '
'x_forwarded_for=$http_x_forwarded_for host=$host '
'$request_id $sent_http_x_request_id';
default_type application/octet-stream;
upstream backend {
server 127.0.0.1:29000;
}
server {
listen 8080 default_server;
{%- if env.ENABLE_IPV6 is defined and env.ENABLE_IPV6|lower == 'true' %}
listen [::]:8080 default_server;
{%- endif %}
access_log /dev/stdout main;
location /api {
include /etc/nginx/uwsgi_params;
uwsgi_pass backend;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}
root /web;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
}
@@ -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}.')
@@ -104,9 +104,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
@@ -118,6 +147,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
@@ -136,6 +178,31 @@ data:
})
.constant('colors', {});
---
{{- /*
The image's entrypoint prints the whole environment and the admin API keys
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. 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
metadata:
name: alerta-entrypoint
labels:
app: alerta
data:
docker-entrypoint.sh: |-
{{ .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
metadata:
@@ -173,20 +240,22 @@ spec:
spec:
containers:
- name: alerta
image: "alerta/alerta-web:9.1.0"
{{- /* Pinned by digest: the entrypoint and nginx template mounted
from alerta-entrypoint are copies of this exact image's. */}}
image: "docker.io/alerta/alerta-web:9.1.0@sha256:693845fb8a95a483c8c9d560786f661f5b6198e5ea0baa6ddf368a7e53c94f39"
imagePullPolicy: IfNotPresent
resources: {{- toYaml .Values.alerta.resources | nindent 12 }}
env:
- 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:
@@ -268,10 +337,32 @@ spec:
- name: alerta-config
mountPath: /app/config.js
subPath: config.js
- name: alerta-server-config
mountPath: /app/alertad.conf
subPath: alertad.conf
- name: alerta-entrypoint
mountPath: /usr/local/bin/docker-entrypoint.sh
subPath: docker-entrypoint.sh
- 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:
name: alerta
- name: alerta-server-config
secret:
secretName: alerta
items:
- key: alertad.conf
path: alertad.conf
- name: alerta-entrypoint
configMap:
name: alerta-entrypoint
defaultMode: 0755
{{- if ne $gatewayEnabled "true" }}
---
apiVersion: networking.k8s.io/v1
@@ -0,0 +1,101 @@
suite: alerta keeps its credentials out of the container log
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: ships an entrypoint without the environment dump and with key creation silenced
documentSelector:
path: metadata.name
value: alerta-entrypoint
asserts:
- isKind:
of: ConfigMap
- notMatchRegex:
path: data["docker-entrypoint.sh"]
pattern: '(?m)^env'
- matchRegex:
path: data["docker-entrypoint.sh"]
pattern: '(?m)^ alertad key --all >/dev/null$'
- matchRegex:
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
value: alerta-entrypoint
asserts:
- matchRegex:
path: data["nginx.conf.j2"]
pattern: 'map \$request \$request_redacted'
- matchRegex:
path: data["nginx.conf.j2"]
pattern: '"\$request_redacted" \$status'
- notMatchRegex:
path: data["nginx.conf.j2"]
pattern: '"\$request" '
- it: mounts both files over the image's copies
documentSelector:
path: kind
value: Deployment
asserts:
- contains:
path: spec.template.spec.containers[0].volumeMounts
content:
name: alerta-entrypoint
mountPath: /usr/local/bin/docker-entrypoint.sh
subPath: docker-entrypoint.sh
- contains:
path: spec.template.spec.containers[0].volumeMounts
content:
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:
name: alerta-entrypoint
configMap:
name: alerta-entrypoint
defaultMode: 0755
- it: runs the exact image the mounted files were copied from
documentSelector:
path: kind
value: Deployment
asserts:
- equal:
path: spec.template.spec.containers[0].image
value: docker.io/alerta/alerta-web:9.1.0@sha256:693845fb8a95a483c8c9d560786f661f5b6198e5ea0baa6ddf368a7e53c94f39
@@ -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