Skip to content

Commit 5ad23ae

Browse files
committed
Validate certificate.spec.secretName is a valid k8s resource name
Signed-off-by: Avi Sharma <avi.08.sh@gmail.com>
1 parent 4620bb9 commit 5ad23ae

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

internal/apis/certmanager/validation/certificate.go

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func ValidateCertificateSpec(crt *internalcmapi.CertificateSpec, fldPath *field.
4444
el := field.ErrorList{}
4545
if crt.SecretName == "" {
4646
el = append(el, field.Required(fldPath.Child("secretName"), "must be specified"))
47+
} else {
48+
for _, msg := range apivalidation.NameIsDNSSubdomain(crt.SecretName, false) {
49+
el = append(el, field.Invalid(fldPath.Child("secretName"), crt.SecretName, msg))
50+
}
4751
}
4852

4953
el = append(el, validateIssuerRef(crt.IssuerRef, fldPath)...)

internal/apis/certmanager/validation/certificate_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ func TestValidateCertificate(t *testing.T) {
142142
},
143143
a: someAdmissionRequest,
144144
},
145+
"certificate invalid secretName": {
146+
cfg: &internalcmapi.Certificate{
147+
Spec: internalcmapi.CertificateSpec{
148+
CommonName: "testcn",
149+
IssuerRef: validIssuerRef,
150+
SecretName: "testFoo",
151+
},
152+
},
153+
errs: []*field.Error{
154+
field.Invalid(fldPath.Child("secretName"), "testFoo", "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')"),
155+
},
156+
a: someAdmissionRequest,
157+
},
145158
"certificate with no domains, URIs or common name": {
146159
cfg: &internalcmapi.Certificate{
147160
Spec: internalcmapi.CertificateSpec{

0 commit comments

Comments
 (0)