diff --git a/packages/system/monitoring/files/alerta/docker-entrypoint.sh b/packages/system/monitoring/files/alerta/docker-entrypoint.sh new file mode 100755 index 000000000..3401907b9 --- /dev/null +++ b/packages/system/monitoring/files/alerta/docker-entrypoint.sh @@ -0,0 +1,107 @@ +#!/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. +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 + 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 "$@" diff --git a/packages/system/monitoring/files/alerta/nginx.conf.j2 b/packages/system/monitoring/files/alerta/nginx.conf.j2 new file mode 100644 index 000000000..5661484ed --- /dev/null +++ b/packages/system/monitoring/files/alerta/nginx.conf.j2 @@ -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 { + "~*^(?.*[?&]api-key=)[^&\s]*(?.*)$" "${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; + } + } +} diff --git a/packages/system/monitoring/templates/alerta/alerta.yaml b/packages/system/monitoring/templates/alerta/alerta.yaml index 960cf87f7..0e368c10a 100644 --- a/packages/system/monitoring/templates/alerta/alerta.yaml +++ b/packages/system/monitoring/templates/alerta/alerta.yaml @@ -166,6 +166,25 @@ 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; 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 }} +--- apiVersion: v1 kind: Service metadata: @@ -298,6 +317,12 @@ spec: - 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 volumes: - name: alerta-config configMap: @@ -308,6 +333,10 @@ spec: 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 diff --git a/packages/system/monitoring/tests/alerta_log_redaction_test.yaml b/packages/system/monitoring/tests/alerta_log_redaction_test.yaml new file mode 100644 index 000000000..744f85d50 --- /dev/null +++ b/packages/system/monitoring/tests/alerta_log_redaction_test.yaml @@ -0,0 +1,74 @@ +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: 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.volumes + content: + name: alerta-entrypoint + configMap: + name: alerta-entrypoint + defaultMode: 0755