diff --git a/management/server/account.go b/management/server/account.go index ecc7f2260..b4e0854d8 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -1439,33 +1439,28 @@ func addAllGroup(account *Account) error { } // newAccountWithId creates a new Account with a default SetupKey (doesn't store in a Store) and provided id -func newAccountWithId(accountId, userId, domain string) *Account { +func newAccountWithId(accountID, userID, domain string) *Account { log.Debugf("creating new account") - setupKeys := make(map[string]*SetupKey) - defaultKey := GenerateDefaultSetupKey() - oneOffKey := GenerateSetupKey("One-off key", SetupKeyOneOff, DefaultSetupKeyDuration, []string{}, - SetupKeyUnlimitedUsage) - setupKeys[defaultKey.Key] = defaultKey - setupKeys[oneOffKey.Key] = oneOffKey network := NewNetwork() peers := make(map[string]*Peer) users := make(map[string]*User) routes := make(map[string]*route.Route) + setupKeys := map[string]*SetupKey{} nameServersGroups := make(map[string]*nbdns.NameServerGroup) - users[userId] = NewAdminUser(userId) + users[userID] = NewAdminUser(userID) dnsSettings := &DNSSettings{ DisabledManagementGroups: make([]string, 0), } - log.Debugf("created new account %s with setup key %s", accountId, defaultKey.Key) + log.Debugf("created new account %s", accountID) acc := &Account{ - Id: accountId, + Id: accountID, SetupKeys: setupKeys, Network: network, Peers: peers, Users: users, - CreatedBy: userId, + CreatedBy: userID, Domain: domain, Routes: routes, NameServerGroups: nameServersGroups, diff --git a/management/server/account_test.go b/management/server/account_test.go index 55aa0d1d0..f90abc41b 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -54,7 +54,7 @@ func verifyNewAccountHasDefaultFields(t *testing.T, account *Account, createdBy t.Errorf("expected account to have len(Peers) = %v, got %v", 0, len(account.Peers)) } - if len(account.SetupKeys) != 2 { + if len(account.SetupKeys) != 0 { t.Errorf("expected account to have len(SetupKeys) = %v, got %v", 2, len(account.SetupKeys)) } @@ -768,20 +768,21 @@ func TestAccountManager_AddPeer(t *testing.T) { return } - account, err := createAccount(manager, "test_account", "account_creator", "netbird.cloud") + userID := "account_creator" + account, err := createAccount(manager, "test_account", userID, "netbird.cloud") if err != nil { t.Fatal(err) } serial := account.Network.CurrentSerial() // should be 0 - var setupKey *SetupKey - for _, key := range account.SetupKeys { - setupKey = key + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID) + if err != nil { + return } - if setupKey == nil { - t.Errorf("expecting account to have a default setup key") + if err != nil { + t.Fatal("error creating setup key") return } @@ -922,16 +923,13 @@ func TestAccountManager_NetworkUpdates(t *testing.T) { t.Fatal(err) } - var setupKey *SetupKey - for _, key := range account.SetupKeys { - setupKey = key - if setupKey.Type == SetupKeyReusable { - break - } + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID) + if err != nil { + return } - if setupKey == nil { - t.Errorf("expecting account to have a default setup key") + if err != nil { + t.Fatal("error creating setup key") return } @@ -1106,9 +1104,14 @@ func TestAccountManager_DeletePeer(t *testing.T) { t.Fatal(err) } - var setupKey *SetupKey - for _, key := range account.SetupKeys { - setupKey = key + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID) + if err != nil { + return + } + + if err != nil { + t.Fatal("error creating setup key") + return } key, err := wgtypes.GenerateKey() diff --git a/management/server/peer_test.go b/management/server/peer_test.go index 43629224d..875aaeaba 100644 --- a/management/server/peer_test.go +++ b/management/server/peer_test.go @@ -78,11 +78,14 @@ func TestAccountManager_GetNetworkMap(t *testing.T) { t.Fatal(err) } - var setupKey *SetupKey - for _, key := range account.SetupKeys { - if key.Type == SetupKeyReusable { - setupKey = key - } + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userId) + if err != nil { + return + } + + if err != nil { + t.Fatal("error creating setup key") + return } peerKey1, err := wgtypes.GeneratePrivateKey() @@ -328,7 +331,15 @@ func TestAccountManager_GetPeerNetwork(t *testing.T) { t.Fatal(err) } - setupKey := getSetupKey(account, SetupKeyReusable) + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userId) + if err != nil { + return + } + + if err != nil { + t.Fatal("error creating setup key") + return + } peerKey1, err := wgtypes.GeneratePrivateKey() if err != nil { @@ -394,7 +405,15 @@ func TestDefaultAccountManager_GetPeer(t *testing.T) { } // two peers one added by a regular user and one with a setup key - setupKey := getSetupKey(account, SetupKeyReusable) + setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, adminUser) + if err != nil { + return + } + + if err != nil { + t.Fatal("error creating setup key") + return + } peerKey1, err := wgtypes.GeneratePrivateKey() if err != nil { t.Fatal(err) @@ -470,13 +489,3 @@ func TestDefaultAccountManager_GetPeer(t *testing.T) { } assert.NotNil(t, peer) } - -func getSetupKey(account *Account, keyType SetupKeyType) *SetupKey { - var setupKey *SetupKey - for _, key := range account.SetupKeys { - if key.Type == keyType { - setupKey = key - } - } - return setupKey -}