2023-03-13 15:14:18 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-05-29 16:00:18 +02:00
|
|
|
"fmt"
|
2023-03-13 15:14:18 +01:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-01 12:02:08 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2023-11-28 13:45:26 +01:00
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2024-02-20 09:59:56 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/posture"
|
2023-03-13 15:14:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccount_getPeersByPolicy(t *testing.T) {
|
|
|
|
account := &Account{
|
2023-11-28 13:45:26 +01:00
|
|
|
Peers: map[string]*nbpeer.Peer{
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerA": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerA",
|
|
|
|
IP: net.ParseIP("100.65.14.88"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerB": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerB",
|
|
|
|
IP: net.ParseIP("100.65.80.39"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerC": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerC",
|
|
|
|
IP: net.ParseIP("100.65.254.139"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerD": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerD",
|
|
|
|
IP: net.ParseIP("100.65.62.5"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerE": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerE",
|
|
|
|
IP: net.ParseIP("100.65.32.206"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerF": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerF",
|
|
|
|
IP: net.ParseIP("100.65.250.202"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerG": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerG",
|
|
|
|
IP: net.ParseIP("100.65.13.186"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerH": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerH",
|
|
|
|
IP: net.ParseIP("100.65.29.55"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
|
|
|
},
|
2024-03-27 18:48:48 +01:00
|
|
|
Groups: map[string]*nbgroup.Group{
|
2023-05-29 16:00:18 +02:00
|
|
|
"GroupAll": {
|
|
|
|
ID: "GroupAll",
|
2023-04-01 12:02:08 +02:00
|
|
|
Name: "All",
|
|
|
|
Peers: []string{
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerB",
|
|
|
|
"peerA",
|
|
|
|
"peerD",
|
|
|
|
"peerC",
|
|
|
|
"peerE",
|
|
|
|
"peerF",
|
|
|
|
"peerG",
|
|
|
|
"peerH",
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
"GroupSwarm": {
|
|
|
|
ID: "GroupSwarm",
|
2023-04-01 12:02:08 +02:00
|
|
|
Name: "swarm",
|
|
|
|
Peers: []string{
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerB",
|
|
|
|
"peerA",
|
|
|
|
"peerD",
|
|
|
|
"peerE",
|
|
|
|
"peerG",
|
|
|
|
"peerH",
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
|
|
|
},
|
2024-02-19 17:17:36 +01:00
|
|
|
Policies: []*Policy{
|
|
|
|
{
|
2023-05-29 16:00:18 +02:00
|
|
|
ID: "RuleDefault",
|
2023-04-01 12:02:08 +02:00
|
|
|
Name: "Default",
|
|
|
|
Description: "This is a default rule that allows connections between all the resources",
|
2024-02-19 17:17:36 +01:00
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleDefault",
|
|
|
|
Name: "Default",
|
|
|
|
Description: "This is a default rule that allows connections between all the resources",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: true,
|
|
|
|
Protocol: PolicyRuleProtocolALL,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Sources: []string{
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
|
|
|
},
|
2024-02-19 17:17:36 +01:00
|
|
|
{
|
2023-05-29 16:00:18 +02:00
|
|
|
ID: "RuleSwarm",
|
2023-04-01 12:02:08 +02:00
|
|
|
Name: "Swarm",
|
2024-02-19 17:17:36 +01:00
|
|
|
Description: "No description",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleSwarm",
|
|
|
|
Name: "Swarm",
|
|
|
|
Description: "No description",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: true,
|
|
|
|
Protocol: PolicyRuleProtocolALL,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Sources: []string{
|
|
|
|
"GroupSwarm",
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"GroupSwarm",
|
|
|
|
},
|
|
|
|
},
|
2023-04-01 12:02:08 +02:00
|
|
|
},
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
validatedPeers := make(map[string]struct{})
|
|
|
|
for p := range account.Peers {
|
|
|
|
validatedPeers[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2023-04-01 12:02:08 +02:00
|
|
|
t.Run("check that all peers get map", func(t *testing.T) {
|
|
|
|
for _, p := range account.Peers {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources(p.ID, validatedPeers)
|
2023-11-01 17:11:16 +01:00
|
|
|
assert.GreaterOrEqual(t, len(peers), 2, "minimum number peers should present")
|
|
|
|
assert.GreaterOrEqual(t, len(firewallRules), 2, "minimum number of firewall rules should present")
|
2023-04-01 12:02:08 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check first peer map details", func(t *testing.T) {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerB", validatedPeers)
|
2023-04-01 12:02:08 +02:00
|
|
|
assert.Len(t, peers, 7)
|
2023-05-29 16:00:18 +02:00
|
|
|
assert.Contains(t, peers, account.Peers["peerA"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerE"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerF"])
|
2023-04-01 12:02:08 +02:00
|
|
|
|
|
|
|
epectedFirewallRules := []*FirewallRule{
|
2023-07-18 11:12:50 +02:00
|
|
|
{
|
|
|
|
PeerIP: "0.0.0.0",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "0.0.0.0",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
2023-04-01 12:02:08 +02:00
|
|
|
{
|
|
|
|
PeerIP: "100.65.14.88",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.14.88",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.62.5",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.62.5",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.32.206",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.32.206",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.250.202",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.250.202",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.13.186",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.13.186",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.29.55",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionOUT,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.29.55",
|
2023-05-29 16:00:18 +02:00
|
|
|
Direction: firewallRuleDirectionIN,
|
2023-04-01 12:02:08 +02:00
|
|
|
Action: "accept",
|
2023-05-29 16:00:18 +02:00
|
|
|
Protocol: "all",
|
2023-04-01 12:02:08 +02:00
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(epectedFirewallRules))
|
2023-05-29 16:00:18 +02:00
|
|
|
slices.SortFunc(epectedFirewallRules, sortFunc())
|
|
|
|
slices.SortFunc(firewallRules, sortFunc())
|
2023-04-01 12:02:08 +02:00
|
|
|
for i := range firewallRules {
|
|
|
|
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])
|
|
|
|
}
|
|
|
|
})
|
2023-03-13 15:14:18 +01:00
|
|
|
}
|
2023-05-29 16:00:18 +02:00
|
|
|
|
|
|
|
func TestAccount_getPeersByPolicyDirect(t *testing.T) {
|
|
|
|
account := &Account{
|
2023-11-28 13:45:26 +01:00
|
|
|
Peers: map[string]*nbpeer.Peer{
|
2023-05-29 16:00:18 +02:00
|
|
|
"peerA": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerA",
|
|
|
|
IP: net.ParseIP("100.65.14.88"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
"peerB": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerB",
|
|
|
|
IP: net.ParseIP("100.65.80.39"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
"peerC": {
|
2023-12-06 15:02:10 +01:00
|
|
|
ID: "peerC",
|
|
|
|
IP: net.ParseIP("100.65.254.139"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
},
|
2024-03-27 18:48:48 +01:00
|
|
|
Groups: map[string]*nbgroup.Group{
|
2023-05-29 16:00:18 +02:00
|
|
|
"GroupAll": {
|
|
|
|
ID: "GroupAll",
|
|
|
|
Name: "All",
|
|
|
|
Peers: []string{
|
|
|
|
"peerB",
|
|
|
|
"peerA",
|
|
|
|
"peerC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"GroupSwarm": {
|
|
|
|
ID: "GroupSwarm",
|
|
|
|
Name: "swarm",
|
|
|
|
Peers: []string{
|
|
|
|
"peerB",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerF": {
|
|
|
|
ID: "peerF",
|
|
|
|
Name: "dmz",
|
|
|
|
Peers: []string{
|
|
|
|
"peerC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-02-19 17:17:36 +01:00
|
|
|
Policies: []*Policy{
|
|
|
|
{
|
2023-05-29 16:00:18 +02:00
|
|
|
ID: "RuleDefault",
|
|
|
|
Name: "Default",
|
|
|
|
Description: "This is a default rule that allows connections between all the resources",
|
2024-02-19 17:17:36 +01:00
|
|
|
Enabled: false,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleDefault",
|
|
|
|
Name: "Default",
|
|
|
|
Description: "This is a default rule that allows connections between all the resources",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: false,
|
|
|
|
Protocol: PolicyRuleProtocolALL,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Sources: []string{
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
},
|
2024-02-19 17:17:36 +01:00
|
|
|
{
|
2023-05-29 16:00:18 +02:00
|
|
|
ID: "RuleSwarm",
|
|
|
|
Name: "Swarm",
|
2024-02-19 17:17:36 +01:00
|
|
|
Description: "No description",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleSwarm",
|
|
|
|
Name: "Swarm",
|
|
|
|
Description: "No description",
|
|
|
|
Bidirectional: true,
|
|
|
|
Enabled: true,
|
|
|
|
Protocol: PolicyRuleProtocolALL,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Sources: []string{
|
|
|
|
"GroupSwarm",
|
|
|
|
},
|
|
|
|
Destinations: []string{
|
|
|
|
"peerF",
|
|
|
|
},
|
|
|
|
},
|
2023-05-29 16:00:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
approvedPeers := make(map[string]struct{})
|
|
|
|
for p := range account.Peers {
|
|
|
|
approvedPeers[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2023-05-29 16:00:18 +02:00
|
|
|
t.Run("check first peer map", func(t *testing.T) {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerB", approvedPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
|
|
|
|
epectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(epectedFirewallRules))
|
|
|
|
slices.SortFunc(epectedFirewallRules, sortFunc())
|
|
|
|
slices.SortFunc(firewallRules, sortFunc())
|
|
|
|
for i := range firewallRules {
|
|
|
|
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check second peer map", func(t *testing.T) {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerC", approvedPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
assert.Contains(t, peers, account.Peers["peerB"])
|
|
|
|
|
|
|
|
epectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.80.39",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.80.39",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(epectedFirewallRules))
|
|
|
|
slices.SortFunc(epectedFirewallRules, sortFunc())
|
|
|
|
slices.SortFunc(firewallRules, sortFunc())
|
|
|
|
for i := range firewallRules {
|
|
|
|
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
account.Policies[1].Rules[0].Bidirectional = false
|
|
|
|
|
|
|
|
t.Run("check first peer map directional only", func(t *testing.T) {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerB", approvedPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
|
|
|
|
epectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(epectedFirewallRules))
|
|
|
|
slices.SortFunc(epectedFirewallRules, sortFunc())
|
|
|
|
slices.SortFunc(firewallRules, sortFunc())
|
|
|
|
for i := range firewallRules {
|
|
|
|
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check second peer map directional only", func(t *testing.T) {
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerC", approvedPeers)
|
2023-05-29 16:00:18 +02:00
|
|
|
assert.Contains(t, peers, account.Peers["peerB"])
|
|
|
|
|
|
|
|
epectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.80.39",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "all",
|
|
|
|
Port: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(epectedFirewallRules))
|
|
|
|
slices.SortFunc(epectedFirewallRules, sortFunc())
|
|
|
|
slices.SortFunc(firewallRules, sortFunc())
|
|
|
|
for i := range firewallRules {
|
|
|
|
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-20 09:59:56 +01:00
|
|
|
func TestAccount_getPeersByPolicyPostureChecks(t *testing.T) {
|
|
|
|
account := &Account{
|
|
|
|
Peers: map[string]*nbpeer.Peer{
|
|
|
|
"peerA": {
|
|
|
|
ID: "peerA",
|
|
|
|
IP: net.ParseIP("100.65.14.88"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.7",
|
|
|
|
WtVersion: "0.25.9",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerB": {
|
|
|
|
ID: "peerB",
|
|
|
|
IP: net.ParseIP("100.65.80.39"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.23.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerC": {
|
|
|
|
ID: "peerC",
|
|
|
|
IP: net.ParseIP("100.65.254.139"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.25.8",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerD": {
|
|
|
|
ID: "peerD",
|
|
|
|
IP: net.ParseIP("100.65.62.5"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.0",
|
|
|
|
WtVersion: "0.25.9",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerE": {
|
|
|
|
ID: "peerE",
|
|
|
|
IP: net.ParseIP("100.65.32.206"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.24.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerF": {
|
|
|
|
ID: "peerF",
|
|
|
|
IP: net.ParseIP("100.65.250.202"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.25.9",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerG": {
|
|
|
|
ID: "peerG",
|
|
|
|
IP: net.ParseIP("100.65.13.186"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.23.2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerH": {
|
|
|
|
ID: "peerH",
|
|
|
|
IP: net.ParseIP("100.65.29.55"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "linux",
|
|
|
|
KernelVersion: "6.6.1",
|
|
|
|
WtVersion: "0.23.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"peerI": {
|
|
|
|
ID: "peerI",
|
|
|
|
IP: net.ParseIP("100.65.21.56"),
|
|
|
|
Status: &nbpeer.PeerStatus{},
|
|
|
|
Meta: nbpeer.PeerSystemMeta{
|
|
|
|
GoOS: "windows",
|
|
|
|
KernelVersion: "10.0.14393.2430",
|
|
|
|
WtVersion: "0.25.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-03-27 18:48:48 +01:00
|
|
|
Groups: map[string]*nbgroup.Group{
|
2024-02-20 09:59:56 +01:00
|
|
|
"GroupAll": {
|
|
|
|
ID: "GroupAll",
|
|
|
|
Name: "All",
|
|
|
|
Peers: []string{
|
|
|
|
"peerB",
|
|
|
|
"peerA",
|
|
|
|
"peerD",
|
|
|
|
"peerC",
|
|
|
|
"peerF",
|
|
|
|
"peerG",
|
|
|
|
"peerH",
|
|
|
|
"peerI",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"GroupSwarm": {
|
|
|
|
ID: "GroupSwarm",
|
|
|
|
Name: "swarm",
|
|
|
|
Peers: []string{
|
|
|
|
"peerB",
|
|
|
|
"peerA",
|
|
|
|
"peerD",
|
|
|
|
"peerE",
|
|
|
|
"peerG",
|
|
|
|
"peerH",
|
|
|
|
"peerI",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PostureChecks: []*posture.Checks{
|
|
|
|
{
|
|
|
|
ID: "PostureChecksDefault",
|
|
|
|
Name: "Default",
|
|
|
|
Description: "This is a posture checks that check if peer is running required versions",
|
|
|
|
Checks: posture.ChecksDefinition{
|
|
|
|
NBVersionCheck: &posture.NBVersionCheck{
|
|
|
|
MinVersion: "0.25",
|
|
|
|
},
|
|
|
|
OSVersionCheck: &posture.OSVersionCheck{
|
|
|
|
Linux: &posture.MinKernelVersionCheck{
|
|
|
|
MinKernelVersion: "6.6.0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Policies = append(account.Policies, &Policy{
|
|
|
|
ID: "PolicyPostureChecks",
|
|
|
|
Name: "",
|
|
|
|
Description: "This is the policy with posture checks applied",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "RuleSwarm",
|
|
|
|
Name: "Swarm",
|
|
|
|
Enabled: true,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
Destinations: []string{
|
|
|
|
"GroupSwarm",
|
|
|
|
},
|
|
|
|
Sources: []string{
|
|
|
|
"GroupAll",
|
|
|
|
},
|
|
|
|
Bidirectional: false,
|
|
|
|
Protocol: PolicyRuleProtocolTCP,
|
|
|
|
Ports: []string{"80"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SourcePostureChecks: []string{
|
|
|
|
"PostureChecksDefault",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
approvedPeers := make(map[string]struct{})
|
|
|
|
for p := range account.Peers {
|
|
|
|
approvedPeers[p] = struct{}{}
|
|
|
|
}
|
2024-02-20 09:59:56 +01:00
|
|
|
t.Run("verify peer's network map with default group peer list", func(t *testing.T) {
|
|
|
|
// peerB doesn't fulfill the NB posture check but is included in the destination group Swarm,
|
|
|
|
// will establish a connection with all source peers satisfying the NB posture check.
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerB", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 4)
|
|
|
|
assert.Len(t, firewallRules, 4)
|
|
|
|
assert.Contains(t, peers, account.Peers["peerA"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerF"])
|
|
|
|
|
|
|
|
// peerC satisfy the NB posture check, should establish connection to all destination group peer's
|
|
|
|
// We expect a single permissive firewall rule which all outgoing connections
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerC", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, len(account.Groups["GroupSwarm"].Peers))
|
|
|
|
assert.Len(t, firewallRules, 1)
|
|
|
|
expectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "0.0.0.0",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, firewallRules, expectedFirewallRules)
|
|
|
|
|
|
|
|
// peerE doesn't fulfill the NB posture check and exists in only destination group Swarm,
|
|
|
|
// all source group peers satisfying the NB posture check should establish connection
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerE", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 4)
|
|
|
|
assert.Len(t, firewallRules, 4)
|
|
|
|
assert.Contains(t, peers, account.Peers["peerA"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerF"])
|
|
|
|
|
|
|
|
// peerI doesn't fulfill the OS version posture check and exists in only destination group Swarm,
|
|
|
|
// all source group peers satisfying the NB posture check should establish connection
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerI", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 4)
|
|
|
|
assert.Len(t, firewallRules, 4)
|
|
|
|
assert.Contains(t, peers, account.Peers["peerA"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerF"])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("verify peer's network map with modified group peer list", func(t *testing.T) {
|
|
|
|
// Removing peerB as the part of destination group Swarm
|
|
|
|
account.Groups["GroupSwarm"].Peers = []string{"peerA", "peerD", "peerE", "peerG", "peerH"}
|
|
|
|
|
|
|
|
// peerB doesn't satisfy the NB posture check, and doesn't exist in destination group peer's
|
|
|
|
// no connection should be established to any peer of destination group
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules := account.getPeerConnectionResources("peerB", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 0)
|
|
|
|
assert.Len(t, firewallRules, 0)
|
|
|
|
|
|
|
|
// peerI doesn't satisfy the OS version posture check, and doesn't exist in destination group peer's
|
|
|
|
// no connection should be established to any peer of destination group
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerI", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 0)
|
|
|
|
assert.Len(t, firewallRules, 0)
|
|
|
|
|
|
|
|
// peerC satisfy the NB posture check, should establish connection to all destination group peer's
|
|
|
|
// We expect a single permissive firewall rule which all outgoing connections
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerC", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, len(account.Groups["GroupSwarm"].Peers))
|
|
|
|
assert.Len(t, firewallRules, len(account.Groups["GroupSwarm"].Peers))
|
|
|
|
|
|
|
|
peerIDs := make([]string, 0, len(peers))
|
|
|
|
for _, peer := range peers {
|
|
|
|
peerIDs = append(peerIDs, peer.ID)
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, peerIDs, account.Groups["GroupSwarm"].Peers)
|
|
|
|
|
|
|
|
// Removing peerF as the part of source group All
|
|
|
|
account.Groups["GroupAll"].Peers = []string{"peerB", "peerA", "peerD", "peerC", "peerG", "peerH"}
|
|
|
|
|
|
|
|
// peerE doesn't fulfill the NB posture check and exists in only destination group Swarm,
|
|
|
|
// all source group peers satisfying the NB posture check should establish connection
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerE", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 3)
|
|
|
|
assert.Len(t, firewallRules, 3)
|
|
|
|
assert.Contains(t, peers, account.Peers["peerA"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
peers, firewallRules = account.getPeerConnectionResources("peerA", approvedPeers)
|
2024-02-20 09:59:56 +01:00
|
|
|
assert.Len(t, peers, 5)
|
|
|
|
// assert peers from Group Swarm
|
|
|
|
assert.Contains(t, peers, account.Peers["peerD"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerE"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerG"])
|
|
|
|
assert.Contains(t, peers, account.Peers["peerH"])
|
|
|
|
|
|
|
|
// assert peers from Group All
|
|
|
|
assert.Contains(t, peers, account.Peers["peerC"])
|
|
|
|
|
|
|
|
expectedFirewallRules := []*FirewallRule{
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.62.5",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.32.206",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.13.186",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.29.55",
|
|
|
|
Direction: firewallRuleDirectionOUT,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.254.139",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PeerIP: "100.65.62.5",
|
|
|
|
Direction: firewallRuleDirectionIN,
|
|
|
|
Action: "accept",
|
|
|
|
Protocol: "tcp",
|
|
|
|
Port: "80",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Len(t, firewallRules, len(expectedFirewallRules))
|
|
|
|
assert.ElementsMatch(t, firewallRules, expectedFirewallRules)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:25:35 +01:00
|
|
|
func sortFunc() func(a *FirewallRule, b *FirewallRule) int {
|
|
|
|
return func(a, b *FirewallRule) int {
|
|
|
|
// Concatenate PeerIP and Direction as string for comparison
|
|
|
|
aStr := a.PeerIP + fmt.Sprintf("%d", a.Direction)
|
|
|
|
bStr := b.PeerIP + fmt.Sprintf("%d", b.Direction)
|
|
|
|
|
|
|
|
// Compare the concatenated strings
|
|
|
|
if aStr < bStr {
|
|
|
|
return -1 // a is less than b
|
|
|
|
}
|
|
|
|
if aStr > bStr {
|
|
|
|
return 1 // a is greater than b
|
|
|
|
}
|
|
|
|
return 0 // a is equal to b
|
2023-05-29 16:00:18 +02:00
|
|
|
}
|
|
|
|
}
|