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

Routing Policy enhancements #1103

Merged
merged 8 commits into from
Sep 9, 2019
Merged

Routing Policy enhancements #1103

merged 8 commits into from
Sep 9, 2019

Conversation

eloycoto
Copy link
Contributor

@eloycoto eloycoto commented Aug 1, 2019

Hi,

Multiple commits here:

  1. Added original request information in the executor in the ngx.ctx.
  2. Clean a little bit the ngx_variable, I think that nothing wrong around this one, but please have a look.
    Other commits are to allow liquid match in the routing policy to accomplish @ppatierno feedback.

Regards

@eloycoto eloycoto requested a review from a team as a code owner August 1, 2019 12:52
@eloycoto eloycoto force-pushed the Issue1084 branch 3 times, most recently from 9bcfe76 to 5772edf Compare August 1, 2019 14:11
@eloycoto eloycoto changed the title Routing Policy enhacements Routing Policy enhancements Aug 1, 2019
@eloycoto eloycoto force-pushed the Issue1084 branch 4 times, most recently from 9a65c86 to a622f85 Compare August 6, 2019 10:36
spec/policy/routing/routing_operation_spec.lua Outdated Show resolved Hide resolved
gateway/src/apicast/policy/routing/README.md Show resolved Hide resolved
gateway/src/apicast/policy/ngx_variable.lua Outdated Show resolved Hide resolved
-- [0] invalid phases: init_worker, init, timer and ssl_cer
-- [1] https://github.com/openresty/lua-resty-core/blob/9937f5d83367e388da4fcc1d7de2141c9e38d7e2/lib/resty/core/request.lua#L96

local result, _ = pcall(function()
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have an if here that sets context.original_request only when it's nil?
Otherwise, we might overwrite this var when some policy has already modified the headers, or the path. The shared_build_context function might guarantee that this never happens, but I'm not 100% sure.

Also, I have doubts about this. You mention that this is going to fail in the init_worker phase. However, the shared_build_context function will build the context in that phase and store it in ctx.context. Subsequent calls to the method will use what's stored in ctx.context instead of building again the context, so when is original_request initialized?

@eloycoto
Copy link
Contributor Author

Hi,

Finally, I remove the linked list to ngx.var due to the security concerts; some critical data can get from "ngx.var" variable (number of connections, client certs, etc..)

I was a bit optimistic when I set that, so it's as it was the only thing that I added is the request path to the ngx variable.

Thanks, David, for the catch!

CHANGELOG.md Outdated
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Option to load service configurations one by one lazily [PR #1099](https://github.com/3scale/APIcast/pull/1099)
- New maintenance mode policy, useful for maintenance periods. [PR #1105](https://github.com/3scale/APIcast/pull/1105), [THREESCALE-3189](https://issues.jboss.org/browse/THREESCALE-3189)
- Remove dnsmasq process for APIcast [PR #1090](https://github.com/3scale/APIcast/pull/1090), [THREESCALE-1555](https://issues.jboss.org/browse/THREESCALE-1555)
- Enable liquid operations and original request variable on routing policy [PR #1103](https://github.com/3scale/APIcast/pull/1103)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remember to include the JIRA as well.

ctx.context = context
store_original_request(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this guarantee that the original request will be initialized on every request?

In other words, store_original_request only runs when the context has not been initialized, but store_original_request fails in some phases, so I wonder if the following case is possible:

  • The body of this if runs and initializes ctx.context
  • store_original_request is called but fails to assign the original request data
  • The body of this if is not executed again and the original request data is never initialized

Copy link
Contributor Author

Choose a reason for hiding this comment

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

due to error happens in the same function call, I do not think that it's the case.

Copy link
Contributor

@davidor davidor Sep 5, 2019

Choose a reason for hiding this comment

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

@eloycoto I've reviewed the latest changes of the PR and everything looks good to me except this. Maybe I'm missing something.

I see 2 scenarios:

  • context is nil. store_original_request is called and it fails (there's a pcall so there are cases where it can fail). In that case, the original request would never be initialized in the context.
  • context is nil, but when store_original_request is called it never fails. In that case, the pcall would be unnecessary.

What am I missing?

@eloycoto eloycoto force-pushed the Issue1084 branch 2 times, most recently from 1c478ea to d963afd Compare August 23, 2019 16:12
@eloycoto eloycoto requested a review from davidor August 27, 2019 13:35
To be able to retrieve original request information on the policies
without adding/deleting headers.

This change allows users to handle routing policy with the original
information, full disclosure on issue 3scale#1084

Fix 3scale#1084

Signed-off-by: Eloy Coto <[email protected]>
This change add liquid matching to the routing policy. The change allow
users to route based on different information that maybe the current
policies are not allowed to.

The example use case here is to route based on original_request
variable, so the user can render the information using liquid and get a
way to route in a better way.

Example config:

```
{
  "services": [
    {
      "id": 42,
      "proxy": {
        "hosts": [
          "one"
        ],
        "policy_chain": [
          {
            "name": "apicast.policy.routing",
            "configuration": {
              "rules": [
                {
                  "url": "https://echo-api.3scale.net/",
                  "condition": {
                    "operations": [
                      {
                        "match": "liquid",
                        "liquid_value": "{{original_request.path}}",
                        "op": "matches",
                        "value": "/bridge-1"
                      }
                    ]
                  }
                }
              ]
            }
          },
          {
            "name": "url_rewriting",
            "configuration": {
              "commands": [
                {
                  "op": "sub",
                  "regex": "^/bridge",
                  "replace": "/"
                }
              ]
            }
          },
          {
            "name": "apicast.policy.echo"
          }
        ]
      }
    }
  ]
}
```

Signed-off-by: Eloy Coto <[email protected]>
Signed-off-by: Eloy Coto <[email protected]>
To be able to filter on routing policy based on the path that it's
currently set.

Signed-off-by: Eloy Coto <[email protected]>
To make the code simpler to read change the store_original_request
function call into the shared_build_context to make it clear what
happens.

Signed-off-by: Eloy Coto <[email protected]>
@davidor davidor merged commit c67b514 into 3scale:master Sep 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants