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 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) GetLastOutput() 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(sshclient SSHClienter)
- 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 SFTPClienter
- type SFTPDialFunc
- 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) 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 SSHClienter)
- 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 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 = 5 SSHRetryDelay = 10 * 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 MockSSHClient ¶
type MockSSHClient struct { mock.Mock Session SSHSessioner Dialer SSHDialer }
func (*MockSSHClient) Close ¶
func (m *MockSSHClient) Close() error
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) GetLastOutput ¶
func (m *MockSSHConfig) GetLastOutput() 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(sshclient SSHClienter)
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
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 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 SSHClient ¶
type SSHClient struct { SSHClientConfig *ssh.ClientConfig Client SSHClienter Dialer SSHDialer }
SSHClient struct definition
func (*SSHClient) NewSession ¶
func (cl *SSHClient) NewSession() (SSHSessioner, error)
type SSHClientWrapper ¶
func (*SSHClientWrapper) Close ¶
func (w *SSHClientWrapper) Close() error
func (*SSHClientWrapper) NewSession ¶
func (w *SSHClientWrapper) NewSession() (SSHSessioner, error)
type SSHClienter ¶
type SSHClienter interface { NewSession() (SSHSessioner, error) Close() error }
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 }
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) InstallSystemdService ¶
func (*SSHConfig) NewSession ¶
func (c *SSHConfig) NewSession() (SSHSessioner, error)
func (*SSHConfig) PushFileWithCallback ¶
func (c *SSHConfig) PushFileWithCallback( ctx context.Context, remotePath string, content []byte, executable bool, _ func(int64, int64), ) error
PushFile copies a local file to the remote server. It takes the local file path and the remote file path as arguments. The file is copied over an SSH session using the stdin pipe. It returns an error if any step of the process fails.
func (*SSHConfig) RestartService ¶
func (*SSHConfig) SetSSHClient ¶
func (c *SSHConfig) SetSSHClient(client SSHClienter)
func (*SSHConfig) StartService ¶
type SSHConfiger ¶
type SSHConfiger interface { SetSSHClient(client SSHClienter) 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 }
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)
type SSHDialer ¶
type SSHDialer interface {
Dial(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