forked from thomseddon/traefik-certs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
170 lines (142 loc) · 3.62 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/fsnotify/fsnotify"
)
// Structs
type CertificateDomain struct {
Main string `json:"Main"`
}
type Certificate struct {
Domain CertificateDomain `json:"Domain"`
Certificate string `json:"Certificate"`
Key string `json:"Key"`
}
type Acme struct {
Certificates []Certificate `json:"Certificates"`
}
// Find given name in certificate array
func findDomain(name string, certs []Certificate) bool {
for _, cert := range certs {
if cert.Domain.Main == name {
return true
}
}
return false
}
// Vars
var certPath, acmeFile string
// Extract and write certificates
func buildCerts() {
before, err := ioutil.ReadDir(certPath)
if err != nil {
fmt.Println(err)
}
f, err := ioutil.ReadFile(acmeFile)
if err != nil {
fmt.Println("Error", err)
return
}
var acme map[string]Acme
json.Unmarshal(f, &acme)
for _, resolver := range acme {
for _, cert := range resolver.Certificates {
// Decode
decoded, err := base64.StdEncoding.DecodeString(cert.Certificate)
if err != nil {
fmt.Println("Unable to decode certificate", cert.Domain.Main)
continue
}
// Write chain
name := fmt.Sprintf("%s/%s.chain.crt", certPath, cert.Domain.Main)
fmt.Println("Writing file", name)
err = ioutil.WriteFile(name, decoded, 0644)
if err != nil {
fmt.Println("Error writing file", name)
}
// Write cert
name = fmt.Sprintf("%s/%s.crt", certPath, cert.Domain.Main)
fmt.Println("Writing file", name)
parts := strings.Split(string(decoded), "\n\n")
err = ioutil.WriteFile(name, []byte(parts[0]), 0644)
if err != nil {
fmt.Println("Error writing file", name)
}
// Decode key
decoded, err = base64.StdEncoding.DecodeString(cert.Key)
if err != nil {
fmt.Println("Unable to decode certificate", cert.Domain.Main)
continue
}
// Write key
name = fmt.Sprintf("%s/%s.key", certPath, cert.Domain.Main)
fmt.Println("Writing file", name)
err = ioutil.WriteFile(name, []byte(decoded), 0644)
if err != nil {
fmt.Println("Error writing file", name)
}
}
// Remove any no longer in use
for _, f := range before {
var domain string
l := len(f.Name())
if l >= 10 && f.Name()[l-10:] == ".chain.crt" {
domain = f.Name()[:l-10]
} else if l >= 4 && f.Name()[l-4:] == ".crt" {
domain = f.Name()[:l-4]
} else {
continue
}
if !findDomain(domain, resolver.Certificates) {
fmt.Println("Removing file", f.Name())
//I'm going to temporarily disable this due to issues
// os.Remove(fmt.Sprintf("%s/%s", certPath, f.Name()))
}
}
}
fmt.Println("Wrote certificates")
}
// Main
func main() {
var exists bool
certPath, exists = os.LookupEnv("CERT_PATH")
if !exists {
certPath = "/certs"
}
acmePath, _ := os.LookupEnv("ACME_PATH")
acmeFile = fmt.Sprintf("%s/acme.json", acmePath)
// Setup watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("Error", err)
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case ev := <-watcher.Events:
if ev.Op&fsnotify.Write == fsnotify.Write {
buildCerts()
}
case err := <-watcher.Errors:
fmt.Printf("Error: %#v\n", err)
}
}
}()
// Watch acme.json
err = watcher.Add(acmeFile)
if err != nil {
fmt.Println("Error", err)
}
fmt.Println("Watching", acmeFile)
// One for kick off
buildCerts()
<-done
}