From 96475fe40223f2b877807dbedd55b3cc80dfb7d2 Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Sun, 29 Oct 2023 16:31:58 -0700 Subject: [PATCH] Enable HTTPRouteRewritePath test Fixes: https://github.com/envoyproxy/gateway/issues/2004 Signed-off-by: Arko Dasgupta --- internal/xds/translator/route.go | 19 ++++++++++++++++--- ...e-rewrite-root-path-url-prefix.routes.yaml | 4 +++- test/conformance/conformance_test.go | 1 - .../experimental_conformance_test.go | 1 - 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/xds/translator/route.go b/internal/xds/translator/route.go index e9546e22772b..8ceda5975e3e 100644 --- a/internal/xds/translator/route.go +++ b/internal/xds/translator/route.go @@ -43,7 +43,7 @@ func buildXdsRoute(httpRoute *ir.HTTPRoute) *routev3.Route { case httpRoute.Redirect != nil: router.Action = &routev3.Route_Redirect{Redirect: buildXdsRedirectAction(httpRoute.Redirect)} case httpRoute.URLRewrite != nil: - routeAction := buildXdsURLRewriteAction(httpRoute.Destination.Name, httpRoute.URLRewrite) + routeAction := buildXdsURLRewriteAction(httpRoute.Destination.Name, httpRoute.URLRewrite, httpRoute.PathMatch) if httpRoute.Mirrors != nil { routeAction.RequestMirrorPolicies = buildXdsRequestMirrorPolicies(httpRoute.Mirrors) } @@ -252,7 +252,7 @@ func buildXdsRedirectAction(redirection *ir.Redirect) *routev3.RedirectAction { return routeAction } -func buildXdsURLRewriteAction(destName string, urlRewrite *ir.URLRewrite) *routev3.RouteAction { +func buildXdsURLRewriteAction(destName string, urlRewrite *ir.URLRewrite, pathMatch *ir.StringMatch) *routev3.RouteAction { routeAction := &routev3.RouteAction{ ClusterSpecifier: &routev3.RouteAction_Cluster{ Cluster: destName, @@ -268,7 +268,20 @@ func buildXdsURLRewriteAction(destName string, urlRewrite *ir.URLRewrite) *route Substitution: *urlRewrite.Path.FullReplace, } } else if urlRewrite.Path.PrefixMatchReplace != nil { - routeAction.PrefixRewrite = *urlRewrite.Path.PrefixMatchReplace + // Circumvent the case of "//" when the replace string is "/" + // An empty replace string does not seem to solve the issue so we are using + // a regex match and replace instead + if pathMatch != nil && pathMatch.Prefix != nil && + (*urlRewrite.Path.PrefixMatchReplace == "" || *urlRewrite.Path.PrefixMatchReplace == "/") { + routeAction.RegexRewrite = &matcherv3.RegexMatchAndSubstitute{ + Pattern: &matcherv3.RegexMatcher{ + Regex: "^" + *pathMatch.Prefix, + }, + Substitution: "", + } + } else { + routeAction.PrefixRewrite = *urlRewrite.Path.PrefixMatchReplace + } } } diff --git a/internal/xds/translator/testdata/out/xds-ir/http-route-rewrite-root-path-url-prefix.routes.yaml b/internal/xds/translator/testdata/out/xds-ir/http-route-rewrite-root-path-url-prefix.routes.yaml index 1604805cc9b7..e171ec00c812 100644 --- a/internal/xds/translator/testdata/out/xds-ir/http-route-rewrite-root-path-url-prefix.routes.yaml +++ b/internal/xds/translator/testdata/out/xds-ir/http-route-rewrite-root-path-url-prefix.routes.yaml @@ -14,4 +14,6 @@ name: rewrite-route route: cluster: rewrite-route-dest - prefixRewrite: / + regexRewrite: + pattern: + regex: ^/origin/ diff --git a/test/conformance/conformance_test.go b/test/conformance/conformance_test.go index eeb8898ffc7b..0e08a5a1ad15 100644 --- a/test/conformance/conformance_test.go +++ b/test/conformance/conformance_test.go @@ -48,7 +48,6 @@ func TestGatewayAPIConformance(t *testing.T) { CleanupBaseResources: *flags.CleanupBaseResources, SupportedFeatures: suite.AllFeatures, SkipTests: []string{ - tests.HTTPRouteRewritePath.ShortName, tests.GatewayStaticAddresses.ShortName, tests.GatewayWithAttachedRoutes.ShortName, tests.HTTPRouteBackendProtocolH2C.ShortName, diff --git a/test/conformance/experimental_conformance_test.go b/test/conformance/experimental_conformance_test.go index fd4a5620973e..a6a308cbbced 100644 --- a/test/conformance/experimental_conformance_test.go +++ b/test/conformance/experimental_conformance_test.go @@ -97,7 +97,6 @@ func experimentalConformance(t *testing.T) { CleanupBaseResources: *flags.CleanupBaseResources, SupportedFeatures: suite.AllFeatures, SkipTests: []string{ - tests.HTTPRouteRewritePath.ShortName, tests.GatewayStaticAddresses.ShortName, tests.GatewayWithAttachedRoutes.ShortName, tests.HTTPRouteBackendProtocolH2C.ShortName,