2022-01-16 17:10:36 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2023-02-13 12:21:02 +01:00
|
|
|
"time"
|
2022-05-21 15:21:39 +02:00
|
|
|
|
|
|
|
"github.com/rs/xid"
|
2024-03-27 16:11:45 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-05-21 15:21:39 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
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"
|
2022-01-16 17:10:36 +01:00
|
|
|
)
|
|
|
|
|
2023-02-13 12:21:02 +01:00
|
|
|
func TestPeer_LoginExpired(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
2023-02-13 15:07:15 +01:00
|
|
|
expirationEnabled bool
|
2023-02-13 12:21:02 +01:00
|
|
|
lastLogin time.Time
|
|
|
|
expected bool
|
2023-02-13 15:07:15 +01:00
|
|
|
accountSettings *Settings
|
2023-02-13 12:21:02 +01:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Peer Login Expiration Disabled. Peer Login Should Not Expire",
|
2023-02-13 15:07:15 +01:00
|
|
|
expirationEnabled: false,
|
2023-04-03 15:09:35 +02:00
|
|
|
lastLogin: time.Now().UTC().Add(-25 * time.Hour),
|
2023-02-13 15:07:15 +01:00
|
|
|
accountSettings: &Settings{
|
|
|
|
PeerLoginExpirationEnabled: true,
|
|
|
|
PeerLoginExpiration: time.Hour,
|
|
|
|
},
|
|
|
|
expected: false,
|
2023-02-13 12:21:02 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer Login Should Expire",
|
2023-02-13 15:07:15 +01:00
|
|
|
expirationEnabled: true,
|
2023-04-03 15:09:35 +02:00
|
|
|
lastLogin: time.Now().UTC().Add(-25 * time.Hour),
|
2023-02-13 15:07:15 +01:00
|
|
|
accountSettings: &Settings{
|
|
|
|
PeerLoginExpirationEnabled: true,
|
|
|
|
PeerLoginExpiration: time.Hour,
|
|
|
|
},
|
|
|
|
expected: true,
|
2023-02-13 12:21:02 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Peer Login Should Not Expire",
|
2023-02-13 15:07:15 +01:00
|
|
|
expirationEnabled: true,
|
2023-04-03 15:09:35 +02:00
|
|
|
lastLogin: time.Now().UTC(),
|
2023-02-13 15:07:15 +01:00
|
|
|
accountSettings: &Settings{
|
|
|
|
PeerLoginExpirationEnabled: true,
|
|
|
|
PeerLoginExpiration: time.Hour,
|
|
|
|
},
|
|
|
|
expected: false,
|
2023-02-13 12:21:02 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range tt {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2023-11-28 13:45:26 +01:00
|
|
|
peer := &nbpeer.Peer{
|
2023-02-13 15:07:15 +01:00
|
|
|
LoginExpirationEnabled: c.expirationEnabled,
|
2023-02-13 12:21:02 +01:00
|
|
|
LastLogin: c.lastLogin,
|
2023-03-02 12:45:10 +01:00
|
|
|
UserID: userID,
|
2023-02-13 12:21:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 16:44:26 +01:00
|
|
|
expired, _ := peer.LoginExpired(c.accountSettings.PeerLoginExpiration)
|
2023-02-13 12:21:02 +01:00
|
|
|
assert.Equal(t, expired, c.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:10:36 +01:00
|
|
|
func TestAccountManager_GetNetworkMap(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedId := "test_account"
|
|
|
|
userId := "account_creator"
|
2022-06-09 13:14:34 +02:00
|
|
|
account, err := createAccount(manager, expectedId, userId, "")
|
2022-01-16 17:10:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-09-04 11:37:39 +02:00
|
|
|
setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userId, false)
|
2023-06-27 17:17:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating setup key")
|
|
|
|
return
|
2022-01-16 17:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
peerKey1, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer1, _, err := manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-01-16 17:10:36 +01:00
|
|
|
Key: peerKey1.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-1"},
|
2022-01-16 17:10:36 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey2, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-28 13:45:26 +01:00
|
|
|
_, _, err = manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-01-16 17:10:36 +01:00
|
|
|
Key: peerKey2.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
2022-01-16 17:10:36 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
networkMap, err := manager.GetNetworkMap(peer1.ID)
|
2022-01-16 17:10:36 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap.Peers) != 1 {
|
|
|
|
t.Errorf("expecting Account NetworkMap to have 1 peers, got %v", len(networkMap.Peers))
|
2023-03-13 15:14:18 +01:00
|
|
|
return
|
2022-01-16 17:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkMap.Peers[0].Key != peerKey2.PublicKey().String() {
|
2022-05-21 15:21:39 +02:00
|
|
|
t.Errorf(
|
|
|
|
"expecting Account NetworkMap to have peer with a key %s, got %s",
|
|
|
|
peerKey2.PublicKey().String(),
|
|
|
|
networkMap.Peers[0].Key,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
func TestAccountManager_GetNetworkMapWithPolicy(t *testing.T) {
|
2023-04-12 09:39:17 +02:00
|
|
|
// TODO: disable until we start use policy again
|
|
|
|
t.Skip()
|
2022-05-21 15:21:39 +02:00
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
expectedID := "test_account"
|
|
|
|
userID := "account_creator"
|
|
|
|
account, err := createAccount(manager, expectedID, userID, "")
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var setupKey *SetupKey
|
|
|
|
for _, key := range account.SetupKeys {
|
|
|
|
if key.Type == SetupKeyReusable {
|
|
|
|
setupKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey1, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer1, _, err := manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-05-21 15:21:39 +02:00
|
|
|
Key: peerKey1.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-1"},
|
2022-05-21 15:21:39 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey2, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
2022-01-16 17:10:36 +01:00
|
|
|
}
|
2023-11-28 13:45:26 +01:00
|
|
|
peer2, _, err := manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-05-21 15:21:39 +02:00
|
|
|
Key: peerKey2.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
2022-05-21 15:21:39 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policies, err := manager.ListPolicies(account.Id, userID)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting to get a list of rules, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
err = manager.DeletePolicy(account.Id, policies[0].ID, userID)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting to delete 1 group, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
2024-03-27 18:48:48 +01:00
|
|
|
group1 nbgroup.Group
|
|
|
|
group2 nbgroup.Group
|
2023-03-13 15:14:18 +01:00
|
|
|
policy Policy
|
2022-05-21 15:21:39 +02:00
|
|
|
)
|
2022-01-16 17:10:36 +01:00
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
group1.ID = xid.New().String()
|
|
|
|
group2.ID = xid.New().String()
|
|
|
|
group1.Name = "src"
|
|
|
|
group2.Name = "dst"
|
2023-03-13 15:14:18 +01:00
|
|
|
policy.ID = xid.New().String()
|
2023-02-03 10:33:28 +01:00
|
|
|
group1.Peers = append(group1.Peers, peer1.ID)
|
|
|
|
group2.Peers = append(group2.Peers, peer2.ID)
|
2022-05-21 15:21:39 +02:00
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = manager.SaveGroup(account.Id, userID, &group1)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting group1 to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
err = manager.SaveGroup(account.Id, userID, &group2)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting group2 to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy.Name = "test"
|
|
|
|
policy.Enabled = true
|
|
|
|
policy.Rules = []*PolicyRule{
|
|
|
|
{
|
2023-05-29 16:00:18 +02:00
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{group1.ID},
|
|
|
|
Destinations: []string{group2.ID},
|
|
|
|
Bidirectional: true,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
2023-03-13 15:14:18 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
err = manager.SavePolicy(account.Id, userID, &policy)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting rule to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
networkMap1, err := manager.GetNetworkMap(peer1.ID)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap1.Peers) != 1 {
|
|
|
|
t.Errorf(
|
|
|
|
"expecting Account NetworkMap to have 1 peers, got %v: %v",
|
|
|
|
len(networkMap1.Peers),
|
|
|
|
networkMap1.Peers,
|
|
|
|
)
|
2022-06-04 22:02:22 +02:00
|
|
|
return
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if networkMap1.Peers[0].Key != peerKey2.PublicKey().String() {
|
|
|
|
t.Errorf(
|
|
|
|
"expecting Account NetworkMap to have peer with a key %s, got %s",
|
|
|
|
peerKey2.PublicKey().String(),
|
|
|
|
networkMap1.Peers[0].Key,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
networkMap2, err := manager.GetNetworkMap(peer2.ID)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap2.Peers) != 1 {
|
|
|
|
t.Errorf("expecting Account NetworkMap to have 1 peers, got %v", len(networkMap2.Peers))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap2.Peers) > 0 && networkMap2.Peers[0].Key != peerKey1.PublicKey().String() {
|
|
|
|
t.Errorf(
|
|
|
|
"expecting Account NetworkMap to have peer with a key %s, got %s",
|
|
|
|
peerKey1.PublicKey().String(),
|
|
|
|
networkMap2.Peers[0].Key,
|
|
|
|
)
|
|
|
|
}
|
2022-06-14 10:32:54 +02:00
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
policy.Enabled = false
|
|
|
|
err = manager.SavePolicy(account.Id, userID, &policy)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting rule to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
networkMap1, err = manager.GetNetworkMap(peer1.ID)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap1.Peers) != 0 {
|
|
|
|
t.Errorf(
|
|
|
|
"expecting Account NetworkMap to have 0 peers, got %v: %v",
|
|
|
|
len(networkMap1.Peers),
|
|
|
|
networkMap1.Peers,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
networkMap2, err = manager.GetNetworkMap(peer2.ID)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkMap2.Peers) != 0 {
|
|
|
|
t.Errorf("expecting Account NetworkMap to have 0 peers, got %v", len(networkMap2.Peers))
|
|
|
|
}
|
2022-01-16 17:10:36 +01:00
|
|
|
}
|
2022-06-24 21:30:51 +02:00
|
|
|
|
|
|
|
func TestAccountManager_GetPeerNetwork(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedId := "test_account"
|
|
|
|
userId := "account_creator"
|
|
|
|
account, err := createAccount(manager, expectedId, userId, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-09-04 11:37:39 +02:00
|
|
|
setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userId, false)
|
2023-06-27 17:17:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating setup key")
|
|
|
|
return
|
|
|
|
}
|
2022-06-24 21:30:51 +02:00
|
|
|
|
|
|
|
peerKey1, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer1, _, err := manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-06-24 21:30:51 +02:00
|
|
|
Key: peerKey1.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-1"},
|
2022-06-24 21:30:51 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey2, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-28 13:45:26 +01:00
|
|
|
_, _, err = manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2022-06-24 21:30:51 +02:00
|
|
|
Key: peerKey2.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
2022-06-24 21:30:51 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:33:28 +01:00
|
|
|
network, err := manager.GetPeerNetwork(peer1.ID)
|
2022-06-24 21:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:42:36 +02:00
|
|
|
if account.Network.Identifier != network.Identifier {
|
|
|
|
t.Errorf("expecting Account Networks ID to be equal, got %s expected %s", network.Identifier, account.Network.Identifier)
|
2022-06-24 21:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 20:11:08 +01:00
|
|
|
|
|
|
|
func TestDefaultAccountManager_GetPeer(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// account with an admin and a regular user
|
|
|
|
accountID := "test_account"
|
|
|
|
adminUser := "account_creator"
|
|
|
|
someUser := "some_user"
|
|
|
|
account := newAccountWithId(accountID, adminUser, "")
|
|
|
|
account.Users[someUser] = &User{
|
|
|
|
Id: someUser,
|
|
|
|
Role: UserRoleUser,
|
|
|
|
}
|
2024-03-27 16:11:45 +01:00
|
|
|
account.Settings.RegularUsersViewBlocked = false
|
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
err = manager.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// two peers one added by a regular user and one with a setup key
|
2023-09-04 11:37:39 +02:00
|
|
|
setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, adminUser, false)
|
2023-06-27 17:17:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("error creating setup key")
|
|
|
|
return
|
|
|
|
}
|
2023-09-04 17:03:44 +02:00
|
|
|
|
2023-02-07 20:11:08 +01:00
|
|
|
peerKey1, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
peer1, _, err := manager.AddPeer("", someUser, &nbpeer.Peer{
|
2023-02-07 20:11:08 +01:00
|
|
|
Key: peerKey1.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
2023-02-07 20:11:08 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey2, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// the second peer added with a setup key
|
2023-11-28 13:45:26 +01:00
|
|
|
peer2, _, err := manager.AddPeer(setupKey.Key, "", &nbpeer.Peer{
|
2023-02-07 20:11:08 +01:00
|
|
|
Key: peerKey2.PublicKey().String(),
|
2023-11-28 13:45:26 +01:00
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
2023-02-07 20:11:08 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// the user can see its own peer
|
|
|
|
peer, err := manager.GetPeer(accountID, peer1.ID, someUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotNil(t, peer)
|
|
|
|
|
|
|
|
// the user can see peer2 because peer1 of the user has access to peer2 due to the All group and the default rule 0 all-to-all access
|
|
|
|
peer, err = manager.GetPeer(accountID, peer2.ID, someUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotNil(t, peer)
|
|
|
|
|
2023-03-13 15:14:18 +01:00
|
|
|
// delete the all-to-all policy so that user's peer1 has no access to peer2
|
|
|
|
for _, policy := range account.Policies {
|
|
|
|
err = manager.DeletePolicy(accountID, policy.ID, adminUser)
|
2023-02-07 20:11:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point the user can't see the details of peer2
|
|
|
|
peer, err = manager.GetPeer(accountID, peer2.ID, someUser) //nolint
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// admin users can always access all the peers
|
|
|
|
peer, err = manager.GetPeer(accountID, peer1.ID, adminUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotNil(t, peer)
|
|
|
|
|
|
|
|
peer, err = manager.GetPeer(accountID, peer2.ID, adminUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotNil(t, peer)
|
|
|
|
}
|
2024-03-27 16:11:45 +01:00
|
|
|
|
|
|
|
func TestDefaultAccountManager_GetPeers(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
role UserRole
|
|
|
|
limitedViewSettings bool
|
|
|
|
isServiceUser bool
|
|
|
|
expectedPeerCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Regular user, no limited view settings, not a service user",
|
|
|
|
role: UserRoleUser,
|
|
|
|
limitedViewSettings: false,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service user, no limited view settings",
|
|
|
|
role: UserRoleUser,
|
|
|
|
limitedViewSettings: false,
|
|
|
|
isServiceUser: true,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Regular user, limited view settings",
|
|
|
|
role: UserRoleUser,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service user, limited view settings",
|
|
|
|
role: UserRoleUser,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: true,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Admin, no limited view settings, not a service user",
|
|
|
|
role: UserRoleAdmin,
|
|
|
|
limitedViewSettings: false,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Admin service user, no limited view settings",
|
|
|
|
role: UserRoleAdmin,
|
|
|
|
limitedViewSettings: false,
|
|
|
|
isServiceUser: true,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Admin, limited view settings",
|
|
|
|
role: UserRoleAdmin,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Admin Service user, limited view settings",
|
|
|
|
role: UserRoleAdmin,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: true,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Owner, no limited view settings",
|
|
|
|
role: UserRoleOwner,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Owner, limited view settings",
|
|
|
|
role: UserRoleOwner,
|
|
|
|
limitedViewSettings: true,
|
|
|
|
isServiceUser: false,
|
|
|
|
expectedPeerCount: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// account with an admin and a regular user
|
|
|
|
accountID := "test_account"
|
|
|
|
adminUser := "account_creator"
|
|
|
|
someUser := "some_user"
|
|
|
|
account := newAccountWithId(accountID, adminUser, "")
|
|
|
|
account.Users[someUser] = &User{
|
|
|
|
Id: someUser,
|
|
|
|
Role: testCase.role,
|
|
|
|
IsServiceUser: testCase.isServiceUser,
|
|
|
|
}
|
|
|
|
account.Policies = []*Policy{}
|
|
|
|
account.Settings.RegularUsersViewBlocked = testCase.limitedViewSettings
|
|
|
|
|
|
|
|
err = manager.Store.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey1, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerKey2, err := wgtypes.GeneratePrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = manager.AddPeer("", someUser, &nbpeer.Peer{
|
|
|
|
Key: peerKey1.PublicKey().String(),
|
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-1"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = manager.AddPeer("", adminUser, &nbpeer.Peer{
|
|
|
|
Key: peerKey2.PublicKey().String(),
|
|
|
|
Meta: nbpeer.PeerSystemMeta{Hostname: "test-peer-2"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expecting peer to be added, got failure %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peers, err := manager.GetPeers(accountID, someUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NotNil(t, peers)
|
|
|
|
|
|
|
|
assert.Len(t, peers, testCase.expectedPeerCount)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|