@@ -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." )
111113cmd .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
115119cmd .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 {
131135return 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.
135139if conf .Token != "" && conf .CredentialsFile != "" {
136140return 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+ }
139151switch {
140152case conf .Token != "" :
141153cmd .Printf ("Authorizing with the -token flag\n " )
154+ opts = append (opts , cloudsqlconn .WithTokenSource (
155+ oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : conf .Token }),
156+ ))
142157case conf .CredentialsFile != "" :
143158cmd .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 ))
144169default :
145- cmd .Printf ("Authorizing with Application Default Credentials" )
170+ cmd .Println ("Authorizing with Application Default Credentials" )
146171}
172+ conf .DialerOpts = opts
147173
148174var ics []proxy.InstanceConnConfig
149175for _ , a := range args {
@@ -227,9 +253,8 @@ func runSignalWrapper(cmd *Command) error {
227253// Otherwise, initialize a new one.
228254d := cmd .conf .Dialer
229255if d == nil {
230- opts := append (cmd .conf .DialerOpts (), cloudsqlconn .WithUserAgent (userAgent ))
231256var err error
232- d , err = cloudsqlconn .NewDialer (ctx , opts ... )
257+ d , err = cloudsqlconn .NewDialer (ctx , cmd . conf . DialerOpts ... )
233258if err != nil {
234259shutdownCh <- fmt .Errorf ("error initializing dialer: %v" , err )
235260return
0 commit comments