Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
57 changes: 56 additions & 1 deletion api/envoy/api/v2/route/route_components.proto
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ message CorsPolicy {
core.RuntimeFractionalPercent shadow_enabled = 10;
}

// [#next-free-field: 32]
// [#next-free-field: 33]
message RouteAction {
enum ClusterNotFoundResponseCode {
// HTTP status code - 503 Service Unavailable.
Expand Down Expand Up @@ -696,6 +696,27 @@ message RouteAction {
google.protobuf.BoolValue enabled = 2;
}

// Describe how to match the path and rewrite it using a regular expression

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we extract this type into envoy.type? I think we're likely to reuse it in other places?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through this again, yes, I think it makes sense to move this to a matcher. How about RegexMatchAndSubstitute or something like that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through this again, yes, I think it makes sense to move this to a matcher. How about RegexMatchAndSubstitute or something like that?

@jphx jphx Feb 25, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged from the master branch again to resolve some conflicts and made the requested change, renaming the RegexRewrite message to RegexMatchAndSubstitute and making it a general type so it can potentially be re-used elsewhere.

// and a substitution string.
message RegexRewrite {
// The regular expression used to find portions of the path (excluding
// query parameters) that should be replaced. All matches in the path are
// replaced by the substitution string, though anchors in the regular
// expression can be used to replace just one occurrence of a pattern.
// Capture groups can be used in the pattern to extract portions of the
// path, and referenced in the substitution string.
type.matcher.RegexMatcher pattern = 1;

// The string that should be substituted into matching portions of the path.
// Capture groups in the pattern can be referenced in this string. Beware,
// however, that the syntax for referring to capture groups is defined by
// the regular expression engines. Google's `RE2 <https://github.com/google/re2>`_
// regular expression engine uses backslash followed by the capture group
// number to denote a numbered capture group. E.g., ``\1`` refers to capture
// group 1, and ``\2`` refers to capture group 2.
string substitution = 2;
}

reserved 12, 18, 19, 16, 22, 21;

oneof cluster_specifier {
Expand Down Expand Up @@ -742,6 +763,10 @@ message RouteAction {
// place the original path before rewrite into the :ref:`x-envoy-original-path
// <config_http_filters_router_x-envoy-original-path>` header.
//
// Only one of *prefix_rewrite* or
// :ref:`regex_rewrite <envoy_api_field_route.RouteAction.regex_rewrite>`
// may be specified.
//
// .. attention::
//
// Pay careful attention to the use of trailing slashes in the
Expand All @@ -765,6 +790,36 @@ message RouteAction {
// requests to */prefix/etc* will be stripped to */etc*.
string prefix_rewrite = 5;

// Indicates that during forwarding, portions of the path that match the
// pattern should be rewritten, even allowing the substitution of capture
// groups from the pattern into the new path as specified by the rewrite
// substitution string. This is useful to allow application paths to be
// rewritten in a way that is aware of segments with variable content like
// identifiers. The router filter will place the original path as it was
// before the rewrite into the :ref:`x-envoy-original-path
// <config_http_filters_router_x-envoy-original-path>` header.
//
// Only one of :ref:`prefix_rewrite <envoy_api_field_route.RouteAction.prefix_rewrite>`
// or *regex_rewrite* may be specified.
//
// Examples using Google's `RE2 <https://github.com/google/re2>`_ engine:
//
// * The path pattern ``^/service/([^/]+)(/.*)$`` paired with a substitution
// string of ``\2/instance/\1`` would transform ``/service/foo/v1/api``
// into ``/v1/api/instance/foo``.
//
// * The pattern ``one`` paired with a substitution string of ``two`` would
// transform ``/xxx/one/yyy/one/zzz`` into ``/xxx/two/yyy/two/zzz``.
//
// * The pattern ``^(.*?)one(.*)$`` paired with a substitution string of
// ``\1two\2`` would replace only the first occurrence of ``one``,
// transforming path ``/xxx/one/yyy/one/zzz`` into ``/xxx/two/yyy/one/zzz``.
//
// * The pattern ``(?i)/xxx/`` paired with a substitution string of ``/yyy/``
// would do a case-insensitive match and transform path ``/aaa/XxX/bbb`` to
// ``/aaa/yyy/bbb``.
RegexRewrite regex_rewrite = 32;
Comment thread
zuercher marked this conversation as resolved.
Outdated

oneof host_rewrite_specifier {
// Indicates that during forwarding, the host header will be swapped with
// this value.
Expand Down
60 changes: 59 additions & 1 deletion api/envoy/config/route/v3/route_components.proto
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ message CorsPolicy {
core.v3.RuntimeFractionalPercent shadow_enabled = 10;
}

// [#next-free-field: 32]
// [#next-free-field: 33]
message RouteAction {
option (udpa.annotations.versioning).previous_message_type = "envoy.api.v2.route.RouteAction";

Expand Down Expand Up @@ -667,6 +667,30 @@ message RouteAction {
google.protobuf.BoolValue enabled = 2;
}

// Describe how to match the path and rewrite it using a regular expression
// and a substitution string.
message RegexRewrite {
option (udpa.annotations.versioning).previous_message_type =
"envoy.api.v2.route.RouteAction.RegexRewrite";

// The regular expression used to find portions of the path (excluding
// query parameters) that should be replaced. All matches in the path are
// replaced by the substitution string, though anchors in the regular
// expression can be used to replace just one occurrence of a pattern.
// Capture groups can be used in the pattern to extract portions of the
// path, and referenced in the substitution string.
type.matcher.v3.RegexMatcher pattern = 1;

// The string that should be substituted into matching portions of the path.
// Capture groups in the pattern can be referenced in this string. Beware,
// however, that the syntax for referring to capture groups is defined by
// the regular expression engines. Google's `RE2 <https://github.com/google/re2>`_
// regular expression engine uses backslash followed by the capture group
// number to denote a numbered capture group. E.g., ``\1`` refers to capture
// group 1, and ``\2`` refers to capture group 2.
string substitution = 2;
}

reserved 12, 18, 19, 16, 22, 21, 10;

reserved "request_mirror_policy";
Expand Down Expand Up @@ -715,6 +739,10 @@ message RouteAction {
// place the original path before rewrite into the :ref:`x-envoy-original-path
// <config_http_filters_router_x-envoy-original-path>` header.
//
// Only one of *prefix_rewrite* or
// :ref:`regex_rewrite <envoy_api_field_config.route.v3.RouteAction.regex_rewrite>`
// may be specified.
//
// .. attention::
//
// Pay careful attention to the use of trailing slashes in the
Expand All @@ -738,6 +766,36 @@ message RouteAction {
// requests to */prefix/etc* will be stripped to */etc*.
string prefix_rewrite = 5;

// Indicates that during forwarding, portions of the path that match the
// pattern should be rewritten, even allowing the substitution of capture
// groups from the pattern into the new path as specified by the rewrite
// substitution string. This is useful to allow application paths to be
// rewritten in a way that is aware of segments with variable content like
// identifiers. The router filter will place the original path as it was
// before the rewrite into the :ref:`x-envoy-original-path
// <config_http_filters_router_x-envoy-original-path>` header.
//
// Only one of :ref:`prefix_rewrite <envoy_api_field_config.route.v3.RouteAction.prefix_rewrite>`
// or *regex_rewrite* may be specified.
//
// Examples using Google's `RE2 <https://github.com/google/re2>`_ engine:
//
// * The path pattern ``^/service/([^/]+)(/.*)$`` paired with a substitution
// string of ``\2/instance/\1`` would transform ``/service/foo/v1/api``
// into ``/v1/api/instance/foo``.
//
// * The pattern ``one`` paired with a substitution string of ``two`` would
// transform ``/xxx/one/yyy/one/zzz`` into ``/xxx/two/yyy/two/zzz``.
//
// * The pattern ``^(.*?)one(.*)$`` paired with a substitution string of
// ``\1two\2`` would replace only the first occurrence of ``one``,
// transforming path ``/xxx/one/yyy/one/zzz`` into ``/xxx/two/yyy/one/zzz``.
//
// * The pattern ``(?i)/xxx/`` paired with a substitution string of ``/yyy/``
// would do a case-insensitive match and transform path ``/aaa/XxX/bbb`` to
// ``/aaa/yyy/bbb``.
RegexRewrite regex_rewrite = 32;

oneof host_rewrite_specifier {
// Indicates that during forwarding, the host header will be swapped with
// this value.
Expand Down
3 changes: 2 additions & 1 deletion docs/root/configuration/http/http_conn_man/headers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ route, virtual host, and/or global route configuration level. See the

No *:-prefixed* pseudo-header may be modified via this mechanism. The *:path*
and *:authority* headers may instead be modified via mechanisms such as
:ref:`prefix_rewrite <envoy_api_field_route.RouteAction.prefix_rewrite>` and
:ref:`prefix_rewrite <envoy_api_field_route.RouteAction.prefix_rewrite>`,
:ref:`regex_rewrite <envoy_api_field_route.RouteAction.regex_rewrite>`, and
:ref:`host_rewrite <envoy_api_field_route.RouteAction.host_rewrite>`.

Headers are appended to requests/responses in the following order: weighted cluster level headers,
Expand Down
3 changes: 2 additions & 1 deletion docs/root/configuration/http/http_filters/router_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ responses.
x-envoy-original-path
^^^^^^^^^^^^^^^^^^^^^

If the route utilizes :ref:`prefix_rewrite <envoy_api_field_route.RouteAction.prefix_rewrite>`,
If the route utilizes :ref:`prefix_rewrite <envoy_api_field_route.RouteAction.prefix_rewrite>`
or :ref:`regex_rewrite <envoy_api_field_route.RouteAction.regex_rewrite>`,
Envoy will put the original path header in this header. This can be useful for logging and
debugging.

Expand Down
1 change: 1 addition & 0 deletions docs/root/intro/arch_overview/http/http_routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ request. The router filter supports the following features:
* :ref:`Automatic host rewriting <envoy_api_field_route.RouteAction.auto_host_rewrite>` based on
the DNS name of the selected upstream host.
* :ref:`Prefix rewriting <envoy_api_field_route.RedirectAction.prefix_rewrite>`.
* :ref:`Path rewriting using a regular expression and capture groups <envoy_api_field_route.RouteAction.regex_rewrite>`.
* :ref:`Request retries <arch_overview_http_routing_retry>` specified either via HTTP header or via
route configuration.
* Request timeout specified either via :ref:`HTTP
Expand Down
2 changes: 2 additions & 0 deletions docs/root/intro/version_history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Version history
* router: added :ref:`auto_san_validation <envoy_api_field_core.UpstreamHttpProtocolOptions.auto_san_validation>` to support overrriding SAN validation to transport socket for new upstream connections based on the downstream HTTP host/authority header.
* router: added the ability to match a route based on whether a downstream TLS connection certificate has been
:ref:`validated <envoy_api_field_route.RouteMatch.TlsContextMatchOptions.validated>`.
* router: added support for :ref:`regex_rewrite
<envoy_api_field_route.RouteAction.regex_rewrite>` for path rewriting using regular expressions and capture groups.
* sds: added :ref:`GenericSecret <envoy_api_msg_auth.GenericSecret>` to support secret of generic type.
* stat sinks: stat sink extensions use the "envoy.stat_sinks" name space. A mapping of extension
names is available in the :ref:`deprecated <deprecated>` documentation.
Expand Down
57 changes: 56 additions & 1 deletion generated_api_shadow/envoy/api/v2/route/route_components.proto

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

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

Loading