-
Notifications
You must be signed in to change notification settings - Fork 2k
/
certificaterequest.go
307 lines (257 loc) · 11 KB
/
certificaterequest.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
/*
Copyright 2020 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validation
import (
"crypto/x509"
"encoding/asn1"
"fmt"
"reflect"
"strings"
"github.com/kr/pretty"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
cmapi "github.com/cert-manager/cert-manager/internal/apis/certmanager"
cmmeta "github.com/cert-manager/cert-manager/internal/apis/meta"
"github.com/cert-manager/cert-manager/internal/webhook/feature"
"github.com/cert-manager/cert-manager/pkg/apis/acme"
"github.com/cert-manager/cert-manager/pkg/apis/certmanager"
"github.com/cert-manager/cert-manager/pkg/util"
utilfeature "github.com/cert-manager/cert-manager/pkg/util/feature"
"github.com/cert-manager/cert-manager/pkg/util/pki"
)
var defaultInternalKeyUsages = []cmapi.KeyUsage{cmapi.UsageDigitalSignature, cmapi.UsageKeyEncipherment}
func ValidateCertificateRequest(a *admissionv1.AdmissionRequest, obj runtime.Object) (field.ErrorList, []string) {
cr := obj.(*cmapi.CertificateRequest)
allErrs := ValidateCertificateRequestSpec(&cr.Spec, field.NewPath("spec"))
allErrs = append(allErrs,
ValidateCertificateRequestApprovalCondition(cr.Status.Conditions, field.NewPath("status", "conditions"))...)
return allErrs, nil
}
func ValidateUpdateCertificateRequest(a *admissionv1.AdmissionRequest, oldObj, newObj runtime.Object) (field.ErrorList, []string) {
oldCR, newCR := oldObj.(*cmapi.CertificateRequest), newObj.(*cmapi.CertificateRequest)
var el field.ErrorList
// Enforce that no cert-manager annotations may be modified after creation.
// This is to prevent changing the request during processing resulting in
// undefined behaviour, and breaking the concept of requests being made by a
// single user.
annotationField := field.NewPath("metadata", "annotations")
el = append(el, validateCertificateRequestAnnotations(oldCR, newCR, annotationField)...)
el = append(el, validateCertificateRequestAnnotations(newCR, oldCR, annotationField)...)
el = append(el,
ValidateUpdateCertificateRequestApprovalCondition(oldCR.Status.Conditions, newCR.Status.Conditions, field.NewPath("status", "conditions"))...)
if !reflect.DeepEqual(oldCR.Spec, newCR.Spec) {
el = append(el, field.Forbidden(field.NewPath("spec"), "cannot change spec after creation"))
}
return el, nil
}
func validateCertificateRequestAnnotations(objA, objB *cmapi.CertificateRequest, fieldPath *field.Path) field.ErrorList {
var el field.ErrorList
for k, v := range objA.Annotations {
if strings.HasPrefix(k, certmanager.GroupName) ||
strings.HasPrefix(k, acme.GroupName) {
if vnew, ok := objB.Annotations[k]; !ok || v != vnew {
el = append(el, field.Forbidden(fieldPath.Child(k), "cannot change cert-manager annotation after creation"))
}
}
}
return el
}
func ValidateCertificateRequestSpec(crSpec *cmapi.CertificateRequestSpec, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
el = append(el, validateIssuerRef(crSpec.IssuerRef, fldPath)...)
if len(crSpec.Request) == 0 {
el = append(el, field.Required(fldPath.Child("request"), "must be specified"))
} else {
csr, err := pki.DecodeX509CertificateRequestBytes(crSpec.Request)
if err != nil {
el = append(el, field.Invalid(fldPath.Child("request"), crSpec.Request, fmt.Sprintf("failed to decode csr: %s", err)))
} else {
// in case DontAllowInsecureCSRUsageDefinition is disabled: only compare usages if set on the CR
// otherwise: always compare usages
if utilfeature.DefaultMutableFeatureGate.Enabled(feature.DontAllowInsecureCSRUsageDefinition) || len(crSpec.Usages) > 0 {
// set capacity to length to obtain a "copy-on-append" slice
crUsages := crSpec.Usages[:len(crSpec.Usages):len(crSpec.Usages)]
if len(crUsages) == 0 {
crUsages = defaultInternalKeyUsages[:len(defaultInternalKeyUsages):len(defaultInternalKeyUsages)]
}
if crSpec.IsCA {
crUsages = ensureCertSignIsSet(crUsages)
}
csrUsages, err := getCSRKeyUsage(crSpec, fldPath, csr, el)
if len(err) > 0 {
el = append(el, err...)
}
if len(csrUsages) > 0 && !isUsageEqual(csrUsages, crUsages) {
el = append(el, field.Invalid(fldPath.Child("request"), crSpec.Request, fmt.Sprintf("csr key usages do not match specified usages, these should match if both are set: %s", pretty.Diff(patchDuplicateKeyUsage(csrUsages), patchDuplicateKeyUsage(crUsages)))))
}
}
}
}
return el
}
// ValidateCertificateRequestApprovalCondition will ensure that only a single
// 'Approved' or 'Denied' condition may exist, and that they are set to True.
func ValidateCertificateRequestApprovalCondition(crConds []cmapi.CertificateRequestCondition, fldPath *field.Path) field.ErrorList {
var (
approvedConditions []cmapi.CertificateRequestCondition
deniedConditions []cmapi.CertificateRequestCondition
el = field.ErrorList{}
)
for _, cond := range crConds {
if cond.Type == cmapi.CertificateRequestConditionApproved {
approvedConditions = append(approvedConditions, cond)
}
if cond.Type == cmapi.CertificateRequestConditionDenied {
deniedConditions = append(deniedConditions, cond)
}
}
for _, condType := range []struct {
condType cmapi.CertificateRequestConditionType
found []cmapi.CertificateRequestCondition
}{
{cmapi.CertificateRequestConditionApproved, approvedConditions},
{cmapi.CertificateRequestConditionDenied, deniedConditions},
} {
if len(condType.found) == 0 {
continue
}
if len(condType.found) > 1 {
el = append(el, field.Forbidden(fldPath, fmt.Sprintf("multiple %q conditions present", condType.condType)))
continue
}
first := condType.found[0]
if first.Status != cmmeta.ConditionTrue {
el = append(el, field.Invalid(fldPath.Child(string(first.Type)), first.Status,
fmt.Sprintf("%q condition may only be set to True", condType.condType)))
continue
}
}
if len(deniedConditions) > 0 && len(approvedConditions) > 0 {
el = append(el, field.Forbidden(fldPath, "both 'Denied' and 'Approved' conditions cannot coexist"))
}
return el
}
// ValidateUpdateCertificateRequestApprovalCondition will ensure that the
// 'Approved' and 'Denied' conditions may not be changed once set, i.e. if they
// exist, they are not modified in the updated resource. Also runs the base
// approval validation on the updated CertificateRequest conditions.
func ValidateUpdateCertificateRequestApprovalCondition(oldCRConds, newCRConds []cmapi.CertificateRequestCondition, fldPath *field.Path) field.ErrorList {
var (
el = field.ErrorList{}
oldCRDenied = getCertificateRequestCondition(oldCRConds, cmapi.CertificateRequestConditionDenied)
oldCRApproved = getCertificateRequestCondition(oldCRConds, cmapi.CertificateRequestConditionApproved)
)
// If the approval condition has been set, ensure it hasn't been modified.
if oldCRApproved != nil && !reflect.DeepEqual(oldCRApproved,
getCertificateRequestCondition(newCRConds, cmapi.CertificateRequestConditionApproved),
) {
el = append(el, field.Forbidden(fldPath, "'Approved' condition may not be modified once set"))
}
// If the denied condition has been set, ensure it hasn't been modified.
if oldCRDenied != nil && !reflect.DeepEqual(oldCRDenied,
getCertificateRequestCondition(newCRConds, cmapi.CertificateRequestConditionDenied),
) {
el = append(el, field.Forbidden(fldPath, "'Denied' condition may not be modified once set"))
}
return append(el, ValidateCertificateRequestApprovalCondition(newCRConds, fldPath)...)
}
func getCSRKeyUsage(crSpec *cmapi.CertificateRequestSpec, fldPath *field.Path, csr *x509.CertificateRequest, el field.ErrorList) ([]cmapi.KeyUsage, field.ErrorList) {
var ekus []x509.ExtKeyUsage
var ku x509.KeyUsage
for _, extension := range csr.Extensions {
if extension.Id.String() == asn1.ObjectIdentifier(pki.OIDExtensionExtendedKeyUsage).String() {
var asn1ExtendedUsages []asn1.ObjectIdentifier
_, err := asn1.Unmarshal(extension.Value, &asn1ExtendedUsages)
if err != nil {
el = append(el, field.Invalid(fldPath.Child("request"), crSpec.Request, fmt.Sprintf("failed to decode csr extended usages: %s", err)))
} else {
for _, asnExtUsage := range asn1ExtendedUsages {
eku, ok := pki.ExtKeyUsageFromOID(asnExtUsage)
if ok {
ekus = append(ekus, eku)
}
}
}
}
if extension.Id.String() == asn1.ObjectIdentifier(pki.OIDExtensionKeyUsage).String() {
// RFC 5280, 4.2.1.3
var asn1bits asn1.BitString
_, err := asn1.Unmarshal(extension.Value, &asn1bits)
if err != nil {
el = append(el, field.Invalid(fldPath.Child("request"), crSpec.Request, fmt.Sprintf("failed to decode csr usages: %s", err)))
} else {
var usage int
for i := 0; i < 9; i++ {
if asn1bits.At(i) != 0 {
usage |= 1 << uint(i)
}
}
ku = x509.KeyUsage(usage)
}
}
}
// convert usages to the internal API
var out []cmapi.KeyUsage
for _, usage := range pki.BuildCertManagerKeyUsages(ku, ekus) {
out = append(out, cmapi.KeyUsage(usage))
}
return out, el
}
func patchDuplicateKeyUsage(usages []cmapi.KeyUsage) []cmapi.KeyUsage {
// usage signing and digital signature are the same key use in x509
// we should patch this for proper validation
newUsages := []cmapi.KeyUsage(nil)
hasUsageSigning := false
for _, usage := range usages {
if (usage == cmapi.UsageSigning || usage == cmapi.UsageDigitalSignature) && !hasUsageSigning {
newUsages = append(newUsages, cmapi.UsageDigitalSignature)
// prevent having 2 UsageDigitalSignature in the slice
hasUsageSigning = true
} else if usage != cmapi.UsageSigning && usage != cmapi.UsageDigitalSignature {
newUsages = append(newUsages, usage)
}
}
return newUsages
}
func isUsageEqual(a, b []cmapi.KeyUsage) bool {
a = patchDuplicateKeyUsage(a)
b = patchDuplicateKeyUsage(b)
var aStrings, bStrings []string
for _, usage := range a {
aStrings = append(aStrings, string(usage))
}
for _, usage := range b {
bStrings = append(bStrings, string(usage))
}
return util.EqualUnsorted(aStrings, bStrings)
}
// ensureCertSignIsSet adds UsageCertSign in case it is not set
// TODO: add a mutating webhook to make sure this is always set
// when isCA is true.
func ensureCertSignIsSet(list []cmapi.KeyUsage) []cmapi.KeyUsage {
for _, usage := range list {
if usage == cmapi.UsageCertSign {
return list
}
}
return append(list, cmapi.UsageCertSign)
}
func getCertificateRequestCondition(conds []cmapi.CertificateRequestCondition, conditionType cmapi.CertificateRequestConditionType) *cmapi.CertificateRequestCondition {
for _, cond := range conds {
if cond.Type == conditionType {
return &cond
}
}
return nil
}