Skip to content

Commit 3130cce

Browse files
authored
[management] Add rule ID validation for policy updates (#4499)
1 parent bd23ab9 commit 3130cce

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

management/server/policy.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,22 @@ func arePolicyChangesAffectPeers(ctx context.Context, transaction store.Store, a
167167
// validatePolicy validates the policy and its rules.
168168
func validatePolicy(ctx context.Context, transaction store.Store, accountID string, policy *types.Policy) error {
169169
if policy.ID != "" {
170-
_, err := transaction.GetPolicyByID(ctx, store.LockingStrengthNone, accountID, policy.ID)
170+
existingPolicy, err := transaction.GetPolicyByID(ctx, store.LockingStrengthNone, accountID, policy.ID)
171171
if err != nil {
172172
return err
173173
}
174+
175+
// TODO: Refactor to support multiple rules per policy
176+
existingRuleIDs := make(map[string]bool)
177+
for _, rule := range existingPolicy.Rules {
178+
existingRuleIDs[rule.ID] = true
179+
}
180+
181+
for _, rule := range policy.Rules {
182+
if rule.ID != "" && !existingRuleIDs[rule.ID] {
183+
return status.Errorf(status.InvalidArgument, "invalid rule ID: %s", rule.ID)
184+
}
185+
}
174186
} else {
175187
policy.ID = xid.New().String()
176188
policy.AccountID = accountID

0 commit comments

Comments
 (0)