Authors describe the compliant state; the compiler inverts it¶
Context and Problem Statement¶
An Azure Policy if block describes the non-compliant state: when it evaluates to true, the effect fires. Authors naturally think in terms of what a good resource looks like, and hand-inverting conditions, especially count/where, is a common source of bugs. How should Bylaw map author intent onto policyRule.if?
Decision Drivers¶
- Authors should write the rule the way they would explain it ("public network access is disabled").
- The emitted JSON must stay readable and look like idiomatic hand-written policy.
- The transformation must be mechanical and testable.
Considered Options¶
- Desired-state authoring, inverted by the compiler, with negation pushed inward
- Desired-state authoring, emitted as
{ "not": { ... } }around the requirements - Violation-state authoring, as in raw Azure Policy
Decision Outcome¶
Chosen option: "Desired-state authoring, inverted by the compiler, with negation pushed inward".
The compiled rule is:
if = allOf[
type condition (omitted for Resource *)
When conditions... (emitted as written, not inverted)
NOT( R1 AND R2 AND ... ) (Require blocks, see ADR-0003)
]
then = effect
NOT(...) is never emitted as a not wrapper. It is pushed inward:
- De Morgan:
NOT(a AND b)becomesanyOf[NOT a, NOT b], andNOT(a OR b)becomesallOf[NOT a, NOT b]. - Operators are inverted using the table in ADR-0015, for example
equalsbecomesnotEquals. Each path[*] { P }means "every element satisfies P". Its negation is "at least one element violates P", which is emitted as{ "count": { "field": "<path>[*]", "where": NOT P }, "greater": 0 }. The negation is pushed intowherewith the same rules.- Missing-field guards are added as described in ADR-0004.
Two places compile a compliant state as written instead of its violation, because Azure expects the compliant form there:
- The existence condition of
AuditIfNotExistsandDeployIfNotExists, which says what a related resource must look like. ItsRequireblocks are emitted uninverted, with the same missing-field rules (ADR-0030). - The
-Whereselection ofCount, which says which elements to count. LikeWhen, it is a selection, not a requirement (ADR-0032).
Every operator Bylaw supports in language version 1.0 has an inverse, so not is never needed. The IR keeps a Not node anyway, so that a future decompiler can read hand-written JSON that uses not. Emitters only produce not for IR that cannot be pushed further. That cannot happen in 1.0, and if it ever does it is a compiler bug, caught by tests.
Edge cases:
- No
Requireblocks. This is allowed. Every resource that matches the type andWhenconditions is non-compliant, which is how a "deny this resource type" policy is written. The compiler emits a warning so the omission is visible. - No
Resourceblock.Resource 'type'with no scriptblock is the same asResource 'type' { }: a type condition with noWhenand noRequire. The same warnings apply. This is common withAuditIfNotExistsandDeployIfNotExists, whose conditions are in the effect's own block (ADR-0030).Resourceitself is still required. - Empty
RequireorEachblock. This is a compile error. - Single-element groups are unwrapped and nested same-kind groups are merged (flattening pass).
Consequences¶
- Good, because authors write what "good" looks like and the compiler handles the inversion, including the easy-to-get-wrong
countform. - Good, because the output reads like idiomatic policy JSON (see the storage golden test).
- Bad, because the emitted JSON no longer mirrors the DSL statement by statement. The IR keeps provenance (which
Requireeach condition came from) so tooling and the decompiler can map between them.
Confirmation¶
Unit tests check each inversion rule. Golden tests check complete compiled rules, starting with storage-deny-public-network-access.
Pros and Cons of the Options¶
Inverted with negation pushed inward¶
- Good, because the output is idiomatic and easy to review.
- Bad, because the compiler must know an exact inverse for every operator.
not wrapper¶
- Good, because it is trivial to implement.
- Bad, because the output is harder to read, and
notaround[*]fields is easy to misunderstand.
Violation-state authoring¶
- Good, because it is a one-to-one mapping with no inversion risk.
- Bad, because it removes one of Bylaw's main reasons to exist.