11package graph
22
33import (
4+ "errors"
45 "fmt"
56 "slices"
67 "strings"
@@ -81,17 +82,37 @@ func validateBackendTLSPolicy(
8182
8283 caCertRefs := backendTLSPolicy .Spec .Validation .CACertificateRefs
8384 wellKnownCerts := backendTLSPolicy .Spec .Validation .WellKnownCACertificates
85+
86+ // Check mutual exclusivity
8487 switch {
8588 case len (caCertRefs ) > 0 && wellKnownCerts != nil :
8689 valid = false
8790 msg := "CACertificateRefs and WellKnownCACertificates are mutually exclusive"
8891 conds = append (conds , conditions .NewPolicyInvalid (msg ))
8992
9093 case len (caCertRefs ) > 0 :
94+ // Validate CACertificateRefs
9195 if err := validateBackendTLSCACertRef (backendTLSPolicy , configMapResolver , secretResolver ); err != nil {
9296 valid = false
93- conds = append (conds , conditions .NewPolicyInvalid (
94- fmt .Sprintf ("invalid CACertificateRef: %s" , err .Error ())))
97+
98+ // Determine the specific condition based on error type
99+ {
100+ var e * invalidKindError
101+ var e1 * invalidCACertificateRefError
102+ switch {
103+ case errors .As (err , & e ):
104+ conds = append (conds , conditions .NewBackendTLSPolicyInvalidKind (e .Error ()))
105+ conds = append (conds , conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ))
106+ case errors .As (err , & e1 ):
107+ conds = append (conds , conditions .NewBackendTLSPolicyInvalidCACertificateRef (e1 .Error ()))
108+ conds = append (conds , conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ))
109+ default :
110+ conds = append (conds , conditions .NewPolicyInvalid (fmt .Sprintf ("invalid CACertificateRef: %s" , err .Error ())))
111+ }
112+ }
113+ } else if valid {
114+ // Only set ResolvedRefs to true if CACertificateRefs are valid AND overall policy is valid
115+ conds = append (conds , conditions .NewBackendTLSPolicyResolvedRefs ())
95116 }
96117
97118 case wellKnownCerts != nil :
@@ -103,8 +124,12 @@ func validateBackendTLSPolicy(
103124
104125 default :
105126 valid = false
106- conds = append (conds , conditions .NewPolicyInvalid ("CACertRefs and WellKnownCACerts are both nil" ))
127+ conds = append (
128+ conds ,
129+ conditions .NewPolicyInvalid ("either CACertificateRefs or WellKnownCACertificates must be specified" ),
130+ )
107131 }
132+
108133 return valid , ignored , conds
109134}
110135
@@ -119,6 +144,24 @@ func validateBackendTLSHostname(btp *v1alpha3.BackendTLSPolicy) error {
119144 return nil
120145}
121146
147+ // invalidCACertificateRefError indicates an error with a CACertificateRef resolution or misconfiguration.
148+ type invalidCACertificateRefError struct {
149+ err error
150+ }
151+
152+ func (e * invalidCACertificateRefError ) Error () string {
153+ return e .err .Error ()
154+ }
155+
156+ // invalidKindError indicates an error with an unknown or unsupported resource kind.
157+ type invalidKindError struct {
158+ err error
159+ }
160+
161+ func (e * invalidKindError ) Error () string {
162+ return e .err .Error ()
163+ }
164+
122165func validateBackendTLSCACertRef (
123166 btp * v1alpha3.BackendTLSPolicy ,
124167 configMapResolver * configMapResolver ,
@@ -136,13 +179,13 @@ func validateBackendTLSCACertRef(
136179 if ! slices .Contains (allowedCaCertKinds , selectedCertRef .Kind ) {
137180 path := field .NewPath ("validation.caCertificateRefs[0].kind" )
138181 valErr := field .NotSupported (path , btp .Spec .Validation .CACertificateRefs [0 ].Kind , allowedCaCertKinds )
139- return valErr
182+ return & invalidKindError { valErr }
140183 }
141184 if selectedCertRef .Group != "" &&
142185 selectedCertRef .Group != "core" {
143186 path := field .NewPath ("validation.caCertificateRefs[0].group" )
144187 valErr := field .NotSupported (path , selectedCertRef .Group , []string {"" , "core" })
145- return valErr
188+ return & invalidKindError { valErr }
146189 }
147190 nsName := types.NamespacedName {
148191 Namespace : btp .Namespace ,
@@ -153,15 +196,15 @@ func validateBackendTLSCACertRef(
153196 case "ConfigMap" :
154197 if err := configMapResolver .resolve (nsName ); err != nil {
155198 path := field .NewPath ("validation.caCertificateRefs[0]" )
156- return field .Invalid (path , selectedCertRef , err .Error ())
199+ return & invalidCACertificateRefError { field .Invalid (path , selectedCertRef , err .Error ())}
157200 }
158201 case "Secret" :
159202 if err := secretResolver .resolve (nsName ); err != nil {
160203 path := field .NewPath ("validation.caCertificateRefs[0]" )
161- return field .Invalid (path , selectedCertRef , err .Error ())
204+ return & invalidCACertificateRefError { field .Invalid (path , selectedCertRef , err .Error ())}
162205 }
163206 default :
164- return fmt .Errorf ("invalid certificate reference kind %q" , selectedCertRef .Kind )
207+ return & invalidKindError { fmt .Errorf ("invalid certificate reference kind %q" , selectedCertRef .Kind )}
165208 }
166209 return nil
167210}
0 commit comments