Kong / kong

๐Ÿฆ The Cloud-Native API Gateway and AI Gateway.
https://konghq.com/install/#kong-community
Apache License 2.0
39.24k stars 4.81k forks source link

allow for parameter mapping in path based routing #677

Closed ahmadnassri closed 7 years ago

ahmadnassri commented 9 years ago

e.g. allow request_path to contain values such as /api/{foo}/bar which map to the upstream_url /upstream/my/api/bar/{foo}

this could also be more globally addressed with regex matching groups e.g.

request_url = '/api/(.+)/bar`
upstream_url = '/upstream/my/api/bar/$1'
thibaultcha commented 7 years ago

Hi everyone! I just submitted #2681 which implements this :)

It contains support for regex URIs in the router, and provides PCRE groups captures as well. Feedback, testing and reviews are much welcome!

Edit: we will probably include this feature in our 0.11 release family (upcoming rc2, our subsequent 0.11.1 release).

rdzak commented 7 years ago

Hi @thibaultcha!

First of all - thank so much for working on it. Do I understand correctly that this feature allows dynamically building upstream URLs from available data such as consumer_ids? For instance, when using auth plugin, consumer_id is sent currently in the header to upstream API. Will this allow me to dynamically build upstream routes using this variable like that: /customers/{consumer_id}/something? Also I'm super curious when is the next release planned :)

Kind regards.

thibaultcha commented 7 years ago

@rdzak Hi,

Do I understand correctly that this feature allows dynamically building upstream URLs from available data such as consumer_ids?

It does not. This work enabled URIs regexp matching and capturing groups extraction. It then exposes the captured groups to the plugins for the lifecycle of the request. It is now up to the plugin to be updated and to use those capturing groups to do something. That is beyond the scope of the proposed PR, and still in the scope of this issue. We are working on it.

Additionally, if the value you want to use comes from the request headers, then it is not dependent on the regexp PR in any way, but entirely on the request-transformer plugin.

Also I'm super curious when is the next release planned :)

We have an RC 2 coming up next week, and stable 0.11 in the upcoming weeks if RC 2 is successful.

thibaultcha commented 7 years ago

Hi everyone!

As of today, all the work that we planned for this so far has been released in 0.11.0. So, before closing the conversation here, I would like to recap a few things.

This issue actually described two features:

  1. The ability to set regexes in an API's uris attribute (and match an API with not only prefixes of URIs, but also dynamic path segments).
  2. Extracting the dynamic path segments (whether named regex capturing groups or not), and substitute them in the upstream call URI.

As of today, 1. is available starting from Kong 0.11.0 ๐ŸŽ‰, and 2. is available as part of our Mashape Enterprise subscription. In our most recent release for our Enterprise customers, we've improved the request-transformer plugin to support substitution for Lua land values, such as: config.uri.replace=/users/$(uri_captures.user_id)/profile.

You can find more information about our Mashape Enterprise subscription and its additional features on top of Kong at: https://mashape.com/enterprise.

As a "bonus feature", I would like to highlight that Kong 0.11.0 also includes:

  1. Consumption of the API router matches from the plugins via the new ngx.ctx.router_matches variable.

This should allow for some good hacking sessions in your Kong custom plugins :wink:

With that said, I believe we can now close this conversation and consider it implemented under the scope provided by this issue! You can checkout Kong 0.11.0 here: https://github.com/Mashape/kong/releases/tag/0.11.0

Thanks!

merlindorin commented 7 years ago

At least, we don't forget that kong is also an entreprise product ๐Ÿค”

Thx for the work, this feature really change the game for us. For the first time we can purpose your product to our customer because it fits our development. ๐Ÿ’Œ

I hope that the next big step will be an integration of Swagger (at least params/body/query sync validation) ๐Ÿ‘

mrproper commented 7 years ago

What is the correct way to do a regex in a uris attribute, when i do it via curl it does not yield the results im after: curl http://127.0.0.1:8001/apis/foo -X PATCH --data 'uris=/v1/foo/\d+/bar' curl http://127.0.0.1:8001/apis/foo -X GET { ... "uris" : [ "/v1/foo/\\d /bar" ], ... }

RolphH commented 7 years ago

@mrproper see https://github.com/Mashape/kong/pull/2681#issuecomment-314228542

techtyler commented 7 years ago

Is it possible to use this feature with QueryString parameters? I'm having issue with getting the regex to match the "?" character in the request uri. I want to set up 2 apis with the only difference being the presence of the query string (such as "?debug=true") is added.

Example (get user by ID with debug querystring) RequestURI: /User/[0-9]{8}\?debug=true

I've attempted to escape and double escape the '?' but it seems to be ignored. If I use another character such as '&', everything works fine. There seems to be some issue with the reserved regex characters. I've also tried using the url encoded "%3F" but that doesn't get sent properly to my service and it does not pick up the query string parameters.

thibaultcha commented 7 years ago

@techtyler Copying here my answer to you from Gitter for future reference to this thread (please avoid cross-posintg in the future):

@techtyler Kongโ€™s router evaluates URIs after stripping out the request arguments from the request line, which is why this regex will never match an incoming URI
@techtyler No plugin or core feature would allow you to route based on querystring args quite yet. Youโ€™d have to develop some in house solution in a plugin

Also, I will now be locking this issue to avoid further noise. The usual support channels (Gitter/Freenode/Mailing list) are the preferred channel of communication for getting help and asking questions. Today, GitHub issues are only looked at when actually reporting bugs. Thank you for your cooperation!