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

Add usePrivateIP for cloudsql postgresql instances #26828

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/26828.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
secrets/database: Add support for GCP CloudSQL private IP's.
```
2 changes: 1 addition & 1 deletion plugins/database/mysql/connection_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (c *mySQLConnectionProducer) rewriteProtocolForGCP(inDSN string) (string, e
}

func registerDriverMySQL(driverName, credentials string) (cleanup func() error, err error) {
opts, err := connutil.GetCloudSQLAuthOptions(credentials)
opts, err := connutil.GetCloudSQLAuthOptions(credentials, false)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions sdk/database/helper/connutil/cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func (c *SQLConnectionProducer) getCloudSQLDriverType() (string, error) {
return driverType, nil
}

func (c *SQLConnectionProducer) registerDrivers(driverName string, credentials string) (func() error, error) {
func (c *SQLConnectionProducer) registerDrivers(driverName string, credentials string, usePrivateIP bool) (func() error, error) {
typ, err := c.getCloudSQLDriverType()
if err != nil {
return nil, err
}

opts, err := GetCloudSQLAuthOptions(credentials)
opts, err := GetCloudSQLAuthOptions(credentials, usePrivateIP)
if err != nil {
return nil, err
}
Expand All @@ -49,13 +49,17 @@ func (c *SQLConnectionProducer) registerDrivers(driverName string, credentials s

// GetCloudSQLAuthOptions takes a credentials JSON and returns
// a set of GCP CloudSQL options - always WithIAMAUthN, and then the appropriate file/JSON option.
func GetCloudSQLAuthOptions(credentials string) ([]cloudsqlconn.Option, error) {
func GetCloudSQLAuthOptions(credentials string, usePrivateIP bool) ([]cloudsqlconn.Option, error) {
opts := []cloudsqlconn.Option{cloudsqlconn.WithIAMAuthN()}

if credentials != "" {
opts = append(opts, cloudsqlconn.WithCredentialsJSON([]byte(credentials)))
}

if usePrivateIP {
opts = append(opts, cloudsqlconn.WithDefaultDialOptions(cloudsqlconn.WithPrivateIP()))
}

return opts, nil
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/database/helper/connutil/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SQLConnectionProducer struct {
AuthType string `json:"auth_type" mapstructure:"auth_type" structs:"auth_type"`
ServiceAccountJSON string `json:"service_account_json" mapstructure:"service_account_json" structs:"service_account_json"`
DisableEscaping bool `json:"disable_escaping" mapstructure:"disable_escaping" structs:"disable_escaping"`
usePrivateIP bool `json:"use_private_ip" mapstructure:"use_private_ip" structs:"use_private_ip"`

// cloud options here - cloudDriverName is globally unique, but only needs to be retained for the lifetime
// of driver registration, not across plugin restarts.
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c *SQLConnectionProducer) Init(ctx context.Context, conf map[string]interf
// however, the driver might store a credentials file, in which case the state stored by the driver is in
// fact critical to the proper function of the connection. So it needs to be registered here inside the
// ConnectionProducer init.
dialerCleanup, err := c.registerDrivers(c.cloudDriverName, c.ServiceAccountJSON)
dialerCleanup, err := c.registerDrivers(c.cloudDriverName, c.ServiceAccountJSON, c.usePrivateIp)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions website/content/api-docs/secret/databases/postgresql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ has a number of parameters to further configure a connection.
- `service_account_json` `(string: "")` - JSON encoded credentials for a GCP Service Account to use
for IAM authentication. Requires `auth_type` to be `gcp_iam`.

- `use_private_ip` `(boolean: false)` - Enables the option to connect to CloudSQL Instances with Private IP.
Requires `auth_type` to be `gcp_iam`.

- `username_template` `(string)` - [Template](/vault/docs/concepts/username-templating) describing how
dynamic usernames are generated.

Expand Down
2 changes: 2 additions & 0 deletions website/content/docs/secrets/databases/postgresql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ ALTER USER "<YOUR DB USERNAME>" WITH CREATEROLE;
plugin_name="postgresql-database-plugin" \
allowed_roles="my-role" \
connection_url="host=project:us-west1:mydb user=test-user@project.iam dbname=postgres sslmode=disable" \
use_private_ip="false" \
auth_type="gcp_iam"
```

Expand All @@ -139,6 +140,7 @@ ALTER USER "<YOUR DB USERNAME>" WITH CREATEROLE;
plugin_name="postgresql-database-plugin" \
allowed_roles="my-role" \
connection_url="host=project:region:instance user=test-user@project.iam dbname=postgres sslmode=disable" \
use_private_ip="false" \
auth_type="gcp_iam" \
service_account_json="@my_credentials.json"
```
Expand Down