Documentation
¶
Overview ¶
Package ssh provides Secure Shell (SSH) connection capabilities for the ARK SDK Golang. This package implements the ArkConnection interface to enable secure command execution on Linux/Unix machines using the SSH protocol.
The package supports multiple authentication methods including password authentication and public key authentication (both from file and from content). It includes automatic retry mechanisms for connection failures and provides session management for command execution.
Key features:
- Secure SSH connections with multiple authentication methods
- Password-based authentication
- Public key authentication (file-based and content-based)
- Automatic retry with connection failure detection
- Session-based command execution
- Connection suspend/restore functionality
Example:
conn := NewArkSSHConnection() err := conn.Connect(&connectionsmodels.ArkConnectionDetails{ Address: "linux-server.example.com", Port: 22, Credentials: &connectionsmodels.ArkConnectionCredentials{ User: "username", Password: "password", }, }) if err != nil { // handle error } defer conn.Disconnect() result, err := conn.RunCommand(&connectionsmodels.ArkConnectionCommand{ Command: "ls -la", ExpectedRC: 0, })
Index ¶
- Constants
- type ArkSSHConnection
- func (c *ArkSSHConnection) Connect(connectionDetails *connectionsmodels.ArkConnectionDetails) error
- func (c *ArkSSHConnection) Disconnect() error
- func (c *ArkSSHConnection) IsConnected() bool
- func (c *ArkSSHConnection) IsSuspended() bool
- func (c *ArkSSHConnection) RestoreConnection() error
- func (c *ArkSSHConnection) RunCommand(command *connectionsmodels.ArkConnectionCommand) (*connectionsmodels.ArkConnectionResult, error)
- func (c *ArkSSHConnection) SuspendConnection() error
Constants ¶
const (
// SSHPort is the default port for SSH connections.
SSHPort = 22
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArkSSHConnection ¶
type ArkSSHConnection struct { connections.ArkConnection // contains filtered or unexported fields }
ArkSSHConnection is a struct that implements the ArkConnection interface for SSH connections.
It provides secure Secure Shell functionality including connection management, command execution with session handling, and automatic retry mechanisms. The connection supports multiple authentication methods including password and public key authentication.
The struct maintains connection state and provides suspend/restore functionality for connection lifecycle management.
func NewArkSSHConnection ¶
func NewArkSSHConnection() *ArkSSHConnection
NewArkSSHConnection creates a new instance of ArkSSHConnection.
Creates and initializes a new SSH connection instance with default settings. The connection is created in a disconnected state and must be explicitly connected using the Connect method before use.
Returns a pointer to the newly created ArkSSHConnection instance with isConnected and isSuspended set to false, and a logger configured for SSH operations.
Example:
conn := NewArkSSHConnection() err := conn.Connect(connectionDetails) if err != nil { // handle connection error }
func (*ArkSSHConnection) Connect ¶
func (c *ArkSSHConnection) Connect(connectionDetails *connectionsmodels.ArkConnectionDetails) error
Connect establishes an SSH connection using the provided connection details.
Establishes a secure SSH connection to the target machine using the provided connection details. The method supports multiple authentication methods including password authentication and public key authentication (both from file and content). It handles automatic retry logic for connection failures.
If the connection is already established, this method returns immediately without error. The method uses the default SSH port (22) if no port is specified in the connection details.
The method supports three authentication methods in order of precedence: 1. Password authentication (if password is provided) 2. Private key file authentication (if PrivateKeyFilepath is provided) 3. Private key content authentication (if PrivateKeyContents is provided)
Parameters:
- connectionDetails: Connection configuration including address, port, credentials, retry settings, and authentication information
Returns an error if the connection cannot be established, including cases where credentials are missing, private key files cannot be read or parsed, or the SSH connection fails.
The method supports automatic retry with configurable retry count and tick period. Connection failures are detected and retried up to the specified limit.
Example:
details := &connectionsmodels.ArkConnectionDetails{ Address: "linux-server.example.com", Port: 22, Credentials: &connectionsmodels.ArkConnectionCredentials{ User: "username", Password: "password", }, ConnectionRetries: 3, RetryTickPeriod: 5, } err := conn.Connect(details)
func (*ArkSSHConnection) Disconnect ¶
func (c *ArkSSHConnection) Disconnect() error
Disconnect closes the SSH connection.
Closes the active SSH client connection and cleans up the connection resources. If the connection is not currently established, this method returns immediately without error.
The method attempts to close the SSH client gracefully. If the client closure fails, a warning is logged but the method continues to clean up the connection state.
After successful completion, the connection state is reset to disconnected and not suspended.
Returns an error only in exceptional circumstances. Client closure errors are logged as warnings but do not cause the method to fail.
Example:
err := conn.Disconnect() if err != nil { // handle disconnect error (rare) }
func (*ArkSSHConnection) IsConnected ¶
func (c *ArkSSHConnection) IsConnected() bool
IsConnected checks if the SSH connection is established.
Returns the current connection state indicating whether an SSH connection has been successfully established and is ready for use. This does not check the network connectivity, only the internal connection state.
Returns true if the connection is established, false otherwise.
Example:
if !conn.IsConnected() { err := conn.Connect(connectionDetails) if err != nil { // handle connection error } }
func (*ArkSSHConnection) IsSuspended ¶
func (c *ArkSSHConnection) IsSuspended() bool
IsSuspended checks if the SSH connection is suspended.
Returns the current suspension state of the connection. When suspended, the connection will refuse to execute commands even if the underlying SSH connection is still active.
Returns true if the connection is currently suspended, false otherwise.
Example:
if conn.IsSuspended() { // Connection is suspended, restore before running commands conn.RestoreConnection() }
func (*ArkSSHConnection) RestoreConnection ¶
func (c *ArkSSHConnection) RestoreConnection() error
RestoreConnection restores the SSH connection.
Restores a previously suspended connection, allowing command execution to resume. This method clears the suspended state without affecting the underlying SSH connection.
Returns nil as this operation always succeeds.
Example:
err := conn.RestoreConnection() // Commands can now be executed again
func (*ArkSSHConnection) RunCommand ¶
func (c *ArkSSHConnection) RunCommand(command *connectionsmodels.ArkConnectionCommand) (*connectionsmodels.ArkConnectionResult, error)
RunCommand executes a command on the connected system.
Executes the specified command on the remote machine through the established SSH connection. The method creates a new SSH session for each command execution, captures stdout and stderr output, and handles exit status detection.
The method validates that the connection is active and not suspended before execution. Commands that return a different exit code than expected will result in an error.
Session management is handled automatically - a new session is created for each command execution and cleaned up afterward. The method properly handles SSH exit errors to extract the actual exit status from the remote command.
Parameters:
- command: The command configuration including the command string and expected return code for validation
Returns the command execution result containing stdout, stderr, and return code, or an error if the command cannot be executed, the session cannot be created, or the command returns an unexpected return code.
Example:
cmd := &connectionsmodels.ArkConnectionCommand{ Command: "ls -la /home", ExpectedRC: 0, } result, err := conn.RunCommand(cmd) if err != nil { // handle execution error } fmt.Printf("Output: %s\n", result.Stdout)
func (*ArkSSHConnection) SuspendConnection ¶
func (c *ArkSSHConnection) SuspendConnection() error
SuspendConnection suspends the SSH connection.
Marks the connection as suspended without actually closing the underlying SSH connection. When suspended, the connection will refuse to execute commands until it is restored using RestoreConnection.
This is useful for temporarily disabling command execution while keeping the underlying network connection alive.
Returns nil as this operation always succeeds.
Example:
err := conn.SuspendConnection() // Commands will now fail until RestoreConnection is called