@@ -14,6 +14,7 @@ import (
14
14
"strings"
15
15
"time"
16
16
17
+ "github.com/fullstorydev/grpcurl/internal/certigo/lib"
17
18
"github.com/jhump/protoreflect/desc"
18
19
"github.com/jhump/protoreflect/grpcreflect"
19
20
"google.golang.org/grpc"
@@ -64,18 +65,21 @@ var (
64
65
cacert = flags .String ("cacert" , "" , prettify (`
65
66
File containing trusted root certificates for verifying the server.
66
67
Ignored if -insecure is specified.` ))
68
+ pCACertFormat = flags .String ("cacert-format" , string (lib .CertKeyFormatPEM ), prettify (`
69
+ cacert Format of given input (PEM, DER; heuristic if missing).` ))
67
70
cert = flags .String ("cert" , "" , prettify (`
68
71
File containing client certificate (public key), to present to the
69
72
server. Not valid with -plaintext option. Must also provide -key option
70
- when use PEM certificate file.` ))
71
- certTypeString = flags .String ("cert-type" , "" , prettify (`
72
- Client certificate file type. (PEM/P12)` ))
73
- certType = grpcurl .CertTypePEM
74
- pass = flags .String ("pass" , "" , prettify (`
73
+ when use PEM/DER certificate file.` ))
74
+ pCertFormat = flags .String ("cert-format" , string (lib .CertKeyFormatPEM ), prettify (`
75
+ cert Format of given input (PEM, DER, PKCS12; heuristic if missing).` ))
76
+ pass = flags .String ("pass" , "" , prettify (`
75
77
Pass phrase for the key` ))
76
78
key = flags .String ("key" , "" , prettify (`
77
79
File containing client private key, to present to the server. Not valid
78
80
with -plaintext option. Must also provide -cert option.` ))
81
+ pKeyFormat = flags .String ("key-format" , string (lib .CertKeyFormatPEM ), prettify (`
82
+ key Format of given input (PEM, DER; heuristic if missing).` ))
79
83
80
84
// ALTS Options
81
85
usealts = flags .Bool ("alts" , false , prettify (`
@@ -294,17 +298,9 @@ func main() {
294
298
295
299
// default behavior is to use tls
296
300
usetls := ! * plaintext && ! * usealts
297
-
298
- //// converto to CertificateFileType
299
- //if len(*certTypeString) == 0 {
300
- // certType = grpcurl.CertTypePEM // default PEM
301
- //} else if strings.EqualFold(*certTypeString, "PEM") {
302
- // certType = grpcurl.CertTypePEM
303
- //} else if strings.EqualFold(*certTypeString, "P12") {
304
- // certType = grpcurl.CertTypeP12
305
- //} else {
306
- // fail(nil, "The -cert-type argument must be PEM or P12.")
307
- //}
301
+ cacertFormat := lib .NewCertificateKeyFormat (* pCACertFormat )
302
+ certFormat := lib .NewCertificateKeyFormat (* pCertFormat )
303
+ keyFormat := lib .NewCertificateKeyFormat (* pKeyFormat )
308
304
309
305
// Do extra validation on arguments and figure out what user asked us to do.
310
306
if * connectTimeout < 0 {
@@ -332,21 +328,61 @@ func main() {
332
328
fail (nil , "The -key argument can only be used with TLS." )
333
329
}
334
330
335
- //switch certType {
336
- //case grpcurl.CertTypePEM:
337
- // if (*key == "") != (*cert == "") {
338
- // fail(nil, "The -cert and -key arguments must be used together and both be present when -cert-type is PEM.")
339
- // }
340
- //case grpcurl.CertTypeP12:
341
- // if *key != "" {
342
- // fail(nil, "The -key arguments must not be used when -cert-type is P12.")
343
- // }
344
- // if *cert == "" {
345
- // fail(nil, "The -cert arguments must be used when -cert-type is P12.")
346
- // }
347
- //default:
348
- // fail(nil, "Not support cert type %v.", certType)
349
- //}
331
+ if usetls {
332
+ if * cacert != "" {
333
+ if cacertFormat .IsNone () {
334
+ guessFormat , err := lib .GuessFormatForFile (* cacert , "" )
335
+ if err != nil {
336
+ fail (nil , "Fail to guess file format of -key err: %s" , err )
337
+ }
338
+ cacertFormat .Set (guessFormat )
339
+ }
340
+ switch cacertFormat {
341
+ case lib .CertKeyFormatPEM , lib .CertKeyFormatDER :
342
+ // do nothing
343
+ default :
344
+ fail (nil , "The -cacert-format %s not support." , keyFormat )
345
+ }
346
+ }
347
+ if * cert != "" {
348
+ if certFormat .IsNone () {
349
+ guessFormat , err := lib .GuessFormatForFile (* cert , "" )
350
+ if err != nil {
351
+ fail (nil , "Fail to guess file format of -cert err: %s" , err )
352
+ }
353
+ certFormat .Set (guessFormat )
354
+ }
355
+
356
+ switch certFormat {
357
+ case lib .CertKeyFormatPEM , lib .CertKeyFormatDER :
358
+ if * cert == "" || * key == "" {
359
+ fail (nil , "The -cert and -key arguments must be used together and both be present." )
360
+ }
361
+ case lib .CertKeyFormatPKCS12 :
362
+ // do nothing
363
+ default :
364
+ fail (nil , "The -cert-format %s not support." , certFormat )
365
+ }
366
+ }
367
+ if * key != "" {
368
+ if keyFormat .IsNone () {
369
+ guessFormat , err := lib .GuessFormatForFile (* key , "" )
370
+ if err != nil {
371
+ fail (nil , "Fail to guess file format of -key err: %s" , err )
372
+ }
373
+ keyFormat .Set (guessFormat )
374
+ }
375
+ switch keyFormat {
376
+ case lib .CertKeyFormatPEM , lib .CertKeyFormatDER :
377
+ if * cert == "" || * key == "" {
378
+ fail (nil , "The -cert and -key arguments must be used together and both be present." )
379
+ }
380
+ default :
381
+ fail (nil , "The -key-format %s not support." , keyFormat )
382
+ }
383
+ }
384
+
385
+ }
350
386
351
387
if * altsHandshakerServiceAddress != "" && ! * usealts {
352
388
fail (nil , "The -alts-handshaker-service argument must be used with the -alts argument." )
@@ -482,7 +518,7 @@ func main() {
482
518
}
483
519
creds = alts .NewClientCreds (clientOptions )
484
520
} else if usetls {
485
- tlsConf , err := grpcurl .ClientTLSConfigV2 (* insecure , * cacert , * cert , * key , certType , * pass )
521
+ tlsConf , err := lib .ClientTLSConfigV2 (* insecure , * cacert , cacertFormat , * cert , certFormat , * key , keyFormat , * pass )
486
522
if err != nil {
487
523
fail (err , "Failed to create TLS config" )
488
524
}
0 commit comments