154 lines
6.3 KiB
Bash
Executable File
154 lines
6.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Render the edge templates from configuration.
|
|
#
|
|
# render.sh [--env FILE] [--out DIR] [--params FILE | --check-secrets] TARGET...
|
|
#
|
|
# TARGET is one or more of: nginx, awg-edge, awg-app.
|
|
#
|
|
# --env FILE variables (default: deploy/edge/edge.env)
|
|
# --out DIR output directory (default: deploy/edge/rendered)
|
|
# --params FILE tunnel keys and obfuscation parameters, written by
|
|
# awg-keys.sh (needed for awg-edge and awg-app)
|
|
# --check-secrets use well-formed throwaway key values instead of a
|
|
# params file; only for template checks, never for hosts
|
|
#
|
|
# Placeholders are written {{NAME}} so they cannot collide with nginx
|
|
# variables ($host) or with shell syntax. Every value is validated before it
|
|
# is substituted, and the script fails when a placeholder is left over.
|
|
set -euo pipefail
|
|
umask 077
|
|
|
|
here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
env_file="$here/edge.env"
|
|
out_dir="$here/rendered"
|
|
params_file=""
|
|
fake_secrets=0
|
|
targets=()
|
|
|
|
die() { printf 'render.sh: %s\n' "$*" >&2; exit 2; }
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--env) [ $# -ge 2 ] || die "--env needs a value"; env_file="$2"; shift 2 ;;
|
|
--out) [ $# -ge 2 ] || die "--out needs a value"; out_dir="$2"; shift 2 ;;
|
|
--params) [ $# -ge 2 ] || die "--params needs a value"; params_file="$2"; shift 2 ;;
|
|
--check-secrets) fake_secrets=1; shift ;;
|
|
nginx|awg-edge|awg-app) targets+=("$1"); shift ;;
|
|
*) die "unknown argument: $1" ;;
|
|
esac
|
|
done
|
|
[ ${#targets[@]} -gt 0 ] || die "no target given (nginx, awg-edge, awg-app)"
|
|
[ -f "$env_file" ] || die "missing $env_file (copy deploy/edge/edge.env.example)"
|
|
|
|
# Read KEY=VALUE lines without executing the file.
|
|
load_vars() {
|
|
local file="$1" line key value
|
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
case "$line" in ''|'#'*) continue ;; esac
|
|
case "$line" in *=*) ;; *) die "$file: not a KEY=VALUE line: $line" ;; esac
|
|
key="${line%%=*}"
|
|
value="${line#*=}"
|
|
# Only the project's own names, so a file cannot override PATH or IFS.
|
|
case "$key" in EDGE_*|AWG_*|APP_HTTP_PORT) ;; *) die "$file: unexpected variable name: $key" ;; esac
|
|
case "$key" in *[!A-Za-z0-9_]*) die "$file: bad variable name: $key" ;; esac
|
|
printf -v "$key" '%s' "$value"
|
|
done < "$file"
|
|
}
|
|
|
|
need() {
|
|
local name="$1" pattern="$2"
|
|
[ -n "${!name:-}" ] || die "$name is not set"
|
|
printf '%s' "${!name}" | grep -Eq "$pattern" || die "$name has an invalid value"
|
|
}
|
|
|
|
ipv4='^((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])$'
|
|
uint='^[0-9]{1,10}$'
|
|
b64key='^[A-Za-z0-9+/]{43}=$'
|
|
|
|
load_vars "$env_file"
|
|
need EDGE_DOMAIN '^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$'
|
|
need EDGE_CERT_NAME '^[A-Za-z0-9._-]+$'
|
|
need EDGE_LISTEN_IP "$ipv4"
|
|
[ "$EDGE_LISTEN_IP" != "0.0.0.0" ] || die "EDGE_LISTEN_IP must be one address, not 0.0.0.0"
|
|
need EDGE_ACME_WEBROOT '^/[A-Za-z0-9._/-]+$'
|
|
need EDGE_HSTS_MAX_AGE "$uint"
|
|
need EDGE_MAX_BODY '^[0-9]+[kKmM]$'
|
|
need AWG_IFACE '^[A-Za-z0-9_.+=-]{1,15}$'
|
|
need AWG_EDGE_IP "$ipv4"
|
|
need AWG_APP_IP "$ipv4"
|
|
need AWG_PREFIX '^(2[89]|3[01])$'
|
|
need AWG_EDGE_ENDPOINT_IP "$ipv4"
|
|
need AWG_PORT '^[0-9]{4,5}$'
|
|
[ "$AWG_PORT" -ge 1024 ] && [ "$AWG_PORT" -le 65535 ] || die "AWG_PORT is out of range"
|
|
[ "$AWG_PORT" != "443" ] || die "AWG_PORT must not be 443"
|
|
need AWG_MTU '^[0-9]{3,4}$'
|
|
need AWG_KEEPALIVE '^[0-9]{1,3}$'
|
|
need APP_HTTP_PORT '^[0-9]{2,5}$'
|
|
[ "$AWG_EDGE_IP" != "$AWG_APP_IP" ] || die "AWG_EDGE_IP and AWG_APP_IP must differ"
|
|
|
|
EDGE_UPSTREAM="${AWG_APP_IP}:${APP_HTTP_PORT}"
|
|
|
|
# Fill every {{NAME}} for the listed variables; fail on leftovers.
|
|
render() {
|
|
local src="$1" dst="$2"; shift 2
|
|
local script="" name value
|
|
for name in "$@"; do
|
|
value="${!name}"
|
|
script="${script}s|{{${name}}}|${value}|g;"
|
|
done
|
|
sed -e "$script" "$src" > "$dst"
|
|
if grep -q '{{[A-Z0-9_]*}}' "$dst"; then
|
|
rm -f "$dst"
|
|
die "$src has placeholders without a value: $(sed -e "$script" "$src" | grep -o '{{[A-Z0-9_]*}}' | sort -u | tr '\n' ' ')"
|
|
fi
|
|
}
|
|
|
|
load_awg_params() {
|
|
if [ "$fake_secrets" -eq 1 ]; then
|
|
AWG_PRIVATE_KEY="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
|
AWG_PEER_PUBLIC_KEY="BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB="
|
|
AWG_JC=4 AWG_JMIN=50 AWG_JMAX=1000 AWG_S1=30 AWG_S2=60
|
|
AWG_H1=1000000001 AWG_H2=1000000002 AWG_H3=1000000003 AWG_H4=1000000004
|
|
else
|
|
[ -n "$params_file" ] || die "awg targets need --params FILE (or --check-secrets for a template check)"
|
|
[ -f "$params_file" ] || die "missing $params_file (run awg-keys.sh)"
|
|
load_vars "$params_file"
|
|
fi
|
|
need AWG_PRIVATE_KEY "$b64key"
|
|
need AWG_PEER_PUBLIC_KEY "$b64key"
|
|
need AWG_JC '^[0-9]{1,3}$'
|
|
need AWG_JMIN '^[0-9]{1,4}$'
|
|
need AWG_JMAX '^[0-9]{1,4}$'
|
|
need AWG_S1 '^[0-9]{1,4}$'
|
|
need AWG_S2 '^[0-9]{1,4}$'
|
|
local n
|
|
for n in AWG_H1 AWG_H2 AWG_H3 AWG_H4; do need "$n" "$uint"; [ "${!n}" -ge 5 ] && [ "${!n}" -le 4294967295 ] || die "$n is out of range"; done
|
|
[ "$AWG_JC" -ge 1 ] && [ "$AWG_JC" -le 128 ] || die "AWG_JC is out of range"
|
|
[ "$AWG_JMIN" -lt "$AWG_JMAX" ] && [ "$AWG_JMAX" -le 1280 ] || die "need AWG_JMIN < AWG_JMAX <= 1280"
|
|
[ "$AWG_S1" -le 1132 ] && [ "$AWG_S2" -le 1188 ] && [ $((AWG_S1 + 56)) -ne "$AWG_S2" ] || die "AWG_S1 and AWG_S2 are not valid together"
|
|
[ "$(printf '%s\n' "$AWG_H1" "$AWG_H2" "$AWG_H3" "$AWG_H4" | sort -u | wc -l | tr -d ' ')" -eq 4 ] || die "AWG_H1..H4 must be distinct"
|
|
}
|
|
|
|
mkdir -p "$out_dir"
|
|
for target in "${targets[@]}"; do
|
|
case "$target" in
|
|
nginx)
|
|
mkdir -p "$out_dir/nginx"
|
|
for name in 00-default lincodex-http lincodex-https; do
|
|
render "$here/nginx/$name.conf.template" "$out_dir/nginx/$name.conf" \
|
|
EDGE_DOMAIN EDGE_CERT_NAME EDGE_LISTEN_IP EDGE_ACME_WEBROOT EDGE_HSTS_MAX_AGE EDGE_MAX_BODY EDGE_UPSTREAM
|
|
done
|
|
printf 'rendered nginx -> %s/nginx\n' "$out_dir"
|
|
;;
|
|
awg-edge|awg-app)
|
|
load_awg_params
|
|
mkdir -p "$out_dir/awg"
|
|
render "$here/awg/awg-lincodex.${target#awg-}.conf.template" "$out_dir/awg/$AWG_IFACE.conf" \
|
|
AWG_IFACE AWG_EDGE_IP AWG_APP_IP AWG_PREFIX AWG_EDGE_ENDPOINT_IP AWG_PORT AWG_MTU AWG_KEEPALIVE \
|
|
AWG_PRIVATE_KEY AWG_PEER_PUBLIC_KEY AWG_JC AWG_JMIN AWG_JMAX AWG_S1 AWG_S2 AWG_H1 AWG_H2 AWG_H3 AWG_H4
|
|
chmod 600 "$out_dir/awg/$AWG_IFACE.conf"
|
|
printf 'rendered %s -> %s/awg/%s.conf\n' "$target" "$out_dir" "$AWG_IFACE"
|
|
;;
|
|
esac
|
|
done
|