From 2d4861947bd390496f7949f9abf0d839ecace215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81oskot?= Date: Fri, 19 Jan 2024 23:00:49 +0100 Subject: [PATCH] fix(cli): session.Run is sensitive to quoting on Windows host There is a strange issue with SSH executor via session.Run on Windows host (i.e. Windows SSH client) which seems to be prone to failure depending on particular arrangements of quote, double-quotes, etc. in PowerShell command. For example, this fails output, err := r.run(`Enable-NetFirewallRule -DisplayGroup "Remote Desktop"`) while this succeeds output, err := r.run(`Enable-NetFirewallRule -DisplayGroup 'Remote Desktop'`) Wrapping the command with { and } also seems to solve this problem. Interestingly, this issue seems not reproducible Linux host. --- experiments/swdt/pkg/connections/ssh.go | 4 +++- experiments/swdt/pkg/pwsh/setup/setup.go | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/experiments/swdt/pkg/connections/ssh.go b/experiments/swdt/pkg/connections/ssh.go index 6102d74..2072049 100644 --- a/experiments/swdt/pkg/connections/ssh.go +++ b/experiments/swdt/pkg/connections/ssh.go @@ -110,7 +110,9 @@ func (c *SSHConnection) Run(args string) (string, error) { // Multiline PowerShell commands over SSH trip over newlines - only first one is executed args = regexp.MustCompile(`\r?\n`).ReplaceAllLiteralString(args, ";") - if err := session.Run("powershell -nologo -noprofile -c " + args); err != nil { + cmd := fmt.Sprintf("powershell -nologo -noprofile -c { %s }", args) + // TODO(mloskot): Log executed commands in --verbose mode + if err := session.Run(cmd); err != nil { return "", err } diff --git a/experiments/swdt/pkg/pwsh/setup/setup.go b/experiments/swdt/pkg/pwsh/setup/setup.go index 34e4511..c221d87 100644 --- a/experiments/swdt/pkg/pwsh/setup/setup.go +++ b/experiments/swdt/pkg/pwsh/setup/setup.go @@ -81,11 +81,12 @@ func (r *SetupRunner) EnableRDP(enable bool) error { return nil } - output, err := r.run(`Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0; + klog.Info(resc.Sprintf("Enabling RDP.")) + output, err := r.run(`Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -name "fDenyTSConnections" -value 0; Enable-NetFirewallRule -DisplayGroup "Remote Desktop"`) if err != nil { return err } - klog.Info(resc.Sprintf("Enabling RDP. %s", output)) + klog.Info(resc.Sprintf(output)) return nil }