Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register postgres drivers with unique names #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package cloudsql

import (
"context"
"fmt"

"cloud.google.com/go/cloudsqlconn"
"cloud.google.com/go/cloudsqlconn/postgres/pgxv4"
"github.com/hashicorp/vault/plugins/database/postgresql"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/hashicorp/vault/sdk/database/helper/connutil"
"github.com/pkg/errors"

uuid "github.com/hashicorp/go-uuid"
)

// CloudSQL implements Vault's Database interface
Expand Down Expand Up @@ -111,9 +114,14 @@ func newPostgresDatabase(dbType DBType, connProducer *connutil.SQLConnectionProd
// attribute 'sslmode=disable' is required. even though the sslmode parameter is set to disable,
// the Cloud SQL Auth proxy does provide an encrypted connection.
// See: https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#connect-to-proxy
cleanup, err := pgxv4.RegisterDriver(dbType.String(), cloudsqlconn.WithIAMAuthN())
driverSuffix, err := uuid.GenerateUUID()
driverName := fmt.Sprintf("postgres-%s", driverSuffix)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to generate unique 'postgres' driver name.")
}
cleanup, err := pgxv4.RegisterDriver(driverName, cloudsqlconn.WithIAMAuthN())
if err != nil {
return nil, nil, nil, errors.Wrap(err, "failed to register 'postgres' driver with 'cloud-sql-go-connector'")
return nil, nil, nil, errors.Wrapf(err, "failed to register 'postgres' driver with name %s.", driverName)
}

// delegate to vault's original postgres backend
Expand Down
5 changes: 3 additions & 2 deletions cloudsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudsql
import (
"database/sql"
"reflect"
"strings"
"testing"
"unsafe"

Expand Down Expand Up @@ -34,11 +35,11 @@ func TestNewDelegatesToVaultPostgresPlugin(t *testing.T) {
// assert that the driver was registered correctly
foundDriver := false
for _, v := range sql.Drivers() {
if v == Postgres.String() {
if strings.HasPrefix(v, "postgres-") {
foundDriver = true
}
}
if !foundDriver {
t.Error("expected the driver 'cloudsql-postgres' to be registered but was not found")
t.Error("expected a driver prefixed with 'postgres-' to be registered but was not found")
}
}
4 changes: 2 additions & 2 deletions cmd/vault-plugin-database-cloudsql/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"database/sql"
"os"
"strings"
"testing"
"time"

"github.com/expel-io/vault-plugin-database-cloudsql/cloudsql"
"github.com/hashicorp/go-plugin"
)

Expand All @@ -29,7 +29,7 @@ func TestServe(t *testing.T) {
// assert that the driver was registered correctly
foundDriver := false
for _, v := range sql.Drivers() {
if v == cloudsql.Postgres.String() {
if strings.HasPrefix(v, "postgres-") {
foundDriver = true
}
}
Expand Down