Documentation
¶
Overview ¶
Package cdb provides a database abstraction layer for storing and retrieving SSH connection information for the sshcm utility.
Index ¶
- Constants
- Variables
- func IsValidDefault(name string) bool
- func IsValidIdOrNickname(s string) bool
- func IsValidProperty(property string) bool
- func ValidateDbSchemaVersion(version string) error
- func ValidateId(id string) error
- func ValidateNickname(nickname string) error
- type Connection
- func (c Connection) Delete() error
- func (c Connection) String() string
- func (c Connection) Update() error
- func (c Connection) Validate() error
- func (c Connection) WriteCSV(w *csv.Writer) error
- func (c Connection) WriteJSON(w io.Writer) error
- func (c Connection) WriteLineLong(w io.Writer) error
- func (c Connection) WriteLineShort(w io.Writer) error
- func (c Connection) WriteRecordLong(w io.Writer) error
- func (c Connection) WriteRecordShort(w io.Writer) error
- type ConnectionDB
- func (conndb *ConnectionDB) Add(c *Connection) (int64, error)
- func (conndb *ConnectionDB) CheckDbHealth() error
- func (conndb ConnectionDB) Close()
- func (conndb *ConnectionDB) Exists(id int64) (bool, error)
- func (conndb *ConnectionDB) ExistsByProperty(property string, value string) (bool, error)
- func (conndb *ConnectionDB) Get(id int64) (Connection, error)
- func (conndb *ConnectionDB) GetAll() ([]*Connection, error)
- func (conndb *ConnectionDB) GetByIdOrNickname(arg string) (Connection, error)
- func (conndb *ConnectionDB) GetByProperty(property string, value string) (Connection, error)
- func (conndb *ConnectionDB) GetDbSchemaVersion() (string, error)
- func (conndb *ConnectionDB) GetDefault(name string) (string, error)
- func (conndb *ConnectionDB) InitializeDb(version string) error
- func (conndb *ConnectionDB) Search(search string) ([]*Connection, error)
- func (conndb *ConnectionDB) SetDefault(name string, value string) error
- type DbConnIface
Constants ¶
const SchemaVersion = "v1.1"
Variables ¶
var ErrConnFromDbInvalid = errors.New("connection from DB is invalid")
var ErrConnIdZero = errors.New("connection id is zero")
var ErrConnNoDb = errors.New("connection does not have a parent db attached")
var ErrConnNoHost = errors.New("connection does not have a host attached")
var ErrConnNoId = errors.New("connection does not have an id attached")
var ErrConnNoNickname = errors.New("connection does not have a nickname attached")
var ErrConnectionNotFound = errors.New("connection not found")
var ErrDuplicateNickname = errors.New("duplicate nickname")
var ErrIdNotExist = errors.New("connection id does not exist")
var ErrInvalidConnectionProperty = errors.New("invalid connection property")
var ErrInvalidDefault = errors.New("invalid default")
var ErrInvalidId = errors.New("invalid id")
var ErrInvalidIdOrNickname = errors.New("invalid id or nickname")
var ErrInvalidNickname = errors.New("invalid nickname")
var ErrNickNameNotExist = errors.New("connection nickname does not exist")
var ErrNicknameLetter = errors.New("nickname does not begin with a letter")
var ErrPropertyInvalid = errors.New("property is invalid")
var ErrSchemaNoUpgrade = errors.New("conndb: schema too old and can't be upgraded")
var ErrSchemaTooNew = errors.New("conndb: schema version too new")
var ErrSchemaUpgradeNeeded = errors.New("conndb: schema upgrade needed")
var ErrSchemaVerInvalid = errors.New("conndb: invalid schema version")
DB schema errors
var ErrUnsupportedSqlDriver = errors.New("sql driver not supported")
var ListViewColumnWidths = map[string]int{
"id": 4,
"nickname": 15,
"user": 10,
"host": 15,
"description": 20,
"args": 10,
"identity": 10,
"command": 10,
}
var ValidDefaults = [4]string{
"args",
"command",
"identity",
"user",
}
var ValidProperties = [7]string{
"nickname",
"host",
"user",
"description",
"args",
"identity",
"command",
}
Functions ¶
func IsValidDefault ¶
IsValidDefault checks the passed default property name against a list of valid default properties. This is similar to IsValidProperty, except there are fewer defaults.
Returns true if the name is valid, false otherwise.
func IsValidIdOrNickname ¶
IsValidIdOrNickname returns true if the passed string is a valid id or nickname. This is a smoke test meant to be used to simplify conditionals around validating command line arguments.
func IsValidProperty ¶
IsValidProperty checks the passed connection property name against a list of valid properties.
Returns true if the name is valid, false otherwise.
func ValidateDbSchemaVersion ¶
ValidateDbSchemaVersion runs checks against the passed schema version to see if it's supported by this package. It will return nil if the DB is usable, and various errors otherwise:
ErrSchemaVerInvalid - Unrecoverable. Something unexpected happened. ErrSchemaTooOld - Recoverable. The caller should call upgradeDbSchema() to attempt to upgrade the DB schema to the latest version. ErrSchemaTooNew - Unrecoverable. This package (or the calling tool) needs to be upgraded. Others - Likely unrecoverable. Other errors returned by called funcs.
func ValidateId ¶
ValidateId runs checks against the passed id as a string.
If the tests pass and the id is valid, nil is returned. If any test fails, a relevant error is returned.
This is implemented as a string, as it is intended to be used in circumstances where a string might contain an id, or what is expected to be an id. The primary situation where this occurs is validating command line arguments - because a user may choose to make a connection by id or nickname, this func is the frontline validation of that user input.
func ValidateNickname ¶
ValidateNickname runs checks against the passed nickname string.
If the tests pass and the nickname is valid, nil is returned. If any test fails, a relevant error is returned.
Types ¶
type Connection ¶
type Connection struct {
Id int64 // unique connection id
Nickname string // unique connection nickname
Host string // connection-specific host name/IP address
User string // connection-specific user name
Description string // connection-specific description (hopefully friendly)
Args string // connection-specific arguments to pass to SSH Command
Identity string // connection-specific OpenSSH-style identity string (ex. path or name)
Command string // connection-specific Command to run (ex. sftp)
Binary string // to be deleted
// contains filtered or unexported fields
}
A Connection is an SSH connection, as stored in a ConnectionDB. A Connection could originate from a ConnectionDB (ex. returned by a SELECT from the underlying SQL DB), or could be destined for one (ex. for later INSERTion).
If a connection originated from a ConnectionDB, a pointer to it will be stored in db. This allows methods such as Update to perform database operations from the context of the Connection. Connections originating from a ConnectionDB will also have an Id set.
Connections created detached from a ConnectionDB will not have a db or Id set. This is a primitive safety mechanism to prevent arbitrary writes to the database bypassing validations that avoid throwing SQL errors (like checking for Nickname uniqueness).
func NewConnection ¶
func NewConnection() Connection
NewConnection will create a new, empty Connection struct. Using this function is preferred vs. creating a new struct via literal, as the format of the struct may change in the future.
func (Connection) Delete ¶
func (c Connection) Delete() error
Delete removes a connection from the underlying SQL database. It will return nil if the operation succeeeded and err otherwise. Several checks are implemented that return package-specific errors. These checks are simple and only cover obvious situations that will cause SQL query exceptions.
func (Connection) String ¶
func (c Connection) String() string
String returns a string containing the connection nickname and ID, in the format: "nickname (id)".
func (Connection) Update ¶
func (c Connection) Update() error
Update updates an existing connection in the SQL database. It will return nil if the operation succeeeded and err otherwise. Several checks are implemented that return package-specific errors. These checks are simple and only cover obvious situations that will cause SQL query exceptions.
func (Connection) Validate ¶
func (c Connection) Validate() error
Currently, the only check performed is whether the nickname is in a valid format (ex. starts with a letter). Additional checks may be added in the future.
func (Connection) WriteCSV ¶
func (c Connection) WriteCSV(w *csv.Writer) error
WriteCSV will write the connection in CSV format to the passed writer. An error will be returned if one occurs, otherwise error will be nil.
func (Connection) WriteJSON ¶
func (c Connection) WriteJSON(w io.Writer) error
WriteJSON will write the connection in JSON format to the passed writer. An error will be returned if one occurs, otherwise error will be nil.
func (Connection) WriteLineLong ¶
func (c Connection) WriteLineLong(w io.Writer) error
WriteLineLong writes a list format, single-line string to the passed writer interface. This is most likely used in listing connections. This func will write all connection properties. An error will be returned if one occurs, otherwise error will be nil.
func (Connection) WriteLineShort ¶
func (c Connection) WriteLineShort(w io.Writer) error
WriteLineShort writes a list format, single-line string to the passed writer interface. This is most likely used in listing connections. This func will write only some connection properties. An error will be returned if one occurs, otherwise error will be nil.
func (Connection) WriteRecordLong ¶
func (c Connection) WriteRecordLong(w io.Writer) error
WriteRecordLong writes a record-format, multi-line string to the passed writer interface. This func will write all connection properties. An error will be returned if one occurs, otherwise error will be nil.
func (Connection) WriteRecordShort ¶
func (c Connection) WriteRecordShort(w io.Writer) error
WriteRecordShort writes a record-format, multi-line string to the passed writer interface. This func will write only some connection properties. An error will be returned if one occurs, otherwise error will be nil.
type ConnectionDB ¶
type ConnectionDB struct {
// contains filtered or unexported fields
}
func Connect ¶
func Connect(driver string, path string) (ConnectionDB, error)
Connect connects to a database and returns a ConnectionDB. In the event an error occurs, it'll be returned.
func (*ConnectionDB) Add ¶
func (conndb *ConnectionDB) Add(c *Connection) (int64, error)
func (*ConnectionDB) CheckDbHealth ¶
func (conndb *ConnectionDB) CheckDbHealth() error
CheckDbHealth runs health checks on the connection DB and returns an error if there is an issue.
func (ConnectionDB) Close ¶
func (conndb ConnectionDB) Close()
Close Gracefully closes a connection to a database.
func (*ConnectionDB) ExistsByProperty ¶
func (conndb *ConnectionDB) ExistsByProperty(property string, value string) (bool, error)
func (*ConnectionDB) Get ¶
func (conndb *ConnectionDB) Get(id int64) (Connection, error)
func (*ConnectionDB) GetAll ¶
func (conndb *ConnectionDB) GetAll() ([]*Connection, error)
func (*ConnectionDB) GetByIdOrNickname ¶
func (conndb *ConnectionDB) GetByIdOrNickname(arg string) (Connection, error)
GetByIdOrNickname looks up a connection by id or nickname, then returns a Connection struct. If the look up succeeded, err will be nil and it can be assumed that the Connection is safe to use.
func (*ConnectionDB) GetByProperty ¶
func (conndb *ConnectionDB) GetByProperty(property string, value string) (Connection, error)
func (*ConnectionDB) GetDbSchemaVersion ¶
func (conndb *ConnectionDB) GetDbSchemaVersion() (string, error)
GetDbSchemaVersion will read and return the schema version from an sshcm Sqlite database file. This does not validate whether the schema version is usable by this package, it simply reads the version from the DB and returns the result.
func (*ConnectionDB) GetDefault ¶
func (conndb *ConnectionDB) GetDefault(name string) (string, error)
GetDefault retrieves a program default property from the connection database.
If the passed property name is not valid, ErrInvalidDefault will be returned.
func (*ConnectionDB) InitializeDb ¶
func (conndb *ConnectionDB) InitializeDb(version string) error
InitializeDb will populate an empty Sqlite file with the tables and default values sshcm expects. The function will return nil upon completion or an error when an exception occurs.
func (*ConnectionDB) Search ¶
func (conndb *ConnectionDB) Search(search string) ([]*Connection, error)
func (*ConnectionDB) SetDefault ¶
func (conndb *ConnectionDB) SetDefault(name string, value string) error
SetDefault updates a program default property in the connection database.
If the passed property name is not valid, ErrInvalidDefault will be returned.