-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
257 lines (222 loc) · 5.92 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
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/ettle/strcase"
"github.com/go-acme/lego/v4/lego"
"github.com/traefik/traefik/v2/pkg/provider/acme"
"github.com/urfave/cli/v2"
)
const (
flagSrcFile = "src"
flagDstFile = "dst"
flagDomain = "domain"
flagResolverName = "resolver-name"
flagRevoke = "revoke"
flagDryRun = "dry-run"
)
type configuration struct {
Source string
Destination string
Domain string
ResolverName string
Revoke bool
DryRun bool
}
func newConfiguration(cliCtx *cli.Context) configuration {
return configuration{
Source: cliCtx.Path(flagSrcFile),
Destination: cliCtx.Path(flagDstFile),
Domain: cliCtx.String(flagDomain),
ResolverName: cliCtx.String(flagResolverName),
Revoke: cliCtx.Bool(flagRevoke),
DryRun: cliCtx.Bool(flagDryRun),
}
}
func main() {
app := &cli.App{
Name: "traefik-certs-cleaner",
Description: "Clean ACME certificates from Traefik acme.json file.",
Usage: "Traefik Certificates Cleaner",
Flags: []cli.Flag{
&cli.PathFlag{
Name: flagSrcFile,
Aliases: []string{"s"},
Usage: "Path to the acme.json file.",
EnvVars: []string{strcase.ToSNAKE(flagSrcFile)},
Value: "./acme.json",
},
&cli.PathFlag{
Name: flagDstFile,
Aliases: []string{"o"},
Usage: "Path to the output of the acme.json file.",
EnvVars: []string{strcase.ToSNAKE(flagDstFile)},
Value: "./acme-new.json",
},
&cli.StringFlag{
Name: flagResolverName,
Aliases: []string{"r"},
Usage: "Name of the resolver. Use * to handle all resolvers.",
EnvVars: []string{strcase.ToSNAKE(flagResolverName)},
Value: "*",
},
&cli.StringFlag{
Name: flagDomain,
Aliases: []string{"d"},
Usage: "Domains to remove. Use * to remove all certificates.",
EnvVars: []string{strcase.ToSNAKE(flagDomain)},
Value: "*",
},
&cli.BoolFlag{
Name: flagRevoke,
Usage: "Revoke certificates",
EnvVars: []string{strcase.ToSNAKE(flagRevoke)},
Value: false,
},
&cli.BoolFlag{
Name: flagDryRun,
Usage: "Dry run mode.",
EnvVars: []string{strcase.ToSNAKE(flagDryRun)},
Value: true,
},
},
Action: func(cliCtx *cli.Context) error {
return cleaner{configuration: newConfiguration(cliCtx)}.run()
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal("Error while executing command ", err)
}
}
type cleaner struct {
configuration
}
func (c cleaner) run() error {
data := map[string]*acme.StoredData{}
err := readJSONFile(c.Source, &data)
if err != nil {
return err
}
err = c.clean(c.configuration, data)
if err != nil {
return err
}
var encoder *json.Encoder
if c.DryRun {
encoder = json.NewEncoder(os.Stdout)
} else {
output, err := os.Create(c.Destination)
if err != nil {
return err
}
defer func() { _ = output.Close() }()
encoder = json.NewEncoder(output)
}
encoder.SetIndent("", " ")
return encoder.Encode(data)
}
func (c cleaner) clean(config configuration, data map[string]*acme.StoredData) error {
for rName, storedData := range data {
if config.ResolverName != "*" && config.ResolverName != rName {
continue
}
if config.Domain == "*" {
c.revoke(storedData.Account, storedData.Certificates)
storedData.Certificates = make([]*acme.CertAndStore, 0)
continue
}
var keep []*acme.CertAndStore
var toRevoke []*acme.CertAndStore
for _, cert := range storedData.Certificates {
if strings.HasSuffix(cert.Domain.Main, config.Domain) || containsSuffixes(cert.Domain.SANs, config.Domain) {
toRevoke = append(toRevoke, cert)
continue
}
if strings.HasSuffix(cert.Certificate.Domain.Main, config.Domain) || containsSuffixes(cert.Certificate.Domain.SANs, config.Domain) {
toRevoke = append(toRevoke, cert)
continue
}
certificate, err := getX509Certificate(&cert.Certificate)
if err != nil {
return err
}
if strings.HasSuffix(certificate.Subject.CommonName, config.Domain) || containsSuffixes(certificate.DNSNames, config.Domain) {
toRevoke = append(toRevoke, cert)
continue
}
keep = append(keep, cert)
}
storedData.Certificates = keep
c.revoke(storedData.Account, toRevoke)
}
return nil
}
func (c cleaner) revoke(account *acme.Account, certificates []*acme.CertAndStore) {
if !c.Revoke {
return
}
if !c.DryRun {
log.Println("Revoke certificate")
return
}
config := lego.NewConfig(account)
config.CADirURL = lego.LEDirectoryProduction
config.UserAgent = "ldez-traefik-certs-cleaner"
client, err := lego.NewClient(config)
if err != nil {
log.Fatalf("Could not create client: %v", err)
}
for _, certificate := range certificates {
err := client.Certificate.Revoke(certificate.Certificate.Certificate)
if err != nil {
log.Printf("Failed to revoke certificate for %s: %v", certificate.Domain, err)
}
}
}
func containsSuffixes(domains []string, suffix string) bool {
for _, domain := range domains {
if strings.HasSuffix(domain, suffix) {
return true
}
}
return false
}
func getX509Certificate(cert *acme.Certificate) (*x509.Certificate, error) {
tlsCert, err := tls.X509KeyPair(cert.Certificate, cert.Key)
if err != nil {
return nil, err
}
crt := tlsCert.Leaf
if crt == nil {
crt, err = x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, err
}
}
return crt, err
}
func readJSONFile(acmeFile string, data interface{}) error {
source, err := os.Open(filepath.Clean(acmeFile))
if err != nil {
return fmt.Errorf("failed to open file %q: %w", acmeFile, err)
}
defer func() { _ = source.Close() }()
err = json.NewDecoder(source).Decode(data)
if errors.Is(err, io.EOF) {
log.Printf("warn: file %q may not be ready: %v", acmeFile, err)
return nil
}
if err != nil {
return fmt.Errorf("failed to unmarshal file %q: %w", acmeFile, err)
}
return nil
}