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

feat(meshhttproute): add path modifier to redirect #5918

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 23 additions & 10 deletions deployments/charts/kuma/crds/kuma.io_meshhttproutes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ spec:
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines parameters used
to modify the path of the incoming request.
The modified path is then used to construct
the Location header. When empty, the request
path is used as-is.
properties:
replaceFullPath:
type: string
replacePrefixMatch:
type: string
type:
enum:
- ReplaceFullPath
- ReplacePrefixMatch
type: string
required:
- type
type: object
port:
description: Port is the port to be used
in the value of the `Location` header
Expand Down Expand Up @@ -270,21 +289,15 @@ spec:
urlRewrite:
properties:
hostname:
description: "PreciseHostname is the fully
qualified domain name of a network host.
This matches the RFC 1123 definition of
a hostname with 1 notable exception that
numeric IP addresses are not allowed.
\n Note that as per RFC1035 and RFC1123,
a *label* must consist of lower case alphanumeric
characters or '-', and must start and
end with an alphanumeric character. No
other punctuation is allowed."
description: Hostname is the value to be
used to replace the Host header value
during forwarding.
maxLength: 253
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines a path rewrite.
properties:
replaceFullPath:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,13 @@ type RequestRedirect struct {
// +kubebuilder:validation:Enum=http;https
Scheme *string `json:"scheme,omitempty"`
Hostname *PreciseHostname `json:"hostname,omitempty"`
// Path defines parameters used to modify the path of the incoming request.
// The modified path is then used to construct the Location header.
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
// When empty, the request path is used as-is.
Path *PathRewrite `json:"path,omitempty"`
// Port is the port to be used in the value of the `Location`
// header in the response.
// When empty, port (if specified) of the request is used.
//
Port *PortNumber `json:"port,omitempty"`
// StatusCode is the HTTP status code to be used in response.
//
Expand All @@ -168,8 +171,10 @@ type PathRewrite struct {
}

type URLRewrite struct {
// Hostname is the value to be used to replace the Host header value during forwarding.
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
Hostname *PreciseHostname `json:"hostname,omitempty"`
Path *PathRewrite `json:"path,omitempty"`
// Path defines a path rewrite.
Path *PathRewrite `json:"path,omitempty"`
}

type Filter struct {
Expand All @@ -178,7 +183,6 @@ type Filter struct {
ResponseHeaderModifier *HeaderModifier `json:"responseHeaderModifier,omitempty"`
RequestRedirect *RequestRedirect `json:"requestRedirect,omitempty"`
URLRewrite *URLRewrite `json:"urlRewrite,omitempty"`
// TODO: add path to redirect after adding URL rewrite
}

type BackendRef struct {
Expand Down
18 changes: 17 additions & 1 deletion pkg/plugins/policies/meshhttproute/api/v1alpha1/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ properties:
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines parameters used to modify the path of the incoming request. The modified path is then used to construct the Location header. When empty, the request path is used as-is.
properties:
replaceFullPath:
type: string
replacePrefixMatch:
type: string
type:
enum:
- ReplaceFullPath
- ReplacePrefixMatch
type: string
required:
- type
type: object
port:
description: Port is the port to be used in the value of the `Location` header in the response. When empty, port (if specified) of the request is used.
format: int32
Expand Down Expand Up @@ -214,12 +229,13 @@ properties:
urlRewrite:
properties:
hostname:
description: "PreciseHostname is the fully qualified domain name of a network host. This matches the RFC 1123 definition of a hostname with 1 notable exception that numeric IP addresses are not allowed. \n Note that as per RFC1035 and RFC1123, a *label* must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character. No other punctuation is allowed."
description: Hostname is the value to be used to replace the Host header value during forwarding.
maxLength: 253
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines a path rewrite.
properties:
replaceFullPath:
type: string
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ spec:
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines parameters used
to modify the path of the incoming request.
The modified path is then used to construct
the Location header. When empty, the request
path is used as-is.
properties:
replaceFullPath:
type: string
replacePrefixMatch:
type: string
type:
enum:
- ReplaceFullPath
- ReplacePrefixMatch
type: string
required:
- type
type: object
port:
description: Port is the port to be used
in the value of the `Location` header
Expand Down Expand Up @@ -270,21 +289,15 @@ spec:
urlRewrite:
properties:
hostname:
description: "PreciseHostname is the fully
qualified domain name of a network host.
This matches the RFC 1123 definition of
a hostname with 1 notable exception that
numeric IP addresses are not allowed.
\n Note that as per RFC1035 and RFC1123,
a *label* must consist of lower case alphanumeric
characters or '-', and must start and
end with an alphanumeric character. No
other punctuation is allowed."
description: Hostname is the value to be
used to replace the Host header value
during forwarding.
maxLength: 253
minLength: 1
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
type: string
path:
description: Path defines a path rewrite.
properties:
replaceFullPath:
type: string
Expand Down
64 changes: 44 additions & 20 deletions pkg/plugins/policies/meshhttproute/xds/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func routeFilter(filter api.Filter, route *envoy_route.Route, matchesPrefix bool
case api.ResponseHeaderModifierType:
responseHeaderModifier(*filter.ResponseHeaderModifier, route)
case api.RequestRedirectType:
requestRedirect(*filter.RequestRedirect, route)
requestRedirect(*filter.RequestRedirect, route, matchesPrefix)
case api.URLRewriteType:
urlRewrite(*filter.URLRewrite, route, matchesPrefix)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func responseHeaderModifier(mod api.HeaderModifier, envoyRoute *envoy_route.Rout
envoyRoute.ResponseHeadersToRemove = append(envoyRoute.ResponseHeadersToRemove, removes...)
}

func requestRedirect(redirect api.RequestRedirect, envoyRoute *envoy_route.Route) {
func requestRedirect(redirect api.RequestRedirect, envoyRoute *envoy_route.Route, withPrefixMatch bool) {
envoyRedirect := &envoy_route.RedirectAction{}
if redirect.Hostname != nil {
envoyRedirect.HostRedirect = string(*redirect.Hostname)
Expand All @@ -88,6 +88,22 @@ func requestRedirect(redirect api.RequestRedirect, envoyRoute *envoy_route.Route
SchemeRedirect: *redirect.Scheme,
}
}
if redirect.Path != nil {
switch redirect.Path.Type {
case api.ReplaceFullPathType:
envoyRedirect.PathRewriteSpecifier = regexToSpecifier(regexRewrite(*redirect.Path.ReplaceFullPath))
case api.ReplacePrefixMatchType:
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
if withPrefixMatch {
if envoyRoute.Match.GetPath() != "" {
// We have the "exact /prefix" match case
envoyRedirect.PathRewriteSpecifier = regexToSpecifier(regexRewrite(*redirect.Path.ReplacePrefixMatch))
} else if envoyRoute.Match.GetPrefix() != "" {
// We have the "prefix /prefix/" match case
envoyRedirect.PathRewriteSpecifier = prefixToSpecifier(*redirect.Path.ReplacePrefixMatch)
}
}
}
}

switch pointer.DerefOr(redirect.StatusCode, 301) {
case 301:
Expand Down Expand Up @@ -119,28 +135,12 @@ func urlRewrite(rewrite api.URLRewrite, envoyRoute *envoy_route.Route, withPrefi
if rewrite.Path != nil {
switch rewrite.Path.Type {
case api.ReplaceFullPathType:
action.RegexRewrite = &envoy_type_matcher.RegexMatchAndSubstitute{
Pattern: &envoy_type_matcher.RegexMatcher{
EngineType: &envoy_type_matcher.RegexMatcher_GoogleRe2{
GoogleRe2: &envoy_type_matcher.RegexMatcher_GoogleRE2{},
},
Regex: `.*`,
},
Substitution: *rewrite.Path.ReplaceFullPath,
}
action.RegexRewrite = regexRewrite(*rewrite.Path.ReplaceFullPath)
case api.ReplacePrefixMatchType:
if withPrefixMatch {
if envoyRoute.Match.GetPath() != "" {
// We have the "exact /prefix" match case
action.RegexRewrite = &envoy_type_matcher.RegexMatchAndSubstitute{
Pattern: &envoy_type_matcher.RegexMatcher{
EngineType: &envoy_type_matcher.RegexMatcher_GoogleRe2{
GoogleRe2: &envoy_type_matcher.RegexMatcher_GoogleRE2{},
},
Regex: `.*`,
},
Substitution: *rewrite.Path.ReplacePrefixMatch,
}
action.RegexRewrite = regexRewrite(*rewrite.Path.ReplacePrefixMatch)
} else if envoyRoute.Match.GetPrefix() != "" {
// We have the "prefix /prefix/" match case
action.PrefixRewrite = *rewrite.Path.ReplacePrefixMatch
Expand All @@ -152,3 +152,27 @@ func urlRewrite(rewrite api.URLRewrite, envoyRoute *envoy_route.Route, withPrefi
Route: action,
}
}

func regexRewrite(s string) *envoy_type_matcher.RegexMatchAndSubstitute {
return &envoy_type_matcher.RegexMatchAndSubstitute{
Pattern: &envoy_type_matcher.RegexMatcher{
EngineType: &envoy_type_matcher.RegexMatcher_GoogleRe2{
GoogleRe2: &envoy_type_matcher.RegexMatcher_GoogleRE2{},
},
Regex: `.*`,
},
Substitution: s,
}
}

func regexToSpecifier(regMatch *envoy_type_matcher.RegexMatchAndSubstitute) *envoy_route.RedirectAction_RegexRewrite {
return &envoy_route.RedirectAction_RegexRewrite{
RegexRewrite: regMatch,
}
}

func prefixToSpecifier(prefix string) *envoy_route.RedirectAction_PrefixRewrite {
return &envoy_route.RedirectAction_PrefixRewrite{
PrefixRewrite: prefix,
}
}
46 changes: 46 additions & 0 deletions test/e2e_env/kubernetes/meshhttproute/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,50 @@ spec:
g.Expect(response.Instance).To(HavePrefix("external-service"))
}, "30s", "1s").Should(Succeed())
})

It("should configure redirect response", func() {
// when
Expect(YamlK8s(fmt.Sprintf(`
apiVersion: kuma.io/v1alpha1
kind: MeshHTTPRoute
metadata:
name: route-3
namespace: %s
labels:
kuma.io/mesh: %s
spec:
targetRef:
kind: MeshService
name: test-client_%s_svc_80
to:
- targetRef:
kind: MeshService
name: test-server_meshhttproute_svc_80
rules:
- matches:
- path:
type: Prefix
value: /
default:
filters:
- type: RequestRedirect
requestRedirect:
statusCode: 307
path:
type: ReplaceFullPath
replaceFullPath: /new-path
backendRefs:
- kind: MeshService
name: test-server_meshhttproute_svc_80
weight: 1
`, Config.KumaNamespace, meshName, meshName))(kubernetes.Cluster)).To(Succeed())

// then receive redirect response
Eventually(func(g Gomega) {
failure, err := client.CollectFailure(kubernetes.Cluster, "test-client", "test-server_meshhttproute_svc_80.mesh", client.FromKubernetesPod(namespace, "test-client"))
g.Expect(err).ToNot(HaveOccurred())
g.Expect(failure.ResponseCode).To(Equal(307))
g.Expect(failure.RedirectURL).To(Equal("http://test-server_meshhttproute_svc_80.mesh/new-path"))
}, "30s", "1s").Should(Succeed())
})
}
1 change: 1 addition & 0 deletions test/framework/client/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ type FailureResponse struct {
ContentType string `json:"content_type"`
URL string `json:"url"`
EffectiveURL string `json:"url_effective"`
RedirectURL string `json:"redirect_url"`
}

// CollectFailure runs Curl to fetch a URL that is expected to fail. The
Expand Down