2021-08-20 15:01:57 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-10-28 17:52:23 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2023-01-02 15:11:32 +01:00
|
|
|
"fmt"
|
2021-08-20 22:33:43 +02:00
|
|
|
"strconv"
|
2024-10-28 17:52:23 +01:00
|
|
|
"strings"
|
2021-08-20 15:01:57 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2023-04-03 15:09:35 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-10-23 12:05:02 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-03 15:09:35 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
2024-03-27 18:48:48 +01:00
|
|
|
nbgroup "github.com/netbirdio/netbird/management/server/group"
|
2021-08-20 15:01:57 +02:00
|
|
|
)
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
func TestDefaultAccountManager_SaveSetupKey(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-01-25 09:50:27 +01:00
|
|
|
userID := "testingUser"
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := manager.GetOrCreateAccountByUser(context.Background(), userID, "")
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:48:05 +02:00
|
|
|
err = manager.SaveGroups(context.Background(), account.Id, userID, []*nbgroup.Group{
|
|
|
|
{
|
|
|
|
ID: "group_1",
|
|
|
|
Name: "group_name_1",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "group_2",
|
|
|
|
Name: "group_name_2",
|
|
|
|
Peers: []string{},
|
|
|
|
},
|
2022-09-11 23:16:40 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expiresIn := time.Hour
|
|
|
|
keyName := "my-test-key"
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
key, err := manager.CreateSetupKey(context.Background(), account.Id, keyName, SetupKeyReusable, expiresIn, []string{},
|
2023-09-04 11:37:39 +02:00
|
|
|
SetupKeyUnlimitedUsage, userID, false)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
autoGroups := []string{"group_1", "group_2"}
|
|
|
|
newKeyName := "my-new-test-key"
|
|
|
|
revoked := true
|
2024-07-03 11:33:02 +02:00
|
|
|
newKey, err := manager.SaveSetupKey(context.Background(), account.Id, &SetupKey{
|
2022-09-11 23:16:40 +02:00
|
|
|
Id: key.Id,
|
|
|
|
Name: newKeyName,
|
|
|
|
Revoked: revoked,
|
|
|
|
AutoGroups: autoGroups,
|
2023-01-02 15:11:32 +01:00
|
|
|
}, userID)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertKey(t, newKey, newKeyName, revoked, "reusable", 0, key.CreatedAt, key.ExpiresAt,
|
2024-10-28 17:52:23 +01:00
|
|
|
key.Id, time.Now().UTC(), autoGroups, true)
|
2023-01-02 15:11:32 +01:00
|
|
|
|
2023-01-24 10:17:24 +01:00
|
|
|
// check the corresponding events that should have been generated
|
|
|
|
ev := getEvent(t, account.Id, manager, activity.SetupKeyRevoked)
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
assert.NotNil(t, ev)
|
|
|
|
assert.Equal(t, account.Id, ev.AccountID)
|
|
|
|
assert.Equal(t, newKeyName, ev.Meta["name"])
|
|
|
|
assert.Equal(t, fmt.Sprint(key.Type), fmt.Sprint(ev.Meta["type"]))
|
|
|
|
assert.NotEmpty(t, ev.Meta["key"])
|
|
|
|
assert.Equal(t, userID, ev.InitiatorID)
|
|
|
|
assert.Equal(t, key.Id, ev.TargetID)
|
2024-08-12 12:48:05 +02:00
|
|
|
|
|
|
|
groupAll, err := account.GetGroupAll()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// saving setup key with All group assigned to auto groups should return error
|
|
|
|
autoGroups = append(autoGroups, groupAll.ID)
|
|
|
|
_, err = manager.SaveSetupKey(context.Background(), account.Id, &SetupKey{
|
|
|
|
Id: key.Id,
|
|
|
|
Name: newKeyName,
|
|
|
|
Revoked: revoked,
|
|
|
|
AutoGroups: autoGroups,
|
|
|
|
}, userID)
|
|
|
|
assert.Error(t, err, "should not save setup key with All group assigned in auto groups")
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultAccountManager_CreateSetupKey(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-01-25 09:50:27 +01:00
|
|
|
userID := "testingUser"
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := manager.GetOrCreateAccountByUser(context.Background(), userID, "")
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
2022-09-11 23:16:40 +02:00
|
|
|
ID: "group_1",
|
|
|
|
Name: "group_name_1",
|
|
|
|
Peers: []string{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
2022-09-11 23:16:40 +02:00
|
|
|
ID: "group_2",
|
|
|
|
Name: "group_name_2",
|
|
|
|
Peers: []string{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:48:05 +02:00
|
|
|
groupAll, err := account.GetGroupAll()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
|
|
|
|
expectedKeyName string
|
|
|
|
expectedUsedTimes int
|
|
|
|
expectedType string
|
|
|
|
expectedGroups []string
|
|
|
|
expectedCreatedAt time.Time
|
|
|
|
expectedUpdatedAt time.Time
|
|
|
|
expectedExpiresAt time.Time
|
2023-04-03 15:09:35 +02:00
|
|
|
expectedFailure bool // indicates whether key creation should fail
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2023-04-03 15:09:35 +02:00
|
|
|
now := time.Now().UTC()
|
2022-09-11 23:16:40 +02:00
|
|
|
expiresIn := time.Hour
|
|
|
|
testCase1 := testCase{
|
|
|
|
name: "Should Create Setup Key successfully",
|
|
|
|
expectedKeyName: "my-test-key",
|
|
|
|
expectedUsedTimes: 0,
|
|
|
|
expectedType: "reusable",
|
|
|
|
expectedGroups: []string{"group_1", "group_2"},
|
|
|
|
expectedCreatedAt: now,
|
|
|
|
expectedUpdatedAt: now,
|
|
|
|
expectedExpiresAt: now.Add(expiresIn),
|
|
|
|
expectedFailure: false,
|
|
|
|
}
|
|
|
|
testCase2 := testCase{
|
|
|
|
name: "Create Setup Key should fail because of unexistent group",
|
|
|
|
expectedKeyName: "my-test-key",
|
|
|
|
expectedGroups: []string{"FAKE"},
|
|
|
|
expectedFailure: true,
|
|
|
|
}
|
2024-08-12 12:48:05 +02:00
|
|
|
testCase3 := testCase{
|
|
|
|
name: "Create Setup Key should fail because of All group",
|
|
|
|
expectedKeyName: "my-test-key",
|
|
|
|
expectedGroups: []string{groupAll.ID},
|
|
|
|
expectedFailure: true,
|
|
|
|
}
|
2022-09-11 23:16:40 +02:00
|
|
|
|
2024-08-12 12:48:05 +02:00
|
|
|
for _, tCase := range []testCase{testCase1, testCase2, testCase3} {
|
2022-09-11 23:16:40 +02:00
|
|
|
t.Run(tCase.name, func(t *testing.T) {
|
2024-07-03 11:33:02 +02:00
|
|
|
key, err := manager.CreateSetupKey(context.Background(), account.Id, tCase.expectedKeyName, SetupKeyReusable, expiresIn,
|
2023-09-04 11:37:39 +02:00
|
|
|
tCase.expectedGroups, SetupKeyUnlimitedUsage, userID, false)
|
2022-09-11 23:16:40 +02:00
|
|
|
|
|
|
|
if tCase.expectedFailure {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected to fail")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertKey(t, key, tCase.expectedKeyName, false, tCase.expectedType, tCase.expectedUsedTimes,
|
|
|
|
tCase.expectedCreatedAt, tCase.expectedExpiresAt, strconv.Itoa(int(Hash(key.Key))),
|
2024-10-28 17:52:23 +01:00
|
|
|
tCase.expectedUpdatedAt, tCase.expectedGroups, false)
|
2023-01-02 15:11:32 +01:00
|
|
|
|
2023-01-24 10:17:24 +01:00
|
|
|
// check the corresponding events that should have been generated
|
|
|
|
ev := getEvent(t, account.Id, manager, activity.SetupKeyCreated)
|
2023-01-02 15:11:32 +01:00
|
|
|
|
|
|
|
assert.NotNil(t, ev)
|
|
|
|
assert.Equal(t, account.Id, ev.AccountID)
|
|
|
|
assert.Equal(t, tCase.expectedKeyName, ev.Meta["name"])
|
|
|
|
assert.Equal(t, tCase.expectedType, fmt.Sprint(ev.Meta["type"]))
|
|
|
|
assert.NotEmpty(t, ev.Meta["key"])
|
2022-09-11 23:16:40 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-27 16:11:45 +01:00
|
|
|
func TestGetSetupKeys(t *testing.T) {
|
|
|
|
manager, err := createManager(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userID := "testingUser"
|
2024-07-03 11:33:02 +02:00
|
|
|
account, err := manager.GetOrCreateAccountByUser(context.Background(), userID, "")
|
2024-03-27 16:11:45 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
2024-03-27 16:11:45 +01:00
|
|
|
ID: "group_1",
|
|
|
|
Name: "group_name_1",
|
|
|
|
Peers: []string{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
err = manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
2024-03-27 16:11:45 +01:00
|
|
|
ID: "group_2",
|
|
|
|
Name: "group_name_2",
|
|
|
|
Peers: []string{},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:01:57 +02:00
|
|
|
func TestGenerateDefaultSetupKey(t *testing.T) {
|
|
|
|
expectedName := "Default key"
|
|
|
|
expectedRevoke := false
|
|
|
|
expectedType := "reusable"
|
|
|
|
expectedUsedTimes := 0
|
2023-04-03 15:09:35 +02:00
|
|
|
expectedCreatedAt := time.Now().UTC()
|
|
|
|
expectedUpdatedAt := time.Now().UTC()
|
|
|
|
expectedExpiresAt := time.Now().UTC().Add(24 * 30 * time.Hour)
|
2022-09-11 23:16:40 +02:00
|
|
|
var expectedAutoGroups []string
|
2021-08-20 15:01:57 +02:00
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
key, plainKey := GenerateDefaultSetupKey()
|
2021-08-20 15:01:57 +02:00
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
assertKey(t, key, expectedName, expectedRevoke, expectedType, expectedUsedTimes, expectedCreatedAt,
|
2024-10-28 17:52:23 +01:00
|
|
|
expectedExpiresAt, strconv.Itoa(int(Hash(plainKey))), expectedUpdatedAt, expectedAutoGroups, true)
|
2021-08-20 15:01:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateSetupKey(t *testing.T) {
|
|
|
|
expectedName := "key"
|
|
|
|
expectedRevoke := false
|
|
|
|
expectedType := "one-off"
|
|
|
|
expectedUsedTimes := 0
|
2023-04-03 15:09:35 +02:00
|
|
|
expectedCreatedAt := time.Now().UTC()
|
|
|
|
expectedExpiresAt := time.Now().UTC().Add(time.Hour)
|
|
|
|
expectedUpdatedAt := time.Now().UTC()
|
2022-09-11 23:16:40 +02:00
|
|
|
var expectedAutoGroups []string
|
2021-08-20 15:01:57 +02:00
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
key, plain := GenerateSetupKey(expectedName, SetupKeyOneOff, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
assertKey(t, key, expectedName, expectedRevoke, expectedType, expectedUsedTimes, expectedCreatedAt,
|
2024-10-28 17:52:23 +01:00
|
|
|
expectedExpiresAt, strconv.Itoa(int(Hash(plain))), expectedUpdatedAt, expectedAutoGroups, true)
|
2021-08-20 15:01:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetupKey_IsValid(t *testing.T) {
|
2024-10-28 17:52:23 +01:00
|
|
|
validKey, _ := GenerateSetupKey("valid key", SetupKeyOneOff, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
if !validKey.IsValid() {
|
|
|
|
t.Errorf("expected key to be valid, got invalid %v", validKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expired
|
2024-10-28 17:52:23 +01:00
|
|
|
expiredKey, _ := GenerateSetupKey("invalid key", SetupKeyOneOff, -time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
if expiredKey.IsValid() {
|
|
|
|
t.Errorf("expected key to be invalid due to expiration, got valid %v", expiredKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// revoked
|
2024-10-28 17:52:23 +01:00
|
|
|
revokedKey, _ := GenerateSetupKey("invalid key", SetupKeyOneOff, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
revokedKey.Revoked = true
|
|
|
|
if revokedKey.IsValid() {
|
|
|
|
t.Errorf("expected revoked key to be invalid, got valid %v", revokedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overused
|
2024-10-28 17:52:23 +01:00
|
|
|
overUsedKey, _ := GenerateSetupKey("invalid key", SetupKeyOneOff, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
overUsedKey.UsedTimes = 1
|
|
|
|
if overUsedKey.IsValid() {
|
|
|
|
t.Errorf("expected overused key to be invalid, got valid %v", overUsedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overused
|
2024-10-28 17:52:23 +01:00
|
|
|
reusableKey, _ := GenerateSetupKey("valid key", SetupKeyReusable, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
reusableKey.UsedTimes = 99
|
|
|
|
if !reusableKey.IsValid() {
|
|
|
|
t.Errorf("expected reusable key to be valid when used many times, got valid %v", reusableKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
func assertKey(t *testing.T, key *SetupKey, expectedName string, expectedRevoke bool, expectedType string,
|
2022-09-11 23:16:40 +02:00
|
|
|
expectedUsedTimes int, expectedCreatedAt time.Time, expectedExpiresAt time.Time, expectedID string,
|
2024-10-28 17:52:23 +01:00
|
|
|
expectedUpdatedAt time.Time, expectedAutoGroups []string, expectHashedKey bool) {
|
2023-11-10 16:33:13 +01:00
|
|
|
t.Helper()
|
2021-08-20 15:01:57 +02:00
|
|
|
if key.Name != expectedName {
|
|
|
|
t.Errorf("expected setup key to have Name %v, got %v", expectedName, key.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.Revoked != expectedRevoke {
|
|
|
|
t.Errorf("expected setup key to have Revoke %v, got %v", expectedRevoke, key.Revoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(key.Type) != expectedType {
|
|
|
|
t.Errorf("expected setup key to have Type %v, got %v", expectedType, key.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.UsedTimes != expectedUsedTimes {
|
|
|
|
t.Errorf("expected setup key to have UsedTimes = %v, got %v", expectedUsedTimes, key.UsedTimes)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.ExpiresAt.Sub(expectedExpiresAt).Round(time.Hour) != 0 {
|
|
|
|
t.Errorf("expected setup key to have ExpiresAt ~ %v, got %v", expectedExpiresAt, key.ExpiresAt)
|
|
|
|
}
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
if key.UpdatedAt.Sub(expectedUpdatedAt).Round(time.Hour) != 0 {
|
|
|
|
t.Errorf("expected setup key to have UpdatedAt ~ %v, got %v", expectedUpdatedAt, key.UpdatedAt)
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:01:57 +02:00
|
|
|
if key.CreatedAt.Sub(expectedCreatedAt).Round(time.Hour) != 0 {
|
|
|
|
t.Errorf("expected setup key to have CreatedAt ~ %v, got %v", expectedCreatedAt, key.CreatedAt)
|
|
|
|
}
|
2021-08-20 15:08:07 +02:00
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
if expectHashedKey {
|
|
|
|
if !isValidBase64SHA256(key.Key) {
|
|
|
|
t.Errorf("expected key to be hashed, got %v", key.Key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := uuid.Parse(key.Key)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected key to be a valid UUID, got %v, %v", key.Key, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(key.KeySecret, "****") {
|
|
|
|
t.Errorf("expected key secret to be secure, got %v", key.Key)
|
2021-08-20 15:08:07 +02:00
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
if key.Id != expectedID {
|
|
|
|
t.Errorf("expected key Id %v, got %v", expectedID, key.Id)
|
2021-08-20 22:33:43 +02:00
|
|
|
}
|
2022-09-11 23:16:40 +02:00
|
|
|
|
|
|
|
if len(key.AutoGroups) != len(expectedAutoGroups) {
|
|
|
|
t.Errorf("expected key AutoGroups size=%d, got %d", len(expectedAutoGroups), len(key.AutoGroups))
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, key.AutoGroups, expectedAutoGroups, "expected key AutoGroups to be equal")
|
2021-08-20 22:33:43 +02:00
|
|
|
}
|
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
func isValidBase64SHA256(encodedKey string) bool {
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(encodedKey)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(decoded) != sha256.Size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
func TestSetupKey_Copy(t *testing.T) {
|
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
key, _ := GenerateSetupKey("key name", SetupKeyOneOff, time.Hour, []string{}, SetupKeyUnlimitedUsage, false)
|
2021-08-20 22:33:43 +02:00
|
|
|
keyCopy := key.Copy()
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
assertKey(t, keyCopy, key.Name, key.Revoked, string(key.Type), key.UsedTimes, key.CreatedAt, key.ExpiresAt, key.Id,
|
2024-10-28 17:52:23 +01:00
|
|
|
key.UpdatedAt, key.AutoGroups, true)
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-08-20 15:01:57 +02:00
|
|
|
}
|
2024-10-23 12:05:02 +02:00
|
|
|
|
|
|
|
func TestSetupKeyAccountPeersUpdate(t *testing.T) {
|
|
|
|
manager, account, peer1, peer2, peer3 := setupNetworkMapTest(t)
|
|
|
|
|
|
|
|
err := manager.SaveGroup(context.Background(), account.Id, userID, &nbgroup.Group{
|
|
|
|
ID: "groupA",
|
|
|
|
Name: "GroupA",
|
|
|
|
Peers: []string{peer1.ID, peer2.ID, peer3.ID},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
policy := Policy{
|
|
|
|
ID: "policy",
|
|
|
|
Enabled: true,
|
|
|
|
Rules: []*PolicyRule{
|
|
|
|
{
|
|
|
|
Enabled: true,
|
|
|
|
Sources: []string{"groupA"},
|
|
|
|
Destinations: []string{"group"},
|
|
|
|
Bidirectional: true,
|
|
|
|
Action: PolicyTrafficActionAccept,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = manager.SavePolicy(context.Background(), account.Id, userID, &policy, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
updMsg := manager.peersUpdateManager.CreateChannel(context.Background(), peer1.ID)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
manager.peersUpdateManager.CloseChannel(context.Background(), peer1.ID)
|
|
|
|
})
|
|
|
|
|
|
|
|
var setupKey *SetupKey
|
|
|
|
|
|
|
|
// Creating setup key should not update account peers and not send peer update
|
|
|
|
t.Run("creating setup key", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
setupKey, err = manager.CreateSetupKey(context.Background(), account.Id, "key1", SetupKeyReusable, time.Hour, nil, 999, userID, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Saving setup key should not update account peers and not send peer update
|
|
|
|
t.Run("saving setup key", func(t *testing.T) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
peerShouldNotReceiveUpdate(t, updMsg)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = manager.SaveSetupKey(context.Background(), account.Id, setupKey, userID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|