mirror of
https://github.com/NetworkCats/rustyip.git
synced 2026-11-27 08:06:17 +00:00
Fix HTTP firewall rules and add firewall-only deployment
This commit is contained in:
+19
-1
@@ -65,8 +65,26 @@ This triggers the full pipeline. Monitor progress under the **Actions** tab in t
|
||||
### Infrastructure Details
|
||||
|
||||
- **VPS**: Vultr High Frequency (`vhf-1c-1gb`, 1 vCPU / 1 GB), Debian 13, region `sjc` (Silicon Valley)
|
||||
- **Firewall**: Vultr opens SSH and HTTPS; on the host, UFW limits HTTPS to Cloudflare's IPv4 ranges, refreshed daily by `ufw-cloudflare-sync.timer` (Docker-published ports are filtered via the `DOCKER-USER` chain)
|
||||
- **Firewall**: Vultr opens SSH, HTTP and HTTPS; on the host, UFW limits HTTP and HTTPS to Cloudflare's IPv4 ranges, refreshed daily by `ufw-cloudflare-sync.timer` (Docker-published ports are filtered via the `DOCKER-USER` chain)
|
||||
- **Deployment**: Blue-green with HAProxy traffic switching
|
||||
- **First deploy**: Automatically applies base OS hardening (UFW, fail2ban, sysctl tuning) and installs Docker
|
||||
- **Subsequent deploys**: The firewall rules are re-applied and the application container is updated via blue-green swap
|
||||
- **Rotating the SSH key**: Updating `SSH_PUBLIC_KEY`/`SSH_PRIVATE_KEY` rebuilds the instance on the next deploy (new IP; update DNS afterwards)
|
||||
|
||||
### Firewall-only update (no application redeploy)
|
||||
|
||||
After merging these changes into the default branch, open **Actions > Deploy >
|
||||
Run workflow** and select that branch. Manual runs only apply the HTTP firewall
|
||||
rule and run the Ansible `firewall` role. They reuse the existing production
|
||||
secrets and Terraform state; no release tag or local credentials are required.
|
||||
|
||||
The saved Terraform plan is checked before applying: only creation or in-place
|
||||
update of `vultr_firewall_rule.http_v4` is allowed. Any other resource change or
|
||||
replacement aborts the run. The VPS, SSH key, Docker daemon, HAProxy and application
|
||||
containers are not redeployed. UFW rules and the daily sync script are updated
|
||||
in place, then both public HTTP and HTTPS `/ip` endpoints must return 200.
|
||||
|
||||
This repair assumes the existing HAProxy already listens on port 80 and Docker
|
||||
publishes `80:80`, as configured in the production templates. If the final HTTP
|
||||
check fails, inspect that running configuration; the workflow deliberately does
|
||||
not recreate containers. Release-triggered runs still perform the full deployment.
|
||||
|
||||
@@ -24,4 +24,5 @@
|
||||
when: first_deploy | bool
|
||||
# Runs on every deploy, and before app so HAProxy never serves unfiltered.
|
||||
- role: firewall
|
||||
tags: [firewall]
|
||||
- role: app
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/bin/sh
|
||||
# Keep UFW route rules allowing Cloudflare's IPv4 ranges to reach HTTPS (443)
|
||||
# Keep UFW route rules allowing Cloudflare's IPv4 ranges to reach HTTP (80) and HTTPS (443)
|
||||
# in sync with https://www.cloudflare.com/ips-v4. HAProxy's port is published
|
||||
# by Docker, so these are `ufw route` rules (evaluated from the DOCKER-USER
|
||||
# chain, see /etc/ufw/after.rules) rather than plain `ufw allow` rules.
|
||||
@@ -9,7 +9,7 @@
|
||||
set -eu
|
||||
|
||||
URL="https://www.cloudflare.com/ips-v4"
|
||||
PORT=443
|
||||
PORTS="80 443"
|
||||
COMMENT=cloudflare
|
||||
# Cloudflare publishes ~15 IPv4 ranges; far fewer means a bad response.
|
||||
MIN_RANGES=10
|
||||
@@ -27,24 +27,26 @@ if printf '%s\n' "$desired" | grep -Evq '^([0-9]{1,3}\.){3}[0-9]{1,3}/([0-9]|[12
|
||||
exit 1
|
||||
fi
|
||||
|
||||
current=$(ufw show added \
|
||||
| sed -n "s|^ufw route allow from \([0-9./]*\) to any port $PORT proto tcp comment '$COMMENT'\$|\1|p" \
|
||||
| sort -u)
|
||||
|
||||
added=0
|
||||
removed=0
|
||||
# Add new ranges before removing stale ones so allowed traffic is never cut off.
|
||||
for cidr in $desired; do
|
||||
if ! printf '%s\n' "$current" | grep -qxF "$cidr"; then
|
||||
ufw route allow proto tcp from "$cidr" to any port "$PORT" comment "$COMMENT" >/dev/null
|
||||
added=$((added + 1))
|
||||
fi
|
||||
done
|
||||
for cidr in $current; do
|
||||
if ! printf '%s\n' "$desired" | grep -qxF "$cidr"; then
|
||||
ufw route delete allow proto tcp from "$cidr" to any port "$PORT" comment "$COMMENT" >/dev/null
|
||||
removed=$((removed + 1))
|
||||
fi
|
||||
for PORT in $PORTS; do
|
||||
current=$(ufw show added \
|
||||
| sed -n "s|^ufw route allow from \([0-9./]*\) to any port $PORT proto tcp comment '$COMMENT'\$|\1|p" \
|
||||
| sort -u)
|
||||
|
||||
# Add new ranges before removing stale ones so allowed traffic is never cut off.
|
||||
for cidr in $desired; do
|
||||
if ! printf '%s\n' "$current" | grep -qxF "$cidr"; then
|
||||
ufw route allow proto tcp from "$cidr" to any port "$PORT" comment "$COMMENT" >/dev/null
|
||||
added=$((added + 1))
|
||||
fi
|
||||
done
|
||||
for cidr in $current; do
|
||||
if ! printf '%s\n' "$desired" | grep -qxF "$cidr"; then
|
||||
ufw route delete allow proto tcp from "$cidr" to any port "$PORT" comment "$COMMENT" >/dev/null
|
||||
removed=$((removed + 1))
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
echo "cloudflare ranges: $count, added: $added, removed: $removed"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
# Restrict HTTPS to Cloudflare at the host. The Vultr firewall only opens the
|
||||
# port; this role decides who may use it. Runs on every deploy so existing
|
||||
# Restrict HTTP and HTTPS to Cloudflare at the host. The Vultr firewall only opens the
|
||||
# ports; this role decides who may use it. Runs on every deploy so existing
|
||||
# hosts pick up changes, and must succeed before HAProxy serves traffic,
|
||||
# because HAProxy trusts CF-Connecting-IP from anything that reaches it.
|
||||
|
||||
@@ -65,8 +65,10 @@
|
||||
|| { echo "DOCKER-USER does not start with ufw-user-forward: $first"; exit 1; }
|
||||
iptables -S DOCKER-USER | grep -q -- '-d 172.16.0.0/12 .*-j ufw-docker-logging-deny' \
|
||||
|| { echo "DOCKER-USER is missing the default deny"; exit 1; }
|
||||
allowed=$(iptables -S ufw-user-forward | grep -c -- '--dport 443 -j ACCEPT' || true)
|
||||
[ "$allowed" -ge 10 ] \
|
||||
|| { echo "only $allowed Cloudflare ranges allowed on 443"; exit 1; }
|
||||
echo "ok: $allowed Cloudflare ranges allowed on 443"
|
||||
for port in 80 443; do
|
||||
allowed=$(iptables -S ufw-user-forward | grep -c -- "--dport $port -j ACCEPT" || true)
|
||||
[ "$allowed" -ge 10 ] \
|
||||
|| { echo "only $allowed Cloudflare ranges allowed on $port"; exit 1; }
|
||||
echo "ok: $allowed Cloudflare ranges allowed on $port"
|
||||
done
|
||||
changed_when: false
|
||||
|
||||
@@ -24,3 +24,15 @@ resource "vultr_firewall_rule" "https_v4" {
|
||||
port = "443"
|
||||
notes = "Allow HTTPS; host UFW limits it to Cloudflare"
|
||||
}
|
||||
|
||||
# HTTP is needed for clients such as `curl ip.nc.gy` without an HTTPS scheme.
|
||||
# The host UFW rules restrict this port to Cloudflare, just like HTTPS.
|
||||
resource "vultr_firewall_rule" "http_v4" {
|
||||
firewall_group_id = vultr_firewall_group.rustyip.id
|
||||
protocol = "tcp"
|
||||
ip_type = "v4"
|
||||
subnet = "0.0.0.0"
|
||||
subnet_size = 0
|
||||
port = "80"
|
||||
notes = "Allow HTTP; host UFW limits it to Cloudflare"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user