Array properties¶
Many resource properties are arrays: a network security group's rules, a storage account's IP rules, a key vault's access policies. Bylaw has two ways to make a statement about them:
Eachsays what every element must be.Countsays how many elements match, and is tested like any number.
Both take a -Where block that chooses the elements, in the same way. Each then states a requirement for the chosen elements, and Count counts them.
Every element: Each¶
Each takes the array's path, ending in [*], and a block of statements that every element must meet. Inside it, field names are relative to the element:
Require 'No IP rule allows every address' {
Each networkAcls.ipRules[*] {
Field value | Must -Not -Be '0.0.0.0/0'
}
}
Statements in an Each combine with AND, as they do in Require, and AnyOf gives alternatives.
Over an array of plain values, such as strings, Element is the element itself. Each blocks nest, one per array level:
Require 'No access policy grants purge' {
Each accessPolicies[*] {
Each permissions.keys[*] {
Element | Must -Not -Be 'purge'
}
}
}
Some elements: Each -Where¶
Often a requirement is about some of the elements: every inbound rule that allows traffic must not come from the internet. -Where chooses those elements, and the block says what they must be:
Require 'No rule allows inbound traffic from the internet' {
Each securityRules[*] -Where {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
} {
Field sourceAddressPrefix | Must -Not -BeIn '*', 'Internet'
}
}
-Whereis written as is, likeWhenandCount -Where: you write the elements you mean, and it isn't inverted or given missing-field checks. A missing field is tested as Azure tests it, so a rule with nodirectiondoesn't matchMust -Be 'Inbound'.- The block is an ordinary
Eachbody: the compliant state, inverted by Bylaw, with the missing-field rules below. - Elements
-Wheredoesn't choose are compliant, and so is an array with no elements at all. -Whereis optional. Without it, every element must meet the block.
The same requirement can be written without -Where, by describing a rule that is fine, Each securityRules[*] { AnyOf { … } }: outbound, or denying, or from elsewhere. That form is correct but easy to get wrong, since AllOf looks just as natural and rejects almost every network security group. Prefer -Where. ConvertTo-Bylaw writes it for policies with this pattern.
Missing and empty arrays¶
An Each passes when the array is missing or empty: there are no elements to break the condition. That is how Azure evaluates it, and it's the behaviour authors most often don't expect. A storage account with no IP rules passes "every IP rule is from an allowed range".
To require the array as well, say so in the same Require:
Require 'Has IP rules, and each is from an allowed range' {
Count networkAcls.ipRules[*] | Must -BeGreaterThan 0
Each networkAcls.ipRules[*] {
Field value | Must -BeLike '10.*'
}
}
The count requires at least one element, so a missing or empty array is a violation. Field networkAcls.ipRules | Must -Exist only requires the property to be set.
A missing field inside an element is compliant too, unlike in Require. Add -Strict to Must to make it a violation. -IfPresent isn't valid inside Each, since a missing field is already compliant there. Neither is valid in -Where, which is written as is. Operators and missing fields has the full table.
How many: Count¶
Count gives the number of elements that match its -Where block, or all of them without one. Pipe it to Must with -Be, -BeIn or the four comparisons:
Require 'At least one IP rule' {
Count networkAcls.ipRules[*] | Must -BeGreaterThan 0
}
Require 'Few rules allow inbound traffic' {
Count securityRules[*] -Where {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
} | Must -BeLessOrEqual 5
}
-Wherechooses what to count, as inEach: written as is, with field names relative to the element,Elementfor the element itself, andAnyOf,AllOfand nested counts inside.- In
Require, the comparison is inverted like any other:-BeGreaterThan 0emitslessOrEquals 0. A count is never missing, so it gets no guard.
Counting a list of values¶
A value count counts the members of a list rather than an array property: a literal array, Param or Arm. Current is the member being counted, and -Name gives it a name, which nested counts need:
Require 'The name matches an allowed pattern' {
Count -Value (Param NamePatterns) -Name pattern -Where {
Field name | Must -BeLike (Current pattern)
} | Must -BeGreaterThan 0
}
To compare with the length of a list, use Param Name -Length. Here every pattern must match, not just one:
Require 'The name matches every pattern' {
Count -Value (Param NamePatterns) -Name pattern -Where {
Field name | Must -BeLike (Current pattern)
} | Must -Be (Param NamePatterns -Length)
}
Current can't name a tag: Tag (Current tagName) is a compile error (BYL1059), because Azure rejects a field named with current(). To require every tag in a list, test the tags object for each name instead:
Require 'Every mandatory tag is set' {
Count -Value (Param MandatoryTags) -Name tagName -Where {
Field tags | Must -ContainKey (Current tagName)
} | Must -Be (Param MandatoryTags -Length)
}
To test the tags' values, check each tag with its own Tag.
Which to use¶
| To say | Write |
|---|---|
| Every element meets P | Each path[*] { P } |
| Every element that matches S meets P | Each path[*] -Where { S } { P } |
| No element matches S | Count path[*] -Where { S } | Must -Be 0 |
| At least one element matches S | Count path[*] -Where { S } | Must -BeGreaterThan 0 |
| At most, at least or exactly N elements match S | Count path[*] -Where { S } with the comparison |
| The array has elements | Count path[*] | Must -BeGreaterThan 0 |
| A value matches one, or all, of a list | Count -Value (Param List), as above |
Each path[*] -Where { S } { P } is the same as Count path[*] -Where { S, and the violation of P } | Must -Be 0, which is what Bylaw emits for it. So "no inbound Allow rule comes from the internet" can also be written by describing the forbidden rule and requiring none:
Require 'No rule allows inbound traffic from the internet' {
Count securityRules[*] -Where {
Field direction | Must -Be 'Inbound'
Field access | Must -Be 'Allow'
Field sourceAddressPrefix | Must -BeIn '*', 'Internet'
} | Must -Be 0
}
Both compile to the same rule. Use Each -Where when the requirement is a property every chosen element must have, especially one with several statements, a nested Each or -Strict. Use Count when you're counting.
Limits¶
Azure allows at most 5 field counts per array, 10 value counts per rule, and 100 iterations over literal lists. Each Each compiles to one count, with or without -Where, so it counts towards the first limit. The build checks all three: if it reports too many counts on one array (BYL3050), combine the Each blocks for that array into one.