|
|
@@ -13,6 +13,7 @@ import (
|
|
|
"github.com/slackhq/nebula/cert"
|
|
|
"github.com/slackhq/nebula/config"
|
|
|
"github.com/slackhq/nebula/firewall"
|
|
|
+ "github.com/slackhq/nebula/iputil"
|
|
|
"github.com/slackhq/nebula/test"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
"github.com/stretchr/testify/require"
|
|
|
@@ -72,20 +73,20 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
ti6, err := netip.ParsePrefix("fd12::34/128")
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", "", "", "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolTCP, 1, 1, []string{}, "", "", "", "", ""))
|
|
|
// An empty rule is any
|
|
|
assert.True(t, fw.InRules.TCP[1].Any.Any.Any)
|
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
|
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", ""))
|
|
|
assert.Nil(t, fw.InRules.UDP[1].Any.Any)
|
|
|
assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0].Groups, "g1")
|
|
|
assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", "", "", "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 1, 1, []string{}, "h1", "", "", "", ""))
|
|
|
//no matter what port is given for icmp, it should end up as "any"
|
|
|
assert.Nil(t, fw.InRules.ICMP[firewall.PortAny].Any.Any)
|
|
|
assert.Empty(t, fw.InRules.ICMP[firewall.PortAny].Any.Groups)
|
|
|
@@ -116,11 +117,11 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
assert.True(t, ok)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "ca-name", ""))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolUDP, 1, 1, []string{"g1"}, "", "", "", "", "ca-sha"))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
@@ -185,7 +186,7 @@ func TestFirewall_Drop(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
@@ -263,7 +264,7 @@ func TestFirewall_DropV6(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("fd12::34"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
@@ -350,7 +351,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
Certificate: &dummyCert{},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoUDP}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolUDP}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -360,7 +361,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
Certificate: &dummyCert{},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 1}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 1}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -370,7 +371,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
}
|
|
|
ip := netip.MustParsePrefix("9.254.254.254/32")
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
b.Run("pass proto, port, fail on local CIDRv6", func(b *testing.B) {
|
|
|
@@ -379,7 +380,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
}
|
|
|
ip := netip.MustParsePrefix("fd99::99/128")
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: ip.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -392,7 +393,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
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))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
b.Run("pass proto, port, any local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
|
|
|
@@ -404,7 +405,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
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))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -417,7 +418,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
b.Run("pass proto, port, specific local CIDRv6, fail all group, name, and cidr", func(b *testing.B) {
|
|
|
@@ -429,7 +430,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.False(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
|
|
+ assert.False(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -441,7 +442,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
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))
|
|
|
+ assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -453,7 +454,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
+ assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
b.Run("pass on group on specific local cidr6", func(b *testing.B) {
|
|
|
@@ -464,7 +465,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
InvertedGroups: map[string]struct{}{"good-group": {}},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- assert.True(b, ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
|
|
+ assert.True(b, ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 100, LocalAddr: pfix6.Addr()}, true, c, cp))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -476,7 +477,7 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
InvertedGroups: map[string]struct{}{"nope": {}},
|
|
|
}
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
- ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 10}, true, c, cp)
|
|
|
+ ft.match(firewall.Packet{Protocol: iputil.IPProtocolTCP, LocalPort: 10}, true, c, cp)
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
@@ -492,7 +493,7 @@ func TestFirewall_Drop2(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
@@ -550,7 +551,7 @@ func TestFirewall_Drop3(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 1,
|
|
|
RemotePort: 1,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
@@ -638,7 +639,7 @@ func TestFirewall_Drop3V6(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("fd12::34"),
|
|
|
LocalPort: 1,
|
|
|
RemotePort: 1,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
@@ -675,7 +676,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
network := netip.MustParsePrefix("1.2.3.4/24")
|
|
|
@@ -758,13 +759,13 @@ func TestFirewall_ICMPPortBehavior(t *testing.T) {
|
|
|
templ := firewall.Packet{
|
|
|
LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
|
|
- Protocol: firewall.ProtoICMP,
|
|
|
+ Protocol: iputil.IPProtocolICMP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
|
|
|
t.Run("ICMP allowed", func(t *testing.T) {
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c.Certificate)
|
|
|
- require.NoError(t, fw.AddRule(true, firewall.ProtoICMP, 0, 0, []string{"any"}, "", "", "", "", ""))
|
|
|
+ require.NoError(t, fw.AddRule(true, iputil.IPProtocolICMP, 0, 0, []string{"any"}, "", "", "", "", ""))
|
|
|
t.Run("zero ports", func(t *testing.T) {
|
|
|
p := templ.Copy()
|
|
|
p.LocalPort = 0
|
|
|
@@ -910,7 +911,7 @@ func TestFirewall_DropIPSpoofing(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("192.0.2.3"),
|
|
|
LocalPort: 1,
|
|
|
RemotePort: 1,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
assert.Equal(t, fw.Drop(p, true, &h1, cp, nil), ErrInvalidRemoteIP)
|
|
|
@@ -961,7 +962,7 @@ func TestFirewall_ConntrackSourceSpoofingAcrossPeers(t *testing.T) {
|
|
|
RemoteAddr: netip.MustParseAddr("192.0.2.2"),
|
|
|
LocalPort: 443,
|
|
|
RemotePort: 55000,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
}
|
|
|
|
|
|
require.NoError(t, fw.Drop(flow, true, &victimHI, cp, nil),
|
|
|
@@ -1031,7 +1032,7 @@ func BenchmarkFirewallDropConntrackHit(b *testing.B) {
|
|
|
RemoteAddr: netip.MustParseAddr("192.0.2.2"),
|
|
|
LocalPort: 443,
|
|
|
RemotePort: 55000,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
}
|
|
|
|
|
|
cases := []struct {
|
|
|
@@ -1317,28 +1318,28 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
|
mf := &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "tcp", "host": "a"}}}
|
|
|
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
|
|
|
// Test adding udp rule
|
|
|
conf = config.NewC(test.NewLogger())
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "udp", "host": "a"}}}
|
|
|
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
|
|
|
// Test adding icmp rule
|
|
|
conf = config.NewC(test.NewLogger())
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"port": "1", "proto": "icmp", "host": "a"}}}
|
|
|
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
|
|
|
// Test adding icmp rule no port
|
|
|
conf = config.NewC(test.NewLogger())
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[string]any{"outbound": []any{map[string]any{"proto": "icmp", "host": "a"}}}
|
|
|
require.NoError(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: iputil.IPProtocolICMP, startPort: firewall.PortAny, endPort: firewall.PortAny, groups: nil, host: "a", ip: "", localIp: ""}, mf.lastCall)
|
|
|
|
|
|
// Test adding any rule
|
|
|
conf = config.NewC(test.NewLogger())
|
|
|
@@ -1582,7 +1583,7 @@ func buildTestCase(setup testsetup, err error, theirPrefixes ...netip.Prefix) te
|
|
|
RemoteAddr: theirPrefixes[0].Addr(),
|
|
|
LocalPort: 10,
|
|
|
RemotePort: 90,
|
|
|
- Protocol: firewall.ProtoUDP,
|
|
|
+ Protocol: iputil.IPProtocolUDP,
|
|
|
Fragment: false,
|
|
|
}
|
|
|
return testcase{
|