Skip to content

fix(azure_policy): follow-ups to sub-resource array rewrapping - #989

Open
snehabandla wants to merge 1 commit into
microsoft:mainfrom
snehabandla:split/azure-policy-sub-resource-review7
Open

snehabandla wants to merge 1 commit into
microsoft:mainfrom
snehabandla:split/azure-policy-sub-resource-review7

Conversation

@snehabandla

Copy link
Copy Markdown
Collaborator

Summary

Follow-ups to #958 (sub-resource array names include unwrapped outer arrays), which merged with five review comments open. Four of them describe regressions #958 introduced; the fifth is older. All five were reproduced, and two more bugs found while fixing them are fixed here too.

Changes

  • A refused write is not recorded. A plain alias whose destination runs through an array or scalar (for example b → properties.x.y while x is an array) cannot be written. It used to be recorded as the value written there, so the sub-resource arrays under x.y were not re-wrapped. set_nested_verbatim (and the setters it uses) now return whether the write succeeded, and the denormalizer records the alias only then, under properties and at the resource root.

  • Destinations that differ only in case stay separate. With a → properties.Items and b → properties.items, both arrays are written, but rewrap targets and the write log keyed them case-insensitively, so one array was left without its properties wrappers. Targets and writes now use the keys as written. Lookups try the exact key first and fall back to a case-insensitive match.

  • Wrapped ARM names stay inside properties. An unwrapped field's short name (for example rules[*].rawstatus) could add a name to the element envelope that a wrapped alias also uses (rules[*].other → …properties.rawstatus). {properties: {rawstatus: 1}} then came back as {rawstatus: 1}. Wrapped ARM names are now removed from the whole envelope set. The standard fields (name, type, id, etag) stay at the element root.

  • The plain-alias fallback reads the alias's own value. When the input key's casing differs from the plain alias's ARM path (Items for a → properties.items), the alias step misses it and the array rename supplies the value. That value had been flattened and remapped for the ARM name, not the alias, so:

    • wrappers that are sub-resource arrays under both names were flattened twice, moving a payload's own properties out;
    • arrays that are sub-resource arrays only under the ARM name lost their wrappers;
    • remaps rooted at the ARM name leaked into the alias, giving it the wrong value;
    • remaps rooted at the alias never ran (for example x[*].y[*].z stayed rawz; this one is older than fix(azure-policy): sub-resource array names include unwrapped outer arrays #958).

    The fallback now reads the alias's ARM path from the input (exact key first, then a case-insensitive match), normalizes it the same way the alias step does, and applies the remaps rooted at the alias. The output matches input whose key casing matches the ARM path.

Testing

New cases in normalize_sub_resources.yaml. Each fails before this change and passes after:

  • a refused write;
  • Items/items destinations, both top-level and nested, plus a non-array write to the other-cased key;
  • an Items/items round trip;
  • the rawstatus envelope (round trip and normalized output);
  • flattening once;
  • wrappers kept when an array is a sub-resource array only under the ARM name;
  • the ARM name's remaps not leaking into the alias.

New case in data_plane_sub_resources.yaml: a refused write at the resource root.

Unchanged-behaviour check: a wrapped name field stays at the element root.

The two existing mixed-case cases now also check expected_normalized.

Results:

  • cargo test --features azure_policy --test mod: 384 passed.
  • --lib azure_policy: 291 passed.
  • scripts/pre-commit: passes.

Not in this PR

Address review comments left open when microsoft#958 merged:

- Record a plain alias's write only when it succeeds, so a write refused by
  an array on the way does not hide the arrays there from rewrapping.
- Track rewrap targets and writes by the keys as written. Plain aliases whose
  ARM keys differ only in case write different arrays, and both are rewrapped.
  Lookups try the exact key first.
- Keep a wrapped field's ARM name inside `properties` even when an unwrapped
  field's short name is the same.
- When an array rename supplies a plain alias's value (its ARM key has
  different casing), read the alias's own ARM value instead of the renamed
  one, so each wrapper is flattened once, arrays that are sub-resource arrays
  only under the source name keep their wrappers, and the source name's
  remaps do not leak into the alias. Then apply the remaps rooted at the
  alias.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved alias-handling defects can drop ARM-named keys, select stale ancestor writes or case-distinct siblings, and incorrectly rename or wrap output fields.

4 open issues
What changed in this PR

Refines Azure Policy normalization and denormalization after #958, focusing on sub-resource array wrappers and plain-alias handling.

Changes:

  • Records only successful alias writes and preserves case-distinct destinations.
  • Adjusts envelope classification and mixed-case alias fallback resolution.
  • Adds regression cases and documents the fixes.
File Description
tests/​azure_policy/​normalization/​cases/​normalize_sub_resources.yaml Adds wrapper, casing, and alias-remapping regression cases.
tests/​azure_policy/​normalization/​cases/​data_plane_sub_resources.yaml Covers refused alias writes at the resource root.
src/​languages/​azure_policy/​aliases/​obj_map.rs Returns success status from nested setters.
src/​languages/​azure_policy/​aliases/​normalizer/​alias_resolution.rs Resolves fallback aliases from raw values and applies alias-specific remaps.
src/​languages/​azure_policy/​aliases/​denormalizer/​sub_resource.rs Updates write tracking, destination lookup, and envelope classification.
src/​languages/​azure_policy/​aliases/​denormalizer/​mod.rs Records alias writes only when they succeed.
CHANGELOG.md Documents the Azure Policy fixes.

Review effort: Max

Comment on lines +76 to +77
let (_, written, source) = last_write(|dest, key| dest == key)
.or_else(|| last_write(|dest, key| dest.eq_ignore_ascii_case(key)))?;
Comment on lines +186 to +188
Some(Value::Object(obj)) => find_key_ci_btree(obj, key).and_then(|k| {
let name = String::from(val_str(&k)?);
Some((name, obj.get(&k)?))
Comment on lines +330 to +331
envelope.retain(|field| {
!wrapped.contains(field) || ELEMENT_ENVELOPE_FIELDS.contains(&field.as_str())
continue;
};
obj_insert(result, target_lc, val);
obj_insert(result, target_lc, resolved);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants