2023-07-14 20:45:40 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-11-07 15:02:51 +01:00
|
|
|
"errors"
|
2024-08-08 17:01:38 +02:00
|
|
|
"fmt"
|
2023-07-14 20:45:40 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2023-11-07 15:02:51 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2023-07-14 20:45:40 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2024-08-08 17:01:38 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-07-14 20:45:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
groupAdminUserID = "testingAdminUser"
|
|
|
|
)
|
|
|
|
|
2024-03-17 11:13:39 +01:00
|
|
|
func TestDefaultAccountManager_CreateGroup(t *testing.T) {
|
|
|
|
am, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
_, account, err := initTestGroupAccount(am)
|
2024-03-17 11:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
for _, group := range account.Groups {
|
2024-03-27 18:48:48 +01:00
|
|
|
group.Issued = nbgroup.GroupIssuedIntegration
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveGroup(context.Background(), account.Id, groupAdminUserID, group)
|
2024-03-17 11:13:39 +01:00
|
|
|
if err != nil {
|
2024-03-27 18:48:48 +01:00
|
|
|
t.Errorf("should allow to create %s groups", nbgroup.GroupIssuedIntegration)
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range account.Groups {
|
2024-03-27 18:48:48 +01:00
|
|
|
group.Issued = nbgroup.GroupIssuedJWT
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveGroup(context.Background(), account.Id, groupAdminUserID, group)
|
2024-03-17 11:13:39 +01:00
|
|
|
if err != nil {
|
2024-03-27 18:48:48 +01:00
|
|
|
t.Errorf("should allow to create %s groups", nbgroup.GroupIssuedJWT)
|
2024-03-17 11:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, group := range account.Groups {
|
2024-03-27 18:48:48 +01:00
|
|
|
group.Issued = nbgroup.GroupIssuedAPI
|
2024-03-17 11:13:39 +01:00
|
|
|
group.ID = ""
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.SaveGroup(context.Background(), account.Id, groupAdminUserID, group)
|
2024-03-17 11:13:39 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("should not create api group with the same name, %s", group.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:45:40 +02:00
|
|
|
func TestDefaultAccountManager_DeleteGroup(t *testing.T) {
|
|
|
|
am, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to create account manager")
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
_, account, err := initTestGroupAccount(am)
|
2023-07-14 20:45:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("failed to init testing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
groupID string
|
|
|
|
expectedReason string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"route",
|
|
|
|
"grp-for-route",
|
|
|
|
"route",
|
|
|
|
},
|
2024-05-27 11:06:43 +02:00
|
|
|
{
|
|
|
|
"route with peer groups",
|
|
|
|
"grp-for-route2",
|
|
|
|
"route",
|
|
|
|
},
|
2023-07-14 20:45:40 +02:00
|
|
|
{
|
|
|
|
"name server groups",
|
|
|
|
"grp-for-name-server-grp",
|
|
|
|
"name server groups",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"policy",
|
|
|
|
"grp-for-policies",
|
|
|
|
"policy",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"setup keys",
|
|
|
|
"grp-for-keys",
|
|
|
|
"setup key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"users",
|
|
|
|
"grp-for-users",
|
|
|
|
"user",
|
|
|
|
},
|
2023-11-01 11:04:17 +01:00
|
|
|
{
|
|
|
|
"integration",
|
|
|
|
"grp-for-integration",
|
2023-12-01 17:24:57 +01:00
|
|
|
"only service users with admin power can delete integration group",
|
2023-11-01 11:04:17 +01:00
|
|
|
},
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
2024-07-03 11:33:02 +02:00
|
|
|
err = am.DeleteGroup(context.Background(), account.Id, groupAdminUserID, testCase.groupID)
|
2023-07-14 20:45:40 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("delete %s group successfully", testCase.groupID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:02:51 +01:00
|
|
|
var sErr *status.Error
|
|
|
|
if errors.As(err, &sErr) {
|
|
|
|
if sErr.Message != testCase.expectedReason {
|
|
|
|
t.Errorf("invalid error case: %s, expected: %s", sErr.Message, testCase.expectedReason)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var gErr *GroupLinkError
|
|
|
|
ok := errors.As(err, &gErr)
|
2023-07-14 20:45:40 +02:00
|
|
|
if !ok {
|
|
|
|
t.Error("invalid error type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if gErr.Resource != testCase.expectedReason {
|
|
|
|
t.Errorf("invalid error case: %s, expected: %s", gErr.Resource, testCase.expectedReason)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
func TestDefaultAccountManager_DeleteGroups(t *testing.T) {
|
|
|
|
am, err := createManager(t)
|
|
|
|
assert.NoError(t, err, "Failed to create account manager")
|
|
|
|
|
|
|
|
manager, account, err := initTestGroupAccount(am)
|
|
|
|
assert.NoError(t, err, "Failed to init testing account")
|
|
|
|
|
|
|
|
groups := make([]*nbgroup.Group, 10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
groups[i] = &nbgroup.Group{
|
|
|
|
ID: fmt.Sprintf("group-%d", i+1),
|
|
|
|
AccountID: account.Id,
|
|
|
|
Name: fmt.Sprintf("group-%d", i+1),
|
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = manager.SaveGroups(context.Background(), account.Id, groupAdminUserID, groups)
|
|
|
|
assert.NoError(t, err, "Failed to save test groups")
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
groupIDs []string
|
|
|
|
expectedReasons []string
|
|
|
|
expectedDeleted []string
|
|
|
|
expectedNotDeleted []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "route",
|
|
|
|
groupIDs: []string{"grp-for-route"},
|
|
|
|
expectedReasons: []string{"route"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "route with peer groups",
|
|
|
|
groupIDs: []string{"grp-for-route2"},
|
|
|
|
expectedReasons: []string{"route"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "name server groups",
|
|
|
|
groupIDs: []string{"grp-for-name-server-grp"},
|
|
|
|
expectedReasons: []string{"name server groups"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "policy",
|
|
|
|
groupIDs: []string{"grp-for-policies"},
|
|
|
|
expectedReasons: []string{"policy"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "setup keys",
|
|
|
|
groupIDs: []string{"grp-for-keys"},
|
|
|
|
expectedReasons: []string{"setup key"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "users",
|
|
|
|
groupIDs: []string{"grp-for-users"},
|
|
|
|
expectedReasons: []string{"user"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "integration",
|
|
|
|
groupIDs: []string{"grp-for-integration"},
|
|
|
|
expectedReasons: []string{"only service users with admin power can delete integration group"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successfully delete multiple groups",
|
|
|
|
groupIDs: []string{"group-1", "group-2"},
|
|
|
|
expectedDeleted: []string{"group-1", "group-2"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete non-existent group",
|
|
|
|
groupIDs: []string{"non-existent-group"},
|
|
|
|
expectedDeleted: []string{"non-existent-group"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete multiple groups with mixed results",
|
|
|
|
groupIDs: []string{"group-3", "grp-for-policies", "group-4", "grp-for-users"},
|
|
|
|
expectedReasons: []string{"policy", "user"},
|
|
|
|
expectedDeleted: []string{"group-3", "group-4"},
|
|
|
|
expectedNotDeleted: []string{"grp-for-policies", "grp-for-users"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "delete groups with multiple errors",
|
|
|
|
groupIDs: []string{"grp-for-policies", "grp-for-users"},
|
|
|
|
expectedReasons: []string{"policy", "user"},
|
|
|
|
expectedNotDeleted: []string{"grp-for-policies", "grp-for-users"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err = am.DeleteGroups(context.Background(), account.Id, groupAdminUserID, tc.groupIDs)
|
|
|
|
if len(tc.expectedReasons) > 0 {
|
|
|
|
assert.Error(t, err)
|
|
|
|
var foundExpectedErrors int
|
|
|
|
|
|
|
|
wrappedErr, ok := err.(interface{ Unwrap() []error })
|
|
|
|
assert.Equal(t, ok, true)
|
|
|
|
|
|
|
|
for _, e := range wrappedErr.Unwrap() {
|
|
|
|
var sErr *status.Error
|
|
|
|
if errors.As(e, &sErr) {
|
|
|
|
assert.Contains(t, tc.expectedReasons, sErr.Message, "unexpected error message")
|
|
|
|
foundExpectedErrors++
|
|
|
|
}
|
|
|
|
|
|
|
|
var gErr *GroupLinkError
|
|
|
|
if errors.As(e, &gErr) {
|
|
|
|
assert.Contains(t, tc.expectedReasons, gErr.Resource, "unexpected error resource")
|
|
|
|
foundExpectedErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, len(tc.expectedReasons), foundExpectedErrors, "not all expected errors were found")
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, groupID := range tc.expectedDeleted {
|
|
|
|
_, err := am.GetGroup(context.Background(), account.Id, groupID, groupAdminUserID)
|
|
|
|
assert.Error(t, err, "group should have been deleted: %s", groupID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, groupID := range tc.expectedNotDeleted {
|
|
|
|
group, err := am.GetGroup(context.Background(), account.Id, groupID, groupAdminUserID)
|
|
|
|
assert.NoError(t, err, "group should not have been deleted: %s", groupID)
|
|
|
|
assert.NotNil(t, group, "group should exist: %s", groupID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestGroupAccount(am *DefaultAccountManager) (*DefaultAccountManager, *Account, error) {
|
2023-07-14 20:45:40 +02:00
|
|
|
accountID := "testingAcc"
|
|
|
|
domain := "example.com"
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForRoute := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-route",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for route",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-05-27 11:06:43 +02:00
|
|
|
groupForRoute2 := &nbgroup.Group{
|
|
|
|
ID: "grp-for-route2",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for route",
|
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
|
|
|
Peers: make([]string, 0),
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForNameServerGroups := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-name-server-grp",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for name server groups",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForPolicies := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-policies",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for policies",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForSetupKeys := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-keys",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for setup keys",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForUsers := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-users",
|
|
|
|
AccountID: "account-id",
|
|
|
|
Name: "Group for users",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedAPI,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
|
|
|
}
|
|
|
|
|
2024-03-27 18:48:48 +01:00
|
|
|
groupForIntegration := &nbgroup.Group{
|
2023-11-01 11:04:17 +01:00
|
|
|
ID: "grp-for-integration",
|
|
|
|
AccountID: "account-id",
|
2024-03-17 11:13:39 +01:00
|
|
|
Name: "Group for users integration",
|
2024-03-27 18:48:48 +01:00
|
|
|
Issued: nbgroup.GroupIssuedIntegration,
|
2023-11-01 11:04:17 +01:00
|
|
|
Peers: make([]string, 0),
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
routeResource := &route.Route{
|
|
|
|
ID: "example route",
|
|
|
|
Groups: []string{groupForRoute.ID},
|
|
|
|
}
|
|
|
|
|
2024-05-27 11:06:43 +02:00
|
|
|
routePeerGroupResource := &route.Route{
|
|
|
|
ID: "example route with peer groups",
|
|
|
|
PeerGroups: []string{groupForRoute2.ID},
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:45:40 +02:00
|
|
|
nameServerGroup := &nbdns.NameServerGroup{
|
|
|
|
ID: "example name server group",
|
|
|
|
Groups: []string{groupForNameServerGroups.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := &Policy{
|
|
|
|
ID: "example policy",
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
ID: "example policy rule",
|
|
|
|
Destinations: []string{groupForPolicies.ID},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := &SetupKey{
|
|
|
|
Id: "example setup key",
|
|
|
|
AutoGroups: []string{groupForSetupKeys.ID},
|
|
|
|
}
|
|
|
|
|
|
|
|
user := &User{
|
|
|
|
Id: "example user",
|
|
|
|
AutoGroups: []string{groupForUsers.ID},
|
|
|
|
}
|
2024-07-03 11:33:02 +02:00
|
|
|
account := newAccountWithId(context.Background(), accountID, groupAdminUserID, domain)
|
2023-07-14 20:45:40 +02:00
|
|
|
account.Routes[routeResource.ID] = routeResource
|
2024-05-27 11:06:43 +02:00
|
|
|
account.Routes[routePeerGroupResource.ID] = routePeerGroupResource
|
2023-07-14 20:45:40 +02:00
|
|
|
account.NameServerGroups[nameServerGroup.ID] = nameServerGroup
|
|
|
|
account.Policies = append(account.Policies, policy)
|
|
|
|
account.SetupKeys[setupKey.Id] = setupKey
|
|
|
|
account.Users[user.Id] = user
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err := am.Store.SaveAccount(context.Background(), account)
|
2023-07-14 20:45:40 +02:00
|
|
|
if err != nil {
|
2024-08-08 17:01:38 +02:00
|
|
|
return nil, nil, err
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForRoute)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForRoute2)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForNameServerGroups)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForPolicies)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForSetupKeys)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForUsers)
|
|
|
|
_ = am.SaveGroup(context.Background(), accountID, groupAdminUserID, groupForIntegration)
|
2023-07-14 20:45:40 +02:00
|
|
|
|
2024-08-08 17:01:38 +02:00
|
|
|
acc, err := am.Store.GetAccount(context.Background(), account.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return am, acc, nil
|
2023-07-14 20:45:40 +02:00
|
|
|
}
|