From d0638f5887bd8ea30da088644b42b25574f9ad82 Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Fri, 30 Aug 2024 10:45:18 +0200 Subject: [PATCH 1/4] Use sudo -S argument to pipe in password --- tests/thread_tests/remote.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/thread_tests/remote.go b/tests/thread_tests/remote.go index a553dd1..82c0919 100644 --- a/tests/thread_tests/remote.go +++ b/tests/thread_tests/remote.go @@ -142,6 +142,12 @@ func remote_exec(t *testing.T, command string) string { t.Logf("[exec-ssh] %s", command) + // Remote commands that require sudo might ask for the password. Always pass it in. See https://stackoverflow.com/a/11955358 + if strings.HasPrefix(command, "sudo ") { + command = command[5:] // remove "sudo " + command = "echo \"" + remotePassword + "\" | sudo -S " + command + } + if SSHClient == nil { t.Fatalf("SSH client not initialized. Please connect to remote device first") } From 4c4c3e8bf4910f55eec771bf556daed397cad2e2 Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Fri, 30 Aug 2024 10:54:46 +0200 Subject: [PATCH 2/4] Escape quotes in password --- tests/thread_tests/remote.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/thread_tests/remote.go b/tests/thread_tests/remote.go index 82c0919..2441e18 100644 --- a/tests/thread_tests/remote.go +++ b/tests/thread_tests/remote.go @@ -144,8 +144,9 @@ func remote_exec(t *testing.T, command string) string { // Remote commands that require sudo might ask for the password. Always pass it in. See https://stackoverflow.com/a/11955358 if strings.HasPrefix(command, "sudo ") { - command = command[5:] // remove "sudo " - command = "echo \"" + remotePassword + "\" | sudo -S " + command + command = command[5:] + escapedPassword := strings.Replace(remotePassword, "\"", "\\\"", -1) + command = "echo \"" + escapedPassword + "\" | sudo -S " + command } if SSHClient == nil { From 94905afb00f0d0a3cea77904ba9402ef88afd600 Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Fri, 30 Aug 2024 10:58:52 +0200 Subject: [PATCH 3/4] Address reviews --- tests/thread_tests/remote.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/thread_tests/remote.go b/tests/thread_tests/remote.go index 2441e18..b256029 100644 --- a/tests/thread_tests/remote.go +++ b/tests/thread_tests/remote.go @@ -144,9 +144,9 @@ func remote_exec(t *testing.T, command string) string { // Remote commands that require sudo might ask for the password. Always pass it in. See https://stackoverflow.com/a/11955358 if strings.HasPrefix(command, "sudo ") { - command = command[5:] + command = strings.TrimPrefix(command, "sudo ") escapedPassword := strings.Replace(remotePassword, "\"", "\\\"", -1) - command = "echo \"" + escapedPassword + "\" | sudo -S " + command + command = fmt.Sprintf(`echo "%s" | sudo -S %s`, escapedPassword, command) } if SSHClient == nil { From 255f62b1af913007045506818b425a32f7a49494 Mon Sep 17 00:00:00 2001 From: JP Meijers Date: Fri, 30 Aug 2024 11:25:04 +0200 Subject: [PATCH 4/4] More readable escaping --- tests/thread_tests/remote.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/thread_tests/remote.go b/tests/thread_tests/remote.go index b256029..7714079 100644 --- a/tests/thread_tests/remote.go +++ b/tests/thread_tests/remote.go @@ -145,7 +145,7 @@ func remote_exec(t *testing.T, command string) string { // Remote commands that require sudo might ask for the password. Always pass it in. See https://stackoverflow.com/a/11955358 if strings.HasPrefix(command, "sudo ") { command = strings.TrimPrefix(command, "sudo ") - escapedPassword := strings.Replace(remotePassword, "\"", "\\\"", -1) + escapedPassword := strings.ReplaceAll(remotePassword, `"`, `\"`) command = fmt.Sprintf(`echo "%s" | sudo -S %s`, escapedPassword, command) }