@@ -29,8 +29,10 @@ import (
2929
3030 "cloud.google.com/go/cloudsqlconn"
3131 "github.com/GoogleCloudPlatform/cloudsql-proxy/v2/cloudsql"
32+ "github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/gcloud"
3233 "github.com/GoogleCloudPlatform/cloudsql-proxy/v2/internal/proxy"
3334 "github.com/spf13/cobra"
35+ "golang.org/x/oauth2"
3436)
3537
3638var (
@@ -110,6 +112,8 @@ any client SSL certificates.`,
110112 "Bearer token used for authorization." )
111113 cmd .PersistentFlags ().StringVarP (& c .conf .CredentialsFile , "credentials-file" , "c" , "" ,
112114 "Path to a service account key to use for authentication." )
115+ cmd .PersistentFlags ().BoolVarP (& c .conf .GcloudAuth , "gcloud-auth" , "g" , false ,
116+ "Use gcloud's user configuration to retrieve a token for authentication." )
113117
114118 // Global and per instance flags
115119 cmd .PersistentFlags ().StringVarP (& c .conf .Addr , "address" , "a" , "127.0.0.1" ,
@@ -131,19 +135,41 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
131135 return newBadCommandError (fmt .Sprintf ("not a valid IP address: %q" , conf .Addr ))
132136 }
133137
134- // If both token and credentials file were set, error.
138+ // If more than one auth method is set, error.
135139 if conf .Token != "" && conf .CredentialsFile != "" {
136140 return newBadCommandError ("Cannot specify --token and --credentials-file flags at the same time" )
137141 }
138-
142+ if conf .Token != "" && conf .GcloudAuth {
143+ return newBadCommandError ("Cannot specify --token and --gcloud-auth flags at the same time" )
144+ }
145+ if conf .CredentialsFile != "" && conf .GcloudAuth {
146+ return newBadCommandError ("Cannot specify --credentials-file and --gcloud-auth flags at the same time" )
147+ }
148+ opts := []cloudsqlconn.Option {
149+ cloudsqlconn .WithUserAgent (userAgent ),
150+ }
139151 switch {
140152 case conf .Token != "" :
141153 cmd .Printf ("Authorizing with the -token flag\n " )
154+ opts = append (opts , cloudsqlconn .WithTokenSource (
155+ oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : conf .Token }),
156+ ))
142157 case conf .CredentialsFile != "" :
143158 cmd .Printf ("Authorizing with the credentials file at %q\n " , conf .CredentialsFile )
159+ opts = append (opts , cloudsqlconn .WithCredentialsFile (
160+ conf .CredentialsFile ,
161+ ))
162+ case conf .GcloudAuth :
163+ cmd .Println ("Authorizing with gcloud user credentials" )
164+ ts , err := gcloud .TokenSource ()
165+ if err != nil {
166+ return err
167+ }
168+ opts = append (opts , cloudsqlconn .WithTokenSource (ts ))
144169 default :
145- cmd .Printf ("Authorizing with Application Default Credentials" )
170+ cmd .Println ("Authorizing with Application Default Credentials" )
146171 }
172+ conf .DialerOpts = opts
147173
148174 var ics []proxy.InstanceConnConfig
149175 for _ , a := range args {
@@ -227,9 +253,8 @@ func runSignalWrapper(cmd *Command) error {
227253 // Otherwise, initialize a new one.
228254 d := cmd .conf .Dialer
229255 if d == nil {
230- opts := append (cmd .conf .DialerOpts (), cloudsqlconn .WithUserAgent (userAgent ))
231256 var err error
232- d , err = cloudsqlconn .NewDialer (ctx , opts ... )
257+ d , err = cloudsqlconn .NewDialer (ctx , cmd . conf . DialerOpts ... )
233258 if err != nil {
234259 shutdownCh <- fmt .Errorf ("error initializing dialer: %v" , err )
235260 return
0 commit comments