|
@@ -69,67 +69,75 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
|
|
|
_, ti, _ := net.ParseCIDR("1.2.3.4/32")
|
|
_, ti, _ := net.ParseCIDR("1.2.3.4/32")
|
|
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoTCP, 1, 1, []string{}, "", nil, nil, "", ""))
|
|
// An empty rule is any
|
|
// An empty rule is any
|
|
assert.True(t, fw.InRules.TCP[1].Any.Any)
|
|
assert.True(t, fw.InRules.TCP[1].Any.Any)
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Groups)
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
|
|
assert.Empty(t, fw.InRules.TCP[1].Any.Hosts)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", ""))
|
|
assert.False(t, fw.InRules.UDP[1].Any.Any)
|
|
assert.False(t, fw.InRules.UDP[1].Any.Any)
|
|
assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0], "g1")
|
|
assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0], "g1")
|
|
assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
|
|
assert.Empty(t, fw.InRules.UDP[1].Any.Hosts)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoICMP, 1, 1, []string{}, "h1", nil, nil, "", ""))
|
|
assert.False(t, fw.InRules.ICMP[1].Any.Any)
|
|
assert.False(t, fw.InRules.ICMP[1].Any.Any)
|
|
assert.Empty(t, fw.InRules.ICMP[1].Any.Groups)
|
|
assert.Empty(t, fw.InRules.ICMP[1].Any.Groups)
|
|
assert.Contains(t, fw.InRules.ICMP[1].Any.Hosts, "h1")
|
|
assert.Contains(t, fw.InRules.ICMP[1].Any.Hosts, "h1")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", ti, nil, "", ""))
|
|
assert.False(t, fw.OutRules.AnyProto[1].Any.Any)
|
|
assert.False(t, fw.OutRules.AnyProto[1].Any.Any)
|
|
assert.Empty(t, fw.OutRules.AnyProto[1].Any.Groups)
|
|
assert.Empty(t, fw.OutRules.AnyProto[1].Any.Groups)
|
|
assert.Empty(t, fw.OutRules.AnyProto[1].Any.Hosts)
|
|
assert.Empty(t, fw.OutRules.AnyProto[1].Any.Hosts)
|
|
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
assert.NotNil(t, fw.OutRules.AnyProto[1].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, "ca-name", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 1, 1, []string{}, "", nil, ti, "", ""))
|
|
|
|
+ assert.False(t, fw.OutRules.AnyProto[1].Any.Any)
|
|
|
|
+ assert.Empty(t, fw.OutRules.AnyProto[1].Any.Groups)
|
|
|
|
+ assert.Empty(t, fw.OutRules.AnyProto[1].Any.Hosts)
|
|
|
|
+ assert.NotNil(t, fw.OutRules.AnyProto[1].Any.LocalCIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
|
|
+
|
|
|
|
+ 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", ""))
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, "", "ca-sha"))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoUDP, 1, 1, []string{"g1"}, "", nil, nil, "", "ca-sha"))
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
|
|
|
|
// Set any and clear fields
|
|
// Set any and clear fields
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"g1", "g2"}, "h1", ti, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"g1", "g2"}, "h1", ti, ti, "", ""))
|
|
assert.Equal(t, []string{"g1", "g2"}, fw.OutRules.AnyProto[0].Any.Groups[0])
|
|
assert.Equal(t, []string{"g1", "g2"}, fw.OutRules.AnyProto[0].Any.Groups[0])
|
|
assert.Contains(t, fw.OutRules.AnyProto[0].Any.Hosts, "h1")
|
|
assert.Contains(t, fw.OutRules.AnyProto[0].Any.Hosts, "h1")
|
|
assert.NotNil(t, fw.OutRules.AnyProto[0].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
assert.NotNil(t, fw.OutRules.AnyProto[0].Any.CIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
|
|
+ assert.NotNil(t, fw.OutRules.AnyProto[0].Any.LocalCIDR.Match(iputil.Ip2VpnIp(ti.IP)))
|
|
|
|
|
|
// run twice just to make sure
|
|
// run twice just to make sure
|
|
//TODO: these ANY rules should clear the CA firewall portion
|
|
//TODO: these ANY rules should clear the CA firewall portion
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, "", ""))
|
|
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, nil, "", ""))
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
assert.Empty(t, fw.OutRules.AnyProto[0].Any.Groups)
|
|
assert.Empty(t, fw.OutRules.AnyProto[0].Any.Groups)
|
|
assert.Empty(t, fw.OutRules.AnyProto[0].Any.Hosts)
|
|
assert.Empty(t, fw.OutRules.AnyProto[0].Any.Hosts)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "any", nil, nil, "", ""))
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
|
|
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
_, anyIp, _ := net.ParseCIDR("0.0.0.0/0")
|
|
_, anyIp, _ := net.ParseCIDR("0.0.0.0/0")
|
|
- assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{}, "", anyIp, nil, "", ""))
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
|
|
|
|
// Test error conditions
|
|
// Test error conditions
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
- assert.Error(t, fw.AddRule(true, math.MaxUint8, 0, 0, []string{}, "", nil, "", ""))
|
|
|
|
- assert.Error(t, fw.AddRule(true, firewall.ProtoAny, 10, 0, []string{}, "", nil, "", ""))
|
|
|
|
|
|
+ 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, "", ""))
|
|
}
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop(t *testing.T) {
|
|
func TestFirewall_Drop(t *testing.T) {
|
|
@@ -169,7 +177,7 @@ func TestFirewall_Drop(t *testing.T) {
|
|
h.CreateRemoteCIDR(&c)
|
|
h.CreateRemoteCIDR(&c)
|
|
|
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
|
|
cp := cert.NewCAPool()
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// Drop outbound
|
|
// Drop outbound
|
|
@@ -188,28 +196,28 @@ func TestFirewall_Drop(t *testing.T) {
|
|
|
|
|
|
// ensure signer doesn't get in the way of group checks
|
|
// ensure signer doesn't get in the way of group checks
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, "", "signer-shasum"))
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, "", "signer-shasum-bad"))
|
|
|
|
|
|
+ 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"))
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
|
|
|
|
// test caSha doesn't drop on match
|
|
// test caSha doesn't drop on match
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, "", "signer-shasum-bad"))
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, "", "signer-shasum"))
|
|
|
|
|
|
+ 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"))
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
|
|
|
|
// ensure ca name doesn't get in the way of group checks
|
|
// 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.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, "ca-good", ""))
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, "ca-good-bad", ""))
|
|
|
|
|
|
+ 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", ""))
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
|
|
|
|
// test caName doesn't drop on match
|
|
// test caName doesn't drop on match
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"nope"}, "", nil, "ca-good-bad", ""))
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group"}, "", nil, "ca-good", ""))
|
|
|
|
|
|
+ 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", ""))
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
}
|
|
}
|
|
|
|
|
|
@@ -219,11 +227,11 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
}
|
|
}
|
|
|
|
|
|
_, n, _ := net.ParseCIDR("172.1.1.1/32")
|
|
_, n, _ := net.ParseCIDR("172.1.1.1/32")
|
|
- _ = ft.TCP.addRule(10, 10, []string{"good-group"}, "good-host", n, "", "")
|
|
|
|
- _ = ft.TCP.addRule(10, 10, []string{"good-group2"}, "good-host", n, "", "")
|
|
|
|
- _ = ft.TCP.addRule(10, 10, []string{"good-group3"}, "good-host", n, "", "")
|
|
|
|
- _ = ft.TCP.addRule(10, 10, []string{"good-group4"}, "good-host", n, "", "")
|
|
|
|
- _ = ft.TCP.addRule(10, 10, []string{"good-group, good-group1"}, "good-host", n, "", "")
|
|
|
|
|
|
+ _ = ft.TCP.addRule(10, 10, []string{"good-group"}, "good-host", n, n, "", "")
|
|
|
|
+ _ = ft.TCP.addRule(10, 10, []string{"good-group2"}, "good-host", n, n, "", "")
|
|
|
|
+ _ = ft.TCP.addRule(10, 10, []string{"good-group3"}, "good-host", n, n, "", "")
|
|
|
|
+ _ = ft.TCP.addRule(10, 10, []string{"good-group4"}, "good-host", n, n, "", "")
|
|
|
|
+ _ = ft.TCP.addRule(10, 10, []string{"good-group, good-group1"}, "good-host", n, n, "", "")
|
|
cp := cert.NewCAPool()
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
b.Run("fail on proto", func(b *testing.B) {
|
|
b.Run("fail on proto", func(b *testing.B) {
|
|
@@ -291,7 +299,20 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
- _ = ft.TCP.addRule(0, 0, []string{"good-group"}, "good-host", n, "", "")
|
|
|
|
|
|
+ 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) {
|
|
b.Run("pass on ip with any port", func(b *testing.B) {
|
|
ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
ip := iputil.Ip2VpnIp(net.IPv4(172, 1, 1, 1))
|
|
@@ -305,6 +326,19 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
ft.match(firewall.Packet{Protocol: firewall.ProtoTCP, LocalPort: 100, RemoteIP: ip}, true, c, cp)
|
|
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) {
|
|
func TestFirewall_Drop2(t *testing.T) {
|
|
@@ -356,7 +390,7 @@ func TestFirewall_Drop2(t *testing.T) {
|
|
h1.CreateRemoteCIDR(&c1)
|
|
h1.CreateRemoteCIDR(&c1)
|
|
|
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
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"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"default-group", "test-group"}, "", nil, nil, "", ""))
|
|
cp := cert.NewCAPool()
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// h1/c1 lacks the proper groups
|
|
// h1/c1 lacks the proper groups
|
|
@@ -438,8 +472,8 @@ func TestFirewall_Drop3(t *testing.T) {
|
|
h3.CreateRemoteCIDR(&c3)
|
|
h3.CreateRemoteCIDR(&c3)
|
|
|
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "host1", nil, "", ""))
|
|
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 1, 1, []string{}, "", nil, "", "signer-sha"))
|
|
|
|
|
|
+ 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"))
|
|
cp := cert.NewCAPool()
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// c1 should pass because host match
|
|
// c1 should pass because host match
|
|
@@ -489,7 +523,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
h.CreateRemoteCIDR(&c)
|
|
h.CreateRemoteCIDR(&c)
|
|
|
|
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"any"}, "", nil, nil, "", ""))
|
|
cp := cert.NewCAPool()
|
|
cp := cert.NewCAPool()
|
|
|
|
|
|
// Drop outbound
|
|
// Drop outbound
|
|
@@ -502,7 +536,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
|
|
|
oldFw := fw
|
|
oldFw := fw
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 10, 10, []string{"any"}, "", nil, nil, "", ""))
|
|
fw.Conntrack = oldFw.Conntrack
|
|
fw.Conntrack = oldFw.Conntrack
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
|
@@ -511,7 +545,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
|
|
|
oldFw = fw
|
|
oldFw = fw
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
- assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", nil, "", ""))
|
|
|
|
|
|
+ assert.Nil(t, fw.AddRule(true, firewall.ProtoAny, 11, 11, []string{"any"}, "", nil, nil, "", ""))
|
|
fw.Conntrack = oldFw.Conntrack
|
|
fw.Conntrack = oldFw.Conntrack
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
|
|
|
|
@@ -653,7 +687,7 @@ func TestNewFirewallFromConfig(t *testing.T) {
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
|
- assert.EqualError(t, err, "firewall.outbound rule #0; at least one of host, group, cidr, ca_name, or ca_sha must be provided")
|
|
|
|
|
|
+ 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")
|
|
|
|
|
|
// Test code/port error
|
|
// Test code/port error
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
@@ -677,6 +711,12 @@ func TestNewFirewallFromConfig(t *testing.T) {
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
|
_, err = NewFirewallFromConfig(l, c, conf)
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; invalid CIDR address: testh")
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; invalid CIDR address: testh")
|
|
|
|
|
|
|
|
+ // 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")
|
|
|
|
+
|
|
// Test both group and groups
|
|
// Test both group and groups
|
|
conf = config.NewC(l)
|
|
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"}}}}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
|
|
@@ -691,63 +731,78 @@ func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
mf := &mockFirewall{}
|
|
mf := &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "tcp", "host": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding udp rule
|
|
// Test adding udp rule
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "udp", "host": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding icmp rule
|
|
// Test adding icmp rule
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "icmp", "host": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: false, proto: firewall.ProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding any rule
|
|
// Test adding any rule
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ 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
|
|
|
|
+ cidr := &net.IPNet{net.ParseIP("10.0.0.0").To4(), net.IPv4Mask(255, 0, 0, 0)}
|
|
|
|
+ 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)
|
|
|
|
|
|
// Test adding rule with ca_sha
|
|
// Test adding rule with ca_sha
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
|
|
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))
|
|
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, caSha: "12312313123"}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caSha: "12312313123"}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with ca_name
|
|
// Test adding rule with ca_name
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_name": "root01"}}}
|
|
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))
|
|
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, caName: "root01"}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, localIp: nil, caName: "root01"}, mf.lastCall)
|
|
|
|
|
|
// Test single group
|
|
// Test single group
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test single groups
|
|
// Test single groups
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": "a"}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test multiple AND groups
|
|
// Test multiple AND groups
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
mf = &mockFirewall{}
|
|
mf = &mockFirewall{}
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
|
|
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))
|
|
assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
- assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
+ assert.Equal(t, addRuleCall{incoming: true, proto: firewall.ProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: nil, localIp: nil}, mf.lastCall)
|
|
|
|
|
|
// Test Add error
|
|
// Test Add error
|
|
conf = config.NewC(l)
|
|
conf = config.NewC(l)
|
|
@@ -892,6 +947,7 @@ type addRuleCall struct {
|
|
groups []string
|
|
groups []string
|
|
host string
|
|
host string
|
|
ip *net.IPNet
|
|
ip *net.IPNet
|
|
|
|
+ localIp *net.IPNet
|
|
caName string
|
|
caName string
|
|
caSha string
|
|
caSha string
|
|
}
|
|
}
|
|
@@ -901,7 +957,7 @@ type mockFirewall struct {
|
|
nextCallReturn error
|
|
nextCallReturn error
|
|
}
|
|
}
|
|
|
|
|
|
-func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, endPort int32, groups []string, host string, ip *net.IPNet, caName string, caSha string) error {
|
|
|
|
|
|
+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 {
|
|
mf.lastCall = addRuleCall{
|
|
mf.lastCall = addRuleCall{
|
|
incoming: incoming,
|
|
incoming: incoming,
|
|
proto: proto,
|
|
proto: proto,
|
|
@@ -910,6 +966,7 @@ func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, end
|
|
groups: groups,
|
|
groups: groups,
|
|
host: host,
|
|
host: host,
|
|
ip: ip,
|
|
ip: ip,
|
|
|
|
+ localIp: localIp,
|
|
caName: caName,
|
|
caName: caName,
|
|
caSha: caSha,
|
|
caSha: caSha,
|
|
}
|
|
}
|