-
Notifications
You must be signed in to change notification settings - Fork 24
/
rotation.go
184 lines (153 loc) · 5.77 KB
/
rotation.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
package certrotation
import (
"crypto/x509"
"crypto/x509/pkix"
"errors"
"fmt"
"strings"
"time"
"github.com/openshift/library-go/pkg/certs"
"github.com/openshift/library-go/pkg/crypto"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/authentication/user"
)
var (
errMissingIssuer = errors.New("no issuer set")
errMissingHostnames = errors.New("no hostnames set")
errMissingUserInfo = errors.New("no user info")
)
type clockFunc func() time.Time
type signerRotation struct {
Clock clockFunc
Issuer string
}
func (r *signerRotation) NewCertificate(validity time.Duration) (*crypto.TLSCertificateConfig, error) {
if r.Issuer == "" {
return nil, errMissingIssuer
}
signerName := fmt.Sprintf("%s@%d", r.Issuer, time.Now().Unix())
return crypto.MakeSelfSignedCAConfigForDuration(signerName, validity)
}
func (r *signerRotation) NeedNewCertificate(annotations map[string]string, refresh time.Duration) string {
return needNewCertificate(annotations, r.Clock, refresh, nil)
}
func (r *signerRotation) SetAnnotations(ca *crypto.TLSCertificateConfig, annotations map[string]string) {
annotations[CertificateNotAfterAnnotation] = ca.Certs[0].NotAfter.Format(time.RFC3339)
annotations[CertificateNotBeforeAnnotation] = ca.Certs[0].NotBefore.Format(time.RFC3339)
annotations[CertificateIssuer] = ca.Certs[0].Issuer.CommonName
}
type certificateRotation struct {
UserInfo user.Info
Clock clockFunc
Hostnames []string
}
func (r *certificateRotation) NewCertificate(signer *crypto.CA, validity time.Duration) (*crypto.TLSCertificateConfig, error) {
if r.UserInfo == nil {
return nil, errMissingUserInfo
}
if len(r.Hostnames) == 0 {
return nil, errMissingHostnames
}
addClientAuthUsage := func(cert *x509.Certificate) error {
cert.ExtKeyUsage = append(cert.ExtKeyUsage, x509.ExtKeyUsageClientAuth)
return nil
}
addSubject := func(cert *x509.Certificate) error {
cert.Subject = pkix.Name{
CommonName: r.UserInfo.GetName(),
SerialNumber: r.UserInfo.GetUID(),
Organization: r.UserInfo.GetGroups(),
}
return nil
}
return signer.MakeServerCertForDuration(sets.NewString(r.Hostnames...), validity, addClientAuthUsage, addSubject)
}
func (r *certificateRotation) NeedNewCertificate(annotations map[string]string, signer *crypto.CA, caBundleCerts []*x509.Certificate, refresh time.Duration) string {
reason := needNewCertificate(annotations, r.Clock, refresh, signer)
if len(reason) > 0 {
return reason
}
// check the signer common name against all the common names in our ca bundle so we don't refresh early
signerCommonName := annotations[CertificateIssuer]
if signerCommonName == "" {
return "missing issuer name"
}
var found bool
for _, caCert := range caBundleCerts {
if signerCommonName == caCert.Subject.CommonName {
found = true
break
}
}
if !found {
return fmt.Sprintf("issuer %q, not in ca bundle:\n%s", signerCommonName, certs.CertificateBundleToString(caBundleCerts))
}
existingHostnames := sets.NewString(strings.Split(annotations[CertificateHostnames], ",")...)
requiredHostnames := sets.NewString(r.Hostnames...)
if !existingHostnames.Equal(requiredHostnames) {
existingNotRequired := existingHostnames.Difference(requiredHostnames)
requiredNotExisting := requiredHostnames.Difference(existingHostnames)
return fmt.Sprintf("hostnames %q are existing and not required, %q are required and not existing", strings.Join(existingNotRequired.List(), ","), strings.Join(requiredNotExisting.List(), ","))
}
return ""
}
func (r *certificateRotation) SetAnnotations(cert *crypto.TLSCertificateConfig, annotations map[string]string) {
hostnames := sets.Set[string]{}
for _, ip := range cert.Certs[0].IPAddresses {
hostnames.Insert(ip.String())
}
for _, dnsName := range cert.Certs[0].DNSNames {
hostnames.Insert(dnsName)
}
annotations[CertificateNotAfterAnnotation] = cert.Certs[0].NotAfter.Format(time.RFC3339)
annotations[CertificateNotBeforeAnnotation] = cert.Certs[0].NotBefore.Format(time.RFC3339)
annotations[CertificateIssuer] = cert.Certs[0].Issuer.CommonName
// List does a sort so that we have a consistent representation
annotations[CertificateHostnames] = strings.Join(hostnames.UnsortedList(), ",")
}
func needNewCertificate(annotations map[string]string, clock clockFunc, refresh time.Duration, signer *crypto.CA) string {
notAfterString := annotations[CertificateNotAfterAnnotation]
if len(notAfterString) == 0 {
return "missing notAfter"
}
notAfter, err := time.Parse(time.RFC3339, notAfterString)
if err != nil {
return fmt.Sprintf("bad expiry: %q", notAfterString)
}
notBeforeString := annotations[CertificateNotBeforeAnnotation]
if len(notAfterString) == 0 {
return "missing notBefore"
}
notBefore, err := time.Parse(time.RFC3339, notBeforeString)
if err != nil {
return fmt.Sprintf("bad expiry: %q", notBeforeString)
}
now := clock()
// Is cert expired?
if now.After(notAfter) {
return "already expired"
}
// Refresh only when expired
validity := notAfter.Sub(notBefore)
if validity == refresh {
return ""
}
// Are we at 80% of validity?
at80Percent := notAfter.Add(-validity / 5)
if now.After(at80Percent) {
return fmt.Sprintf("past its latest possible time %v", at80Percent)
}
// If Certificate is past its refresh time, we may have action to take. We only do this if the signer is old enough.
developerSpecifiedRefresh := notBefore.Add(refresh)
if now.After(developerSpecifiedRefresh) {
if signer == nil {
return fmt.Sprintf("past its refresh time %v", developerSpecifiedRefresh)
}
// make sure the signer has been valid for more than 10% of the target's refresh time.
timeToWaitForTrustRotation := refresh / 10
if now.After(signer.Config.Certs[0].NotBefore.Add(timeToWaitForTrustRotation)) {
return fmt.Sprintf("past its refresh time %v", developerSpecifiedRefresh)
}
}
return ""
}