Skip to content

Commit 0bfa456

Browse files
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into dev
2 parents 91d3738 + b0c96a1 commit 0bfa456

File tree

4 files changed

+90
-47
lines changed

4 files changed

+90
-47
lines changed

Modules/CIPPCore/Public/Authentication/Get-CippApiAuth.ps1

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,31 @@ function Get-CippApiAuth {
44
[string]$FunctionAppName
55
)
66

7-
if ($env:MSI_SECRET) {
8-
Disable-AzContextAutosave -Scope Process | Out-Null
9-
$null = Connect-AzAccount -Identity
10-
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
11-
$Context = Set-AzContext -SubscriptionId $SubscriptionId
12-
} else {
13-
$Context = Get-AzContext
14-
$SubscriptionId = $Context.Subscription.Id
7+
if ($env:WEBSITE_AUTH_V2_CONFIG_JSON) {
8+
$AuthSettings = $env:WEBSITE_AUTH_V2_CONFIG_JSON | ConvertFrom-Json -ErrorAction SilentlyContinue
159
}
1610

17-
# Get auth settings
18-
$AuthSettings = Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2/list?api-version=2020-06-01" -ErrorAction Stop | Select-Object -ExpandProperty Content | ConvertFrom-Json
11+
if (-not $AuthSettings) {
12+
if ($env:MSI_SECRET) {
13+
Disable-AzContextAutosave -Scope Process | Out-Null
14+
$null = Connect-AzAccount -Identity
15+
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
16+
$Context = Set-AzContext -SubscriptionId $SubscriptionId
17+
} else {
18+
$Context = Get-AzContext
19+
$SubscriptionId = $Context.Subscription.Id
20+
}
21+
22+
# Get auth settings
23+
$AuthSettings = (Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2/list?api-version=2020-06-01" -ErrorAction Stop | Select-Object -ExpandProperty Content | ConvertFrom-Json).properties
24+
}
1925

20-
if ($AuthSettings.properties) {
26+
if ($AuthSettings) {
2127
[PSCustomObject]@{
2228
ApiUrl = "https://$($env:WEBSITE_HOSTNAME)"
23-
TenantID = $AuthSettings.properties.identityProviders.azureActiveDirectory.registration.openIdIssuer -replace 'https://sts.windows.net/', '' -replace '/v2.0', ''
24-
ClientIDs = $AuthSettings.properties.identityProviders.azureActiveDirectory.validation.defaultAuthorizationPolicy.allowedApplications
25-
Enabled = $AuthSettings.properties.identityProviders.azureActiveDirectory.enabled
29+
TenantID = $AuthSettings.identityProviders.azureActiveDirectory.registration.openIdIssuer -replace 'https://sts.windows.net/', '' -replace '/v2.0', ''
30+
ClientIDs = $AuthSettings.identityProviders.azureActiveDirectory.validation.defaultAuthorizationPolicy.allowedApplications
31+
Enabled = $AuthSettings.identityProviders.azureActiveDirectory.enabled
2632
}
2733
} else {
2834
throw 'No auth settings found'

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-AuditLogProcessingOrchestrator.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function Start-AuditLogProcessingOrchestrator {
2828
$ProcessBatch = foreach ($TenantGroup in $TenantGroups) {
2929
$TenantFilter = $TenantGroup.Name
3030
$RowIds = @($TenantGroup.Group.RowKey)
31-
for ($i = 0; $i -lt $RowIds.Count; $i += 1000) {
32-
Write-Host "Processing $TenantFilter with $($RowIds.Count) row IDs. We're processing id $($RowIds[$i]) to $($RowIds[[Math]::Min($i + 999, $RowIds.Count - 1)])"
33-
$BatchRowIds = $RowIds[$i..([Math]::Min($i + 999, $RowIds.Count - 1))]
31+
for ($i = 0; $i -lt $RowIds.Count; $i += 500) {
32+
Write-Host "Processing $TenantFilter with $($RowIds.Count) row IDs. We're processing id $($RowIds[$i]) to $($RowIds[[Math]::Min($i + 499, $RowIds.Count - 1)])"
33+
$BatchRowIds = $RowIds[$i..([Math]::Min($i + 499, $RowIds.Count - 1))]
3434
[PSCustomObject]@{
3535
TenantFilter = $TenantFilter
3636
RowIds = $BatchRowIds

Modules/CIPPCore/Public/Webhooks/Test-CIPPAuditLogRules.ps1

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -148,39 +148,76 @@ function Test-CIPPAuditLogRules {
148148
}
149149
}
150150

151-
# Collect bulk data for users/groups/devices/applications
152-
$Requests = @(
153-
@{
154-
id = 'users'
155-
url = '/users?$select=id,displayName,userPrincipalName,accountEnabled&$top=999'
156-
method = 'GET'
157-
}
158-
@{
159-
id = 'groups'
160-
url = '/groups?$select=id,displayName,mailEnabled,securityEnabled&$top=999'
161-
method = 'GET'
162-
}
163-
@{
164-
id = 'devices'
165-
url = '/devices?$select=id,displayName,deviceId&$top=999'
166-
method = 'GET'
167-
}
168-
@{
169-
id = 'servicePrincipals'
170-
url = '/servicePrincipals?$select=id,displayName&$top=999'
171-
method = 'GET'
172-
}
173-
)
174-
$Response = New-GraphBulkRequest -TenantId $TenantFilter -Requests $Requests
151+
$Table = Get-CIPPTable -tablename 'cacheauditloglookups'
152+
$1dayago = (Get-Date).AddDays(-1).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
153+
$Lookups = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$TenantFilter' and Timestamp gt datetime'$1dayago'"
154+
if (!$Lookups) {
155+
# Collect bulk data for users/groups/devices/applications
156+
$Requests = @(
157+
@{
158+
id = 'users'
159+
url = '/users?$select=id,displayName,userPrincipalName,accountEnabled&$top=999'
160+
method = 'GET'
161+
}
162+
@{
163+
id = 'groups'
164+
url = '/groups?$select=id,displayName,mailEnabled,securityEnabled&$top=999'
165+
method = 'GET'
166+
}
167+
@{
168+
id = 'devices'
169+
url = '/devices?$select=id,displayName,deviceId&$top=999'
170+
method = 'GET'
171+
}
172+
@{
173+
id = 'servicePrincipals'
174+
url = '/servicePrincipals?$select=id,displayName&$top=999'
175+
method = 'GET'
176+
}
177+
)
178+
$Response = New-GraphBulkRequest -TenantId $TenantFilter -Requests $Requests
179+
$Users = ($Response | Where-Object { $_.id -eq 'users' }).body.value
180+
$Groups = ($Response | Where-Object { $_.id -eq 'groups' }).body.value ?? @()
181+
$Devices = ($Response | Where-Object { $_.id -eq 'devices' }).body.value ?? @()
182+
$ServicePrincipals = ($Response | Where-Object { $_.id -eq 'servicePrincipals' }).body.value
183+
# Cache the lookups for 1 day
184+
$Entities = @(
185+
@{
186+
PartitionKey = $TenantFilter
187+
RowKey = 'users'
188+
Data = [string]($Users | ConvertTo-Json -Compress)
189+
}
190+
@{
191+
PartitionKey = $TenantFilter
192+
RowKey = 'groups'
193+
Data = [string]($Groups | ConvertTo-Json -Compress)
194+
}
195+
@{
196+
PartitionKey = $TenantFilter
197+
RowKey = 'devices'
198+
Data = [string]($Devices | ConvertTo-Json -Compress)
199+
}
200+
@{
201+
PartitionKey = $TenantFilter
202+
RowKey = 'servicePrincipals'
203+
Data = [string]($ServicePrincipals | ConvertTo-Json -Compress)
204+
}
205+
)
206+
# Save the cached lookups
207+
Add-CIPPAzDataTableEntity @Table -Entity $Entities -Force
208+
Write-Information "Cached directory lookups for tenant $TenantFilter"
209+
} else {
210+
# Use cached lookups
211+
$Users = ($Lookups | Where-Object { $_.RowKey -eq 'users' }).Data | ConvertFrom-Json
212+
$Groups = ($Lookups | Where-Object { $_.RowKey -eq 'groups' }).Data | ConvertFrom-Json
213+
$Devices = ($Lookups | Where-Object { $_.RowKey -eq 'devices' }).Data | ConvertFrom-Json
214+
$ServicePrincipals = ($Lookups | Where-Object { $_.RowKey -eq 'servicePrincipals' }).Data | ConvertFrom-Json
215+
Write-Information "Using cached directory lookups for tenant $TenantFilter"
216+
}
175217

176218
# partner users
177219
$PartnerUsers = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$select=id,displayName,userPrincipalName,accountEnabled&`$top=999" -AsApp $true -NoAuthCheck $true
178220

179-
$Users = ($Response | Where-Object { $_.id -eq 'users' }).body.value
180-
$Groups = ($Response | Where-Object { $_.id -eq 'groups' }).body.value ?? @()
181-
$Devices = ($Response | Where-Object { $_.id -eq 'devices' }).body.value ?? @()
182-
$ServicePrincipals = ($Response | Where-Object { $_.id -eq 'servicePrincipals' }).body.value
183-
184221
Write-Warning '## Audit Log Configuration ##'
185222
Write-Information ($Configuration | ConvertTo-Json -Depth 10)
186223

Modules/CippEntrypoints/CippEntrypoints.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function Receive-CippOrchestrationTrigger {
228228
if (($Output | Measure-Object).Count -gt 0) {
229229
Write-Information "Waiting for ($($Output.Count)) activity functions to complete..."
230230
foreach ($Task in $Output) {
231-
Write-Information ($Task | ConvertTo-Json -Depth 10 -Compress)
231+
#Write-Information ($Task | ConvertTo-Json -Depth 10 -Compress)
232232
try {
233233
$Results = Wait-ActivityFunction -Task $Task
234234
} catch {}

0 commit comments

Comments
 (0)