Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions conformance/base/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,39 @@ spec:
matchLabels:
gateway-conformance: backend
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: gateway-backendtlspolicy
namespace: gateway-conformance-infra
spec:
gatewayClassName: "{GATEWAY_CLASS_NAME}"
listeners:
- name: http
port: 80
protocol: HTTP
hostname: "abc.example.com"
allowedRoutes:
namespaces:
from: Same
kinds:
- kind: HTTPRoute
- name: https
port: 443
protocol: HTTPS
tls:
mode: Terminate
certificateRefs:
- group: ""
kind: Secret
name: tls-checks-certificate
hostname: "abc.example.com"
allowedRoutes:
namespaces:
from: Same
kinds:
- kind: HTTPRoute
---
apiVersion: v1
kind: Service
metadata:
Expand Down
163 changes: 163 additions & 0 deletions conformance/tests/backendtlspolicy-san.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tests

import (
"testing"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
h "sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/pkg/features"
)

func init() {
ConformanceTests = append(ConformanceTests, BackendTLSPolicySANValidation)
}

var BackendTLSPolicySANValidation = suite.ConformanceTest{
ShortName: "BackendTLSPolicySANValidation",
Description: "BackendTLSPolicySANValidation extend BackendTLSPolicy with SubjectAltNames validation",
Features: []features.FeatureName{
features.SupportGateway,
features.SupportHTTPRoute,
features.SupportBackendTLSPolicy,
features.SupportBackendTLSPolicySANValidation,
},
Manifests: []string{"tests/backendtlspolicy-san.yaml"},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "route-backendtlspolicy-san-test", Namespace: ns}
gwNN := types.NamespacedName{Name: "gateway-backendtlspolicy", Namespace: ns}

kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, []string{ns})
gwAddr := kubernetes.GatewayAndRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), &gatewayv1.HTTPRoute{}, false, routeNN)
kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)

policyCond := metav1.Condition{
Type: string(v1alpha2.PolicyConditionAccepted),
Status: metav1.ConditionTrue,
Reason: string(v1alpha2.PolicyReasonAccepted),
}

serverStr := "abc.example.com"

// Verify that the request sent to Service with valid BackendTLSPolicy containing dns SAN should succeed.
t.Run("HTTP request sent to Service with valid BackendTLSPolicy containing dns SAN should succeed", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-san-dns", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSSanDns",
SNI: serverStr,
},
Response: h.Response{StatusCode: 200},
})
})

// Verify that the request sent to a Service targeted by a BackendTLSPolicy with mismatched dns SAN should fail.
t.Run("HTTP request sent to Service targeted by BackendTLSPolicy with mismatched dns SAN should return an HTTP error", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-san-dns-mismatch", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectFailure(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSSanDnsMismatch",
SNI: serverStr,
},
})
})

// Verify that the request sent to Service with valid BackendTLSPolicy containing uri SAN should succeed.
t.Run("HTTP request sent to Service with valid BackendTLSPolicy containing uri SAN should succeed", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-san-uri", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSSanUri",
SNI: serverStr,
},
Response: h.Response{StatusCode: 200},
})
})

// Verify that the request sent to a Service targeted by a BackendTLSPolicy with mismatched uri SAN should fail.
t.Run("HTTP request sent to Service targeted by BackendTLSPolicy with mismatched uri SAN should return an HTTP error", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-san-uri-mismatch", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectFailure(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSSanUriMismatch",
SNI: serverStr,
},
})
})

// Verify that the request sent to Service with valid BackendTLSPolicy containing multi SANs should succeed.
t.Run("HTTP request sent to Service with valid BackendTLSPolicy containing multi SAN should succeed", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-multiple-sans", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSMultiSans",
SNI: serverStr,
},
Response: h.Response{StatusCode: 200},
})
})

// Verify that the request sent to a Service targeted by a BackendTLSPolicy with mismatched multi SAN should fail.
t.Run("HTTP request sent to Service targeted by BackendTLSPolicy with mismatched multi SAN should return an HTTP error", func(t *testing.T) {
policyNN := types.NamespacedName{Name: "backendtlspolicy-multiple-mismatch-sans", Namespace: ns}
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, policyCond)

h.MakeRequestAndExpectFailure(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr,
h.ExpectedResponse{
Namespace: ns,
Request: h.Request{
Host: serverStr,
Path: "/backendTLSMultiMismatchSans",
SNI: serverStr,
},
})
})
},
}
Loading