@@ -29,8 +29,10 @@ import (
29
29
30
30
"cloud.google.com/go/alloydbconn"
31
31
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
32
+ "github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/gcloud"
32
33
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
33
34
"github.com/spf13/cobra"
35
+ "golang.org/x/oauth2"
34
36
)
35
37
36
38
var (
@@ -122,6 +124,8 @@ without having to manage any client SSL certificates.`,
122
124
"Bearer token used for authorization." )
123
125
cmd .PersistentFlags ().StringVarP (& c .conf .CredentialsFile , "credentials-file" , "c" , "" ,
124
126
"Path to a service account key to use for authentication." )
127
+ cmd .PersistentFlags ().BoolVarP (& c .conf .GcloudAuth , "gcloud-auth" , "g" , false ,
128
+ "Use gcloud's user configuration to retrieve a token for authentication." )
125
129
126
130
// Global and per instance flags
127
131
cmd .PersistentFlags ().StringVarP (& c .conf .Addr , "address" , "a" , "127.0.0.1" ,
@@ -154,19 +158,41 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
154
158
return newBadCommandError (fmt .Sprintf ("not a valid IP address: %q" , conf .Addr ))
155
159
}
156
160
157
- // If both token and credentials file were set, error.
161
+ // If more than one auth method is set, error.
158
162
if conf .Token != "" && conf .CredentialsFile != "" {
159
- return newBadCommandError ("Cannot specify --token and --credentials-file flags at the same time" )
163
+ return newBadCommandError ("cannot specify --token and --credentials-file flags at the same time" )
164
+ }
165
+ if conf .Token != "" && conf .GcloudAuth {
166
+ return newBadCommandError ("cannot specify --token and --gcloud-auth flags at the same time" )
167
+ }
168
+ if conf .CredentialsFile != "" && conf .GcloudAuth {
169
+ return newBadCommandError ("cannot specify --credentials-file and --gcloud-auth flags at the same time" )
170
+ }
171
+ opts := []alloydbconn.Option {
172
+ alloydbconn .WithUserAgent (userAgent ),
160
173
}
161
-
162
174
switch {
163
175
case conf .Token != "" :
164
176
cmd .Printf ("Authorizing with the -token flag\n " )
177
+ opts = append (opts , alloydbconn .WithTokenSource (
178
+ oauth2 .StaticTokenSource (& oauth2.Token {AccessToken : conf .Token }),
179
+ ))
165
180
case conf .CredentialsFile != "" :
166
181
cmd .Printf ("Authorizing with the credentials file at %q\n " , conf .CredentialsFile )
182
+ opts = append (opts , alloydbconn .WithCredentialsFile (
183
+ conf .CredentialsFile ,
184
+ ))
185
+ case conf .GcloudAuth :
186
+ cmd .Println ("Authorizing with gcloud user credentials" )
187
+ ts , err := gcloud .TokenSource ()
188
+ if err != nil {
189
+ return err
190
+ }
191
+ opts = append (opts , alloydbconn .WithTokenSource (ts ))
167
192
default :
168
193
cmd .Println ("Authorizing with Application Default Credentials" )
169
194
}
195
+ conf .DialerOpts = opts
170
196
171
197
var ics []proxy.InstanceConnConfig
172
198
for _ , a := range args {
@@ -269,9 +295,8 @@ func runSignalWrapper(cmd *Command) error {
269
295
// Otherwise, initialize a new one.
270
296
d := cmd .conf .Dialer
271
297
if d == nil {
272
- opts := append (cmd .conf .DialerOpts (), alloydbconn .WithUserAgent (userAgent ))
273
298
var err error
274
- d , err = alloydbconn .NewDialer (ctx , opts ... )
299
+ d , err = alloydbconn .NewDialer (ctx , cmd . conf . DialerOpts ... )
275
300
if err != nil {
276
301
shutdownCh <- fmt .Errorf ("error initializing dialer: %v" , err )
277
302
return
0 commit comments