Skip to content

Refactor logging, state handling, and input validation in CIPP standards #1310

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Invoke-CIPPStandardActivityBasedTimeout {
"CIS"
"spo_idle_session_timeout"
ADDEDCOMPONENT
{"type":"select","multiple":false,"label":"Select value","name":"standards.ActivityBasedTimeout.timeout","options":[{"label":"1 Hour","value":"01:00:00"},{"label":"3 Hours","value":"03:00:00"},{"label":"6 Hours","value":"06:00:00"},{"label":"12 Hours","value":"12:00:00"},{"label":"24 Hours","value":"1.00:00:00"}]}
{"type":"autoComplete","multiple":false,"creatable":false,"label":"Select value","name":"standards.ActivityBasedTimeout.timeout","options":[{"label":"1 Hour","value":"01:00:00"},{"label":"3 Hours","value":"03:00:00"},{"label":"6 Hours","value":"06:00:00"},{"label":"12 Hours","value":"12:00:00"},{"label":"24 Hours","value":"1.00:00:00"}]}
IMPACT
Medium Impact
POWERSHELLEQUIVALENT
Expand All @@ -33,28 +33,31 @@ function Invoke-CIPPStandardActivityBasedTimeout {
param($Tenant, $Settings)
#$Rerun -Type Standard -Tenant $Tenant -API 'ActivityBasedTimeout' -Settings $Settings

# Get timeout value using null-coalescing operator
$timeout = $Settings.timeout.value ?? $Settings.timeout

# Input validation
if ([string]::IsNullOrWhiteSpace($Settings.timeout) -or $Settings.timeout -eq 'Select a value' ) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'ActivityBasedTimeout: Invalid timeout parameter set' -sev Error
if ([string]::IsNullOrWhiteSpace($timeout) -or $timeout -eq 'Select a value' ) {
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'ActivityBasedTimeout: Invalid timeout parameter set' -sev Error
Return
}

# Backwards compatibility for v5.7.0 and older
if ($null -eq $Settings.timeout ) { $Settings.timeout = '01:00:00' }
if ($null -eq $timeout ) { $timeout = '01:00:00' }

$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $tenant
$StateIsCorrect = if ($CurrentState.definition -like "*$($Settings.timeout)*") { $true } else { $false }
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $Tenant
$StateIsCorrect = if ($CurrentState.definition -like "*$timeout*") { $true } else { $false }

If ($Settings.remediate -eq $true) {
try {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is already enabled and set to $($Settings.timeout)" -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Activity Based Timeout is already enabled and set to $timeout" -sev Info
} else {
$PolicyTemplate = @{
displayName = 'DefaultTimeoutPolicy'
isOrganizationDefault = $true
definition = @(
"{`"ActivityBasedTimeoutPolicy`":{`"Version`":1,`"ApplicationPolicies`":[{`"ApplicationId`":`"default`",`"WebSessionIdleTimeout`":`"$($Settings.timeout)`"}]}}"
"{`"ActivityBasedTimeoutPolicy`":{`"Version`":1,`"ApplicationPolicies`":[{`"ApplicationId`":`"default`",`"WebSessionIdleTimeout`":`"$timeout`"}]}}"
)
}
$body = ConvertTo-Json -InputObject $PolicyTemplate -Depth 10 -Compress
Expand All @@ -67,26 +70,26 @@ function Invoke-CIPPStandardActivityBasedTimeout {
$RequestType = 'PATCH'
$URI = "https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies/$($CurrentState.id)"
}
New-GraphPostRequest -tenantid $tenant -Uri $URI -Type $RequestType -Body $body -ContentType 'application/json'
Write-LogMessage -API 'Standards' -tenant $tenant -message "Enabled Activity Based Timeout with a value of $($Settings.timeout)" -sev Info
New-GraphPostRequest -tenantid $Tenant -Uri $URI -Type $RequestType -Body $body -ContentType 'application/json'
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Enabled Activity Based Timeout with a value of $timeout" -sev Info
}
} catch {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to enable Activity Based Timeout a value of $($Settings.timeout)." -sev Error -LogData $_
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to enable Activity Based Timeout a value of $timeout." -sev Error -LogData $_
}
}

if ($Settings.alert -eq $true) {

if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is enabled and set to $($Settings.timeout)" -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Activity Based Timeout is enabled and set to $timeout" -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is not set to $($Settings.timeout)" -sev Alert
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Activity Based Timeout is not set to $timeout" -sev Alert
}
}

