Skip to content

Commit 2910a6d

Browse files
krallsmSean Krall
andauthored
bring pattern matching feature to exemptions (#1008)
* added subscription pattern matching to exemptions * undo formatting to try and get a cleaner PR * add documentation * update doc with tip --------- Co-authored-by: Sean Krall <[email protected]>
1 parent cbde977 commit 2910a6d

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

Docs/policy-exemptions.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,30 @@ It is unchanged from previous versions.
168168
In CSV files, the `scope` column is still supported for backward compatibility. We recommend using the `scopes` column for all new exemptions. `scopes` is a list of ampersand `&` separated strings.
169169

170170
In JSON files, `scope` is a string and `scopes` is an array of strings.
171-
171+
172+
### Pattern Matching
173+
174+
You can define a pattern to match on subscriptions or resource groups for scopes. This allows an exemption to add matched subscriptions or resource group names to the exempted scope. It is not dynamic i.e. if you add subscriptions or resource groups later and want to include them you would have to run the plan again.
175+
176+
The syntax is:
177+
178+
```json
179+
"scopes": [
180+
"/subscriptions/subscriptionsPattern/wildcard-pattern-to-match"
181+
]
182+
```
183+
184+
or
185+
186+
```json
187+
"scopes": [
188+
"/subscriptions/*/resourceGroups/rg-pattern-to-match"
189+
]
190+
```
191+
192+
> [!TIP]
193+
> If you want to match against a subscriptions name, rather than it's ID, you need to use the `subscriptionsPattern` to designate the name with the wildcards.
194+
172195
## Combining Policy Definitions at multiple Scopes
173196

174197
When using **Option A** or **Option C** and/or `scopes`, EPAC needs to generate concatenated values for `name`, `displayName`, and `description` to ensure uniqueness and readability.

Scripts/Helpers/Build-ExemptionsPlan.ps1

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,60 @@ function Build-ExemptionsPlan {
547547
$scopeIsValid = $true
548548
$resourceStatus = "notAnIndividualResource"
549549
$splits = $currentScope -split "/"
550+
551+
$expandedScopes = [System.Collections.ArrayList]::new()
552+
$patternMatched = $false
553+
554+
if ($trimmedScope -match "subscriptionsPattern") {
555+
$patternMatched = $true
556+
$rootScope = $ScopeTable["root"]
557+
if ($null -ne $rootScope) {
558+
$rootScopeChildren = $rootScope.childrenTable
559+
$pattern = $trimmedScope.split("/")[-1]
560+
$rootScopeChildren.Keys | Foreach-Object {
561+
if ($rootScopeChildren.$_.type -eq "/subscriptions") {
562+
$subName = $rootScopeChildren.$_.displayName
563+
if ($subName -like $pattern) {
564+
$expandedScope = @{
565+
scope = $rootScopeChildren.$_.id
566+
scopePostfix = $scopePostfix
567+
}
568+
$null = $expandedScopes.Add($expandedScope)
569+
}
570+
}
571+
}
572+
}
573+
}
574+
elseif ($trimmedScope.Contains("*")) {
575+
$patternMatched = $true
576+
foreach ($scopeId in $ScopeTable.Keys) {
577+
if ($scopeId -ne "root" -and $scopeId -like $trimmedScope) {
578+
$expandedScope = @{
579+
scope = $scopeId
580+
scopePostfix = $scopePostfix
581+
}
582+
$null = $expandedScopes.Add($expandedScope)
583+
}
584+
}
585+
}
586+
587+
if (-not $patternMatched) {
588+
$expandedScope = @{
589+
scope = $trimmedScope
590+
scopePostfix = $scopePostfix
591+
}
592+
$null = $expandedScopes.Add($expandedScope)
593+
}
594+
595+
foreach ($expandedScopeInfo in $expandedScopes) {
596+
$currentScope = $expandedScopeInfo.scope
597+
$scopePostfix = $expandedScopeInfo.scopePostfix
598+
$trimmedScope = $currentScope.Trim()
599+
$subscriptionId = ""
600+
$scopeIsValid = $true
601+
$resourceStatus = "notAnIndividualResource"
602+
$splits = $currentScope -split "/"
603+
550604
if ($currentScope.StartsWith("/subscriptions/")) {
551605
$subscriptionId = $splits[2]
552606
if ($currentScope.Contains("/providers/")) {
@@ -1011,6 +1065,7 @@ function Build-ExemptionsPlan {
10111065
}
10121066
#endregion process each assignment (or multiple assignments)
10131067

1068+
}
10141069
}
10151070
#endregion process each scope
10161071
}

0 commit comments

Comments
 (0)