Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic in provider when the direct response body is nil #4647

Merged
merged 1 commit into from
Nov 7, 2024
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
1 change: 1 addition & 0 deletions internal/provider/kubernetes/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
resourceMap *resourceMappings, resourceTree *resource.Resources,
) {
if filter.Spec.DirectResponse != nil &&
filter.Spec.DirectResponse.Body != nil &&

Check warning on line 69 in internal/provider/kubernetes/filters.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/filters.go#L69

Added line #L69 was not covered by tests
filter.Spec.DirectResponse.Body.ValueRef != nil &&
string(filter.Spec.DirectResponse.Body.ValueRef.Kind) == resource.KindConfigMap {
configMap := new(corev1.ConfigMap)
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/testdata/direct-response.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ spec:
group: gateway.envoyproxy.io
kind: HTTPRouteFilter
name: direct-response-value-ref
- matches:
- path:
type: PathPrefix
value: /401
filters:
- type: ExtensionRef
extensionRef:
group: gateway.envoyproxy.io
kind: HTTPRouteFilter
name: direct-response-status

---
apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -62,3 +73,12 @@ spec:
group: ""
kind: ConfigMap
name: value-ref-response
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: HTTPRouteFilter
metadata:
name: direct-response-status
namespace: gateway-conformance-infra
spec:
directResponse:
statusCode: 401
5 changes: 3 additions & 2 deletions test/e2e/tests/direct-response.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ var DirectResponseTest = suite.ConformanceTest{
gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)

kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/inline", "text/plain", "Oops! Your request is not found.")
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/value-ref", "application/json", `{"error": "Internal Server Error"}`)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/inline", "text/plain", "Oops! Your request is not found.", 200)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/value-ref", "application/json", `{"error": "Internal Server Error"}`, 200)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/401", "", ``, 401)
})
},
}
13 changes: 10 additions & 3 deletions test/e2e/tests/response-override.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ var ResponseOverrideTest = suite.ConformanceTest{
Name: gwapiv1.ObjectName(gwNN.Name),
}
BackendTrafficPolicyMustBeAccepted(t, suite.Client, types.NamespacedName{Name: "response-override", Namespace: ns}, suite.ControllerName, ancestorRef)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/status/404", "text/plain", "Oops! Your request is not found.")
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/status/500", "application/json", `{"error": "Internal Server Error"}`)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/status/404", "text/plain", "Oops! Your request is not found.", 404)
verifyCustomResponse(t, suite.TimeoutConfig, gwAddr, "/status/500", "application/json", `{"error": "Internal Server Error"}`, 500)
})
},
}

func verifyCustomResponse(t *testing.T, timeoutConfig config.TimeoutConfig, gwAddr, path, expectedContentType, expectedBody string) {
func verifyCustomResponse(t *testing.T, timeoutConfig config.TimeoutConfig, gwAddr,
path, expectedContentType, expectedBody string, expectedStatusCode int,
) {
reqURL := url.URL{
Scheme: "http",
Host: httputils.CalculateHost(t, gwAddr, "http"),
Expand Down Expand Up @@ -88,6 +90,11 @@ func verifyCustomResponse(t *testing.T, timeoutConfig config.TimeoutConfig, gwAd
return false
}

if expectedStatusCode != rsp.StatusCode {
tlog.Logf(t, "expected status code to be %d but got %d", expectedStatusCode, rsp.StatusCode)
return false
}

return true
})

Expand Down