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
100 changes: 100 additions & 0 deletions conformance/tests/cors-allow-credentials-behavior.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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"

"k8s.io/apimachinery/pkg/types"

"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, CORSAllowCredentialsBehavior)
}

var CORSAllowCredentialsBehavior = suite.ConformanceTest{
ShortName: "CORSAllowCredentialsBehavior",
Description: "Validate ACA-Credentials responses",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a better description, what is it doing, what is expected, what kind of validation will happen here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this seem ok?
Validate ACA-Credentials responses, the gateway needs to return Access-Control-Allow-Credentials: true for credentialed CORS requests when enabled on the gateway, and omit it when disabled

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much better! thank you!

Manifests: []string{"tests/cors-allow-credentials-behavior.yaml"},
Features: []features.FeatureName{
features.SupportGateway,
features.SupportHTTPRoute,
features.SupportHTTPRouteCORS,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this reminds me: we don't have any other CORS test. Wouldn't be better to either add those tests as well (support Cors, then test authorization)? Maybe this test should cover the whole extension, eg.:

My point is that while the authorization test is desired, we lack some basic CORS conformance test that I think could be part of this change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(but also, if we feel comfortable with merging this and following up, just be sure to open an issue to track the lack of basic cors tests)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't mind either way — I just don't know if I'll have time in the next few days, so it depends on whether we want to get it merged now. @shaneutt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okie, let's wait for Shane to chime in! thanks!

},
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
ns := "gateway-conformance-infra"
routeNN := types.NamespacedName{Name: "cors-allow-credentials", Namespace: ns}
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)
kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)

origin := "https://app.example"

testCases := []http.ExpectedResponse{
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it's good to use the TestCaseName here when you can. We kind of (ab)use it in various places to provide a description explaining the tests intent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pascal or Camel case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually several of them are basically just descriptions with spaces and everything. We may even want to rename it Description (or I suppose add Description) but in the meantime, if you wanna just look how the bulk of them are done and follow that that's fine. We need to revisit these things (but that doesn't need to be done as part of this specific PR).

Request: http.Request{
Method: "GET",
Path: "/cors-behavior-creds-false",
Headers: map[string]string{
"Origin": origin,
"Cookie": "sid=abc123",
"Authorization": "Bearer test",
},
},
Response: http.Response{
StatusCode: 200,
AbsentHeaders: []string{"Access-Control-Allow-Credentials"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you wanna follow the same test as bellow here and check if the Access-Control-Allow-Origin reflects what you added?

},
Namespace: ns,
},
{
Request: http.Request{
Method: "GET",
Path: "/cors-behavior-creds-true",
Headers: map[string]string{
"Origin": origin,
"Cookie": "sid=abc123",
"Authorization": "Bearer test",
},
},
Response: http.Response{
StatusCode: 200,
Headers: map[string]string{
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": origin,
},
},
Namespace: ns,
},
}

for i := range testCases {
// Declare tc here to avoid loop variable
// reuse issues across parallel tests.
Comment on lines +91 to +92
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a linter checking for this, right? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so? are you talking about copyloopvar maybe?

tc := testCases[i]
t.Run(tc.GetTestCaseName(i), func(t *testing.T) {
t.Parallel()
http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, tc)
})
}
},
}
32 changes: 32 additions & 0 deletions conformance/tests/cors-allow-credentials-behavior.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: cors-allow-credentials
namespace: gateway-conformance-infra
spec:
parentRefs:
- name: same-namespace
rules:
- matches:
- path:
type: PathPrefix
value: /cors-behavior-creds-false
backendRefs:
- name: infra-backend-v1
port: 8080
filters:
- cors:
allowCredentials: false
type: CORS
- matches:
- path:
type: PathPrefix
value: /cors-behavior-creds-true
backendRefs:
- name: infra-backend-v1
port: 8080
filters:
- cors:
allowCredentials: true
type: CORS

9 changes: 9 additions & 0 deletions pkg/features/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ const (

// This option indicates support for the name field in the HTTPRouteRule (extended conformance)
SupportHTTPRouteNamedRouteRule FeatureName = "HTTPRouteNamedRouteRule"

// This option indicates support for the cors filter in the HTTPRouteFilter (extended conformance)
SupportHTTPRouteCORS FeatureName = "HTTPRouteCORS"
)

var (
Expand Down Expand Up @@ -198,6 +201,11 @@ var (
Name: SupportHTTPRouteNamedRouteRule,
Channel: FeatureChannelStandard,
}
// HTTPRouteCORS contains metadata for the SupportHTTPRouteCORS feature.
HTTPRouteCORS = Feature{
Name: SupportHTTPRouteCORS,
Channel: FeatureChannelExperimental,
}
)

// HTTPRouteExtendedFeatures includes all extended features for HTTPRoute
Expand All @@ -223,4 +231,5 @@ var HTTPRouteExtendedFeatures = sets.New(
HTTPRouteBackendProtocolH2CFeature,
HTTPRouteBackendProtocolWebSocketFeature,
HTTPRouteNamedRouteRule,
HTTPRouteCORS,
)