connections

package
v0.0.0-...-24fabdb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MockDefaultPort = 555
	MockProtocol    = MOCK
)
View Source
const (
	SSHDefaultPort = 22
	SSHProtocol    = SSH
)

Variables

View Source
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")
)
View Source
var (
	Pool               ConnectionPool // Our shared connection pool
	TTL                int            // Time To Live in number of minutes
	ErrConnectionFound = errors.New("connection found in pool")
)
View Source
var ErrInvalidAuthType = fmt.Errorf("invalid auth type")

Functions

func GetLastBufferLine

func GetLastBufferLine(buf *bytes.Buffer) string

Types

type AuthMethod

type AuthMethod struct {
	ID       int
	Name     string
	AuthType string
	Proto    Protocol
	Data     []byte
}

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

type Buffers struct {
	User     string
	Hostname string
	Results  *bytes.Buffer
	Logs     *bytes.Buffer
}

func NewBuffers

func NewBuffers(hostname string, results, logs *bytes.Buffer) Buffers

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) Clear

func (b Buffers) Clear()

Clear resets both the Results and Logs buffers.

func (Buffers) ClearLogs

func (b Buffers) ClearLogs()

ClearLogs resets the Logs buffer.

func (Buffers) ClearResults

func (b Buffers) ClearResults()

ClearResults resets the Results buffer.

func (Buffers) Log

func (b Buffers) Log(eventTime time.Time, txt string)

Logs sends the returned connection data to the Server.Logs buffer.

func (Buffers) PrintResults

func (b Buffers) PrintResults(eventTime time.Time, result string, err error)

PrintResults adds the formated result to the Server.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) Close

func (p *ConnectionPool) Close(hostname string, force bool) error

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) Open

func (c *MockConnector) Open(addr string, bufs Buffers) error

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) Run

func (c MockConnector) Run(bufs Buffers, cmd, exp string) error

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
const (
	INVALID Protocol = 0
	SSH     Protocol = 1
	RDP     Protocol = 2
	TELNET  Protocol = 3
	REST    Protocol = 4
	K8S     Protocol = 5
	MOCK    Protocol = 6
)

func StringToProtocol

func StringToProtocol(proto string) Protocol

StringToProtocal parses a string into a Protocol. Returns 0 if proto is an invalid Protocol.

func (Protocol) String

func (p Protocol) String() string

String converts the Protocol into a string. SSH => "ssh", RDP => "rdp", etc.

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) Run

func (c *SSHConnector) Run(bufs Buffers, cmd string, exp string) error

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

type Server struct {
	Name     string
	Hostname string
	IP       net.IP
	Port     int
	UseIP    bool
	Connector
	Buffers
}

func NewServer

func NewServer(hostname string, port int, results, logs *bytes.Buffer) (Server, error)

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

func (s Server) GetAddr() string

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

func (s Server) GetHostAddr() string

GetAddr returns the host address to use, without a port. Returns "hostname" or "ip".

func (Server) GetIP

func (s Server) GetIP() string

GetIP returns Server.ip as a string.

func (Server) IsEmpty

func (s Server) IsEmpty() bool

IsEmpty returns true of Server.hostname is not set.

func (Server) IsValid

func (s Server) IsValid() bool

IsValid retuns true if all fields needed to connect to a server are not nil or empty.

func (Server) Run

func (s Server) Run(cmd, exp string) error

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

func (s *Server) SetConnector(connector Connector) error

SetConnector sets the Connector interface to be used for connecting to the server. MockConnector, SSHConnector, etc. server.SetConnector(&SSHConnector{})

func (*Server) SetHostname

func (s *Server) SetHostname(hostname string) error

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

func (s *Server) SetIP(ip string) error

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) SetName

func (s *Server) SetName(name string) error

SetName sets the display name for the server.

func (*Server) SetPort

func (s *Server) SetPort(port int) error

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

func (s *Server) SetUseIP(flag bool)

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

func (s Server) TestConnection() error

TestConnection tries to open a connection to the server and sends an echo command to validate connectivity and basic access.

func (Server) Validate

func (s Server) Validate() error

IsValid retuns an error if a field needed to connect to a server is nil or empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL