Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add ability to exclude policy set/initiative child definitions for China policies #377

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions .github/scripts/Invoke-PolicyToBicep-China.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ param (
[string]
$defintionsSetTxtFileName = "_mc_policySetDefinitionsBicepInput.txt",
[string]
$assignmentsTxtFileName = "_mc_policyAssignmentsBicepInput.txt"
$assignmentsTxtFileName = "_mc_policyAssignmentsBicepInput.txt",
[array]
$excludedPolicySetChildDefinitionsReferenceIds = @("AVDHostPoolsDeployDiagnosticLogDeployLogAnalytics", "defenderForSqlServerVirtualMachines", "defenderForOssDb", "defenderForCosmosDbs", "defenderForAppServices", "defenderForStorageAccounts", "defenderForKeyVaults")
)

# This script relies on a custom set of classes and functions
Expand Down Expand Up @@ -129,19 +131,26 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$definitionReferenceId = $_.policyDefinitionReferenceId
$definitionParameters = $_.parameters

if ($definitionParameters) {
$definitionParameters | Sort-Object | ForEach-Object {
if ($definitionReferenceId -notin $excludedPolicySetChildDefinitionsReferenceIds) {

if ($definitionParameters) {
$definitionParameters | Sort-Object | ForEach-Object {
[System.Collections.Hashtable]$definitionParametersOutputArray = [ordered]@{}
$definitionParametersOutputArray.Add("parameters", $_)
}
}
else {
[System.Collections.Hashtable]$definitionParametersOutputArray = [ordered]@{}
$definitionParametersOutputArray.Add("parameters", $_)
$definitionParametersOutputArray.Add("parameters", @{})
}

$definitionParametersOutputJSONObject.Add("$definitionReferenceId", $definitionParametersOutputArray)
}
else {
[System.Collections.Hashtable]$definitionParametersOutputArray = [ordered]@{}
$definitionParametersOutputArray.Add("parameters", @{})
Write-Information "==> Skipping '$definitionReferenceId' as it is in the excluded list" -InformationAction Continue
}

$definitionParametersOutputJSONObject.Add("$definitionReferenceId", $definitionParametersOutputArray)
}

Write-Information "==> Adding parameters to '$parametersFileName'" -InformationAction Continue
Add-Content -Path "$rootPath/$definitionsSetLongPath/$parametersFileName" -Value ($definitionParametersOutputJSONObject | ConvertTo-Json -Depth 10) -Encoding "utf8"

Expand Down Expand Up @@ -193,25 +202,31 @@ function New-PolicySetDefinitionsBicepInputTxtFile {
$definitionId = $($policySetDefinitionsOutputForBicep[$_][0])
$groups = $($policySetDefinitionsOutputForBicep[$_][1])

# If definitionReferenceId or definitionReferenceIdForParameters contains apostrophes, replace that apostrophe with a backslash and an apostrohphe for Bicep string escaping
if ($definitionReferenceId.Contains("'")) {
$definitionReferenceId = $definitionReferenceId.Replace("'", "\'")
}
if ($definitionReferenceId -notin $excludedPolicySetChildDefinitionsReferenceIds) {

if ($definitionReferenceIdForParameters.Contains("'")) {
$definitionReferenceIdForParameters = $definitionReferenceIdForParameters.Replace("'", "\'")
}
# If definitionReferenceId or definitionReferenceIdForParameters contains apostrophes, replace that apostrophe with a backslash and an apostrohphe for Bicep string escaping
if ($definitionReferenceId.Contains("'")) {
$definitionReferenceId = $definitionReferenceId.Replace("'", "\'")
}

if ($definitionReferenceIdForParameters.Contains("'")) {
$definitionReferenceIdForParameters = $definitionReferenceIdForParameters.Replace("'", "\'")
}

# If definitionReferenceId contains, then wrap in definitionReferenceId value in [] to comply with bicep formatting
if ($definitionReferenceIdForParameters.Contains("-") -or $definitionReferenceIdForParameters.Contains(" ") -or $definitionReferenceIdForParameters.Contains("\'")) {
$definitionReferenceIdForParameters = "['$definitionReferenceIdForParameters']"
# If definitionReferenceId contains, then wrap in definitionReferenceId value in [] to comply with bicep formatting
if ($definitionReferenceIdForParameters.Contains("-") -or $definitionReferenceIdForParameters.Contains(" ") -or $definitionReferenceIdForParameters.Contains("\'")) {
$definitionReferenceIdForParameters = "['$definitionReferenceIdForParameters']"

# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable, without the '.' before the definitionReferenceId to make it an accessor
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable, without the '.' before the definitionReferenceId to make it an accessor
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
else {
# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
}
}
else {
# Add nested array of objects to each Policy Set/Initiative Definition in the Bicep variable
Add-Content -Path "$rootPath/$definitionsSetLongPath/$defintionsSetTxtFileName" -Encoding "utf8" -Value "`t`t`t{`r`n`t`t`t`tdefinitionReferenceId: '$definitionReferenceId'`r`n`t`t`t`tdefinitionId: '$definitionId'`r`n`t`t`t`tdefinitionParameters: $policySetDefParamVarCreation.$definitionReferenceIdForParameters.parameters`r`n`t`t`t`tdefinitionGroups: [$groups]`r`n`t`t`t}"
Write-Information "==> Skipping '$definitionReferenceId' as it is in the excluded list" -InformationAction Continue
}
}
}
Expand Down