Skip to content

Add firewalld-cloudflare-http RPM package for HTTP/HTTPS filtering#1

Merged
39ff merged 6 commits intomainfrom
claude/cloudflare-firewall-rpm-l4rwT
Feb 3, 2026
Merged

Add firewalld-cloudflare-http RPM package for HTTP/HTTPS filtering#1
39ff merged 6 commits intomainfrom
claude/cloudflare-firewall-rpm-l4rwT

Conversation

@39ff
Copy link
Owner

@39ff 39ff commented Feb 3, 2026

Summary

This PR introduces a complete RPM package that manages firewalld rules to restrict HTTP/HTTPS traffic to only Cloudflare IP addresses. The package automatically fetches and maintains Cloudflare's public IP ranges via a weekly systemd timer.

Key Changes

  • Build system: Added Makefile for building tarball, SRPM, and RPM packages
  • RPM spec file: Created firewalld-cloudflare-http.spec with proper packaging metadata, dependencies, and lifecycle hooks
  • Main script: Implemented firewalld-cloudflare-http-update bash script that:
    • Fetches Cloudflare IPv4 and IPv6 ranges from official sources
    • Creates/updates firewalld ipsets (cloudflare-ipv4, cloudflare-ipv6)
    • Manages rich rules to allow HTTP/HTTPS only from Cloudflare IPs
    • Provides update, setup, and remove commands
  • Systemd integration: Added service and timer units for weekly automatic IP list updates
  • Documentation: Comprehensive README with installation, usage, and troubleshooting instructions
  • Build artifacts: Added .gitignore for build outputs

Notable Implementation Details

  • Input validation: IP ranges are validated as proper CIDR notation before being written to ipsets
  • Error handling: Script uses set -euo pipefail and includes proper error checking with informative messages
  • Idempotent operations: Rich rules are only added if they don't already exist
  • Graceful degradation: Post-install setup continues even if firewalld isn't running
  • Clean uninstall: Pre-uninstall hooks remove all created rules and ipsets
  • Flexible zone support: Commands accept optional zone parameter, defaulting to firewalld's default zone
  • Logging: All operations are timestamped and logged for auditability

The package is architecture-independent (noarch) and requires firewalld, curl, and systemd as dependencies.

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg

Summary by CodeRabbit

  • 新機能

    • RPM配布対応、インストール時の自動設定、週次タイマーとワンショット更新サービスを追加。CloudflareのIPリストを自動取得してfirewalldのipsetとHTTP/HTTPS許可ルールへ反映するCLI(更新・設定・削除)を追加。
  • ドキュメント

    • インストール・動作・確認手順を含むREADMEを追加。
  • テスト

    • ユニットおよび統合テスト(ネットワーク名前空間での接続検証含む)を追加。
  • CI/CD

    • ビルド・テスト・パッケージ・リリースを行うワークフローを追加。
  • 雑多

    • .gitignore、Makefile、RPM spec を追加。

- Update script fetches Cloudflare IP ranges and manages firewalld ipsets
- Rich rules allow HTTP/HTTPS (80/443) only from Cloudflare IPs
- systemd timer for weekly automatic IP list updates
- RPM spec with proper post/preun scriptlets for setup and cleanup
- Makefile for building tarball and RPM

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
- Build RPM on push/PR to main using Fedora container
- Upload RPM and SRPM as artifacts
- Auto-create GitHub Release with RPM assets on tag push (v*)

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
@coderabbitai
Copy link

coderabbitai bot commented Feb 3, 2026

Walkthrough

RPMパッケージ、CIワークフロー、Cloudflare IP を反映する更新スクリプト、systemd ユニット/タイマー、単体・統合テスト、README とビルド用ファイルを追加する変更。

Changes

