From 7523a9e7bec56536939dde1654491987d3781c54 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Thu, 5 Sep 2024 19:36:20 +0300 Subject: [PATCH] Refactor posture check policy linking logic Signed-off-by: bcmmbaga --- management/server/policy.go | 13 ++++++------- management/server/posture_checks.go | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/management/server/policy.go b/management/server/policy.go index b64cbd543..e3a6df3f2 100644 --- a/management/server/policy.go +++ b/management/server/policy.go @@ -191,11 +191,10 @@ func (p *Policy) UpgradeAndFix() { } } -// appliedGroups returns a list of groups applied in the policy, including -// both Sources and Destinations from all rules. -func (p *Policy) appliedGroups() []string { +// ruleGroups returns a list of all groups referenced in the policy's rules, +// including sources and destinations. +func (p *Policy) ruleGroups() []string { groups := make([]string, 0) - for _, rule := range p.Rules { groups = append(groups, rule.Sources...) groups = append(groups, rule.Destinations...) @@ -405,7 +404,7 @@ func (am *DefaultAccountManager) DeletePolicy(ctx context.Context, accountID, po am.StoreEvent(ctx, userID, policy.ID, accountID, activity.PolicyRemoved, policy.EventMeta()) - if anyGroupHasPeers(account, policy.appliedGroups()) { + if anyGroupHasPeers(account, policy.ruleGroups()) { am.updateAccountPeers(ctx, account) } @@ -457,14 +456,14 @@ func (am *DefaultAccountManager) savePolicy(account *Account, policy *Policy) (e account.Policies[i] = policy exists = true - updateAccountPeers = anyGroupHasPeers(account, p.appliedGroups()) || anyGroupHasPeers(account, policy.appliedGroups()) + updateAccountPeers = anyGroupHasPeers(account, p.ruleGroups()) || anyGroupHasPeers(account, policy.ruleGroups()) break } } if !exists { account.Policies = append(account.Policies, policy) - updateAccountPeers = anyGroupHasPeers(account, policy.appliedGroups()) + updateAccountPeers = anyGroupHasPeers(account, policy.ruleGroups()) } return } diff --git a/management/server/posture_checks.go b/management/server/posture_checks.go index 5bad42f4e..c0a9c6a65 100644 --- a/management/server/posture_checks.go +++ b/management/server/posture_checks.go @@ -82,8 +82,8 @@ func (am *DefaultAccountManager) SavePostureChecks(ctx context.Context, accountI am.StoreEvent(ctx, userID, postureChecks.ID, accountID, action, postureChecks.EventMeta()) - updateAccountPeers, _ := isPostureCheckLinkedToPolicy(account, postureChecks.ID) - if exists && updateAccountPeers { + isLinked, linkedPolicy := isPostureCheckLinkedToPolicy(account, postureChecks.ID) + if exists && isLinked && anyGroupHasPeers(account, linkedPolicy.ruleGroups()) { am.updateAccountPeers(ctx, account) }