@@ -81,17 +81,22 @@ func validateBackendTLSPolicy(
81
81
82
82
caCertRefs := backendTLSPolicy .Spec .Validation .CACertificateRefs
83
83
wellKnownCerts := backendTLSPolicy .Spec .Validation .WellKnownCACertificates
84
+
85
+ // Check mutual exclusivity
84
86
switch {
85
87
case len (caCertRefs ) > 0 && wellKnownCerts != nil :
86
88
valid = false
87
89
msg := "CACertificateRefs and WellKnownCACertificates are mutually exclusive"
88
90
conds = append (conds , conditions .NewPolicyInvalid (msg ))
89
91
90
92
case len (caCertRefs ) > 0 :
91
- if err := validateBackendTLSCACertRef (backendTLSPolicy , configMapResolver , secretResolver ); err != nil {
93
+ certConds := validateBackendTLSCACertRef (backendTLSPolicy , configMapResolver , secretResolver )
94
+ if len (certConds ) > 0 {
92
95
valid = false
93
- conds = append (conds , conditions .NewPolicyInvalid (
94
- fmt .Sprintf ("invalid CACertificateRef: %s" , err .Error ())))
96
+ conds = append (conds , certConds ... )
97
+ } else if valid {
98
+ // Only set ResolvedRefs to true if CACertificateRefs are valid AND overall policy is valid
99
+ conds = append (conds , conditions .NewBackendTLSPolicyResolvedRefs ())
95
100
}
96
101
97
102
case wellKnownCerts != nil :
@@ -103,8 +108,12 @@ func validateBackendTLSPolicy(
103
108
104
109
default :
105
110
valid = false
106
- conds = append (conds , conditions .NewPolicyInvalid ("CACertRefs and WellKnownCACerts are both nil" ))
111
+ conds = append (
112
+ conds ,
113
+ conditions .NewPolicyInvalid ("either CACertificateRefs or WellKnownCACertificates must be specified" ),
114
+ )
107
115
}
116
+
108
117
return valid , ignored , conds
109
118
}
110
119
@@ -123,11 +132,11 @@ func validateBackendTLSCACertRef(
123
132
btp * v1alpha3.BackendTLSPolicy ,
124
133
configMapResolver * configMapResolver ,
125
134
secretResolver * secretResolver ,
126
- ) error {
135
+ ) []conditions. Condition {
127
136
if len (btp .Spec .Validation .CACertificateRefs ) != 1 {
128
137
path := field .NewPath ("validation.caCertificateRefs" )
129
138
valErr := field .TooMany (path , len (btp .Spec .Validation .CACertificateRefs ), 1 )
130
- return valErr
139
+ return []conditions. Condition { conditions . NewPolicyInvalid ( valErr . Error ())}
131
140
}
132
141
133
142
selectedCertRef := btp .Spec .Validation .CACertificateRefs [0 ]
@@ -136,13 +145,19 @@ func validateBackendTLSCACertRef(
136
145
if ! slices .Contains (allowedCaCertKinds , selectedCertRef .Kind ) {
137
146
path := field .NewPath ("validation.caCertificateRefs[0].kind" )
138
147
valErr := field .NotSupported (path , btp .Spec .Validation .CACertificateRefs [0 ].Kind , allowedCaCertKinds )
139
- return valErr
148
+ return []conditions.Condition {
149
+ conditions .NewBackendTLSPolicyInvalidKind (valErr .Error ()),
150
+ conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ),
151
+ }
140
152
}
141
153
if selectedCertRef .Group != "" &&
142
154
selectedCertRef .Group != "core" {
143
155
path := field .NewPath ("validation.caCertificateRefs[0].group" )
144
156
valErr := field .NotSupported (path , selectedCertRef .Group , []string {"" , "core" })
145
- return valErr
157
+ return []conditions.Condition {
158
+ conditions .NewBackendTLSPolicyInvalidKind (valErr .Error ()),
159
+ conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ),
160
+ }
146
161
}
147
162
nsName := types.NamespacedName {
148
163
Namespace : btp .Namespace ,
@@ -153,15 +168,21 @@ func validateBackendTLSCACertRef(
153
168
case "ConfigMap" :
154
169
if err := configMapResolver .resolve (nsName ); err != nil {
155
170
path := field .NewPath ("validation.caCertificateRefs[0]" )
156
- return field .Invalid (path , selectedCertRef , err .Error ())
171
+ valErr := field .Invalid (path , selectedCertRef , err .Error ())
172
+ return []conditions.Condition {
173
+ conditions .NewBackendTLSPolicyInvalidCACertificateRef (valErr .Error ()),
174
+ conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ),
175
+ }
157
176
}
158
177
case "Secret" :
159
178
if err := secretResolver .resolve (nsName ); err != nil {
160
179
path := field .NewPath ("validation.caCertificateRefs[0]" )
161
- return field .Invalid (path , selectedCertRef , err .Error ())
180
+ valErr := field .Invalid (path , selectedCertRef , err .Error ())
181
+ return []conditions.Condition {
182
+ conditions .NewBackendTLSPolicyInvalidCACertificateRef (valErr .Error ()),
183
+ conditions .NewBackendTLSPolicyNoValidCACertificate ("No valid CACertificateRef found" ),
184
+ }
162
185
}
163
- default :
164
- return fmt .Errorf ("invalid certificate reference kind %q" , selectedCertRef .Kind )
165
186
}
166
187
return nil
167
188
}
0 commit comments