Skip to main content

Update Hetzner Cloud firewall rules with Cloudflare IP ranges

Project description

Update Hetzner Cloud Firewall Rules with Current Cloudflare IP Ranges

codecov OpenSSF Best Practices OpenSSF Scorecard CodeQL Python package Docker build PyPI - Version PyPI - Python Version

This tool, cf-ips-to-hcloud-fw, helps you keep your Hetzner Cloud firewall rules up-to-date with the current Cloudflare IP ranges.

Table of Contents

Overview

cf-ips-to-hcloud-fw fetches the current Cloudflare IP ranges and updates your Hetzner Cloud firewall rules using the hcloud API.

The tool specifically targets incoming firewall rules and replaces the networks with Cloudflare networks if their description contains __CLOUDFLARE_IPS_V4__, __CLOUDFLARE_IPS_V6__ or __CLOUDFLARE_IPS__.

Text in rule description Cloudflare IP ranges
__CLOUDFLARE_IPS_V4__ IPv4 only
__CLOUDFLARE_IPS_V6__ IPv6 only
__CLOUDFLARE_IPS__ IPv4 + IPv6

Note: Having both __CLOUDFLARE_IPS_V4__ and __CLOUDFLARE_IPS_V6__ in a rule description is equivalent to having __CLOUDFLARE_IPS__ there.

Installation

Using Python

To install cf-ips-to-hcloud-fw using Python, we recommend using pipx. pipx is a tool for installing and running Python applications in isolated environments.

Using pipx (Recommended)

  1. Install cf-ips-to-hcloud-fw using pipx:

    pipx install cf-ips-to-hcloud-fw
    
  2. Verify the installation:

    cf-ips-to-hcloud-fw -h
    

You should see the usage information for cf-ips-to-hcloud-fw.

To upgrade cf-ips-to-hcloud-fw, run:

[!TIP] To upgrade cf-ips-to-hcloud-fw, run pipx upgrade cf-ips-to-hcloud-fw.

Using pip

We strongly recommend using a virtual environment when installing Python packages with pip. This helps to avoid conflicts between packages and allows you to manage packages on a per-project basis.

  1. Create a virtual environment:

    python3 -m venv cf-ips-to-hcloud-fw-venv
    
  2. Install cf-ips-to-hcloud-fw into the virtual environment:

    ./cf-ips-to-hcloud-fw-venv/bin/pip3 install cf-ips-to-hcloud-fw
    
  3. Verify the installation:

    ./cf-ips-to-hcloud-fw-venv/bin/cf-ips-to-hcloud-fw -h
    

You should see the usage information for cf-ips-to-hcloud-fw.

[!TIP] To upgrade cf-ips-to-hcloud-fw in your virtual environment, run ./cf-ips-to-hcloud-fw-venv/bin/pip3 install --upgrade cf-ips-to-hcloud-fw.

Docker and Kubernetes

As an alternative, cf-ips-to-hcloud-fw can be run using Docker or a Kubernetes CronJob. Simply mount your configuration file as /usr/src/app/config.yaml.

Here's an example using Docker:

docker run --rm \
  --mount type=bind,source=$(pwd)/config.yaml,target=/usr/src/app/config.yaml,readonly \
  jkreileder/cf-ips-to-hcloud-fw:1.0.17

(Add --pull=always if you use a rolling image tag.)

Docker images for cf-ips-to-hcloud-fw are available for both linux/amd64 and linux/arm64 architectures. The Docker images support the following tags:

  • 1: This tag always points to the latest 1.x.x release.
  • 1.0: This tag always points to the latest 1.0.x release.
  • 1.0.17: This tag points to the specific 1.0.17 release.
  • main: This tag points to the most recent development version of cf-ips-to-hcloud-fw. Use this at your own risk as it may contain unstable changes.

You can find the Docker images at:

Here's an example of how to create a Kubernetes Secret for your configuration:

