Skip to content

Commit

Permalink
fix: ssh formatting exception bug when executing commands (#2406)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dbbDylan authored Sep 18, 2024
1 parent 92dd64f commit e4957a6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion builtin/roles/init/init-os/tasks/init_repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/local_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/connector/ssh_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 != "" {
Expand Down

0 comments on commit e4957a6

Please sign in to comment.