-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
334 lines (321 loc) · 8.22 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"github.com/BTrDB/mr-plotter/keys"
"github.com/BTrDB/smartgridstore/tools"
etcd "github.com/coreos/etcd/clientv3"
)
var etcdConn *etcd.Client
func main() {
if len(os.Args) == 2 && os.Args[1] == "-version" {
fmt.Printf("%d.%d.%d\n", tools.VersionMajor, tools.VersionMinor, tools.VersionPatch)
os.Exit(0)
}
fmt.Printf("[global] Booting certificate proxy %d.%d.%d\n", tools.VersionMajor, tools.VersionMinor, tools.VersionPatch)
var etcdEndpoint string = os.Getenv("ETCD_ENDPOINT")
if len(etcdEndpoint) == 0 {
etcdEndpoint = "localhost:2379"
log.Printf("ETCD_ENDPOINT is not set; using %s", etcdEndpoint)
}
var etcdConfig etcd.Config = etcd.Config{Endpoints: []string{etcdEndpoint}}
log.Println("[global] Connecting to etcd...")
var err error
etcdConn, err = etcd.New(etcdConfig)
if err != nil {
log.Fatalf("Error: %v", err)
}
defer etcdConn.Close()
fmt.Printf("[global] Loading certificate state...\n")
cd := loadCertDetails()
if cd.IsAutocert {
skipInitialConfig := loadEtcDirectory(cd)
if !skipInitialConfig {
fmt.Printf("[global] performing initial certificate acquisition\n")
certbotAcquire(cd)
}
}
go startNginx(cd)
time.Sleep(1 * time.Minute)
if cd.IsAutocert {
go renewalLoop(cd)
}
for {
time.Sleep(1 * time.Second)
}
}
func renewalLoop(cd *CertDetails) {
for {
{
fmt.Printf("[global] RENEWING CERTIFICATE\n")
cmd := exec.Command("certbot", "renew", "--webroot", "--webroot-path", "/var/www")
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
go printPrefix(stderr, "certbot_renew:stderr")
go printPrefix(stdout, "certbot_renew:stdout")
err = cmd.Run()
if err != nil {
fmt.Printf("CERTBOT_RENEW RUN FAILURE: %v\n", err)
os.Exit(1)
}
}
fmt.Printf("[global] Certificate renewal successful, reloading nginx\n")
{
cmd := exec.Command("/usr/sbin/nginx", "-s", "reload")
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
go printPrefix(stderr, "nginx_reload:stderr")
go printPrefix(stdout, "nginx_reload:stdout")
err = cmd.Run()
if err != nil {
fmt.Printf("NGINX_RELOAD RUN FAILURE: %v\n", err)
os.Exit(1)
}
}
saveEtcDirectory()
saveAutocertCache("/etc/letsencrypt/live/"+cd.Domain+"/privkey.pem",
"/etc/letsencrypt/live/"+cd.Domain+"/fullchain.pem",
cd.Domain)
time.Sleep(24 * time.Hour)
}
}
func printPrefix(pipe io.ReadCloser, prefix string) {
br := bufio.NewReader(pipe)
for {
ln, _, err := br.ReadLine()
if err != nil {
fmt.Printf("[%s] ERROR: %v\n", prefix, err)
return
}
fmt.Printf("[%s] %s\n", prefix, ln)
}
}
func startNginx(cd *CertDetails) {
writeNginxConfig(cd)
cmd := exec.Command("/usr/sbin/nginx")
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
go printPrefix(stderr, "nginx:stderr")
go printPrefix(stdout, "nginx:stdout")
err = cmd.Run()
if err != nil {
fmt.Printf("NGINX RUN FAILURE: %v\n", err)
os.Exit(1)
}
}
const statePath = "certproxy/state/"
const configDir = "/etc/letsencrypt/"
type CertDetails struct {
IsAutocert bool
Domain string
Email string
HardcodedKey []byte
HardcodedCert []byte
}
func loadCertDetails() *CertDetails {
ctx := context.Background()
certsrc, err := keys.GetCertificateSource(ctx, etcdConn)
if err != nil {
panic(err)
}
if certsrc == "" {
fmt.Printf("certificate source not set in admin console\n")
fmt.Printf("aborting...")
os.Exit(1)
}
rv := &CertDetails{
IsAutocert: certsrc != "hardcoded",
}
domain, err := keys.GetAutocertHostname(ctx, etcdConn)
if err != nil {
panic(err)
}
rv.Domain = domain
if domain == "" {
fmt.Printf("autocert domain must be set in the admin console\n")
os.Exit(1)
}
if certsrc == "autocert" {
email, err := keys.GetAutocertEmail(ctx, etcdConn)
if err != nil {
panic(err)
}
rv.Email = email
} else {
hardcoded, err := keys.RetrieveHardcodedTLSCertificate(ctx, etcdConn)
if err != nil {
panic(err)
}
if hardcoded == nil || len(hardcoded.Cert) == 0 || len(hardcoded.Key) == 0 {
fmt.Printf("certificate source set to hardcoded, but the hardcoded certificate is missing\n")
os.Exit(1)
}
rv.HardcodedCert = hardcoded.Cert
rv.HardcodedKey = hardcoded.Key
}
return rv
}
func certbotAcquire(cd *CertDetails) {
//certbot certonly -d bunker.cal-sdb.org --staging --standalone --agree-tos -m testing@steelcode.com -n
cmd := exec.Command("certbot", "certonly", "-d", cd.Domain, "--standalone", "--agree-tos", "-m", cd.Email, "-n")
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
go printPrefix(stderr, "certbot_initial:stderr")
go printPrefix(stdout, "certbot_initial:stdout")
err = cmd.Run()
if err != nil {
fmt.Printf("CERTBOT ACQUIRE RUN FAILURE: %v\n", err)
os.Exit(1)
}
saveEtcDirectory()
saveAutocertCache("/etc/letsencrypt/live/"+cd.Domain+"/privkey.pem",
"/etc/letsencrypt/live/"+cd.Domain+"/fullchain.pem",
cd.Domain)
}
func loadEtcDirectory(cd *CertDetails) bool {
if !cd.IsAutocert {
panic("we should not load directory if not in autocert mode\n")
}
domain := cd.Domain
tofind := "live/" + domain + "/privkey.pem"
found := false
//Load the certificates from etcd
resp, err := etcdConn.Get(context.Background(), statePath, etcd.WithPrefix())
if err != nil {
fmt.Printf("failed to query state from etcd: %v\n", err)
os.Exit(1)
}
for _, kv := range resp.Kvs {
suffix := strings.TrimPrefix(string(kv.Key), statePath)
if suffix == tofind {
found = true
}
kpath := configDir + suffix
content := kv.Value
d := path.Dir(kpath)
err := os.MkdirAll(d, 0777)
if err != nil {
fmt.Printf("failed to create directory: %v\n", err)
os.Exit(1)
}
if content[0] == 'B' {
err = ioutil.WriteFile(kpath, content[1:], 0555)
if err != nil {
fmt.Printf("failed to write file: %v\n", err)
os.Exit(1)
}
} else if content[0] == 'S' {
err := os.Symlink(string(content[1:]), string(kpath))
if err != nil {
fmt.Printf("failed to symlink: %v\n", err)
os.Exit(1)
}
}
}
fmt.Printf("finished loading etcd directory\n")
return found
}
func walk(dir string, files map[string][]byte) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("file walking error: %v\n", err)
os.Exit(1)
}
if info.IsDir() {
//do nothing, we will traverse into the directory anyway
} else {
if !info.Mode().IsRegular() {
link, err := os.Readlink(path)
if err != nil {
fmt.Printf("file walk error: %v\n", err)
os.Exit(1)
}
files[path] = []byte("S" + link)
} else {
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("file read error: %v\n", err)
os.Exit(1)
}
prefixedcontent := append([]byte("B"), content...)
files[path] = prefixedcontent
}
}
return nil
})
if err != nil {
fmt.Printf("file walking error: %v\n", err)
os.Exit(1)
}
}
func saveAutocertCache(keyfile string, certfile string, domain string) {
key, err := ioutil.ReadFile(keyfile)
if err != nil {
fmt.Printf("could not read key file: %v\n", err)
os.Exit(1)
}
cert, err := ioutil.ReadFile(certfile)
if err != nil {
fmt.Printf("could not read certificate file: %v\n", err)
os.Exit(1)
}
concat := string(key) + string(cert)
_, err = etcdConn.Put(context.Background(), "mrplotter/keys/autocert_cache/"+domain, concat)
if err != nil {
fmt.Printf("failed to write back autocert cache: %v\n", err)
os.Exit(1)
}
}
func saveEtcDirectory() {
files := make(map[string][]byte)
walk(configDir, files)
ops := []etcd.Op{}
for file, content := range files {
suffix := strings.TrimPrefix(file, configDir)
key := statePath + suffix
ops = append(ops, etcd.OpPut(key, string(content)))
}
ctx, cancel := context.WithCancel(context.Background())
_, err := etcdConn.Txn(ctx).
Then(ops...).
Commit()
cancel()
if err != nil {
fmt.Printf("failed to write back etcd state: %v\n", err)
os.Exit(1)
}
}