Skip to content

Commit

Permalink
Add OpenSSH client protocol #124
Browse files Browse the repository at this point in the history
  • Loading branch information
kke authored Oct 10, 2023
2 parents 70cb294 + f0ffd33 commit 6730b4a
Show file tree
Hide file tree
Showing 10 changed files with 598 additions and 94 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ linters:

linters-settings:
funlen:
lines: 65
statements: 45
lines: 100
statements: 75
varnamelen:
max-distance: 10
ignore-decls:
Expand Down
108 changes: 67 additions & 41 deletions cmd/rigtest/rigtest.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"errors"
Expand Down Expand Up @@ -99,13 +98,14 @@ func retry(fn func() error) error {
func main() {
dh := flag.String("host", "127.0.0.1", "target host [+ :port], can give multiple comma separated")
usr := flag.String("user", "root", "user name")
proto := flag.String("proto", "ssh", "ssh/winrm")
proto := flag.String("proto", "ssh", "ssh/winrm/localhost/openssh")
kp := flag.String("keypath", "", "ssh keypath")
pc := flag.Bool("askpass", false, "ask ssh passwords")
pwd := flag.String("pass", "", "winrm password")
https := flag.Bool("https", false, "use https for winrm")
connectOnly := flag.Bool("connect", false, "just connect and quit")
sshKey := flag.String("ssh-private-key", "", "ssh private key")
multiplex := flag.Bool("ssh-multiplex", true, "use ssh multiplexing")

fn := fmt.Sprintf("test_%s.txt", time.Now().Format("20060102150405"))

Expand Down Expand Up @@ -193,7 +193,7 @@ func main() {
h = &Host{
Connection: rig.Connection{
WinRM: &rig.WinRM{
Address: *dh,
Address: address,
Port: port,
User: *usr,
UseHTTPS: *https,
Expand All @@ -210,6 +210,25 @@ func main() {
},
},
}
case "openssh":
h = &Host{
Connection: rig.Connection{
OpenSSH: &rig.OpenSSH{
Address: address,
KeyPath: kp,
DisableMultiplexing: !*multiplex,
},
},
}
if *usr != "" {
h.OpenSSH.User = usr
}
if port != 22 && port != 0 {
h.OpenSSH.Port = &port
}
if cfgPath := goos.Getenv("SSH_CONFIG"); cfgPath != "" {
h.OpenSSH.ConfigPath = &cfgPath
}
default:
panic("unknown protocol " + *proto)
}
Expand Down Expand Up @@ -268,64 +287,71 @@ func main() {
require.NoError(t, h.Configurer.DeleteFile(h, fn))
require.False(t, h.Configurer.FileExist(h, fn))

testFileSize := int64(1 << (10 * 2)) // 1MB
fsyses := []rigfs.Fsys{h.Fsys(), h.SudoFsys()}

for idx, fsys := range fsyses {
t.Run("fsys functions (%d) on %s", idx+1, h)
for _, testFileSize := range []int64{
int64(500), // less than one block on most filesystems
int64(1 << (10 * 2)), // exactly 1MB
int64(4096), // exactly one block on most filesystems
int64(4097), // plus 1
} {
t.Run("fsys (%d) functions for file size %d on %s", idx+1, testFileSize, h)

origin := io.LimitReader(rand.Reader, testFileSize)
shasum := sha256.New()
reader := io.TeeReader(origin, shasum)
origin := io.LimitReader(rand.Reader, testFileSize)
shasum := sha256.New()
reader := io.TeeReader(origin, shasum)

destf, err := fsys.OpenFile(fn, rigfs.ModeCreate, 0644)
require.NoError(t, err, "open file")
destf, err := fsys.OpenFile(fn, rigfs.ModeCreate, 0644)
require.NoError(t, err, "open file")

n, err := io.Copy(destf, reader)
require.NoError(t, err, "io.copy file from local to remote")
require.Equal(t, testFileSize, n, "file size not as expected after copy")
n, err := io.Copy(destf, reader)
require.NoError(t, err, "io.copy file from local to remote")
require.Equal(t, testFileSize, n, "file size not as expected after copy")

require.NoError(t, destf.Close(), "error while closing file")
require.NoError(t, destf.Close(), "error while closing file")

fstat, err := fsys.Stat(fn)
require.NoError(t, err, "stat error")
require.Equal(t, testFileSize, fstat.Size(), "file size not as expected in stat result")
fstat, err := fsys.Stat(fn)
require.NoError(t, err, "stat error")
require.Equal(t, testFileSize, fstat.Size(), "file size not as expected in stat result")

destSum, err := fsys.Sha256(fn)
require.NoError(t, err, "sha256 error")
destSum, err := fsys.Sha256(fn)
require.NoError(t, err, "sha256 error")

require.Equal(t, fmt.Sprintf("%x", shasum.Sum(nil)), destSum, "sha256 mismatch after io.copy from local to remote")
require.Equal(t, fmt.Sprintf("%x", shasum.Sum(nil)), destSum, "sha256 mismatch after io.copy from local to remote")

destf, err = fsys.OpenFile(fn, rigfs.ModeRead, 0644)
require.NoError(t, err, "open file for read")
destf, err = fsys.OpenFile(fn, rigfs.ModeRead, 0644)
require.NoError(t, err, "open file for read")

readSha := sha256.New()
n, err = io.Copy(readSha, destf)
require.NoError(t, err, "io.copy file from remote to local")
readSha := sha256.New()
n, err = io.Copy(readSha, destf)
require.NoError(t, err, "io.copy file from remote to local")

require.Equal(t, testFileSize, n, "file size not as expected after copy from remote to local")
require.Equal(t, testFileSize, n, "file size not as expected after copy from remote to local")

fstat, err = destf.Stat()
require.NoError(t, err, "stat error after read")
require.Equal(t, testFileSize, fstat.Size(), "file size not as expected in stat result after read")
require.True(t, bytes.Equal(readSha.Sum(nil), shasum.Sum(nil)), "sha256 mismatch after io.copy from remote to local")
fstat, err = destf.Stat()
require.NoError(t, err, "stat error after read")
require.Equal(t, testFileSize, fstat.Size(), "file size not as expected in stat result after read")
require.Equal(t, readSha.Sum(nil), shasum.Sum(nil), "sha256 mismatch after io.copy from remote to local")

_, err = destf.Seek(0, 0)
require.NoError(t, err, "seek")
_, err = destf.Seek(0, 0)
require.NoError(t, err, "seek")

readSha.Reset()
readSha.Reset()

n, err = io.Copy(readSha, destf)
require.NoError(t, err, "io.copy file from remote to local after seek")
n, err = io.Copy(readSha, destf)
require.NoError(t, err, "io.copy file from remote to local after seek")

require.Equal(t, testFileSize, n, "file size not as expected after copy from remote to local after seek")
require.Equal(t, testFileSize, n, "file size not as expected after copy from remote to local after seek")

require.True(t, bytes.Equal(readSha.Sum(nil), shasum.Sum(nil)), "sha256 mismatch after io.copy from remote to local after seek")
require.Equal(t, readSha.Sum(nil), shasum.Sum(nil), "sha256 mismatch after io.copy from remote to local after seek")

require.NoError(t, destf.Close(), "close after seek + read")
require.NoError(t, fsys.Remove(fn), "remove file")
_, err = destf.Stat()
require.ErrorIs(t, err, fs.ErrNotExist, "file still exists")
require.NoError(t, destf.Close(), "close after seek + read")
require.NoError(t, fsys.Remove(fn), "remove file")
_, err = destf.Stat()
require.ErrorIs(t, err, fs.ErrNotExist, "file still exists")
}
t.Run("fsys (%d) dir ops on %s", idx+1, h)

// fsys dirops
require.NoError(t, fsys.MkDirAll("tmpdir/nested", 0644), "make nested dir")
Expand Down
5 changes: 5 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Connection struct {
WinRM *WinRM `yaml:"winRM,omitempty"`
SSH *SSH `yaml:"ssh,omitempty"`
Localhost *Localhost `yaml:"localhost,omitempty"`
OpenSSH *OpenSSH `yaml:"openSSH,omitempty"`

OSVersion *OSVersion `yaml:"-"`

Expand Down Expand Up @@ -406,6 +407,10 @@ func (c *Connection) configuredClient() client {
return c.SSH
}

if c.OpenSSH != nil {
return c.OpenSSH
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Localhost) ExecStreams(cmd string, stdin io.ReadCloser, stdout, stderr
}

// Exec executes a command on the host
func (c *Localhost) Exec(cmd string, opts ...exec.Option) error { //nolint:cyclop,funlen
func (c *Localhost) Exec(cmd string, opts ...exec.Option) error { //nolint:cyclop
execOpts := exec.Build(opts...)
command, err := c.command(cmd, execOpts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Log Logger

// Tracef logs a trace level log message
func Tracef(t string, args ...interface{}) {
Log.Debugf(t, args...)
Log.Tracef(t, args...)
}

// Debugf logs a debug level log message
Expand Down
Loading

0 comments on commit 6730b4a

Please sign in to comment.