feat(deploy): add edge proxy HTTPS and AmneziaWG tunnel templates
This commit is contained in:
1 parent
bdc5855001
commit
b8c3c83ed8
21 files changed
+954
-3
No files matched your search
Executable
+121
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env bash
|
||||
# Generate and exchange AmneziaWG keys and obfuscation parameters.
|
||||
#
|
||||
# awg-keys.sh init PARAMS [--shared EXPORTED] create PARAMS on this host
|
||||
# awg-keys.sh export PARAMS print the shareable part
|
||||
# awg-keys.sh peer PARAMS EXPORTED store the other end's key
|
||||
# awg-keys.sh public PARAMS print this host's public key
|
||||
#
|
||||
# PARAMS is a root-only KEY=VALUE file (mode 600), for example
|
||||
# /etc/amnezia/amneziawg/awg-lincodex.params. It holds the private key and is
|
||||
# read by render.sh. Run this script as root on the host that owns the key;
|
||||
# the private key is never printed and never leaves PARAMS.
|
||||
#
|
||||
# Flow:
|
||||
# edge: awg-keys.sh init PARAMS (keys + parameters)
|
||||
# edge: awg-keys.sh export PARAMS > shared.env (public data only)
|
||||
# app: awg-keys.sh init PARAMS --shared shared.env (own key, same parameters)
|
||||
# app: awg-keys.sh export PARAMS > app-shared.env
|
||||
# edge: awg-keys.sh peer PARAMS app-shared.env
|
||||
# EXPORTED files carry a public key and obfuscation parameters. They are not
|
||||
# public data: the parameters are shared secrets of the tunnel, so move them
|
||||
# over SSH and delete the copies afterwards.
|
||||
set -euo pipefail
|
||||
umask 077
|
||||
|
||||
die() { printf 'awg-keys.sh: %s\n' "$*" >&2; exit 2; }
|
||||
command -v awg >/dev/null || die "awg (amneziawg-tools) is not installed"
|
||||
|
||||
b64key='^[A-Za-z0-9+/]{43}=$'
|
||||
shared_names="AWG_JC AWG_JMIN AWG_JMAX AWG_S1 AWG_S2 AWG_H1 AWG_H2 AWG_H3 AWG_H4"
|
||||
|
||||
# Print one value from a KEY=VALUE file.
|
||||
get() { sed -n "s/^$2=//p" "$1" | head -n 1; }
|
||||
|
||||
# Uniform-enough integer in [lo, hi] from the kernel random source.
|
||||
rand_range() {
|
||||
local lo="$1" hi="$2" n
|
||||
n="$(od -An -N4 -tu4 /dev/urandom | tr -d ' ')"
|
||||
printf '%s' $((lo + n % (hi - lo + 1)))
|
||||
}
|
||||
|
||||
cmd="${1:-}"; [ $# -ge 1 ] && shift
|
||||
case "$cmd" in
|
||||
init)
|
||||
[ $# -ge 1 ] || die "usage: init PARAMS [--shared EXPORTED]"
|
||||
params="$1"; shift
|
||||
shared=""
|
||||
if [ "${1:-}" = "--shared" ]; then [ $# -eq 2 ] || die "--shared needs a file"; shared="$2"; fi
|
||||
[ ! -e "$params" ] || die "$params exists; refusing to overwrite keys"
|
||||
[ "$(id -u)" -eq 0 ] || die "run as root"
|
||||
priv="$(awg genkey)"
|
||||
pub="$(printf '%s' "$priv" | awg pubkey)"
|
||||
tmp="$(mktemp "$params.XXXXXX")"
|
||||
trap 'rm -f "$tmp"' EXIT
|
||||
{
|
||||
printf 'AWG_PRIVATE_KEY=%s\n' "$priv"
|
||||
printf 'AWG_PUBLIC_KEY=%s\n' "$pub"
|
||||
if [ -n "$shared" ]; then
|
||||
[ -f "$shared" ] || die "missing $shared"
|
||||
peer="$(get "$shared" AWG_PEER_PUBLIC_KEY)"
|
||||
printf '%s' "$peer" | grep -Eq "$b64key" || die "$shared has no valid AWG_PEER_PUBLIC_KEY"
|
||||
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$peer"
|
||||
for name in $shared_names; do
|
||||
value="$(get "$shared" "$name")"
|
||||
printf '%s' "$value" | grep -Eq '^[0-9]+$' || die "$shared has no valid $name"
|
||||
printf '%s=%s\n' "$name" "$value"
|
||||
done
|
||||
else
|
||||
jmin="$(rand_range 40 80)"
|
||||
jmax="$(rand_range 700 1000)"
|
||||
s1="$(rand_range 15 150)"
|
||||
s2="$(rand_range 15 150)"
|
||||
while [ $((s1 + 56)) -eq "$s2" ]; do s2="$(rand_range 15 150)"; done
|
||||
printf 'AWG_JC=%s\nAWG_JMIN=%s\nAWG_JMAX=%s\nAWG_S1=%s\nAWG_S2=%s\n' "$(rand_range 4 8)" "$jmin" "$jmax" "$s1" "$s2"
|
||||
seen=" "
|
||||
for i in 1 2 3 4; do
|
||||
h="$(rand_range 5 4294967295)"
|
||||
while case "$seen" in *" $h "*) true ;; *) false ;; esac; do h="$(rand_range 5 4294967295)"; done
|
||||
seen="$seen$h "
|
||||
printf 'AWG_H%s=%s\n' "$i" "$h"
|
||||
done
|
||||
fi
|
||||
} > "$tmp"
|
||||
chmod 600 "$tmp"
|
||||
mv "$tmp" "$params"
|
||||
trap - EXIT
|
||||
printf 'created %s; this host public key: %s\n' "$params" "$pub"
|
||||
;;
|
||||
export)
|
||||
[ $# -eq 1 ] || die "usage: export PARAMS"
|
||||
[ -f "$1" ] || die "missing $1"
|
||||
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$(get "$1" AWG_PUBLIC_KEY)"
|
||||
for name in $shared_names; do printf '%s=%s\n' "$name" "$(get "$1" "$name")"; done
|
||||
;;
|
||||
peer)
|
||||
[ $# -eq 2 ] || die "usage: peer PARAMS EXPORTED"
|
||||
params="$1"; exported="$2"
|
||||
[ -f "$params" ] || die "missing $params"
|
||||
[ -f "$exported" ] || die "missing $exported"
|
||||
peer="$(get "$exported" AWG_PEER_PUBLIC_KEY)"
|
||||
printf '%s' "$peer" | grep -Eq "$b64key" || die "$exported has no valid AWG_PEER_PUBLIC_KEY"
|
||||
[ "$peer" != "$(get "$params" AWG_PUBLIC_KEY)" ] || die "that is this host's own public key"
|
||||
for name in $shared_names; do
|
||||
[ "$(get "$exported" "$name")" = "$(get "$params" "$name")" ] || die "$name differs between the hosts"
|
||||
done
|
||||
tmp="$(mktemp "$params.XXXXXX")"
|
||||
trap 'rm -f "$tmp"' EXIT
|
||||
grep -v '^AWG_PEER_PUBLIC_KEY=' "$params" > "$tmp" || true
|
||||
printf 'AWG_PEER_PUBLIC_KEY=%s\n' "$peer" >> "$tmp"
|
||||
chmod 600 "$tmp"
|
||||
mv "$tmp" "$params"
|
||||
trap - EXIT
|
||||
printf 'stored peer public key in %s\n' "$params"
|
||||
;;
|
||||
public)
|
||||
[ $# -eq 1 ] || die "usage: public PARAMS"
|
||||
[ -f "$1" ] || die "missing $1"
|
||||
get "$1" AWG_PUBLIC_KEY
|
||||
;;
|
||||
*) die "usage: awg-keys.sh init|export|peer|public ..." ;;
|
||||
esac
|
||||
Executable
+52
@@ -0,0 +1,52 @@
|
||||
#!/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"
|
||||
Executable
+153
@@ -0,0 +1,153 @@
|
||||
#!/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
|
||||
Reference in new issue
Block a user