<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Patrick de Ruiter on Medium]]></title>
        <description><![CDATA[Stories by Patrick de Ruiter on Medium]]></description>
        <link>https://medium.com/@unixnerd?source=rss-697a97c58a80------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vQylnq6cK-AcaZWrMHEFcA.jpeg</url>
            <title>Stories by Patrick de Ruiter on Medium</title>
            <link>https://medium.com/@unixnerd?source=rss-697a97c58a80------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 00:30:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@unixnerd/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Eight Reasons I Switched from Traefik to HAProxy Ingress]]></title>
            <link>https://medium.com/@unixnerd/eight-reasons-i-switched-from-traefik-to-haproxy-ingress-fc00e7fd79d3?source=rss-697a97c58a80------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc00e7fd79d3</guid>
            <category><![CDATA[opentelemetry]]></category>
            <category><![CDATA[haproxy]]></category>
            <category><![CDATA[waf]]></category>
            <category><![CDATA[tempo]]></category>
            <category><![CDATA[coraza]]></category>
            <dc:creator><![CDATA[Patrick de Ruiter]]></dc:creator>
            <pubDate>Fri, 20 Nov 2026 09:00:51 GMT</pubDate>
            <atom:updated>2026-11-20T13:23:19.785Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8Bd7kz5pdxiqNY7c.jpg" /></figure><p>Traefik was my ingress controller for years, on Docker hosts first and then on every Kubernetes cluster I run. It’s a good proxy. Labels and CRDs feel natural, Let’s Encrypt is built in, and the dashboard is nice to have.</p><p>A month ago I replaced it with HAProxy on all seven clusters anyway. This post explains why, describes the setup before and after, and is honest about what I gave up.</p><p>One naming note first, because it confuses everyone. There are two HAProxy-based ingress controllers:</p><p>Both are fine. Everything below is about the first one, at controller 3.4.1 (Helm chart 1.57.1, HAProxy 3.4).</p><h3>The setup before <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#the-setup-before">#</a></h3><p>Seven single-node <a href="https://www.talos.dev/">Talos</a> clusters (production, non-production, management, security, monitoring, identity and a pilot cluster), all managed with Flux from one Git repository. Each cluster ran the same Traefik stack:</p><pre>internet / LAN | MetalLB LoadBalancer IP (externalTrafficPolicy: Local) | +-----------------------+ | Traefik | Helm chart, one per cluster | entryPoints 80/443 | HTTP -&gt; HTTPS redirect | Middlewares: | | ipAllowList | | basicAuth | | headers | | IngressRouteTCP | TLS passthrough for a few apps +-----------------------+ | | | metrics access OTLP traces | logs | +-------&gt; Alloy -&gt; Prometheus / Loki / Tempo TLS: cert-manager, Let&#39;s Encrypt DNS-01 DNS: external-dns, reading Ingress and IngressRoute objects WAF: none in the cluster</pre><p>It worked. Certificates renewed, routing was predictable, and the dashboards looked good. The gap was the WAF: there wasn’t one inside the clusters. A web application firewall is the piece I wanted most.</p><h3>The setup now <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#the-setup-now">#</a></h3><p>Same clusters, same Flux repository, same MetalLB address per cluster. Traefik is gone:</p><pre>internet / LAN | MetalLB LoadBalancer IP (the same address Traefik had) | +----------------------------------------------------------+ | haproxy-ingress pod | | | | +--------------------+ SPOP +---------------------+ | | | HAProxy 3.4 |--------&gt;| coraza-spoa | | | | Ingress controller | :9000 | OWASP CRS 4.30 | | | | | +---------------------+ | | | IngressClass | SPOP +---------------------+ | | | haproxy-waf |--------&gt;| otel-spoa |--+--&gt; Alloy -&gt; Tempo | +--------------------+ :12345 +---------------------+ | +----------------------------------------------------------+ | | | stats/metrics one log line Gatus checks the (Alloy scrapes) per request default backend (WAF verdict + trace id) TLS: cert-manager, Let&#39;s Encrypt DNS-01 (unchanged) DNS: external-dns, reading Ingress objects CI: render the chart, then `haproxy -c` on the generated config</pre><p>The controller runs with two sidecars in the same pod:</p><ul><li><strong>coraza-spoa</strong> is the <a href="https://coraza.io/">Coraza</a> WAF with the OWASP Core Rule Set. HAProxy hands each request to it over SPOE (the Stream Processing Offload Engine) and acts on the verdict.</li><li><strong>otel-spoa</strong> turns HAProxy’s events into OpenTelemetry spans and sends them to the cluster’s Alloy, which forwards them to Tempo.</li></ul><p>Both sidecars listen on 127.0.0.1 only. Nothing outside the pod can talk to them.</p><p>Each cluster switched over in one merge request: HAProxy took over Traefik’s LoadBalancer address and Traefik was removed. Each switch took about a minute. Before it, I recorded status code, redirect target and key headers for every host; after it, all 37 hosts on the first five clusters matched that baseline.</p><p>That comparison also caught the one real bug, which no lint or render check would have found. It’s under “what I gave up” below.</p><h3>The eight reasons <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#the-eight-reasons">#</a></h3><h3>1. WAF integration that doesn’t live inside the proxy <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#1-waf-integration-that-doesnt-live-inside-the-proxy">#</a></h3><p>This was the trigger. Traefik can run Coraza through its WebAssembly plugin system, and I tried it. It ran, but never reliably enough to switch it from logging to blocking. The <a href="https://github.com/jcchavezs/coraza-http-wasm-traefik">Traefik plugin</a> has had no release since version 0.3.0 in October 2024. I don’t want a WAF I can’t trust to block.</p><p>HAProxy takes a different approach. The WAF is a separate process, and HAProxy talks to it over SPOE, a protocol built for exactly this. The proxy and the WAF fail separately, scale separately, and can be upgraded separately. The Coraza SPOA release I run is from October 2026, and the Core Rule Set is current.</p><p>The wiring is a few lines of HAProxy configuration in the controller’s frontend snippet:</p><pre>option http-buffer-request filter spoe engine coraza config /etc/haproxy/spoe/coraza.cfg http-request send-spoe-group coraza coraza-req http-request deny deny_status 403 if { var(txn.coraza.action) -m found }</pre><p>The SPOE file defines what HAProxy sends to Coraza and how long it waits:</p><pre>[coraza] spoe-agent coraza-agent groups coraza-req option var-prefix coraza option set-on-error error timeout processing 500ms use-backend coraza-spoa spoe-message coraza-req args app=var(txn.coraza.app) src-ip=src src-port=src_port dst-ip=dst dst-port=dst_port method=method path=path query=query version=req.ver headers=req.hdrs body=req.body spoe-group coraza-req messages coraza-req</pre><p>My earlier post, <a href="https://blog.bsdserver.nl/securing-haproxy-with-coraza-spoa-a-step-by-step-guide/">Securing HAProxy with Coraza-SPOA</a>, covers the agent itself.</p><h3>2. WAF policy as plain HAProxy ACLs <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#2-waf-policy-as-plain-haproxy-acls">#</a></h3><p>Because the WAF verdict comes back as HAProxy variables, deciding what to do with it is ordinary HAProxy configuration. That matters more than I expected:</p><ul><li><strong>Per-application profiles.</strong> txn.coraza.app picks a Coraza profile per host. A telemetry ingest endpoint gets a relaxed profile (no body inspection); everything else gets the default.</li><li><strong>Gradual enforcement.</strong> Coraza started in DetectionOnly on every cluster. I then moved hosts to blocking one at a time, by adding them to an ACL, after their logs had been clean for a while.</li><li><strong>An explicit failure policy.</strong> If Coraza errors or times out, set-on-error sets a variable. The config decides, per host, whether that means &quot;let it through and log it&quot; or &quot;block&quot;.</li></ul><pre>acl waf_enforce_host req.hdr(host),field(1,:) -m str -i blog.example.com docs.example.com http-request set-var(txn.waf_decision) str(blocked) if waf_enforce_host { var(txn.coraza.action) -m found } http-request set-var(txn.waf_decision) str(error) if !{ var(txn.waf_decision) -m found } { var(txn.coraza.error) -m int gt 0 } http-request set-var(txn.waf_decision) str(detected) if !{ var(txn.waf_decision) -m found } { var(txn.coraza.rules_hit) -m int gt 0 } http-request deny deny_status 403 if { var(txn.waf_decision) -m str blocked }</pre><p>In Traefik a plugin is a black box with a config block. Here every decision is a line I can read, review and test.</p><h3>3. Performance <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#3-performance">#</a></h3><p>HAProxy is an event-driven, multi-threaded C proxy with a long record of doing exactly this job at very high request rates. Traefik is fast too, and on single-node clusters neither one is the bottleneck.</p><p>The difference that matters to me is where the WAF runs. A WebAssembly plugin runs inside the proxy’s own process, on every request. With SPOE, HAProxy hands the request to Coraza and moves on to other connections while it waits, with a hard timeout I set myself (500 ms). A slow inspection can’t stall the proxy.</p><p>I didn’t run a formal benchmark, so I won’t quote numbers. What I can say: since the switch, the ingress has never been the reason a page was slow.</p><h3>4. Load-balancing options and methods <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#4-load-balancing-options-and-methods">#</a></h3><p>Traefik has caught up here: version 3.7 offers weighted round robin, power-of-two-choices, consistent hashing by client IP and a least-time strategy. That covers most workloads.</p><p>HAProxy goes further, and the controller exposes it as plain Ingress annotations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PphcnVD2bFfaOgsZ.jpg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hVByRMyNROqhLpvH.jpg" /></figure><p>Limit concurrent requests per pod, queue the rest</p><p>haproxy.org/pod-maxconn, haproxy.org/timeout-queue</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R-NCPvS2oJn-joGs.jpg" /></figure><p>Active health checks with their own interval</p><p>haproxy.org/check, haproxy.org/check-http, haproxy.org/check-interval</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aSK0RpPSf1SYzqRL.jpg" /></figure><p>Rate limiting per source</p><p>haproxy.org/rate-limit-requests, haproxy.org/rate-limit-period</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cIi49MFkb62U3qvk.jpg" /></figure><p>Sticky sessions</p><p>haproxy.org/cookie-persistence</p><p>Queueing is the one I value most. With pod-maxconn, a pod that can handle 20 concurrent requests gets 20, and request 21 waits in HAProxy&#39;s queue instead of piling onto a busy pod. For a slow application that is the difference between &quot;a bit slower under load&quot; and &quot;falls over under load&quot;.</p><h3>5. Stability <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#5-stability">#</a></h3><p>Three things make HAProxy boring, in the good sense:</p><ul><li><strong>LTS branches.</strong> Even-numbered HAProxy releases (3.2, 3.4) are long-term support, maintained for five years. The controller follows them.</li><li><strong>Endpoint changes without reloads.</strong> When pods scale or move, the controller updates HAProxy’s server slots through the runtime API. Only real config changes trigger a reload, and HAProxy reloads without dropping connections.</li><li><strong>One locked combination.</strong> I pin the chart, the controller image, both sidecar images and the rule set together in a release lock. A combination is tested on the pilot cluster first, then promoted cluster by cluster to production.</li></ul><p>None of that is impossible with Traefik. It’s just the default way of working with HAProxy.</p><h3>6. Observability <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#6-observability">#</a></h3><p>Traefik’s observability is good: Prometheus metrics, access logs and OpenTelemetry tracing out of the box. I’m not claiming HAProxy is better at any single one of those.</p><p>What I gained is correlation. HAProxy’s log-format can include any variable, so one log line per request carries:</p><ul><li>the timings: time to receive the request, time in the queue, time to connect, time until the response;</li><li>the backend and server that handled it;</li><li>the WAF verdict, matched rule IDs and anomaly score;</li><li>the W3C traceparent of the request&#39;s trace.</li></ul><pre>log-format &quot;%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %tsc \&quot;%HM %[var(txn.base)] %HV\&quot; id=%ID waf=%[var(txn.waf_decision)] waf_ids=%[var(txn.coraza.rule_ids)] waf_score=%[var(txn.coraza.anomaly_score)] traceparent=%[var(txn.otel.traceparent)]&quot;</pre><p>When someone reports a 403, one Loki query shows which rule blocked them, and the trace ID takes me from the log line to the trace in Tempo. Alloy also scrapes HAProxy’s stats endpoint and the Coraza metrics port, and <a href="https://gatus.io/">Gatus</a> checks every cluster’s default backend. My <a href="https://blog.bsdserver.nl/haproxy-monitoring-with-prometheus-complete-observability-guide/">HAProxy monitoring post</a> shows the metrics side.</p><h3>7. Project maturity <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#7-project-maturity">#</a></h3><p>HAProxy has been around since 2001. Its configuration language, logging format and runtime API have stayed recognisable for more than two decades, and HAProxy Technologies maintains both the proxy and the ingress controller. The controller sees regular releases: two branches got updates in the week this post was written.</p><p>For the WAF, maturity also means the pieces are maintained. Coraza SPOA is an official Coraza project with releases this autumn, and the Core Rule Set ships signed releases. That beats a plugin that hasn’t been released in two years.</p><h3>8. One proxy everywhere <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#8-one-proxy-everywhere">#</a></h3><p>Outside Kubernetes I already ran HAProxy: on the edge, as a load balancer for DNS and for TCP services. Those setups have their own posts in the <a href="https://blog.bsdserver.nl/series/haproxy/">HAProxy series</a>.</p><p>With HAProxy as the ingress too, there’s one proxy to understand, one log format to parse, one set of metrics and one place to look when something misbehaves. A WAF rule tuned at the edge works the same inside the cluster. That reason alone wouldn’t have made me switch, but it’s why the switch felt like simplifying rather than adding.</p><h3>How I test the config <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#how-i-test-the-config">#</a></h3><p>The controller generates haproxy.cfg from Ingress objects plus my snippets, so a typo in a snippet only shows up when the pod starts. Two CI jobs catch that before merge:</p><ol><li><strong>Render.</strong> Build the Flux overlay with kustomize, merge the values the HelmRelease would get, check the chart checksum against the release lock, and run helm template. The job also asserts that images are pinned by digest and that the pod meets the restricted Pod Security Standard.</li><li><strong>Validate.</strong> Run haproxy -c against the snippets inside the exact controller image that will run them.</li></ol><p>With Traefik, a broken Middleware reference meant a 404 in production. Now a broken snippet means a red pipeline.</p><h3>What I gave up <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#what-i-gave-up">#</a></h3><p>Fair is fair. Traefik does some things better:</p><ul><li><strong>CRDs over annotations.</strong> Traefik’s Middleware objects are reusable and composable. HAProxy annotations live on a backend, so two Ingresses that share a service port also share its auth and allow-list settings. The workaround is haproxy.org/standalone-backend: &quot;true&quot;, which gives an Ingress its own backend. That works, but you need to know it exists.</li><li><strong>A dashboard.</strong> Traefik’s dashboard shows routers and services at a glance. HAProxy has a stats page; Grafana does the rest.</li><li><strong>Redirect surprise.</strong> The controller put its internal HTTPS port (8443) into the HTTP-to-HTTPS redirect. Clients got sent to https://host:8443/, a port the LoadBalancer doesn&#39;t expose. The fix is one line in the controller config, ssl-redirect-port: &quot;443&quot;, but every cluster shipped with the bug for three days. Only the per-host before-and-after comparison caught it.</li><li><strong>Learning curve.</strong> HAProxy’s config language is powerful and terse. ACLs, fetches and converters take time to learn, and a snippet you don’t understand is a liability.</li></ul><h3>Should you switch? <a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/#should-you-switch">#</a></h3><ul><li><strong>Switch</strong> if you want a WAF you can enforce, need queueing or fine-grained load balancing, or already run HAProxy elsewhere.</li><li><strong>Stay on Traefik</strong> if your clusters are small, you don’t need a WAF in the cluster, and Middlewares and the dashboard make your life easier. It’s a good proxy; it just stopped being the right one for me.</li></ul><p><em>Originally published at </em><a href="https://blog.bsdserver.nl/why-i-switched-from-traefik-to-haproxy-ingress/"><em>https://blog.bsdserver.nl</em></a><em> on November 20, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc00e7fd79d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Default-Deny NAT Firewall on OpenBSD pf, Managed with Terraform]]></title>
            <link>https://medium.com/@unixnerd/a-default-deny-nat-firewall-on-openbsd-pf-managed-with-terraform-de730f6b0a43?source=rss-697a97c58a80------2</link>
            <guid isPermaLink="false">https://medium.com/p/de730f6b0a43</guid>
            <category><![CDATA[pf]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[openbsd]]></category>
            <category><![CDATA[firewall]]></category>
            <dc:creator><![CDATA[Patrick de Ruiter]]></dc:creator>
            <pubDate>Fri, 20 Nov 2026 00:22:00 GMT</pubDate>
            <atom:updated>2026-11-20T12:46:01.902Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4YZCE6zmKb5NtUhr.jpg" /></figure><p>pf is the best firewall I have worked with: one readable file, atomic reloads, NAT and queueing in the same language as the filter rules. What it lacks is an API. Every tool that wants to change a pf firewall ends up templating pf.conf, copying it over SSH and hoping pfctl -f likes it.</p><p>I wrote <a href="https://github.com/pderuiter/pfapid">pfapid</a> to fix that: a small REST API for pf that runs on the firewall itself, with a <a href="https://registry.terraform.io/providers/pderuiter/pf/latest">Terraform provider</a> on top. This post builds a complete NAT firewall with it: a default-deny base, outbound NAT, a port forward to a DMZ web server, a blocklist and QoS on the uplink.</p><h3>What pfapid manages, and what it doesn’t <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#what-pfapid-manages-and-what-it-doesnt">#</a></h3><p>pfapid splits the firewall into two owners:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Wom04DHB4M47yZsg.jpg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IRsoxaPOC89zJe48.jpg" /></figure><p>Defaults, block all, antispoof, management access</p><p>You, by hand or config management</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CKaGbxnSn4hj6UY7.jpg" /></figure><p>Filter and NAT rules, per unit of work</p><p>Terraform via pfapid</p><p>Anchors api/&lt;name&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qIRTaf_bMkJkM9C3.jpg" /></figure><p>Contents of tables such as &lt;blocklist&gt;</p><p>Terraform via pfapid</p><p>Tables declared in pf.conf</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QD6xGtXuNqrqkQYK.jpg" /></figure><p>HFSC queues and the rules that classify into them</p><p>Terraform via pfapid</p><p>The api-qos anchor and a queue include</p><p>The base ruleset is the part you can’t afford to get wrong, so the API can’t touch it. Whatever Terraform does, the block and management rules in pf.conf come first.</p><h3>Why not just template pf.conf? <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#why-not-just-template-pfconf">#</a></h3><ul><li><strong>Commit-confirm.</strong> Every change is loaded, then rolled back by the firewall itself unless the provider confirms it within confirm_timeout. If an apply cuts off the path between Terraform and the firewall, the change undoes itself.</li><li><strong>Drift detection.</strong> Someone runs pfctl by hand at 3 a.m.? The next plan warns, and the next apply puts the rules back.</li><li><strong>Small blast radius.</strong> pfapid runs privilege-separated: the HTTPS side runs as _pfapid under pledge(&quot;stdio inet&quot;), and only a tiny root process runs pfctl. Clients authenticate with mTLS and are scoped to anchors and tables. There is no raw pf.conf passthrough: rules are typed objects, rendered and checked before pf sees them.</li></ul><h3>When to use it <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#when-to-use-it">#</a></h3><ul><li><strong>Use it</strong> when firewall rules belong to the same change as the thing they protect: a new service, a Kubernetes cluster, a customer network. They get reviewed and planned together.</li><li><strong>Skip it</strong> for a firewall nobody changes, or one with a single admin who is happy with vi /etc/pf.conf. A text file with good comments is hard to beat.</li></ul><h3>The network <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#the-network">#</a></h3><pre>internet | wan vio0 203.0.113.2/24, 2001:db8:1::2/64 (egress) +----------------------+ mgmt | | lan vio1 10.0.10.1/24, 2001:db8:10::1/64 vio3 ----+ OpenBSD + pfapid +------------- office clients, VoIP phones 192.168. | | 100.1/24 | | dmz vio2 10.0.20.1/24, 2001:db8:20::1/64 +----------------------+------------- web server 10.0.20.10, 2001:db8:20::10</pre><p>The rules use <strong>interface groups</strong> ( wan, lan, dmz, mgmt), never device names. When you replace a NIC or move a network to a VLAN, you add the new interface to the group and no rule changes:</p><pre># /etc/hostname.vio0 and friends: one &quot;group&quot; line each ifconfig vio0 group wan</pre><p>What we want:</p><h3>Step 1: Install pfapid <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-1-install-pfapid">#</a></h3><p>pfapid ships as signed release archives for OpenBSD amd64 and arm64. Check the signature before you install it:</p><p>gpg must report a good signature from key 10A2F1058821D55C61E45C7EE97C02FEE2F51B23. Then on the firewall, as root:</p><p>The three empty files must exist before pf.conf includes them; pfapid fills them later.</p><p>pfapid has a built-in CA, so you don’t need a PKI to get started. Create the CA and the server certificate, then a client certificate for Terraform:</p><pre>pfapid pki init -dns fw.example.net -ip 192.168.100.1 pfapid pki issue -name tf-firewall -out /root/tf-firewall # writes tf-firewall.crt and .key</pre><p>Copy /root/tf-firewall.crt, /root/tf-firewall.key and the CA certificate (/etc/pfapid/pki/ca.crt) to the machine that runs Terraform, and delete the key from the firewall. A company CA, Vault or SPIFFE IDs work too; see <a href="https://github.com/pderuiter/pfapid/blob/main/docs/CERTIFICATES.md">CERTIFICATES.md</a>.</p><h3>Step 2: The default-deny base ruleset <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-2-the-default-deny-base-ruleset">#</a></h3><p>This file is yours. It holds everything that must hold no matter what the API does:</p><pre># /etc/pf.conf: default-deny base ruleset for a pfapid-managed NAT firewall. # # wan vio0 (egress) 203.0.113.2/24, 2001:db8:1::2/64 # lan vio1 10.0.10.1/24, 2001:db8:10::1/64 # dmz vio2 10.0.20.1/24, 2001:db8:20::1/64 # mgmt vio3 192.168.100.1/24 admin_nets = &quot;192.168.100.0/24&quot; set skip on lo set block-policy drop set loginterface egress # Tables whose contents pfapid manages: declared empty, with persist, # so a reload of this file keeps what Terraform put in them. table &lt;dmz_web&gt; persist table &lt;blocklist&gt; persist # Default deny: nothing passes, in or out, unless a rule says so. block log all # Packets on lan or dmz must come from that network. antispoof quick for { lan dmz } # Management first, and quick: nothing in the API anchors can take it away. pass in quick on mgmt proto tcp from $admin_nets to (mgmt) port { 22 8443 } # IPv6 breaks without these: neighbour discovery and path MTU discovery. pass quick inet6 proto icmp6 icmp6-type { unreach toobig timex paramprob \ routersol routeradv neighbrsol neighbradv } # The firewall offers no services to the inside networks. block in quick on { lan dmz } to self # The firewall&#39;s own traffic (DNS, NTP, syspatch). pass out quick on egress from (egress) # pfapid: QoS classification, then the API anchors. include &quot;/etc/pf.anchors/api-queues.conf&quot; anchor &quot;api-qos&quot; load anchor &quot;api-qos&quot; from &quot;/etc/pf.anchors/api-qos.conf&quot; anchor &quot;api/*&quot; include &quot;/etc/pf.anchors/api.conf&quot;</pre><p>Block by block:</p><ul><li><strong>block log all</strong> is the default-deny. pf uses the <em>last</em> matching rule, so every pass further down (and in the anchors) can open a hole, but nothing is open by default. It applies in both directions, which matters for forwarded traffic later.</li><li><strong>antispoof</strong> drops packets on lan or dmz with a source address that doesn&#39;t belong there.</li><li><strong>Management</strong> comes before the anchors and is quick: pf stops evaluating on the first quick match, so no anchor can override it. pfapid also refuses the mgmt group in any API rule.</li><li><strong>ICMPv6</strong> for neighbour discovery and path MTU. Without it, IPv6 breaks in slow, confusing ways. pfapid won’t let the API block these either.</li><li><strong>The pfapid hooks</strong>: the queue include and the api-qos anchor come before anchor &quot;api/*&quot;, so traffic is classified before an API rule passes it. api.conf is generated by pfapid with one load anchor line per anchor, so the anchors survive a reboot.</li></ul><p>Check the file, then load it with a way back. I locked myself out of my lab firewall while writing this post: my workstation wasn’t in admin_nets. A background job that restores the old file after 90 seconds unless I cancel it brought the firewall back on its own:</p><p>The test has to be a <em>new</em> connection: existing SSH sessions keep their state and survive a rule change that would lock out the next login.</p><h3>Step 3: Tell pfapid what it may touch <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-3-tell-pfapid-what-it-may-touch">#</a></h3><p>/etc/pfapid.json (mode 0600, root) lists the interface groups, tables and queue devices the API may use, and which client may do what:</p><ul><li>must also be declared in pf.conf, empty and with persist.</li><li><strong>queue_interfaces</strong> are physical devices: pf can&#39;t hang a queue on a group.</li><li><strong>internal_nets</strong>: pfapid refuses NAT <em>between</em> two of these networks, a common mistake that hides where traffic really comes from. NAT from the LAN to the internet is fine.</li><li>: tf-firewall (the certificate&#39;s CN) may manage every anchor, these two tables and QoS. A team that only owns its own service would get &quot;anchors&quot;: [&quot;webshop-*&quot;] and nothing else.</li></ul><p>Start it:</p><pre>pfapid -f /etc/pfapid.json -n # checks the configuration and certificates rcctl start pfapid</pre><h3>Step 4: The Terraform provider <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-4-the-terraform-provider">#</a></h3><p>The provider is signed with the same key as the pfapid releases; terraform init checks that for you.</p><h3>Step 5: Tables <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-5-tables">#</a></h3><p>Tables are lists of addresses that rules refer to as &lt;name&gt;. Changing a table never reloads a rule, so it&#39;s the cheap way to add a web server or block a network. A table can hold IPv4 and IPv6 entries side by side:</p><h3>Step 6: Filtering and outbound NAT <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-6-filtering-and-outbound-nat">#</a></h3><p>Each pf_anchor is one anchor, api/&lt;name&gt;, and its rules are loaded as a whole: pf is order-sensitive, so a ruleset is one atomic unit, never rule by rule. Split anchors along ownership: whoever owns the DMZ web server owns dmz-web.</p><p>Things worth knowing:</p><ul><li><strong>Every rule names one family.</strong> pfapid refuses a rule that mixes IPv4 and IPv6: pfctl would silently turn it into two rules, and what&#39;s loaded would no longer match what you wrote. Tables are the way to cover both.</li><li><strong>Forwarded traffic is filtered twice.</strong> A packet from the LAN to the internet goes <em>in</em> on lan and <em>out</em> on wan, and with block all both directions need a pass. The lan-in rule allows everything in, but the out rules decide where it can go: only out on wan. A LAN client trying to reach the DMZ gets in on lan and is then dropped going out on dmz, unless a rule there allows it (the next anchor does, for the web ports).</li><li><strong>IPv6 isn’t translated.</strong> The LAN has a routed prefix, so the IPv6 rule just passes it.</li></ul><h3>Step 7: Port forwarding to the DMZ <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-7-port-forwarding-to-the-dmz">#</a></h3><p>The rdr-to rule changes the destination of connections to the wan address on port 80 or 443 to 10.0.20.10. The same pass rule creates the state, so there is no separate redirect-then-filter step to keep in sync. The redirected packet then leaves on dmz, and the web-out rules let it through: forget those and the port forward silently goes nowhere. Those same rules let LAN clients reach the web server directly.</p><p>For IPv6 the server has its own address, so there is nothing to translate: pass it in on wan and out on dmz.</p><h3>Step 8: The blocklist <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-8-the-blocklist">#</a></h3><p>quick ends evaluation on a match, so these blocks win over every pass rule in every anchor, whatever order pf evaluates the anchors in. log sends the dropped packets to pflog0.</p><p>Feed the table from wherever your threat data lives: a variable, a file, or a data source. Updating it is a table replace on the firewall, not a ruleset reload.</p><p>An upload that fills the uplink makes everything else on the line feel slow: VoIP breaks up, SSH lags, and downloads crawl because their ACKs queue behind the upload. HFSC queues fix that by sharing the uplink by rule:</p><p>How it fits together:</p><ul><li><strong>Queues shape what <em>leaves</em> an interface.</strong> These queues sit on the wan device, so they shape uploads. To shape downloads, put a second tree on the LAN device.</li><li><strong>The parent caps the total</strong> at 100M with max; set it slightly below your real upload speed so the queue builds here, where pf controls it, not in the modem. Children share it by bandwidth, min is a guarantee, max a hard cap. pfapid refuses children that add up to more than their parent; pfctl would accept that.</li><li><strong>Two queues in one rule</strong>: queue = { name = &quot;std&quot;, priority = &quot;interactive&quot; } puts data in std and bare TCP ACKs in interactive. That one line is what keeps downloads fast during a big upload.</li><li><strong>prio</strong> is pf&#39;s simple 0-7 priority on interfaces without HFSC queues, such as lan here: VoIP towards the phones goes first too.</li><li><strong>QoS rules can only classify.</strong> They’re match rules with no pass, block or NAT, so QoS can never change what the firewall lets through. That&#39;s also why it&#39;s a separate permission (&quot;qos&quot;: true).</li></ul><h3>Step 10: Plan and apply <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-10-plan-and-apply">#</a></h3><p>Each anchor’s plan includes pf_rules: the rules exactly as pf will load them, as pfctl -nv on the firewall printed them. This is what the plan showed in my lab (the QoS root was on vio1 there):</p><pre>pf_anchor.dmz_web: &quot;pass in on wan inet proto tcp from any to (wan) port = 80 flags S/SA label \&quot;web-rdr\&quot; rdr-to 10.0.20.10&quot;, &quot;pass in on wan inet proto tcp from any to (wan) port = 443 flags S/SA label \&quot;web-rdr\&quot; rdr-to 10.0.20.10&quot;, &quot;pass in on wan inet6 proto tcp from any to &lt;dmz_web&gt; port = 80 flags S/SA label \&quot;web-in\&quot;&quot;, ... pf_anchor.egress: &quot;pass out on wan inet from 10.0.10.0/24 to any flags S/SA label \&quot;nat-out\&quot; nat-to (wan:0) round-robin&quot;, &quot;pass out on wan inet from 10.0.20.0/24 to any flags S/SA label \&quot;nat-out\&quot; nat-to (wan:0) round-robin&quot;, ... pf_qos.uplink: &quot;queue uplink on vio1 bandwidth 100M, max 100M&quot;, &quot;queue std parent uplink flows 1024 bandwidth 60M default&quot;, ... &quot;match in on lan inet proto tcp all set (queue(std, interactive)) label \&quot;qos-acks\&quot;&quot;,</pre><p>Lists become one line per port and per prefix, and pf adds its defaults ( flags S/SA, round-robin). You see that before anything changes, not after.</p><pre>pf_table.dmz_web: Creation complete after 0s [id=dmz_web] pf_table.blocklist: Creation complete after 0s [id=blocklist] pf_anchor.egress: Creation complete after 0s [id=egress] pf_qos.uplink: Creation complete after 0s [id=qos] pf_anchor.dmz_web: Creation complete after 0s [id=dmz-web] pf_anchor.blocklist: Creation complete after 0s [id=blocklist] Apply complete! Resources: 6 added, 0 changed, 0 destroyed.</pre><p>Each change was loaded, confirmed by the provider over the same connection, and only then written to pfapid’s state. A second terraform plan reports no changes.</p><h3>Step 11: Check it on the firewall <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#step-11-check-it-on-the-firewall">#</a></h3><h3>The guardrails in action <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#the-guardrails-in-action">#</a></h3><p><strong>Drift.</strong> I flushed an anchor by hand with pfctl -a api/dmz-web -F rules. The next plan:</p><pre>Warning: pf anchor api/dmz-web differs from its committed rules The rules loaded in pf were changed outside pfapid. The next apply loads the configured rules again. Expected: pass in on wan inet proto tcp from any to (wan) port = 80 flags S/SA label &quot;web-rdr&quot; rdr-to 10.0.20.10 ... Plan: 0 to add, 1 to change, 0 to destroy.</pre><p>terraform apply loaded the rules again.</p><p><strong>The management interface.</strong> A rule that opens RDP on mgmt fails at plan time:</p><pre>Error: Rules rejected by the firewall pfapid: 400 invalid: rule 0: invalid: interface &quot;mgmt&quot; is not allowed</pre><p><strong>A rule pf won’t take.</strong> pfapid runs pfctl -n during the plan, so a rule pf would refuse fails terraform plan, with pf&#39;s own error message, before anything is loaded.</p><h3>Limits worth knowing <a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/#limits-worth-knowing">#</a></h3><ul><li><strong>One firewall, one provider block.</strong> For an HA pair, configure two providers (with alias) and apply the same modules to both; pfsync keeps the states in sync, not the rules.</li><li><strong>No hairpin NAT.</strong> LAN clients reach the web server on its DMZ address, not on the public one. Use split DNS.</li><li><strong>Queues are bit rates, not percentages</strong>: pf doesn’t support percentages in HFSC yet.</li></ul><p>Everything in this post was planned and applied with provider v0.1.0 and pfapid v0.1.2 on an OpenBSD 7.9 lab VM, and the pf output shown is what pf loaded there. The code, the design notes and the pf behaviour pfapid relies on are on GitHub: <a href="https://github.com/pderuiter/pfapid">pfapid</a> and <a href="https://github.com/pderuiter/terraform-provider-pf">terraform-provider-pf</a>. Issues and pull requests are welcome.</p><p><strong>Patrick de Ruiter</strong> runs <a href="https://webuildyourcloud.io/">We Build Your Cloud</a>, building and managing infrastructure for small and growing companies. Questions about this post? Reply to the newsletter or find me on <a href="https://www.linkedin.com/company/wbyc/">LinkedIn</a>.</p><p>Newsletter</p><h3>Get new posts by email</h3><p>One mail when a new article goes live: what it covers and a link. No tracking pixels, unsubscribe in one click.</p><p><em>Originally published at </em><a href="https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/"><em>https://blog.bsdserver.nl</em></a><em> on November 20, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de730f6b0a43" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>