Skip to content

Commit b9cb9d5

Browse files
David MontoyaIsmail Ahmad
andcommitted
register postgres drivers with unique names
Co-authored-by: Ismail Ahmad <[email protected]>
1 parent 02b1b95 commit b9cb9d5

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

cloudsql.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package cloudsql
22

33
import (
44
"context"
5+
"fmt"
56

67
"cloud.google.com/go/cloudsqlconn"
78
"cloud.google.com/go/cloudsqlconn/postgres/pgxv4"
89
"github.com/hashicorp/vault/plugins/database/postgresql"
910
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
1011
"github.com/hashicorp/vault/sdk/database/helper/connutil"
1112
"github.com/pkg/errors"
13+
14+
uuid "github.com/hashicorp/go-uuid"
1215
)
1316

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

119127
// delegate to vault's original postgres backend

cloudsql_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloudsql
33
import (
44
"database/sql"
55
"reflect"
6+
"strings"
67
"testing"
78
"unsafe"
89

@@ -34,11 +35,11 @@ func TestNewDelegatesToVaultPostgresPlugin(t *testing.T) {
3435
// assert that the driver was registered correctly
3536
foundDriver := false
3637
for _, v := range sql.Drivers() {
37-
if v == Postgres.String() {
38+
if strings.HasPrefix(v, "postgres-") {
3839
foundDriver = true
3940
}
4041
}
4142
if !foundDriver {
42-
t.Error("expected the driver 'cloudsql-postgres' to be registered but was not found")
43+
t.Error("expected a driver prefixed with 'postgres-' to be registered but was not found")
4344
}
4445
}

cmd/vault-plugin-database-cloudsql/serve_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"context"
55
"database/sql"
66
"os"
7+
"strings"
78
"testing"
89
"time"
910

10-
"github.com/expel-io/vault-plugin-database-cloudsql/cloudsql"
1111
"github.com/hashicorp/go-plugin"
1212
)
1313

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

0 commit comments

Comments
 (0)