Skip to content

Commit 720017c

Browse files
committed
fix: track last setting update for triggers
also add env var backup table
1 parent 95161f5 commit 720017c

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function Set-CIPPEnvVarBackup {
2+
param()
3+
4+
$FunctionAppName = $env:WEBSITE_SITE_NAME
5+
$PropertiesToBackup = @(
6+
'AzureWebJobsStorage'
7+
'WEBSITE_RUN_FROM_PACKAGE'
8+
'FUNCTIONS_EXTENSION_VERSION'
9+
'FUNCTIONS_WORKER_RUNTIME'
10+
'CIPP_HOSTED'
11+
'CIPP_HOSTED_KV_SUB'
12+
'WEBSITE_ENABLE_SYNC_UPDATE_SITE'
13+
'WEBSITE_AUTH_AAD_ALLOWED_TENANTS'
14+
)
15+
16+
$RequiredProperties = @('AzureWebJobsStorage', 'FUNCTIONS_EXTENSION_VERSION', 'FUNCTIONS_WORKER_RUNTIME', 'WEBSITE_RUN_FROM_PACKAGE')
17+
18+
if ($env:WEBSITE_SKU -eq 'FlexConsumption') {
19+
$RequiredProperties = $RequiredProperties | Where-Object { $_ -ne 'WEBSITE_RUN_FROM_PACKAGE' }
20+
}
21+
22+
$Backup = @{}
23+
foreach ($Property in $PropertiesToBackup) {
24+
$Backup[$Property] = [environment]::GetEnvironmentVariable($Property)
25+
}
26+
27+
$EnvBackupTable = Get-CIPPTable -tablename 'EnvVarBackups'
28+
$CurrentBackup = Get-CIPPAzDataTableEntity @EnvBackupTable -Filter "PartitionKey eq 'EnvVarBackup' and RowKey eq '$FunctionAppName'"
29+
30+
# ConvertFrom-Json returns PSCustomObject - convert to hashtable for consistent key/value access
31+
$CurrentValues = @{}
32+
if ($CurrentBackup -and $CurrentBackup.Values) {
33+
($CurrentBackup.Values | ConvertFrom-Json).PSObject.Properties | ForEach-Object {
34+
$CurrentValues[$_.Name] = $_.Value
35+
}
36+
}
37+
38+
$IsNew = $CurrentValues.Count -eq 0
39+
40+
if ($IsNew) {
41+
# First capture - write everything from the live environment
42+
$SavedValues = $Backup
43+
Write-Information "Creating new environment variable backup for $FunctionAppName"
44+
} else {
45+
# Backup already exists - keep existing values fixed, only backfill any properties not yet captured
46+
$SavedValues = $CurrentValues
47+
foreach ($Property in $PropertiesToBackup) {
48+
if (-not $SavedValues[$Property] -and $Backup[$Property]) {
49+
Write-Information "Backfilling missing backup property '$Property' from current environment."
50+
$SavedValues[$Property] = $Backup[$Property]
51+
}
52+
}
53+
Write-Information "Environment variable backup already exists for $FunctionAppName - preserving fixed values"
54+
}
55+
56+
# Validate all required properties are present in the final backup
57+
$MissingRequired = $RequiredProperties | Where-Object { -not $SavedValues[$_] }
58+
if ($MissingRequired) {
59+
Write-Warning "Environment variable backup for $FunctionAppName is missing required properties: $($MissingRequired -join ', ')"
60+
}
61+
62+
$Entity = @{
63+
PartitionKey = 'EnvVarBackup'
64+
RowKey = $FunctionAppName
65+
Values = [string]($SavedValues | ConvertTo-Json -Compress)
66+
}
67+
Add-CIPPAzDataTableEntity @EnvBackupTable -Entity $Entity -Force | Out-Null
68+
}

Modules/CIPPCore/Public/GraphHelper/Set-CIPPOffloadFunctionTriggers.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ function Set-CIPPOffloadFunctionTriggers {
2828
$OffloadEnabled = $false
2929
[bool]::TryParse($OffloadConfig.state, [ref]$OffloadEnabled) | Out-Null
3030

31+
# Trigger Last change table
32+
$TriggerChangeTable = Get-CippTable -tablename 'OffloadTriggerChange'
33+
$LastChange = Get-CIPPAzDataTableEntity @TriggerChangeTable
34+
35+
if ($LastChange -and $LastChange.Timestamp -gt (Get-Date).AddMinutes(-30).ToUniversalTime() -and $LastChange.Offloading -eq $OffloadEnabled) {
36+
Write-Information "Last trigger change was at $LastChange, skipping update to avoid rapid changes."
37+
return $true
38+
}
39+
3140
# Determine resource group
3241
if ($env:WEBSITE_RESOURCE_GROUP) {
3342
$ResourceGroupName = $env:WEBSITE_RESOURCE_GROUP
@@ -70,6 +79,12 @@ function Set-CIPPOffloadFunctionTriggers {
7079
# Update app settings only if there are changes to make
7180
if ($AppSettings.Count -gt 0) {
7281
if ($PSCmdlet.ShouldProcess($FunctionAppName, 'Disable non-HTTP triggers')) {
82+
$LastChange = @{
83+
PartitionKey = 'TriggerChange'
84+
RowKey = 'LastChange'
85+
Offloading = $OffloadEnabled
86+
}
87+
Add-CIPPAzDataTableEntity @TriggerChangeTable -Entity $LastChange -Force | Out-Null
7388
Update-CIPPAzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $ResourceGroupName -AppSetting $AppSettings | Out-Null
7489
Write-Information "Successfully disabled $($AppSettings.Count) non-HTTP trigger(s) on $FunctionAppName"
7590
}
@@ -95,6 +110,12 @@ function Set-CIPPOffloadFunctionTriggers {
95110
# Update app settings with removal of keys only if there are changes to make
96111
if ($RemoveKeys.Count -gt 0) {
97112
if ($PSCmdlet.ShouldProcess($FunctionAppName, 'Re-enable non-HTTP triggers')) {
113+
$LastChange = @{
114+
PartitionKey = 'TriggerChange'
115+
RowKey = 'LastChange'
116+
Offloading = $OffloadEnabled
117+
}
118+
Add-CIPPAzDataTableEntity @TriggerChangeTable -Entity $LastChange -Force | Out-Null
98119
Update-CIPPAzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $ResourceGroupName -AppSetting @{} -RemoveKeys $RemoveKeys | Out-Null
99120
Write-Information "Successfully re-enabled $($RemoveKeys.Count) non-HTTP trigger(s) on $FunctionAppName"
100121
}

profile.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ if (!$LastStartup -or $CurrentVersion -ne $LastStartup.Version) {
127127
$SwVersion.Stop()
128128
$Timings['VersionCheck'] = $SwVersion.Elapsed.TotalMilliseconds
129129

130+
Set-CIPPEnvVarBackup
130131
if ($env:AzureWebJobsStorage -ne 'UseDevelopmentStorage=true' -and $env:NonLocalHostAzurite -ne 'true') {
131132
Set-CIPPOffloadFunctionTriggers
132133
}

0 commit comments

Comments
 (0)