|
| 1 | +Param( |
| 2 | + |
| 3 | + [Parameter(Mandatory = $false)] |
| 4 | + [string] $DefinitionsRootFolder, |
| 5 | + |
| 6 | + [ValidateSet("ALZ", "AMBA")] |
| 7 | + [string]$Type = "ALZ", |
| 8 | + |
| 9 | + [Parameter(Mandatory = $true)] |
| 10 | + [string]$PacEnvironmentSelector, |
| 11 | + |
| 12 | + [string]$LibraryPath |
| 13 | +) |
| 14 | + |
| 15 | +if ($LibraryPath -eq "") { |
| 16 | + if ($Tag) { |
| 17 | + git clone --depth 1 --branch $Tag https://github.com/anwather/Azure-Landing-Zones-Library.git .\temp |
| 18 | + $LibraryPath = ".\temp" |
| 19 | + } |
| 20 | + else { |
| 21 | + git clone --depth 1 https://github.com/anwather/Azure-Landing-Zones-Library.git .\temp |
| 22 | + $LibraryPath = ".\temp" |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +if ($DefinitionsRootFolder -eq "") { |
| 27 | + if ($null -eq $env:PAC_DEFINITIONS_FOLDER) { |
| 28 | + if ($ModuleRoot) { |
| 29 | + $DefinitionsRootFolder = "./Definitions" |
| 30 | + } |
| 31 | + else { |
| 32 | + $DefinitionsRootFolder = "$PSScriptRoot/../../Definitions" |
| 33 | + } |
| 34 | + } |
| 35 | + else { |
| 36 | + $DefinitionsRootFolder = $env:PAC_DEFINITIONS_FOLDER |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +try { |
| 41 | + $telemetryEnabled = (Get-Content $DefinitionsRootFolder/global-settings.jsonc | ConvertFrom-Json).telemetryOptOut |
| 42 | + $deploymentRootScope = (Get-Content $DefinitionsRootFolder/global-settings.jsonc | ConvertFrom-Json).pacEnvironments[0] |
| 43 | + if (!($telemetryEnabled)) { |
| 44 | + Write-Information "Telemetry is enabled" |
| 45 | + Submit-EPACTelemetry -Cuapid "pid-adaa7564-1962-46e6-92b4-735e91f76d43" -DeploymentRootScope $deploymentRootScope |
| 46 | + } |
| 47 | + else { |
| 48 | + Write-Information "Telemetry is disabled" |
| 49 | + } |
| 50 | +} |
| 51 | +catch {} |
| 52 | + |
| 53 | +New-Item -Path "$DefinitionsRootFolder\policyDefinitions" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 54 | +New-Item -Path "$DefinitionsRootFolder\policyDefinitions\$Type" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 55 | +New-Item -Path "$DefinitionsRootFolder\policySetDefinitions" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 56 | +New-Item -Path "$DefinitionsRootFolder\policySetDefinitions\$Type" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 57 | +New-Item -Path "$DefinitionsRootFolder\policyAssignments" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 58 | +New-Item -Path "$DefinitionsRootFolder\policyAssignments\$Type" -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 59 | + |
| 60 | +# Create policy definition objects |
| 61 | + |
| 62 | +foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\policy_definitions" -Recurse -File -Include *.json) { |
| 63 | + $fileContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json |
| 64 | + $baseTemplate = @{ |
| 65 | + name = $fileContent.name |
| 66 | + properties = $fileContent.properties |
| 67 | + } |
| 68 | + $category = $baseTemplate.properties.Metadata.category |
| 69 | + if (!(Test-Path $DefinitionsRootFolder\policyDefinitions\$Type\$category)) { |
| 70 | + New-Item -Path $DefinitionsRootFolder\policyDefinitions\$Type\$category -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 71 | + } |
| 72 | + $baseTemplate | Select-Object name, properties | ConvertTo-Json -Depth 50 | Out-File -FilePath $DefinitionsRootFolder\policyDefinitions\$Type\$category\$($fileContent.name).json -Force |
| 73 | + (Get-Content $DefinitionsRootFolder\policyDefinitions\$Type\$category\$($fileContent.name).json) -replace "\[\[", "[" | Set-Content $DefinitionsRootFolder\policyDefinitions\$Type\$category\$($fileContent.name).json |
| 74 | +} |
| 75 | + |
| 76 | +# Create policy set definition objects |
| 77 | + |
| 78 | +foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\policy_set_definitions" -Recurse -File -Include *.json) { |
| 79 | + $fileContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json |
| 80 | + $baseTemplate = @{ |
| 81 | + name = $fileContent.name |
| 82 | + properties = @{ |
| 83 | + description = $fileContent.properties.description |
| 84 | + displayName = $fileContent.properties.displayName |
| 85 | + metadata = $fileContent.properties.metadata |
| 86 | + parameters = $fileContent.properties.parameters |
| 87 | + policyType = $fileContent.properties.policyType |
| 88 | + policyDefinitionGroups = $fileContent.properties.policyDefinitionGroups |
| 89 | + } |
| 90 | + } |
| 91 | + $policyDefinitions = @() |
| 92 | + # Fix the policyDefinitionIds for custom policies |
| 93 | + foreach ($policyDefinition in $fileContent.properties.policyDefinitions) { |
| 94 | + $obj = @{ |
| 95 | + parameters = $policyDefinition.parameters |
| 96 | + groupNames = $policyDefinition.groupNames |
| 97 | + policyDefinitionReferenceId = $policyDefinition.policyDefinitionReferenceId |
| 98 | + } |
| 99 | + if ($policyDefinition.policyDefinitionId -match "managementGroups") { |
| 100 | + $obj.Add("policyDefinitionName", $policyDefinition.policyDefinitionId.split("/")[-1]) |
| 101 | + } |
| 102 | + else { |
| 103 | + $obj.Add("policyDefinitionId", $policyDefinition.policyDefinitionId) |
| 104 | + } |
| 105 | + $policyDefinitions += $obj |
| 106 | + } |
| 107 | + $baseTemplate.properties.policyDefinitions = $policyDefinitions |
| 108 | + |
| 109 | + $category = $baseTemplate.properties.Metadata.category |
| 110 | + if (!(Test-Path $DefinitionsRootFolder\policySetDefinitions\$Type\$category)) { |
| 111 | + New-Item -Path $DefinitionsRootFolder\policySetDefinitions\$Type\$category -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 112 | + } |
| 113 | + $baseTemplate | Select-Object name, properties | ConvertTo-Json -Depth 50 | Out-File -FilePath $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json -Force |
| 114 | + (Get-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json) -replace "\[\[", "[" | Set-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json |
| 115 | + (Get-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json) -replace "variables\('scope'\)", "'/providers/Microsoft.Management/managementGroups/$managementGroupId'" | Set-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json |
| 116 | + (Get-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json) -replace "', '", "" | Set-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json |
| 117 | + (Get-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json) -replace "\[concat\(('(.+)')\)\]", "`$2" | Set-Content $DefinitionsRootFolder\policySetDefinitions\$Type\$category\$($fileContent.name).json |
| 118 | +} |
| 119 | + |
| 120 | +# Create assignment objects |
| 121 | + |
| 122 | +try { |
| 123 | + $structureFile = Get-Content .\$Type.policy_default_structure.json -ErrorAction Stop | ConvertFrom-Json |
| 124 | + switch ($structureFile.enforcementMode) { |
| 125 | + "Default" { $enforcementModeText = "must" } |
| 126 | + "DoNotEnforce" { $enforcementModeText = "should" } |
| 127 | + } |
| 128 | +} |
| 129 | +catch { |
| 130 | + Write-Host "No policy default structure file found. Please run Create-ALZPolicyDefaultStructure.ps1 first." |
| 131 | + exit |
| 132 | +} |
| 133 | + |
| 134 | +foreach ($file in Get-ChildItem -Path "$LibraryPath\platform\$($Type.ToLower())\archetype_definitions" -Recurse -File -Include *.json) { |
| 135 | + $archetypeContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Json |
| 136 | + foreach ($requiredAssignment in $archetypeContent.policy_assignments) { |
| 137 | + switch ($Type) { |
| 138 | + "ALZ" { $fileContent = Get-ChildItem -Path $LibraryPath\platform\$Type\policy_assignments | Where-Object { $_.BaseName.Split(".")[0] -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json } |
| 139 | + "AMBA" { $fileContent = Get-ChildItem -Path $LibraryPath\platform\$Type\policy_assignments | Where-Object { $_.BaseName.Split(".")[0].Replace("_", "-") -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json } |
| 140 | + default { $fileContent = Get-ChildItem -Path $LibraryPath\platform\$Type\policy_assignments | Where-Object { $_.BaseName.Split(".")[0] -eq $requiredAssignment } | Get-Content -Raw | ConvertFrom-Json } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + $baseTemplate = @{ |
| 145 | + nodeName = "$($archetypeContent.name)/$($fileContent.name)" |
| 146 | + assignment = @{ |
| 147 | + name = $fileContent.Name |
| 148 | + displayName = $fileContent.properties.displayName |
| 149 | + description = $fileContent.properties.description |
| 150 | + } |
| 151 | + definitionEntry = @{ |
| 152 | + displayName = $fileContent.properties.displayName |
| 153 | + } |
| 154 | + parameters = @{} |
| 155 | + enforcementMode = $structureFile.enforcementMode |
| 156 | + } |
| 157 | + |
| 158 | + # Definition Version |
| 159 | + if ($null -ne $fileContent.properties.definitionVersion) { |
| 160 | + $baseTemplate.Add("definitionVersion", $fileContent.properties.definitionVersion) |
| 161 | + } |
| 162 | + |
| 163 | + # Definition Entry |
| 164 | + if ($fileContent.properties.policyDefinitionId -match "placeholder.+policySetDefinition") { |
| 165 | + $baseTemplate.definitionEntry.Add("policySetName", ($fileContent.properties.policyDefinitionId).Split("/")[-1]) |
| 166 | + } |
| 167 | + elseif ($fileContent.properties.policyDefinitionId -match "placeholder.+policyDefinition") { |
| 168 | + $baseTemplate.definitionEntry.Add("policyName", ($fileContent.properties.policyDefinitionId).Split("/")[-1]) |
| 169 | + } |
| 170 | + else { |
| 171 | + if ($fileContent.properties.policyDefinitionId -match "policySetDefinitions") { |
| 172 | + $baseTemplate.definitionEntry.Add("policySetId", ($fileContent.properties.policyDefinitionId)) |
| 173 | + } |
| 174 | + else { |
| 175 | + $baseTemplate.definitionEntry.Add("policyId", ($fileContent.properties.policyDefinitionId)) |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + #Scope |
| 181 | + $scopeTrim = $file.BaseName.split(".")[0] |
| 182 | + if ($scopeTrim -eq "root") { |
| 183 | + $scopeTrim = "alz" |
| 184 | + } |
| 185 | + if ($scopeTrim -eq "landing_zones") { |
| 186 | + $scopeTrim = "landingzones" |
| 187 | + } |
| 188 | + $scope = @{ |
| 189 | + $PacEnvironmentSelector = @( |
| 190 | + $structureFile.managementGroupNameMappings.$scopeTrim.value |
| 191 | + ) |
| 192 | + } |
| 193 | + $baseTemplate.Add("scope", $scope) |
| 194 | + |
| 195 | + # Base Parameters |
| 196 | + if ($fileContent.name -ne "Deploy-Private-DNS-Zones") { |
| 197 | + foreach ($parameter in $fileContent.properties.parameters.psObject.Properties.Name) { |
| 198 | + $baseTemplate.parameters.Add($parameter, $fileContent.properties.parameters.$parameter.value) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + # Non-compliance messages |
| 203 | + if ($null -ne $fileContent.properties.nonComplianceMessages) { |
| 204 | + $obj = @( |
| 205 | + @{ |
| 206 | + message = $fileContent.properties.nonComplianceMessages.message -replace "{enforcementMode}", $enforcementModeText |
| 207 | + } |
| 208 | + ) |
| 209 | + $baseTemplate.Add("nonComplianceMessages", $obj) |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + # Check for explicit parameters |
| 214 | + if ($fileContent.name -ne "Deploy-Private-DNS-Zones") { |
| 215 | + foreach ($key in $structureFile.defaultParameterValues.psObject.Properties.Name) { |
| 216 | + if ($structureFile.defaultParameterValues.$key.policy_assignment_name -eq $fileContent.name) { |
| 217 | + $keyName = $structureFile.defaultParameterValues.$key.parameters.parameter_name |
| 218 | + $baseTemplate.parameters.$keyName = $structureFile.defaultParameterValues.$key.parameters.value |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + else { |
| 223 | + $dnsZoneRegion = $structureFile.defaultParameterValues.private_dns_zone_region.parameters.value |
| 224 | + $dnzZoneSubscription = $structureFile.defaultParameterValues.private_dns_zone_subscription_id.parameters.value |
| 225 | + $dnzZoneResourceGroupName = $structureFile.defaultParameterValues.private_dns_zone_resource_group_name.parameters.value |
| 226 | + foreach ($parameter in $fileContent.properties.parameters.psObject.Properties.Name) { |
| 227 | + $value = "/subscriptions/$dnzZoneSubscription/resourceGroups/$dnzZoneResourceGroupName/providers/Microsoft.Network/privateDnsZones/$($fileContent.properties.parameters.$parameter.value.split("/")[-1])" |
| 228 | + #$value = $fileContent.properties.parameters.$parameter.value -replace "00000000-0000-0000-0000-000000000000", $dnzZoneSubscription -replace "placeholder", $dnzZoneResourceGroupName |
| 229 | + $baseTemplate.parameters.Add($parameter, $value) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | + $category = $structureFile.managementGroupNameMappings.$scopeTrim.management_group_function |
| 235 | + if (!(Test-Path $DefinitionsRootFolder\policyAssignments\$Type\$category)) { |
| 236 | + New-Item -Path $DefinitionsRootFolder\policyAssignments\$Type\$category -ItemType Directory -Force -ErrorAction SilentlyContinue |
| 237 | + } |
| 238 | + $baseTemplate | Select-Object nodeName, assignment, definitionEntry, definitionVersion, enforcementMode, parameters, nonComplianceMessages, scope | ConvertTo-Json -Depth 50 | Out-File -FilePath $DefinitionsRootFolder\policyAssignments\$Type\$category\$($fileContent.name).json -Force |
| 239 | + (Get-Content $DefinitionsRootFolder\policyAssignments\$Type\$category\$($fileContent.name).json) -replace "\[\[", "[" | Set-Content $DefinitionsRootFolder\policyAssignments\$Type\$category\$($fileContent.name).json |
| 240 | + if ($fileContent.name -eq "Deploy-Private-DNS-Zones") { |
| 241 | + (Get-Content $DefinitionsRootFolder\policyAssignments\$Type\$category\$($fileContent.name).json) -replace "\.ne\.", ".$dnsZoneRegion." | Set-Content $DefinitionsRootFolder\policyAssignments\$Type\$category\$($fileContent.name).json |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + |
| 246 | +} |
| 247 | + |
| 248 | +if ($LibraryPath -eq ".\temp") { |
| 249 | + Remove-Item .\temp -Recurse -Force -ErrorAction SilentlyContinue |
| 250 | +} |
| 251 | + |
0 commit comments