2023-05-29 16:00:18 +02:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2023-12-08 10:48:21 +01:00
|
|
|
"context"
|
2023-09-05 21:07:32 +02:00
|
|
|
"net"
|
2023-05-29 16:00:18 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
|
2023-12-08 10:48:21 +01:00
|
|
|
"github.com/netbirdio/netbird/client/firewall"
|
|
|
|
"github.com/netbirdio/netbird/client/firewall/manager"
|
2023-05-29 16:00:18 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/acl/mocks"
|
2023-09-05 21:07:32 +02:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
2023-05-29 16:00:18 +02:00
|
|
|
mgmProto "github.com/netbirdio/netbird/management/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDefaultManager(t *testing.T) {
|
2023-06-02 08:14:47 +02:00
|
|
|
networkMap := &mgmProto.NetworkMap{
|
|
|
|
FirewallRules: []*mgmProto.FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_TCP,
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_DROP,
|
|
|
|
Protocol: mgmProto.FirewallRule_UDP,
|
|
|
|
Port: "53",
|
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
|
2024-01-11 12:21:58 +01:00
|
|
|
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
|
2023-09-05 21:07:32 +02:00
|
|
|
ifaceMock.EXPECT().SetFilter(gomock.Any())
|
|
|
|
ip, network, err := net.ParseCIDR("172.0.0.1/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse IP address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
|
|
|
|
ifaceMock.EXPECT().Address().Return(iface.WGAddress{
|
|
|
|
IP: ip,
|
|
|
|
Network: network,
|
|
|
|
}).AnyTimes()
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
// we receive one rule from the management so for testing purposes ignore it
|
2023-12-08 10:48:21 +01:00
|
|
|
fw, err := firewall.NewFirewall(context.Background(), ifaceMock)
|
2023-05-29 16:00:18 +02:00
|
|
|
if err != nil {
|
2023-12-08 10:48:21 +01:00
|
|
|
t.Errorf("create firewall: %v", err)
|
2023-05-29 16:00:18 +02:00
|
|
|
return
|
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
defer func(fw manager.Manager) {
|
|
|
|
_ = fw.Reset()
|
|
|
|
}(fw)
|
|
|
|
acl := NewDefaultManager(fw)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
t.Run("apply firewall rules", func(t *testing.T) {
|
2023-06-02 08:14:47 +02:00
|
|
|
acl.ApplyFiltering(networkMap)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
if len(acl.rulesPairs) != 2 {
|
|
|
|
t.Errorf("firewall rules not applied: %v", acl.rulesPairs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("add extra rules", func(t *testing.T) {
|
2023-06-20 20:33:41 +02:00
|
|
|
existedPairs := map[string]struct{}{}
|
|
|
|
for id := range acl.rulesPairs {
|
|
|
|
existedPairs[id] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
// remove first rule
|
2023-06-02 08:14:47 +02:00
|
|
|
networkMap.FirewallRules = networkMap.FirewallRules[1:]
|
|
|
|
networkMap.FirewallRules = append(
|
|
|
|
networkMap.FirewallRules,
|
|
|
|
&mgmProto.FirewallRule{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_DROP,
|
|
|
|
Protocol: mgmProto.FirewallRule_ICMP,
|
|
|
|
},
|
|
|
|
)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
2023-06-02 08:14:47 +02:00
|
|
|
acl.ApplyFiltering(networkMap)
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
// we should have one old and one new rule in the existed rules
|
|
|
|
if len(acl.rulesPairs) != 2 {
|
|
|
|
t.Errorf("firewall rules not applied")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:33:41 +02:00
|
|
|
// check that old rule was removed
|
|
|
|
previousCount := 0
|
|
|
|
for id := range acl.rulesPairs {
|
|
|
|
if _, ok := existedPairs[id]; ok {
|
|
|
|
previousCount++
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-20 20:33:41 +02:00
|
|
|
if previousCount != 1 {
|
|
|
|
t.Errorf("old rule was not removed")
|
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
})
|
2023-05-31 19:04:38 +02:00
|
|
|
|
|
|
|
t.Run("handle default rules", func(t *testing.T) {
|
2023-06-02 08:14:47 +02:00
|
|
|
networkMap.FirewallRules = networkMap.FirewallRules[:0]
|
|
|
|
|
|
|
|
networkMap.FirewallRulesIsEmpty = true
|
|
|
|
if acl.ApplyFiltering(networkMap); len(acl.rulesPairs) != 0 {
|
|
|
|
t.Errorf("rules should be empty if FirewallRulesIsEmpty is set, got: %v", len(acl.rulesPairs))
|
2023-05-31 19:04:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-02 08:14:47 +02:00
|
|
|
networkMap.FirewallRulesIsEmpty = false
|
|
|
|
acl.ApplyFiltering(networkMap)
|
2023-05-31 19:04:38 +02:00
|
|
|
if len(acl.rulesPairs) != 2 {
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("rules should contain 2 rules if FirewallRulesIsEmpty is not set, got: %v", len(acl.rulesPairs))
|
2023-05-31 19:04:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
2023-06-02 08:14:47 +02:00
|
|
|
|
|
|
|
func TestDefaultManagerSquashRules(t *testing.T) {
|
|
|
|
networkMap := &mgmProto.NetworkMap{
|
|
|
|
RemotePeers: []*mgmProto.RemotePeerConfig{
|
|
|
|
{AllowedIps: []string{"10.93.0.1"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.2"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.3"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.4"}},
|
|
|
|
},
|
|
|
|
FirewallRules: []*mgmProto.FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.4",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.4",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
manager := &DefaultManager{}
|
2023-06-02 13:26:33 +02:00
|
|
|
rules, _ := manager.squashAcceptRules(networkMap)
|
2023-06-02 08:14:47 +02:00
|
|
|
if len(rules) != 2 {
|
|
|
|
t.Errorf("rules should contain 2, got: %v", rules)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := rules[0]
|
2023-11-27 16:40:02 +01:00
|
|
|
switch {
|
|
|
|
case r.PeerIP != "0.0.0.0":
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("IP should be 0.0.0.0, got: %v", r.PeerIP)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Direction != mgmProto.FirewallRule_IN:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("direction should be IN, got: %v", r.Direction)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Protocol != mgmProto.FirewallRule_ALL:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("protocol should be ALL, got: %v", r.Protocol)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Action != mgmProto.FirewallRule_ACCEPT:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("action should be ACCEPT, got: %v", r.Action)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r = rules[1]
|
2023-11-27 16:40:02 +01:00
|
|
|
switch {
|
|
|
|
case r.PeerIP != "0.0.0.0":
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("IP should be 0.0.0.0, got: %v", r.PeerIP)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Direction != mgmProto.FirewallRule_OUT:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("direction should be OUT, got: %v", r.Direction)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Protocol != mgmProto.FirewallRule_ALL:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("protocol should be ALL, got: %v", r.Protocol)
|
|
|
|
return
|
2023-11-27 16:40:02 +01:00
|
|
|
case r.Action != mgmProto.FirewallRule_ACCEPT:
|
2023-06-02 08:14:47 +02:00
|
|
|
t.Errorf("action should be ACCEPT, got: %v", r.Action)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultManagerSquashRulesNoAffect(t *testing.T) {
|
|
|
|
networkMap := &mgmProto.NetworkMap{
|
|
|
|
RemotePeers: []*mgmProto.RemotePeerConfig{
|
|
|
|
{AllowedIps: []string{"10.93.0.1"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.2"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.3"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.4"}},
|
|
|
|
},
|
|
|
|
FirewallRules: []*mgmProto.FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.4",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_TCP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_ALL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.4",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_UDP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
manager := &DefaultManager{}
|
2023-06-02 13:26:33 +02:00
|
|
|
if rules, _ := manager.squashAcceptRules(networkMap); len(rules) != len(networkMap.FirewallRules) {
|
2023-11-07 13:37:57 +01:00
|
|
|
t.Errorf("we should get the same amount of rules as output, got %v", len(rules))
|
2023-06-02 08:14:47 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-02 13:26:33 +02:00
|
|
|
|
|
|
|
func TestDefaultManagerEnableSSHRules(t *testing.T) {
|
|
|
|
networkMap := &mgmProto.NetworkMap{
|
|
|
|
PeerConfig: &mgmProto.PeerConfig{
|
|
|
|
SshConfig: &mgmProto.SSHConfig{
|
|
|
|
SshEnabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RemotePeers: []*mgmProto.RemotePeerConfig{
|
|
|
|
{AllowedIps: []string{"10.93.0.1"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.2"}},
|
|
|
|
{AllowedIps: []string{"10.93.0.3"}},
|
|
|
|
},
|
|
|
|
FirewallRules: []*mgmProto.FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.1",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_TCP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.2",
|
|
|
|
Direction: mgmProto.FirewallRule_IN,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_TCP,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "10.93.0.3",
|
|
|
|
Direction: mgmProto.FirewallRule_OUT,
|
|
|
|
Action: mgmProto.FirewallRule_ACCEPT,
|
|
|
|
Protocol: mgmProto.FirewallRule_UDP,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
2023-09-05 21:07:32 +02:00
|
|
|
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
|
2024-01-11 12:21:58 +01:00
|
|
|
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
|
2023-09-05 21:07:32 +02:00
|
|
|
ifaceMock.EXPECT().SetFilter(gomock.Any())
|
|
|
|
ip, network, err := net.ParseCIDR("172.0.0.1/32")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to parse IP address: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
|
|
|
|
ifaceMock.EXPECT().Address().Return(iface.WGAddress{
|
|
|
|
IP: ip,
|
|
|
|
Network: network,
|
|
|
|
}).AnyTimes()
|
2023-06-02 13:26:33 +02:00
|
|
|
|
|
|
|
// we receive one rule from the management so for testing purposes ignore it
|
2023-12-08 10:48:21 +01:00
|
|
|
fw, err := firewall.NewFirewall(context.Background(), ifaceMock)
|
2023-06-02 13:26:33 +02:00
|
|
|
if err != nil {
|
2023-12-08 10:48:21 +01:00
|
|
|
t.Errorf("create firewall: %v", err)
|
2023-06-02 13:26:33 +02:00
|
|
|
return
|
|
|
|
}
|
2023-12-08 10:48:21 +01:00
|
|
|
defer func(fw manager.Manager) {
|
|
|
|
_ = fw.Reset()
|
|
|
|
}(fw)
|
|
|
|
acl := NewDefaultManager(fw)
|
2023-06-02 13:26:33 +02:00
|
|
|
|
|
|
|
acl.ApplyFiltering(networkMap)
|
|
|
|
|
|
|
|
if len(acl.rulesPairs) != 4 {
|
|
|
|
t.Errorf("expect 4 rules (last must be SSH), got: %d", len(acl.rulesPairs))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|