Documentation
¶
Index ¶
- Constants
- Variables
- func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string
- func ExportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error)
- func ExtractSSHKeyPaths() (string, string, string, string, error)
- func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey)
- func GetAggregateSSHTimeout() time.Duration
- func GetHostKeyCallback(host string) (ssh.HostKeyCallback, error)
- func GetTypedMockClient(t *testing.T, log *logger.Logger) (*MockSSHClient, SSHConfiger)
- func NewMockSSHClient(dialer SSHDialer) (*MockSSHClient, SSHConfiger)
- func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error)
- func ParseRsaPublicKeyFromPemStr(pubPEM string) (*rsa.PublicKey, error)
- func ReadPrivateKey(path string) ([]byte, error)
- func ReadPublicKey(path string) ([]byte, error)
- func ValidateSSHKeysFromPath(publicKeyPath, privateKeyPath string) error
- func ValidateSSHPublicKey(key string) error
- type ExecuteCommandExpectation
- type Expectation
- type ExpectedSSHBehavior
- type MockSFTPClient
- type MockSSHClient
- type MockSSHConfig
- func (m *MockSSHConfig) Close() error
- func (m *MockSSHConfig) Connect() (SSHClienter, error)
- func (m *MockSSHConfig) ExecuteCommand(ctx context.Context, cmd string) (string, error)
- func (m *MockSSHConfig) ExecuteCommandWithCallback(ctx context.Context, cmd string, progressCallback func(string)) (string, error)
- func (m *MockSSHConfig) GetHost() string
- func (m *MockSSHConfig) GetLastOutput() string
- func (m *MockSSHConfig) GetPort() int
- func (m *MockSSHConfig) GetPrivateKeyMaterial() []byte
- func (m *MockSSHConfig) GetSSHClient() *ssh.Client
- func (m *MockSSHConfig) GetSSHClienter() SSHClienter
- func (m *MockSSHConfig) GetSSHDial() SSHDialer
- func (m *MockSSHConfig) GetUser() string
- func (m *MockSSHConfig) InstallSystemdService(ctx context.Context, serviceName string, serviceContent string) error
- func (m *MockSSHConfig) PushFile(ctx context.Context, dst string, fileContents []byte, executable bool) error
- func (m *MockSSHConfig) PushFileWithCallback(ctx context.Context, dst string, fileContents []byte, executable bool, ...) error
- func (m *MockSSHConfig) RestartService(ctx context.Context, serviceName string) error
- func (m *MockSSHConfig) SetSSHClient(client *ssh.Client)
- func (m *MockSSHConfig) SetSSHClienter(clienter SSHClienter)
- func (m *MockSSHConfig) SetSSHDial(dialer SSHDialer)
- func (m *MockSSHConfig) SetValidateSSHConnection(callback func() error)
- func (m *MockSSHConfig) StartService(ctx context.Context, serviceName string) error
- func (m *MockSSHConfig) WaitForSSH(ctx context.Context, retries int, retryDelay time.Duration) error
- type MockSSHDialer
- type MockSSHSession
- func (m *MockSSHSession) Close() error
- func (m *MockSSHSession) CombinedOutput(cmd string) ([]byte, error)
- func (m *MockSSHSession) Run(cmd string) error
- func (m *MockSSHSession) Start(cmd string) error
- func (m *MockSSHSession) StderrPipe() (io.Reader, error)
- func (m *MockSSHSession) StdinPipe() (io.WriteCloser, error)
- func (m *MockSSHSession) StdoutPipe() (io.Reader, error)
- func (m *MockSSHSession) Wait() error
- type MockSessioner
- type MockWriteCloser
- type PushFileExpectation
- type SFTPClient
- type SFTPClientCreator
- type SFTPClienter
- type SFTPDialFunc
- type SFTPFile
- type SSHClient
- type SSHClientWrapper
- type SSHClienter
- type SSHConfig
- func (c *SSHConfig) Connect() (SSHClienter, error)
- func (c *SSHConfig) ExecuteCommand(ctx context.Context, command string) (string, error)
- func (c *SSHConfig) ExecuteCommandWithCallback(ctx context.Context, command string, callback func(string)) (string, error)
- func (c *SSHConfig) GetHost() string
- func (c *SSHConfig) GetPort() int
- func (c *SSHConfig) GetPrivateKeyMaterial() []byte
- func (c *SSHConfig) GetSSHClient() *ssh.Client
- func (c *SSHConfig) GetSSHClienter() SSHClienter
- func (c *SSHConfig) GetSSHDial() SSHDialer
- func (c *SSHConfig) GetUser() string
- func (c *SSHConfig) InstallSystemdService(ctx context.Context, serviceName, serviceContent string) error
- func (c *SSHConfig) NewSession() (SSHSessioner, error)
- func (c *SSHConfig) PushFile(ctx context.Context, remotePath string, content []byte, executable bool) error
- func (c *SSHConfig) PushFileWithCallback(ctx context.Context, remotePath string, content []byte, executable bool, ...) error
- func (c *SSHConfig) RestartService(ctx context.Context, serviceName string) error
- func (c *SSHConfig) SetSSHClient(client *ssh.Client)
- func (c *SSHConfig) SetSSHClienter(client SSHClienter)
- func (c *SSHConfig) SetSSHDial(dialer SSHDialer)
- func (c *SSHConfig) SetValidateSSHConnection(fn func() error)
- func (c *SSHConfig) StartService(ctx context.Context, serviceName string) error
- func (c *SSHConfig) WaitForSSH(ctx context.Context, retry int, timeout time.Duration) error
- type SSHConfiger
- type SSHDial
- type SSHDialer
- type SSHError
- type SSHSessionWrapper
- func (s *SSHSessionWrapper) Close() error
- func (s *SSHSessionWrapper) CombinedOutput(cmd string) ([]byte, error)
- func (s *SSHSessionWrapper) Run(cmd string) error
- func (s *SSHSessionWrapper) Signal(sig ssh.Signal) error
- func (s *SSHSessionWrapper) Start(cmd string) error
- func (s *SSHSessionWrapper) StderrPipe() (io.Reader, error)
- func (s *SSHSessionWrapper) StdinPipe() (io.WriteCloser, error)
- func (s *SSHSessionWrapper) StdoutPipe() (io.Reader, error)
- func (s *SSHSessionWrapper) Wait() error
- type SSHSessioner
Constants ¶
const ( DefaultSSHPort = 22 DefaultRetryCount = 5 DefaultRetryDelay = 10 * time.Second )
Variables ¶
var ( TimeInBetweenSSHRetries = 2 * time.Second SSHTimeOut = 1 * time.Minute SSHRetryAttempts = 3 SSHDialTimeout = 3 * time.Second SSHClientConfigTimeout = 3 * time.Second SSHRetryDelay = 3 * time.Second )
var MockSSHKeyReader = func(path string) ([]byte, error) { isPublicKey := len(path) > 4 && path[len(path)-4:] == ".pub" if isPublicKey { return []byte(testdata.TestPublicSSHKeyMaterial), nil } else { return []byte(testdata.TestPrivateSSHKeyMaterial), nil } }
var NewSSHConfigFunc = NewSSHConfig
var NullCallback = func(int64, int64) {}
var SSHDialerFunc = NewSSHDial
Functions ¶
func ExportRsaPrivateKeyAsPemStr ¶ added in v0.0.9
func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string
func ExportRsaPublicKeyAsPemStr ¶ added in v0.0.9
func GenerateRsaKeyPair ¶ added in v0.0.9
func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey)
func GetAggregateSSHTimeout ¶
func GetHostKeyCallback ¶
func GetHostKeyCallback(host string) (ssh.HostKeyCallback, error)
func GetTypedMockClient ¶
func GetTypedMockClient(t *testing.T, log *logger.Logger) (*MockSSHClient, SSHConfiger)
func NewMockSSHClient ¶
func NewMockSSHClient(dialer SSHDialer) (*MockSSHClient, SSHConfiger)
func ParseRsaPrivateKeyFromPemStr ¶ added in v0.0.9
func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error)
func ParseRsaPublicKeyFromPemStr ¶ added in v0.0.9
func ReadPrivateKey ¶
func ReadPublicKey ¶
func ValidateSSHKeysFromPath ¶
GetSSHKeysFromPath reads and returns the public and private SSH keys from the specified path. If the path ends with ".pub", it is assumed to be the public key, and the corresponding private key is expected to be located at the same path without the ".pub" extension. The function uses SSHKeyReader to read the keys from the filesystem.
Parameters: - path: The filesystem path to the public key or the base path for both keys if the path does not end with ".pub".
Returns: - The public key as the first return value. - The private key as the second return value. - An error if either key could not be read, otherwise nil.
Note: The function assumes that the public and private keys are named identically with the only difference being the ".pub" extension for the public key.
func ValidateSSHPublicKey ¶
ValidateSSHPublicKey checks if the provided SSH public key is valid
Types ¶
type Expectation ¶
type ExpectedSSHBehavior ¶
type ExpectedSSHBehavior struct {
PushFileExpectations []PushFileExpectation
ExecuteCommandExpectations []ExecuteCommandExpectation
InstallSystemdServiceExpectation *Expectation
RestartServiceExpectation *Expectation
}
ExpectedSSHBehavior holds the expected outcomes for SSH methods
type MockSFTPClient ¶ added in v0.0.20
MockSSHClient is a mock implementation of SSHClienter
func NewMockSFTPClient ¶ added in v0.0.20
func NewMockSFTPClient() *MockSFTPClient
type MockSSHClient ¶
type MockSSHClient struct {
mock.Mock
Session SSHSessioner
Dialer SSHDialer
}
func (*MockSSHClient) Close ¶
func (m *MockSSHClient) Close() error
func (*MockSSHClient) GetClient ¶ added in v0.0.20
func (m *MockSSHClient) GetClient() *ssh.Client
func (*MockSSHClient) IsConnected ¶ added in v0.0.18
func (m *MockSSHClient) IsConnected() bool
func (*MockSSHClient) NewSession ¶
func (m *MockSSHClient) NewSession() (SSHSessioner, error)
type MockSSHConfig ¶
MockSSHConfig is a mock implementation of SSHConfiger
func NewMockSSHConfigWithBehavior ¶
func NewMockSSHConfigWithBehavior(behavior ExpectedSSHBehavior) *MockSSHConfig
NewMockSSHConfigWithBehavior creates a mock SSHConfig based on the expected behavior
func (*MockSSHConfig) Close ¶
func (m *MockSSHConfig) Close() error
func (*MockSSHConfig) Connect ¶
func (m *MockSSHConfig) Connect() (SSHClienter, error)
func (*MockSSHConfig) ExecuteCommand ¶
func (*MockSSHConfig) ExecuteCommandWithCallback ¶
func (*MockSSHConfig) GetHost ¶ added in v0.0.20
func (m *MockSSHConfig) GetHost() string
func (*MockSSHConfig) GetLastOutput ¶
func (m *MockSSHConfig) GetLastOutput() string
func (*MockSSHConfig) GetPort ¶ added in v0.0.20
func (m *MockSSHConfig) GetPort() int
func (*MockSSHConfig) GetPrivateKeyMaterial ¶ added in v0.0.20
func (m *MockSSHConfig) GetPrivateKeyMaterial() []byte
func (*MockSSHConfig) GetSSHClient ¶ added in v0.0.20
func (m *MockSSHConfig) GetSSHClient() *ssh.Client
func (*MockSSHConfig) GetSSHClienter ¶ added in v0.0.20
func (m *MockSSHConfig) GetSSHClienter() SSHClienter
func (*MockSSHConfig) GetSSHDial ¶ added in v0.0.20
func (m *MockSSHConfig) GetSSHDial() SSHDialer
func (*MockSSHConfig) GetUser ¶ added in v0.0.20
func (m *MockSSHConfig) GetUser() string
func (*MockSSHConfig) InstallSystemdService ¶
func (*MockSSHConfig) PushFileWithCallback ¶
func (*MockSSHConfig) RestartService ¶
func (m *MockSSHConfig) RestartService(ctx context.Context, serviceName string) error
func (*MockSSHConfig) SetSSHClient ¶
func (m *MockSSHConfig) SetSSHClient(client *ssh.Client)
func (*MockSSHConfig) SetSSHClienter ¶ added in v0.0.20
func (m *MockSSHConfig) SetSSHClienter(clienter SSHClienter)
func (*MockSSHConfig) SetSSHDial ¶ added in v0.0.20
func (m *MockSSHConfig) SetSSHDial(dialer SSHDialer)
func (*MockSSHConfig) SetValidateSSHConnection ¶ added in v0.0.20
func (m *MockSSHConfig) SetValidateSSHConnection(callback func() error)
func (*MockSSHConfig) StartService ¶
func (m *MockSSHConfig) StartService( ctx context.Context, serviceName string, ) error
func (*MockSSHConfig) WaitForSSH ¶
type MockSSHDialer ¶
MockSSHDialer is a mock implementation of SSHDialer
func NewMockSSHDialer ¶
func NewMockSSHDialer() *MockSSHDialer
NewMockSSHDialer returns a MockSSHDialer with a default implementation
func (*MockSSHDialer) Dial ¶
func (m *MockSSHDialer) Dial(network, addr string, config *ssh.ClientConfig) (SSHClienter, error)
Dial is a mock implementation of the Dial method
func (*MockSSHDialer) DialContext ¶ added in v0.0.20
func (m *MockSSHDialer) DialContext( ctx context.Context, network, addr string, config *ssh.ClientConfig, ) (SSHClienter, error)
DialContext is a mock implementation of the DialContext method
type MockSSHSession ¶
func NewMockSSHSession ¶
func NewMockSSHSession() *MockSSHSession
func (*MockSSHSession) Close ¶
func (m *MockSSHSession) Close() error
func (*MockSSHSession) CombinedOutput ¶
func (m *MockSSHSession) CombinedOutput(cmd string) ([]byte, error)
func (*MockSSHSession) Run ¶
func (m *MockSSHSession) Run(cmd string) error
func (*MockSSHSession) Start ¶
func (m *MockSSHSession) Start(cmd string) error
func (*MockSSHSession) StderrPipe ¶
func (m *MockSSHSession) StderrPipe() (io.Reader, error)
func (*MockSSHSession) StdinPipe ¶
func (m *MockSSHSession) StdinPipe() (io.WriteCloser, error)
func (*MockSSHSession) StdoutPipe ¶
func (m *MockSSHSession) StdoutPipe() (io.Reader, error)
func (*MockSSHSession) Wait ¶
func (m *MockSSHSession) Wait() error
type MockSessioner ¶
type MockWriteCloser ¶
func (*MockWriteCloser) Close ¶
func (m *MockWriteCloser) Close() error
type PushFileExpectation ¶
type SFTPClient ¶ added in v0.0.20
type SFTPClient interface {
MkdirAll(path string) error
Create(path string) (SFTPFile, error)
Chmod(path string, mode os.FileMode) error
Close() error
}
SFTPClient interface defines the SFTP operations we need
type SFTPClientCreator ¶ added in v0.0.20
type SFTPClientCreator func(client *ssh.Client) (SFTPClient, error)
SFTPClientCreator is a function type for creating SFTP clients
type SFTPClienter ¶
type SFTPClienter interface {
Create(path string) (io.WriteCloser, error)
Open(path string) (io.ReadCloser, error)
Close() error
}
SFTPClienter interface defines the methods we need for SFTP operations
type SFTPDialFunc ¶
type SFTPDialFunc func(conn *ssh.Client) (SFTPClienter, error)
SFTPDial is a function type for creating SFTP clients
type SFTPFile ¶ added in v0.0.20
type SFTPFile interface {
io.WriteCloser
}
SFTPFile interface defines the file operations we need
type SSHClient ¶
type SSHClient struct {
SSHClientConfig *ssh.ClientConfig
Client SSHClienter
Dialer SSHDialer
}
SSHClient struct definition
func (*SSHClient) IsConnected ¶ added in v0.0.18
func (*SSHClient) NewSession ¶
func (cl *SSHClient) NewSession() (SSHSessioner, error)
type SSHClientWrapper ¶
SSHClientWrapper is a thin wrapper around an *ssh.Client that allows us to satisfy the SSHClienter interface.
func (*SSHClientWrapper) Close ¶
func (w *SSHClientWrapper) Close() error
func (*SSHClientWrapper) GetClient ¶ added in v0.0.20
func (w *SSHClientWrapper) GetClient() *ssh.Client
func (*SSHClientWrapper) IsConnected ¶ added in v0.0.18
func (w *SSHClientWrapper) IsConnected() bool
func (*SSHClientWrapper) NewSession ¶
func (w *SSHClientWrapper) NewSession() (SSHSessioner, error)
type SSHClienter ¶
type SSHClienter interface {
NewSession() (SSHSessioner, error)
IsConnected() bool
Close() error
GetClient() *ssh.Client
}
SSHClienter interface defines the methods we need for SSH operations
type SSHConfig ¶
type SSHConfig struct {
Host string
Port int
User string
SSHPrivateKeyPath string
PrivateKeyMaterial []byte
Timeout time.Duration
Logger *logger.Logger
SSHClient SSHClienter
SSHDial SSHDialer
SSHPrivateKeyReader func(path string) ([]byte, error)
SSHPublicKeyReader func(path string) ([]byte, error)
ClientConfig *ssh.ClientConfig
InsecureIgnoreHostKey bool
ValidateSSHConnectionFunc func() error
}
func (*SSHConfig) Connect ¶
func (c *SSHConfig) Connect() (SSHClienter, error)
func (*SSHConfig) ExecuteCommand ¶
func (*SSHConfig) ExecuteCommandWithCallback ¶
func (c *SSHConfig) ExecuteCommandWithCallback(ctx context.Context, command string, callback func(string)) (string, error)
ExecuteCommandWithCallback runs a command on the remote server over SSH. It takes the command as a string argument. It retries the execution a configurable number of times if it fails. It returns the output of the command as a string and any error encountered.
func (*SSHConfig) GetPrivateKeyMaterial ¶ added in v0.0.20
func (*SSHConfig) GetSSHClient ¶ added in v0.0.20
func (*SSHConfig) GetSSHClienter ¶ added in v0.0.20
func (c *SSHConfig) GetSSHClienter() SSHClienter
func (*SSHConfig) GetSSHDial ¶ added in v0.0.20
func (*SSHConfig) InstallSystemdService ¶
func (*SSHConfig) NewSession ¶
func (c *SSHConfig) NewSession() (SSHSessioner, error)
func (*SSHConfig) PushFileWithCallback ¶
func (*SSHConfig) RestartService ¶
func (*SSHConfig) SetSSHClient ¶
func (*SSHConfig) SetSSHClienter ¶ added in v0.0.20
func (c *SSHConfig) SetSSHClienter(client SSHClienter)
func (*SSHConfig) SetSSHDial ¶ added in v0.0.20
func (*SSHConfig) SetValidateSSHConnection ¶ added in v0.0.20
func (*SSHConfig) StartService ¶
type SSHConfiger ¶
type SSHConfiger interface {
GetSSHClienter() SSHClienter
SetSSHClienter(client SSHClienter)
GetSSHClient() *ssh.Client
SetSSHClient(client *ssh.Client)
Connect() (SSHClienter, error)
WaitForSSH(ctx context.Context, retry int, timeout time.Duration) error
ExecuteCommand(ctx context.Context, command string) (string, error)
ExecuteCommandWithCallback(ctx context.Context,
command string,
callback func(string)) (string, error)
PushFile(ctx context.Context, remotePath string, content []byte, executable bool) error
PushFileWithCallback(ctx context.Context,
remotePath string,
content []byte,
executable bool,
callback func(int64, int64)) error
InstallSystemdService(ctx context.Context, serviceName, serviceContent string) error
StartService(ctx context.Context, serviceName string) error
RestartService(ctx context.Context, serviceName string) error
GetHost() string
GetPort() int
GetUser() string
GetPrivateKeyMaterial() []byte
GetSSHDial() SSHDialer
SetSSHDial(dialer SSHDialer)
SetValidateSSHConnection(func() error)
}
func NewSSHConfig ¶
type SSHDial ¶
type SSHDial struct {
DialCreator func(network, addr string, config *ssh.ClientConfig) (SSHClienter, error)
}
func (*SSHDial) Dial ¶
func (d *SSHDial) Dial(network, addr string, config *ssh.ClientConfig) (SSHClienter, error)
func (*SSHDial) DialContext ¶ added in v0.0.20
func (d *SSHDial) DialContext( ctx context.Context, network, addr string, config *ssh.ClientConfig, ) (SSHClienter, error)
type SSHDialer ¶
type SSHDialer interface {
Dial(network, addr string, config *ssh.ClientConfig) (SSHClienter, error)
DialContext(
ctx context.Context,
network, addr string,
config *ssh.ClientConfig,
) (SSHClienter, error)
}
func NewSSHDial ¶
func NewSSHDial(host string, port int, config *ssh.ClientConfig) SSHDialer
type SSHSessionWrapper ¶
func (*SSHSessionWrapper) Close ¶
func (s *SSHSessionWrapper) Close() error
func (*SSHSessionWrapper) CombinedOutput ¶
func (s *SSHSessionWrapper) CombinedOutput(cmd string) ([]byte, error)
func (*SSHSessionWrapper) Run ¶
func (s *SSHSessionWrapper) Run(cmd string) error
func (*SSHSessionWrapper) Start ¶
func (s *SSHSessionWrapper) Start(cmd string) error
func (*SSHSessionWrapper) StderrPipe ¶
func (s *SSHSessionWrapper) StderrPipe() (io.Reader, error)
func (*SSHSessionWrapper) StdinPipe ¶
func (s *SSHSessionWrapper) StdinPipe() (io.WriteCloser, error)
func (*SSHSessionWrapper) StdoutPipe ¶
func (s *SSHSessionWrapper) StdoutPipe() (io.Reader, error)
func (*SSHSessionWrapper) Wait ¶
func (s *SSHSessionWrapper) Wait() error