2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2019-12-13 14:45:21 -07:00
|
|
|
"bytes"
|
2019-11-19 10:00:20 -07:00
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-12-12 09:34:17 -07:00
|
|
|
|
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
|
"github.com/slackhq/nebula/firewall"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-11-10 20:47:38 -07:00
|
|
|
"github.com/slackhq/nebula/test"
|
2019-12-12 09:34:17 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewFirewall(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{}
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
conntrack := fw.Conntrack
|
|
|
|
assert.NotNil(t, conntrack)
|
|
|
|
assert.NotNil(t, conntrack.Conns)
|
|
|
|
assert.NotNil(t, conntrack.TimerWheel)
|
2019-11-19 10:00:20 -07:00
|
|
|
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)
|
|
|
|
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c)
|
2020-07-31 16:53:36 -06:00
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
2023-01-11 18:35:19 -07:00
|
|
|
assert.Equal(t, 3602, conntrack.TimerWheel.wheelLen)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirewall_AddRule(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-12-18 12:06:51 -07:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{}
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.NotNil(t, fw.InRules)
|
|
|
|
assert.NotNil(t, fw.OutRules)
|
|
|
|
|
|
|
|
_, ti, _ := net.ParseCIDR("1.2.3.4/32")
|
|
|
|
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", nil, nil, "", ""))
|
2019-12-18 22:23:34 -07:00
|
|
|
// An empty rule is any
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
|
2024-01-29 14:30:52 -07:00
|
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
|
|
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", ""))
|
2024-01-29 14:30:52 -07:00
|
|
|
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)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", nil, nil, "", ""))
|
2024-01-29 14:30:52 -07:00
|
|
|
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")
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, nil, "", ""))
|
2024-01-29 14:30:52 -07:00
|
|
|
assert.Nil(t, fw.OutRules.AnyProto[1].Any.Any)
|
|
|
|
ok, _ := fw.OutRules.AnyProto[1].Any.CIDR.GetCIDR(ti)
|
2023-11-02 16:05:08 -06:00
|
|
|
assert.True(t, ok)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", nil, ti, "", ""))
|
2024-01-29 14:30:52 -07:00
|
|
|
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.Any)
|
|
|
|
ok, _ = fw.OutRules.AnyProto[1].Any.Any.LocalCIDR.GetCIDR(ti)
|
2023-11-02 16:05:08 -06:00
|
|
|
assert.True(t, ok)
|
2023-05-09 09:37:23 -06:00
|
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "ca-name", ""))
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", "ca-sha"))
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, nil, "", ""))
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2019-11-19 10:00:20 -07:00
|
|
|
_, anyIp, _ := net.ParseCIDR("0.0.0.0/0")
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, nil, "", ""))
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any.Any)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test error conditions
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", nil, nil, "", ""))
|
|
|
|
assert.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", nil, nil, "", ""))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirewall_Drop(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-12-18 12:06:51 -07:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p := firewall.Packet{
|
2023-03-13 13:35:12 -06:00
|
|
|
LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
LocalPort: 10,
|
|
|
|
RemotePort: 90,
|
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
|
Fragment: false,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ipNet := net.IPNet{
|
|
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
2019-12-18 12:06:51 -07:00
|
|
|
Name: "host1",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
Groups: []string{"default-group"},
|
|
|
|
InvertedGroups: map[string]struct{}{"default-group": {}},
|
|
|
|
Issuer: "signer-shasum",
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
}
|
2019-12-12 09:34:17 -07:00
|
|
|
h := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2019-12-12 09:34:17 -07:00
|
|
|
}
|
|
|
|
h.CreateRemoteCIDR(&c)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
|
2019-11-19 10:00:20 -07:00
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
|
|
// Drop outbound
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
2019-11-19 10:00:20 -07:00
|
|
|
// Allow inbound
|
2019-12-18 22:23:34 -07:00
|
|
|
resetConntrack(fw)
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
2019-11-19 10:00:20 -07:00
|
|
|
// Allow outbound because conntrack
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
2019-12-12 09:34:17 -07:00
|
|
|
|
|
|
|
// test remote mismatch
|
|
|
|
oldRemote := p.RemoteIP
|
2021-11-03 19:54:04 -06:00
|
|
|
p.RemoteIP = iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 10))
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrInvalidRemoteIP)
|
2019-12-12 09:34:17 -07:00
|
|
|
p.RemoteIP = oldRemote
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2019-12-18 12:06:51 -07:00
|
|
|
// ensure signer doesn't get in the way of group checks
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "", "signer-shasum"))
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "", "signer-shasum-bad"))
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2019-12-18 12:06:51 -07:00
|
|
|
// test caSha doesn't drop on match
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "", "signer-shasum-bad"))
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "", "signer-shasum"))
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2019-12-18 12:06:51 -07:00
|
|
|
// ensure ca name doesn't get in the way of group checks
|
2019-11-19 10:00:20 -07:00
|
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "ca-good", ""))
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "ca-good-bad", ""))
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, true, &h, cp, nil), ErrNoMatchingRule)
|
2019-12-18 12:06:51 -07:00
|
|
|
|
|
|
|
// test caName doesn't drop on match
|
|
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, nil, "ca-good-bad", ""))
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, nil, "ca-good", ""))
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFirewallTable_match(b *testing.B) {
|
2024-01-29 14:30:52 -07:00
|
|
|
f := &Firewall{}
|
2019-11-19 10:00:20 -07:00
|
|
|
ft := FirewallTable{
|
|
|
|
TCP: firewallPort{},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, n, _ := net.ParseCIDR("172.1.1.1/32")
|
2024-01-23 15:02:10 -07:00
|
|
|
goodLocalCIDRIP := iputil.Ip2VpnIp(n.IP)
|
2024-01-29 14:30:52 -07:00
|
|
|
_ = ft.TCP.addRule(f, 10, 10, []string{"good-group"}, "good-host", n, nil, "", "")
|
|
|
|
_ = ft.TCP.addRule(f, 100, 100, []string{"good-group"}, "good-host", nil, n, "", "")
|
2019-11-19 10:00:20 -07:00
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
|
|
b.Run("fail on proto", func(b *testing.B) {
|
2024-01-23 15:02:10 -07:00
|
|
|
// This benchmark is showing us the cost of failing to match the protocol
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
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
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
b.Run("pass proto, port, fail on local CIDR", func(b *testing.B) {
|
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
|
ip, _, _ := net.ParseCIDR("9.254.254.254/32")
|
|
|
|
lip := iputil.Ip2VpnIp(ip)
|
2019-11-19 10:00:20 -07:00
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalIP: lip}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
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")
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
2024-01-23 15:02:10 -07:00
|
|
|
Name: "nope",
|
|
|
|
Ips: []*net.IPNet{ip},
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
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")
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
2024-01-23 15:02:10 -07:00
|
|
|
Name: "nope",
|
|
|
|
Ips: []*net.IPNet{ip},
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalIP: goodLocalCIDRIP}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
b.Run("pass on group on any local cidr", func(b *testing.B) {
|
2023-05-09 09:37:23 -06:00
|
|
|
c := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
2024-01-23 15:02:10 -07:00
|
|
|
InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
|
Name: "nope",
|
2023-05-09 09:37:23 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp))
|
2023-05-09 09:37:23 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
b.Run("pass on group on specific local cidr", func(b *testing.B) {
|
2019-11-19 10:00:20 -07:00
|
|
|
c := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
2024-01-23 15:02:10 -07:00
|
|
|
InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
|
Name: "nope",
|
2019-11-19 10:00:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalIP: goodLocalCIDRIP}, true, c, cp))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
})
|
2023-05-09 09:37:23 -06:00
|
|
|
|
2024-01-23 15:02:10 -07:00
|
|
|
b.Run("pass on name", func(b *testing.B) {
|
2023-05-09 09:37:23 -06:00
|
|
|
c := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
|
Name: "good-host",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for n := 0; n < b.N; n++ {
|
2024-01-23 15:02:10 -07:00
|
|
|
ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
|
2023-05-09 09:37:23 -06:00
|
|
|
}
|
|
|
|
})
|
2024-01-23 15:02:10 -07:00
|
|
|
//
|
|
|
|
//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)
|
|
|
|
// }
|
|
|
|
//})
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFirewall_Drop2(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-12-18 12:06:51 -07:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p := firewall.Packet{
|
2023-03-13 13:35:12 -06:00
|
|
|
LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
LocalPort: 10,
|
|
|
|
RemotePort: 90,
|
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
|
Fragment: false,
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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},
|
|
|
|
InvertedGroups: map[string]struct{}{"default-group": {}, "test-group": {}},
|
|
|
|
},
|
|
|
|
}
|
2019-12-12 09:34:17 -07:00
|
|
|
h := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2019-12-12 09:34:17 -07:00
|
|
|
}
|
|
|
|
h.CreateRemoteCIDR(&c)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
c1 := cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
Name: "host1",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
InvertedGroups: map[string]struct{}{"default-group": {}, "test-group-not": {}},
|
|
|
|
},
|
|
|
|
}
|
2019-12-12 09:34:17 -07:00
|
|
|
h1 := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h1.CreateRemoteCIDR(&c1)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", nil, nil, "", ""))
|
2019-11-19 10:00:20 -07:00
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
2019-12-12 09:34:17 -07:00
|
|
|
// h1/c1 lacks the proper groups
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Error(t, fw.Drop(p, true, &h1, cp, nil), ErrNoMatchingRule)
|
2019-11-19 10:00:20 -07:00
|
|
|
// c has the proper groups
|
2019-12-18 22:23:34 -07:00
|
|
|
resetConntrack(fw)
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:23:34 -07:00
|
|
|
func TestFirewall_Drop3(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-12-18 22:23:34 -07:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p := firewall.Packet{
|
2023-03-13 13:35:12 -06:00
|
|
|
LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
LocalPort: 1,
|
|
|
|
RemotePort: 1,
|
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
|
Fragment: false,
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 := cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
Name: "host1",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
Issuer: "signer-sha-bad",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h1 := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c1,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|
|
|
|
h1.CreateRemoteCIDR(&c1)
|
|
|
|
|
|
|
|
c2 := cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
Name: "host2",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
Issuer: "signer-sha",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h2 := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c2,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|
|
|
|
h2.CreateRemoteCIDR(&c2)
|
|
|
|
|
|
|
|
c3 := cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
Name: "host3",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
Issuer: "signer-sha-bad",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h3 := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c3,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|
|
|
|
h3.CreateRemoteCIDR(&c3)
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", nil, nil, "", ""))
|
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", nil, nil, "", "signer-sha"))
|
2019-12-18 22:23:34 -07:00
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
|
|
// c1 should pass because host match
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h1, cp, nil))
|
2019-12-18 22:23:34 -07:00
|
|
|
// c2 should pass because ca sha match
|
|
|
|
resetConntrack(fw)
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h2, cp, nil))
|
2019-12-18 22:23:34 -07:00
|
|
|
// c3 should fail because no match
|
|
|
|
resetConntrack(fw)
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, true, &h3, cp, nil), ErrNoMatchingRule)
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|
|
|
|
|
2020-07-31 16:53:36 -06:00
|
|
|
func TestFirewall_DropConntrackReload(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2020-07-31 16:53:36 -06:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
p := firewall.Packet{
|
2023-03-13 13:35:12 -06:00
|
|
|
LocalIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
RemoteIP: iputil.Ip2VpnIp(net.IPv4(1, 2, 3, 4)),
|
|
|
|
LocalPort: 10,
|
|
|
|
RemotePort: 90,
|
|
|
|
Protocol: firewall.ProtoUDP,
|
|
|
|
Fragment: false,
|
2020-07-31 16:53:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
h := HostInfo{
|
|
|
|
ConnectionState: &ConnectionState{
|
|
|
|
peerCert: &c,
|
|
|
|
},
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2020-07-31 16:53:36 -06:00
|
|
|
}
|
|
|
|
h.CreateRemoteCIDR(&c)
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
|
2020-07-31 16:53:36 -06:00
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
|
|
// Drop outbound
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
2020-07-31 16:53:36 -06:00
|
|
|
// Allow inbound
|
|
|
|
resetConntrack(fw)
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, true, &h, cp, nil))
|
2020-07-31 16:53:36 -06:00
|
|
|
// Allow outbound because conntrack
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
2020-07-31 16:53:36 -06:00
|
|
|
|
|
|
|
oldFw := fw
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", nil, nil, "", ""))
|
2020-07-31 16:53:36 -06:00
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
|
|
|
// Allow outbound because conntrack and new rules allow port 10
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.NoError(t, fw.Drop(p, false, &h, cp, nil))
|
2020-07-31 16:53:36 -06:00
|
|
|
|
|
|
|
oldFw = fw
|
2021-03-26 08:46:30 -06:00
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", nil, nil, "", ""))
|
2020-07-31 16:53:36 -06:00
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
|
|
|
// Drop outbound because conntrack doesn't match new ruleset
|
2024-04-11 20:44:22 -06:00
|
|
|
assert.Equal(t, fw.Drop(p, false, &h, cp, nil), ErrNoMatchingRule)
|
2020-07-31 16:53:36 -06:00
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//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; ``")
|
|
|
|
|
|
|
|
_, _, err = parsePort(" ")
|
|
|
|
assert.EqualError(t, err, "was not a number; ` `")
|
|
|
|
|
|
|
|
_, _, err = parsePort("-")
|
|
|
|
assert.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; ` - `")
|
|
|
|
|
|
|
|
_, _, err = parsePort("a-b")
|
|
|
|
assert.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`")
|
|
|
|
|
|
|
|
s, e, err := parsePort(" 1 - 2 ")
|
|
|
|
assert.Equal(t, int32(1), s)
|
|
|
|
assert.Equal(t, int32(2), e)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
s, e, err = parsePort("0-1")
|
|
|
|
assert.Equal(t, int32(0), s)
|
|
|
|
assert.Equal(t, int32(0), e)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
s, e, err = parsePort("9919")
|
|
|
|
assert.Equal(t, int32(9919), s)
|
|
|
|
assert.Equal(t, int32(9919), e)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
s, e, err = parsePort("any")
|
|
|
|
assert.Equal(t, int32(0), s)
|
|
|
|
assert.Equal(t, int32(0), e)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewFirewallFromConfig(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-11-19 10:00:20 -07:00
|
|
|
// Test a bad rule definition
|
|
|
|
c := &cert.NebulaCertificate{}
|
2021-11-03 19:54:04 -06:00
|
|
|
conf := config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": "asdf"}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err := NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
|
|
|
|
|
|
|
|
// Test both port and code
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "code": "2"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.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
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2023-05-09 09:37:23 -06:00
|
|
|
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")
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test code/port error
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "a", "host": "testh"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.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"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
|
|
|
|
|
|
|
|
// Test proto error
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "host": "testh"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
|
|
|
|
|
|
|
|
// Test cidr parse error
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "cidr": "testh", "proto": "any"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; invalid CIDR address: testh")
|
|
|
|
|
2023-05-09 09:37:23 -06:00
|
|
|
// 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; invalid CIDR address: testh")
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Test both group and groups
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddFirewallRulesFromConfig(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-11-19 10:00:20 -07:00
|
|
|
// Test adding tcp rule
|
2021-11-03 19:54:04 -06:00
|
|
|
conf := config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf := &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "tcp", "host": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test adding udp rule
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "udp", "host": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test adding icmp rule
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "icmp", "host": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test adding any rule
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
|
|
// Test adding rule with cidr
|
2023-05-09 10:01:30 -06:00
|
|
|
cidr := &net.IPNet{IP: net.ParseIP("10.0.0.0").To4(), Mask: net.IPv4Mask(255, 0, 0, 0)}
|
2023-05-09 09:37:23 -06:00
|
|
|
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))
|
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: cidr, localIp: nil}, 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))
|
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: cidr}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test adding rule with ca_sha
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caSha: "12312313123"}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test adding rule with ca_name
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_name": "root01"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caName: "root01"}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test single group
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test single groups
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": "a"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test multiple AND groups
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
mf = &mockFirewall{}
|
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
2023-05-09 09:37:23 -06:00
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: nil, localIp: nil}, mf.lastCall)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Test Add error
|
2021-11-03 19:54:04 -06:00
|
|
|
conf = config.NewC(l)
|
2019-11-19 10:00:20 -07:00
|
|
|
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"}}}
|
2021-03-26 08:46:30 -06:00
|
|
|
assert.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2019-12-13 14:45:21 -07:00
|
|
|
func TestFirewall_convertRule(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-12-13 14:45:21 -07:00
|
|
|
ob := &bytes.Buffer{}
|
|
|
|
l.SetOutput(ob)
|
|
|
|
|
|
|
|
// Ensure group array of 1 is converted and a warning is printed
|
|
|
|
c := map[interface{}]interface{}{
|
|
|
|
"group": []interface{}{"group1"},
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
r, err := convertRule(l, c, "test", 1)
|
2019-12-13 14:45:21 -07:00
|
|
|
assert.Contains(t, ob.String(), "test rule #1; group was an array with a single value, converting to simple value")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
|
|
|
|
|
|
// Ensure group array of > 1 is errord
|
|
|
|
ob.Reset()
|
|
|
|
c = map[interface{}]interface{}{
|
|
|
|
"group": []interface{}{"group1", "group2"},
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
r, err = convertRule(l, c, "test", 1)
|
2019-12-13 14:45:21 -07:00
|
|
|
assert.Equal(t, "", ob.String())
|
|
|
|
assert.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[interface{}]interface{}{
|
|
|
|
"group": "group1",
|
|
|
|
}
|
|
|
|
|
2021-03-26 08:46:30 -06:00
|
|
|
r, err = convertRule(l, c, "test", 1)
|
2019-12-13 14:45:21 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
type addRuleCall struct {
|
|
|
|
incoming bool
|
|
|
|
proto uint8
|
|
|
|
startPort int32
|
|
|
|
endPort int32
|
|
|
|
groups []string
|
|
|
|
host string
|
|
|
|
ip *net.IPNet
|
2023-05-09 09:37:23 -06:00
|
|
|
localIp *net.IPNet
|
2019-11-19 10:00:20 -07:00
|
|
|
caName string
|
|
|
|
caSha string
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockFirewall struct {
|
|
|
|
lastCall addRuleCall
|
|
|
|
nextCallReturn error
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:37:23 -06:00
|
|
|
func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, localIp *net.IPNet, caName string, caSha string) error {
|
2019-11-19 10:00:20 -07:00
|
|
|
mf.lastCall = addRuleCall{
|
|
|
|
incoming: incoming,
|
|
|
|
proto: proto,
|
|
|
|
startPort: startPort,
|
|
|
|
endPort: endPort,
|
|
|
|
groups: groups,
|
|
|
|
host: host,
|
|
|
|
ip: ip,
|
2023-05-09 09:37:23 -06:00
|
|
|
localIp: localIp,
|
2019-11-19 10:00:20 -07:00
|
|
|
caName: caName,
|
|
|
|
caSha: caSha,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mf.nextCallReturn
|
|
|
|
mf.nextCallReturn = nil
|
|
|
|
return err
|
|
|
|
}
|
2019-12-18 22:23:34 -07:00
|
|
|
|
|
|
|
func resetConntrack(fw *Firewall) {
|
2020-07-31 16:53:36 -06:00
|
|
|
fw.Conntrack.Lock()
|
2021-11-03 19:54:04 -06:00
|
|
|
fw.Conntrack.Conns = map[firewall.Packet]*conn{}
|
2020-07-31 16:53:36 -06:00
|
|
|
fw.Conntrack.Unlock()
|
2019-12-18 22:23:34 -07:00
|
|
|
}
|