mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
Refactor GetValidatedPeers
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
@@ -100,18 +100,7 @@ func (am *DefaultAccountManager) GetValidatedPeers(ctx context.Context, accountI
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
groupsMap := make(map[string]*types.Group, len(groups))
|
return am.integratedPeerValidator.GetValidatedPeers(accountID, groups, peers, settings.Extra)
|
||||||
|
|
||||||
for _, group := range groups {
|
|
||||||
groupsMap[group.ID] = group
|
|
||||||
}
|
|
||||||
|
|
||||||
peersMap := make(map[string]*nbpeer.Peer, len(peers))
|
|
||||||
for _, peer := range peers {
|
|
||||||
peersMap[peer.ID] = peer
|
|
||||||
}
|
|
||||||
|
|
||||||
return am.integratedPeerValidator.GetValidatedPeers(accountID, groupsMap, peersMap, settings.Extra)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MocIntegratedValidator struct {
|
type MocIntegratedValidator struct {
|
||||||
@@ -129,7 +118,7 @@ func (a MocIntegratedValidator) ValidatePeer(_ context.Context, update *nbpeer.P
|
|||||||
return update, false, nil
|
return update, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a MocIntegratedValidator) GetValidatedPeers(accountID string, groups map[string]*types.Group, peers map[string]*nbpeer.Peer, extraSettings *account.ExtraSettings) (map[string]struct{}, error) {
|
func (a MocIntegratedValidator) GetValidatedPeers(accountID string, groups []*types.Group, peers []*nbpeer.Peer, extraSettings *account.ExtraSettings) (map[string]struct{}, error) {
|
||||||
validatedPeers := make(map[string]struct{})
|
validatedPeers := make(map[string]struct{})
|
||||||
for _, peer := range peers {
|
for _, peer := range peers {
|
||||||
validatedPeers[peer.ID] = struct{}{}
|
validatedPeers[peer.ID] = struct{}{}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type IntegratedValidator interface {
|
|||||||
ValidatePeer(ctx context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *account.ExtraSettings) (*nbpeer.Peer, bool, error)
|
ValidatePeer(ctx context.Context, update *nbpeer.Peer, peer *nbpeer.Peer, userID string, accountID string, dnsDomain string, peersGroup []string, extraSettings *account.ExtraSettings) (*nbpeer.Peer, bool, error)
|
||||||
PreparePeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) *nbpeer.Peer
|
PreparePeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) *nbpeer.Peer
|
||||||
IsNotValidPeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) (bool, bool, error)
|
IsNotValidPeer(ctx context.Context, accountID string, peer *nbpeer.Peer, peersGroup []string, extraSettings *account.ExtraSettings) (bool, bool, error)
|
||||||
GetValidatedPeers(accountID string, groups map[string]*types.Group, peers map[string]*nbpeer.Peer, extraSettings *account.ExtraSettings) (map[string]struct{}, error)
|
GetValidatedPeers(accountID string, groups []*types.Group, peers []*nbpeer.Peer, extraSettings *account.ExtraSettings) (map[string]struct{}, error)
|
||||||
PeerDeleted(ctx context.Context, accountID, peerID string) error
|
PeerDeleted(ctx context.Context, accountID, peerID string) error
|
||||||
SetPeerInvalidationListener(fn func(accountID string))
|
SetPeerInvalidationListener(fn func(accountID string))
|
||||||
Stop(ctx context.Context)
|
Stop(ctx context.Context)
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func (am *DefaultAccountManager) GetPeers(ctx context.Context, accountID, userID
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.Groups, account.Peers, account.Settings.Extra)
|
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -404,7 +404,7 @@ func (am *DefaultAccountManager) GetNetworkMap(ctx context.Context, peerID strin
|
|||||||
groups[groupID] = group.Peers
|
groups[groupID] = group.Peers
|
||||||
}
|
}
|
||||||
|
|
||||||
validatedPeers, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra)
|
validatedPeers, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -962,7 +962,7 @@ func (am *DefaultAccountManager) getValidatedPeerWithMap(ctx context.Context, is
|
|||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra)
|
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -1071,7 +1071,7 @@ func (am *DefaultAccountManager) GetPeer(ctx context.Context, accountID, peerID,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.Groups, account.Peers, account.Settings.Extra)
|
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(accountID, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1104,7 +1104,7 @@ func (am *DefaultAccountManager) UpdateAccountPeers(ctx context.Context, account
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra)
|
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithContext(ctx).Errorf("failed to send out updates to peers, failed to get validate peers: %v", err)
|
log.WithContext(ctx).Errorf("failed to send out updates to peers, failed to get validate peers: %v", err)
|
||||||
return
|
return
|
||||||
@@ -1165,7 +1165,7 @@ func (am *DefaultAccountManager) UpdateAccountPeer(ctx context.Context, accountI
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.Groups, account.Peers, account.Settings.Extra)
|
approvedPeersMap, err := am.integratedPeerValidator.GetValidatedPeers(account.Id, account.GroupsG, account.PeersG, account.Settings.Extra)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithContext(ctx).Errorf("failed to send update to peer %s, failed to validate peers: %v", peerId, err)
|
log.WithContext(ctx).Errorf("failed to send update to peer %s, failed to validate peers: %v", peerId, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ func generateAccountSQLTypes(account *types.Account) {
|
|||||||
|
|
||||||
for id, peer := range account.Peers {
|
for id, peer := range account.Peers {
|
||||||
peer.ID = id
|
peer.ID = id
|
||||||
account.PeersG = append(account.PeersG, *peer)
|
account.PeersG = append(account.PeersG, peer)
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, user := range account.Users {
|
for id, user := range account.Users {
|
||||||
@@ -235,7 +235,7 @@ func generateAccountSQLTypes(account *types.Account) {
|
|||||||
|
|
||||||
for id, group := range account.Groups {
|
for id, group := range account.Groups {
|
||||||
group.ID = id
|
group.ID = id
|
||||||
account.GroupsG = append(account.GroupsG, *group)
|
account.GroupsG = append(account.GroupsG, group)
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, route := range account.Routes {
|
for id, route := range account.Routes {
|
||||||
|
|||||||
@@ -54,11 +54,11 @@ type Account struct {
|
|||||||
SetupKeysG []SetupKey `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
SetupKeysG []SetupKey `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
||||||
Network *Network `gorm:"embedded;embeddedPrefix:network_"`
|
Network *Network `gorm:"embedded;embeddedPrefix:network_"`
|
||||||
Peers map[string]*nbpeer.Peer `gorm:"-"`
|
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:"-"`
|
Users map[string]*User `gorm:"-"`
|
||||||
UsersG []User `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
UsersG []User `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
||||||
Groups map[string]*Group `gorm:"-"`
|
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"`
|
Policies []*Policy `gorm:"foreignKey:AccountID;references:id"`
|
||||||
Routes map[route.ID]*route.Route `gorm:"-"`
|
Routes map[route.ID]*route.Route `gorm:"-"`
|
||||||
RoutesG []route.Route `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
RoutesG []route.Route `json:"-" gorm:"foreignKey:AccountID;references:id"`
|
||||||
|
|||||||
Reference in New Issue
Block a user