Blueforce-Linux/scripts/install/installer/modules/13-firewall.sh
emirhanmamak-blueforce ef102b9657 feat(field-os): provisioning layer, real ISO build, offline contracts
Adds the Blueforce Field OS provisioning stack on top of the existing fleet
architecture, without rewriting the working systems (installer, diagnostics,
Ansible, WireGuard/SSH/xRDP/RustDesk, Docker policies, monitoring, Docusaurus).

ISO build (the previous builder was a skeleton that produced no medium):
- boot layout is read from the base ISO at build time via
  `xorriso -report_el_torito as_mkisofs` and rebuilt with `-as mkisofs`;
  `-boot_image any replay` cannot work on Ubuntu live ISOs because the GRUB2
  MBR lives in the source image's first 16 sectors and the EFI system partition
  is an appended partition, neither of which is a file in the ISO9660 tree
- preflight now asserts El Torito boot metadata positively: xorriso exits 0 on
  a non-ISO input, which previously let an arbitrary file pass --check
- artifact produced and verified: dist/Blueforce-Field-OS-1.0.0-amd64.iso,
  6 481 917 952 bytes, SHA256 cb0cc56f...d431296, BIOS+UEFI entries preserved,
  /autoinstall.yaml at the media root, casper/minimal.squashfs byte-identical
  to the base
- recorded baseline deviation: the verified medium on the build host is the
  Desktop flavour, so the manifest carries a baseline_deviation block instead
  of hiding it; Server remains one flag away

Offline and identity:
- offline APT contract with concrete package requirements, pin format and an
  audit table; builds fail closed while pins are UNPINNED
- autoinstall keeps interactive-sections: [storage]: the operator confirms the
  target disk, and no automatic wipe is ever performed
- firstboot terminal wizard for the 8-digit dealer id, three-state lifecycle
  (PROVISIONED_OFFLINE -> ENROLLED -> READY) with fail-closed gates, so a
  missing internet link or WireGuard handshake cannot fail provisioning but can
  never produce a false READY

Fleet and migration:
- five read-only Ansible playbooks for Field OS version, provisioning,
  enrollment, remote channels and offline readiness, all wave-scoped with a
  separate production override
- Windows pre-migration inventory and data export extended to the full brief
  scope, read-only, never collecting credentials

Tests: tests/run-all.sh -> 8/8 pass, exit 0. Boot acceptance (UEFI, Legacy
BIOS, Secure Boot), air-gapped install, real enrollment endpoint and Windows
rollback remain LAB work and are listed in IMPLEMENTATION-REPORT.md.
2026-09-16 11:14:42 +03:00

65 lines
No EOL
3.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Apply UFW deny-in baseline and persist Docker published-port policy.
set -euo pipefail
MODULE="13-firewall"
LOG_FILE="${LOG_FILE:-/var/log/blueforce-install.log}"
CHECK_MODE=0
[[ "${1:-}" == "--check" ]] && CHECK_MODE=1
SSH_PORT="${SSH_PORT:-22}"
RDP_PORT="${RDP_PORT:-3389}"
DOCKER_ALLOWLIST="/etc/blueforce/docker-published-port-allowlist.conf"
DOCKER_POLICY_BIN="/usr/local/sbin/blueforce-docker-firewall"
DOCKER_POLICY_UNIT="blueforce-docker-firewall.service"
DOCKER_POLICY_DROPIN="/etc/systemd/system/docker.service.d/blueforce-docker-firewall.conf"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../../.." && pwd)"
# shellcheck source=../offline-apt.sh
source "$SCRIPT_DIR/../offline-apt.sh"
log() { echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) [13-firewall] $1" | tee -a "$LOG_FILE"; }
if [[ "$CHECK_MODE" -eq 1 ]]; then
ufw status 2>/dev/null | grep -q 'Status: active' || { log "FAIL(check): ufw not active"; exit 1; }
ufw status 2>/dev/null | grep -Eq "${SSH_PORT}/tcp.*on wg0|${SSH_PORT}/tcp.*wg0" || { log "FAIL(check): SSH is not restricted to wg0"; exit 1; }
ufw status 2>/dev/null | grep -Eq "${RDP_PORT}/tcp.*on wg0|${RDP_PORT}/tcp.*wg0" || { log "FAIL(check): RDP is not restricted to wg0"; exit 1; }
[[ -f "$DOCKER_ALLOWLIST" && -x "$DOCKER_POLICY_BIN" && -f "$DOCKER_POLICY_DROPIN" ]] || { log "FAIL(check): persistent Docker firewall assets missing"; exit 1; }
systemctl is-enabled --quiet "$DOCKER_POLICY_UNIT" || { log "FAIL(check): persistent Docker firewall unit is not enabled"; exit 1; }
"$DOCKER_POLICY_BIN" --check || { log "FAIL(check): Docker published-port policy is not enforced"; exit 1; }
log "OK(check): SSH/RDP restricted to wg0 and Docker policy persists through systemd"
exit 0
fi
export DEBIAN_FRONTEND=noninteractive
command -v ufw >/dev/null 2>&1 || bf_apt_install ufw
command -v iptables >/dev/null 2>&1 || bf_apt_install iptables
ufw default deny incoming
ufw default allow outgoing
ufw delete allow "$SSH_PORT/tcp" 2>/dev/null || true
ufw delete allow "$RDP_PORT/tcp" 2>/dev/null || true
ufw allow in on wg0 to any port "$SSH_PORT" proto tcp comment 'SSH over WireGuard only'
ufw allow in on wg0 to any port "$RDP_PORT" proto tcp comment 'RDP over WireGuard only'
install -d -m 755 /etc/blueforce /etc/iptables
if [[ ! -f "$DOCKER_ALLOWLIST" ]]; then
cat > "$DOCKER_ALLOWLIST" <<'EOF'
# Explicit Docker published-port exceptions. One reviewed entry per line:
# tcp 8443
# udp 51820
# This allowlist intentionally permits no externally published Docker ports by default.
EOF
fi
chown root:root "$DOCKER_ALLOWLIST"
chmod 640 "$DOCKER_ALLOWLIST"
install -o root -g root -m 700 "$REPO_ROOT/config/firewall/blueforce-docker-user-restore.sh" "$DOCKER_POLICY_BIN"
install -o root -g root -m 644 "$REPO_ROOT/config/systemd/blueforce-docker-firewall.service" "/etc/systemd/system/$DOCKER_POLICY_UNIT"
install -d -m 755 "$(dirname "$DOCKER_POLICY_DROPIN")"
install -o root -g root -m 644 "$REPO_ROOT/config/systemd/docker.service.d/blueforce-docker-firewall.conf" "$DOCKER_POLICY_DROPIN"
systemctl daemon-reload
systemctl enable "$DOCKER_POLICY_UNIT"
# Docker creates DOCKER-USER during startup; applying now proves the parsed policy.
systemctl start docker
"$DOCKER_POLICY_BIN"
ufw --force enable
# xRDP remains stopped until the deny-in/WireGuard-only rules are active.
systemctl enable xrdp xrdp-sesman >/dev/null 2>&1 || systemctl enable xrdp
systemctl start xrdp xrdp-sesman >/dev/null 2>&1 || systemctl start xrdp
systemctl is-enabled --quiet xrdp && systemctl is-active --quiet xrdp || { log 'FAIL: xrdp did not start after firewall baseline'; exit 1; }
log "OK: UFW allows SSH/RDP only on wg0; xrdp started only after firewall baseline; persisted Docker policy denies all unapproved published ports"