-
Notifications
You must be signed in to change notification settings - Fork 37
/
certify.go
182 lines (156 loc) · 4.71 KB
/
certify.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
package certify
import (
"context"
"crypto/tls"
"errors"
"strings"
"sync"
"time"
"golang.org/x/sync/singleflight"
)
// Certify implements automatic certificate acquisition
// via the configured Issuer.
//
// CommonName and Issuer are required.
// It is recommended that you specify a Cache to prevent requesting a
// new certificate for every incoming connection.
type Certify struct {
// CommonName is the Certificate Common Name
// that will be used when issuing certificates.
// This can be a DNS record or a regular name.
CommonName string
// Issuer is the certificate issuer to use.
Issuer Issuer
// RenewBefore configures how long before
// expiry a certificate should be considered too
// old to use when fetched from the cache.
RenewBefore time.Duration
// Cache is the Cache implementation to use.
Cache Cache
// CertConfig is the certificate configuration that
// should be used. It can be specified to set explicit
// requirements of certificates issued.
CertConfig *CertConfig
// IssueTimeout is the upper bound of time allowed
// per certificate call. Defaults to 1 minute.
IssueTimeout time.Duration
// Logger configures logging of events such as renewals.
// Defaults to no logging. Use one of the adapters in
// https://logur.dev/logur to use with specific
// logging libraries, or implement the interface yourself.
Logger Logger
issueGroup singleflight.Group
initOnce sync.Once
}
func (c *Certify) init() {
if c.Cache == nil {
c.Cache = &noopCache{}
}
if c.Logger == nil {
c.Logger = &noopLogger{}
}
if c.IssueTimeout == 0 {
c.IssueTimeout = time.Minute
}
if c.CertConfig == nil {
c.CertConfig = &CertConfig{}
}
if c.CertConfig.KeyGenerator == nil {
c.CertConfig.KeyGenerator = &singletonKey{}
}
}
// GetCertificate implements the GetCertificate TLS config hook.
func (c *Certify) GetCertificate(hello *tls.ClientHelloInfo) (cert *tls.Certificate, err error) {
c.initOnce.Do(c.init)
defer func() {
if err != nil {
c.Logger.Error("Error getting server certificate", map[string]interface{}{
"error": err.Error(),
})
return
}
}()
name := strings.ToLower(hello.ServerName)
if name == "" {
return nil, errors.New("missing server name")
}
if strings.ContainsAny(name, `/\`) {
return nil, errors.New("server name contains invalid character")
}
// Remove ending dot, if any
name = strings.TrimSuffix(name, ".")
// Remove port, if used
if strings.Contains(name, ":") {
name = strings.Split(name, ":")[0]
}
ctx := getRequestContext(hello)
return c.getOrRenewCert(ctx, name)
}
// GetClientCertificate implements the GetClientCertificate TLS config hook.
func (c *Certify) GetClientCertificate(cri *tls.CertificateRequestInfo) (cert *tls.Certificate, err error) {
c.initOnce.Do(c.init)
defer func() {
if err != nil {
c.Logger.Error("Error getting client certificate", map[string]interface{}{
"error": err.Error(),
})
return
}
}()
ctx := getClientRequestContext(cri)
return c.getOrRenewCert(ctx, c.CommonName)
}
func (c *Certify) getOrRenewCert(ctx context.Context, name string) (*tls.Certificate, error) {
ctx, cancel := context.WithTimeout(ctx, c.IssueTimeout)
defer cancel()
cert, err := c.Cache.Get(ctx, name)
if err == nil {
// If we're not within the renewal threshold of the expiry, return the cert
if time.Now().Before(cert.Leaf.NotAfter.Add(-c.RenewBefore)) {
return cert, nil
}
c.Logger.Debug("Cached certificate found but expiry within renewal threshold", map[string]interface{}{
"serial": cert.Leaf.SerialNumber.String(),
"expiry": cert.Leaf.NotAfter.Format(time.RFC3339),
})
// Delete the cert, we want to renew it
_ = c.Cache.Delete(ctx, name)
} else if err != ErrCacheMiss {
return nil, err
}
// De-duplicate simultaneous requests for the same name
ch := c.issueGroup.DoChan(name, func() (interface{}, error) {
c.Logger.Debug("Requesting new certificate from issuer")
conf := c.CertConfig.Clone()
conf.appendName(name)
// Add CommonName to SANS if not already added
if name != c.CommonName {
conf.appendName(c.CommonName)
}
cert, err := c.Issuer.Issue(ctx, c.CommonName, conf)
if err != nil {
return nil, err
}
c.Logger.Debug("New certificate issued", map[string]interface{}{
"serial": cert.Leaf.SerialNumber.String(),
"expiry": cert.Leaf.NotAfter.Format(time.RFC3339),
})
err = c.Cache.Put(ctx, name, cert)
if err != nil {
c.Logger.Error("Failed to save certificate in cache", map[string]interface{}{
"error": err.Error(),
})
// Ignore error, it'll just mean we renew again next time
}
return cert, nil
})
select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-ch:
if res.Err != nil {
return nil, res.Err
}
return res.Val.(*tls.Certificate), nil
}
}