Operators and missing fields¶
Operators¶
Each Must takes one operator. The violation column is what Bylaw emits: the condition that finds a resource breaking the requirement.
Must |
Compliant when the value... | Azure operator | Violation emitted |
|---|---|---|---|
-Be v |
equals v |
equals |
notEquals |
-BeIn a |
is one of a |
in |
notIn |
-BeLike p |
matches a wildcard pattern, with at most one * |
like |
notLike |
-Match p |
matches an Azure match pattern, ignoring case | matchInsensitively |
notMatchInsensitively |
-MatchCase p |
matches an Azure match pattern, case-sensitively | match |
notMatch |
-ContainText s |
contains the text s |
contains |
notContains |
-ContainKey k |
has the key k (for example a tag) |
containsKey |
notContainsKey |
-Exist |
is present | exists: true |
exists: false |
-BeGreaterThan n |
is greater than n |
greater |
lessOrEquals |
-BeGreaterOrEqual n |
is greater than or equal to n |
greaterOrEquals |
less |
-BeLessThan n |
is less than n |
less |
greaterOrEquals |
-BeLessOrEqual n |
is less than or equal to n |
lessOrEquals |
greater |
| Switch | Effect |
|---|---|
-Not |
Reverses the operator: Must -Not -Be 'X' emits equals 'X' as the violation. |
-IfPresent |
A missing field is compliant. Not valid inside Each, with -Exist, or in When. |
-Strict |
A missing element field is a violation. Only valid inside Each. |
Azure match patterns are not regular expressions. # matches a digit, ? a letter, . any character, and every other character, including *, matches only itself. Bylaw warns about patterns that look like a regex.
-Match is case-insensitive, like PowerShell's. -ContainText tests text, unlike Pester's -Contain, which tests membership of a collection. Values can be literals, (Param Name), (ResourceGroupTag name) or (Arm '[...]').
Must can also test an expression instead of a field: pipe ResourceGroupTag, Param or Arm into it. For example, When { ResourceGroupTag CostCenter | Must -Not -Be '' } applies a policy only to resource groups that have the tag. An expression is never missing, so -IfPresent, -Strict and -Exist don't apply to it, and it can't be used inside Each yet (a Count can).
Missing fields¶
If a resource doesn't set a property that a Require checks, Bylaw treats it as non-compliant. Add -IfPresent to Must when the property is optional and only matters if it's set.
| Where | A missing field is | To change it |
|---|---|---|
Require |
a violation | Must -IfPresent makes it compliant |
A field inside Each |
compliant | Must -Strict makes it a violation |
When |
not treated specially; operators are emitted as written | |
The array of an Each |
compliant: a missing or empty array has no elements to break the condition | add Field path | Must -Exist to require it, or Count path[*] | Must -BeGreaterThan 0 |
An existence condition (AuditIfNotExists, DeployIfNotExists) |
a related resource without the field doesn't count | Must -IfPresent, as in Require |
Count -Where |
not treated specially; emitted as written, like When |
Some operators already give the right answer on a missing field, and others don't. notEquals finds a missing value, so Must -Be 'Disabled' needs nothing extra. equals doesn't, so for Must -Not -Be 'X' Bylaw adds an exists check:
"anyOf": [
{
"field": "Microsoft.Storage/storageAccounts/allowSharedKeyAccess",
"exists": false
},
{
"field": "Microsoft.Storage/storageAccounts/allowSharedKeyAccess",
"equals": true
}
]
Bylaw adds these checks only where they change the result. An empty string counts as present. To require a non-empty value, add Must -Not -Be ''.
The behaviour of each operator on a missing field isn't documented by Microsoft. Bylaw's rules come from measurements in a real subscription, where missing tags and missing properties behaved the same. ADR-0004 has the full table and the measurement script.