Skip to content

Commit e4957a6

Browse files
authored
fix: ssh formatting exception bug when executing commands (#2406)
* fix: ssh formatting exception bug when executing commands * Update ssh_connector.go WARNING: Use `nolint:gosec` annotation * Update ssh_connector.go * Update ssh_connector.go * Update local_connector.go * Update local_connector.go * Update init_repository.yaml
1 parent 92dd64f commit e4957a6

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

builtin/roles/init/init-os/tasks/init_repository.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
mkdir -p /etc/apt/sources.list.d
3232
# add repository
3333
rm -rf /etc/apt/sources.list.d/*
34-
echo 'deb [trusted=yes] file://tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list
34+
echo 'deb [trusted=yes] file:///tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list
3535
# update repository
3636
apt-get update
3737
# install

pkg/connector/local_connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *localConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte
9797
klog.V(5).InfoS("exec local command", "cmd", cmd)
9898
// find command interpreter in env. default /bin/bash
9999

100-
command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", cmd)
100+
command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", "\"", cmd, "\"")
101101
if c.Password != "" {
102102
command.SetStdin(bytes.NewBufferString(c.Password + "\n"))
103103
}

pkg/connector/ssh_connector.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io"
2525
"io/fs"
2626
"os"
27+
"os/exec"
2728
"os/user"
2829
"path/filepath"
2930
"strings"
@@ -234,7 +235,7 @@ func (c *sshConnector) FetchFile(_ context.Context, src string, dst io.Writer) e
234235
}
235236

236237
// ExecuteCommand in remote host
237-
func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, error) {
238+
func (c *sshConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte, error) {
238239
klog.V(5).InfoS("exec ssh command", "cmd", cmd, "host", c.Host)
239240
// create ssh session
240241
session, err := c.client.NewSession()
@@ -245,7 +246,8 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er
245246
}
246247
defer session.Close()
247248

248-
cmd = fmt.Sprintf("sudo -E %s -c \"%q\"", c.shell, cmd)
249+
//nolint:gosec
250+
command := exec.CommandContext(ctx, "sudo", "-E", c.shell, "-c", "\"", cmd, "\"")
249251
// get pipe from session
250252
stdin, _ := session.StdinPipe()
251253
stdout, _ := session.StdoutPipe()
@@ -255,7 +257,7 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er
255257
return nil, err
256258
}
257259
// Start the remote command
258-
if err := session.Start(cmd); err != nil {
260+
if err := session.Start(command.String()); err != nil {
259261
return nil, err
260262
}
261263
if c.Password != "" {

0 commit comments

Comments
 (0)