Skip to content

Commit

Permalink
[release/v1.2] fix panic in provider when the direct response body is…
Browse files Browse the repository at this point in the history
… nil (#4647) (#4654)

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

body is an optional field, it may be nil


(cherry picked from commit ad51832)

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
arkodg authored Nov 7, 2024
1 parent dd1c66f commit 18d10e8
Showing 4 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions internal/provider/kubernetes/filters.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ func (r *gatewayAPIReconciler) processRouteFilterConfigMapRef(
resourceMap *resourceMappings, resourceTree *resource.Resources,
) {
if filter.Spec.DirectResponse != nil &&
filter.Spec.DirectResponse.Body != nil &&
filter.Spec.DirectResponse.Body.ValueRef != nil &&
string(filter.Spec.DirectResponse.Body.ValueRef.Kind) == resource.KindConfigMap {
configMap := new(corev1.ConfigMap)
20 changes: 20 additions & 0 deletions test/e2e/testdata/direct-response.yaml
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
@@ -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"),
@@ -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
})

0 comments on commit 18d10e8

Please sign in to comment.