ConvertTo-Bylaw turns policy JSON into Bylaw files that compile back to the same rule¶
Context and Problem Statement¶
Teams adopting Bylaw already have policy definitions as JSON: their own, EPAC's, the Azure Landing Zones Library's, or exported from Azure. Rewriting them by hand is slow, and easy to get subtly wrong, which is the problem Bylaw exists to solve. A decompiler, ConvertTo-Bylaw, was planned from the start (ADR-0005, ADR-0006), and the IR already keeps Not and generic groups for it.
Decompiling isn't the reverse of a single step. An if condition finds violations, while Bylaw describes compliance, split into When and Require. Compiling also adds missing-field guards (ADR-0004), generates effect parameters (ADR-0017) and resolves short alias names (ADR-0008). Hand-written JSON follows none of Bylaw's patterns. How does ConvertTo-Bylaw produce a readable .bylaw.ps1 file, and how does it know the file is right?
Decision Drivers¶
- Correct first: a converted file compiles to a rule with the same meaning as the input. A readable file that means something else is worse than none.
- Readable second: the output looks like what a person would write: short field names,
Requireblocks that say what they require, and an effect parameter only when the input had a real one. - Honest: what can't be converted is reported, per definition, never silently dropped or approximated.
- Works on any policy JSON, not only Bylaw's own output.
Considered Options¶
- Convert, then prove the result by compiling it and comparing
- Convert with fixed patterns, without checking
- Convert everything to raw conditions
Decision Outcome¶
Chosen option: "Convert, then prove the result by compiling it and comparing".
The command¶
The command is ConvertTo-Bylaw: it reads as "convert this JSON to Bylaw", and carries Bylaw's name like the other commands, so it is unlikely to clash with another module's. The spec's earlier name was ConvertFrom-PolicyJson.
ConvertTo-Bylaw -Path ./Definitions -OutputPath ./policies
Get-AzPolicyDefinition -Custom | ConvertTo-Bylaw -OutputPath ./policies
- Input:
-Pathtakes files and folders of policy and policy set definitions in the EPAC format, plain{ name, properties }JSON, or a barepropertiesobject, and objects fromGet-AzPolicyDefinitionandGet-AzPolicySetDefinitionon the pipeline. Built-in definitions can be converted too. - Output: one
.bylaw.ps1file per definition, named after it, mirroring the input folders. A DeployIfNotExists template is written beside it, intemplates/<name>.json, and referenced with-TemplateFile(ADR-0031).-PassThrureturns, per definition: name, source, the script text, a status, and diagnostics. Without-OutputPath, nothing is written. - Status:
Converted(the file uses only the language),EscapeHatch(it uses raw conditions for some parts, ADR-0040), orFailed(nothing was written, and the diagnostics say why). Diagnostics are a new range, BYL6xxx, in the catalogue (ADR-0037). Since 2026-11-29, the warnings a converted file compiles with are reported too, as BYL6009 with the warning's own code and line, so they show at conversion rather than at the first build. Unknown-alias warnings are left out, since BYL6007 covers them. - Existing files are never overwritten unless
-Forceis given.
How it converts¶
- Read the JSON into the IR, which can hold any supported condition, including
notand generic groups. - Recognise each part, and choose the Bylaw form for it:
- Resource: a top-level
type equalsbecomesResource '<type>'; with no type condition,Resource *. Field names are shortened to the formFieldresolves back to the same alias, and kept in full when the short form would be ambiguous (ADR-0008). - When and Require: the top-level conjuncts other than the type are applicability conditions, written in
Whenas they are, except one, which is the violation: ananyOf, if there is one, or otherwise the last conjunct. Each alternative of the violation becomes aRequire, inverted back into its compliant form. Any split compiles to the same rule, so this is a readability choice, and the proof in step 4 checks it. - Statements: each condition is matched against the forms the compiler emits. A violation
notEqualswith no guard isMust -Be; anequalswith no guard isMust -Not -Be -IfPresent; anequalsinsideanyOfwithexists: falseis the strictMust -Not -Be; and so on through ADR-0004's table. To stay exact, candidate statements are compiled with Bylaw's own inversion, and the one that reproduces the condition is chosen. A guard Bylaw never writes, such asanyOf[ exists: false, notEquals ], wherenotEqualsis already true on a missing field, is read as the group it is, so its parts become statements of their own:Must -ExistandMust -Be(added 2026-11-29). - Arrays:
countwithwherebecomesEachwhen it's the formEachemits, andCount -Whereotherwise (ADR-0032). - Values:
[parameters('x')]becomesParam X, recognised tag and resource group expressions becomeTagandResourceGroupTag, and other expressionsArm. - Effect:
[parameters('effect')]with a declared parameter becomesParameter Effect ...andEffect (Param Effect). A parameter identical to the one Bylaw generates for a literal effect becomes the literal, e.g.Effect Deny. Effects with details become their keywords: Modify with its operations, and roles by name from the role map (ADR-0022); Append; DenyAction; Manual; AuditIfNotExists and DeployIfNotExists with their existence condition asRequire(ADR-0030). - Parameters and properties: parameter names become PascalCase DSL names, with
JsonNamewhere the original wouldn't round-trip (ADR-0018). Unknown metadata goes toProperties.Metadata. - Initiatives: an EPAC member referenced by
policyDefinitionNamebecomesPolicyDefinition '<name>'; a built-in becomes-Id <guid>; any other ID is kept in full, including a plain JSON member from the user's own build, because naming it needs adefinitionScopethe converted file can't be built without (ADR-0035). A member too long for one line is splatted from a hashtable. Groups becomeDefinitionGroup. A member without apolicyDefinitionReferenceId, which Azure generates when it's left out, is named after its policy, its name or the last part of its ID, made unique with-2and so on, since Bylaw always writes one; the proof compares the input with those names filled in, and BYL6010 lists them, because a deployed initiative needs the IDs Azure gave its members (added 2026-11-30). - Write the file in Bylaw's style: the README's layout, the hashtable form for parameters with several options (ADR-0021), and
Requiredescriptions made from the condition, such as'publicNetworkAccess is Disabled', because the JSON has none. A comment at the top says which file it was converted from. - Prove it: the converted file is compiled in memory and compared with the input by meaning (ADR-0041). A file that doesn't prove
Equivalentisn't written, and is reported asFailedwith the first difference.
Anything step 2 can't express in the language is written with the escape hatch (ADR-0040), for that condition only, so the rest of the policy stays readable. A definition fails outright only when even that can't express it, for example a resource provider mode such as Microsoft.Kubernetes.Data, which language version 1.0 doesn't support.
Consequences¶
- Good, because every converted file is known to compile to the same rule as its input.
- Good, because the readable choices, such as the
WhenandRequiresplit and the descriptions, are safe to improve later: the proof catches a mistake. - Good, because migrating a set of definitions is one command, with a status per definition.
- Bad, because the generated
Requiredescriptions describe the condition, not the intent, and need a person to improve them. - Bad, because the proof is only as strong as the comparison (ADR-0041): a pair it can't prove equivalent is reported as failed, even if it is.
Confirmation¶
The round-trip tests (ADR-0041) convert every golden file and every example back to Bylaw, prove each one, and compare the converted source with the original where the original is in the style the converter writes. The ALZ coverage report (ADR-0042) runs it over the Azure Landing Zones Library.
Pros and Cons of the Options¶
Convert, then prove the result by compiling it and comparing¶
- Good, because correctness doesn't depend on every pattern being right.
- Bad, because it needs a comparison that understands meaning.
Convert with fixed patterns, without checking¶
- Good, because it is simpler.
- Bad, because a wrong pattern, such as a missed guard, silently changes the policy, which is the worst outcome for a migration.
Convert everything to raw conditions¶
- Good, because it is always exact.
- Bad, because the output isn't Bylaw in any useful sense: no checked aliases, no inversion, nothing to read.