From 4af6a406f9e2c366599c9b42024947f3cd062877 Mon Sep 17 00:00:00 2001 From: Billy Schulze Date: Tue, 26 Nov 2024 15:46:48 +1100 Subject: [PATCH 1/4] Update HTTPRequestRedirectPolicy StatusCode Use RedirectResponseCode instead of just 301/302 for HTTPRequestRedirectPolicy StatusCode Fixes #6788 Signed-off-by: Billy Schulze --- apis/projectcontour/v1/httpproxy.go | 3 +-- changelogs/unreleased/6789-billyjs-small.md | 1 + examples/contour/01-crds.yaml | 4 ++++ examples/render/contour-deployment.yaml | 4 ++++ examples/render/contour-gateway-provisioner.yaml | 4 ++++ examples/render/contour-gateway.yaml | 4 ++++ examples/render/contour.yaml | 4 ++++ site/content/docs/main/config/api-reference.html | 7 +++++-- 8 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 changelogs/unreleased/6789-billyjs-small.md diff --git a/apis/projectcontour/v1/httpproxy.go b/apis/projectcontour/v1/httpproxy.go index 72ff28096d2..c173e0dd11f 100644 --- a/apis/projectcontour/v1/httpproxy.go +++ b/apis/projectcontour/v1/httpproxy.go @@ -714,8 +714,7 @@ type HTTPRequestRedirectPolicy struct { // StatusCode is the HTTP status code to be used in response. // +optional // +kubebuilder:default=302 - // +kubebuilder:validation:Enum=301;302 - StatusCode *int `json:"statusCode,omitempty"` + StatusCode *RedirectResponseCode `json:"statusCode,omitempty"` // Path allows for redirection to a different path from the // original on the request. The path must start with a diff --git a/changelogs/unreleased/6789-billyjs-small.md b/changelogs/unreleased/6789-billyjs-small.md new file mode 100644 index 00000000000..1beceb9ebba --- /dev/null +++ b/changelogs/unreleased/6789-billyjs-small.md @@ -0,0 +1 @@ +Use RedirectResponseCode instead of just 301/302 for HTTPRequestRedirectPolicy StatusCode diff --git a/examples/contour/01-crds.yaml b/examples/contour/01-crds.yaml index 2319e19c962..00103d3de92 100644 --- a/examples/contour/01-crds.yaml +++ b/examples/contour/01-crds.yaml @@ -6632,6 +6632,10 @@ spec: enum: - 301 - 302 + - 303 + - 307 + - 308 + format: int32 type: integer type: object responseHeadersPolicy: diff --git a/examples/render/contour-deployment.yaml b/examples/render/contour-deployment.yaml index a2db3e69e2a..5d3e2cc1acd 100644 --- a/examples/render/contour-deployment.yaml +++ b/examples/render/contour-deployment.yaml @@ -6852,6 +6852,10 @@ spec: enum: - 301 - 302 + - 303 + - 307 + - 308 + format: int32 type: integer type: object responseHeadersPolicy: diff --git a/examples/render/contour-gateway-provisioner.yaml b/examples/render/contour-gateway-provisioner.yaml index ec9255a644d..27218a1bbf1 100644 --- a/examples/render/contour-gateway-provisioner.yaml +++ b/examples/render/contour-gateway-provisioner.yaml @@ -6643,6 +6643,10 @@ spec: enum: - 301 - 302 + - 303 + - 307 + - 308 + format: int32 type: integer type: object responseHeadersPolicy: diff --git a/examples/render/contour-gateway.yaml b/examples/render/contour-gateway.yaml index a121a0e0872..f051eebfa66 100644 --- a/examples/render/contour-gateway.yaml +++ b/examples/render/contour-gateway.yaml @@ -6668,6 +6668,10 @@ spec: enum: - 301 - 302 + - 303 + - 307 + - 308 + format: int32 type: integer type: object responseHeadersPolicy: diff --git a/examples/render/contour.yaml b/examples/render/contour.yaml index 190b4c5999a..4c2c5400901 100644 --- a/examples/render/contour.yaml +++ b/examples/render/contour.yaml @@ -6852,6 +6852,10 @@ spec: enum: - 301 - 302 + - 303 + - 307 + - 308 + format: int32 type: integer type: object responseHeadersPolicy: diff --git a/site/content/docs/main/config/api-reference.html b/site/content/docs/main/config/api-reference.html index 57bc87795fd..ea317ba3b1a 100644 --- a/site/content/docs/main/config/api-reference.html +++ b/site/content/docs/main/config/api-reference.html @@ -1767,7 +1767,9 @@

