From 4f581b630c9f3396db5f6270a8f733b0e34baa97 Mon Sep 17 00:00:00 2001 From: Florian Carrier Date: Mon, 13 Dec 2021 18:00:19 +0000 Subject: [PATCH] Fix WhatIf gaps Ensure that WhatIf covers all processing steps --- powershell/Invoke-RestoreAlteryx.ps1 | 180 ++++++++++++++------------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/powershell/Invoke-RestoreAlteryx.ps1 b/powershell/Invoke-RestoreAlteryx.ps1 index a7a1094..aeef961 100644 --- a/powershell/Invoke-RestoreAlteryx.ps1 +++ b/powershell/Invoke-RestoreAlteryx.ps1 @@ -138,90 +138,94 @@ function Invoke-RestoreAlteryx { # ---------------------------------------------------------------------------- # Update configuration Write-Log -Type "INFO" -Message "Updating configuration" - $RunTimeSettingsXML = New-Object -TypeName "System.XML.XMLDocument" - $RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings) - # Gallery URL - $BaseAddress = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/BaseAddress" - if ($BaseAddress.InnerText -match "https?://.+?/gallery") { - # Assume this is a hostname - $BaseAddressMatch = Select-String -InputObject $BaseAddress.InnerText -Pattern "https?://(.+?)/gallery" - $SourceHostname = $BaseAddressMatch.Matches.Groups[1].Value - $NewHostname = $env:ComputerName - if ($SourceHostname -ne $NewHostname) { - Write-Log -Type "INFO" -Message "Updating hostname" - # Update base Gallery URL - Write-Log -Type "DEBUG" -Message "Source=$SourceHostname" - Write-Log -Type "DEBUG" -Message "Destination=$NewHostname" - $NewBaseAddress = $BaseAddress.InnerText.Replace($SourceHostname, $NewHostname) - Write-Log -Type "DEBUG" -Message $NewBaseAddress - $BaseAddress.InnerText = $NewBaseAddress - # Update authentication URL - $ServiceProviderEntityID = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Authentication/ServiceProviderEntityID" - $ServiceProviderEntityID.InnerText = $ServiceProviderEntityID.InnerText.Replace($SourceHostname, $NewHostname) - } - } - # SSL - if ($Properties.EnableSSL -eq $true) { - $Protocol = "https" - $Action = "Enabling" - } else { - $Protocol = "http" - $Action = "Disabling" - } - # ! Node name is case-sensitive: SslEnabled - $SSLEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/SslEnabled" - if ($null -ne $SSLEnabled) { - if ($SSLEnabled.InnerText -ne $Properties.EnableSSL.ToString()) { - Write-Log -Type "INFO" -Message ($Action + " SSL") - $SSLEnabled.InnerText = $Properties.EnableSSL.ToString() + if ($PSCmdlet.ShouldProcess("RunTimeSetting.xml", "Update")) { + $RunTimeSettingsXML = New-Object -TypeName "System.XML.XMLDocument" + $RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings) + # Gallery URL + $BaseAddress = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/BaseAddress" + if ($BaseAddress.InnerText -match "https?://.+?/gallery") { + # Assume this is a hostname + $BaseAddressMatch = Select-String -InputObject $BaseAddress.InnerText -Pattern "https?://(.+?)/gallery" + $SourceHostname = $BaseAddressMatch.Matches.Groups[1].Value + $NewHostname = $env:ComputerName + if ($SourceHostname -ne $NewHostname) { + Write-Log -Type "INFO" -Message "Updating hostname" + # Update base Gallery URL + Write-Log -Type "DEBUG" -Message "Source=$SourceHostname" + Write-Log -Type "DEBUG" -Message "Destination=$NewHostname" + $NewBaseAddress = $BaseAddress.InnerText.Replace($SourceHostname, $NewHostname) + Write-Log -Type "DEBUG" -Message $NewBaseAddress + $BaseAddress.InnerText = $NewBaseAddress + # Update authentication URL + $ServiceProviderEntityID = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Authentication/ServiceProviderEntityID" + $ServiceProviderEntityID.InnerText = $ServiceProviderEntityID.InnerText.Replace($SourceHostname, $NewHostname) + } } - } else { - # If SSL configuration is not explicitly defined + # SSL if ($Properties.EnableSSL -eq $true) { - # Create SSL node - $Gallery = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery" - $SSLEnabled = $RunTimeSettingsXML.CreateElement("SslEnabled") - $SSLEnabled.InnerText = $Properties.EnableSSL.ToString() - [Void]$Gallery.AppendChild($SSLEnabled) + $Protocol = "https" + $Action = "Enabling" + } else { + $Protocol = "http" + $Action = "Disabling" } - } - if ($BaseAddress.InnerText -notmatch "^${Protocol}://") { - Write-Log -Type "INFO" -Message ($Action + " HTTPS") - $SSLAddress = $BaseAddress.InnerText -replace "^https?", "$Protocol" - Write-Log -Type "DEBUG" -Message $SSLAddress - $BaseAddress.InnerText = $SSLAddress - } - # Update configuration file - $RunTimeSettingsXML.Save($ConfigurationFiles.RunTimeSettings) - # Installation path - $WorkingPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Environment/WorkingPath" - if ($WorkingPath.InnerText -ne $Properties.InstallationPath) { - Write-Log -Type "INFO" -Message "Updating installation paths" - Write-Log -Type "DEBUG" -Message "Source=$($WorkingPath.InnerText)" - Write-Log -Type "DEBUG" -Message "Destination=$($Properties.InstallationPath)" - $RunTimeSettings = Get-Content -Path $ConfigurationFiles.RunTimeSettings - $UpdatedSettings = New-Object -TypeName "System.Collections.Arraylist" - foreach ($Property in $RunTimeSettings) { - [Void]$UpdatedSettings.Add($Property.Replace($WorkingPath.InnerText, $Properties.InstallationPath)) + # ! Node name is case-sensitive: SslEnabled + $SSLEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/SslEnabled" + if ($null -ne $SSLEnabled) { + if ($SSLEnabled.InnerText -ne $Properties.EnableSSL.ToString()) { + Write-Log -Type "INFO" -Message ($Action + " SSL") + $SSLEnabled.InnerText = $Properties.EnableSSL.ToString() + } + } else { + # If SSL configuration is not explicitly defined + if ($Properties.EnableSSL -eq $true) { + # Create SSL node + $Gallery = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery" + $SSLEnabled = $RunTimeSettingsXML.CreateElement("SslEnabled") + $SSLEnabled.InnerText = $Properties.EnableSSL.ToString() + [Void]$Gallery.AppendChild($SSLEnabled) + } + } + if ($BaseAddress.InnerText -notmatch "^${Protocol}://") { + Write-Log -Type "INFO" -Message ($Action + " HTTPS") + $SSLAddress = $BaseAddress.InnerText -replace "^https?", "$Protocol" + Write-Log -Type "DEBUG" -Message $SSLAddress + $BaseAddress.InnerText = $SSLAddress + } + # Update configuration file + $RunTimeSettingsXML.Save($ConfigurationFiles.RunTimeSettings) + # Installation path + $WorkingPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Environment/WorkingPath" + if ($WorkingPath.InnerText -ne $Properties.InstallationPath) { + Write-Log -Type "INFO" -Message "Updating installation paths" + Write-Log -Type "DEBUG" -Message "Source=$($WorkingPath.InnerText)" + Write-Log -Type "DEBUG" -Message "Destination=$($Properties.InstallationPath)" + $RunTimeSettings = Get-Content -Path $ConfigurationFiles.RunTimeSettings + $UpdatedSettings = New-Object -TypeName "System.Collections.Arraylist" + foreach ($Property in $RunTimeSettings) { + [Void]$UpdatedSettings.Add($Property.Replace($WorkingPath.InnerText, $Properties.InstallationPath)) + } + # Overwrite source configuration + Set-Content -Path $ConfigurationFiles.RunTimeSettings -Value $UpdatedSettings + # Reload XML settings + $RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings) } - # Overwrite source configuration - Set-Content -Path $ConfigurationFiles.RunTimeSettings -Value $UpdatedSettings - # Reload XML settings - $RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings) } # ---------------------------------------------------------------------------- # (Re)Set controller token if ($Restore.Token -eq $true) { Write-Log -Type "INFO" -Message "Remove encrypted controller token" - $ControllerSettingsXML = New-Object -TypeName "System.XML.XMLDocument" - $ControllerSettingsXML.Load($ConfigurationFiles.RunTimeSettings) - $ServerSecretEncrypted = Select-XMLNode -XML $ControllerSettingsXML -XPath "SystemSettings/Controller/ServerSecretEncrypted" - if ($null -ne $ServerSecretEncrypted) { - Write-Log -Type "DEBUG" -Message $ServerSecretEncrypted.InnerText - [Void]$ServerSecretEncrypted.ParentNode.RemoveChild($ServerSecretEncrypted) - $ControllerSettingsXML.Save($ConfigurationFiles.RunTimeSettings) - } else { - Write-Log -Type "WARN" -Message "Encrypted controller token could not be found" + if ($PSCmdlet.ShouldProcess("Encrypted controller token", "Remove")) { + $ControllerSettingsXML = New-Object -TypeName "System.XML.XMLDocument" + $ControllerSettingsXML.Load($ConfigurationFiles.RunTimeSettings) + $ServerSecretEncrypted = Select-XMLNode -XML $ControllerSettingsXML -XPath "SystemSettings/Controller/ServerSecretEncrypted" + if ($null -ne $ServerSecretEncrypted) { + Write-Log -Type "DEBUG" -Message $ServerSecretEncrypted.InnerText + [Void]$ServerSecretEncrypted.ParentNode.RemoveChild($ServerSecretEncrypted) + $ControllerSettingsXML.Save($ConfigurationFiles.RunTimeSettings) + } else { + Write-Log -Type "WARN" -Message "Encrypted controller token could not be found" + } } Write-Log -Type "INFO" -Message "Restore controller token" if ($PSCmdlet.ShouldProcess("Controller token", "Restore")) { @@ -242,16 +246,20 @@ function Invoke-RestoreAlteryx { } } # ---------------------------------------------------------------------------- - # Set Run-As user + # Set Run-as user if ($Restore.RunAsUser -eq $true) { - # TODO - Write-Log -Type "WARN" -Message "Run As User restore is not currently supported" + if ($PSCmdlet.ShouldProcess("Run-as user credentials", "Restore")) { + # TODO + Write-Log -Type "WARN" -Message "Run-as user credentials restore is not currently supported" + } } # ---------------------------------------------------------------------------- # Set SMTP password if ($Restore.SMTPPassword -eq $true) { - # TODO - Write-Log -Type "WARN" -Message "SMTP password restore is not currently supported" + if ($PSCmdlet.ShouldProcess("SMTP password", "Restore")) { + # TODO + Write-Log -Type "WARN" -Message "SMTP password restore is not currently supported" + } } # ---------------------------------------------------------------------------- # Reset storage key @@ -281,9 +289,9 @@ function Invoke-RestoreAlteryx { # Restore database if ($Restore.Database -eq $true) { Write-Log -Type "INFO" -Message "Restore MongoDB database from backup" - $EmbeddedMongoDBEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBEnabled" - if ($EmbeddedMongoDBEnabled.InnerText -eq $true) { - if ($PSCmdlet.ShouldProcess("MongoDB", "Restore")) { + if ($PSCmdlet.ShouldProcess("MongoDB", "Restore")) { + $EmbeddedMongoDBEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBEnabled" + if ($EmbeddedMongoDBEnabled.InnerText -eq $true) { $EmbeddedMongoDBRootPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBRootPath" if ($null -ne $EmbeddedMongoDBRootPath) { $TargetPath = $EmbeddedMongoDBRootPath.InnerText @@ -299,10 +307,10 @@ function Invoke-RestoreAlteryx { } else { Write-Log -Type "DEBUG" -Message $DatabaseRestore } + } else { + Write-Log -Type "ERROR" -Message "User-managed MongoDB is not supported" + Write-Log -Type "WARN" -Message "Skipping database restore" } - } else { - Write-Log -Type "ERROR" -Message "User-managed MongoDB is not supported" - Write-Log -Type "WARN" -Message "Skipping database restore" } } # ----------------------------------------------------------------------------