Cohort / File(s) Summary
CI / ワークフロー
/.github/workflows/build.yml
「Build & Test」GitHub Actions ワークフロー追加:unit/integration テスト、複数ディストロの RPM ビルド(matrix)、アーティファクト upload/download、タグ時の Release 作成を定義。
ビルド / パッケージング
Makefile, firewalld-cloudflare-http.spec
Makefile に tarball/srpm/rpm/clean ターゲットを追加。spec は noarch パッケージ定義、依存(firewalld,curl,systemd)、インストールパス、%post/%preun/%postun と systemd ユニット/タイマー取り扱いを追加。
実行スクリプト
src/firewalld-cloudflare-http-update
Cloudflare IPv4/IPv6 リスト取得・CIDR 検証・/etc/firewalld/ipsets へ ipset XML 出力、rich rule の setup/update/remove を行う CLI(update/setup/remove/help)を追加。冪等性とエラーチェックあり。
systemd ユニット/タイマー
src/firewalld-cloudflare-http-update.service, src/firewalld-cloudflare-http-update.timer
oneshot サービス(ExecStart: update)と週次タイマー(OnCalendar=weekly、RandomizedDelaySec=3600、Persistent=true)を追加。network-online と firewalld 依存を指定。
テスト
tests/test_unit.sh, tests/test_integration.sh
unit テストで XML 生成・CIDR 検証・fetch_ips のモック検証を追加。integration テストはネットワーク名前空間を用いて Cloudflare からの HTTP 接続のみ許可されることを検証し、クリーンアップ処理を含む。
ドキュメント / メタ
README.md, .gitignore
README にインストール/動作/ビルド/アンインストール手順とライセンスを追加。.gitignore に *.tar.gz, *.rpm, firewalld-cloudflare-http-*/ を追加。

Sequence Diagram(s)

sequenceDiagram
    rect rgba(200,230,201,0.5)
    participant Dev as 開発者
    participant GH as GitHub Actions
    participant Art as アーティファクト
    end
    rect rgba(187,222,251,0.5)
    participant Fedora as Fedora コンテナ
    participant Build as rpmbuild / Makefile
    end

    Dev->>GH: push / PR / tag(v*)
    GH->>GH: unit-test ジョブ実行
    GH->>GH: integration-test ジョブ実行
    GH->>Fedora: build ジョブ(依存: unit-test)
    Fedora->>Build: make rpm / rpmbuild 実行
    Build->>Art: RPM と SRPM をアップロード
    GH->>GH: tag の場合 release ジョブが Art をダウンロードして Release 作成
Loading
sequenceDiagram
    rect rgba(255,224,178,0.5)
    participant Timer as systemd.timer
    participant Service as systemd.service
    participant Script as update スクリプト
    participant CF as Cloudflare API
    participant FW as firewalld
    end

    Timer->>Service: 週次トリガー(RandomizedDelaySec)
    Service->>Script: ExecStart: update
    Script->>CF: IPv4 リスト取得 (curl)
    CF-->>Script: IPv4 CIDR リスト
    Script->>CF: IPv6 リスト取得 (curl)
    CF-->>Script: IPv6 CIDR リスト
    Script->>Script: ipset XML を生成 (/etc/firewalld/ipsets)
    Script->>FW: firewall-cmd で ipset/rich rules 更新および reload
    Service-->>Timer: 実行完了
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 分

ウサギの詩

ぴょんとビルドして、パッケージを抱え 🐇
毎週そっと網を直し、IPを並べる風🌬️
systemd が時を刻み、firewalld が門を守る
小さなスクリプトは静かに道をひらく🥕


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands and usage tips.

- Unit tests: XML generation, CIDR validation, fetch_ips with mock curl
- Integration tests: network namespace-based connectivity verification
  - Simulates Cloudflare IP (173.245.48.2) -> HTTP allowed
  - Simulates non-Cloudflare IP (198.51.100.2) -> HTTP blocked
- GitHub Actions workflow updated with test jobs
- Make update script variables overridable via env for testability

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
- Add BuildRequires: systemd-rpm-macros so %{_unitdir} macro is defined
- Install systemd-rpm-macros in CI build step
- Fix changelog date: Feb 03 2026 is Tuesday, not Monday

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
firewalld defaults ipset family to inet (IPv4). Without explicit
family="inet6", adding a rich rule with family="ipv6" referencing
the ipset fails with INVALID_RULE: Source address family conflict.

- write_ipset_xml now takes a family parameter (inet/inet6)
- IPv6 ipsets include <option name="family" value="inet6"/>
- Unit tests verify family option presence/absence
- Integration test updated to use new calling convention

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
Fedora-built RPMs may not work on EL8 (zstd compression unsupported)
and carry wrong dist tags. Build natively on each target distro:

- fedora:latest  -> .fc* RPM
- rockylinux:8   -> .el8 RPM
- rockylinux:9   -> .el9 RPM
- almalinux:9    -> .el9 RPM

All distro RPMs are uploaded as artifacts and attached to GitHub
Releases on tag push.

https://claude.ai/code/session_01BNCMS3DQ4UJAELZsGytzwg
@39ff 39ff merged commit 1f636d1 into main Feb 3, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants