From 4afaabb33c60ce9c1442cb2aacaf3a955af9cf67 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Wed, 15 Jan 2025 12:50:25 +0300 Subject: [PATCH] Use peers and groups map for peers validation Signed-off-by: bcmmbaga --- management/server/migration/migration_test.go | 2 +- management/server/peer.go | 12 ++++++------ management/server/store/sql_store.go | 4 ++-- management/server/types/account.go | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/management/server/migration/migration_test.go b/management/server/migration/migration_test.go index 4b4aa1184..a645ae325 100644 --- a/management/server/migration/migration_test.go +++ b/management/server/migration/migration_test.go @@ -147,7 +147,7 @@ func TestMigrateNetIPFieldFromBlobToJSON_WithJSONData(t *testing.T) { err = db.Save(&types.Account{ Id: "1234", - PeersG: []*nbpeer.Peer{ + PeersG: []nbpeer.Peer{ {Location: nbpeer.Location{ConnectionIP: net.IP{10, 0, 0, 1}}}, }}, ).Error diff --git a/management/server/peer.go b/management/server/peer.go index c7f1830de..3ddb0a22d 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -101,7 +101,7 @@ func (am *DefaultAccountManager) GetPeers(ctx context.Context, accountID, userID return nil, err } - approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.GroupsG, account.PeersG, account.Settings.Extra) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { return nil, err } @@ -404,7 +404,7 @@ func (am *DefaultAccountManager) GetNetworkMap(ctx context.Context, peerID strin groups[groupID] = group.Peers } - validatedPeers, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra) + validatedPeers, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { return nil, err } @@ -962,7 +962,7 @@ func (am *DefaultAccountManager) getValidatedPeerWithMap(ctx context.Context, is return nil, nil, nil, err } - approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { return nil, nil, nil, err } @@ -1071,7 +1071,7 @@ func (am *DefaultAccountManager) GetPeer(ctx context.Context, accountID, peerID, return nil, err } - approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.GroupsG, account.PeersG, account.Settings.Extra) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { return nil, err } @@ -1104,7 +1104,7 @@ func (am *DefaultAccountManager) UpdateAccountPeers(ctx context.Context, account } }() - approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { log.WithContext(ctx).Errorf("failed to send out updates to peers, failed to get validate peers: %v", err) return @@ -1165,7 +1165,7 @@ func (am *DefaultAccountManager) UpdateAccountPeer(ctx context.Context, accountI return } - approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra) + approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, maps.Values(account.Groups), maps.Values(account.Peers), account.Settings.Extra) if err != nil { log.WithContext(ctx).Errorf("failed to send update to peer %s, failed to validate peers: %v", peerId, err) return diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index 29f1cde66..46e896b55 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -221,7 +221,7 @@ func generateAccountSQLTypes(account *types.Account) { for id, peer := range account.Peers { peer.ID = id - account.PeersG = append(account.PeersG, peer) + account.PeersG = append(account.PeersG, *peer) } for id, user := range account.Users { @@ -235,7 +235,7 @@ func generateAccountSQLTypes(account *types.Account) { for id, group := range account.Groups { group.ID = id - account.GroupsG = append(account.GroupsG, group) + account.GroupsG = append(account.GroupsG, *group) } for id, route := range account.Routes { diff --git a/management/server/types/account.go b/management/server/types/account.go index 8e5d303ce..f74d38cb6 100644 --- a/management/server/types/account.go +++ b/management/server/types/account.go @@ -54,11 +54,11 @@ type Account struct { SetupKeysG []SetupKey `json:"-" gorm:"foreignKey:AccountID;references:id"` Network *Network `gorm:"embedded;embeddedPrefix:network_"` Peers map[string]*nbpeer.Peer `gorm:"-"` - PeersG []*nbpeer.Peer `json:"-" gorm:"foreignKey:AccountID;references:id"` + PeersG []nbpeer.Peer `json:"-" gorm:"foreignKey:AccountID;references:id"` Users map[string]*User `gorm:"-"` UsersG []User `json:"-" gorm:"foreignKey:AccountID;references:id"` Groups map[string]*Group `gorm:"-"` - GroupsG []*Group `json:"-" gorm:"foreignKey:AccountID;references:id"` + GroupsG []Group `json:"-" gorm:"foreignKey:AccountID;references:id"` Policies []*Policy `gorm:"foreignKey:AccountID;references:id"` Routes map[route.ID]*route.Route `gorm:"-"` RoutesG []route.Route `json:"-" gorm:"foreignKey:AccountID;references:id"`