Update account peer if posture check is linked to policy

This commit is contained in:
bcmmbaga 2024-07-16 18:19:05 +03:00
parent d676c41c74
commit dedf13d8f1
No known key found for this signature in database
GPG Key ID: 7249A19D20613553

View File

@ -70,10 +70,15 @@ func (am *DefaultAccountManager) SavePostureChecks(ctx context.Context, accountI
return status.Errorf(status.PreconditionFailed, "Posture check name should be unique") return status.Errorf(status.PreconditionFailed, "Posture check name should be unique")
} }
updateAccountPeers := false
action := activity.PostureCheckCreated action := activity.PostureCheckCreated
if exists { if exists {
action = activity.PostureCheckUpdated action = activity.PostureCheckUpdated
account.Network.IncSerial()
updateAccountPeers, _ = isPostureCheckLinkedToPolicy(account, postureChecks.ID)
if updateAccountPeers {
account.Network.IncSerial()
}
} }
if err = am.Store.SaveAccount(ctx, account); err != nil { if err = am.Store.SaveAccount(ctx, account); err != nil {
@ -81,7 +86,7 @@ func (am *DefaultAccountManager) SavePostureChecks(ctx context.Context, accountI
} }
am.StoreEvent(ctx, userID, postureChecks.ID, accountID, action, postureChecks.EventMeta()) am.StoreEvent(ctx, userID, postureChecks.ID, accountID, action, postureChecks.EventMeta())
if exists { if updateAccountPeers {
am.updateAccountPeers(ctx, account) am.updateAccountPeers(ctx, account)
} }
@ -170,13 +175,9 @@ func (am *DefaultAccountManager) deletePostureChecks(account *Account, postureCh
return nil, status.Errorf(status.NotFound, "posture checks with ID %s doesn't exist", postureChecksID) return nil, status.Errorf(status.NotFound, "posture checks with ID %s doesn't exist", postureChecksID)
} }
// check policy links // Check if posture check is linked to any policy
for _, policy := range account.Policies { if isLinked, linkedPolicy := isPostureCheckLinkedToPolicy(account, postureChecksID); isLinked {
for _, id := range policy.SourcePostureChecks { return nil, status.Errorf(status.PreconditionFailed, "posture checks have been linked to policy: %s", linkedPolicy.Name)
if id == postureChecksID {
return nil, status.Errorf(status.PreconditionFailed, "posture checks have been linked to policy: %s", policy.Name)
}
}
} }
postureChecks := account.PostureChecks[postureChecksIdx] postureChecks := account.PostureChecks[postureChecksIdx]
@ -239,3 +240,12 @@ func addPolicyPostureChecks(account *Account, policy *Policy, peerPostureChecks
} }
} }
} }
func isPostureCheckLinkedToPolicy(account *Account, postureChecksID string) (bool, *Policy) {
for _, policy := range account.Policies {
if slices.Contains(policy.SourcePostureChecks, postureChecksID) {
return true, policy
}
}
return false, nil
}