|
| 1 | +function Invoke-ExecBackupReplicationConfig { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + CIPP.AppSettings.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $Table = Get-CIPPTable -TableName Config |
| 12 | + $Scopes = @('Core', 'Tenant') |
| 13 | + |
| 14 | + # Returns whether a SAS URL secret currently exists for the given scope, without ever exposing it. |
| 15 | + function Get-ReplicationSecretIsSet { |
| 16 | + param([string]$Scope) |
| 17 | + try { |
| 18 | + if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true') { |
| 19 | + $DevSecretsTable = Get-CIPPTable -tablename 'DevSecrets' |
| 20 | + $Secret = (Get-CIPPAzDataTableEntity @DevSecretsTable -Filter "PartitionKey eq 'BackupReplication$Scope' and RowKey eq 'BackupReplication$Scope'").SASUrl |
| 21 | + if ([string]::IsNullOrWhiteSpace($Secret)) { |
| 22 | + return $null |
| 23 | + } |
| 24 | + return "SentToKeyVault" |
| 25 | + } |
| 26 | + else { |
| 27 | + $Secret = Get-CippKeyVaultSecret -Name "BackupReplication$Scope" -AsPlainText |
| 28 | + if ([string]::IsNullOrWhiteSpace($Secret)) { |
| 29 | + return $null |
| 30 | + } |
| 31 | + return "SentToKeyVault" |
| 32 | + } |
| 33 | + } catch { |
| 34 | + return $null |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + $results = try { |
| 39 | + if ($Request.Query.List) { |
| 40 | + $Output = @{} |
| 41 | + foreach ($Scope in $Scopes) { |
| 42 | + $Config = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'BackupReplication' and RowKey eq '$Scope'" |
| 43 | + $Output[$Scope] = @{ |
| 44 | + Enabled = [bool]($Config.Enabled) |
| 45 | + IsSet = Get-ReplicationSecretIsSet -Scope $Scope |
| 46 | + } |
| 47 | + } |
| 48 | + [pscustomobject]$Output |
| 49 | + } else { |
| 50 | + $BackupType = $Request.Body.BackupType |
| 51 | + if ($BackupType -notin $Scopes) { |
| 52 | + throw "BackupType must be one of: $($Scopes -join ', ')" |
| 53 | + } |
| 54 | + |
| 55 | + $SASUrl = $Request.Body.SASUrl |
| 56 | + $Enabled = if ($null -ne $Request.Body.Enabled) { [bool]$Request.Body.Enabled } else { $true } |
| 57 | + |
| 58 | + # Only update the stored secret when a real new value is supplied (the UI sends the |
| 59 | + # 'SentToKeyVault' sentinel when the existing, masked secret is left untouched). |
| 60 | + if (-not [string]::IsNullOrWhiteSpace($SASUrl) -and $SASUrl -ne 'SentToKeyVault') { |
| 61 | + $ParsedUri = $SASUrl -as [uri] |
| 62 | + if (-not $ParsedUri -or $ParsedUri.Query -notmatch 'sig=') { |
| 63 | + throw 'SAS URL must contain a SAS token (sig=...)' |
| 64 | + } |
| 65 | + |
| 66 | + # Confirm the SAS actually grants write+create by writing and removing a tiny probe blob. |
| 67 | + $guid = [guid]::NewGuid().ToString() |
| 68 | + $UrlParts = $SASUrl -split '\?', 2 |
| 69 | + $BaseUrl = $UrlParts[0].TrimEnd('/') |
| 70 | + $ProbeUrl = "$BaseUrl/.cipp-replication-test-$guid`?$($UrlParts[1])" |
| 71 | + try { |
| 72 | + $null = Invoke-CIPPRestMethod -Uri $ProbeUrl -Method 'PUT' -Body "cipp-replication-test-$guid" -ContentType 'text/plain' -Headers @{ 'x-ms-blob-type' = 'BlockBlob' } |
| 73 | + try { $null = Invoke-CIPPRestMethod -Uri $ProbeUrl -Method 'DELETE' -Headers @{} } catch { } |
| 74 | + } catch { |
| 75 | + $ProbeError = Get-CippException -Exception $_ |
| 76 | + throw "SAS URL validation failed (could not write to the container): $($ProbeError.NormalizedError)" |
| 77 | + } |
| 78 | + |
| 79 | + if ($env:AzureWebJobsStorage -eq 'UseDevelopmentStorage=true' -or $env:NonLocalHostAzurite -eq 'true') { |
| 80 | + $DevSecretsTable = Get-CIPPTable -tablename 'DevSecrets' |
| 81 | + $Secret = [PSCustomObject]@{ |
| 82 | + 'PartitionKey' = "BackupReplication$BackupType" |
| 83 | + 'RowKey' = "BackupReplication$BackupType" |
| 84 | + 'SASUrl' = $SASUrl |
| 85 | + } |
| 86 | + Add-CIPPAzDataTableEntity @DevSecretsTable -Entity $Secret -Force | Out-Null |
| 87 | + } |
| 88 | + else { |
| 89 | + Set-CippKeyVaultSecret -Name "BackupReplication$BackupType" -SecretValue (ConvertTo-SecureString -String $SASUrl -AsPlainText -Force) | Out-Null |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + $Config = @{ |
| 94 | + 'PartitionKey' = 'BackupReplication' |
| 95 | + 'RowKey' = $BackupType |
| 96 | + 'Enabled' = $Enabled |
| 97 | + } |
| 98 | + Add-CIPPAzDataTableEntity @Table -Entity $Config -Force | Out-Null |
| 99 | + |
| 100 | + Write-LogMessage -headers $Request.Headers -API $Request.Params.CIPPEndpoint -message "Updated $BackupType backup replication settings (Enabled: $Enabled)" -Sev 'Info' |
| 101 | + "Successfully updated $BackupType backup replication settings" |
| 102 | + } |
| 103 | + } catch { |
| 104 | + $ErrorMessage = Get-CippException -Exception $_ |
| 105 | + Write-LogMessage -headers $Request.Headers -API $Request.Params.CIPPEndpoint -message "Failed to update backup replication configuration: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage |
| 106 | + "Failed to update configuration: $($ErrorMessage.NormalizedError)" |
| 107 | + } |
| 108 | + |
| 109 | + $body = [pscustomobject]@{'Results' = $Results } |
| 110 | + |
| 111 | + return ([HttpResponseContext]@{ |
| 112 | + StatusCode = [HttpStatusCode]::OK |
| 113 | + Body = $body |
| 114 | + }) |
| 115 | +} |
0 commit comments