@@ -19,6 +19,7 @@ import (
19
19
"google.golang.org/grpc"
20
20
"google.golang.org/grpc/codes"
21
21
"google.golang.org/grpc/credentials"
22
+ "google.golang.org/grpc/credentials/alts"
22
23
"google.golang.org/grpc/keepalive"
23
24
"google.golang.org/grpc/metadata"
24
25
"google.golang.org/grpc/status"
@@ -52,11 +53,14 @@ var (
52
53
Print usage instructions and exit.` ))
53
54
printVersion = flags .Bool ("version" , false , prettify (`
54
55
Print version.` ))
56
+
55
57
plaintext = flags .Bool ("plaintext" , false , prettify (`
56
58
Use plain-text HTTP/2 when connecting to server (no TLS).` ))
57
59
insecure = flags .Bool ("insecure" , false , prettify (`
58
60
Skip server certificate and domain verification. (NOT SECURE!) Not
59
61
valid with -plaintext option.` ))
62
+
63
+ // TLS Options
60
64
cacert = flags .String ("cacert" , "" , prettify (`
61
65
File containing trusted root certificates for verifying the server.
62
66
Ignored if -insecure is specified.` ))
66
70
key = flags .String ("key" , "" , prettify (`
67
71
File containing client private key, to present to the server. Not valid
68
72
with -plaintext option. Must also provide -cert option.` ))
73
+
74
+ // ALTS Options
75
+ usealts = flags .Bool ("alts" , false , prettify (`
76
+ Use Application Layer Transport Security (ALTS) when connecting to server.` ))
77
+ altsHandshakerServiceAddress = flags .String ("alts-handshaker-service" , "" , prettify (`If set, this server will be used to do the ATLS handshaking.` ))
78
+ altsTargetServiceAccounts multiString
79
+
69
80
protoset multiString
70
81
protoFiles multiString
71
82
importPaths multiString
@@ -199,6 +210,14 @@ func init() {
199
210
-use-reflection is used in combination with a -proto or -protoset flag,
200
211
the provided descriptor sources will be used in addition to server
201
212
reflection to resolve messages and extensions.` ))
213
+ flags .Var (& altsTargetServiceAccounts , "alts-target-service-account" , prettify (`
214
+ The full email address of the service account that the server is
215
+ expected to be using when ALTS is used. You can specify this option
216
+ multiple times to indicate multiple allowed service accounts. If the
217
+ server authenticates with a service account that is not one of the
218
+ expected accounts, the RPC will not be issued. If no such arguments are
219
+ provided, no check will be performed, and the RPC will be issued
220
+ regardless of the server's service account.` ))
202
221
}
203
222
204
223
type multiString []string
@@ -267,6 +286,9 @@ func main() {
267
286
os .Exit (0 )
268
287
}
269
288
289
+ // default behavior is to use tls
290
+ usetls := ! * plaintext && ! * usealts
291
+
270
292
// Do extra validation on arguments and figure out what user asked us to do.
271
293
if * connectTimeout < 0 {
272
294
fail (nil , "The -connect-timeout argument must not be negative." )
@@ -280,18 +302,27 @@ func main() {
280
302
if * maxMsgSz < 0 {
281
303
fail (nil , "The -max-msg-sz argument must not be negative." )
282
304
}
283
- if * plaintext && * insecure {
284
- fail (nil , "The -plaintext and -insecure arguments are mutually exclusive." )
305
+ if * plaintext && * usealts {
306
+ fail (nil , "The -plaintext and -alts arguments are mutually exclusive." )
307
+ }
308
+ if * insecure && ! usetls {
309
+ fail (nil , "The -insecure argument can only be used with TLS." )
285
310
}
286
- if * plaintext && * cert != "" {
287
- fail (nil , "The -plaintext and -cert arguments are mutually exclusive ." )
311
+ if * cert != "" && ! usetls {
312
+ fail (nil , "The -cert argument can only be used with TLS ." )
288
313
}
289
- if * plaintext && * key != "" {
290
- fail (nil , "The -plaintext and -key arguments are mutually exclusive ." )
314
+ if * key != "" && ! usetls {
315
+ fail (nil , "The -key argument can only be used with TLS ." )
291
316
}
292
317
if (* key == "" ) != (* cert == "" ) {
293
318
fail (nil , "The -cert and -key arguments must be used together and both be present." )
294
319
}
320
+ if * altsHandshakerServiceAddress != "" && ! * usealts {
321
+ fail (nil , "The -alts-handshaker-service argument must be used with the -alts argument." )
322
+ }
323
+ if len (altsTargetServiceAccounts ) > 0 && ! * usealts {
324
+ fail (nil , "The -alts-target-service-account argument must be used with the -alts argument." )
325
+ }
295
326
if * format != "json" && * format != "text" {
296
327
fail (nil , "The -format option must be 'json' or 'text'." )
297
328
}
@@ -406,7 +437,20 @@ func main() {
406
437
opts = append (opts , grpc .WithDefaultCallOptions (grpc .MaxCallRecvMsgSize (* maxMsgSz )))
407
438
}
408
439
var creds credentials.TransportCredentials
409
- if ! * plaintext {
440
+ if * plaintext {
441
+ if * authority != "" {
442
+ opts = append (opts , grpc .WithAuthority (* authority ))
443
+ }
444
+ } else if * usealts {
445
+ clientOptions := alts .DefaultClientOptions ()
446
+ if len (altsTargetServiceAccounts ) > 0 {
447
+ clientOptions .TargetServiceAccounts = altsTargetServiceAccounts
448
+ }
449
+ if * altsHandshakerServiceAddress != "" {
450
+ clientOptions .HandshakerServiceAddress = * altsHandshakerServiceAddress
451
+ }
452
+ creds = alts .NewClientCreds (clientOptions )
453
+ } else if usetls {
410
454
tlsConf , err := grpcurl .ClientTLSConfig (* insecure , * cacert , * cert , * key )
411
455
if err != nil {
412
456
fail (err , "Failed to create TLS config" )
@@ -439,8 +483,8 @@ func main() {
439
483
if overrideName != "" {
440
484
opts = append (opts , grpc .WithAuthority (overrideName ))
441
485
}
442
- } else if * authority != "" {
443
- opts = append ( opts , grpc . WithAuthority ( * authority ) )
486
+ } else {
487
+ panic ( "Should have defaulted to use TLS." )
444
488
}
445
489
446
490
grpcurlUA := "grpcurl/" + version
0 commit comments