Don't create setup keys on new account (#972)

This commit is contained in:
Misha Bragin 2023-06-27 18:17:24 +03:00 committed by GitHub
parent 8b619a8224
commit d409219b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 46 deletions

View File

@ -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 // 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") 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() network := NewNetwork()
peers := make(map[string]*Peer) peers := make(map[string]*Peer)
users := make(map[string]*User) users := make(map[string]*User)
routes := make(map[string]*route.Route) routes := make(map[string]*route.Route)
setupKeys := map[string]*SetupKey{}
nameServersGroups := make(map[string]*nbdns.NameServerGroup) nameServersGroups := make(map[string]*nbdns.NameServerGroup)
users[userId] = NewAdminUser(userId) users[userID] = NewAdminUser(userID)
dnsSettings := &DNSSettings{ dnsSettings := &DNSSettings{
DisabledManagementGroups: make([]string, 0), 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{ acc := &Account{
Id: accountId, Id: accountID,
SetupKeys: setupKeys, SetupKeys: setupKeys,
Network: network, Network: network,
Peers: peers, Peers: peers,
Users: users, Users: users,
CreatedBy: userId, CreatedBy: userID,
Domain: domain, Domain: domain,
Routes: routes, Routes: routes,
NameServerGroups: nameServersGroups, NameServerGroups: nameServersGroups,

View File

@ -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)) 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)) 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 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
serial := account.Network.CurrentSerial() // should be 0 serial := account.Network.CurrentSerial() // should be 0
var setupKey *SetupKey setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID)
for _, key := range account.SetupKeys { if err != nil {
setupKey = key return
} }
if setupKey == nil { if err != nil {
t.Errorf("expecting account to have a default setup key") t.Fatal("error creating setup key")
return return
} }
@ -922,16 +923,13 @@ func TestAccountManager_NetworkUpdates(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var setupKey *SetupKey setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID)
for _, key := range account.SetupKeys { if err != nil {
setupKey = key return
if setupKey.Type == SetupKeyReusable {
break
}
} }
if setupKey == nil { if err != nil {
t.Errorf("expecting account to have a default setup key") t.Fatal("error creating setup key")
return return
} }
@ -1106,9 +1104,14 @@ func TestAccountManager_DeletePeer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var setupKey *SetupKey setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userID)
for _, key := range account.SetupKeys { if err != nil {
setupKey = key return
}
if err != nil {
t.Fatal("error creating setup key")
return
} }
key, err := wgtypes.GenerateKey() key, err := wgtypes.GenerateKey()

View File

@ -78,11 +78,14 @@ func TestAccountManager_GetNetworkMap(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var setupKey *SetupKey setupKey, err := manager.CreateSetupKey(account.Id, "test-key", SetupKeyReusable, time.Hour, nil, 999, userId)
for _, key := range account.SetupKeys { if err != nil {
if key.Type == SetupKeyReusable { return
setupKey = key }
}
if err != nil {
t.Fatal("error creating setup key")
return
} }
peerKey1, err := wgtypes.GeneratePrivateKey() peerKey1, err := wgtypes.GeneratePrivateKey()
@ -328,7 +331,15 @@ func TestAccountManager_GetPeerNetwork(t *testing.T) {
t.Fatal(err) 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() peerKey1, err := wgtypes.GeneratePrivateKey()
if err != nil { 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 // 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() peerKey1, err := wgtypes.GeneratePrivateKey()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -470,13 +489,3 @@ func TestDefaultAccountManager_GetPeer(t *testing.T) {
} }
assert.NotNil(t, peer) 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
}