Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jaredpetersen/dynamicsqldriver
🍬 Wrap Go SQL drivers for generated credentials support
https://github.com/jaredpetersen/dynamicsqldriver
Last synced: about 5 hours ago
JSON representation
🍬 Wrap Go SQL drivers for generated credentials support
- Host: GitHub
- URL: https://github.com/jaredpetersen/dynamicsqldriver
- Owner: jaredpetersen
- License: mit
- Created: 2022-05-14T01:01:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-14T02:46:05.000Z (over 2 years ago)
- Last Synced: 2024-06-21T13:04:22.840Z (5 months ago)
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# dynamicsqldriver
[![CI](https://github.com/jaredpetersen/dynamicsqldriver/actions/workflows/ci.yaml/badge.svg)](https://github.com/jaredpetersen/dynamicsqldriver/actions/workflows/ci.yaml)
[![Go Reference](https://pkg.go.dev/badge/github.com/jaredpetersen/dynamicsqldriver.svg)](https://pkg.go.dev/github.com/jaredpetersen/dynamicsqldriver)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
```go
package mainimport (
"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
```shell
go get github.com/jaredpetersen/dynamicsqldriver
```