Categorygithub.com/jfcote87/sshdb
modulepackage
0.6.0
Repository: https://github.com/jfcote87/sshdb.git
Documentation: pkg.go.dev

# README

sshdb

Go Reference Build Status codecov Go Report Card

A pure go library that allows connections to remote sql databases via ssh tunnels. The package works with db packages by creating an ssh connection and a dial function for connecting to the tunnel.

install

go get -v github.com/jfcote87/sshdb

making connections

To setup connections, create a Tunnel object with the sshdb.New method using an ssh.ClientConfig (see documentation on the "golang.org/x/crypto/ssh" package) and the remote address of the ssh server. The returned Tunnel is safe for concurrent use by multiple goroutines and maintains its own pool of db connections. Thus, the New function should be called just once for each remote server. It is rarely necessary to close a Tunnel.

Create a dsn string using addresses based upon the remote server. The connections will be created on the remote ssh server to the database server. Select the appropriate driver for your database. If the database is not in included drivers, review the sshdb.Driver interface{} and the existing driver code. Essentially the passed dialer in the OpenConnector function should replace the default dialer for the db. Use the db.Connector to create a sql.DB object.

Example for creating connections may be found in the code below and the example_test.go file.

imports (
	"golang.org/x/crypto/ssh"
	"github.com/jfcote87/sshdb"
)

func main() {
	exampleCfg := &ssh.ClientConfig{
		User:            "jfcote87",
		Auth:            []ssh.AuthMethod{ssh.Password("my second favorite password")},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	}
	// New creates a "tunnel" for database connections. A Tunnel can support
	// multiple database connections.
	tunnel, err := sshdb.New(exampleCfg, remoteAddr)
	if err != nil {
		log.Fatalf("new tunnel create failed: %v", err)
	}
    // serverAddr is a valid hostname for the db server from the remote ssh server (often localhost).
	dsn := "username:dbpassword@tcp(serverAddress:3306)/schemaName?parseTime=true"

	// open connector and then new DB
	connector, err := tunnel.OpenConnector(mysql.TunnelDriver, dsn)
	if err != nil {
		return fmt.Errorf("open connector failed %s - %v", dsn, err)
	}
	db := sql.OpenDB(connector)
}

testing

$ go test ./...

# Packages

Package mssql provides for mssql connection via the sshdb package.
Package mysql provide for mysql connections via the sshdb package.
Package oracle provide for mysql connections via the sshdb package.
Package pgx provides for ssh postgres connections via the github.com/jackc/pgx package.
Package pgxv4 provides for ssh postgres connections via the "github.com/jackc/pgx/v4".

# Functions

New returns a Tunnel based upon the ssh clientConfig for creating new connectors/connections via an ssh client connection.
RegisterDriver associates a database's Driver interface with the key.

# Structs

ConfigError used to describe errors when opening DBs based upon a Config.
Datasource defines a database connection using the Driver name and a connection string for use by the underlying sql driver.
Tunnel manages an ssh client connections and creates and tracks db connections made through the client.
TunnelConfig describes an ssh connection to a remote host and the databases accessed via the connection.

# Interfaces

Dialer creates a net.Conn via the tunnel's ssh client.
Driver creates a Connector for a specific database type using the dialer passed from a Tunnel and a dsn string that defines a connection from the remote server to the desired database.

# Type aliases

DialerFunc allows a func to fulfill the Dialer interface.