Skip to content

Commit 1ecdd66

Browse files
authored
Add validation for https tls mode (#2652)
* add validation for https tls mode Signed-off-by: huabing zhao <[email protected]> * fix Signed-off-by: huabing zhao <[email protected]> * add valid and invalid examples Signed-off-by: huabing zhao <[email protected]> * fix test Signed-off-by: huabing zhao <[email protected]> * verify tls exists Signed-off-by: huabing zhao <[email protected]> * verify tls exists Signed-off-by: huabing zhao <[email protected]> --------- Signed-off-by: huabing zhao <[email protected]>
1 parent 7b9876e commit 1ecdd66

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

apis/v1/gateway_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ type GatewaySpec struct {
188188
// +kubebuilder:validation:MaxItems=64
189189
// +kubebuilder:validation:XValidation:message="tls must be specified for protocols ['HTTPS', 'TLS']",rule="self.all(l, l.protocol in ['HTTPS', 'TLS'] ? has(l.tls) : true)"
190190
// +kubebuilder:validation:XValidation:message="tls must not be specified for protocols ['HTTP', 'TCP', 'UDP']",rule="self.all(l, l.protocol in ['HTTP', 'TCP', 'UDP'] ? !has(l.tls) : true)"
191+
// +kubebuilder:validation:XValidation:message="tls mode must be Terminate for protocol HTTPS",rule="self.all(l, (l.protocol == 'HTTPS' && has(l.tls)) ? (l.tls.mode == '' || l.tls.mode == 'Terminate') : true)"
191192
// +kubebuilder:validation:XValidation:message="hostname must not be specified for protocols ['TCP', 'UDP']",rule="self.all(l, l.protocol in ['TCP', 'UDP'] ? (!has(l.hostname) || l.hostname == '') : true)"
192193
// +kubebuilder:validation:XValidation:message="Listener name must be unique within the Gateway",rule="self.all(l1, self.exists_one(l2, l1.name == l2.name))"
193194
// +kubebuilder:validation:XValidation:message="Combination of port, protocol and hostname must be unique for each listener",rule="self.all(l1, self.exists_one(l2, l1.port == l2.port && l1.protocol == l2.protocol && (has(l1.hostname) && has(l2.hostname) ? l1.hostname == l2.hostname : !has(l1.hostname) && !has(l2.hostname))))"

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

Lines changed: 6 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: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/standard/simple-http-https/gateway.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ spec:
1818
certificateRefs:
1919
- kind: Secret
2020
name: example-com
21+
- name: https-default-tls-mode
22+
port: 8443
23+
protocol: HTTPS
24+
hostname: "*.foo.com"
25+
tls:
26+
certificateRefs:
27+
- kind: Secret
28+
name: foo-com
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: Gateway
3+
metadata:
4+
name: duplicate-listeners
5+
spec:
6+
gatewayClassName: acme-lb
7+
listeners:
8+
- name: foo
9+
protocol: HTTPS
10+
port: 443
11+
tls:
12+
mode: Passthrough

pkg/test/cel/gateway_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,39 @@ func TestValidateGateway(t *testing.T) {
6767
},
6868
wantErrors: []string{"tls must not be specified for protocols ['HTTP', 'TCP', 'UDP']"},
6969
},
70+
{
71+
desc: "https protocol with Passthrough tls mode",
72+
mutate: func(gw *gatewayv1.Gateway) {
73+
gw.Spec.Listeners = []gatewayv1.Listener{
74+
{
75+
Name: gatewayv1.SectionName("https"),
76+
Protocol: gatewayv1.HTTPSProtocolType,
77+
Port: gatewayv1.PortNumber(8080),
78+
TLS: &gatewayv1.GatewayTLSConfig{
79+
Mode: ptrTo(gatewayv1.TLSModeType("Passthrough")),
80+
},
81+
},
82+
}
83+
},
84+
wantErrors: []string{"tls mode must be Terminate for protocol HTTPS"},
85+
},
86+
{
87+
desc: "tls mode not set with https protocol and tls config present",
88+
mutate: func(gw *gatewayv1.Gateway) {
89+
gw.Spec.Listeners = []gatewayv1.Listener{
90+
{
91+
Name: gatewayv1.SectionName("https"),
92+
Protocol: gatewayv1.HTTPSProtocolType,
93+
Port: gatewayv1.PortNumber(8080),
94+
TLS: &gatewayv1.GatewayTLSConfig{
95+
CertificateRefs: []gatewayv1.SecretObjectReference{
96+
{Name: gatewayv1.ObjectName("foo")},
97+
},
98+
},
99+
},
100+
}
101+
},
102+
},
70103
{
71104
desc: "tls config present with tcp protocol",
72105
mutate: func(gw *gatewayv1.Gateway) {

0 commit comments

Comments
 (0)