GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - progbits/pg-secrets-driver: A credential rotation aware Postgres connector.

BranchesTags
Open more actions menu

Latest commit

 

History

23 Commits
23 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
.gitignore
.gitignore
 
 
LICENCE
LICENCE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
connector.go
connector.go
 
 
connector_test.go
connector_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 

Repository files navigation

pg-secrets-driver

Build

A Postgres database driver that supports credential rotation.

Getting Started

The following example demonstrates a simple CredentialsProvider implementation, configured with a fixed list of passwords. The implementation cycles though the fixed list of passwords, trying each in turn, until it is able to establish a connection successfully.

In actual usage, it is likely that a CredentialsProvider implementation would source the dynamic components of the data source name (e.g. the password) from either the environment of the process or a service such as AWS SecretsManager or HashiCorp Vault.

package main

import (
	"database/sql"
	conn "github.com/progbits/pg-secrets-driver"
	"log"
	"net/url"
)

type TestCredentialsProvider struct {
	count     int
	passwords []string
}

func (p *TestCredentialsProvider) GetDataSourceName() (string, error) {
	withPassword := func(password string) string {
		dsn, _ := url.Parse("postgresql://localhost/postgres")
		query := dsn.Query()
		query.Add("user", "postgres")
		query.Add("password", password)
		query.Add("sslmode", "disable")
		dsn.RawQuery = query.Encode()
		return dsn.String()
	}

	password := p.passwords[p.count]
	p.count++

	fmt.Printf("Returning data source name with password: %s\n", password)

	return withPassword(password), nil
}

func (p *TestCredentialsProvider) Retries() int {
	return len(p.passwords)
}

func main() {
	passwords := []string{"foo", "bar", "baz", "password", "pa$$w0rd", "p@ssw0rd"}
	provider := &TestCredentialsProvider{
		passwords: passwords,
	}
	connector := conn.NewPgSecretsConnector(provider)

	db := sql.OpenDB(connector)
	_, err := db.Exec("SELECT 1")
	if err != nil {
		log.Print(err)
	}
}

The example code above can be found in the example directory.

To run the example, start a new Postgres instance with the superuser password pa$$w0rd, the 5th value in the list held by TestCredentialsProvider:

docker run -e 'POSTGRES_PASSWORD=pa$$w0rd' -d -p 5432:5432 postgres:14.3

The example can then be run using make:

make run-example

About

A credential rotation aware Postgres connector.

Topics

awscredentialsgolangpostgressecret-managementsecrets-managervault

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.