Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/expel-io/vault-plugin-database-cloudsql/cloudsql v0.0.0-00010101000000-000000000000
github.com/hashicorp/go-hclog v1.5.0
github.com/hashicorp/go-plugin v1.5.2
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/vault v1.10.3
github.com/hashicorp/vault/api v1.9.1
github.com/hashicorp/vault/sdk v0.10.2
Expand Down Expand Up @@ -47,7 +48,6 @@ require (
github.com/hashicorp/go-secure-stdlib/plugincontainer v0.2.2 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
Expand Down