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
@@ -91,6 +91,19 @@ ml-logs: ## Follow ML service logs
|
||||
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
|
||||
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" logs -f
|
||||
|
||||
# Edge proxy (TLS and tunnel templates under deploy/edge). edge.env is local;
|
||||
# the checked-in example is used when it does not exist.
|
||||
.PHONY: edge-render edge-config
|
||||
EDGE_DIR := $(ROOT)/deploy/edge
|
||||
EDGE_ENV ?= $(if $(wildcard $(EDGE_DIR)/edge.env),$(EDGE_DIR)/edge.env,$(EDGE_DIR)/edge.env.example)
|
||||
EDGE_NGINX_IMAGE ?= nginx:stable-alpine
|
||||
|
||||
edge-render: ## Render the edge nginx config from deploy/edge/edge.env into deploy/edge/rendered
|
||||
@bash "$(EDGE_DIR)/scripts/render.sh" --env "$(EDGE_ENV)" --out "$(EDGE_DIR)/rendered" nginx
|
||||
|
||||
edge-config: ## Render the edge templates and check the nginx config with nginx -t in a container
|
||||
@DOCKER="$(DOCKER)" EDGE_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" bash "$(EDGE_DIR)/scripts/check.sh" "$(EDGE_ENV)"
|
||||
|
||||
check: config check-workflows check-release ## Validate Compose files, CI workflow aggregates, and release tooling without starting services
|
||||
|
||||
check-workflows: ## Check that each workflow aggregate job requires every other job
|
||||
|
||||
@@ -10,6 +10,7 @@ Configuration for running the application with Docker Compose and Nginx.
|
||||
- `docker/docker-compose.e2e.yml` is an overlay used only by the browser suite: it removes the frontend host port and adds the Playwright runner.
|
||||
- `docker/docker-compose.release.yml` and `docker/docker-compose.ml.release.yml` are overlays that name the release images `lincodex.invalid/<service>:<tag>` and never pull them from a registry.
|
||||
- `scripts/` contains the isolated stack smoke check, the browser E2E runner, and `release_manifest.py`, which builds, describes, and checks release images.
|
||||
- `edge/` holds the public edge proxy: nginx and tunnel templates, certbot renewal hook, systemd drop-ins, and the scripts that render and check them. `make -C deploy edge-config` renders the nginx configuration and runs `nginx -t` in a container; see the [edge proxy runbook](../docs/en/DEPLOY.md#edge-proxy-https-and-tunnel).
|
||||
|
||||
Dockerfiles live in service directories and `.env.example` at the repository root. From the root, `make -C deploy config` checks Compose and `make -C deploy up` starts the core stack after `.env` is prepared. Start monitoring separately with `make -C deploy monitoring-up`. `make -C deploy test-e2e` runs the Playwright suite against a throwaway stack under a unique project name and removes it afterwards; see [TESTING.md](../docs/en/TESTING.md#browser-e2e-suite).
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
- `docker/docker-compose.e2e.yml` - overlay только для браузерного набора: убирает host-порт frontend и добавляет запуск Playwright.
|
||||
- `docker/docker-compose.release.yml` и `docker/docker-compose.ml.release.yml` - overlay с именами образов выпуска `lincodex.invalid/<service>:<tag>` без скачивания из registry.
|
||||
- `scripts/` содержит smoke-проверку изолированного стека, запуск браузерных E2E и `release_manifest.py` для сборки, описания и сверки образов выпуска.
|
||||
- `edge/` содержит публичный edge proxy: шаблоны nginx и туннеля, hook продления certbot, drop-in systemd и скрипты, которые их отрисовывают и проверяют. `make -C deploy edge-config` отрисовывает конфигурацию nginx и выполняет `nginx -t` в контейнере; см. [runbook edge proxy](../docs/ru/DEPLOY.md#edge-proxy-https-и-туннель).
|
||||
|
||||
Dockerfile лежат в каталогах сервисов, `.env.example` - в корне репозитория. Из корня: `make -C deploy config` проверяет Compose, `make -C deploy up` запускает основной стек после подготовки `.env`. Мониторинг включается отдельно через `make -C deploy monitoring-up`. `make -C deploy test-e2e` запускает набор Playwright на одноразовом стеке с уникальным именем проекта и удаляет его после прогона; см. [TESTING.md](../docs/ru/TESTING.md#браузерный-e2e-набор).
|
||||
|
||||
|
||||
@@ -272,7 +272,10 @@ services:
|
||||
backend:
|
||||
condition: service_started
|
||||
ports:
|
||||
- "127.0.0.1:${APP_HTTP_PORT:-8080}:8080"
|
||||
# Loopback by default. On the app host behind the edge proxy,
|
||||
# APP_HTTP_BIND is the tunnel address (see docs/en/DEPLOY.md); never
|
||||
# set it to 0.0.0.0, because Docker publishing bypasses ufw.
|
||||
- "${APP_HTTP_BIND:-127.0.0.1}:${APP_HTTP_PORT:-8080}:8080"
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"]
|
||||
interval: 10s
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Local edge settings and rendered output; neither is committed.
|
||||
/edge.env
|
||||
/rendered/
|
||||
@@ -0,0 +1,30 @@
|
||||
# AmneziaWG, app host (client role): /etc/amnezia/amneziawg/{{AWG_IFACE}}.conf
|
||||
# Rendered by deploy/edge/scripts/render.sh from the root-only parameters
|
||||
# file on the host. The rendered file holds the private key: mode 600,
|
||||
# never commit or print it.
|
||||
|
||||
[Interface]
|
||||
Address = {{AWG_APP_IP}}/{{AWG_PREFIX}}
|
||||
MTU = {{AWG_MTU}}
|
||||
PrivateKey = {{AWG_PRIVATE_KEY}}
|
||||
# Obfuscation parameters. Both ends must carry identical values.
|
||||
Jc = {{AWG_JC}}
|
||||
Jmin = {{AWG_JMIN}}
|
||||
Jmax = {{AWG_JMAX}}
|
||||
S1 = {{AWG_S1}}
|
||||
S2 = {{AWG_S2}}
|
||||
H1 = {{AWG_H1}}
|
||||
H2 = {{AWG_H2}}
|
||||
H3 = {{AWG_H3}}
|
||||
H4 = {{AWG_H4}}
|
||||
|
||||
[Peer]
|
||||
# The edge proxy.
|
||||
PublicKey = {{AWG_PEER_PUBLIC_KEY}}
|
||||
Endpoint = {{AWG_EDGE_ENDPOINT_IP}}:{{AWG_PORT}}
|
||||
# Only the edge tunnel address. Never 0.0.0.0/0: the host keeps its own
|
||||
# default route and its LAN access.
|
||||
AllowedIPs = {{AWG_EDGE_IP}}/32
|
||||
# This side sits behind NAT; the keepalive holds the mapping open so the
|
||||
# edge can reach the app host at any time.
|
||||
PersistentKeepalive = {{AWG_KEEPALIVE}}
|
||||
@@ -0,0 +1,27 @@
|
||||
# AmneziaWG, edge side (server role): /etc/amnezia/amneziawg/{{AWG_IFACE}}.conf
|
||||
# Rendered by deploy/edge/scripts/render.sh from the root-only parameters
|
||||
# file on the host. The rendered file holds the private key: mode 600,
|
||||
# never commit or print it.
|
||||
|
||||
[Interface]
|
||||
Address = {{AWG_EDGE_IP}}/{{AWG_PREFIX}}
|
||||
ListenPort = {{AWG_PORT}}
|
||||
MTU = {{AWG_MTU}}
|
||||
PrivateKey = {{AWG_PRIVATE_KEY}}
|
||||
# Obfuscation parameters. Both ends must carry identical values.
|
||||
Jc = {{AWG_JC}}
|
||||
Jmin = {{AWG_JMIN}}
|
||||
Jmax = {{AWG_JMAX}}
|
||||
S1 = {{AWG_S1}}
|
||||
S2 = {{AWG_S2}}
|
||||
H1 = {{AWG_H1}}
|
||||
H2 = {{AWG_H2}}
|
||||
H3 = {{AWG_H3}}
|
||||
H4 = {{AWG_H4}}
|
||||
|
||||
[Peer]
|
||||
# The app host. It initiates from behind NAT, so no Endpoint is set here.
|
||||
PublicKey = {{AWG_PEER_PUBLIC_KEY}}
|
||||
# Only the peer's tunnel address: the tunnel never carries other traffic and
|
||||
# installs no default route.
|
||||
AllowedIPs = {{AWG_APP_IP}}/32
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
# Certbot deploy hook, installed as
|
||||
# /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh (mode 755).
|
||||
# Certbot runs it only after a certificate was actually renewed. The reload
|
||||
# is skipped when the new configuration does not pass the syntax test, so a
|
||||
# bad file cannot take the running nginx down.
|
||||
set -eu
|
||||
nginx -t
|
||||
systemctl reload nginx
|
||||
@@ -0,0 +1,41 @@
|
||||
# Edge proxy configuration. Copy to edge.env and adjust; the file holds no
|
||||
# secrets, only names and addresses. Keys and obfuscation parameters of the
|
||||
# tunnel live in a separate root-only file on each server (see awg/).
|
||||
#
|
||||
# Rendered with: make -C deploy edge-render
|
||||
# Checked with: make -C deploy edge-config
|
||||
|
||||
# Public name served by the edge nginx. It must resolve to EDGE_LISTEN_IP.
|
||||
EDGE_DOMAIN=lincodex.safelysvn.space
|
||||
# Certbot lineage name; the certificate is read from
|
||||
# /etc/letsencrypt/live/<EDGE_CERT_NAME>/. Use the domain unless certbot
|
||||
# was run with --cert-name.
|
||||
EDGE_CERT_NAME=lincodex.safelysvn.space
|
||||
# The only address nginx binds to on ports 80 and 443. Never 0.0.0.0: the
|
||||
# shared edge host has other listeners (for example UDP 443).
|
||||
EDGE_LISTEN_IP=89.185.85.208
|
||||
# Directory served for ACME http-01 challenges (certbot --webroot-path).
|
||||
EDGE_ACME_WEBROOT=/var/www/certbot
|
||||
# Strict-Transport-Security max-age in seconds (180 days). Raise it to
|
||||
# 31536000 and consider preload only after the site has run on HTTPS for a while.
|
||||
EDGE_HSTS_MAX_AGE=15552000
|
||||
# Request body limit. Keep it at or above the inner nginx limit (10m) and the
|
||||
# largest upload policy (resume: 5 MB plus multipart overhead).
|
||||
EDGE_MAX_BODY=10m
|
||||
|
||||
# Tunnel: one dedicated point-to-point AmneziaWG link between the edge (server
|
||||
# role) and the app host (client role, behind NAT, initiates the handshake).
|
||||
# The /30 must not overlap any route or Docker range on either host.
|
||||
AWG_IFACE=awg-lincodex
|
||||
AWG_EDGE_IP=10.79.0.1
|
||||
AWG_APP_IP=10.79.0.2
|
||||
AWG_PREFIX=30
|
||||
# Public address and UDP port the edge listens on for the tunnel (not 443).
|
||||
AWG_EDGE_ENDPOINT_IP=213.165.42.19
|
||||
AWG_PORT=51871
|
||||
AWG_MTU=1380
|
||||
AWG_KEEPALIVE=25
|
||||
|
||||
# Port the app host publishes for the frontend container (APP_HTTP_PORT in the
|
||||
# app .env). The edge proxies to AWG_APP_IP:APP_HTTP_PORT.
|
||||
APP_HTTP_PORT=8080
|
||||
@@ -0,0 +1,27 @@
|
||||
# Catch-all for requests whose Host is not a served name, on the one address
|
||||
# this edge uses. It keeps unknown names away from the application and
|
||||
# stops the TLS handshake without presenting a certificate.
|
||||
#
|
||||
# Rendered from deploy/edge/nginx/00-default.conf.template. Do not edit the
|
||||
# installed copy; change the template and render again.
|
||||
|
||||
# Access log without the query string: $uri never contains it, so values
|
||||
# such as email addresses or tokens in a query are not written to the log.
|
||||
log_format lincodex '$remote_addr [$time_local] "$request_method $uri" '
|
||||
'$status $body_bytes_sent rt=$request_time '
|
||||
'urt=$upstream_response_time id=$request_id';
|
||||
|
||||
server {
|
||||
listen {{EDGE_LISTEN_IP}}:80 default_server;
|
||||
server_name _;
|
||||
server_tokens off;
|
||||
# 444 closes the connection without a response.
|
||||
return 444;
|
||||
}
|
||||
|
||||
server {
|
||||
listen {{EDGE_LISTEN_IP}}:443 ssl default_server;
|
||||
server_name _;
|
||||
server_tokens off;
|
||||
ssl_reject_handshake on;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
# Port 80 for {{EDGE_DOMAIN}}: ACME http-01 challenges and the redirect to
|
||||
# HTTPS. This is the only file needed to obtain the first certificate.
|
||||
#
|
||||
# Rendered from deploy/edge/nginx/lincodex-http.conf.template.
|
||||
|
||||
server {
|
||||
listen {{EDGE_LISTEN_IP}}:80;
|
||||
server_name {{EDGE_DOMAIN}};
|
||||
server_tokens off;
|
||||
access_log /var/log/nginx/lincodex.access.log lincodex;
|
||||
|
||||
# Certbot writes challenge files here (webroot method). Nothing else is
|
||||
# served from this directory.
|
||||
location ^~ /.well-known/acme-challenge/ {
|
||||
root {{EDGE_ACME_WEBROOT}};
|
||||
default_type text/plain;
|
||||
try_files $uri =404;
|
||||
}
|
||||
|
||||
# The target uses the configured name, not $host, so a forged Host header
|
||||
# cannot turn the redirect into an open redirect.
|
||||
location / {
|
||||
return 301 https://{{EDGE_DOMAIN}}$request_uri;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
# Port 443 for {{EDGE_DOMAIN}}: TLS termination and a reverse proxy to the
|
||||
# frontend container of the app host, reached through the AmneziaWG tunnel.
|
||||
# The frontend container serves the SPA (including the index.html fallback
|
||||
# for deep links) and proxies /api to the Go backend, so this proxy forwards
|
||||
# every path unchanged and both live on one origin.
|
||||
#
|
||||
# Rendered from deploy/edge/nginx/lincodex-https.conf.template. Needs the
|
||||
# certificate from certbot; install it only after the first issuance.
|
||||
|
||||
upstream lincodex_app {
|
||||
server {{EDGE_UPSTREAM}};
|
||||
# Reused connections to the app host; the tunnel round trip is the
|
||||
# expensive part of a request.
|
||||
keepalive 16;
|
||||
}
|
||||
|
||||
server {
|
||||
listen {{EDGE_LISTEN_IP}}:443 ssl;
|
||||
# No HTTP/3: QUIC would need UDP 443, which another service on this host
|
||||
# already owns.
|
||||
http2 on;
|
||||
server_name {{EDGE_DOMAIN}};
|
||||
server_tokens off;
|
||||
access_log /var/log/nginx/lincodex.access.log lincodex;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/{{EDGE_CERT_NAME}}/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/{{EDGE_CERT_NAME}}/privkey.pem;
|
||||
|
||||
# Mozilla "intermediate" profile: TLS 1.2 and 1.3, AEAD suites with
|
||||
# forward secrecy. The client picks the suite (prefer_server_ciphers off).
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_timeout 1d;
|
||||
ssl_session_cache shared:lincodex_tls:10m;
|
||||
# Tickets would keep one long-lived key that undoes forward secrecy.
|
||||
ssl_session_tickets off;
|
||||
# OCSP stapling is deliberately absent: Let's Encrypt certificates no
|
||||
# longer carry an OCSP responder URL, so there is nothing to staple.
|
||||
|
||||
# HSTS without preload. The header is set at server level only; a nested
|
||||
# add_header in a location would drop it.
|
||||
add_header Strict-Transport-Security "max-age={{EDGE_HSTS_MAX_AGE}}; includeSubDomains" always;
|
||||
|
||||
# Largest accepted request body. The resume upload limit is 5 MB; the
|
||||
# frontend container allows 10 MB, and this value must not be lower.
|
||||
client_max_body_size {{EDGE_MAX_BODY}};
|
||||
client_header_timeout 15s;
|
||||
client_body_timeout 60s;
|
||||
send_timeout 60s;
|
||||
|
||||
# Compression for the static shell only. gzip over responses that mix
|
||||
# secrets with attacker-influenced content enables BREACH, so JSON is not
|
||||
# in gzip_types and the API location turns compression off outright.
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_proxied any;
|
||||
gzip_comp_level 5;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/css text/javascript application/javascript image/svg+xml;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
# Clear Connection so the upstream keepalive pool is used.
|
||||
proxy_set_header Connection "";
|
||||
# Host is preserved: the backend and the frontend container see the public
|
||||
# name, which is what origin and link checks compare against.
|
||||
proxy_set_header Host $host;
|
||||
# The edge is the first trusted hop, so it replaces client-supplied
|
||||
# forwarding headers instead of appending to them.
|
||||
proxy_set_header X-Forwarded-For $remote_addr;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_redirect off;
|
||||
proxy_connect_timeout 5s;
|
||||
proxy_send_timeout 60s;
|
||||
# The frontend container gives the backend 30 s; leave room for it.
|
||||
proxy_read_timeout 60s;
|
||||
|
||||
location ^~ /api/ {
|
||||
gzip off;
|
||||
proxy_pass http://lincodex_app;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://lincodex_app;
|
||||
}
|
||||
}
|
||||
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
|
||||
@@ -0,0 +1,9 @@
|
||||
# Drop-in for awg-quick@awg-lincodex.service on both hosts, installed as
|
||||
# /etc/systemd/system/awg-quick@awg-lincodex.service.d/userspace.conf.
|
||||
#
|
||||
# The kernel module is not used: awg-quick falls back to the userspace
|
||||
# daemon, which is installed from a pinned build (see docs/en/DEPLOY.md).
|
||||
# The daemon is one process per interface and is not restarted by systemd
|
||||
# on its own; check the handshake with `awg show` after incidents.
|
||||
[Service]
|
||||
Environment=WG_QUICK_USERSPACE_IMPLEMENTATION=/usr/local/bin/amneziawg-go
|
||||
@@ -0,0 +1,12 @@
|
||||
# Drop-in for docker.service on the app host, installed as
|
||||
# /etc/systemd/system/docker.service.d/10-lincodex-awg.conf.
|
||||
#
|
||||
# The frontend container publishes its port on the tunnel address
|
||||
# (APP_HTTP_BIND). Docker cannot publish a port on an address that does not
|
||||
# exist yet, so the tunnel interface must be up before the daemon starts the
|
||||
# restart-policy containers. Wants (not Requires) keeps Docker running when
|
||||
# the tunnel unit fails; the frontend container then fails to start and is
|
||||
# visible as such.
|
||||
[Unit]
|
||||
After=awg-quick@awg-lincodex.service
|
||||
Wants=awg-quick@awg-lincodex.service
|
||||
@@ -1,3 +1,13 @@
|
||||
# Behind the TLS edge proxy the client's scheme arrives in X-Forwarded-Proto
|
||||
# (deploy/edge). Keep it when it is https so the backend sees the original
|
||||
# scheme; every other value falls back to the scheme of this server. The
|
||||
# header is trustworthy only because the frontend port is published on
|
||||
# loopback or on the tunnel address, never on a public one.
|
||||
map $http_x_forwarded_proto $forwarded_proto {
|
||||
default $scheme;
|
||||
https https;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
server_name _;
|
||||
@@ -25,7 +35,7 @@ server {
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Proto $forwarded_proto;
|
||||
proxy_set_header X-Request-ID $request_id;
|
||||
proxy_connect_timeout 3s;
|
||||
proxy_send_timeout 30s;
|
||||
@@ -41,7 +51,7 @@ server {
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Proto $forwarded_proto;
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_send_timeout 5s;
|
||||
proxy_read_timeout 5s;
|
||||
|
||||
Reference in new issue
Block a user