dynamicsqldriver

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MIT Imports: 5 Imported by: 0

README

dynamicsqldriver

CI Go Reference

dynamicsqldriver is a SQL driver implementation for Go that wraps your favorite SQL driver and adds the ability to generate credentials any time the SQL package opens up a new connection. This is particularly useful with secrets management systems like HashiCorp Vault that generate and manage database users on your behalf.

Create your own implementation of dynamicsqldriver.CredentialsGenerator and set the username and password portion of your database connection string to genusername and genpassword respectively. dynamicsqldriver will call your generator when a new database connection is needed and then replace those values in the connection string before passing it along to your favorite SQL driver.

Usage

package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/go-sql-driver/mysql"
	"github.com/google/uuid"
	"github.com/jaredpetersen/dynamicsqldriver"
)

// Generator is an implementation of dynamicsqldriver.CredentialsGenerator that generates credentials and caches them.
type Generator struct {
	Cache dynamicsqldriver.Credentials
}

func (g *Generator) Generate() (dynamicsqldriver.Credentials, error) {
	now := time.Now()
	if now.After(g.Cache.Expiration) || now.Equal(g.Cache.Expiration) {
		g.Cache = dynamicsqldriver.Credentials{
			Username:   uuid.NewString(),
			Password:   uuid.NewString(),
			Expiration: now.Add(30 * time.Minute),
		}
	}

	return g.Cache, nil
}

func main() {
	generator := Generator{}

	dbHost := "localhost:3306"
	dbName := "mydb"

	// Specify "genusername" and "genpassword" to have the values replaced by the generator function
	dsn := fmt.Sprintf("genusername:genpassword@tcp(%s)/%s?parseTime=true", dbHost, dbName)
	db := sql.OpenDB(dynamicsqldriver.NewConnector(mysql.MySQLDriver{}, &generator, dsn))
}

Install

go get github.com/jaredpetersen/dynamicsqldriver

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector implements driver.Connector so that the driver can be used with sql.OpenDB.

func NewConnector

func NewConnector(driver driver.Driver, generator CredentialsGenerator, dsn string) Connector

NewConnector creates a new Connector.

func (Connector) Connect

func (c Connector) Connect(_ context.Context) (driver.Conn, error)

func (Connector) Driver

func (c Connector) Driver() driver.Driver

type Credentials

type Credentials struct {
	Username   string
	Password   string
	Expiration time.Time
}

Credentials stores database credentials.

type CredentialsGenerator

type CredentialsGenerator interface {
	Generate() (Credentials, error)
}

CredentialsGenerator generates new database credentials.

type Driver

type Driver struct {
	Actual               driver.Driver
	CredentialsGenerator CredentialsGenerator
}

Driver wraps a real driver.Driver while maintaining the same interface and offering support for credentials generation upon opening a new connection.

func (Driver) Open

func (d Driver) Open(dsn string) (driver.Conn, error)

Open generates new database credentials using Vault and updates the provided DSN before opening a connection using actual driver.

Jump to

Keyboard shortcuts

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