|
@@ -15,8 +15,9 @@ import (
|
|
|
)
|
|
|
|
|
|
func TestNewFirewall(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
conntrack := fw.Conntrack
|
|
|
assert.NotNil(t, conntrack)
|
|
|
assert.NotNil(t, conntrack.Conns)
|
|
@@ -31,35 +32,34 @@ func TestNewFirewall(t *testing.T) {
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Hour, time.Minute, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Hour, time.Minute, c)
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
|
|
|
- fw = NewFirewall(time.Hour, time.Second, time.Minute, c)
|
|
|
+ fw = NewFirewall(l, time.Hour, time.Second, time.Minute, c)
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
|
|
|
- fw = NewFirewall(time.Hour, time.Minute, time.Second, c)
|
|
|
+ fw = NewFirewall(l, time.Hour, time.Minute, time.Second, c)
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
|
|
|
- fw = NewFirewall(time.Minute, time.Hour, time.Second, c)
|
|
|
+ fw = NewFirewall(l, time.Minute, time.Hour, time.Second, c)
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
|
|
|
- fw = NewFirewall(time.Minute, time.Second, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Minute, time.Second, time.Hour, c)
|
|
|
assert.Equal(t, time.Hour, conntrack.TimerWheel.wheelDuration)
|
|
|
assert.Equal(t, 3601, conntrack.TimerWheel.wheelLen)
|
|
|
}
|
|
|
|
|
|
func TestFirewall_AddRule(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.NotNil(t, fw.InRules)
|
|
|
assert.NotNil(t, fw.OutRules)
|
|
|
|
|
@@ -74,7 +74,7 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
assert.Nil(t, fw.InRules.TCP[1].Any.CIDR.root.right)
|
|
|
assert.Nil(t, fw.InRules.TCP[1].Any.CIDR.root.value)
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoUDP, 1, 1, []string{"g1"}, "", nil, "", ""))
|
|
|
assert.False(t, fw.InRules.UDP[1].Any.Any)
|
|
|
assert.Contains(t, fw.InRules.UDP[1].Any.Groups[0], "g1")
|
|
@@ -83,7 +83,7 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
assert.Nil(t, fw.InRules.UDP[1].Any.CIDR.root.right)
|
|
|
assert.Nil(t, fw.InRules.UDP[1].Any.CIDR.root.value)
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoICMP, 1, 1, []string{}, "h1", nil, "", ""))
|
|
|
assert.False(t, fw.InRules.ICMP[1].Any.Any)
|
|
|
assert.Empty(t, fw.InRules.ICMP[1].Any.Groups)
|
|
@@ -92,23 +92,23 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
assert.Nil(t, fw.InRules.ICMP[1].Any.CIDR.root.right)
|
|
|
assert.Nil(t, fw.InRules.ICMP[1].Any.CIDR.root.value)
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(false, fwProtoAny, 1, 1, []string{}, "", 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.CIDR.Match(ip2int(ti.IP)))
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoUDP, 1, 1, []string{"g1"}, "", nil, "ca-name", ""))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CANames, "ca-name")
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoUDP, 1, 1, []string{"g1"}, "", nil, "", "ca-sha"))
|
|
|
assert.Contains(t, fw.InRules.UDP[1].CAShas, "ca-sha")
|
|
|
|
|
|
// Set any and clear fields
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(false, fwProtoAny, 0, 0, []string{"g1", "g2"}, "h1", ti, "", ""))
|
|
|
assert.Equal(t, []string{"g1", "g2"}, fw.OutRules.AnyProto[0].Any.Groups[0])
|
|
|
assert.Contains(t, fw.OutRules.AnyProto[0].Any.Hosts, "h1")
|
|
@@ -125,26 +125,25 @@ func TestFirewall_AddRule(t *testing.T) {
|
|
|
assert.Nil(t, fw.OutRules.AnyProto[0].Any.CIDR.root.right)
|
|
|
assert.Nil(t, fw.OutRules.AnyProto[0].Any.CIDR.root.value)
|
|
|
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, c)
|
|
|
assert.Nil(t, fw.AddRule(false, fwProtoAny, 0, 0, []string{}, "any", nil, "", ""))
|
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
|
|
|
|
- fw = NewFirewall(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")
|
|
|
assert.Nil(t, fw.AddRule(false, fwProtoAny, 0, 0, []string{}, "", anyIp, "", ""))
|
|
|
assert.True(t, fw.OutRules.AnyProto[0].Any.Any)
|
|
|
|
|
|
// Test error conditions
|
|
|
- fw = NewFirewall(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, fwProtoAny, 10, 0, []string{}, "", nil, "", ""))
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
p := FirewallPacket{
|
|
|
ip2int(net.IPv4(1, 2, 3, 4)),
|
|
@@ -177,7 +176,7 @@ func TestFirewall_Drop(t *testing.T) {
|
|
|
}
|
|
|
h.CreateRemoteCIDR(&c)
|
|
|
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"any"}, "", nil, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
@@ -196,27 +195,27 @@ func TestFirewall_Drop(t *testing.T) {
|
|
|
p.RemoteIP = oldRemote
|
|
|
|
|
|
// ensure signer doesn't get in the way of group checks
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"nope"}, "", nil, "", "signer-shasum"))
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"default-group"}, "", nil, "", "signer-shasum-bad"))
|
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
|
|
|
|
// test caSha doesn't drop on match
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"nope"}, "", nil, "", "signer-shasum-bad"))
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"default-group"}, "", nil, "", "signer-shasum"))
|
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
|
|
|
|
// ensure ca name doesn't get in the way of group checks
|
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"nope"}, "", nil, "ca-good", ""))
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"default-group"}, "", nil, "ca-good-bad", ""))
|
|
|
assert.Equal(t, fw.Drop([]byte{}, p, true, &h, cp, nil), ErrNoMatchingRule)
|
|
|
|
|
|
// test caName doesn't drop on match
|
|
|
cp.CAs["signer-shasum"] = &cert.NebulaCertificate{Details: cert.NebulaCertificateDetails{Name: "ca-good"}}
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"nope"}, "", nil, "ca-good-bad", ""))
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"default-group"}, "", nil, "ca-good", ""))
|
|
|
assert.NoError(t, fw.Drop([]byte{}, p, true, &h, cp, nil))
|
|
@@ -317,10 +316,9 @@ func BenchmarkFirewallTable_match(b *testing.B) {
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop2(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
p := FirewallPacket{
|
|
|
ip2int(net.IPv4(1, 2, 3, 4)),
|
|
@@ -365,7 +363,7 @@ func TestFirewall_Drop2(t *testing.T) {
|
|
|
}
|
|
|
h1.CreateRemoteCIDR(&c1)
|
|
|
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"default-group", "test-group"}, "", nil, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
@@ -377,10 +375,9 @@ func TestFirewall_Drop2(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestFirewall_Drop3(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
p := FirewallPacket{
|
|
|
ip2int(net.IPv4(1, 2, 3, 4)),
|
|
@@ -448,7 +445,7 @@ func TestFirewall_Drop3(t *testing.T) {
|
|
|
}
|
|
|
h3.CreateRemoteCIDR(&c3)
|
|
|
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 1, 1, []string{}, "host1", nil, "", ""))
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 1, 1, []string{}, "", nil, "", "signer-sha"))
|
|
|
cp := cert.NewCAPool()
|
|
@@ -464,10 +461,9 @@ func TestFirewall_Drop3(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
p := FirewallPacket{
|
|
|
ip2int(net.IPv4(1, 2, 3, 4)),
|
|
@@ -500,7 +496,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
}
|
|
|
h.CreateRemoteCIDR(&c)
|
|
|
|
|
|
- fw := NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw := NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 0, 0, []string{"any"}, "", nil, "", ""))
|
|
|
cp := cert.NewCAPool()
|
|
|
|
|
@@ -513,7 +509,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
assert.NoError(t, fw.Drop([]byte{}, p, false, &h, cp, nil))
|
|
|
|
|
|
oldFw := fw
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 10, 10, []string{"any"}, "", nil, "", ""))
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
@@ -522,7 +518,7 @@ func TestFirewall_DropConntrackReload(t *testing.T) {
|
|
|
assert.NoError(t, fw.Drop([]byte{}, p, false, &h, cp, nil))
|
|
|
|
|
|
oldFw = fw
|
|
|
- fw = NewFirewall(time.Second, time.Minute, time.Hour, &c)
|
|
|
+ fw = NewFirewall(l, time.Second, time.Minute, time.Hour, &c)
|
|
|
assert.Nil(t, fw.AddRule(true, fwProtoAny, 11, 11, []string{"any"}, "", nil, "", ""))
|
|
|
fw.Conntrack = oldFw.Conntrack
|
|
|
fw.rulesVersion = oldFw.rulesVersion + 1
|
|
@@ -647,124 +643,126 @@ func Test_parsePort(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestNewFirewallFromConfig(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
// Test a bad rule definition
|
|
|
c := &cert.NebulaCertificate{}
|
|
|
- conf := NewConfig()
|
|
|
+ conf := NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": "asdf"}
|
|
|
- _, err := NewFirewallFromConfig(c, conf)
|
|
|
+ _, err := NewFirewallFromConfig(l, c, conf)
|
|
|
assert.EqualError(t, err, "firewall.outbound failed to parse, should be an array of rules")
|
|
|
|
|
|
// Test both port and code
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "code": "2"}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
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
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{}}}
|
|
|
- _, err = NewFirewallFromConfig(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")
|
|
|
|
|
|
// Test code/port error
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "a", "host": "testh"}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
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"}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; port was not a number; `a`")
|
|
|
|
|
|
// Test proto error
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "host": "testh"}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; proto was not understood; ``")
|
|
|
|
|
|
// Test cidr parse error
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"code": "1", "cidr": "testh", "proto": "any"}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
assert.EqualError(t, err, "firewall.outbound rule #0; cidr did not parse; invalid CIDR address: testh")
|
|
|
|
|
|
// Test both group and groups
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a", "groups": []string{"b", "c"}}}}
|
|
|
- _, err = NewFirewallFromConfig(c, conf)
|
|
|
+ _, err = NewFirewallFromConfig(l, c, conf)
|
|
|
assert.EqualError(t, err, "firewall.inbound rule #0; only one of group or groups should be defined, both provided")
|
|
|
}
|
|
|
|
|
|
func TestAddFirewallRulesFromConfig(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
// Test adding tcp rule
|
|
|
- conf := NewConfig()
|
|
|
+ conf := NewConfig(l)
|
|
|
mf := &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "tcp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(false, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: fwProtoTCP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding udp rule
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "udp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(false, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: fwProtoUDP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding icmp rule
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"outbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "icmp", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(false, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, false, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: false, proto: fwProtoICMP, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding any rule
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: nil, host: "a", ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with ca_sha
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_sha": "12312313123"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, caSha: "12312313123"}, mf.lastCall)
|
|
|
|
|
|
// Test adding rule with ca_name
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "ca_name": "root01"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: nil, ip: nil, caName: "root01"}, mf.lastCall)
|
|
|
|
|
|
// Test single group
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "group": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test single groups
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": "a"}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: []string{"a"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test multiple AND groups
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "groups": []string{"a", "b"}}}}
|
|
|
- assert.Nil(t, AddFirewallRulesFromConfig(true, conf, mf))
|
|
|
+ assert.Nil(t, AddFirewallRulesFromConfig(l, true, conf, mf))
|
|
|
assert.Equal(t, addRuleCall{incoming: true, proto: fwProtoAny, startPort: 1, endPort: 1, groups: []string{"a", "b"}, ip: nil}, mf.lastCall)
|
|
|
|
|
|
// Test Add error
|
|
|
- conf = NewConfig()
|
|
|
+ conf = NewConfig(l)
|
|
|
mf = &mockFirewall{}
|
|
|
mf.nextCallReturn = errors.New("test error")
|
|
|
conf.Settings["firewall"] = map[interface{}]interface{}{"inbound": []interface{}{map[interface{}]interface{}{"port": "1", "proto": "any", "host": "a"}}}
|
|
|
- assert.EqualError(t, AddFirewallRulesFromConfig(true, conf, mf), "firewall.inbound rule #0; `test error`")
|
|
|
+ assert.EqualError(t, AddFirewallRulesFromConfig(l, true, conf, mf), "firewall.inbound rule #0; `test error`")
|
|
|
}
|
|
|
|
|
|
func TestTCPRTTTracking(t *testing.T) {
|
|
@@ -859,17 +857,16 @@ func TestTCPRTTTracking(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestFirewall_convertRule(t *testing.T) {
|
|
|
+ l := NewTestLogger()
|
|
|
ob := &bytes.Buffer{}
|
|
|
- out := l.Out
|
|
|
l.SetOutput(ob)
|
|
|
- defer l.SetOutput(out)
|
|
|
|
|
|
// Ensure group array of 1 is converted and a warning is printed
|
|
|
c := map[interface{}]interface{}{
|
|
|
"group": []interface{}{"group1"},
|
|
|
}
|
|
|
|
|
|
- r, err := convertRule(c, "test", 1)
|
|
|
+ r, err := convertRule(l, c, "test", 1)
|
|
|
assert.Contains(t, ob.String(), "test rule #1; group was an array with a single value, converting to simple value")
|
|
|
assert.Nil(t, err)
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
@@ -880,7 +877,7 @@ func TestFirewall_convertRule(t *testing.T) {
|
|
|
"group": []interface{}{"group1", "group2"},
|
|
|
}
|
|
|
|
|
|
- r, err = convertRule(c, "test", 1)
|
|
|
+ r, err = convertRule(l, c, "test", 1)
|
|
|
assert.Equal(t, "", ob.String())
|
|
|
assert.Error(t, err, "group should contain a single value, an array with more than one entry was provided")
|
|
|
|
|
@@ -890,7 +887,7 @@ func TestFirewall_convertRule(t *testing.T) {
|
|
|
"group": "group1",
|
|
|
}
|
|
|
|
|
|
- r, err = convertRule(c, "test", 1)
|
|
|
+ r, err = convertRule(l, c, "test", 1)
|
|
|
assert.Nil(t, err)
|
|
|
assert.Equal(t, "group1", r.Group)
|
|
|
}
|