internal/stats samples every minute what the kernel already counts: the
interfaces' counters (per zone too), Housecarl's table (hits per rule,
built-in and policy; drops by chain; quotas; per-device sets), the
connection tracking table, and housecarld's and the host's memory, CPU
and load. Logged drops feed top lists of sources and what they tried.
Aggregates go to a database of their own (stats.db; per minute 7 days,
per hour 90 days, per day 2 years), with the store and queries from
Jarl's.
Traffic per inside device is a setting (stats_per_device): counting sets
stats_up/down_v4/v6 and rules at the top of the forward chain, IPv6 per
/64, carried across reloads. Set names starting with meter_, conn_,
quota_ or stats_ are reserved (set_name_reserved).
GET /stats/{status,summary,timeline,top,hits,heatmap}, hc stats, a
Statistics page with Jarl's charts, two charts on the dashboard, and a
conntrack_full warning. e2e: 188 checks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
9.8 KiB
Go
260 lines
9.8 KiB
Go
package nft
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
|
|
"housecarl/internal/model"
|
|
"housecarl/internal/render"
|
|
)
|
|
|
|
func TestParseTable(t *testing.T) {
|
|
data := []byte(`{"nftables": [{"metainfo": {"version": "1.1.3"}},
|
|
{"set": {"family": "inet", "name": "guests_v4", "table": "housecarl", "type": "ipv4_addr", "flags": ["timeout"],
|
|
"elem": [{"elem": {"val": "10.0.30.5", "timeout": 1800, "expires": 1799}}, "10.0.30.6"]}},
|
|
{"set": {"family": "inet", "name": "meter_r4_v4", "table": "housecarl", "elem": ["10.0.0.1"]}},
|
|
{"rule": {"family": "inet", "table": "housecarl", "chain": "input", "comment": "hc:r12 ssh",
|
|
"expr": [{"counter": {"packets": 5, "bytes": 300}}, {"accept": null}]}},
|
|
{"rule": {"family": "inet", "table": "housecarl", "chain": "input", "comment": "hc:r12 ssh",
|
|
"expr": [{"counter": {"packets": 2, "bytes": 100}}, {"accept": null}]}},
|
|
{"rule": {"family": "inet", "table": "housecarl", "chain": "input", "comment": "not ours",
|
|
"expr": [{"counter": {"packets": 9, "bytes": 900}}]}}]}`)
|
|
tb, err := ParseTable(data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c := tb.Counters["r12"]; c.Packets != 7 || c.Bytes != 400 || len(tb.Counters) != 1 {
|
|
t.Errorf("counters %+v", tb.Counters)
|
|
}
|
|
if e := tb.Elements["guests"]; len(e) != 2 || e[0] != (render.Element{Addr: "10.0.30.5", Timeout: 1799}) || e[1].Addr != "10.0.30.6" || len(tb.Elements) != 1 {
|
|
t.Errorf("elements %+v", tb.Elements)
|
|
}
|
|
}
|
|
|
|
func TestExplain(t *testing.T) {
|
|
rs := &render.Ruleset{Lines: map[int]string{10: "input #4 (ssh)"}}
|
|
err := explain(&Error{Output: "/dev/stdin:11:3-12: Error: syntax error, unexpected junk\n\t\tjunk\n"}, rs)
|
|
if err.Error() != "input #4 (ssh): syntax error, unexpected junk" {
|
|
t.Errorf("explained: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestFirewallInContainer applies rulesets in a container with nft
|
|
// (HOUSECARL_NFT_IMAGE, e.g. housecarl-nft: Debian 13 with nftables):
|
|
// dynamic sets keep their entries across a reload, counters come back,
|
|
// and a broken rule is reported against its object.
|
|
func TestFirewallInContainer(t *testing.T) {
|
|
image := os.Getenv("HOUSECARL_NFT_IMAGE")
|
|
if image == "" {
|
|
t.Skip("HOUSECARL_NFT_IMAGE not set")
|
|
}
|
|
name := "housecarl-nft-test"
|
|
exec.Command("docker", "rm", "-f", name).Run()
|
|
if out, err := exec.Command("docker", "run", "-d", "--name", name, "--cap-add", "NET_ADMIN", image, "sleep", "300").CombinedOutput(); err != nil {
|
|
t.Fatalf("starting the container: %v %s", err, out)
|
|
}
|
|
t.Cleanup(func() { exec.Command("docker", "rm", "-f", name).Run() })
|
|
fw := &Firewall{Runner: &Runner{Command: []string{"docker", "exec", "-i", name, "nft"}}}
|
|
ctx := context.Background()
|
|
|
|
st := model.NewState()
|
|
st.Zones = []*model.Zone{{ID: 1, Name: "wan", Interfaces: []string{"eth0"}, External: true}}
|
|
st.Sets = []*model.AddressSet{{ID: 2, Name: "guests", Dynamic: true, Timeout: 3600}}
|
|
st.Rules = []*model.Rule{{ID: 3, Chain: model.ChainOutput, Position: 1, Action: model.ActionAccept, Match: model.Match{Destinations: []string{"guests"}}}}
|
|
if err := fw.Check(ctx, st); err != nil {
|
|
t.Fatalf("check: %v", err)
|
|
}
|
|
if _, err := fw.Table(ctx); err != ErrNoTable {
|
|
t.Errorf("before applying, the table: %v", err)
|
|
}
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatalf("apply: %v", err)
|
|
}
|
|
if err := fw.Runner.AddElement(ctx, "guests", "127.0.0.2", 600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := fw.Runner.AddElement(ctx, "guests", "::2", 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exec.Command("docker", "exec", name, "ping", "-c", "2", "-W", "1", "127.0.0.2").Run()
|
|
// Reapplying keeps the entries.
|
|
st.Settings.ForwardPolicy = model.PolicyAccept
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatalf("reapply: %v", err)
|
|
}
|
|
tb, err := fw.Table(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
for _, e := range tb.Elements["guests"] {
|
|
got = append(got, e.Addr)
|
|
if e.Addr == "127.0.0.2" && (e.Timeout < 500 || e.Timeout > 600) {
|
|
t.Errorf("timeout after reload: %+v", e)
|
|
}
|
|
}
|
|
if strings.Join(got, ",") != "127.0.0.2,::2" {
|
|
t.Errorf("entries after reload: %v", got)
|
|
}
|
|
if _, ok := tb.Counters["r3"]; !ok {
|
|
t.Errorf("counters %+v", tb.Counters)
|
|
}
|
|
if tb.Counters["b-established"].Packets == 0 && tb.Counters["r3"].Packets == 0 {
|
|
t.Logf("no packets counted (ping may be missing): %+v", tb.Counters)
|
|
}
|
|
// A broken raw rule: refused, named, and nothing changes.
|
|
st.Rules = append(st.Rules, &model.Rule{ID: 4, Chain: model.ChainInput, Position: 1, Action: model.ActionAccept, Raw: "tcp dport bogus accept", Comment: "broken"})
|
|
err = fw.Check(ctx, st)
|
|
if err == nil || !strings.Contains(err.Error(), "input #4 (broken)") {
|
|
t.Errorf("check of a broken rule: %v", err)
|
|
}
|
|
if err := fw.Apply(ctx, st); err == nil {
|
|
t.Error("a broken ruleset was applied")
|
|
}
|
|
tb, _ = fw.Table(ctx)
|
|
if len(tb.Elements["guests"]) != 2 {
|
|
t.Errorf("a refused apply changed the table: %+v", tb.Elements)
|
|
}
|
|
// The table is Housecarl's alone: another one is left alone.
|
|
exec.Command("docker", "exec", name, "nft", "add", "table", "inet", "other").Run()
|
|
fw.Apply(ctx, st.Clone())
|
|
if out, _ := exec.Command("docker", "exec", name, "nft", "list", "tables").CombinedOutput(); !strings.Contains(string(out), "inet other") {
|
|
t.Errorf("another table was touched: %s", out)
|
|
}
|
|
}
|
|
|
|
// TestQuotasInContainer: quotas keep what they used across a reload,
|
|
// reset by hand, and come back after a reboot from what was saved.
|
|
func TestQuotasInContainer(t *testing.T) {
|
|
image := os.Getenv("HOUSECARL_NFT_IMAGE")
|
|
if image == "" {
|
|
t.Skip("HOUSECARL_NFT_IMAGE not set")
|
|
}
|
|
name := "housecarl-nft-quota-test"
|
|
exec.Command("docker", "rm", "-f", name).Run()
|
|
if out, err := exec.Command("docker", "run", "-d", "--name", name, "--cap-add", "NET_ADMIN", image, "sleep", "300").CombinedOutput(); err != nil {
|
|
t.Fatalf("starting the container: %v %s", err, out)
|
|
}
|
|
t.Cleanup(func() { exec.Command("docker", "rm", "-f", name).Run() })
|
|
fw := &Firewall{Runner: &Runner{Command: []string{"docker", "exec", "-i", name, "nft"}}}
|
|
ctx := context.Background()
|
|
st := model.NewState()
|
|
// A total quota on pings to 127.0.0.2, and one per destination on
|
|
// pings to 127.0.0.3.
|
|
st.Rules = []*model.Rule{
|
|
{ID: 1, Chain: model.ChainOutput, Position: 1, Action: model.ActionDrop, Match: model.Match{Destinations: []string{"127.0.0.2"}},
|
|
Quota: &model.Quota{Bytes: 10 << 20, Over: true}},
|
|
{ID: 2, Chain: model.ChainOutput, Position: 2, Action: model.ActionDrop, Match: model.Match{Destinations: []string{"127.0.0.3"}},
|
|
Quota: &model.Quota{Bytes: 10 << 20, Over: true, PerDestination: true}},
|
|
}
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatalf("apply: %v", err)
|
|
}
|
|
ping := func(addr string) {
|
|
exec.Command("docker", "exec", name, "ping", "-c", "3", "-s", "1000", "-W", "1", addr).Run()
|
|
}
|
|
ping("127.0.0.2")
|
|
ping("127.0.0.3")
|
|
used := func() (int64, int64) {
|
|
tb, err := fw.Table(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var dev int64
|
|
for _, e := range tb.QuotaSets["quota_r2_v4"] {
|
|
if e.Addr == "127.0.0.3" {
|
|
dev = e.Used
|
|
}
|
|
}
|
|
return tb.Quotas["quota_r1"], dev
|
|
}
|
|
total, dev := used()
|
|
if total < 3000 || dev < 3000 {
|
|
t.Fatalf("used: total %d, per destination %d", total, dev)
|
|
}
|
|
// A reload (a change) keeps what was used.
|
|
st.Rules[0].Comment = "changed"
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if t2, d2 := used(); t2 < total || d2 < dev {
|
|
t.Errorf("after a reload: total %d (was %d), per destination %d (was %d)", t2, total, d2, dev)
|
|
}
|
|
// Reset by hand.
|
|
if err := fw.ResetQuota(ctx, 1, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := fw.ResetQuota(ctx, 2, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if t3, d3 := used(); t3 != 0 || d3 != 0 {
|
|
t.Errorf("after a reset: total %d, per destination %d", t3, d3)
|
|
}
|
|
// A reboot: the table is gone (the boot ruleset has no usage); the
|
|
// saved usage comes back.
|
|
ping("127.0.0.2")
|
|
ping("127.0.0.3")
|
|
tb, _ := fw.Table(ctx)
|
|
saved := tb.QuotaUsage
|
|
exec.Command("docker", "exec", name, "nft", "delete", "table", "inet", "housecarl").Run()
|
|
fw.RestoreQuotas(&saved)
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if t4, d4 := used(); t4 < saved.Quotas["quota_r1"] || t4 == 0 || d4 == 0 {
|
|
t.Errorf("after a reboot: total %d, per destination %d (saved %+v)", t4, d4, saved)
|
|
}
|
|
}
|
|
|
|
// TestDeviceCountsInContainer: the per-device counters are read back and
|
|
// survive a reload.
|
|
func TestDeviceCountsInContainer(t *testing.T) {
|
|
image := os.Getenv("HOUSECARL_NFT_IMAGE")
|
|
if image == "" {
|
|
t.Skip("HOUSECARL_NFT_IMAGE not set")
|
|
}
|
|
name := "housecarl-nft-stats-test"
|
|
exec.Command("docker", "rm", "-f", name).Run()
|
|
if out, err := exec.Command("docker", "run", "-d", "--name", name, "--cap-add", "NET_ADMIN", image, "sleep", "300").CombinedOutput(); err != nil {
|
|
t.Fatalf("starting the container: %v %s", err, out)
|
|
}
|
|
t.Cleanup(func() { exec.Command("docker", "rm", "-f", name).Run() })
|
|
fw := &Firewall{Runner: &Runner{Command: []string{"docker", "exec", "-i", name, "nft"}}}
|
|
ctx := context.Background()
|
|
st := model.NewState()
|
|
st.Zones = []*model.Zone{{ID: 1, Name: "wan", Interfaces: []string{"eth0"}, External: true}}
|
|
st.Settings.StatsPerDevice = true
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatalf("apply: %v", err)
|
|
}
|
|
if out, err := exec.Command("docker", "exec", name, "nft", "add", "element", "inet", "housecarl", "stats_up_v4", "{ 10.0.10.5 counter packets 7 bytes 7000 }").CombinedOutput(); err != nil {
|
|
t.Fatalf("adding an element: %v %s", err, out)
|
|
}
|
|
read := func() render.Element {
|
|
tb, err := fw.Table(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, e := range tb.DeviceSets["stats_up_v4"] {
|
|
if e.Addr == "10.0.10.5" {
|
|
return e
|
|
}
|
|
}
|
|
t.Fatalf("no 10.0.10.5 in %+v", tb.DeviceSets)
|
|
return render.Element{}
|
|
}
|
|
if e := read(); e.Packets != 7 || e.Bytes != 7000 || e.Timeout <= 0 {
|
|
t.Fatalf("read back %+v", e)
|
|
}
|
|
st.Settings.InputPolicy = model.PolicyAccept // a change: a reload
|
|
if err := fw.Apply(ctx, st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := read(); e.Packets != 7 || e.Bytes != 7000 {
|
|
t.Errorf("after a reload: %+v", e)
|
|
}
|
|
}
|