Skip to content

Commit ede18a4

Browse files
committed
add unit test for cert PKCS12 DER PEM
1 parent 2a75c01 commit ede18a4

17 files changed

+654
-226
lines changed

cmd/grpcurl/grpcurl.go

+25-37
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ var (
7373
when use PEM/DER certificate file.`))
7474
pCertFormat = flags.String("cert-format", string(lib.CertKeyFormatPEM), prettify(`
7575
cert Format of given input (PEM, DER, PKCS12; heuristic if missing).`))
76-
pass = flags.String("pass", "", prettify(`
77-
Pass phrase for the key`))
76+
certPass = flags.String("pass", "", prettify(`
77+
Pass phrase for the PKCS12 cert`))
7878
key = flags.String("key", "", prettify(`
7979
File containing client private key, to present to the server. Not valid
8080
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).`))
8381

8482
// ALTS Options
8583
usealts = flags.Bool("alts", false, prettify(`
@@ -300,7 +298,7 @@ func main() {
300298
usetls := !*plaintext && !*usealts
301299
cacertFormat := lib.NewCertificateKeyFormat(*pCACertFormat)
302300
certFormat := lib.NewCertificateKeyFormat(*pCertFormat)
303-
keyFormat := lib.NewCertificateKeyFormat(*pKeyFormat)
301+
keyFormat := lib.CertKeyFormatPEM
304302

305303
// Do extra validation on arguments and figure out what user asked us to do.
306304
if *connectTimeout < 0 {
@@ -330,55 +328,45 @@ func main() {
330328

331329
if usetls {
332330
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)
331+
guessFormat, err := lib.GuessFormatForFile(*cacert, cacertFormat)
332+
if err != nil {
333+
fail(nil, "Fail to guess file format of -key err: %s", err)
339334
}
340-
switch cacertFormat {
335+
switch guessFormat {
341336
case lib.CertKeyFormatPEM, lib.CertKeyFormatDER:
342-
// do nothing
337+
cacertFormat = guessFormat
343338
default:
344-
fail(nil, "The -cacert-format %s not support.", keyFormat)
339+
fail(nil, "The -cacert-format %s not support.", cacertFormat)
345340
}
346341
}
347342
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)
343+
guessFormat, err := lib.GuessFormatForFile(*cert, certFormat)
344+
if err != nil {
345+
fail(nil, "Fail to guess file format of -cert err: %s", err)
354346
}
355347

356-
switch certFormat {
348+
switch guessFormat {
357349
case lib.CertKeyFormatPEM, lib.CertKeyFormatDER:
358350
if *cert == "" || *key == "" {
359351
fail(nil, "The -cert and -key arguments must be used together and both be present.")
360352
}
353+
certFormat = guessFormat
361354
case lib.CertKeyFormatPKCS12:
362-
// do nothing
355+
certFormat = guessFormat
363356
default:
364357
fail(nil, "The -cert-format %s not support.", certFormat)
365358
}
366359
}
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-
}
360+
if *certPass != "" {
361+
switch certFormat {
362+
case lib.CertKeyFormatPKCS12:
380363
default:
381-
fail(nil, "The -key-format %s not support.", keyFormat)
364+
fail(nil, "The -pass argument is only supported when -cert-type is PKCS12.")
365+
}
366+
}
367+
if *key != "" {
368+
if *cert == "" || *key == "" {
369+
fail(nil, "The -cert and -key arguments must be used together and both be present.")
382370
}
383371
}
384372

@@ -518,7 +506,7 @@ func main() {
518506
}
519507
creds = alts.NewClientCreds(clientOptions)
520508
} else if usetls {
521-
tlsConf, err := lib.ClientTLSConfigV2(*insecure, *cacert, cacertFormat, *cert, certFormat, *key, keyFormat, *pass)
509+
tlsConf, err := lib.ClientTLSConfigV2(*insecure, *cacert, cacertFormat, *cert, certFormat, *key, keyFormat, *certPass)
522510
if err != nil {
523511
fail(err, "Failed to create TLS config")
524512
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package lib
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func NewCertificateKeyFormat(fileFormat string) CertificateKeyFormat {
8+
fileFormat = strings.ToUpper(fileFormat)
9+
switch fileFormat {
10+
case "":
11+
return CertKeyFormatNONE
12+
case "PEM":
13+
return CertKeyFormatPEM
14+
case "DER":
15+
return CertKeyFormatDER
16+
case "JCEKS":
17+
return CertKeyFormatJCEKS
18+
case "PKCS12", "P12":
19+
return CertKeyFormatPKCS12
20+
default:
21+
return CertKeyFormatNONE
22+
}
23+
}
24+
25+
type CertificateKeyFormat string
26+
27+
const (
28+
CertKeyFormatNONE CertificateKeyFormat = ""
29+
// The file contains plain-text PEM data
30+
CertKeyFormatPEM CertificateKeyFormat = "PEM"
31+
// The file contains X.509 DER encoded data
32+
CertKeyFormatDER CertificateKeyFormat = "DER"
33+
// The file contains JCEKS keystores
34+
CertKeyFormatJCEKS CertificateKeyFormat = "JCEKS"
35+
// The file contains PFX data describing PKCS#12
36+
CertKeyFormatPKCS12 CertificateKeyFormat = "PKCS12"
37+
)
38+
39+
func (f *CertificateKeyFormat) Set(fileFormat string) {
40+
*f = NewCertificateKeyFormat(fileFormat)
41+
}
42+
43+
func (f CertificateKeyFormat) IsNone() bool {
44+
return f == CertKeyFormatNONE
45+
}

0 commit comments

Comments
 (0)