Skip to content

Commit 355bf87

Browse files
author
Abdelkarim Boujida
committed
Add option to use access token for authentication
1 parent 6255971 commit 355bf87

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/hashicorp/terraform-plugin-go v0.22.1
1111
github.com/hashicorp/terraform-plugin-log v0.9.0
1212
golang.org/x/net v0.24.0
13+
golang.org/x/oauth2 v0.18.0
1314
)
1415

1516
require (
@@ -83,7 +84,6 @@ require (
8384
golang.org/x/crypto v0.22.0 // indirect
8485
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
8586
golang.org/x/mod v0.15.0 // indirect
86-
golang.org/x/oauth2 v0.18.0 // indirect
8787
golang.org/x/sys v0.19.0 // indirect
8888
golang.org/x/text v0.14.0 // indirect
8989
golang.org/x/time v0.5.0 // indirect

internal/provider/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"cloud.google.com/go/cloudsqlconn/postgres/pgxv4"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
"golang.org/x/net/proxy"
15+
"golang.org/x/oauth2"
1516
)
1617

1718
type Config struct {
@@ -79,6 +80,11 @@ func (c *Config) registerDriver(ctx context.Context, cc *ConnectionConfig) error
7980

8081
options = append(options, cloudsqlconn.WithDefaultDialOptions(dialOptions...))
8182

83+
if cc.GoogleApiAccessToken.ValueString() != "" {
84+
token := &oauth2.Token{AccessToken: cc.GoogleApiAccessToken.ValueString()}
85+
options = append(options, cloudsqlconn.WithTokenSource(oauth2.StaticTokenSource(token)))
86+
}
87+
8288
if !cc.Proxy.IsNull() {
8389
options = append(options, cloudsqlconn.WithDialFunc(createDialer(cc.Proxy.ValueString(), ctx)))
8490
}

internal/provider/provider.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ type CloudSqlPostgresqlProviderModel struct {
3434
}
3535

3636
type ConnectionConfig struct {
37-
ConnectionName types.String `tfsdk:"connection_name"`
38-
Database types.String `tfsdk:"database"`
39-
Username types.String `tfsdk:"username"`
40-
Password types.String `tfsdk:"password"`
41-
Proxy types.String `tfsdk:"proxy"`
42-
PrivateIP types.Bool `tfsdk:"private_ip"`
43-
PSC types.Bool `tfsdk:"psc"`
44-
SslMode types.String `tfsdk:"ssl_mode"`
45-
// IAMAuthentication types.Bool `tfsdk:"iam_authentication"` # Not supporting IAM authentication for now.
37+
ConnectionName types.String `tfsdk:"connection_name"`
38+
Database types.String `tfsdk:"database"`
39+
Username types.String `tfsdk:"username"`
40+
Password types.String `tfsdk:"password"`
41+
Proxy types.String `tfsdk:"proxy"`
42+
PrivateIP types.Bool `tfsdk:"private_ip"`
43+
PSC types.Bool `tfsdk:"psc"`
44+
SslMode types.String `tfsdk:"ssl_mode"`
45+
GoogleApiAccessToken types.String `tfsdk:"google_api_access_token"`
46+
// IAMAuthentication types.Bool `tfsdk:"iam_authentication"` # Not supporting IAM authentication on the database for now.
4647
}
4748

4849
func (p *CloudSqlPostgresqlProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
@@ -177,6 +178,12 @@ func (p *CloudSqlPostgresqlProvider) Configure(ctx context.Context, req provider
177178
"The provider cannot create the Cloud SQL Postgresql client as there is an unknown configuration value for the `ssl_mode`")
178179
}
179180

181+
if connectionConfig.GoogleApiAccessToken.IsUnknown() {
182+
resp.Diagnostics.AddAttributeError(connectionConfigsPath.AtName("google_api_access_token"),
183+
"Unknown Cloud SQL Postgresql google api access token value",
184+
"The provider cannot create the Cloud SQL Postgresql client as there is an unknown configuration value for `google_api_access_token`")
185+
}
186+
180187
if resp.Diagnostics.HasError() {
181188
return
182189
}

0 commit comments

Comments
 (0)