Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the execution of SSH commands with optional input. #1831

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pkg/ssh/connection_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func golangConnectionDial(options ConnectionDialOptions) (*ConnectionDialReport,
return &ConnectionDialReport{dial}, nil
}

func golangConnectionExec(options ConnectionExecOptions) (*ConnectionExecReport, error) {
func golangConnectionExec(options ConnectionExecOptions, input io.Reader) (*ConnectionExecReport, error) {
if !strings.HasPrefix(options.Host, "ssh://") {
options.Host = "ssh://" + options.Host
}
Expand All @@ -117,7 +117,7 @@ func golangConnectionExec(options ConnectionExecOptions) (*ConnectionExecReport,
return nil, fmt.Errorf("failed to connect: %w", err)
}

out, err := ExecRemoteCommand(dialAdd, strings.Join(options.Args, " "))
out, err := ExecRemoteCommandWithInput(dialAdd, strings.Join(options.Args, " "), input)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -189,6 +189,10 @@ func golangConnectionScp(options ConnectionScpOptions) (*ConnectionScpReport, er
// ExecRemoteCommand takes a ssh client connection and a command to run and executes the
// command on the specified client. The function returns the Stdout from the client or the Stderr
func ExecRemoteCommand(dial *ssh.Client, run string) ([]byte, error) {
return ExecRemoteCommandWithInput(dial, run, nil)
}

func ExecRemoteCommandWithInput(dial *ssh.Client, run string, input io.Reader) ([]byte, error) {
sess, err := dial.NewSession() // new ssh client session
if err != nil {
return nil, err
Expand All @@ -197,8 +201,11 @@ func ExecRemoteCommand(dial *ssh.Client, run string) ([]byte, error) {

var buffer bytes.Buffer
var bufferErr bytes.Buffer
sess.Stdout = &buffer // output from client funneled into buffer
sess.Stderr = &bufferErr // err form client funneled into buffer
sess.Stdout = &buffer // output from client funneled into buffer
sess.Stderr = &bufferErr // err from client funneled into buffer
if input != nil {
sess.Stdin = input
}
if err := sess.Run(run); err != nil { // run the command on the ssh client
return nil, fmt.Errorf("%v: %w", bufferErr.String(), err)
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/ssh/connection_native.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"regexp"
"strings"
Expand Down Expand Up @@ -100,7 +101,7 @@ func nativeConnectionCreate(options ConnectionCreateOptions) error {
})
}

func nativeConnectionExec(options ConnectionExecOptions) (*ConnectionExecReport, error) {
func nativeConnectionExec(options ConnectionExecOptions, input io.Reader) (*ConnectionExecReport, error) {
dst, uri, err := Validate(options.User, options.Host, options.Port, options.Identity)
if err != nil {
return nil, err
Expand Down Expand Up @@ -134,6 +135,9 @@ func nativeConnectionExec(options ConnectionExecOptions) (*ConnectionExecReport,
info := exec.Command(ssh, args...)
info.Stdout = output
info.Stderr = errors
if input != nil {
info.Stdin = input
}
err = info.Run()
if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ssh

import (
"fmt"
"io"

"golang.org/x/crypto/ssh"
)
Expand All @@ -27,15 +28,19 @@ func Dial(options *ConnectionDialOptions, kind EngineMode) (*ssh.Client, error)
}

func Exec(options *ConnectionExecOptions, kind EngineMode) (string, error) {
return ExecWithInput(options, kind, nil)
}

func ExecWithInput(options *ConnectionExecOptions, kind EngineMode, input io.Reader) (string, error) {
var rep *ConnectionExecReport
var err error
if kind == NativeMode {
rep, err = nativeConnectionExec(*options)
rep, err = nativeConnectionExec(*options, input)
if err != nil {
return "", err
}
} else {
rep, err = golangConnectionExec(*options)
rep, err = golangConnectionExec(*options, input)
if err != nil {
return "", err
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ func TestExec(t *testing.T) {
require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}

func TestExecWithInput(t *testing.T) {
options := ConnectionExecOptions{
Port: 22,
Host: "localhost",
Args: []string{"md5sum"},
}

input, err := os.Open("/etc/fstab")
require.NoError(t, err)
defer input.Close()

_, err = ExecWithInput(&options, NativeMode, input)
// exit status 255 is what you get when ssh is not enabled or the connection failed
// this means up to that point, everything worked
require.Error(t, err, "exit status 255")

_, err = ExecWithInput(&options, GolangMode, input)
require.Error(t, err, "failed to connect: ssh: handshake failed: ssh: disconnect, reason 2: Too many authentication failures")
}

func TestDial(t *testing.T) {
options := ConnectionDialOptions{
Port: 22,
Expand Down
Loading