-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-certificate.go
363 lines (301 loc) · 9.36 KB
/
get-certificate.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package nameshift
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/certmagic"
"go.uber.org/zap"
)
var mutex sync.RWMutex = sync.RWMutex{}
func tlsCertFromCertAndKeyPEMBundle(bundle []byte) (tls.Certificate, error) {
certBuilder, keyBuilder := new(bytes.Buffer), new(bytes.Buffer)
var foundKey bool // use only the first key in the file
for {
// Decode next block so we can see what type it is
var derBlock *pem.Block
derBlock, bundle = pem.Decode(bundle)
if derBlock == nil {
break
}
if derBlock.Type == "CERTIFICATE" {
// Re-encode certificate as PEM, appending to certificate chain
if err := pem.Encode(certBuilder, derBlock); err != nil {
return tls.Certificate{}, err
}
} else if derBlock.Type == "EC PARAMETERS" {
// EC keys generated from openssl can be composed of two blocks:
// parameters and key (parameter block should come first)
if !foundKey {
// Encode parameters
if err := pem.Encode(keyBuilder, derBlock); err != nil {
return tls.Certificate{}, err
}
// Key must immediately follow
derBlock, bundle = pem.Decode(bundle)
if derBlock == nil || derBlock.Type != "EC PRIVATE KEY" {
return tls.Certificate{}, fmt.Errorf("expected elliptic private key to immediately follow EC parameters")
}
if err := pem.Encode(keyBuilder, derBlock); err != nil {
return tls.Certificate{}, err
}
foundKey = true
}
} else if derBlock.Type == "PRIVATE KEY" || strings.HasSuffix(derBlock.Type, " PRIVATE KEY") {
// RSA key
if !foundKey {
if err := pem.Encode(keyBuilder, derBlock); err != nil {
return tls.Certificate{}, err
}
foundKey = true
}
} else {
return tls.Certificate{}, fmt.Errorf("unrecognized PEM block type: %s", derBlock.Type)
}
}
certPEMBytes, keyPEMBytes := certBuilder.Bytes(), keyBuilder.Bytes()
if len(certPEMBytes) == 0 {
return tls.Certificate{}, fmt.Errorf("failed to parse PEM data")
}
if len(keyPEMBytes) == 0 {
return tls.Certificate{}, fmt.Errorf("no private key block found")
}
// if the start of the key file looks like an encrypted private key,
// reject it with a helpful error message
if strings.HasPrefix(string(keyPEMBytes[:40]), "ENCRYPTED") {
return tls.Certificate{}, fmt.Errorf("encrypted private keys are not supported; please decrypt the key first")
}
cert, err := tls.X509KeyPair(certPEMBytes, keyPEMBytes)
if err != nil {
return tls.Certificate{}, fmt.Errorf("making X509 key pair: %v", err)
}
return cert, nil
}
func init() {
caddy.RegisterModule(NameshiftCertGetter{})
}
type CertificateResponse struct {
Id string `json:"id"`
Certificate string `json:"certificate"`
}
var (
domainCertMap map[string]string
certificates map[string]*tls.Certificate
)
// NameshiftCertGetter can get a certificate via HTTP(S) request.
type NameshiftCertGetter struct {
// The URL from which to download the certificate. Required.
//
// The URL will be augmented with query string parameters taken
// from the TLS handshake:
//
// - server_name: The SNI value
//
// To be valid, the response must be HTTP 200 with a PEM body
// consisting of blocks for the certificate chain and the private
// key.
//
// To indicate that this manager is not managing a certificate for
// the described handshake, the endpoint should return HTTP 204
// (No Content). Error statuses will indicate that the manager is
// capable of providing a certificate but was unable to.
URL string `json:"url,omitempty"`
LocalCache string `json:"local_cache,omitempty"`
logger *zap.Logger
ctx context.Context
}
// CaddyModule returns the Caddy module information.
func (hcg NameshiftCertGetter) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "tls.get_certificate.nameshift",
New: func() caddy.Module { return new(NameshiftCertGetter) },
}
}
func (hcg *NameshiftCertGetter) Provision(ctx caddy.Context) error {
hcg.ctx = ctx
hcg.logger = ctx.Logger(hcg)
domainCertMap = make(map[string]string)
certificates = make(map[string]*tls.Certificate)
if hcg.URL == "" {
return fmt.Errorf("URL is required")
}
if hcg.LocalCache != "" {
hcg.logger.Debug(fmt.Sprintf("Loading local certificates from cache: %s", hcg.LocalCache))
files, err := os.ReadDir(hcg.LocalCache)
if err != nil {
os.MkdirAll(hcg.LocalCache, 0770)
hcg.logger.Error("Could not read local cache dir, creating...")
return nil
}
for _, file := range files {
if !file.IsDir() {
// grab contents
hcg.logger.Debug(fmt.Sprintf("Loading certificate %s from disk", file.Name()))
fullname := filepath.Join(hcg.LocalCache, file.Name())
contents, err := os.ReadFile(fullname)
if err != nil {
log.Fatal(err)
}
// @TODO @mastercoding Order certificates by expiry date desc and don't
// override domainMap[cert] if it already has one
cert, err := tlsCertFromCertAndKeyPEMBundle(contents)
if err == nil {
if time.Now().After(cert.Leaf.NotAfter) {
hcg.logger.Debug(fmt.Sprintf("Certificate %s expired: %s", file.Name(), cert.Leaf.NotAfter))
os.Remove(fullname)
continue
}
hcg.loadCertificateIntoMemoryCache(file.Name(), &cert)
}
}
}
}
return nil
}
func (hcg NameshiftCertGetter) storeCertificateToDisk(id string, cert *[]byte) {
os.WriteFile(
filepath.Join(hcg.LocalCache, id),
*cert,
0644,
)
}
func HasCertificate(name string) bool {
mutex.RLock()
defer mutex.RUnlock()
_, ok := domainCertMap[name]
return ok
}
func (hcg NameshiftCertGetter) loadCertificateIntoMemoryCache(id string, cert *tls.Certificate) {
mutex.Lock()
defer mutex.Unlock()
// store in cache
certificates[id] = cert
hcg.logger.Debug(fmt.Sprintf("Storing certificate %s in cache. DNS Names: %s", id, cert.Leaf.DNSNames))
for _, element := range cert.Leaf.DNSNames {
domainCertMap[element] = id
}
}
func (hcg NameshiftCertGetter) fetchCertificate(name string) (*tls.Certificate, error) {
hcg.logger.Debug(fmt.Sprintf("Fetching certificate from API for %s.", name))
if name == "" {
return nil, fmt.Errorf("ignoring empty name")
}
// ignore ips
if name == "168.220.85.117" || name == "2a09:8280:1::50:73de:0" {
return nil, fmt.Errorf("ignoring %s, it is an IP", name)
}
parsed, err := url.Parse(hcg.URL)
if err != nil {
return nil, err
}
qs := parsed.Query()
qs.Set("server_name", name)
parsed.RawQuery = qs.Encode()
req, err := http.NewRequestWithContext(hcg.ctx, http.MethodGet, parsed.String(), nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
// no certificate found right now
return nil, nil
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got HTTP %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
var result CertificateResponse
if err := json.Unmarshal(bodyBytes, &result); err != nil { // Parse []byte to go struct pointer
fmt.Println("Can not unmarshal JSON")
}
pem := []byte(result.Certificate)
cert, err := tlsCertFromCertAndKeyPEMBundle(pem)
if err != nil {
return nil, err
}
hcg.loadCertificateIntoMemoryCache(result.Id, &cert)
hcg.storeCertificateToDisk(result.Id, &pem)
return &cert, nil
}
func (hcg NameshiftCertGetter) GetCertificate(ctx context.Context, hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
// check if we have the certificate cached
mutex.RLock()
certificateId, ok := domainCertMap[hello.ServerName]
mutex.RUnlock()
if ok {
hcg.logger.Debug(fmt.Sprintf("Cached certificate found for %s. Certificate id: %s", hello.ServerName, certificateId))
mutex.RLock()
cert := certificates[certificateId]
mutex.RUnlock()
// check if expired
if time.Now().After(cert.Leaf.NotAfter) {
hcg.logger.Debug(fmt.Sprintf("Cached certificate for %s was expired, removing. Certificate id: %s", hello.ServerName, certificateId))
mutex.Lock()
delete(certificates, certificateId)
mutex.Unlock()
} else {
// epxires soon
if time.Now().AddDate(0, 0, 5).After(cert.Leaf.NotAfter) {
hcg.logger.Debug(fmt.Sprintf("Cached certificate for %s is expiring soon (%s), fetching new one. Certificate id: %s", hello.ServerName, cert.Leaf.NotAfter, certificateId))
defer hcg.fetchCertificate(hello.ServerName)
}
return cert, nil
}
}
return hcg.fetchCertificate(hello.ServerName)
}
// UnmarshalCaddyfile deserializes Caddyfile tokens into ts.
//
// ... nameshift <url>
func (hcg *NameshiftCertGetter) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Next() // consume cert manager name
if d.NextArg() {
hcg.URL = d.Val()
}
if d.NextArg() {
return d.ArgErr()
}
for d.NextBlock(0) {
switch d.Val() {
case "local_cache":
if !d.NextArg() {
return d.ArgErr()
}
hcg.LocalCache = d.Val()
case "url":
if !d.NextArg() {
return d.ArgErr()
}
hcg.URL = d.Val()
default:
return d.Errf("unrecognized subdirective %q", d.Val())
}
}
return nil
}
// Interface guards
var (
_ certmagic.Manager = (*NameshiftCertGetter)(nil)
_ caddy.Provisioner = (*NameshiftCertGetter)(nil)
_ caddyfile.Unmarshaler = (*NameshiftCertGetter)(nil)
)