[management] policy delete cleans policy rules (#3788)

This commit is contained in:
Pascal Fischer 2025-05-07 07:25:25 +02:00 committed by GitHub
parent ebda0fc538
commit fcd2c15a37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1683,18 +1683,26 @@ func (s *SqlStore) SavePolicy(ctx context.Context, lockStrength LockingStrength,
} }
func (s *SqlStore) DeletePolicy(ctx context.Context, lockStrength LockingStrength, accountID, policyID string) error { func (s *SqlStore) DeletePolicy(ctx context.Context, lockStrength LockingStrength, accountID, policyID string) error {
result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
Delete(&types.Policy{}, accountAndIDQueryCondition, accountID, policyID) if err := tx.Where("policy_id = ?", policyID).Delete(&types.PolicyRule{}).Error; err != nil {
if err := result.Error; err != nil { return fmt.Errorf("delete policy rules: %w", err)
log.WithContext(ctx).Errorf("failed to delete policy from store: %s", err) }
return status.Errorf(status.Internal, "failed to delete policy from store")
}
if result.RowsAffected == 0 { result := tx.Clauses(clause.Locking{Strength: string(lockStrength)}).
return status.NewPolicyNotFoundError(policyID) Where(accountAndIDQueryCondition, accountID, policyID).
} Delete(&types.Policy{})
return nil if err := result.Error; err != nil {
log.WithContext(ctx).Errorf("failed to delete policy from store: %s", err)
return status.Errorf(status.Internal, "failed to delete policy from store")
}
if result.RowsAffected == 0 {
return status.NewPolicyNotFoundError(policyID)
}
return nil
})
} }
// GetAccountPostureChecks retrieves posture checks for an account. // GetAccountPostureChecks retrieves posture checks for an account.