Files

53 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Render the edge templates with the given variables and check the result:
# - nginx -t inside a throwaway nginx container, with a self-signed
# certificate at the path the config expects;
# - every nginx listen directive uses the configured address, never a
# wildcard;
# - both AmneziaWG templates render with well-formed throwaway keys.
#
# check.sh [ENV_FILE]
#
# DOCKER and EDGE_NGINX_IMAGE can be set in the environment. The image should
# follow the nginx version on the edge host (the Ubuntu package is 1.28).
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
env_file="${1:-$here/edge.env.example}"
docker_bin="${DOCKER:-docker}"
image="${EDGE_NGINX_IMAGE:-nginx:stable-alpine}"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
"$here/scripts/render.sh" --env "$env_file" --out "$work/out" nginx
"$here/scripts/render.sh" --env "$env_file" --out "$work/out-edge" awg-edge --check-secrets
"$here/scripts/render.sh" --env "$env_file" --out "$work/out-app" awg-app --check-secrets
listen_ip="$(sed -n 's/^EDGE_LISTEN_IP=//p' "$env_file")"
cert_name="$(sed -n 's/^EDGE_CERT_NAME=//p' "$env_file")"
bad="$(grep -hE '^[[:space:]]*listen[[:space:]]' "$work"/out/nginx/*.conf | grep -vF "listen ${listen_ip}:" || true)"
if [ -n "$bad" ]; then
printf 'edge-config: listen directives that are not bound to %s:\n%s\n' "$listen_ip" "$bad" >&2
exit 1
fi
mkdir -p "$work/cert"
openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=edge-config-check" \
-keyout "$work/cert/privkey.pem" -out "$work/cert/fullchain.pem" >/dev/null 2>&1
# nginx -t binds the listen sockets, so the container gets the edge address
# on its loopback (NET_ADMIN is needed only for that).
"$docker_bin" run --rm --entrypoint sh --cap-add NET_ADMIN \
-v "$work/out/nginx:/rendered:ro" -v "$work/cert:/cert:ro" \
-e "CERT_NAME=$cert_name" -e "LISTEN_IP=$listen_ip" "$image" -ec '
ip addr add "$LISTEN_IP/32" dev lo
rm -f /etc/nginx/conf.d/default.conf
cp /rendered/*.conf /etc/nginx/conf.d/
mkdir -p "/etc/letsencrypt/live/$CERT_NAME" /var/log/nginx /var/www/certbot
cp /cert/*.pem "/etc/letsencrypt/live/$CERT_NAME/"
nginx -v
nginx -t
'
printf 'edge-config: nginx -t passed; templates render with %s\n' "$env_file"