-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (188 loc) · 4.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Add a remote TLS certificate to a local Java keystore
// Requires a Java environment
// Return codes:
// 1: wrong usage (commandline arguments)
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
)
const (
maxBackup = 1000
)
var (
maxBackupLimitReached = fmt.Errorf("maximum number of %d backups "+
"reached\n", maxBackup)
)
func main() {
chain := flag.Bool("chain", true, "Import complete CA chain")
insecure := flag.Bool("insecure", true, "Allow custom cert path")
keep := flag.Bool("keep", false,
"Keep interim certificates in temporary directory")
passwd := flag.String("keystorepasswd", "changeit",
"keystore password")
flag.Parse()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [host:port]...\n",
os.Args[0])
}
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
keystore := locate()
err := numberedBackup(keystore, maxBackup)
die(err)
// Fetch remote cert
for _, addr := range flag.Args() {
var cfg tls.Config
if *insecure {
cfg.InsecureSkipVerify = true
}
conn, err := tls.Dial("tcp", addr, &cfg)
die(err)
conn.Handshake()
certs := conn.ConnectionState().PeerCertificates
log.Printf("%s: %d certs (%T)\n", addr, len(certs), certs)
for i, c := range certs {
log.Printf("--------------------------------------\n")
// log.Printf("cert: %+v\n", c)
log.Printf("Issuer: %+v\n", c.Issuer)
log.Printf("IssuingCertificateURL: %+v\n",
c.IssuingCertificateURL)
f, err := persist(c)
die(err)
alias := fmt.Sprintf("%s_%d", addr, i)
addCert(keystore, *passwd, f, alias)
// defer()ing can result in resource saturation such as
// maximum number of open files e.a., but a common cert
// chain usually consists of < 10 certs.
if *keep {
log.Printf("keeping interim file %s\n", f)
} else {
defer os.Remove(f)
}
if *chain {
caf, err := fetchIssuer(*c)
die(err)
addCert(keystore, *passwd, caf,
filepath.Base(caf))
if *keep {
log.Printf("keeping interim file %s\n",
caf)
} else {
defer os.Remove(caf)
}
}
}
}
}
func addCert(keystore, password, certfilename, alias string) {
safePart := []string{
filepath.Join(javaHome(), "jre/bin/keytool"),
"-import",
"-noprompt",
"-trustcacerts",
"-alias", alias,
"-keystore", keystore,
"-file", certfilename,
}
log.Printf("executing %+v\n", safePart)
sensitivePart := []string{"--storepass", password}
both := append(safePart, sensitivePart...)
cmd := exec.Command(both[0], both[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
die(err)
}
func die(err error) {
if err != nil {
log.Fatal(err)
}
}
func fetchIssuer(c x509.Certificate) (string, error) {
// Should we support multiple URLs?
url := c.IssuingCertificateURL[0]
log.Printf("fetching referenced CA %s\n", url)
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
filename := filepath.Join(os.TempDir(), path.Base(url))
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
if err := ioutil.WriteFile(filename, buf, 0600); err != nil {
return "", err
}
log.Printf("wrote %s\n", filename)
return filename, nil
}
func javaHome() string {
return os.Getenv("JAVA_HOME")
}
// Create a numbered backup à la cp --numbered-backup
func nextBackup(filename string, maxBackup int) (string, error) {
for i := 0; i < maxBackup; i++ {
s := fmt.Sprintf("%s.%d", filename, i)
_, err := os.Stat(s)
if os.IsNotExist(err) {
return s, nil
} else {
die(err)
}
}
return "", maxBackupLimitReached
}
func numberedBackup(filename string, maxBackup int) error {
dest, err := nextBackup(filename, maxBackup)
log.Printf("creating backup %s\n", dest)
if err != nil {
return err
}
// Certs are small, so we can use in-memory approach
buf, err := ioutil.ReadFile(filename)
die(err)
// Create new file with same perms
fi, err := os.Stat(filename)
die(err)
if err := ioutil.WriteFile(dest, buf, fi.Mode()); err != nil {
log.Fatal(err)
}
log.Printf("created backup %s\n", dest)
return nil
}
func locate() string {
val := javaHome()
if len(val) == 0 {
// TODO determine via 'which java' and following links
log.Fatalf("missing JAVA_HOME")
}
p := filepath.Join(val, "jre/lib/security/cacerts")
resolved, err := filepath.EvalSymlinks(p)
die(err)
return resolved
}
func persist(cert *x509.Certificate) (string, error) {
// Write to tmp directory
tmpfile, err := ioutil.TempFile("", "cert-")
if err != nil {
return "", err
}
pem.Encode(tmpfile, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
tmpfile.Close()
return tmpfile.Name(), nil
}