-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
certificate.go
170 lines (146 loc) · 6.05 KB
/
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
/*
Copyright 2019 The Jetstack cert-manager contributors.
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 (
"fmt"
"net"
"net/mail"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/jetstack/cert-manager/pkg/api/util"
cmapiv1alpha2 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
cmapi "github.com/jetstack/cert-manager/pkg/internal/apis/certmanager"
cmmeta "github.com/jetstack/cert-manager/pkg/internal/apis/meta"
)
// Validation functions for cert-manager Certificate types
func ValidateCertificateSpec(crt *cmapi.CertificateSpec, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
if crt.SecretName == "" {
el = append(el, field.Required(fldPath.Child("secretName"), "must be specified"))
}
el = append(el, validateIssuerRef(crt.IssuerRef, fldPath)...)
if len(crt.CommonName) == 0 && len(crt.DNSNames) == 0 && len(crt.URISANs) == 0 && len(crt.EmailSANs) == 0 {
el = append(el, field.Invalid(fldPath, "", "at least one of commonName, dnsNames, uriSANs or emailSANs must be set"))
}
// if a common name has been specified, ensure it is no longer than 64 chars
if len(crt.CommonName) > 64 {
el = append(el, field.TooLong(fldPath.Child("commonName"), crt.CommonName, 64))
}
if len(crt.IPAddresses) > 0 {
el = append(el, validateIPAddresses(crt, fldPath)...)
}
if len(crt.EmailSANs) > 0 {
el = append(el, validateEmailAddresses(crt, fldPath)...)
}
if crt.PrivateKey != nil {
switch crt.PrivateKey.Algorithm {
case "", cmapi.RSAKeyAlgorithm:
if crt.PrivateKey.Size > 0 && (crt.PrivateKey.Size < 2048 || crt.PrivateKey.Size > 8192) {
el = append(el, field.Invalid(fldPath.Child("privateKey", "size"), crt.PrivateKey.Size, "must be between 2048 & 8192 for rsa keyAlgorithm"))
}
case cmapi.ECDSAKeyAlgorithm:
if crt.PrivateKey.Size > 0 && crt.PrivateKey.Size != 256 && crt.PrivateKey.Size != 384 && crt.PrivateKey.Size != 521 {
el = append(el, field.NotSupported(fldPath.Child("privateKey", "size"), crt.PrivateKey.Size, []string{"256", "384", "521"}))
}
default:
el = append(el, field.Invalid(fldPath.Child("privateKey", "algorithm"), crt.PrivateKey.Algorithm, "must be either empty or one of rsa or ecdsa"))
}
}
if crt.Duration != nil || crt.RenewBefore != nil {
el = append(el, ValidateDuration(crt, fldPath)...)
}
if len(crt.Usages) > 0 {
el = append(el, validateUsages(crt, fldPath)...)
}
return el
}
func ValidateCertificate(obj runtime.Object) field.ErrorList {
crt := obj.(*cmapi.Certificate)
allErrs := ValidateCertificateSpec(&crt.Spec, field.NewPath("spec"))
return allErrs
}
func validateIssuerRef(issuerRef cmmeta.ObjectReference, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
issuerRefPath := fldPath.Child("issuerRef")
if issuerRef.Name == "" {
el = append(el, field.Required(issuerRefPath.Child("name"), "must be specified"))
}
if issuerRef.Group == "" || issuerRef.Group == cmapi.SchemeGroupVersion.Group {
switch issuerRef.Kind {
case "":
case "Issuer", "ClusterIssuer":
default:
el = append(el, field.Invalid(issuerRefPath.Child("kind"), issuerRef.Kind, "must be one of Issuer or ClusterIssuer"))
}
}
return el
}
func validateIPAddresses(a *cmapi.CertificateSpec, fldPath *field.Path) field.ErrorList {
if len(a.IPAddresses) <= 0 {
return nil
}
el := field.ErrorList{}
for i, d := range a.IPAddresses {
ip := net.ParseIP(d)
if ip == nil {
el = append(el, field.Invalid(fldPath.Child("ipAddresses").Index(i), d, "invalid IP address"))
}
}
return el
}
func validateEmailAddresses(a *cmapi.CertificateSpec, fldPath *field.Path) field.ErrorList {
if len(a.EmailSANs) <= 0 {
return nil
}
el := field.ErrorList{}
for i, d := range a.EmailSANs {
e, err := mail.ParseAddress(d)
if err != nil {
el = append(el, field.Invalid(fldPath.Child("emailSANs").Index(i), d, fmt.Sprintf("invalid email address: %s", err)))
} else if e.Address != d {
// Go accepts email names as per RFC 5322 (name <email>)
// This checks if the supplied value only contains the email address and nothing else
el = append(el, field.Invalid(fldPath.Child("emailSANs").Index(i), d, "invalid email address: make sure the supplied value only contains the email address itself"))
}
}
return el
}
func validateUsages(a *cmapi.CertificateSpec, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
for i, u := range a.Usages {
_, kok := util.KeyUsageType(cmapiv1alpha2.KeyUsage(u))
_, ekok := util.ExtKeyUsageType(cmapiv1alpha2.KeyUsage(u))
if !kok && !ekok {
el = append(el, field.Invalid(fldPath.Child("usages").Index(i), u, "unknown keyusage"))
}
}
return el
}
func ValidateDuration(crt *cmapi.CertificateSpec, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
duration := util.DefaultCertDuration(crt.Duration)
renewBefore := cmapiv1alpha2.DefaultRenewBefore
if crt.RenewBefore != nil {
renewBefore = crt.RenewBefore.Duration
}
if duration < cmapiv1alpha2.MinimumCertificateDuration {
el = append(el, field.Invalid(fldPath.Child("duration"), duration, fmt.Sprintf("certificate duration must be greater than %s", cmapiv1alpha2.MinimumCertificateDuration)))
}
if renewBefore < cmapiv1alpha2.MinimumRenewBefore {
el = append(el, field.Invalid(fldPath.Child("renewBefore"), renewBefore, fmt.Sprintf("certificate renewBefore must be greater than %s", cmapiv1alpha2.MinimumRenewBefore)))
}
if duration <= renewBefore {
el = append(el, field.Invalid(fldPath.Child("renewBefore"), renewBefore, fmt.Sprintf("certificate duration %s must be greater than renewBefore %s", duration, renewBefore)))
}
return el
}