-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
208 lines (172 loc) · 6.34 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"rafi0101/traefik-ssl-certificate-exporter/models"
"reflect"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func main() {
// pflag.String("certResolver", "dode", "Acme certresolver to extract these certs (requiered)")
pflag.String("source", "traefik/acme.json", "path to traefik acme.json")
pflag.String("dest", "certs/", "path to destination where to store certificates")
pflag.Int("owner", 0, "owner for the extracted cert/keys")
pflag.Int("group", 0, "group for the extracted cert/keys")
pflag.String("config", "", "Path to config file")
pflag.Usage = func() {
fmt.Println("traefik-ssl-certificate-exporter exports the ssl certificates from the provided traefik acme.json")
pflag.PrintDefaults()
os.Exit(0)
}
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.SetConfigFile(viper.GetString("config"))
viper.ReadInConfig()
ownerId := viper.GetInt("owner")
groupId := viper.GetInt("group")
acmejson, err := ioutil.ReadFile(viper.GetString("source"))
if err != nil {
fmt.Println("Failed to read traefik acme.json", err)
os.Exit(1)
}
// //Unmarshal traefik acme.json
acme := new(models.ProviderMdl)
if err := json.Unmarshal(acmejson, acme); err != nil {
fmt.Println(err)
os.Exit(1)
}
//For each certificateprovider
for _, certifcateProviderValue := range *acme {
//For each certificate within one provider
for _, certificate := range certifcateProviderValue.Certificates {
//Get cert destination folder path, and replace * (Wildcard) with "_"
certPath := viper.GetString("dest") + strings.Replace(certificate.Domain.Main, "*", "_", -1) + "/"
os.MkdirAll(certPath, 0755)
os.Chown(certPath, ownerId, groupId)
//Decode private key
privateKey, err := base64.StdEncoding.DecodeString(certificate.Key)
if err != nil {
fmt.Println("Failed to decode private key for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
//Decode fullChain cert
fullChain, err := base64.StdEncoding.DecodeString(certificate.Certificate)
if err != nil {
fmt.Println("Failed to decode fullchain for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
// ------------- [START] Write privateKey to destination path ------------- //
privateKeyFilePath := certPath + "privkey.pem"
//Open or Create privateKey is not exists
privateKeyFile, err := os.OpenFile(privateKeyFilePath, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
fmt.Println("Failed to create privateKey file for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
//Read old privateKey
privateKeyOld, err := ioutil.ReadFile(privateKeyFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//Compate old and new privateKey. If they are identical the file won't be touched
if !reflect.DeepEqual(privateKeyOld, privateKey) {
_, err = privateKeyFile.Write(privateKey)
if err != nil {
fmt.Println("Failed to write privateKey for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
privateKeyFile.Chown(ownerId, groupId)
}
// ------------- [END] Write privateKey to destination path ------------- //
// ------------- [START] Write fullChain to destination path ------------- //
fullChainFilePath := certPath + "fullchain.pem"
//Open or Create fullChain is not exists
fullChainFile, err := os.OpenFile(fullChainFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Failed to create fullChain file for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
//Read old fullChain
fullChainOld, err := ioutil.ReadFile(fullChainFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//Compate old and new fullChain. If they are identical the file won't be touched
if !reflect.DeepEqual(fullChainOld, fullChain) {
_, err = fullChainFile.Write(fullChain)
if err != nil {
fmt.Println("Failed to write fullChain for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
fullChainFile.Chown(ownerId, groupId)
}
// ------------- [END] Write fullChain to destination path ------------- //
//Convert fullChain []byte to string
fullChainString := string(fullChain)
//Get Index where cert ends and chain begins
fullChainIndex := strings.Index(fullChainString, "\n-----BEGIN CERTIFICATE-----")
if fullChainIndex < 0 {
fmt.Println("Could not read fullchain cert")
os.Exit(1)
}
//Cert we need is on top of the file. chain is everything else
cert := fullChainString[:fullChainIndex]
chain := fullChainString[fullChainIndex+1:]
// ------------- [START] Write cert to destination path ------------- //
certFilePath := certPath + "cert.pem"
//Open or Create cert is not exists
certFile, err := os.OpenFile(certFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Failed to create cert file for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
//Read old cert
certOld, err := ioutil.ReadFile(certFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//Compate old and new cert. If they are identical the file won't be touched
if !reflect.DeepEqual(string(certOld), cert) {
_, err = certFile.WriteString(cert)
if err != nil {
fmt.Println("Failed to write cert for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
certFile.Chown(ownerId, groupId)
}
// ------------- [END] Write cert to destination path ------------- //
// ------------- [START] Write chain to destination path ------------- //
chainFilePath := certPath + "chain.pem"
//Open or Create chain is not exists
chainFile, err := os.OpenFile(chainFilePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("Failed to create chain file for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
//Read old chain
chainOld, err := ioutil.ReadFile(chainFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//Compate old and new chain. If they are identical the file won't be touched
if !reflect.DeepEqual(string(chainOld), chain) {
_, err = chainFile.WriteString(chain)
if err != nil {
fmt.Println("Failed to write chain for ", certificate.Domain.Main, " :", err)
os.Exit(1)
}
chainFile.Chown(ownerId, groupId)
}
// ------------- [END] Write chain to destination path ------------- //
}
}
}