Skip to content

Commit bc80e6c

Browse files
authored
Merge pull request #390 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 6fcab6f + 53f6dfe commit bc80e6c

File tree

13 files changed

+245
-145
lines changed

13 files changed

+245
-145
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
function Push-CippDriftManagement {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
param (
7+
$Item
8+
)
9+
10+
Write-Information "Received queue item for $($Item.Tenant)"
11+
12+
try {
13+
$Drift = Get-CIPPDrift -TenantFilter $item.tenant
14+
if ($Drift.newDeviationsCount -gt 0) {
15+
$email = (Get-CIPPTenantAlignment -TenantFilter $Item.Tenant | Where-Object -Property standardType -EQ 'drift').standardSettings.email
16+
$webhook = (Get-CIPPTenantAlignment -TenantFilter $Item.Tenant | Where-Object -Property standardType -EQ 'drift').standardSettings.webhook
17+
$CippConfigTable = Get-CippTable -tablename Config
18+
$CippConfig = Get-CIPPAzDataTableEntity @CippConfigTable -Filter "PartitionKey eq 'InstanceProperties' and RowKey eq 'CIPPURL'"
19+
$CIPPURL = 'https://{0}' -f $CippConfig.Value
20+
$Data = $Drift.currentDeviations | ForEach-Object {
21+
$currentValue = if ($_.receivedValue -and $_.receivedValue.Length -gt 200) {
22+
$_.receivedValue.Substring(0, 200) + '...'
23+
} else {
24+
$_.receivedValue
25+
}
26+
[PSCustomObject]@{
27+
Standard = $_.standardDisplayName ? $_.standardDisplayName : $_.standardName
28+
'Expected Value' = $_.expectedValue
29+
'Current Value' = $currentValue
30+
Status = $_.status
31+
}
32+
}
33+
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $item.tenant -InputObject 'driftStandard'
34+
$CIPPAlert = @{
35+
Type = 'email'
36+
Title = $GenerateEmail.title
37+
HTMLContent = $GenerateEmail.htmlcontent
38+
TenantFilter = $item.Tenant
39+
}
40+
Write-Host 'Going to send the mail'
41+
Send-CIPPAlert @CIPPAlert -altEmail $email
42+
$WebhookData = @{
43+
Title = $GenerateEmail.title
44+
ActionUrl = $GenerateEmail.ButtonUrl
45+
ActionText = $GenerateEmail.ButtonText
46+
AlertData = $Data
47+
Tenant = $Item.Tenant
48+
} | ConvertTo-Json -Depth 15 -Compress
49+
$CippAlert = @{
50+
Type = 'webhook'
51+
Title = $GenerateEmail.title
52+
JSONContent = $WebhookData
53+
TenantFilter = $item.tenant
54+
}
55+
Write-Host 'Sending Webhook Content'
56+
Send-CIPPAlert @CippAlert -altWebhook $webhook
57+
#Always do PSA.
58+
$CIPPAlert = @{
59+
Type = 'psa'
60+
Title = $GenerateEmail.title
61+
HTMLContent = $GenerateEmail.htmlcontent
62+
TenantFilter = $TenantFilter
63+
}
64+
Send-CIPPAlert @CIPPAlert
65+
return $true
66+
} else {
67+
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "No new drift deviations found for tenant $($Item.Tenant)" -sev Info
68+
return $true
69+
}
70+
Write-Information "Drift management completed for tenant $($Item.Tenant)"
71+
} catch {
72+
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "Error running Drift Check for tenant $($Item.Tenant) - $($_.Exception.Message)" -sev Error -LogData (Get-CippException -Exception $_)
73+
Write-Warning "Error running standard $($Item.Standard) for tenant $($Item.Tenant) - $($_.Exception.Message)"
74+
Write-Information $_.InvocationInfo.PositionMessage
75+
throw $_.Exception.Message
76+
}
77+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecRestoreBackup.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecRestoreBackup {
3+
function Invoke-ExecRestoreBackup {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -27,7 +27,7 @@ Function Invoke-ExecRestoreBackup {
2727
$Table.Entity = $ht2
2828
Add-CIPPAzDataTableEntity @Table -Force
2929
}
30-
Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Created backup' -Sev 'Debug'
30+
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Restored backup $($Request.Body.BackupName)" -Sev 'Info'
3131
$body = [pscustomobject]@{
3232
'Results' = 'Successfully restored backup.'
3333
}
@@ -44,7 +44,7 @@ Function Invoke-ExecRestoreBackup {
4444
$Table.Entity = $ht2
4545
Add-AzDataTableEntity @Table -Force
4646
}
47-
Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Created backup' -Sev 'Debug'
47+
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Restored backup $($Request.Body.BackupName)" -Sev 'Info'
4848

4949
$body = [pscustomobject]@{
5050
'Results' = 'Successfully restored backup.'

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-RemovePolicy.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-RemovePolicy {
3+
function Invoke-RemovePolicy {
44
<#
55
.FUNCTIONALITY
66
Entrypoint

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ExecUpdateDriftDeviation.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ function Invoke-ExecUpdateDriftDeviation {
3030
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Removed all drift customizations for tenant $TenantFilter" -Sev 'Info'
3131
} else {
3232
$Deviations = $Request.Body.deviations
33+
$Reason = $Request.Body.reason
3334
$Results = foreach ($Deviation in $Deviations) {
3435
try {
35-
$Result = Set-CIPPDriftDeviation -TenantFilter $TenantFilter -StandardName $Deviation.standardName -Status $Deviation.status
36+
$user = $request.headers.'x-ms-client-principal'
37+
$username = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails
38+
$Result = Set-CIPPDriftDeviation -TenantFilter $TenantFilter -StandardName $Deviation.standardName -Status $Deviation.status -Reason $Reason -user $username
3639
[PSCustomObject]@{
3740
success = $true
3841
result = $Result
3942
}
40-
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Updated drift deviation status for $($Deviation.standardName) to $($Deviation.status)" -Sev 'Info'
43+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Updated drift deviation status for $($Deviation.standardName) to $($Deviation.status) with reason: $Reason" -Sev 'Info'
4144
if ($Deviation.status -eq 'DeniedRemediate') {
4245
$Setting = $Deviation.standardName -replace 'standards.', ''
4346
$StandardTemplate = Get-CIPPTenantAlignment -TenantFilter $TenantFilter | Where-Object -Property standardType -EQ 'drift'

Modules/CIPPCore/Public/Entrypoints/Invoke-ListLogs.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function Invoke-ListLogs {
2828
$LogLevel = if ($Request.Query.Severity) { ($Request.query.Severity).split(',') } else { 'Info', 'Warn', 'Error', 'Critical', 'Alert' }
2929
$PartitionKey = $Request.Query.DateFilter
3030
$username = $Request.Query.User ?? '*'
31+
$TenantFilter = $Request.Query.Tenant
3132

3233
$StartDate = $Request.Query.StartDate ?? $Request.Query.DateFilter
3334
$EndDate = $Request.Query.EndDate ?? $Request.Query.DateFilter
@@ -48,12 +49,17 @@ function Invoke-ListLogs {
4849
$LogLevel = 'Info', 'Warn', 'Error', 'Critical', 'Alert'
4950
$PartitionKey = Get-Date -UFormat '%Y%m%d'
5051
$username = '*'
52+
$TenantFilter = $null
5153
$Filter = "PartitionKey eq '{0}'" -f $PartitionKey
5254
}
5355
$AllowedTenants = Test-CIPPAccess -Request $Request -TenantList
5456
Write-Host "Getting logs for filter: $Filter, LogLevel: $LogLevel, Username: $username"
5557

56-
$Rows = Get-AzDataTableEntity @Table -Filter $Filter | Where-Object { $_.Severity -in $LogLevel -and $_.Username -like $username }
58+
$Rows = Get-AzDataTableEntity @Table -Filter $Filter | Where-Object {
59+
$_.Severity -in $LogLevel -and
60+
$_.Username -like $username -and
61+
($TenantFilter -eq $null -or $TenantFilter -eq 'AllTenants' -or $_.Tenant -like "*$TenantFilter*" -or $_.TenantID -eq $TenantFilter)
62+
}
5763

5864
if ($AllowedTenants -notcontains 'AllTenants') {
5965
$TenantList = Get-Tenants -IncludeErrors | Where-Object { $_.customerId -in $AllowedTenants }

Modules/CIPPCore/Public/Functions/Get-CIPPTenantAlignment.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ function Get-CIPPTenantAlignment {
150150
}
151151
}
152152
}
153+
}
154+
# Handle Conditional Access templates specially
155+
elseif ($StandardKey -eq 'ConditionalAccessTemplate' -and $StandardConfig -is [array]) {
156+
foreach ($CATemplate in $StandardConfig) {
157+
if ($CATemplate.TemplateList.value) {
158+
$CAStandardId = "standards.ConditionalAccessTemplate.$($CATemplate.TemplateList.value)"
159+
$CAActions = if ($CATemplate.action) { $CATemplate.action } else { @() }
160+
$CAReportingEnabled = ($CAActions | Where-Object { $_.value -and ($_.value.ToLower() -eq 'report' -or $_.value.ToLower() -eq 'remediate') }).Count -gt 0
161+
162+
[PSCustomObject]@{
163+
StandardId = $CAStandardId
164+
ReportingEnabled = $CAReportingEnabled
165+
}
166+
}
167+
}
153168
} else {
154169
[PSCustomObject]@{
155170
StandardId = $StandardId

0 commit comments

Comments
 (0)