From e4957a648bc77b26d907208998bde0f9fb04a5b9 Mon Sep 17 00:00:00 2001 From: dbb_DingYongliang <62107013+dbbDylan@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:33:33 +0800 Subject: [PATCH] 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 --- builtin/roles/init/init-os/tasks/init_repository.yaml | 2 +- pkg/connector/local_connector.go | 2 +- pkg/connector/ssh_connector.go | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/builtin/roles/init/init-os/tasks/init_repository.yaml b/builtin/roles/init/init-os/tasks/init_repository.yaml index ea80cd0f..b4758423 100644 --- a/builtin/roles/init/init-os/tasks/init_repository.yaml +++ b/builtin/roles/init/init-os/tasks/init_repository.yaml @@ -31,7 +31,7 @@ mkdir -p /etc/apt/sources.list.d # add repository rm -rf /etc/apt/sources.list.d/* - echo 'deb [trusted=yes] file://tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list + echo 'deb [trusted=yes] file:///tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list # update repository apt-get update # install diff --git a/pkg/connector/local_connector.go b/pkg/connector/local_connector.go index 533e20ed..4ebd68d2 100644 --- a/pkg/connector/local_connector.go +++ b/pkg/connector/local_connector.go @@ -97,7 +97,7 @@ func (c *localConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte klog.V(5).InfoS("exec local command", "cmd", cmd) // find command interpreter in env. default /bin/bash - command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", cmd) + command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", "\"", cmd, "\"") if c.Password != "" { command.SetStdin(bytes.NewBufferString(c.Password + "\n")) } diff --git a/pkg/connector/ssh_connector.go b/pkg/connector/ssh_connector.go index 112d0ac7..51ffe184 100644 --- a/pkg/connector/ssh_connector.go +++ b/pkg/connector/ssh_connector.go @@ -24,6 +24,7 @@ import ( "io" "io/fs" "os" + "os/exec" "os/user" "path/filepath" "strings" @@ -234,7 +235,7 @@ func (c *sshConnector) FetchFile(_ context.Context, src string, dst io.Writer) e } // ExecuteCommand in remote host -func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, error) { +func (c *sshConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte, error) { klog.V(5).InfoS("exec ssh command", "cmd", cmd, "host", c.Host) // create ssh session session, err := c.client.NewSession() @@ -245,7 +246,8 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er } defer session.Close() - cmd = fmt.Sprintf("sudo -E %s -c \"%q\"", c.shell, cmd) + //nolint:gosec + command := exec.CommandContext(ctx, "sudo", "-E", c.shell, "-c", "\"", cmd, "\"") // get pipe from session stdin, _ := session.StdinPipe() stdout, _ := session.StdoutPipe() @@ -255,7 +257,7 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er return nil, err } // Start the remote command - if err := session.Start(cmd); err != nil { + if err := session.Start(command.String()); err != nil { return nil, err } if c.Password != "" {