mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-27 06:49:07 +01:00
[management] remove db calls in nested loops (#6470)
This commit is contained in:
+29
-44
@@ -1026,7 +1026,12 @@ func (am *DefaultAccountManager) SyncPeer(ctx context.Context, sync types.PeerSy
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
postureChecks, err = getPeerPostureChecks(ctx, transaction, accountID, peer.ID)
|
policies, err := transaction.GetAccountPolicies(ctx, store.LockingStrengthNone, accountID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
postureChecks, err = getPeerPostureChecks(ctx, transaction, accountID, peerGroupIDs, policies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1290,12 +1295,22 @@ func getPeerLoginInfo(ctx context.Context, transaction store.Store, accountID st
|
|||||||
return network, nil, false, nil
|
return network, nil, false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
postureChecks, err := getPeerPostureChecks(ctx, transaction, accountID, peer.ID)
|
policies, err := transaction.GetAccountPolicies(ctx, store.LockingStrengthNone, accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, false, err
|
return nil, nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
enableSSH, err := isPeerSSHEnabled(ctx, transaction, accountID, peer)
|
peerGroupIDs, err := transaction.GetPeerGroupIDs(ctx, store.LockingStrengthNone, accountID, peer.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
postureChecks, err := getPeerPostureChecks(ctx, transaction, accountID, peerGroupIDs, policies)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
enableSSH, err := isPeerSSHEnabled(ctx, peer, policies, peerGroupIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, false, err
|
return nil, nil, false, err
|
||||||
}
|
}
|
||||||
@@ -1303,32 +1318,16 @@ func getPeerLoginInfo(ctx context.Context, transaction store.Store, accountID st
|
|||||||
return network, postureChecks, enableSSH, nil
|
return network, postureChecks, enableSSH, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPeerSSHEnabled(ctx context.Context, transaction store.Store, accountID string, peer *nbpeer.Peer) (bool, error) {
|
func isPeerSSHEnabled(ctx context.Context, peer *nbpeer.Peer, policies []*types.Policy, peerGroupIDs []string) (bool, error) {
|
||||||
policies, err := transaction.GetAccountPolicies(ctx, store.LockingStrengthNone, accountID)
|
groupIDsMap := make(map[string]struct{}, len(peerGroupIDs))
|
||||||
if err != nil {
|
for _, peerID := range peerGroupIDs {
|
||||||
return false, err
|
groupIDsMap[peerID] = struct{}{}
|
||||||
}
|
}
|
||||||
|
return types.PeerSSHEnabledFromPolicies(policies, peer.ID, groupIDsMap, peer.SSHEnabled), nil
|
||||||
peerGroups, err := transaction.GetPeerGroups(ctx, store.LockingStrengthNone, accountID, peer.ID)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
peerGroupIDs := make(map[string]struct{}, len(peerGroups))
|
|
||||||
for _, g := range peerGroups {
|
|
||||||
peerGroupIDs[g.ID] = struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return types.PeerSSHEnabledFromPolicies(policies, peer.ID, peerGroupIDs, peer.SSHEnabled), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPeerPostureChecks returns the posture checks for the peer.
|
// getPeerPostureChecks returns the posture checks for the peer.
|
||||||
func getPeerPostureChecks(ctx context.Context, transaction store.Store, accountID, peerID string) ([]*posture.Checks, error) {
|
func getPeerPostureChecks(ctx context.Context, transaction store.Store, accountID string, peerGroupIDs []string, policies []*types.Policy) ([]*posture.Checks, error) {
|
||||||
policies, err := transaction.GetAccountPolicies(ctx, store.LockingStrengthNone, accountID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(policies) == 0 {
|
if len(policies) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -1340,11 +1339,7 @@ func getPeerPostureChecks(ctx context.Context, transaction store.Store, accountI
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
postureChecksIDs, err := processPeerPostureChecks(ctx, transaction, policy, accountID, peerID)
|
postureChecksIDs := processPeerPostureChecks(policy, peerGroupIDs)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
peerPostureChecksIDs = append(peerPostureChecksIDs, postureChecksIDs...)
|
peerPostureChecksIDs = append(peerPostureChecksIDs, postureChecksIDs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1357,29 +1352,19 @@ func getPeerPostureChecks(ctx context.Context, transaction store.Store, accountI
|
|||||||
}
|
}
|
||||||
|
|
||||||
// processPeerPostureChecks checks if the peer is in the source group of the policy and returns the posture checks.
|
// processPeerPostureChecks checks if the peer is in the source group of the policy and returns the posture checks.
|
||||||
func processPeerPostureChecks(ctx context.Context, transaction store.Store, policy *types.Policy, accountID, peerID string) ([]string, error) {
|
func processPeerPostureChecks(policy *types.Policy, peerGroupIDs []string) []string {
|
||||||
for _, rule := range policy.Rules {
|
for _, rule := range policy.Rules {
|
||||||
if !rule.Enabled {
|
if !rule.Enabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceGroups, err := transaction.GetGroupsByIDs(ctx, store.LockingStrengthNone, accountID, rule.Sources)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sourceGroup := range rule.Sources {
|
for _, sourceGroup := range rule.Sources {
|
||||||
group, ok := sourceGroups[sourceGroup]
|
if slices.Contains(peerGroupIDs, sourceGroup) {
|
||||||
if !ok {
|
return policy.SourcePostureChecks
|
||||||
return nil, fmt.Errorf("failed to check peer in policy source group")
|
|
||||||
}
|
|
||||||
|
|
||||||
if slices.Contains(group.Peers, peerID) {
|
|
||||||
return policy.SourcePostureChecks, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkIFPeerNeedsLoginWithoutLock checks if the peer needs login without acquiring the account lock. The check validate if the peer was not added via SSO
|
// checkIFPeerNeedsLoginWithoutLock checks if the peer needs login without acquiring the account lock. The check validate if the peer was not added via SSO
|
||||||
|
|||||||
Reference in New Issue
Block a user