Skip to content

Commit 097d08a

Browse files
committed
Tightening validation to require options or certRefs when "Terminate"
mode is set
1 parent 25b2e74 commit 097d08a

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

apis/v1/gateway_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ const (
374374
)
375375

376376
// GatewayTLSConfig describes a TLS configuration.
377+
//
378+
// +kubebuilder:validation:XValidation:message="certificateRefs or options must be specified when mode is Terminate",rule="self.mode == 'Terminate' ? size(self.certificateRefs) > 0 || size(self.options) > 0 : true"
377379
type GatewayTLSConfig struct {
378380
// Mode defines the TLS behavior for the TLS session initiated by the client.
379381
// There are two possible modes:

config/crd/experimental/gateway.networking.k8s.io_gateways.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/standard/gateway.networking.k8s.io_gateways.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/test/cel/gateway_test.go

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func TestValidateGateway(t *testing.T) {
205205
wantErrors: []string{"hostname must not be specified for protocols ['TCP', 'UDP']"},
206206
},
207207
{
208-
desc: "certificateRefs not set with https protocol and TLS terminate mode",
208+
desc: "certificateRefs not set with HTTPS protocol and TLS terminate mode",
209209
mutate: func(gw *gatewayv1.Gateway) {
210210
tlsMode := gatewayv1.TLSModeType("Terminate")
211211
gw.Spec.Listeners = []gatewayv1.Listener{
@@ -219,9 +219,10 @@ func TestValidateGateway(t *testing.T) {
219219
},
220220
}
221221
},
222+
wantErrors: []string{"certificateRefs or options must be specified when mode is Terminate"},
222223
},
223224
{
224-
desc: "certificateRefs not set with tls protocol and TLS terminate mode",
225+
desc: "certificateRefs not set with TLS protocol and TLS terminate mode",
225226
mutate: func(gw *gatewayv1.Gateway) {
226227
tlsMode := gatewayv1.TLSModeType("Terminate")
227228
gw.Spec.Listeners = []gatewayv1.Listener{
@@ -235,9 +236,29 @@ func TestValidateGateway(t *testing.T) {
235236
},
236237
}
237238
},
239+
wantErrors: []string{"certificateRefs or options must be specified when mode is Terminate"},
238240
},
239241
{
240-
desc: "certificateRefs set with tls protocol and TLS terminate mode",
242+
desc: "certificateRefs set with HTTPS protocol and TLS terminate mode",
243+
mutate: func(gw *gatewayv1.Gateway) {
244+
tlsMode := gatewayv1.TLSModeType("Terminate")
245+
gw.Spec.Listeners = []gatewayv1.Listener{
246+
{
247+
Name: gatewayv1.SectionName("https"),
248+
Protocol: gatewayv1.HTTPSProtocolType,
249+
Port: gatewayv1.PortNumber(8443),
250+
TLS: &gatewayv1.GatewayTLSConfig{
251+
Mode: &tlsMode,
252+
CertificateRefs: []gatewayv1.SecretObjectReference{
253+
{Name: gatewayv1.ObjectName("foo")},
254+
},
255+
},
256+
},
257+
}
258+
},
259+
},
260+
{
261+
desc: "certificateRefs set with TLS protocol and TLS terminate mode",
241262
mutate: func(gw *gatewayv1.Gateway) {
242263
tlsMode := gatewayv1.TLSModeType("Terminate")
243264
gw.Spec.Listeners = []gatewayv1.Listener{
@@ -255,6 +276,44 @@ func TestValidateGateway(t *testing.T) {
255276
}
256277
},
257278
},
279+
{
280+
desc: "options set with HTTPS protocol and TLS terminate mode",
281+
mutate: func(gw *gatewayv1.Gateway) {
282+
tlsMode := gatewayv1.TLSModeType("Terminate")
283+
gw.Spec.Listeners = []gatewayv1.Listener{
284+
{
285+
Name: gatewayv1.SectionName("https"),
286+
Protocol: gatewayv1.HTTPSProtocolType,
287+
Port: gatewayv1.PortNumber(8443),
288+
TLS: &gatewayv1.GatewayTLSConfig{
289+
Mode: &tlsMode,
290+
Options: map[gatewayv1.AnnotationKey]gatewayv1.AnnotationValue{
291+
"networking.example.com/tls-version": "1.2",
292+
},
293+
},
294+
},
295+
}
296+
},
297+
},
298+
{
299+
desc: "options set with tls protocol and TLS terminate mode",
300+
mutate: func(gw *gatewayv1.Gateway) {
301+
tlsMode := gatewayv1.TLSModeType("Terminate")
302+
gw.Spec.Listeners = []gatewayv1.Listener{
303+
{
304+
Name: gatewayv1.SectionName("tls"),
305+
Protocol: gatewayv1.TLSProtocolType,
306+
Port: gatewayv1.PortNumber(8443),
307+
TLS: &gatewayv1.GatewayTLSConfig{
308+
Mode: &tlsMode,
309+
Options: map[gatewayv1.AnnotationKey]gatewayv1.AnnotationValue{
310+
"networking.example.com/tls-version": "1.2",
311+
},
312+
},
313+
},
314+
}
315+
},
316+
},
258317
{
259318
desc: "names are not unique within the Gateway",
260319
mutate: func(gw *gatewayv1.Gateway) {

site-src/guides/implementers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ certificate stored by the external `vendor.example.com` TLS Certificate
211211
provider.
212212

213213
#### 2. Automatically generated TLS certs that are populated later
214-
Many users would prefer that TLS certs were automatically generated on their
214+
Many users would prefer that TLS certs will be automatically generated on their
215215
behalf. One potential implementation of that would involve a controller that
216216
watches Gateways and HTTPRoutes, generates TLS certs, and attaches them to the
217217
Gateway. Depending on the implementation details, Gateway owners may need to

0 commit comments

Comments
 (0)