Skip to content

Commit 2a75c01

Browse files
committed
tidy code not finished yet
1 parent 6fb0837 commit 2a75c01

File tree

3 files changed

+330
-189
lines changed

3 files changed

+330
-189
lines changed

cmd/grpcurl/grpcurl.go

+68-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17+
"github.com/fullstorydev/grpcurl/internal/certigo/lib"
1718
"github.com/jhump/protoreflect/desc"
1819
"github.com/jhump/protoreflect/grpcreflect"
1920
"google.golang.org/grpc"
@@ -64,18 +65,21 @@ var (
6465
cacert = flags.String("cacert", "", prettify(`
6566
File containing trusted root certificates for verifying the server.
6667
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).`))
6770
cert = flags.String("cert", "", prettify(`
6871
File containing client certificate (public key), to present to the
6972
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(`
7577
Pass phrase for the key`))
7678
key = flags.String("key", "", prettify(`
7779
File containing client private key, to present to the server. Not valid
7880
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).`))
7983

8084
// ALTS Options
8185
usealts = flags.Bool("alts", false, prettify(`
@@ -294,17 +298,9 @@ func main() {
294298

295299
// default behavior is to use tls
296300
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)
308304

309305
// Do extra validation on arguments and figure out what user asked us to do.
310306
if *connectTimeout < 0 {
@@ -332,21 +328,61 @@ func main() {
332328
fail(nil, "The -key argument can only be used with TLS.")
333329
}
334330

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+
}
350386

351387
if *altsHandshakerServiceAddress != "" && !*usealts {
352388
fail(nil, "The -alts-handshaker-service argument must be used with the -alts argument.")
@@ -482,7 +518,7 @@ func main() {
482518
}
483519
creds = alts.NewClientCreds(clientOptions)
484520
} 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)
486522
if err != nil {
487523
fail(err, "Failed to create TLS config")
488524
}

grpcurl.go

+1-67
Original file line numberDiff line numberDiff line change
@@ -526,78 +526,12 @@ func ClientTransportCredentials(insecureSkipVerify bool, cacertFile, clientCertF
526526
return credentials.NewTLS(tlsConf), nil
527527
}
528528

529-
type CertificateType int
530-
531-
const (
532-
// The certificate file contains PEM encoded data
533-
CertTypePEM CertificateType = 1
534-
// The certificate file contains PFX data describing PKCS#12.
535-
CertTypeP12 CertificateType = 2
536-
)
537-
538529
// ClientTLSConfig builds transport-layer config for a gRPC client using the
539530
// given properties. If cacertFile is blank, only standard trusted certs are used to
540531
// verify the server certs. If clientCertFile is blank, the client will not use a client
541532
// certificate. If clientCertFile is not blank then clientKeyFile must not be blank.
542533
func ClientTLSConfig(insecureSkipVerify bool, cacertFile, clientCertFile, clientKeyFile string) (*tls.Config, error) {
543-
return ClientTLSConfigV2(insecureSkipVerify, cacertFile, clientCertFile, clientKeyFile, CertTypePEM, "")
544-
}
545-
546-
// ClientTLSConfigV2 builds transport-layer config for a gRPC client using the
547-
// given properties. Support certificate file both PEM and P12.
548-
func ClientTLSConfigV2(insecureSkipVerify bool, cacertFile, clientCertFile, clientKeyFile string, clientCertType CertificateType, clientPass string) (*tls.Config, error) {
549-
var tlsConf tls.Config
550-
551-
if clientCertFile != "" {
552-
// Load the client certificates from disk
553-
clientCertFormat := ""
554-
var pemBuf bytes.Buffer
555-
err := lib.ReadAsPEMEx(clientCertFile, clientCertFormat, clientPass, func(block *pem.Block, format string) error {
556-
return pem.Encode(&pemBuf, block)
557-
})
558-
if err != nil {
559-
return nil, fmt.Errorf("could not load client cert: %v", err)
560-
}
561-
pemBytes := pemBuf.Bytes()
562-
pemKeyBytes := pemBytes
563-
564-
if clientKeyFile != "" {
565-
var pemKeyBuf bytes.Buffer
566-
err := lib.ReadAsPEMEx(clientKeyFile, clientCertFormat, clientPass, func(block *pem.Block, format string) error {
567-
return pem.Encode(&pemKeyBuf, block)
568-
})
569-
if err != nil {
570-
return nil, fmt.Errorf("could not load client key: %v", err)
571-
}
572-
pemKeyBytes = pemKeyBuf.Bytes()
573-
}
574-
575-
certificate, err := tls.X509KeyPair(pemBytes, pemKeyBytes)
576-
if err != nil {
577-
return nil, fmt.Errorf("could not load client key pair: %v", err)
578-
}
579-
tlsConf.Certificates = []tls.Certificate{certificate}
580-
}
581-
582-
if insecureSkipVerify {
583-
tlsConf.InsecureSkipVerify = true
584-
} else if cacertFile != "" {
585-
// Create a certificate pool from the certificate authority
586-
certPool := x509.NewCertPool()
587-
ca, err := ioutil.ReadFile(cacertFile)
588-
if err != nil {
589-
return nil, fmt.Errorf("could not read ca certificate: %v", err)
590-
}
591-
592-
// Append the certificates from the CA
593-
if ok := certPool.AppendCertsFromPEM(ca); !ok {
594-
return nil, errors.New("failed to append ca certs")
595-
}
596-
597-
tlsConf.RootCAs = certPool
598-
}
599-
600-
return &tlsConf, nil
534+
return lib.ClientTLSConfigV2(insecureSkipVerify, cacertFile, lib.CertKeyFormatPEM, clientCertFile, lib.CertKeyFormatPEM, clientKeyFile, lib.CertKeyFormatPEM, "")
601535
}
602536

603537
func inputFiles(fileNames []string) ([]*os.File, error) {

0 commit comments

Comments
 (0)