grafana-iam: prevent duplicate permissions upon ResourcePermission create (#111361)

prevent duplicate permissions
This commit is contained in:
mohammad-hamid
2025-09-19 16:55:08 +02:00
committed by GitHub
parent 0db140e697
commit db61acf513
2 changed files with 189 additions and 0 deletions
@@ -516,6 +516,16 @@ func validateCreateAndUpdateInput(v0ResourcePerm *v0alpha1.ResourcePermission, g
return fmt.Errorf("resource permission name does not match spec: %w", errInvalidSpec)
}
// Check for duplicate entities (same kind and name should appear only once)
seen := make(map[string]bool)
for _, perm := range v0ResourcePerm.Spec.Permissions {
key := fmt.Sprintf("%s:%s", perm.Kind, perm.Name)
if seen[key] {
return fmt.Errorf("duplicate entity found: kind=%s, name=%s (each entity can only appear once per resource): %w", perm.Kind, perm.Name, errInvalidSpec)
}
seen[key] = true
}
return nil
}