apiVersion: v1
kind: Secret
metadata:
  name: cf-ips-to-hcloud-fw-config
type: Opaque
stringData:
  config.yaml: |
    - token: API_TOKEN_FOR_PROJECT_1
      firewalls:
        - firewall-1
        - firewall-2
    - token: API_TOKEN_FOR_PROJECT_2
      firewalls:
        - default

And here's an example of a Kubernetes CronJob that uses the Secret:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: cf-ips-to-hcloud-fw
spec:
  schedule: "0 * * * *" # Run every hour
  jobTemplate:
    spec:
      template:
        spec:
          securityContext:
            runAsNonRoot: true
            runAsUser: 65534
          containers:
            - name: cf-ips-to-hcloud-fw
              image: jkreileder/cf-ips-to-hcloud-fw:1.0.17
              # imagePullPolicy: Always # Uncomment this if you use a rolling image tag
              securityContext:
                allowPrivilegeEscalation: false
                readOnlyRootFilesystem: true
                capabilities:
                  drop:
                    - ALL
              volumeMounts:
                - name: config-volume
                  mountPath: /usr/src/app/config.yaml
                  subPath: config.yaml
          volumes:
            - name: config-volume
              secret:
                secretName: cf-ips-to-hcloud-fw-config
          restartPolicy: OnFailure

Configuration

Preparing the Hetzner Cloud Firewall

To prepare your Hetzner Cloud Firewall:

  1. Set the rule descriptions: Include __CLOUDFLARE_IPS_V4__, __CLOUDFLARE_IPS_V6__, or __CLOUDFLARE_IPS__ in the description of any incoming firewall rule where you want to insert Cloudflare networks. This will be used as a marker to identify which rules should be updated with the Cloudflare IP ranges.

  2. Generate an API token: You'll need an API token with write permissions for the project that contains the firewall. This token will be used to authenticate your requests to the Hetzner Cloud API. You can generate a token in the Hetzner Cloud Console by going to "Security" > "API Tokens" > "Generate API Token".

Configuring the Application

To configure the application, you'll need to create a config.yaml file with your API tokens and the names of the firewalls you want to update:

- token: API_TOKEN_FOR_PROJECT_1 # Token with read-write permissions for a Hetzner Cloud project
  firewalls:
    - firewall-1
    - firewall-2
- token: API_TOKEN_FOR_PROJECT_2 # Token with read-write permissions for another Hetzner Cloud project
  firewalls:
    - default

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cf_ips_to_hcloud_fw-1.0.17.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cf_ips_to_hcloud_fw-1.0.17-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file cf_ips_to_hcloud_fw-1.0.17.tar.gz.

File metadata

  • Download URL: cf_ips_to_hcloud_fw-1.0.17.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cf_ips_to_hcloud_fw-1.0.17.tar.gz
Algorithm Hash digest
SHA256 6208b58586a7f19e16b0b490fbe08a8ec65b00759ffae6bfcf5d36ce5452ce08
MD5 a479bb3f29c38642dfd56fceebf1d5a1
BLAKE2b-256 eb501132f254849261af381d7c6bf93ba07a2b1c993455e29d96a887395cdaa9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cf_ips_to_hcloud_fw-1.0.17.tar.gz:

Publisher: python-package.yaml on jkreileder/cf-ips-to-hcloud-fw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cf_ips_to_hcloud_fw-1.0.17-py3-none-any.whl.

File metadata

File hashes

Hashes for cf_ips_to_hcloud_fw-1.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 e7ad8d6ec71a9843055e3d6fa2783d3da5654773cc92ccfcb3a02e30209983c3
MD5 9ddd456f4da464591d7aa5d4d330e7b1
BLAKE2b-256 1728db957a0260fa09818efb3a2e18e9703ea26eef02e2908500379231f3e88a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cf_ips_to_hcloud_fw-1.0.17-py3-none-any.whl:

Publisher: python-package.yaml on jkreileder/cf-ips-to-hcloud-fw

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page