| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534 |
- package nebula
- import (
- "bytes"
- "errors"
- "math"
- "net/netip"
- "testing"
- "time"
- "github.com/gaissmai/bart"
- "github.com/sirupsen/logrus"
- "github.com/slackhq/nebula/cert"
- "github.com/slackhq/nebula/config"
- "github.com/slackhq/nebula/firewall"
- "github.com/slackhq/nebula/test"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- func TestNewFirewall(t *testing.T) {
- l := test.NewLogger()
- c := &dummyCert{}
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- conntrack := fw.Conntrack
- assert.NotNil(t, conntrack)
- assert.NotNil(t, conntrack.Conns)
- assert.NotNil(t, conntrack.TimerWheel)
- assert.NotNil(t, fw.InRules)
- assert.NotNil(t, fw.OutRules)
- assert.Equal(t, time.Second, fw.TCPTimeout)
- assert.Equal(t, time.Minute, fw.UDPTimeout)
- assert.Equal(t, time.Hour, fw.DefaultTimeout)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c)
- assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
- assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
- }
- func TestFirewall_AddRule(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- c := &dummyCert{}
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- assert.NotNil(t, fw.InRules)
- assert.NotNil(t, fw.OutRules)
- ti, err := netip.ParsePrefix("1.2.3.4/32")
- require.NoError(t, err)
- ti6, err := netip.ParsePrefix("fd12::34/128")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", "", "", "", ""))
- // An empty rule is any
- assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
- assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
- assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
- assert.Nil(t, fw.InRules.UDP[1].Any.Any)
- assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1")
- assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", "", "", "", ""))
- //no matter what port is given for icmp, it should end up as "any"
- assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any)
- assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups)
- assert.Contains(t, fw.InRules.ICMP[firewall.PortAny].Any.Hosts, "h1")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
- _, ok := fw.OutRules.AnyProto[1].Any.CIDR.Get(ti)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti6.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
- _, ok = fw.OutRules.AnyProto[1].Any.CIDR.Get(ti6)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti.String(), "", ""))
- assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
- ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", "", ti6.String(), "", ""))
- assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
- ok = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.Get(ti6)
- assert.True(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
- assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
- assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", "", "", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- anyIp, err := netip.ParsePrefix("0.0.0.0/0")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
- table, ok := fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
- assert.True(t, table.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
- assert.False(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- anyIp6, err := netip.ParsePrefix("::/0")
- require.NoError(t, err)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp6.String(), "", "", ""))
- assert.Nil(t, fw.OutRules.AnyProto[0].Any.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("9::9"))
- assert.True(t, table.Any)
- table, ok = fw.OutRules.AnyProto[0].Any.CIDR.Lookup(netip.MustParseAddr("1.1.1.1"))
- assert.False(t, ok)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "any", "", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp.String(), "", ""))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", anyIp6.String(), "", ""))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("9::9")))
- assert.False(t, fw.OutRules.AnyProto[0].Any.Any.LocalCIDR.Lookup(netip.MustParseAddr("1.1.1.1")))
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", "", "any", "", ""))
- assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
- // Test error conditions
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
- require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", "", "", "", ""))
- require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", "", "", "", ""))
- }
- func TestFirewall_Drop(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- c := dummyCert{
- name: "host1",
- networks: []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
- }
- h.buildNetworks(myVpnNetworksTable, &c)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- // test remote mismatch
- oldRemote := p.RemoteAddr
- p.RemoteAddr = netip.MustParseAddr("1.2.3.10")
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
- p.RemoteAddr = oldRemote
- // ensure signer doesn't get in the way of group checks
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caSha doesn't drop on match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // ensure ca name doesn't get in the way of group checks
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caName doesn't drop on match
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_DropV6(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("fd12::34"),
- RemoteAddr: netip.MustParseAddr("fd12::34"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- c := dummyCert{
- name: "host1",
- networks: []netip.Prefix{netip.MustParsePrefix("fd12::34/120")},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: []netip.Addr{netip.MustParseAddr("fd12::34")},
- }
- h.buildNetworks(myVpnNetworksTable, &c)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- // test remote mismatch
- oldRemote := p.RemoteAddr
- p.RemoteAddr = netip.MustParseAddr("fd12::56")
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
- p.RemoteAddr = oldRemote
- // ensure signer doesn't get in the way of group checks
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum-bad"))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caSha doesn't drop on match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "", "signer-shasum-bad"))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "", "signer-shasum"))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // ensure ca name doesn't get in the way of group checks
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good-bad", ""))
- assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
- // test caName doesn't drop on match
- cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", "", "", "ca-good-bad", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", "", "", "ca-good", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func BenchmarkFirewallTable_match(b *testing.B) {
- f := &Firewall{}
- ft := FirewallTable{
- TCP: firewallPort{},
- }
- pfix := netip.MustParsePrefix("172.1.1.1/32")
- _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix.String(), "", "", "")
- _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix.String(), "", "")
- pfix6 := netip.MustParsePrefix("fd11::11/128")
- _ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", pfix6.String(), "", "", "")
- _ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", "", pfix6.String(), "", "")
- cp := cert.NewCAPool()
- b.Run("fail on proto", func(b *testing.B) {
- // This benchmark is showing us the cost of failing to match the protocol
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp))
- }
- })
- b.Run("pass proto, fail on port", func(b *testing.B) {
- // This benchmark is showing us the cost of matching a specific protocol but failing to match the port
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp))
- }
- })
- b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- ip := netip.MustParsePrefix("9.254.254.254/32")
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{},
- }
- ip := netip.MustParsePrefix("fd99::99/128")
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, any local CIDR, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("9.254.254.245/32")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
- }
- })
- b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- networks: []netip.Prefix{netip.MustParsePrefix("fd99::99/128")},
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on group on any local cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
- }
- })
- b.Run("pass on group on specific local cidr", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on group on specific local cidr6", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "nope",
- },
- InvertedGroups: map[string]struct{}{"good-group": {}},
- }
- for n := 0; n < b.N; n++ {
- assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
- }
- })
- b.Run("pass on name", func(b *testing.B) {
- c := &cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "good-host",
- },
- InvertedGroups: map[string]struct{}{"nope": {}},
- }
- for n := 0; n < b.N; n++ {
- ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
- }
- })
- }
- func TestFirewall_Drop2(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- },
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- },
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
- }
- h1 := HostInfo{
- vpnAddrs: []netip.Addr{network.Addr()},
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // h1/c1 lacks the proper groups
- require.ErrorIs(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
- // c has the proper groups
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_Drop3(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{network},
- },
- }
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- issuer: "signer-sha-bad",
- },
- }
- h1 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- c2 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host2",
- networks: []netip.Prefix{network},
- issuer: "signer-sha",
- },
- }
- h2 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c2,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h2.buildNetworks(myVpnNetworksTable, c2.Certificate)
- c3 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host3",
- networks: []netip.Prefix{network},
- issuer: "signer-sha-bad",
- },
- }
- h3 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c3,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h3.buildNetworks(myVpnNetworksTable, c3.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", "", "", "", ""))
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", "signer-sha"))
- cp := cert.NewCAPool()
- // c1 should pass because host match
- require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
- // c2 should pass because ca sha match
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h2, cp, nil))
- // c3 should fail because no match
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(p, true, &h3, cp, nil), ErrNoMatchingRule)
- // Test a remote address match
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "1.2.3.4/24", "", "", ""))
- require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
- }
- func TestFirewall_Drop3V6(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("fd00::/7"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("fd12::34"),
- RemoteAddr: netip.MustParseAddr("fd12::34"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("fd12::34/120")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{network},
- },
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- // Test a remote address match
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- cp := cert.NewCAPool()
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "fd12::34/120", "", "", ""))
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- }
- func TestFirewall_DropConntrackReload(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- },
- InvertedGroups: map[string]struct{}{"default-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Drop outbound
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(p, true, &h, cp, nil))
- // Allow outbound because conntrack
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- oldFw := fw
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", "", "", "", ""))
- fw.Conntrack = oldFw.Conntrack
- fw.rulesVersion = oldFw.rulesVersion + 1
- // Allow outbound because conntrack and new rules allow port 10
- require.NoError(t, fw.Drop(p, false, &h, cp, nil))
- oldFw = fw
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", "", "", "", ""))
- fw.Conntrack = oldFw.Conntrack
- fw.rulesVersion = oldFw.rulesVersion + 1
- // Drop outbound because conntrack doesn't match new ruleset
- assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
- }
- func TestFirewall_ICMPPortBehavior(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
- network := netip.MustParsePrefix("1.2.3.4/24")
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host1",
- networks: []netip.Prefix{network},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- },
- InvertedGroups: map[string]struct{}{"default-group": {}},
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c,
- },
- vpnAddrs: []netip.Addr{network.Addr()},
- }
- h.buildNetworks(myVpnNetworksTable, c.Certificate)
- cp := cert.NewCAPool()
- templ := firewall.Packet{
- LocalAddr: netip.MustParseAddr("1.2.3.4"),
- RemoteAddr: netip.MustParseAddr("1.2.3.4"),
- Protocol: firewall.ProtoICMP,
- Fragment: false,
- }
- t.Run("ICMP allowed", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 0, 0, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- t.Run("nonzero ports", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- })
- t.Run("Any proto, some ports allowed", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 80, 444, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- t.Run("nonzero ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- t.Run("nonzero, matching ports, still blocked", func(t *testing.T) {
- p := templ.Copy()
- p.LocalPort = 80
- p.RemotePort = 80
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- assert.Equal(t, fw.Drop(*p, true, &h, cp, nil), ErrNoMatchingRule)
- //now also allow outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- })
- t.Run("Any proto, any port", func(t *testing.T) {
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- t.Run("zero ports, allowed", func(t *testing.T) {
- resetConntrack(fw)
- p := templ.Copy()
- p.LocalPort = 0
- p.RemotePort = 0
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- })
- t.Run("nonzero ports, allowed", func(t *testing.T) {
- resetConntrack(fw)
- p := templ.Copy()
- p.LocalPort = 0xabcd
- p.RemotePort = 0x1234
- // Drop outbound
- assert.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- // Allow inbound
- resetConntrack(fw)
- require.NoError(t, fw.Drop(*p, true, &h, cp, nil))
- //now also allow outbound
- require.NoError(t, fw.Drop(*p, false, &h, cp, nil))
- //different ID is blocked
- p.RemotePort++
- require.Equal(t, fw.Drop(*p, false, &h, cp, nil), ErrNoMatchingRule)
- })
- })
- }
- func TestFirewall_DropIPSpoofing(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myVpnNetworksTable := new(bart.Lite)
- myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
- c := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host-owner",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
- },
- }
- c1 := cert.CachedCertificate{
- Certificate: &dummyCert{
- name: "host",
- networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24")},
- unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
- },
- }
- h1 := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &c1,
- },
- vpnAddrs: []netip.Addr{c1.Certificate.Networks()[0].Addr()},
- }
- h1.buildNetworks(myVpnNetworksTable, c1.Certificate)
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", "", "", "", ""))
- cp := cert.NewCAPool()
- // Packet spoofed by `c1`. Note that the remote addr is not a valid one.
- p := firewall.Packet{
- LocalAddr: netip.MustParseAddr("192.0.2.1"),
- RemoteAddr: netip.MustParseAddr("192.0.2.3"),
- LocalPort: 1,
- RemotePort: 1,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP)
- }
- func BenchmarkLookup(b *testing.B) {
- ml := func(m map[string]struct{}, a [][]string) {
- for n := 0; n < b.N; n++ {
- for _, sg := range a {
- found := false
- for _, g := range sg {
- if _, ok := m[g]; !ok {
- found = false
- break
- }
- found = true
- }
- if found {
- return
- }
- }
- }
- }
- b.Run("array to map best", func(b *testing.B) {
- m := map[string]struct{}{
- "1ne": {},
- "2wo": {},
- "3hr": {},
- "4ou": {},
- "5iv": {},
- "6ix": {},
- }
- a := [][]string{
- {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "six"},
- }
- for n := 0; n < b.N; n++ {
- ml(m, a)
- }
- })
- b.Run("array to map worst", func(b *testing.B) {
- m := map[string]struct{}{
- "one": {},
- "two": {},
- "thr": {},
- "fou": {},
- "fiv": {},
- "six": {},
- }
- a := [][]string{
- {"1ne", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "2wo", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "3hr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "4ou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "5iv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "6ix"},
- {"one", "two", "thr", "fou", "fiv", "six"},
- }
- for n := 0; n < b.N; n++ {
- ml(m, a)
- }
- })
- }
- func Test_parsePort(t *testing.T) {
- _, _, err := parsePort("")
- require.EqualError(t, err, "was not a number; ``")
- _, _, err = parsePort(" ")
- require.EqualError(t, err, "was not a number; ` `")
- _, _, err = parsePort("-")
- require.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
- _, _, err = parsePort(" - ")
- require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
- _, _, err = parsePort("a-b")
- require.EqualError(t, err, "beginning range was not a number; `a`")
- _, _, err = parsePort("1-b")
- require.EqualError(t, err, "ending range was not a number; `b`")
- s, e, err := parsePort(" 1 - 2 ")
- assert.Equal(t, int32(1), s)
- assert.Equal(t, int32(2), e)
- require.NoError(t, err)
- s, e, err = parsePort("0-1")
- assert.Equal(t, int32(0), s)
- assert.Equal(t, int32(0), e)
- require.NoError(t, err)
- s, e, err = parsePort("9919")
- assert.Equal(t, int32(9919), s)
- assert.Equal(t, int32(9919), e)
- require.NoError(t, err)
- s, e, err = parsePort("any")
- assert.Equal(t, int32(0), s)
- assert.Equal(t, int32(0), e)
- require.NoError(t, err)
- }
- func TestNewFirewallFromConfig(t *testing.T) {
- l := test.NewLogger()
- // Test a bad rule definition
- c := &dummyCert{}
- cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil)
- require.NoError(t, err)
- conf := config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": "asdf"}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
- // Test both port and code
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "code": "2"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
- // Test missing host, group, cidr, ca_name and ca_sha
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided")
- // Test code/port error
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "a", "host": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "a", "host": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
- // Test proto error
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "host": "testh"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
- // Test cidr parse error
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "cidr": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
- // Test local_cidr parse error
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"code": "1", "local_cidr": "testh", "proto": "any"}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
- // Test both group and groups
- conf = config.NewC(l)
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
- _, err = NewFirewallFromConfig(l, cs, conf)
- require.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
- }
- func TestAddFirewallRulesFromConfig(t *testing.T) {
- l := test.NewLogger()
- // Test adding tcp rule
- conf := config.NewC(l)
- mf := &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding udp rule
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding icmp rule
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding icmp rule no port
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding any rule
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
- // Test adding rule with cidr
- cidr := netip.MustParsePrefix("10.0.0.0/8")
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr.String(), localIp: ""}, mf.lastCall)
- // Test adding rule with local_cidr
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr.String()}, mf.lastCall)
- // Test adding rule with cidr ipv6
- cidr6 := netip.MustParsePrefix("fd00::/8")
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": cidr6.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr6.String(), localIp: ""}, mf.lastCall)
- // Test adding rule with any cidr
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "any"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "any", localIp: ""}, mf.lastCall)
- // Test adding rule with junk cidr
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "cidr": "junk/junk"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
- // Test adding rule with local_cidr ipv6
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": cidr6.String()}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: cidr6.String()}, mf.lastCall)
- // Test adding rule with any local_cidr
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "any"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, localIp: "any"}, mf.lastCall)
- // Test adding rule with junk local_cidr
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "local_cidr": "junk/junk"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"junk/junk\"): ParseAddr(\"junk\"): unable to parse IP")
- // Test adding rule with ca_sha
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caSha: "12312313123"}, mf.lastCall)
- // Test adding rule with ca_name
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "ca_name": "root01"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: "", localIp: "", caName: "root01"}, mf.lastCall)
- // Test single group
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "group": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
- // Test single groups
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": "a"}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: "", localIp: ""}, mf.lastCall)
- // Test multiple AND groups
- conf = config.NewC(l)
- mf = &mockFirewall{}
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
- require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: "", localIp: ""}, mf.lastCall)
- // Test Add error
- conf = config.NewC(l)
- mf = &mockFirewall{}
- mf.nextCallReturn = errors.New("test error")
- conf.Settings["firewall"] = map[string]any{"inbound": []any{map[string]any{"port": "1", "proto": "any", "host": "a"}}}
- require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
- }
- func TestFirewall_convertRule(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- // Ensure group array of 1 is converted and a warning is printed
- c := map[string]any{
- "group": []any{"group1"},
- }
- r, err := convertRule(l, c, "test", 1)
- assert.Contains(t, ob.String(), "test rule #1; group was an array with a single value, converting to simple value")
- require.NoError(t, err)
- assert.Equal(t, []string{"group1"}, r.Groups)
- // Ensure group array of > 1 is errord
- ob.Reset()
- c = map[string]any{
- "group": []any{"group1", "group2"},
- }
- r, err = convertRule(l, c, "test", 1)
- assert.Empty(t, ob.String())
- require.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
- // Make sure a well formed group is alright
- ob.Reset()
- c = map[string]any{
- "group": "group1",
- }
- r, err = convertRule(l, c, "test", 1)
- require.NoError(t, err)
- assert.Equal(t, []string{"group1"}, r.Groups)
- }
- func TestFirewall_convertRuleSanity(t *testing.T) {
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- noWarningPlease := []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"host": "bob"},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range noWarningPlease {
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- require.NoError(t, r.sanity(), "should not generate a sanity warning, %+v", c)
- }
- yesWarningPlease := []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range yesWarningPlease {
- c["host"] = "any"
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- err = r.sanity()
- require.Error(t, err, "I wanted a warning: %+v", c)
- }
- //reset the list
- yesWarningPlease = []map[string]any{
- {"group": "group1"},
- {"groups": []any{"group2"}},
- {"cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "host": "bob"},
- {"cidr": "1.1.1.1/1", "host": "bob"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1"},
- {"groups": []any{"group2"}, "cidr": "1.1.1.1/1", "host": "bob"},
- }
- for _, c := range yesWarningPlease {
- r, err := convertRule(l, c, "test", 1)
- require.NoError(t, err)
- r.Groups = append(r.Groups, "any")
- err = r.sanity()
- require.Error(t, err, "I wanted a warning: %+v", c)
- }
- }
- type testcase struct {
- h *HostInfo
- p firewall.Packet
- c cert.Certificate
- err error
- }
- func (c *testcase) Test(t *testing.T, fw *Firewall) {
- t.Helper()
- cp := cert.NewCAPool()
- resetConntrack(fw)
- err := fw.Drop(c.p, true, c.h, cp, nil)
- if c.err == nil {
- require.NoError(t, err, "failed to not drop remote address %s", c.p.RemoteAddr)
- } else {
- require.ErrorIs(t, c.err, err, "failed to drop remote address %s", c.p.RemoteAddr)
- }
- }
- func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) testcase {
- c1 := dummyCert{
- name: "host1",
- networks: theirPrefixes,
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- h := HostInfo{
- ConnectionState: &ConnectionState{
- peerCert: &cert.CachedCertificate{
- Certificate: &c1,
- InvertedGroups: map[string]struct{}{"default-group": {}},
- },
- },
- vpnAddrs: make([]netip.Addr, len(theirPrefixes)),
- }
- for i := range theirPrefixes {
- h.vpnAddrs[i] = theirPrefixes[i].Addr()
- }
- h.buildNetworks(setup.myVpnNetworksTable, &c1)
- p := firewall.Packet{
- LocalAddr: setup.c.Networks()[0].Addr(), //todo?
- RemoteAddr: theirPrefixes[0].Addr(),
- LocalPort: 10,
- RemotePort: 90,
- Protocol: firewall.ProtoUDP,
- Fragment: false,
- }
- return testcase{
- h: &h,
- p: p,
- c: &c1,
- err: err,
- }
- }
- type testsetup struct {
- c dummyCert
- myVpnNetworksTable *bart.Lite
- fw *Firewall
- }
- func newSetup(t *testing.T, l *logrus.Logger, myPrefixes ...netip.Prefix) testsetup {
- c := dummyCert{
- name: "me",
- networks: myPrefixes,
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- return newSetupFromCert(t, l, c)
- }
- func newSetupFromCert(t *testing.T, l *logrus.Logger, c dummyCert) testsetup {
- myVpnNetworksTable := new(bart.Lite)
- for _, prefix := range c.Networks() {
- myVpnNetworksTable.Insert(prefix)
- }
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
- require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
- return testsetup{
- c: c,
- fw: fw,
- myVpnNetworksTable: myVpnNetworksTable,
- }
- }
- func TestFirewall_Drop_EnforceIPMatch(t *testing.T) {
- t.Parallel()
- l := test.NewLogger()
- ob := &bytes.Buffer{}
- l.SetOutput(ob)
- myPrefix := netip.MustParsePrefix("1.1.1.1/8")
- // for now, it's okay that these are all "incoming", the logic this test tries to check doesn't care about in/out
- t.Run("allow inbound all matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, nil, netip.MustParsePrefix("1.2.3.4/24"))
- tc.Test(t, setup.fw)
- })
- t.Run("allow inbound local matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidLocalIP, netip.MustParsePrefix("1.2.3.4/24"))
- tc.p.LocalAddr = netip.MustParseAddr("1.2.3.8")
- tc.Test(t, setup.fw)
- })
- t.Run("block inbound remote mismatched", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidRemoteIP, netip.MustParsePrefix("1.2.3.4/24"))
- tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
- tc.Test(t, setup.fw)
- })
- t.Run("Block a vpn peer packet", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrPeerRejected, netip.MustParsePrefix("2.2.2.2/24"))
- tc.Test(t, setup.fw)
- })
- twoPrefixes := []netip.Prefix{
- netip.MustParsePrefix("1.2.3.4/24"), netip.MustParsePrefix("2.2.2.2/24"),
- }
- t.Run("allow inbound one matching", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, nil, twoPrefixes...)
- tc.Test(t, setup.fw)
- })
- t.Run("block inbound multimismatch", func(t *testing.T) {
- t.Parallel()
- setup := newSetup(t, l, myPrefix)
- tc := buildTestCase(setup, ErrInvalidRemoteIP, twoPrefixes...)
- tc.p.RemoteAddr = netip.MustParseAddr("9.9.9.9")
- tc.Test(t, setup.fw)
- })
- t.Run("allow inbound 2nd one matching", func(t *testing.T) {
- t.Parallel()
- setup2 := newSetup(t, l, netip.MustParsePrefix("2.2.2.1/24"))
- tc := buildTestCase(setup2, nil, twoPrefixes...)
- tc.p.RemoteAddr = twoPrefixes[1].Addr()
- tc.Test(t, setup2.fw)
- })
- t.Run("allow inbound unsafe route", func(t *testing.T) {
- t.Parallel()
- unsafePrefix := netip.MustParsePrefix("192.168.0.0/24")
- c := dummyCert{
- name: "me",
- networks: []netip.Prefix{myPrefix},
- unsafeNetworks: []netip.Prefix{unsafePrefix},
- groups: []string{"default-group"},
- issuer: "signer-shasum",
- }
- unsafeSetup := newSetupFromCert(t, l, c)
- tc := buildTestCase(unsafeSetup, nil, twoPrefixes...)
- tc.p.LocalAddr = netip.MustParseAddr("192.168.0.3")
- tc.err = ErrNoMatchingRule
- tc.Test(t, unsafeSetup.fw) //should hit firewall and bounce off
- require.NoError(t, unsafeSetup.fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", unsafePrefix.String(), "", ""))
- tc.err = nil
- tc.Test(t, unsafeSetup.fw) //should pass
- })
- }
- type addRuleCall struct {
- incoming bool
- proto uint8
- startPort int32
- endPort int32
- groups []string
- host string
- ip string
- localIp string
- caName string
- caSha string
- }
- type mockFirewall struct {
- lastCall addRuleCall
- nextCallReturn error
- }
- func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip, localIp, caName string, caSha string) error {
- mf.lastCall = addRuleCall{
- incoming: incoming,
- proto: proto,
- startPort: startPort,
- endPort: endPort,
- groups: groups,
- host: host,
- ip: ip,
- localIp: localIp,
- caName: caName,
- caSha: caSha,
- }
- err := mf.nextCallReturn
- mf.nextCallReturn = nil
- return err
- }
- func resetConntrack(fw *Firewall) {
- fw.Conntrack.Lock()
- fw.Conntrack.Conns = map[firewall.Packet]*conn{}
- fw.Conntrack.Unlock()
- }
|