-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraefik-certs-exporter.go
196 lines (162 loc) · 5.04 KB
/
traefik-certs-exporter.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
// exports traefik's certificates as files
package main
import (
"encoding/base64"
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"crypto/x509"
"github.com/fsnotify/fsnotify"
)
// ACME is an imported global traefik acme.json structure
type ACME struct {
Le struct {
Account Account
Certificates []Certificates
} `json:"le"`
}
// Account represents the account details in acme.json
type Account struct {
Email string
}
// Certificates list in acme.json
type Certificates struct {
Domain Domain
Certificate string `json:"certificate"`
Key string `json:"key"`
}
// Domain represents the domain details in acme.json
type Domain struct {
Main string `json:"main"`
}
func main() {
// Parse command-line arguments
acmeJSONPath := flag.String("acmejson", "input/acme.json", "Path to the acme.json file")
outputDir := flag.String("output", "output", "Path to the output directory")
serviceMode := flag.Bool("service", false, "Enable service mode to monitor acme.json for changes")
flag.Parse()
if *serviceMode {
// Run in service mode
runServiceMode(*acmeJSONPath, *outputDir)
} else {
// Run once
exportCertificates(*acmeJSONPath, *outputDir)
}
}
func exportCertificates(acmeJSONPath, outputDir string) {
// Open acme.json file
jsonFile, err := os.Open(acmeJSONPath)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
log.Println("Successfully Opened acme.json")
// Read JSON content
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
var traefik ACME
if err := json.Unmarshal(byteValue, &traefik); err != nil {
log.Fatal(err)
}
// Create output directory if not exists
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
log.Fatal(err)
}
for _, cert := range traefik.Le.Certificates {
domainDir := filepath.Join(outputDir, cert.Domain.Main)
if err := os.MkdirAll(domainDir, os.ModePerm); err != nil {
log.Fatal(err)
}
// Decode certificate and key
certBody, err := base64.StdEncoding.DecodeString(cert.Certificate)
if err != nil {
log.Println(err)
continue
}
certKey, err := base64.StdEncoding.DecodeString(cert.Key)
if err != nil {
log.Println(err)
continue
}
// Write certificate and key to files
certPath := filepath.Join(domainDir, "cert.pem")
if err := ioutil.WriteFile(certPath, append(certBody, []byte("\n")...), os.ModePerm); err != nil {
log.Println(err)
}
// Build and write fullchain.pem
chain, err := buildFullChain(certBody, traefik.Le.Certificates)
if err != nil {
log.Println(err)
} else {
chainPath := filepath.Join(domainDir, "fullchain.pem")
if err := ioutil.WriteFile(chainPath, append(chain, []byte("\n")...), os.ModePerm); err != nil {
log.Println(err)
}
}
keyPath := filepath.Join(domainDir, "privkey.pem")
if err := ioutil.WriteFile(keyPath, append(certKey, []byte("\n")...), os.ModePerm); err != nil {
log.Println(err)
}
log.Println("Saved certificate and key for", cert.Domain.Main)
}
}
func buildFullChain(cert []byte, certificates []Certificates) ([]byte, error) {
certChain := append([]byte{}, cert...)
for {
var parentCert *Certificates
for _, c := range certificates {
if c.Domain.Main == certIssuer(certChain) {
parentCert = &c
break
}
}
if parentCert == nil {
break
}
certBody, err := base64.StdEncoding.DecodeString(parentCert.Certificate)
if err != nil {
return nil, err
}
certChain = append(certChain, certBody...)
}
return certChain, nil
}
func certIssuer(certPEM []byte) string {
cert, err := x509.ParseCertificate(certPEM)
if err != nil {
return ""
}
return cert.Issuer.CommonName
}
func runServiceMode(acmeJSONPath, outputDir string) {
exportCertificates(acmeJSONPath, outputDir)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
err = watcher.Add(acmeJSONPath)
if err != nil {
log.Fatal(err)
}
log.Printf("Monitoring %s for changes...\n", acmeJSONPath)
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("acme.json has been modified. Exporting certificates...")
exportCertificates(acmeJSONPath, outputDir)
}
case err := <-watcher.Errors:
log.Println("Error:", err)
}
// Sleep for a moment to avoid high CPU usage in the loop
time.Sleep(1000 * time.Millisecond)
}
}