Skip to content

Commit 398dd79

Browse files
committed
support import pem by index
1 parent 0b31692 commit 398dd79

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

cmd/jks.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import (
1414
)
1515

1616
var (
17-
jksPassword string
18-
jksSource string
19-
jksCertIndexes []int
17+
jksPassword string
18+
jksSource string
2019
)
2120

2221
// jksCmd represents the jks command
@@ -83,7 +82,7 @@ var jksCmd = &cobra.Command{
8382
k, _ := os.Create(fileName)
8483
defer k.Close()
8584
keystore.Encode(k, ks, []byte(jksPassword))
86-
fmt.Printf("java keystore file %s with %d certificates created.\n", fileName, cnt)
85+
fmt.Printf("java keystore file %s with %d certificate(s) created.\n", fileName, cnt)
8786
return nil
8887
},
8988
}
@@ -92,25 +91,13 @@ func init() {
9291
rootCmd.AddCommand(jksCmd)
9392
jksCmd.PersistentFlags().StringVarP(&jksPassword, "password", "p", "changeit", "the password to be used for the java keystore")
9493
jksCmd.PersistentFlags().StringVarP(&jksSource, "source", "s", "", "the source keystore to add the certs to")
95-
jksCmd.PersistentFlags().IntSliceVarP(&jksCertIndexes, "add", "a", make([]int, 0), "import the certificates at the given indexes")
94+
9695
}
9796

9897
func alias(cert *x509.Certificate) string {
9998
return fmt.Sprintf("%s (%s)", strings.ToLower(cert.Subject.CommonName), strings.ToLower(cert.Issuer.CommonName))
10099
}
101100

102-
func isToExport(i int) bool {
103-
if len(jksCertIndexes) == 0 {
104-
return true
105-
}
106-
for _, a := range jksCertIndexes {
107-
if a == i {
108-
return true
109-
}
110-
}
111-
return false
112-
}
113-
114101
func alreadyContained(ks keystore.KeyStore, cert *x509.Certificate, index int) bool {
115102
for a, e := range ks {
116103
switch tce := e.(type) {

cmd/pem.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var pemCmd = &cobra.Command{
2424
return err
2525
}
2626

27-
pem, err := certChainToPEM(certs)
27+
pem, cnt, err := certChainToPEM(certs)
2828

2929
if err != nil {
3030
return err
@@ -45,7 +45,7 @@ var pemCmd = &cobra.Command{
4545

4646
defer f.Close()
4747
f.Write(pem)
48-
fmt.Printf("pem file %s with %d certificates created.\n", fileName, len(certs))
48+
fmt.Printf("pem file %s with %d certificate(s) created.\n", fileName, cnt)
4949
return nil
5050
},
5151
}
@@ -55,13 +55,19 @@ func init() {
5555
}
5656

5757
// CertChainToPEM is a utility function returns a PEM encoded chain of x509 Certificates, in the order they are passed
58-
func certChainToPEM(certChain []*x509.Certificate) ([]byte, error) {
58+
func certChainToPEM(certChain []*x509.Certificate) ([]byte, int, error) {
5959
var pemBytes bytes.Buffer
60+
cnt := 0
6061
for i, cert := range certChain {
61-
fmt.Printf("Adding certificate #%d: %s\n", i, cert.Subject.CommonName)
62-
if err := pem.Encode(&pemBytes, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
63-
return nil, err
62+
if isToExport(i) {
63+
fmt.Printf(" + Adding certificate #%d: %s\n", i, cert.Subject.CommonName)
64+
if err := pem.Encode(&pemBytes, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
65+
return nil, 0, err
66+
}
67+
cnt++
68+
} else {
69+
fmt.Printf(" - Skipping certificate #%d: %s\n", i, cert.Subject.CommonName)
6470
}
6571
}
66-
return pemBytes.Bytes(), nil
72+
return pemBytes.Bytes(), cnt, nil
6773
}

cmd/root.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ NotAfter: %s
2222
)
2323

2424
var (
25-
targetURL string
26-
outputFile string
25+
targetURL string
26+
outputFile string
27+
certIndexes []int
2728
)
2829

2930
// rootCmd represents the base command when called without any subcommands
@@ -59,6 +60,7 @@ func init() {
5960
rootCmd.PersistentFlags().StringVarP(&targetURL, "url", "u", "", "the URL to fetch the certificate from")
6061
rootCmd.MarkPersistentFlagRequired("url")
6162
rootCmd.PersistentFlags().StringVarP(&outputFile, "out-file", "o", "", "the output file")
63+
rootCmd.PersistentFlags().IntSliceVarP(&certIndexes, "import-at", "i", make([]int, 0), "import the certificates at the given indexes")
6264
}
6365

6466
func fetchCertificates() ([]*x509.Certificate, error) {
@@ -76,3 +78,15 @@ func fetchCertificates() ([]*x509.Certificate, error) {
7678
}
7779
return nil, fmt.Errorf("Could not find any certificates")
7880
}
81+
82+
func isToExport(i int) bool {
83+
if len(certIndexes) == 0 {
84+
return true
85+
}
86+
for _, a := range certIndexes {
87+
if a == i {
88+
return true
89+
}
90+
}
91+
return false
92+
}

0 commit comments

Comments
 (0)