Skip to content

Commit b50c4bb

Browse files
committed
improve template imports
bring in named location data
1 parent 0f0af2d commit b50c4bb

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tools/GitHub/Invoke-ExecCommunityRepo.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,14 @@ function Invoke-ExecCommunityRepo {
172172
Write-Host 'Found a migration table, getting contents'
173173
$MigrationTable = (Get-GitHubFileContents -FullName $FullName -Branch $Branch -Path $MigrationTable.path).content | ConvertFrom-Json
174174
}
175+
176+
$NamedLocations = $Files | Where-Object { $_.name -match 'ALLOWED COUNTRIES' }
177+
$LocationData = foreach ($Location in $NamedLocations) {
178+
(Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $Location.path).content | ConvertFrom-Json
179+
}
175180
}
176-
Import-CommunityTemplate -Template $Content -SHA $Template.sha -MigrationTable $MigrationTable
181+
Import-CommunityTemplate -Template $Content -SHA $Template.sha -MigrationTable $MigrationTable -LocationData $LocationData
182+
177183
$Results = @{
178184
resultText = 'Template imported'
179185
state = 'success'

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,19 @@ function New-CIPPCAPolicy {
123123
if (!$locations) { continue }
124124
foreach ($location in $locations) {
125125
if (!$location.displayName) { continue }
126-
$CheckExististing = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -tenantid $TenantFilter -asApp $true
127-
if ($Location.displayName -in $CheckExististing.displayName) {
126+
$CheckExisting = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -tenantid $TenantFilter -asApp $true
127+
if ($Location.displayName -in $CheckExisting.displayName) {
128128
[pscustomobject]@{
129-
id = ($CheckExististing | Where-Object -Property displayName -EQ $Location.displayName).id
130-
name = ($CheckExististing | Where-Object -Property displayName -EQ $Location.displayName).displayName
129+
id = ($CheckExisting | Where-Object -Property displayName -EQ $Location.displayName).id
130+
name = ($CheckExisting | Where-Object -Property displayName -EQ $Location.displayName).displayName
131+
templateId = $location.id
131132
}
132133
Write-LogMessage -Headers $User -API $APINAME -message "Matched a CA policy with the existing Named Location: $($location.displayName)" -Sev 'Info'
133134

134135
} else {
135136
if ($location.countriesAndRegions) { $location.countriesAndRegions = @($location.countriesAndRegions) }
137+
$location | Select-Object * -ExcludeProperty id
138+
Remove-ODataProperties -Object $location
136139
$Body = ConvertTo-Json -InputObject $Location
137140
$GraphRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -body $body -Type POST -tenantid $tenantfilter -asApp $true
138141
$retryCount = 0
@@ -151,19 +154,21 @@ function New-CIPPCAPolicy {
151154
}
152155
}
153156
}
157+
Write-Information 'Location Lookup Table:'
158+
Write-Information ($LocationLookupTable | ConvertTo-Json -Depth 10)
154159

155160
foreach ($location in $JSONobj.conditions.locations.includeLocations) {
156-
Write-Information "Replacing named location - $location"
157-
$lookup = $LocationLookupTable | Where-Object -Property name -EQ $location
158-
Write-Information "Found $lookup"
161+
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location }
159162
if (!$lookup) { continue }
163+
Write-Information "Replacing named location - $location"
160164
$index = [array]::IndexOf($JSONobj.conditions.locations.includeLocations, $location)
161165
$JSONobj.conditions.locations.includeLocations[$index] = $lookup.id
162166
}
163167

164168
foreach ($location in $JSONobj.conditions.locations.excludeLocations) {
165-
$lookup = $LocationLookupTable | Where-Object -Property name -EQ $location
169+
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location }
166170
if (!$lookup) { continue }
171+
Write-Information "Replacing named location - $location"
167172
$index = [array]::IndexOf($JSONobj.conditions.locations.excludeLocations, $location)
168173
$JSONobj.conditions.locations.excludeLocations[$index] = $lookup.id
169174
}

Modules/CIPPCore/Public/New-CIPPTemplateRun.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,30 @@ function New-CIPPTemplateRun {
3939
if ($MigrationTable) {
4040
$MigrationTable = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $MigrationTable.path).content | ConvertFrom-Json
4141
}
42+
$NamedLocations = $Files | Where-Object { $_.name -match 'ALLOWED COUNTRIES' }
43+
$LocationData = foreach ($Location in $NamedLocations) {
44+
(Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $Location.path).content | ConvertFrom-Json
45+
}
46+
4247
foreach ($File in $Files) {
43-
if ($File.name -eq 'MigrationTable' -or $file.name -eq 'ALLOWED COUNTRIES') { continue }
44-
$ExistingTemplate = $ExistingTemplates | Where-Object { (![string]::IsNullOrEmpty($_.displayName) -and (Get-SanitizedFilename -filename $_.displayName) -eq $File.name) -or (![string]::IsNullOrEmpty($_.templateName) -and (Get-SanitizedFilename -filename $_.templateName) -eq $File.name ) -and ![string]::IsNullOrEmpty($_.SHA) } | Select-Object -First 1
48+
if ($File.name -eq 'MigrationTable' -or $file.name -match 'ALLOWED COUNTRIES') { continue }
49+
Write-Information "Processing template file $($File.name) - Sanitized as $(Get-SanitizedFilename -filename $File.name)"
50+
$ExistingTemplate = $ExistingTemplates | Where-Object { (![string]::IsNullOrEmpty($_.displayName) -and (Get-SanitizedFilename -filename $_.displayName) -eq (Get-SanitizedFilename -filename $File.name)) -or (![string]::IsNullOrEmpty($_.templateName) -and (Get-SanitizedFilename -filename $_.templateName) -eq (Get-SanitizedFilename -filename $File.name) ) -and ![string]::IsNullOrEmpty($_.SHA) } | Select-Object -First 1
4551

4652
$UpdateNeeded = $false
4753
if ($ExistingTemplate -and $ExistingTemplate.SHA -ne $File.sha) {
4854
$Name = $ExistingTemplate.displayName ?? $ExistingTemplate.templateName
4955
Write-Information "Existing template $($Name) found, but SHA is different. Updating template."
5056
$UpdateNeeded = $true
5157
"Template $($Name) needs to be updated as the SHA is different"
52-
} else {
58+
} elseif ($ExistingTemplate -and $ExistingTemplate.SHA -eq $File.sha) {
5359
Write-Information "Existing template $($File.name) found, but SHA is the same. No update needed."
5460
"Template $($File.name) found, but SHA is the same. No update needed."
5561
}
5662

5763
if (!$ExistingTemplate -or $UpdateNeeded) {
5864
$Template = (Get-GitHubFileContents -FullName $TemplateSettings.templateRepo.value -Branch $TemplateSettings.templateRepoBranch.value -Path $File.path).content | ConvertFrom-Json
59-
Import-CommunityTemplate -Template $Template -SHA $File.sha -MigrationTable $MigrationTable
65+
Import-CommunityTemplate -Template $Template -SHA $File.sha -MigrationTable $MigrationTable -LocationData $LocationData
6066
if ($UpdateNeeded) {
6167
Write-Information "Template $($File.name) needs to be updated as the SHA is different"
6268
"Template $($File.name) updated"
@@ -69,6 +75,7 @@ function New-CIPPTemplateRun {
6975
} catch {
7076
$Message = "Failed to get data from community repo $($TemplateSettings.templateRepo.value). Error: $($_.Exception.Message)"
7177
Write-LogMessage -API 'Community Repo' -tenant $TenantFilter -message $Message -sev Error
78+
Write-Information $_.InvocationInfo.PositionMessage
7279
return "Failed to get data from community repo $($TemplateSettings.templateRepo.value). Error: $($_.Exception.Message)"
7380
}
7481
} else {

Modules/CIPPCore/Public/Tools/Import-CommunityTemplate.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function Import-CommunityTemplate {
88
$Template,
99
$SHA,
1010
$MigrationTable,
11+
$LocationData,
1112
[switch]$Force
1213
)
1314

@@ -104,13 +105,29 @@ function Import-CommunityTemplate {
104105
$id = $Template.id
105106
$Template = $Template | Select-Object * -ExcludeProperty lastModifiedDateTime, 'assignments', '#microsoft*', '*@odata.navigationLink', '*@odata.associationLink', '*@odata.context', 'ScopeTagIds', 'supportsScopeTags', 'createdDateTime', '@odata.id', '@odata.editLink', '*odata.type', '[email protected]', createdDateTime, '[email protected]'
106107
Remove-ODataProperties -Object $Template
108+
109+
$LocationInfo = [system.collections.generic.list[object]]::new()
110+
if ($LocationData) {
111+
$LocationData | ForEach-Object {
112+
if ($Template.conditions.locations.includeLocations -contains $_.id -or $Template.conditions.locations.excludeLocations -contains $_.id) {
113+
Write-Information "Adding location info for location ID $($_.id)"
114+
$LocationInfo.Add($_)
115+
}
116+
}
117+
if ($LocationInfo.Count -gt 0) {
118+
$Template | Add-Member -MemberType NoteProperty -Name LocationInfo -Value $LocationInfo -Force
119+
}
120+
}
121+
107122
$RawJson = ConvertTo-Json -InputObject $Template -Depth 100 -Compress
108123
#Replace the ids with the displayname by using the migration table, this is a simple find and replace each instance in the JSON.
109124
$MigrationTable.objects | ForEach-Object {
110125
if ($RawJson -match $_.ID) {
111126
$RawJson = $RawJson.Replace($_.ID, $($_.DisplayName))
112127
}
113128
}
129+
130+
114131
$entity = @{
115132
JSON = "$RawJson"
116133
PartitionKey = 'CATemplate'

Modules/CippExtensions/Public/GitHub/Invoke-GitHubApiRequest.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Invoke-GitHubApiRequest {
1313

1414
$Table = Get-CIPPTable -TableName Extensionsconfig
1515
$ExtensionConfig = (Get-CIPPAzDataTableEntity @Table).config
16-
if (Test-Json -Json $ExtensionConfig) {
16+
if ($ExtensionConfig -and (Test-Json -Json $ExtensionConfig)) {
1717
$Configuration = ($ExtensionConfig | ConvertFrom-Json).GitHub
1818
} else {
1919
$Configuration = @{ Enabled = $false }
@@ -64,6 +64,8 @@ function Invoke-GitHubApiRequest {
6464
Body = $Body
6565
Accept = $Accept
6666
}
67-
(Invoke-RestMethod -Uri 'https://cippy.azurewebsites.net/api/ExecGitHubAction' -Method POST -Body ($Action | ConvertTo-Json -Depth 10) -ContentType 'application/json').Results
67+
$Body = $Action | ConvertTo-Json -Depth 10
68+
69+
(Invoke-RestMethod -Uri 'https://cippy.azurewebsites.net/api/ExecGitHubAction' -Method POST -Body $Body -ContentType 'application/json').Results
6870
}
6971
}

0 commit comments

Comments
 (0)