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:
@@ -1,6 +1,8 @@
|
||||
name: Deploy
|
||||
|
||||
on:
|
||||
# Manual runs update only firewall rules; releases still deploy the app.
|
||||
workflow_dispatch:
|
||||
workflow_run:
|
||||
workflows: [Release]
|
||||
types: [completed]
|
||||
@@ -15,7 +17,7 @@ jobs:
|
||||
deploy:
|
||||
name: Deploy
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event.workflow_run.conclusion == 'success'
|
||||
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
|
||||
environment: production
|
||||
concurrency:
|
||||
group: deploy-production
|
||||
@@ -25,6 +27,7 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Resolve image tag from release
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
id: image
|
||||
run: |
|
||||
TAG="${{ github.event.workflow_run.head_branch }}"
|
||||
@@ -48,6 +51,7 @@ jobs:
|
||||
-backend-config="endpoints={s3=\"${{ secrets.TF_STATE_ENDPOINT }}\"}"
|
||||
|
||||
- name: Terraform apply
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
working-directory: deploy/terraform
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.TF_STATE_ACCESS_KEY }}
|
||||
@@ -57,6 +61,27 @@ jobs:
|
||||
-var="vultr_api_key=${{ secrets.VULTR_API_KEY }}" \
|
||||
-var="ssh_public_key=${{ secrets.SSH_PUBLIC_KEY }}"
|
||||
|
||||
# Target only the new rule. Refuse any plan that changes other resources,
|
||||
# including dependencies, so a manual repair cannot rebuild the VPS.
|
||||
- name: Apply HTTP firewall rule only
|
||||
if: github.event_name == 'workflow_dispatch'
|
||||
working-directory: deploy/terraform
|
||||
env:
|
||||
AWS_ACCESS_KEY_ID: ${{ secrets.TF_STATE_ACCESS_KEY }}
|
||||
AWS_SECRET_ACCESS_KEY: ${{ secrets.TF_STATE_SECRET_KEY }}
|
||||
TF_VAR_vultr_api_key: ${{ secrets.VULTR_API_KEY }}
|
||||
TF_VAR_ssh_public_key: ${{ secrets.SSH_PUBLIC_KEY }}
|
||||
run: |
|
||||
terraform plan -input=false -out=firewall.tfplan \
|
||||
-target=vultr_firewall_rule.http_v4
|
||||
terraform show -json firewall.tfplan | jq -e '
|
||||
all(.resource_changes[]?;
|
||||
.change.actions == ["no-op"] or
|
||||
(.address == "vultr_firewall_rule.http_v4" and
|
||||
(.change.actions == ["create"] or .change.actions == ["update"])))
|
||||
' > /dev/null
|
||||
terraform apply -input=false firewall.tfplan
|
||||
|
||||
- name: Extract VPS IP (masked)
|
||||
id: vps
|
||||
working-directory: deploy/terraform
|
||||
@@ -105,6 +130,7 @@ jobs:
|
||||
|
||||
# ---- Detect first deployment ----
|
||||
- name: Detect first deployment
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
id: detect
|
||||
run: |
|
||||
set +e
|
||||
@@ -134,6 +160,7 @@ jobs:
|
||||
EOF
|
||||
|
||||
- name: Run Ansible playbook
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
working-directory: deploy/ansible
|
||||
env:
|
||||
APP_IMAGE: ${{ steps.image.outputs.full }}
|
||||
@@ -150,8 +177,26 @@ jobs:
|
||||
run: |
|
||||
ansible-playbook -i inventory.yml playbook.yml
|
||||
|
||||
- name: Update host firewall only
|
||||
if: github.event_name == 'workflow_dispatch'
|
||||
working-directory: deploy/ansible
|
||||
run: ansible-playbook -i inventory.yml playbook.yml --tags firewall
|
||||
|
||||
- name: Verify public HTTP and HTTPS
|
||||
if: github.event_name == 'workflow_dispatch'
|
||||
env:
|
||||
SITE_DOMAIN: ${{ secrets.SITE_DOMAIN }}
|
||||
run: |
|
||||
for scheme in http https; do
|
||||
code=$(curl -sS --connect-timeout 10 --max-time 40 \
|
||||
-o /dev/null -w '%{http_code}' "$scheme://$SITE_DOMAIN/ip")
|
||||
test "$code" = 200 || { echo "$scheme returned $code"; exit 1; }
|
||||
echo "$scheme: 200"
|
||||
done
|
||||
|
||||
# ---- Verify deployment ----
|
||||
- name: Verify deployment health
|
||||
if: github.event_name != 'workflow_dispatch'
|
||||
env:
|
||||
SSH_OPTS: -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new -i ~/.ssh/deploy_key
|
||||
VPS_HOST: root@${{ steps.vps.outputs.ip }}
|
||||
|
||||
+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