diff --git a/packages/system/monitoring/files/alerta/docker-entrypoint.sh b/packages/system/monitoring/files/alerta/docker-entrypoint.sh index 3401907b9..d5656d2ee 100755 --- a/packages/system/monitoring/files/alerta/docker-entrypoint.sh +++ b/packages/system/monitoring/files/alerta/docker-entrypoint.sh @@ -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 diff --git a/packages/system/monitoring/files/alerta/sync-admin-password.py b/packages/system/monitoring/files/alerta/sync-admin-password.py new file mode 100644 index 000000000..21ce71258 --- /dev/null +++ b/packages/system/monitoring/files/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}.') diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 5b4b248c3..dfd2b42ec 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -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: diff --git a/packages/system/monitoring/tests/alerta_log_redaction_test.yaml b/packages/system/monitoring/tests/alerta_log_redaction_test.yaml index 744f85d50..fa2a3740b 100644 --- a/packages/system/monitoring/tests/alerta_log_redaction_test.yaml +++ b/packages/system/monitoring/tests/alerta_log_redaction_test.yaml @@ -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: