-
Notifications
You must be signed in to change notification settings - Fork 0
fix: remove host-wide WireGuard dependency #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,8 +117,38 @@ jobs: | |
|
|
||
| cd /home/ec2-user/crypto_sentiment_crawler | ||
|
|
||
| # Keep WireGuard active for non-Reddit crawler dependencies. Reddit | ||
| # HTML is fetched by Unbrowser with a local cookie refresh socket. | ||
| # Retire the legacy host-wide WireGuard tunnel before any | ||
| # DNS-dependent deployment step. Reddit now uses the cookie-backed | ||
| # Unbrowser transport; all other traffic uses direct EC2 egress. | ||
| sudo systemctl disable --now wg-quick@wg0 >/dev/null 2>&1 || true | ||
| if /sbin/ip link show wg0 >/dev/null 2>&1; then | ||
| echo "Stopping legacy WireGuard interface..." | ||
| if ! command -v wg-quick >/dev/null 2>&1; then | ||
| echo "ERROR: wg0 is active but wg-quick is unavailable" | ||
| exit 1 | ||
| fi | ||
| sudo wg-quick down wg0 | ||
| fi | ||
| if /sbin/ip link show wg0 >/dev/null 2>&1; then | ||
| echo "ERROR: Legacy WireGuard interface is still active" | ||
| exit 1 | ||
| fi | ||
| WG_UNIT_STATE=$(sudo systemctl is-enabled wg-quick@wg0 2>/dev/null || true) | ||
| case "$WG_UNIT_STATE" in | ||
| enabled|enabled-runtime|linked|linked-runtime) | ||
| echo "ERROR: Legacy WireGuard service remains enabled ($WG_UNIT_STATE)" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| sudo rm -f /etc/wireguard/wg0.conf | ||
| if ! timeout 15 getent ahostsv4 github.com >/dev/null 2>&1; then | ||
| echo "ERROR: Direct EC2 DNS is unavailable after WireGuard cleanup" | ||
| exit 1 | ||
| fi | ||
| if ! curl -fsS --connect-timeout 5 --max-time 15 https://github.com/robots.txt >/dev/null; then | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the DNS check above: github.com/robots.txt is not a meaningful direct-egress canary because GitHub was explicitly bypass-routed in the retired WireGuard config. Prefer a host like api.coingecko.com that actually traversed the VPN tunnel. |
||
| echo "ERROR: Direct EC2 HTTPS egress is unavailable after WireGuard cleanup" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Pull latest code. Preserve any EC2-local edits so deployment can | ||
| # move to the release tag without discarding operational changes. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,17 +56,75 @@ echo "Deploying on EC2..." | |
| ssh -i "$KEY_PATH" -o StrictHostKeyChecking=no "ec2-user@$EC2_IP" << 'ENDSSH' | ||
| set -e | ||
|
|
||
| # Extract application | ||
| sudo rm -rf /opt/crypto-sentiment/* | ||
| APP_DIR=/opt/crypto-sentiment | ||
| PERSIST_DIR=/tmp/crypto-sentiment-persist | ||
|
|
||
| # Retire the legacy host-wide VPN before any destructive deployment step. | ||
| # Reddit uses the cookie-backed Unbrowser transport in production. | ||
| sudo systemctl disable --now wg-quick@wg0 >/dev/null 2>&1 || true | ||
| if /sbin/ip link show wg0 >/dev/null 2>&1; then | ||
| echo "Stopping legacy WireGuard interface..." | ||
| if ! command -v wg-quick >/dev/null 2>&1; then | ||
| echo "ERROR: wg0 is active but wg-quick is unavailable" | ||
| exit 1 | ||
| fi | ||
| sudo wg-quick down wg0 | ||
| fi | ||
| if /sbin/ip link show wg0 >/dev/null 2>&1; then | ||
| echo "ERROR: Legacy WireGuard interface is still active" | ||
| exit 1 | ||
| fi | ||
| WG_UNIT_STATE=$(sudo systemctl is-enabled wg-quick@wg0 2>/dev/null || true) | ||
| case "$WG_UNIT_STATE" in | ||
| enabled|enabled-runtime|linked|linked-runtime) | ||
| echo "ERROR: Legacy WireGuard service remains enabled ($WG_UNIT_STATE)" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
| sudo rm -f /etc/wireguard/wg0.conf | ||
| if ! timeout 15 getent ahostsv4 github.com >/dev/null 2>&1; then | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mirrors the deploy.yml canary gap: getent/curl on github.com won't detect a lingering full tunnel because GitHub was a hardcoded bypass route in the old PostUp. Switch the canary to api.coingecko.com to actually verify direct EC2 egress. |
||
| echo "ERROR: Direct EC2 DNS is unavailable after WireGuard cleanup" | ||
| exit 1 | ||
| fi | ||
| if ! curl -fsS --connect-timeout 5 --max-time 15 https://github.com/robots.txt >/dev/null; then | ||
| echo "ERROR: Direct EC2 HTTPS egress is unavailable after WireGuard cleanup" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Preserve runtime state before replacing application files. | ||
| rm -rf "$PERSIST_DIR" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PERSIST_DIR lives under /tmp (typically mode 1777), and the loop moves .env there with its original ownership/permissions intact. For the duration of the deploy, the API keys and UNBROWSER_COOKIE_SERVICE_TOKEN in .env are readable by any local user. Prefer a persist location outside /tmp (e.g. /var/tmp/crypto-sentiment-persist or a root-owned dir) and/or chmod 600 the staged .env after the mv. |
||
| mkdir -p "$PERSIST_DIR" | ||
| for name in data logs backups run .env; do | ||
| if [ -e "$APP_DIR/$name" ]; then | ||
| mv "$APP_DIR/$name" "$PERSIST_DIR/$name" | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The persist-out phase (rm -rf $PERSIST_DIR through the for-loop mv) runs before the EXIT trap is installed, so an interruption mid-move leaves runtime state split between /opt and /tmp with no automatic restore. Consider installing the trap (or a targeted cleanup) before the move-out loop so a failed move cannot strand data. |
||
| fi | ||
| done | ||
|
|
||
| # Extract application. Restore runtime state even if extraction fails. | ||
| restore_runtime_state() { | ||
| sudo mkdir -p "$APP_DIR" | ||
| sudo chown ec2-user:ec2-user "$APP_DIR" | ||
| for name in data logs backups run .env; do | ||
| if [ -e "$PERSIST_DIR/$name" ]; then | ||
| rm -rf "$APP_DIR/$name" | ||
| mv "$PERSIST_DIR/$name" "$APP_DIR/$name" | ||
| fi | ||
| done | ||
| } | ||
| trap restore_runtime_state EXIT | ||
|
|
||
| sudo rm -rf "$APP_DIR" | ||
| sudo tar -xzf /tmp/crypto-sentiment.tar.gz -C /opt | ||
| sudo mv /opt/app/* /opt/crypto-sentiment/ | ||
| sudo rmdir /opt/app | ||
| sudo chown -R ec2-user:ec2-user /opt/crypto-sentiment | ||
| sudo mv /opt/app "$APP_DIR" | ||
| sudo chown -R ec2-user:ec2-user "$APP_DIR" | ||
| restore_runtime_state | ||
| trap - EXIT | ||
| rm -rf "$PERSIST_DIR" | ||
|
|
||
| cd /opt/crypto-sentiment | ||
| cd "$APP_DIR" | ||
|
|
||
| # Create data directories | ||
| mkdir -p data logs | ||
| mkdir -p data logs backups run | ||
|
|
||
| # Check for .env file | ||
| if [ ! -f .env ]; then | ||
|
|
@@ -77,12 +135,6 @@ if [ ! -f .env ]; then | |
| echo "" | ||
| fi | ||
|
|
||
| # Setup WireGuard VPN if configured | ||
| if grep -q WG_PRIVATE_KEY .env 2>/dev/null; then | ||
| echo "Setting up WireGuard VPN..." | ||
| bash deploy/setup-wireguard.sh .env | ||
| fi | ||
|
|
||
| # Build and start services | ||
| echo "Building Docker images..." | ||
| docker-compose build --no-cache | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weak egress canary: the removed deploy/setup-wireguard.sh added static bypass routes for GitHub ranges (140.82.112.0/20, 185.199.108.0/22 in PostUp), so github.com DNS + robots.txt were reachable via direct EC2 even while the full tunnel and VPN-only DNS were installed. A host with a still-active (dead-peer) wg0 would pass this check and silently deploy with broken DNS for the exact feeds this PR fixes. Use api.coingecko.com (as docs/EC2_INSTANCES.md already does) for both the DNS and HTTPS egress checks, since that host was carried over the VPN and is the true signal of direct egress.