HTTPRequestRedirectPolic statusCode
-int + +RedirectResponseCode + @@ -3085,7 +3087,8 @@

RedirectResponseCode (uint32 alias)

(Appears on: -HTTPInternalRedirectPolicy) +HTTPInternalRedirectPolicy, +HTTPRequestRedirectPolicy)

RedirectResponseCode is a uint32 type alias with validation to ensure that the value is valid.

From f4e4e33ed52151b61ca94c1c19083410df906cd8 Mon Sep 17 00:00:00 2001 From: Billy Schulze Date: Tue, 26 Nov 2024 18:20:42 +1100 Subject: [PATCH 2/4] Update HTTPRequestRedirectPolicy tests to use RedirectResponseCode Signed-off-by: Billy Schulze --- apis/projectcontour/v1/zz_generated.deepcopy.go | 2 +- internal/dag/builder_test.go | 8 ++++---- internal/dag/httpproxy_processor.go | 2 +- internal/featuretests/v3/redirectroutepolicy_test.go | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/apis/projectcontour/v1/zz_generated.deepcopy.go b/apis/projectcontour/v1/zz_generated.deepcopy.go index 3207fef641a..b3719ec6c39 100644 --- a/apis/projectcontour/v1/zz_generated.deepcopy.go +++ b/apis/projectcontour/v1/zz_generated.deepcopy.go @@ -516,7 +516,7 @@ func (in *HTTPRequestRedirectPolicy) DeepCopyInto(out *HTTPRequestRedirectPolicy } if in.StatusCode != nil { in, out := &in.StatusCode, &out.StatusCode - *out = new(int) + *out = new(RedirectResponseCode) **out = **in } if in.Path != nil { diff --git a/internal/dag/builder_test.go b/internal/dag/builder_test.go index 42048278367..6d0fa1011ee 100644 --- a/internal/dag/builder_test.go +++ b/internal/dag/builder_test.go @@ -12395,7 +12395,7 @@ func TestDAGInsert(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), }, }}, }, @@ -12438,7 +12438,7 @@ func TestDAGInsert(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), }, }}, }, @@ -12490,7 +12490,7 @@ func TestDAGInsert(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), }, }}, }, @@ -12643,7 +12643,7 @@ func TestDAGInsert(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), }, }, }, diff --git a/internal/dag/httpproxy_processor.go b/internal/dag/httpproxy_processor.go index f1d0741ab49..8a61bc5f132 100644 --- a/internal/dag/httpproxy_processor.go +++ b/internal/dag/httpproxy_processor.go @@ -1951,7 +1951,7 @@ func redirectRoutePolicy(redirect *contour_v1.HTTPRequestRedirectPolicy) (*Redir var statusCode int if redirect.StatusCode != nil { - statusCode = *redirect.StatusCode + statusCode = int(*redirect.StatusCode) } if redirect.Path != nil && redirect.Prefix != nil { diff --git a/internal/featuretests/v3/redirectroutepolicy_test.go b/internal/featuretests/v3/redirectroutepolicy_test.go index 5a7bc7645e1..4a2c3e0f889 100644 --- a/internal/featuretests/v3/redirectroutepolicy_test.go +++ b/internal/featuretests/v3/redirectroutepolicy_test.go @@ -43,7 +43,7 @@ func TestRedirectResponsePolicy_HTTProxy(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), Path: ptr.To("/blog"), }, }}, @@ -87,7 +87,7 @@ func TestRedirectResponsePolicy_HTTProxy(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), Prefix: ptr.To("/blogprefix"), }, }}, @@ -135,7 +135,7 @@ func TestRedirectResponsePolicy_HTTProxy(t *testing.T) { Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(443)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), Prefix: ptr.To("/blogprefix"), Path: ptr.To("/blogprefix"), }, From faf2829c0e30bc9c3f633e83d9558f12031af885 Mon Sep 17 00:00:00 2001 From: Billy Schulze Date: Wed, 27 Nov 2024 03:57:02 +1100 Subject: [PATCH 3/4] Fix type in tests Signed-off-by: Billy Schulze --- test/e2e/httpproxy/internal_redirect_test.go | 2 +- test/e2e/httpproxy/request_redirect_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/httpproxy/internal_redirect_test.go b/test/e2e/httpproxy/internal_redirect_test.go index 3875bd9199d..a020dab9068 100644 --- a/test/e2e/httpproxy/internal_redirect_test.go +++ b/test/e2e/httpproxy/internal_redirect_test.go @@ -190,7 +190,7 @@ func getInternalRedirectHTTPProxy(namespace string) *contour_v1.HTTPProxy { Services: []contour_v1.Service{}, RequestRedirectPolicy: &contour_v1.HTTPRequestRedirectPolicy{ Hostname: ptr.To(fqdn), - StatusCode: ptr.To(302), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(302)), Path: ptr.To("/echo"), }, }, diff --git a/test/e2e/httpproxy/request_redirect_test.go b/test/e2e/httpproxy/request_redirect_test.go index e1f5a50beca..e380d26ce27 100644 --- a/test/e2e/httpproxy/request_redirect_test.go +++ b/test/e2e/httpproxy/request_redirect_test.go @@ -124,7 +124,7 @@ func getRedirectHTTPProxy(namespace string, removeServices bool) *contour_v1.HTT Scheme: ptr.To("https"), Hostname: ptr.To("envoyproxy.io"), Port: ptr.To(int32(8080)), - StatusCode: ptr.To(301), + StatusCode: ptr.To(contour_v1.RedirectResponseCode(301)), }, }, { Conditions: []contour_v1.MatchCondition{{ From 276ae32b89f89225c72bcd62c29960aaf9cdcd6f Mon Sep 17 00:00:00 2001 From: Billy Schulze Date: Mon, 16 Dec 2024 13:33:03 +1100 Subject: [PATCH 4/4] Add 303, 307, 308 as valid envoy redirect route status codes Signed-off-by: Billy Schulze --- internal/dag/dag.go | 4 ++-- internal/envoy/v3/route.go | 6 ++++++ internal/envoy/v3/route_test.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/dag/dag.go b/internal/dag/dag.go index d5832c7e051..0e75755abfd 100644 --- a/internal/dag/dag.go +++ b/internal/dag/dag.go @@ -215,7 +215,7 @@ type DirectResponse struct { Body string } -// Redirect allows for a 301/302 redirect to be the response +// Redirect allows for a redirect to be the response // to a route request vs. routing to an envoy cluster. type Redirect struct { // Hostname is the host name to redirect to. @@ -230,7 +230,7 @@ type Redirect struct { PortNumber uint32 // StatusCode is the HTTP response code to - // use. Valid options are 301 or 302. + // use. Valid options are 301, 302, 303, 307, or 308. StatusCode int // PathRewritePolicy is the policy for rewriting diff --git a/internal/envoy/v3/route.go b/internal/envoy/v3/route.go index 4cd4ea75425..5204687b63f 100644 --- a/internal/envoy/v3/route.go +++ b/internal/envoy/v3/route.go @@ -392,6 +392,12 @@ func routeRedirect(redirect *dag.Redirect) *envoy_config_route_v3.Route_Redirect r.Redirect.ResponseCode = envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY case http.StatusFound: r.Redirect.ResponseCode = envoy_config_route_v3.RedirectAction_FOUND + case http.StatusSeeOther: + r.Redirect.ResponseCode = envoy_config_route_v3.RedirectAction_SEE_OTHER + case http.StatusTemporaryRedirect: + r.Redirect.ResponseCode = envoy_config_route_v3.RedirectAction_TEMPORARY_REDIRECT + case http.StatusPermanentRedirect: + r.Redirect.ResponseCode = envoy_config_route_v3.RedirectAction_PERMANENT_REDIRECT } return r diff --git a/internal/envoy/v3/route_test.go b/internal/envoy/v3/route_test.go index 152776134ae..456fb70b08d 100644 --- a/internal/envoy/v3/route_test.go +++ b/internal/envoy/v3/route_test.go @@ -2762,7 +2762,7 @@ func TestRouteRedirect(t *testing.T) { }, "unsupported status code specified": { redirect: &dag.Redirect{ - StatusCode: 303, + StatusCode: 306, }, want: &envoy_config_route_v3.Route_Redirect{ Redirect: &envoy_config_route_v3.RedirectAction{},