|
@@ -4,7 +4,6 @@ import (
|
|
|
"bytes"
|
|
|
"errors"
|
|
|
"math"
|
|
|
- "net"
|
|
|
"net/netip"
|
|
|
"testing"
|
|
|
"time"
|
|
@@ -14,11 +13,12 @@ import (
|
|
|
"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 := &cert.NebulaCertificate{}
|
|
|
+ c := &dummyCert{}
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
conntrack := fw.Conntrack
|
|
|
assert.NotNil(t, conntrack)
|
|
@@ -60,67 +60,67 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
ob := &bytes.Buffer{}
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
- c := &cert.NebulaCertificate{}
|
|
|
+ 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")
|
|
|
- assert.NoError(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
// 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)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
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)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
assert.Nil(t, fw.InRules.ICMP[1].Any.Any)
|
|
|
assert.Empty(t, fw.InRules.ICMP[1].Any.Groups)
|
|
|
assert.Contains(t, fw.InRules.ICMP[1].Any.Hosts, "h1")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, netip.Prefix{}, "", ""))
|
|
|
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)
|
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, ti, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, ti, "", ""))
|
|
|
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)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "ca-name", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "ca-name", ""))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "", "ca-sha"))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", netip.Prefix{}, netip.Prefix{}, "", "ca-sha"))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
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")
|
|
|
- assert.NoError(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, netip.Prefix{}, "", ""))
|
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
|
|
|
|
|
// Test error conditions
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- assert.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
- assert.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop(t *testing.T) {
|
|
@@ -129,79 +129,74 @@ func TestFirewall_Drop(t *testing.T) {
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
p := firewall.Packet{
|
|
|
- LocalIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
- RemoteIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
- ipNet := net.IPNet{
|
|
|
- IP: net.IPv4(1, 2, 3, 4),
|
|
|
- Mask: net.IPMask{255, 255, 255, 0},
|
|
|
- }
|
|
|
-
|
|
|
- c := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host1",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- Groups: []string{"default-group"},
|
|
|
- InvertedGroups: map[string]struct{}{"default-group": {}},
|
|
|
- Issuer: "signer-shasum",
|
|
|
- },
|
|
|
+ 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: &c,
|
|
|
+ peerCert: &cert.CachedCertificate{
|
|
|
+ Certificate: &c,
|
|
|
+ InvertedGroups: map[string]struct{}{"default-group": {}},
|
|
|
+ },
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
|
|
|
}
|
|
|
- h.CreateRemoteCIDR(&c)
|
|
|
+ h.buildNetworks(c.networks, c.unsafeNetworks)
|
|
|
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// Drop outbound
|
|
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(p, false, &h, cp, nil))
|
|
|
// Allow inbound
|
|
|
resetConntrack(fw)
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
// Allow outbound because conntrack
|
|
|
- assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
|
|
|
// test remote mismatch
|
|
|
- oldRemote := p.RemoteIP
|
|
|
- p.RemoteIP = netip.MustParseAddr("1.2.3.10")
|
|
|
+ oldRemote := p.RemoteAddr
|
|
|
+ p.RemoteAddr = netip.MustParseAddr("1.2.3.10")
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
|
|
|
- p.RemoteIP = oldRemote
|
|
|
+ p.RemoteAddr = oldRemote
|
|
|
|
|
|
// ensure signer doesn't get in the way of group checks
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum"))
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum-bad"))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum"))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "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)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum-bad"))
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum"))
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-shasum-bad"))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "", "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.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
|
+ cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good-bad", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "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.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
|
+ cp.CAs["signer-shasum"] = &cert.CachedCertificate{Certificate: &dummyCert{name: "ca-good"}}
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good-bad", ""))
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good-bad", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", netip.Prefix{}, netip.Prefix{}, "ca-good", ""))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
}
|
|
|
|
|
|
func BenchmarkFirewallTable_match(b *testing.B) {
|
|
@@ -217,7 +212,9 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
|
|
|
b.Run("fail on proto", func(b *testing.B) {
|
|
|
// This benchmark is showing us the cost of failing to match the protocol
|
|
|
- c := &cert.NebulaCertificate{}
|
|
|
+ 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))
|
|
|
}
|
|
@@ -225,28 +222,31 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
|
|
|
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.NebulaCertificate{}
|
|
|
+ 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.NebulaCertificate{}
|
|
|
+ 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, LocalIP: ip.Addr()}, true, c, cp))
|
|
|
+ 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) {
|
|
|
- _, ip, _ := net.ParseCIDR("9.254.254.254/32")
|
|
|
- c := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- Name: "nope",
|
|
|
- Ips: []*net.IPNet{ip},
|
|
|
+ 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))
|
|
@@ -254,25 +254,24 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
})
|
|
|
|
|
|
b.Run("pass proto, port, specific local CIDR, fail all group, name, and cidr", func(b *testing.B) {
|
|
|
- _, ip, _ := net.ParseCIDR("9.254.254.254/32")
|
|
|
- c := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- Name: "nope",
|
|
|
- Ips: []*net.IPNet{ip},
|
|
|
+ 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, LocalIP: pfix.Addr()}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
b.Run("pass on group on any local cidr", func(b *testing.B) {
|
|
|
- c := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
- Name: "nope",
|
|
|
+ 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))
|
|
@@ -280,82 +279,28 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
})
|
|
|
|
|
|
b.Run("pass on group on specific local cidr", func(b *testing.B) {
|
|
|
- c := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
- Name: "nope",
|
|
|
+ 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, LocalIP: pfix.Addr()}, true, c, cp))
|
|
|
+ assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
b.Run("pass on name", func(b *testing.B) {
|
|
|
- c := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- Name: "good-host",
|
|
|
+ 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)
|
|
|
}
|
|
|
})
|
|
|
- //
|
|
|
- //b.Run("pass on ip", func(b *testing.B) {
|
|
|
- // ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
|
- // c := &cert.NebulaCertificate{
|
|
|
- // Details: cert.NebulaCertificateDetails{
|
|
|
- // InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- // Name: "good-host",
|
|
|
- // },
|
|
|
- // }
|
|
|
- // for n := 0; n < b.N; n++ {
|
|
|
- // ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10, RemoteIP: ip}, true, c, cp)
|
|
|
- // }
|
|
|
- //})
|
|
|
- //
|
|
|
- //b.Run("pass on local ip", func(b *testing.B) {
|
|
|
- // ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
|
- // c := &cert.NebulaCertificate{
|
|
|
- // Details: cert.NebulaCertificateDetails{
|
|
|
- // InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- // Name: "good-host",
|
|
|
- // },
|
|
|
- // }
|
|
|
- // for n := 0; n < b.N; n++ {
|
|
|
- // ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10, LocalIP: ip}, true, c, cp)
|
|
|
- // }
|
|
|
- //})
|
|
|
- //
|
|
|
- //_ = ft.TCP.addRule(0, 0, []string{"good-group"}, "good-host", n, n, "", "")
|
|
|
- //
|
|
|
- //b.Run("pass on ip with any port", func(b *testing.B) {
|
|
|
- // ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
|
- // c := &cert.NebulaCertificate{
|
|
|
- // Details: cert.NebulaCertificateDetails{
|
|
|
- // InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- // Name: "good-host",
|
|
|
- // },
|
|
|
- // }
|
|
|
- // for n := 0; n < b.N; n++ {
|
|
|
- // ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, RemoteIP: ip}, true, c, cp)
|
|
|
- // }
|
|
|
- //})
|
|
|
- //
|
|
|
- //b.Run("pass on local ip with any port", func(b *testing.B) {
|
|
|
- // ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
|
- // c := &cert.NebulaCertificate{
|
|
|
- // Details: cert.NebulaCertificateDetails{
|
|
|
- // InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
- // Name: "good-host",
|
|
|
- // },
|
|
|
- // }
|
|
|
- // for n := 0; n < b.N; n++ {
|
|
|
- // ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalIP: ip}, true, c, cp)
|
|
|
- // }
|
|
|
- //})
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop2(t *testing.T) {
|
|
@@ -364,57 +309,55 @@ func TestFirewall_Drop2(t *testing.T) {
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
p := firewall.Packet{
|
|
|
- LocalIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
- RemoteIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
- ipNet := net.IPNet{
|
|
|
- IP: net.IPv4(1, 2, 3, 4),
|
|
|
- Mask: net.IPMask{255, 255, 255, 0},
|
|
|
- }
|
|
|
+ network := netip.MustParsePrefix("1.2.3.4/24")
|
|
|
|
|
|
- c := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host1",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
|
|
|
+ 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,
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr(ipNet.IP.String()),
|
|
|
+ vpnAddrs: []netip.Addr{network.Addr()},
|
|
|
}
|
|
|
- h.CreateRemoteCIDR(&c)
|
|
|
+ h.buildNetworks(c.Certificate.Networks(), c.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- c1 := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host1",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
|
|
|
+ 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.CreateRemoteCIDR(&c1)
|
|
|
+ h1.buildNetworks(c1.Certificate.Networks(), c1.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ 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"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// h1/c1 lacks the proper groups
|
|
|
- assert.Error(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
|
|
|
+ require.ErrorIs(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
|
|
|
// c has the proper groups
|
|
|
resetConntrack(fw)
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop3(t *testing.T) {
|
|
@@ -423,84 +366,85 @@ func TestFirewall_Drop3(t *testing.T) {
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
p := firewall.Packet{
|
|
|
- LocalIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
- RemoteIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 1,
|
|
|
RemotePort: 1,
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
- ipNet := net.IPNet{
|
|
|
- IP: net.IPv4(1, 2, 3, 4),
|
|
|
- Mask: net.IPMask{255, 255, 255, 0},
|
|
|
- }
|
|
|
-
|
|
|
- c := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host-owner",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
+ network := netip.MustParsePrefix("1.2.3.4/24")
|
|
|
+ c := cert.CachedCertificate{
|
|
|
+ Certificate: &dummyCert{
|
|
|
+ name: "host-owner",
|
|
|
+ networks: []netip.Prefix{network},
|
|
|
},
|
|
|
}
|
|
|
|
|
|
- c1 := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host1",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- Issuer: "signer-sha-bad",
|
|
|
+ c1 := cert.CachedCertificate{
|
|
|
+ Certificate: &dummyCert{
|
|
|
+ name: "host1",
|
|
|
+ networks: []netip.Prefix{network},
|
|
|
+ issuer: "signer-sha-bad",
|
|
|
},
|
|
|
}
|
|
|
h1 := HostInfo{
|
|
|
ConnectionState: &ConnectionState{
|
|
|
peerCert: &c1,
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr(ipNet.IP.String()),
|
|
|
+ vpnAddrs: []netip.Addr{network.Addr()},
|
|
|
}
|
|
|
- h1.CreateRemoteCIDR(&c1)
|
|
|
+ h1.buildNetworks(c1.Certificate.Networks(), c1.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- c2 := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host2",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- Issuer: "signer-sha",
|
|
|
+ c2 := cert.CachedCertificate{
|
|
|
+ Certificate: &dummyCert{
|
|
|
+ name: "host2",
|
|
|
+ networks: []netip.Prefix{network},
|
|
|
+ issuer: "signer-sha",
|
|
|
},
|
|
|
}
|
|
|
h2 := HostInfo{
|
|
|
ConnectionState: &ConnectionState{
|
|
|
peerCert: &c2,
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr(ipNet.IP.String()),
|
|
|
+ vpnAddrs: []netip.Addr{network.Addr()},
|
|
|
}
|
|
|
- h2.CreateRemoteCIDR(&c2)
|
|
|
+ h2.buildNetworks(c2.Certificate.Networks(), c2.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- c3 := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host3",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- Issuer: "signer-sha-bad",
|
|
|
+ c3 := cert.CachedCertificate{
|
|
|
+ Certificate: &dummyCert{
|
|
|
+ name: "host3",
|
|
|
+ networks: []netip.Prefix{network},
|
|
|
+ issuer: "signer-sha-bad",
|
|
|
},
|
|
|
}
|
|
|
h3 := HostInfo{
|
|
|
ConnectionState: &ConnectionState{
|
|
|
peerCert: &c3,
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr(ipNet.IP.String()),
|
|
|
+ vpnAddrs: []netip.Addr{network.Addr()},
|
|
|
}
|
|
|
- h3.CreateRemoteCIDR(&c3)
|
|
|
+ h3.buildNetworks(c3.Certificate.Networks(), c3.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-sha"))
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", netip.Prefix{}, netip.Prefix{}, "", "signer-sha"))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// c1 should pass because host match
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
|
|
// c2 should pass because ca sha match
|
|
|
resetConntrack(fw)
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h2, cp, nil))
|
|
|
+ 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{}, "", netip.MustParsePrefix("1.2.3.4/24"), netip.Prefix{}, "", ""))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
|
|
}
|
|
|
|
|
|
func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
@@ -509,60 +453,56 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
p := firewall.Packet{
|
|
|
- LocalIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
- RemoteIP: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
+ RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
-
|
|
|
- ipNet := net.IPNet{
|
|
|
- IP: net.IPv4(1, 2, 3, 4),
|
|
|
- Mask: net.IPMask{255, 255, 255, 0},
|
|
|
- }
|
|
|
-
|
|
|
- c := cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "host1",
|
|
|
- Ips: []*net.IPNet{&ipNet},
|
|
|
- Groups: []string{"default-group"},
|
|
|
- InvertedGroups: map[string]struct{}{"default-group": {}},
|
|
|
- Issuer: "signer-shasum",
|
|
|
+ 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,
|
|
|
},
|
|
|
- vpnIp: netip.MustParseAddr(ipNet.IP.String()),
|
|
|
+ vpnAddrs: []netip.Addr{network.Addr()},
|
|
|
}
|
|
|
- h.CreateRemoteCIDR(&c)
|
|
|
+ h.buildNetworks(c.Certificate.Networks(), c.Certificate.UnsafeNetworks())
|
|
|
|
|
|
- fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// Drop outbound
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
|
|
// Allow inbound
|
|
|
resetConntrack(fw)
|
|
|
- assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
|
|
// Allow outbound because conntrack
|
|
|
- assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
|
|
|
oldFw := fw
|
|
|
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
|
// Allow outbound because conntrack and new rules allow port 10
|
|
|
- assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
+ require.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
|
|
|
|
|
oldFw = fw
|
|
|
- fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
|
|
+ require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", netip.Prefix{}, netip.Prefix{}, "", ""))
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
@@ -641,104 +581,105 @@ func BenchmarkLookup(b *testing.B) {
|
|
|
ml(m, a)
|
|
|
}
|
|
|
})
|
|
|
-
|
|
|
- //TODO: only way array lookup in array will help is if both are sorted, then maybe it's faster
|
|
|
}
|
|
|
|
|
|
func Test_parsePort(t *testing.T) {
|
|
|
_, _, err := parsePort("")
|
|
|
- assert.EqualError(t, err, "was not a number; ``")
|
|
|
+ require.EqualError(t, err, "was not a number; ``")
|
|
|
|
|
|
_, _, err = parsePort(" ")
|
|
|
- assert.EqualError(t, err, "was not a number; ` `")
|
|
|
+ require.EqualError(t, err, "was not a number; ` `")
|
|
|
|
|
|
_, _, err = parsePort("-")
|
|
|
- assert.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
|
|
|
+ require.EqualError(t, err, "appears to be a range but could not be parsed; `-`")
|
|
|
|
|
|
_, _, err = parsePort(" - ")
|
|
|
- assert.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
|
|
|
+ require.EqualError(t, err, "appears to be a range but could not be parsed; ` - `")
|
|
|
|
|
|
_, _, err = parsePort("a-b")
|
|
|
- assert.EqualError(t, err, "beginning range was not a number; `a`")
|
|
|
+ require.EqualError(t, err, "beginning range was not a number; `a`")
|
|
|
|
|
|
_, _, err = parsePort("1-b")
|
|
|
- assert.EqualError(t, err, "ending range was not a number; `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)
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
s, e, err = parsePort("0-1")
|
|
|
assert.Equal(t, int32(0), s)
|
|
|
assert.Equal(t, int32(0), e)
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
s, e, err = parsePort("9919")
|
|
|
assert.Equal(t, int32(9919), s)
|
|
|
assert.Equal(t, int32(9919), e)
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
|
s, e, err = parsePort("any")
|
|
|
assert.Equal(t, int32(0), s)
|
|
|
assert.Equal(t, int32(0), e)
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
}
|
|
|
|
|
|
func TestNewFirewallFromConfig(t *testing.T) {
|
|
|
l := test.NewLogger()
|
|
|
// Test a bad rule definition
|
|
|
- c := &cert.NebulaCertificate{}
|
|
|
+ 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[interface{}]interface{}{"outbound": "asdf"}
|
|
|
- _, err := NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "code": "2"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; only one of port or code should be provided")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, local_cidr, ca_name, or ca_sha must be provided")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "a", "host": "testh"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
|
|
|
+ _, err = NewFirewallFromConfig(l, cs, conf)
|
|
|
+ require.EqualError(t, err, "firewall.outbound rule #0; code was not a number; `a`")
|
|
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "a", "host": "testh"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "host": "testh"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "cidr": "testh", "proto": "any"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
|
|
|
+ _, 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[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "local_cidr": "testh", "proto": "any"}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; local_cidr did not parse; netip.ParsePrefix(\"testh\"): no '/'")
|
|
|
+ _, 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[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
|
|
|
- _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
- assert.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
|
|
|
+ _, 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) {
|
|
@@ -747,28 +688,28 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
|
conf := config.NewC(l)
|
|
|
mf := &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "tcp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test adding udp rule
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "udp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test adding icmp rule
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "icmp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
+ require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test adding any rule
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with cidr
|
|
@@ -776,49 +717,49 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "cidr": cidr.String()}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ 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, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with local_cidr
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "local_cidr": cidr.String()}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: cidr}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with ca_sha
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: netip.Prefix{}, caSha: "12312313123"}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with ca_name
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_name": "root01"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ require.NoError(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: netip.Prefix{}, localIp: netip.Prefix{}, caName: "root01"}, mf.lastCall)
|
|
|
|
|
|
// Test single group
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test single groups
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test multiple AND groups
|
|
|
conf = config.NewC(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
+ 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: netip.Prefix{}, localIp: netip.Prefix{}}, mf.lastCall)
|
|
|
|
|
|
// Test Add error
|
|
@@ -826,7 +767,7 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
|
mf = &mockFirewall{}
|
|
|
mf.nextCallReturn = errors.New("test error")
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
|
|
- assert.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
|
|
|
+ require.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
|
|
|
}
|
|
|
|
|
|
func TestFirewall_convertRule(t *testing.T) {
|
|
@@ -841,7 +782,7 @@ func TestFirewall_convertRule(t *testing.T) {
|
|
|
|
|
|
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")
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
|
|
|
|
// Ensure group array of > 1 is errord
|
|
@@ -852,7 +793,7 @@ func TestFirewall_convertRule(t *testing.T) {
|
|
|
|
|
|
r, err = convertRule(l, c, "test", 1)
|
|
|
assert.Equal(t, "", ob.String())
|
|
|
- assert.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
|
|
|
+ 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()
|
|
@@ -861,7 +802,7 @@ func TestFirewall_convertRule(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
r, err = convertRule(l, c, "test", 1)
|
|
|
- assert.Nil(t, err)
|
|
|
+ require.NoError(t, err)
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
|
}
|
|
|
|