Skip to content

Commit

Permalink
Merge pull request #1420 from datawire/story/regex_rewrite_nits
Browse files Browse the repository at this point in the history
Regex rewrite nits.
  • Loading branch information
alphashr committed May 27, 2020
2 parents bc91b27 + bd01501 commit 6548022
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
60 changes: 36 additions & 24 deletions docs/topics/using/rewrites.md
Expand Up @@ -8,56 +8,68 @@ There are two approaches for rewriting: `rewrite` for simpler scenarios and `reg

## `rewrite`

By default, the `prefix` is rewritten to `/`, so e.g., if we map `/backend-api/` to the service `service1`, then

By default, the `prefix` is rewritten to `/`, so e.g., if we map `/prefix1/` to the service `service1`, then
<code>
http://ambassador.example.com<span style="color:red">/backend-api/</span><span style="color:green">foo/bar</span>
</code>

* ```prefix```: <span style="color:red">/backend-api/</span> which rewrites to <span style="color:red">/</span> by default.
* ```rewrite```: <span style="color:red">/</span>
* ```remainder```: <span style="color:green">foo/bar</span>

```shell
http://ambassador.example.com/prefix1/foo/bar
```

would effectively be written to

```shell
http://service1/foo/bar
```
<code>
http://service1<span style="color:red">/</span><span style="color:green">foo/bar</span>
</code>

when it was handed to `service1`.
* ```prefix```: was <span style="color:red">/backend-api/</span>
* ```rewrite```: <span style="color:red">/</span> (by default)

You can change the rewriting: for example, if you choose to rewrite the prefix as `/v1/` in this example, the final target would be:
You can change the rewriting: for example, if you choose to rewrite the prefix as <span style="color:red">/v1/</span> in this example, the final target would be:

```shell
http://service1/v1/foo/bar
```

<code>
http://service1<span style="color:red">/v1/</span><span style="color:green">foo/bar</span>
</code>

* ```prefix```: was <span style="color:red">/backend-api/</span>
* ```rewrite```: <span style="color:red">/v1/</span>

And, of course, you can choose to rewrite the prefix to the prefix itself, so that

```shell
http://ambassador.example.com/prefix1/foo/bar
```
<code>
http://ambassador.example.com<span style="color:red">/backend-api/</span><span style="color:green">foo/bar</span>
</code>

* ```prefix```: <span style="color:red">/backend-api/</span>
* ```rewrite```: <span style="color:red">/backend-api/</span>

would be "rewritten" as:

```shell
http://service1/prefix1/foo/bar
```
<code>
http://service1<span style="color:red">/backend-api/</span><span style="color:green">foo/bar</span>
</code>

Ambassador Edge Stack can be configured to not change the prefix as it forwards a request to the upstream service. To do that, specify an empty `rewrite` directive:
Ambassador can be configured to not change the prefix as it forwards a request to the upstream service. To do that, specify an empty `rewrite` directive:

- `rewrite: ""`

## `regex_rewrite`

In some cases, a portion of URL needs to be extracted before making the upstream service URL. For example, suppose that when a request is made to `leaderboards/v1/12345/find`, the target URL must be rewritten as `game/12345`. We can do this as follows:
In some cases, a portion of URL needs to be extracted before making the upstream service URL. For example, suppose that when a request is made to `foo/12345/list`, the target URL must be rewritten as `bar/12345`. We can do this as follows:

```shell
prefix: /leaderboards/
prefix: /foo/
regex_rewrite:
pattern: 'leaderboards/v1/([0-9]*)/find'
substitution: '/game/\1'
pattern: '/foo/([0-9]*)/list'
substitution: '/bar/\1'
```

`([0-9]*)` can be replaced with `(\d)` for simplicity.

More than one group can be captured in the `pattern` to be referenced by `\2`, `\3` and `\n` in the `substitution` section.

For more information on how rewrite and prefix can be configured, see [Mappings](../mappings).
For more information on how `Mapping` can be configured, see [Mappings](../mappings).
28 changes: 14 additions & 14 deletions python/tests/t_regexrewrite_forwarding.py
Expand Up @@ -18,8 +18,8 @@ def config(self):
prefix: /foo/
service: http://{self.target.path.fqdn}
regex_rewrite:
pattern: "foo/baz"
substitution: "baz/foo"
pattern: "/foo/baz"
substitution: "/baz/foo"
""")

def queries(self):
Expand All @@ -37,29 +37,29 @@ class RegexRewriteForwardingWithExtractAndSubstituteTest(AmbassadorTest):
target: ServiceType

def init(self):
self.target = HTTP(name="lboards")
self.target = HTTP(name="foo")

def config(self):
yield self.target, self.format(r"""
---
apiVersion: ambassador/v2
kind: Mapping
name: regex_rewrite_mapping
prefix: /leaderboards/
prefix: /foo/
service: http://{self.target.path.fqdn}
regex_rewrite:
pattern: "leaderboards/v1/([0-9]*)/find"
substitution: "game/\\1"
pattern: "/foo/([0-9]*)/list"
substitution: "/bar/\\1"
""")

def queries(self):
yield Query(self.url("leaderboards/v1/123456789/find"), expected=200)
yield Query(self.url("leaderboards/v1/987654321/find"), expected=200)
yield Query(self.url("leaderboardddddds/v1/123456789/find"), expected=404)
yield Query(self.url("leaderboards/v1/"), expected=200)
yield Query(self.url("foo/123456789/list"), expected=200)
yield Query(self.url("foo/987654321/list"), expected=200)
yield Query(self.url("fooooo/123456789/list"), expected=404)
yield Query(self.url("foo/"), expected=200)

def check(self):
assert self.results[0].backend.request.headers['x-envoy-original-path'][0] == f'/leaderboards/v1/123456789/find'
assert self.results[0].backend.request.url.path == "/game/123456789"
assert self.results[1].backend.request.headers['x-envoy-original-path'][0] == f'/leaderboards/v1/987654321/find'
assert self.results[1].backend.request.url.path == "/game/987654321"
assert self.results[0].backend.request.headers['x-envoy-original-path'][0] == f'/foo/123456789/list'
assert self.results[0].backend.request.url.path == "/bar/123456789"
assert self.results[1].backend.request.headers['x-envoy-original-path'][0] == f'/foo/987654321/list'
assert self.results[1].backend.request.url.path == "/bar/987654321"

0 comments on commit 6548022

Please sign in to comment.