Documentation
¶
Index ¶
- Constants
- Variables
- func GetLastBufferLine(buf *bytes.Buffer) string
- type AuthMethod
- type Buffers
- type Connection
- type ConnectionPool
- type Connector
- type MockConnector
- func (c *MockConnector) Close(force bool) error
- func (c *MockConnector) CloseSession() error
- func (c MockConnector) DefaultPort() int
- func (c *MockConnector) ErrOnConnectionClose(do bool)
- func (c *MockConnector) ErrOnConnectionOpen(do bool)
- func (c *MockConnector) ErrOnSessionClose(do bool)
- func (c *MockConnector) ErrOnSessionOpen(do bool)
- func (c MockConnector) GetUser() string
- func (c MockConnector) IsActive() bool
- func (c MockConnector) IsConnected() bool
- func (c MockConnector) IsEmpty() bool
- func (c MockConnector) IsValid() bool
- func (c *MockConnector) Open(addr string, bufs Buffers) error
- func (c *MockConnector) OpenSession(bufs Buffers) error
- func (c MockConnector) Protocol() Protocol
- func (c MockConnector) Run(bufs Buffers, cmd, exp string) error
- func (c *MockConnector) SetUser(username string) error
- func (c MockConnector) TestConnection(bufs Buffers) error
- func (c MockConnector) Validate() error
- type Protocol
- type SSHConnector
- func (c *SSHConnector) AddKeyAuth(key ssh.Signer) error
- func (c *SSHConnector) AddPasswordAuth(password string)
- func (c *SSHConnector) Close(force bool) error
- func (c *SSHConnector) CloseSession() error
- func (c *SSHConnector) DefaultPort() int
- func (c *SSHConnector) GetUser() string
- func (c *SSHConnector) IsActive() bool
- func (c *SSHConnector) IsConnected() bool
- func (c *SSHConnector) IsEmpty() bool
- func (c *SSHConnector) IsValid() bool
- func (c *SSHConnector) Open(addr string, bufs Buffers) error
- func (c *SSHConnector) OpenSession(bufs Buffers) error
- func (c *SSHConnector) ParseKey(privateKey []byte) error
- func (c *SSHConnector) ParseKeyWithPassphrase(privateKey, passphrase []byte) error
- func (c *SSHConnector) Protocol() Protocol
- func (c *SSHConnector) Run(bufs Buffers, cmd string, exp string) error
- func (c *SSHConnector) SetName(name string) error
- func (c *SSHConnector) SetUser(username string) error
- func (c *SSHConnector) TestConnection(bufs Buffers) error
- func (c SSHConnector) Validate() error
- type Server
- func (s Server) GetAddr() string
- func (s Server) GetHostAddr() string
- func (s Server) GetIP() string
- func (s Server) IsEmpty() bool
- func (s Server) IsValid() bool
- func (s Server) Run(cmd, exp string) error
- func (s *Server) SetConnector(connector Connector) error
- func (s *Server) SetHostname(hostname string) error
- func (s *Server) SetIP(ip string) error
- func (s *Server) SetName(name string) error
- func (s *Server) SetPort(port int) error
- func (s *Server) SetUseIP(flag bool)
- func (s Server) TestConnection() error
- func (s Server) Validate() error
Constants ¶
const ( MockDefaultPort = 555 MockProtocol = MOCK )
const ( SSHDefaultPort = 22 SSHProtocol = SSH )
Variables ¶
var ( // Invalid Connector Errors ErrInvalidEmtpyUser = errors.New("user is empty") ErrInvalidNoAuthMethod = errors.New("no AuthMethod set") // Client Errors ErrNotConnected = errors.New("not connected") // Session Errors ErrSessionActive = errors.New("cannot close connection, session active") // Run Errors ErrEmtpyCmd = errors.New("cmd is empty") ErrEmtpyExp = errors.New("exp is empty") )
var ( Pool ConnectionPool // Our shared connection pool TTL int // Time To Live in number of minutes ErrConnectionFound = errors.New("connection found in pool") )
var ErrInvalidAuthType = fmt.Errorf("invalid auth type")
Functions ¶
func GetLastBufferLine ¶
Types ¶
type AuthMethod ¶
func NewAuthMethod ¶
func NewAuthMethod(name string) AuthMethod
func ParseAuthMethod ¶
func ParseAuthMethod(data db.AuthMethodData) (AuthMethod, error)
func (*AuthMethod) SSHKey ¶
func (a *AuthMethod) SSHKey(name string, key []byte)
func (*AuthMethod) SSHPassword ¶
func (a *AuthMethod) SSHPassword(name string, password []byte)
func (AuthMethod) ToAuthMethodData ¶
func (a AuthMethod) ToAuthMethodData(passphrase []byte) (db.AuthMethodData, error)
func (AuthMethod) ToSSHAuthMethod ¶
func (a AuthMethod) ToSSHAuthMethod(passphrase []byte) (ssh.AuthMethod, error)
type Buffers ¶
func NewBuffers ¶
NewBuffers creates a new Buffers object with the hostname, Results buffer, and Logs buffer set. You will need to set the User field before using the Buffers object.
func (Buffers) ClearResults ¶
func (b Buffers) ClearResults()
ClearResults resets the Results buffer.
type Connection ¶
type Connection struct { *Server // contains filtered or unexported fields }
Connection holds a Server ref and our time to kill for connection cleanup.
func (*Connection) Close ¶
func (c *Connection) Close(force bool) error
Close closes the connection and removes it from the Pool. If the connection is not in the Pool, Close will return an error and will NOT try to close the connection.
func (Connection) Expired ¶
func (c Connection) Expired() bool
Expired returns true if it is currently past the Connection.killAt time.
func (Connection) Expires ¶
func (c Connection) Expires() time.Time
Expires returns the Connection.killAt time.
func (*Connection) Extend ¶
func (c *Connection) Extend(minutes int)
Extend add the specified number of minutes to the killAt time.
func (*Connection) Open ¶
func (c *Connection) Open(pool ConnectionPool) (*Connection, error)
func (*Connection) TimeOut ¶
func (c *Connection) TimeOut() error
TimeOut checks the connection to see if it is passed its killAt time. If so it will attempt to close the connection. If a connection is active TimeOut will extend the killAt time by the TTL.
type ConnectionPool ¶
type ConnectionPool map[string]*Connection // map[Open.hostame]Open
ConnectionPool holds an array of connections used to setup our shared pool.
func (ConnectionPool) CloseAll ¶
func (p ConnectionPool) CloseAll() error
CloseAll will force close all connections in the ConnectionPool. This means it will try to close the connection if it it has an active session.
func (ConnectionPool) Count ¶
func (p ConnectionPool) Count() int
func (ConnectionPool) GetConnection ¶
func (p ConnectionPool) GetConnection(server Server) *Connection
GetConnection returns a connection for the server if one exists. Returns nil if no connection is found.
func (ConnectionPool) Open ¶
func (p ConnectionPool) Open(server *Server) (*Connection, error)
Open creates a new Connection and adds it to Pool.
type Connector ¶
type Connector interface { // IsConnected returns true if there is a connection established to the server. IsConnected() bool // IsActive returns true if the Connector is being used such as if there's an open session // with the SSHConnector. IsActive() bool // Protocol returns the Protocol enum for this Connector type. Protocol() Protocol // GetUser returns the username used by this Connector. GetUser() string // DefaultPort returns the default port number used by this Connector type. DefaultPort() int // IsEmpty checks that fields populated by New contain data. IsEmpty() bool // IsValid returns true if no errors are returned by Connector.Validate(). If IsValid // returns true then you should be able to create a connection using this Connector. IsValid() bool // Validate checks each Connector field required for connecting to an endpoint are returns // an error if anything is missing or incoorect. Validate() error // TestConnection creates a connection to the server and performs a minimal command test // such as a basic echo for ssh. Logs and Results are handled the same way as with // Connector.Run(). TestConnection(bufs Buffers) error // Run executes the given cmd(command) against the server, if exp(expect) != "" performs a // match of expect against the output of the command. The output of command is sent to // Server.Log() and the expect is sent to Server.PrintResults(). Results will either be // "ok" or "failed" with the error. // Example: // Connector.Run(server, "echo 'we did it'", "we did it") // Logs Buffer // 2024/05/30 12:15:42 debian@test.home:~ we did it // Results Buffer // 2024/05/30 12:15:42: test.home...ok Run(bufs Buffers, cmd string, exp string) error // Open creates a connection to the server. Open(addr string, bufs Buffers) error // Close ends the connecton to the server. Setting force to true will close the connection // even if there is an active session. Close(force bool) error }
type MockConnector ¶
type MockConnector struct {
// contains filtered or unexported fields
}
MockConnector holds the minimal information needed for creating a mock Connector interface.
func NewMockConnector ¶
func NewMockConnector(name, username string) (MockConnector, error)
NewMockConnector creates a MockConnector to simulate connecting to a server.
func (*MockConnector) Close ¶
func (c *MockConnector) Close(force bool) error
func (*MockConnector) CloseSession ¶
func (c *MockConnector) CloseSession() error
CloseSession closes an open session.
func (MockConnector) DefaultPort ¶
func (c MockConnector) DefaultPort() int
func (*MockConnector) ErrOnConnectionClose ¶
func (c *MockConnector) ErrOnConnectionClose(do bool)
func (*MockConnector) ErrOnConnectionOpen ¶
func (c *MockConnector) ErrOnConnectionOpen(do bool)
func (*MockConnector) ErrOnSessionClose ¶
func (c *MockConnector) ErrOnSessionClose(do bool)
func (*MockConnector) ErrOnSessionOpen ¶
func (c *MockConnector) ErrOnSessionOpen(do bool)
func (MockConnector) GetUser ¶
func (c MockConnector) GetUser() string
func (MockConnector) IsActive ¶
func (c MockConnector) IsActive() bool
func (MockConnector) IsConnected ¶
func (c MockConnector) IsConnected() bool
func (MockConnector) IsEmpty ¶
func (c MockConnector) IsEmpty() bool
func (MockConnector) IsValid ¶
func (c MockConnector) IsValid() bool
func (*MockConnector) OpenSession ¶
func (c *MockConnector) OpenSession(bufs Buffers) error
OpenSession creates a new single command session.
func (MockConnector) Protocol ¶
func (c MockConnector) Protocol() Protocol
func (*MockConnector) SetUser ¶
func (c *MockConnector) SetUser(username string) error
SetUser sets the username to be used for the connection credentials.
func (MockConnector) TestConnection ¶
func (c MockConnector) TestConnection(bufs Buffers) error
func (MockConnector) Validate ¶
func (c MockConnector) Validate() error
type Protocol ¶
type Protocol int
func StringToProtocol ¶
StringToProtocal parses a string into a Protocol. Returns 0 if proto is an invalid Protocol.
type SSHConnector ¶
type SSHConnector struct { Name string // A unique name for the connector to make it easier to add to a server. Auth []ssh.AuthMethod // Each auth method will be tried in turn until one works or all fail. // AuthMethods []AuthMethod // A list of AuthMethods to be used for authentication. User string // The username to login to the server with. *ssh.Client *ssh.Session // contains filtered or unexported fields }
SSHConnector impletments the Connector interface for SSH connectivity.
func NewSSHConnector ¶
func NewSSHConnector(name, username string) (SSHConnector, error)
NewSSHConnector creates an SSHConnector struct to be used to connect via SSH to a server.
func (*SSHConnector) AddKeyAuth ¶
func (c *SSHConnector) AddKeyAuth(key ssh.Signer) error
AddKeyAuth adds an AuthMethod using the ssh private key.
func (*SSHConnector) AddPasswordAuth ¶
func (c *SSHConnector) AddPasswordAuth(password string)
AddPasswordAuth adds an AuthMethod using a password.
func (*SSHConnector) Close ¶
func (c *SSHConnector) Close(force bool) error
func (*SSHConnector) CloseSession ¶
func (c *SSHConnector) CloseSession() error
CloseSession closes an open session.
func (*SSHConnector) DefaultPort ¶
func (c *SSHConnector) DefaultPort() int
func (*SSHConnector) GetUser ¶
func (c *SSHConnector) GetUser() string
func (*SSHConnector) IsActive ¶
func (c *SSHConnector) IsActive() bool
func (*SSHConnector) IsConnected ¶
func (c *SSHConnector) IsConnected() bool
func (*SSHConnector) IsEmpty ¶
func (c *SSHConnector) IsEmpty() bool
func (*SSHConnector) IsValid ¶
func (c *SSHConnector) IsValid() bool
func (*SSHConnector) Open ¶
func (c *SSHConnector) Open(addr string, bufs Buffers) error
Open creates a connection to the server. addr is the server address to connect to in the format of "hostname:port" or "ip:port".
func (*SSHConnector) OpenSession ¶
func (c *SSHConnector) OpenSession(bufs Buffers) error
OpenSession creates a new single command session.
func (*SSHConnector) ParseKey ¶
func (c *SSHConnector) ParseKey(privateKey []byte) error
ParseKey parses the private key into a key signer and sends it to SSHConnector.AddKeyAuth().
func (*SSHConnector) ParseKeyWithPassphrase ¶
func (c *SSHConnector) ParseKeyWithPassphrase(privateKey, passphrase []byte) error
ParseKeyWithPassphrase parses a passhphrase protected private key into a key signer and sends it to SSHConnector.SetKey().
func (*SSHConnector) Protocol ¶
func (c *SSHConnector) Protocol() Protocol
func (*SSHConnector) SetName ¶
func (c *SSHConnector) SetName(name string) error
SetName sets a unique Name to make it easier to add to a server.
func (*SSHConnector) SetUser ¶
func (c *SSHConnector) SetUser(username string) error
SetUser sets the User to be used for connection credentials.
func (*SSHConnector) TestConnection ¶
func (c *SSHConnector) TestConnection(bufs Buffers) error
func (SSHConnector) Validate ¶
func (c SSHConnector) Validate() error
type Server ¶
func NewServer ¶
NewServer creates a new Server object with a display name, port, and stores the byte.Buffers to be used for results and logs output. If port is set to 0, the default port for the Connector will always be used.
func (Server) GetAddr ¶
GetAddr determines the address to connect to. Returns "hostname:port" or "ip:port". If port is set to 0, GetAddr uses protocol's default port instead.
func (Server) GetHostAddr ¶
GetAddr returns the host address to use, without a port. Returns "hostname" or "ip".
func (Server) IsValid ¶
IsValid retuns true if all fields needed to connect to a server are not nil or empty.
func (Server) Run ¶
Run passes cmd(command) and exp(expect), along with itself, on to Connector.Run to be executed. See Connector.Run() for more details.
func (*Server) SetConnector ¶
SetConnector sets the Connector interface to be used for connecting to the server. MockConnector, SSHConnector, etc. server.SetConnector(&SSHConnector{})
func (*Server) SetHostname ¶
SetHostname sets the hostname to use for the server. If the hostname is an IP it will set both Server.hostname and Server.ip to the IP and set Server.useIP to true.
func (*Server) SetIP ¶
SetIP sets the ip address to be used for connecting to the server. If Server.hostname is set to an ip, this field will automatically be set. Setting this field prevents hostname lookup. If hostname is unset, hostname will be set to ip.
func (*Server) SetPort ¶
SetPort sets the Port to be used when connecting to the server. Setting port to 0 will cause Connector.DefaultPort() to be used when a connection string is created.
func (*Server) SetUseIP ¶
SetUseIP sets the useIP field to true or false. This field is used to determine if the ip field should be used instead of the hostname for connecting to the server.
func (Server) TestConnection ¶
TestConnection tries to open a connection to the server and sends an echo command to validate connectivity and basic access.