From 843100322c13beac0afd2307755f37d38197e17c Mon Sep 17 00:00:00 2001 From: shawnh2 Date: Wed, 19 Jun 2024 13:54:46 +0800 Subject: [PATCH 1/2] do not propagate well-known port number in xds translator for portRedirect Signed-off-by: shawnh2 --- internal/xds/translator/route.go | 4 ++- .../in/xds-ir/http-route-redirect.yaml | 29 +++++++++++++++++++ .../xds-ir/http-route-redirect.routes.yaml | 16 ++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/xds/translator/route.go b/internal/xds/translator/route.go index 1765d2252eb..2bd7302e69c 100644 --- a/internal/xds/translator/route.go +++ b/internal/xds/translator/route.go @@ -345,7 +345,9 @@ func buildXdsRedirectAction(httpRoute *ir.HTTPRoute) *routev3.RedirectAction { if redirection.Hostname != nil { routeAction.HostRedirect = *redirection.Hostname } - if redirection.Port != nil { + // Ignore the redirect port if it is a well-known port number, in order to + // prevent the port be added in the response's location header. + if redirection.Port != nil && *redirection.Port != 80 && *redirection.Port != 443 { routeAction.PortRedirect = *redirection.Port } if redirection.StatusCode != nil { diff --git a/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml b/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml index 36599609deb..848866c0503 100644 --- a/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml +++ b/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml @@ -50,3 +50,32 @@ http: redirect: path: prefixMatchReplace: / + - name: "redirect-route-4" + hostname: "*" + destination: + name: "redirect-route-dest" + settings: + - endpoints: + - host: "1.2.3.4" + port: 50000 + redirect: + scheme: https + statusCode: 302 + hostname: "redirected.com" + path: + prefixMatchReplace: /redirected + - name: "redirect-route-5" + hostname: "*" + destination: + name: "redirect-route-dest" + settings: + - endpoints: + - host: "1.2.3.4" + port: 50000 + redirect: + scheme: https + statusCode: 302 + port: 443 + hostname: "redirected.com" + path: + prefixMatchReplace: /redirected diff --git a/internal/xds/translator/testdata/out/xds-ir/http-route-redirect.routes.yaml b/internal/xds/translator/testdata/out/xds-ir/http-route-redirect.routes.yaml index d2c46435ee3..21d751bc5ac 100644 --- a/internal/xds/translator/testdata/out/xds-ir/http-route-redirect.routes.yaml +++ b/internal/xds/translator/testdata/out/xds-ir/http-route-redirect.routes.yaml @@ -30,3 +30,19 @@ pattern: regex: ^/redirect/\/* substitution: / + - match: + prefix: / + name: redirect-route-4 + redirect: + hostRedirect: redirected.com + prefixRewrite: /redirected + responseCode: FOUND + schemeRedirect: https + - match: + prefix: / + name: redirect-route-5 + redirect: + hostRedirect: redirected.com + prefixRewrite: /redirected + responseCode: FOUND + schemeRedirect: https From 97a52e38d4b11b27781a0da6645f776cb3f535e2 Mon Sep 17 00:00:00 2001 From: shawnh2 Date: Wed, 19 Jun 2024 14:04:31 +0800 Subject: [PATCH 2/2] fix yaml lint Signed-off-by: shawnh2 --- .../testdata/in/xds-ir/http-route-redirect.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml b/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml index 848866c0503..1c541a9caac 100644 --- a/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml +++ b/internal/xds/translator/testdata/in/xds-ir/http-route-redirect.yaml @@ -55,9 +55,9 @@ http: destination: name: "redirect-route-dest" settings: - - endpoints: - - host: "1.2.3.4" - port: 50000 + - endpoints: + - host: "1.2.3.4" + port: 50000 redirect: scheme: https statusCode: 302 @@ -69,9 +69,9 @@ http: destination: name: "redirect-route-dest" settings: - - endpoints: - - host: "1.2.3.4" - port: 50000 + - endpoints: + - host: "1.2.3.4" + port: 50000 redirect: scheme: https statusCode: 302