if ($Settings.report -eq $true) {

Add-CIPPBPAField -FieldName 'ActivityBasedTimeout' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $tenant
Add-CIPPBPAField -FieldName 'ActivityBasedTimeout' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $Tenant
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Invoke-CIPPStandardAddDKIM {
New-DkimSigningConfig and Set-DkimSigningConfig
RECOMMENDEDBY
"CIS"
"CIPP"
UPDATECOMMENTBLOCK
Run the Tools\Update-StandardsComments.ps1 script to update this comment block
.LINK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Invoke-CIPPStandardAnonReportDisable {
POWERSHELLEQUIVALENT
Update-MgBetaAdminReportSetting -BodyParameter @{displayConcealedNames = \$true}
RECOMMENDEDBY
"CIPP"
UPDATECOMMENTBLOCK
Run the Tools\Update-StandardsComments.ps1 script to update this comment block
.LINK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Invoke-CIPPStandardAuditLog {
Enable-OrganizationCustomization
RECOMMENDEDBY
"CIS"
"CIPP"
UPDATECOMMENTBLOCK
Run the Tools\Update-StandardsComments.ps1 script to update this comment block
.LINK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ function Invoke-CIPPStandardAuthMethodsSettings {
.SYNOPSIS
(Label) Configure Authentication Methods Policy Settings
.DESCRIPTION
(Helptext) Configures the report suspicious activity settings and system credential preferences in the authentication methods policy
(DocsDescription) This standard allows you to configure the reportSuspiciousActivitySettings and systemCredentialPreferences properties within the authentication methods policy.
(Helptext) Configures the report suspicious activity settings and system credential preferences in the authentication methods policy.
(DocsDescription) Controls the authentication methods policy settings for reporting suspicious activity and system credential preferences. These settings help enhance the security of authentication in your organization.
.NOTES
CAT
Entra Standards
Entra (AAD) Standards
TAG
"lowimpact"
ADDEDCOMPONENT
{"type":"autoComplete","multiple":false,"name":"standards.AuthMethodsSettings.ReportSuspiciousActivity","label":"Report Suspicious Activity Settings","options":[{"label":"Default","value":"default"},{"label":"Enabled","value":"enabled"},{"label":"Disabled","value":"disabled"}]}
{"type":"autoComplete","multiple":false,"name":"standards.AuthMethodsSettings.SystemCredential","label":"System Credential Preferences","options":[{"label":"Default","value":"default"},{"label":"Enabled","value":"enabled"},{"label":"Disabled","value":"disabled"}]}
{"type":"autoComplete","multiple":false,"creatable":false,"required":false,"name":"standards.AuthMethodsSettings.ReportSuspiciousActivity","label":"Report Suspicious Activity Settings","options":[{"label":"Microsoft managed","value":"default"},{"label":"Enabled","value":"enabled"},{"label":"Disabled","value":"disabled"}]}
{"type":"autoComplete","multiple":false,"creatable":false,"required":false,"name":"standards.AuthMethodsSettings.SystemCredential","label":"System Credential Preferences","options":[{"label":"Microsoft managed","value":"default"},{"label":"Enabled","value":"enabled"},{"label":"Disabled","value":"disabled"}]}
IMPACT
Low Impact
POWERSHELLEQUIVALENT
Update-MgBetaPolicyAuthenticationMethodPolicy
RECOMMENDEDBY
UPDATECOMMENTBLOCK
Run the Tools\Update-StandardsComments.ps1 script to update this comment block
.LINK
https://docs.cipp.app/user-documentation/tenant/standards/list-standards/global-standards#low-impact
https://docs.cipp.app/user-documentation/tenant/standards/list-standards/entra-aad-standards#low-impact
#>

param($Tenant, $Settings)
Expand Down
27 changes: 15 additions & 12 deletions Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardBookings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Invoke-CIPPStandardBookings {
TAG
"mediumimpact"
ADDEDCOMPONENT
{"type":"select","multiple":false,"label":"Select value","name":"standards.Bookings.state","options":[{"label":"Enabled","value":"true"},{"label":"Disabled","value":"false"}]}
{"type":"autoComplete","multiple":false,"label":"Select value","name":"standards.Bookings.state","options":[{"label":"Enabled","value":"true"},{"label":"Disabled","value":"false"}]}
IMPACT
Medium Impact
POWERSHELLEQUIVALENT
Expand All @@ -30,42 +30,45 @@ function Invoke-CIPPStandardBookings {
param($Tenant, $Settings)
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'Bookings'

# Get state value using null-coalescing operator
$state = $Settings.state.value ?? $Settings.state

$CurrentState = (New-ExoRequest -tenantid $Tenant -cmdlet 'Get-OrganizationConfig').BookingsEnabled
$WantedState = if ($Settings.state -eq 'true') { $true } else { $false }
$WantedState = if ($state -eq 'true') { $true } else { $false }
$StateIsCorrect = if ($CurrentState -eq $WantedState) { $true } else { $false }

if ($Settings.report -eq $true) {
# Default is not set, not set means it's enabled
if ($null -eq $CurrentState ) { $CurrentState = $true }
Add-CIPPBPAField -FieldName 'BookingsState' -FieldValue $CurrentState -StoreAs bool -Tenant $tenant
Add-CIPPBPAField -FieldName 'BookingsState' -FieldValue $CurrentState -StoreAs bool -Tenant $Tenant
}

# Input validation
if (([string]::IsNullOrWhiteSpace($Settings.state) -or $Settings.state -eq 'Select a value') -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'BookingsEnabled: Invalid state parameter set' -sev Error
if (([string]::IsNullOrWhiteSpace($state) -or $state -eq 'Select a value') -and ($Settings.remediate -eq $true -or $Settings.alert -eq $true)) {
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'BookingsEnabled: Invalid state parameter set' -sev Error
Return
}
if ($Settings.remediate -eq $true) {
Write-Host 'Time to remediate'
if ($StateIsCorrect -eq $false) {
try {
New-ExoRequest -tenantid $Tenant -cmdlet 'Set-OrganizationConfig' -cmdParams @{ BookingsEnabled = $WantedState } -useSystemMailbox $true
Write-LogMessage -API 'Standards' -tenant $tenant -message "Successfully set the tenant Bookings state to $($Settings.state)" -sev Info
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-OrganizationConfig' -cmdParams @{ BookingsEnabled = $WantedState } -useSystemMailbox $true
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Successfully set the tenant Bookings state to $state" -sev Info
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to set the tenant Bookings state to $($Settings.state). Error: $ErrorMessage" -sev Error
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to set the tenant Bookings state to $state. Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
}
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "The tenant Bookings state is already set correctly to $($Settings.state)" -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message "The tenant Bookings state is already set correctly to $state" -sev Info
}

}

if ($Settings.alert -eq $true) {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "The tenant Bookings is set correctly to $($Settings.state)" -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message "The tenant Bookings is set correctly to $state" -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "The tenant Bookings is not set correctly to $($Settings.state)" -sev Alert
Write-LogMessage -API 'Standards' -tenant $Tenant -message "The tenant Bookings is not set correctly to $state" -sev Alert
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Invoke-CIPPStandardBranding {
{"type":"textField","name":"standards.Branding.signInPageText","label":"Sign-in page text","required":false}
{"type":"textField","name":"standards.Branding.usernameHintText","label":"Username hint Text","required":false}
{"type":"switch","name":"standards.Branding.hideAccountResetCredentials","label":"Hide self-service password reset"}
{"type":"select","multiple":false,"label":"Visual Template","name":"standards.Branding.layoutTemplateType","options":[{"label":"Full-screen background","value":"default"},{"label":"Partial-screen background","value":"verticalSplit"}]}
{"type":"autoComplete","multiple":false,"label":"Visual Template","name":"standards.Branding.layoutTemplateType","options":[{"label":"Full-screen background","value":"default"},{"label":"Partial-screen background","value":"verticalSplit"}]}
{"type":"switch","name":"standards.Branding.isHeaderShown","label":"Show header"}
{"type":"switch","name":"standards.Branding.isFooterShown","label":"Show footer"}
IMPACT
Expand All @@ -44,10 +44,13 @@ function Invoke-CIPPStandardBranding {
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Could not get the branding for $Tenant. This tenant might not have premium licenses available: $ErrorMessage" -Sev Error
}

# Get layoutTemplateType value using null-coalescing operator
$layoutTemplateType = $Settings.layoutTemplateType.value ?? $Settings.layoutTemplateType

$StateIsCorrect = ($CurrentState.signInPageText -eq $Settings.signInPageText) -and
($CurrentState.usernameHintText -eq $Settings.usernameHintText) -and
($CurrentState.loginPageTextVisibilitySettings.hideAccountResetCredentials -eq $Settings.hideAccountResetCredentials) -and
($CurrentState.loginPageLayoutConfiguration.layoutTemplateType -eq $Settings.layoutTemplateType) -and
($CurrentState.loginPageLayoutConfiguration.layoutTemplateType -eq $layoutTemplateType) -and
($CurrentState.loginPageLayoutConfiguration.isHeaderShown -eq $Settings.isHeaderShown) -and
($CurrentState.loginPageLayoutConfiguration.isFooterShown -eq $Settings.isFooterShown)

Expand All @@ -69,17 +72,17 @@ function Invoke-CIPPStandardBranding {
hideAccountResetCredentials = $Settings.hideAccountResetCredentials
}
loginPageLayoutConfiguration = [pscustomobject]@{
layoutTemplateType = $Settings.layoutTemplateType
layoutTemplateType = $layoutTemplateType
isHeaderShown = $Settings.isHeaderShown
isFooterShown = $Settings.isFooterShown
}
} | ConvertTo-Json -Compress
}
New-GraphPostRequest @GraphRequest
$null = New-GraphPostRequest @GraphRequest
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message 'Successfully updated branding.' -Sev Info
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Failed to update branding. Error: $($ErrorMessage)" -Sev Error
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -API 'Standards' -Tenant $Tenant -Message "Failed to update branding. Error: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage
}
}

Expand Down
Loading