Skip to content

Commit

Permalink
Tightening validation to require options or certRefs when "Terminate"
Browse files Browse the repository at this point in the history
mode is set
  • Loading branch information
robscott committed Feb 29, 2024
1 parent 25b2e74 commit ab303cb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apis/v1/gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ const (
)

// GatewayTLSConfig describes a TLS configuration.
//
// +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"
type GatewayTLSConfig struct {
// Mode defines the TLS behavior for the TLS session initiated by the client.
// There are two possible modes:
Expand Down
10 changes: 10 additions & 0 deletions config/crd/experimental/gateway.networking.k8s.io_gateways.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions config/crd/standard/gateway.networking.k8s.io_gateways.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 62 additions & 3 deletions pkg/test/cel/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestValidateGateway(t *testing.T) {
wantErrors: []string{"hostname must not be specified for protocols ['TCP', 'UDP']"},
},
{
desc: "certificateRefs not set with https protocol and TLS terminate mode",
desc: "certificateRefs not set with HTTPS protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
Expand All @@ -219,9 +219,10 @@ func TestValidateGateway(t *testing.T) {
},
}
},
wantErrors: []string{"certificateRefs or options must be specified when mode is Terminate"},
},
{
desc: "certificateRefs not set with tls protocol and TLS terminate mode",
desc: "certificateRefs not set with TLS protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
Expand All @@ -235,9 +236,29 @@ func TestValidateGateway(t *testing.T) {
},
}
},
wantErrors: []string{"certificateRefs or options must be specified when mode is Terminate"},
},
{
desc: "certificateRefs set with tls protocol and TLS terminate mode",
desc: "certificateRefs set with HTTPS protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
{
Name: gatewayv1.SectionName("https"),
Protocol: gatewayv1.HTTPSProtocolType,
Port: gatewayv1.PortNumber(8443),
TLS: &gatewayv1.GatewayTLSConfig{
Mode: &tlsMode,
CertificateRefs: []gatewayv1.SecretObjectReference{
{Name: gatewayv1.ObjectName("foo")},
},
},
},
}
},
},
{
desc: "certificateRefs set with TLS protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
Expand All @@ -255,6 +276,44 @@ func TestValidateGateway(t *testing.T) {
}
},
},
{
desc: "options set with HTTPS protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
{
Name: gatewayv1.SectionName("https"),
Protocol: gatewayv1.HTTPSProtocolType,
Port: gatewayv1.PortNumber(8443),
TLS: &gatewayv1.GatewayTLSConfig{
Mode: &tlsMode,
Options: map[gatewayv1.AnnotationKey]gatewayv1.AnnotationValue{
"networking.example.com/tls-version": "1.2",
},
},
},
}
},
},
{
desc: "options set with tls protocol and TLS terminate mode",
mutate: func(gw *gatewayv1.Gateway) {
tlsMode := gatewayv1.TLSModeType("Terminate")
gw.Spec.Listeners = []gatewayv1.Listener{
{
Name: gatewayv1.SectionName("tls"),
Protocol: gatewayv1.TLSProtocolType,
Port: gatewayv1.PortNumber(8443),
TLS: &gatewayv1.GatewayTLSConfig{
Mode: &tlsMode,
Options: map[gatewayv1.AnnotationKey]gatewayv1.AnnotationValue{
"networking.example.com/tls-version": "1.2",
},
},
},
}
},
},
{
desc: "names are not unique within the Gateway",
mutate: func(gw *gatewayv1.Gateway) {
Expand Down

0 comments on commit ab303cb

Please sign in to comment.