You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/07/13 09:16:37 UTC

[GitHub] [apisix] soulbird opened a new pull request, #7453: feat: supports dynamic control of whether plugins are running

soulbird opened a new pull request, #7453:
URL: https://github.com/apache/apisix/pull/7453

   ### Description
   
   Fixes #7144
   
   ### Checklist
   
   - [ ] I have explained the need for this PR and the problem it solves
   - [ ] I have explained the changes or the new features added to this PR
   - [ ] I have added tests corresponding to this change
   - [ ] I have updated the documentation to reflect this change
   - [ ] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
   
   <!--
   
   Note
   
   1. Mark the PR as draft until it's ready to be reviewed.
   2. Always add/update tests for any changes unless you have a good reason.
   3. Always update the documentation to reflect the changes made in the PR.
   4. Make a new commit to resolve conversations instead of `push -f`.
   5. To resolve merge conflicts, merge master instead of rebasing.
   6. Use "request review" to notify the reviewer after making changes.
   7. Only a reviewer can mark a conversation as resolved.
   
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r924322577


##########
apisix/schema_def.lua:
##########
@@ -956,6 +956,11 @@ _M.plugin_injected_schema = {
                 description = "priority of plugins by customized order",
                 type = "integer",
             },
+            filter = {
+                description = "filter determines whether the plugin "..
+                                "needs to be executed at runtime",
+                type  = "array",
+            }

Review Comment:
   From a prudential point of view, I would like to add, in order to gradually unify.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r921840059


##########
apisix/plugin.lua:
##########
@@ -371,6 +375,28 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
+local function meta_filter(plugin_name, plugin_conf)
+    local filter_vars = plugin_conf._meta and
+                plugin_conf._meta.filter and plugin_conf._meta.filter.vars
+    if not filter_vars then
+        return true
+    end
+
+    local ex, ok, err
+    ex, err = expr_lrucache(plugin_name, plugin_conf, expr.new, filter_vars)

Review Comment:
   However, we need one filter for each plugin configuration (route/x -> jwt-auth), not a particular plugin like `jwt-auth`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r923000428


##########
apisix/plugin.lua:
##########
@@ -375,14 +375,15 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
-local function meta_filter(plugin_name, plugin_conf)
+local function meta_filter(route, plugin_name, plugin_conf)

Review Comment:
   How about using `api_ctx.conf_id` and `plugin_name` as union key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r920710870


##########
t/plugin/plugin.t:
##########
@@ -284,7 +284,7 @@ GET /hello
                     error_response = "OK"
                 }},
             }) do
-                local code, body = t('/apisix/admin/global_rules/1',
+                local code, body = t('/apisix/admin/plugin_configs/1',

Review Comment:
   why do we change `global_rules` to `plugin_configs` here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r923000428


##########
apisix/plugin.lua:
##########
@@ -375,14 +375,15 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
-local function meta_filter(plugin_name, plugin_conf)
+local function meta_filter(route, plugin_name, plugin_conf)

Review Comment:
   How about using `api_ctx.conf_id` and `plugin_name` as union key.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r923293721


##########
apisix/schema_def.lua:
##########
@@ -956,6 +956,11 @@ _M.plugin_injected_schema = {
                 description = "priority of plugins by customized order",
                 type = "integer",
             },
+            filter = {
+                description = "filter determines whether the plugin "..
+                                "needs to be executed at runtime",
+                type  = "array",
+            }

Review Comment:
   It is just a convention within APISIX to create expr, which in general does not require many child elements.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r920714115


##########
t/plugin/plugin.t:
##########
@@ -284,7 +284,7 @@ GET /hello
                     error_response = "OK"
                 }},
             }) do
-                local code, body = t('/apisix/admin/global_rules/1',
+                local code, body = t('/apisix/admin/plugin_configs/1',

Review Comment:
   > why do we change `global_rules` to `plugin_configs` here?
   
   The global jwt plugin affects later test cases. Here is just a test schema, it is also possible to use ·`plugin_configs`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r921178860


##########
apisix/plugin.lua:
##########
@@ -371,6 +375,28 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
+local function meta_filter(plugin_name, plugin_conf)
+    local filter_vars = plugin_conf._meta and
+                plugin_conf._meta.filter and plugin_conf._meta.filter.vars
+    if not filter_vars then
+        return true
+    end
+
+    local ex, ok, err
+    ex, err = expr_lrucache(plugin_name, plugin_conf, expr.new, filter_vars)

Review Comment:
   What I plan is only one filter for a plugin.Is this reasonable?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] tzssangglass commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
tzssangglass commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r922371249


##########
apisix/schema_def.lua:
##########
@@ -956,6 +956,11 @@ _M.plugin_injected_schema = {
                 description = "priority of plugins by customized order",
                 type = "integer",
             },
+            filter = {
+                description = "filter determines whether the plugin "..
+                                "needs to be executed at runtime",
+                type  = "array",
+            }

Review Comment:
   ```suggestion
               filter = {
                   description = "filter determines whether the plugin "..
                                   "needs to be executed at runtime",
                   type  = "array",
                   maxItems = 20,
                   items = {
                       type = "array",
                   },
               }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander merged pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
spacewander merged PR #7453:
URL: https://github.com/apache/apisix/pull/7453


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r920807393


##########
apisix/plugin.lua:
##########
@@ -371,6 +375,28 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
+local function meta_filter(plugin_name, plugin_conf)
+    local filter_vars = plugin_conf._meta and
+                plugin_conf._meta.filter and plugin_conf._meta.filter.vars
+    if not filter_vars then
+        return true
+    end
+
+    local ex, ok, err
+    ex, err = expr_lrucache(plugin_name, plugin_conf, expr.new, filter_vars)

Review Comment:
   We can't use plugin_name as a key unless there is only one filter for a plugin.



##########
docs/en/latest/terminology/plugin.md:
##########
@@ -90,12 +90,29 @@ Some common configurations can be applied to the plugin configuration. For examp
 
 the configuration above means customizing the error response from the jwt-auth plugin to '{"message": "Missing credential in request"}'.
 
+```
+{
+    "jwt-auth": {
+        "_meta": {
+            "filter": {

Review Comment:
   Why don't we put the expression under filter directly?



##########
t/plugin/plugin.t:
##########
@@ -306,3 +306,139 @@ GET /hello
 {"error_msg":"failed to check the configuration of plugin jwt-auth err: property \"_meta\" validation failed: wrong type: expected object, got boolean"}
 {"error_msg":"failed to check the configuration of plugin jwt-auth err: property \"_meta\" validation failed: property \"error_response\" validation failed: value should match only one schema, but matches none"}
 passed
+
+
+
+=== TEST 10: invalid _meta filter vars schema with wrong type
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/plugin_configs/1',
+                ngx.HTTP_PUT,
+                {
+                    plugins = {
+                        ["jwt-auth"] = {
+                            _meta = {
+                                filter = {
+                                    vars = "arg_k == v"
+                                }
+                            }
+                        }
+                    }
+                }
+            )
+            if code >= 300 then
+                ngx.print(body)
+            else
+                ngx.say(body)
+            end
+        }
+    }
+--- response_body
+{"error_msg":"failed to check the configuration of plugin jwt-auth err: property \"_meta\" validation failed: property \"filter\" validation failed: property \"vars\" validation failed: wrong type: expected array, got string"}
+
+
+
+=== TEST 11: invalid _meta filter schema with wrong expr
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            for _, vars in ipairs({
+                {"arg_name", "==", "json"},
+                {
+                    {"arg_name", "*=", "json"}
+                }
+            }) do
+                local code, body = t('/apisix/admin/plugin_configs/1',
+                    ngx.HTTP_PUT,
+                    {
+                        plugins = {
+                            ["jwt-auth"] = {
+                                _meta = {
+                                    filter = {
+                                        vars = vars
+                                    }
+                                }
+                            }
+                        }
+                    }
+                )
+                if code >= 300 then
+                    ngx.print(body)
+                else
+                    ngx.say(body)
+                end
+            end
+        }
+    }
+--- response_body
+{"error_msg":"failed to validate the 'vars' expression: rule should be wrapped inside brackets"}
+{"error_msg":"failed to validate the 'vars' expression: invalid operator '*='"}
+
+
+
+=== TEST 12: proxy-rewrite plugin run with _meta filter vars
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                {
+                    plugins = {
+                        ["proxy-rewrite"] = {
+                            _meta = {
+                                filter = {
+                                    vars = {
+                                        {"arg_version", "==", "v2"}
+                                    }
+                                }
+                            },
+                            uri = "/echo",
+                            headers = {
+                                ["X-Api-Version"] = "v2"
+                            }
+                        }
+                    },
+                    upstream = {
+                        nodes = {
+                            ["127.0.0.1:1980"] = 1
+                        },
+                        type = "roundrobin"
+                    },
+                    uri = "/hello"
+                }
+            )
+            if code >= 300 then
+                ngx.print(body)
+            else
+                ngx.say(body)
+            end
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 13: hit route: run proxy-rewrite plugin
+--- request
+GET /hello?version=v2
+--- response_headers
+x-api-version: v2
+--- no_error_log

Review Comment:
   It is already set: https://github.com/apache/apisix/blob/ea6b55db7d50133c6cf6adb32539223c45cb0d38/t/plugin/plugin.t#L22



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r924070184


##########
apisix/schema_def.lua:
##########
@@ -956,6 +956,11 @@ _M.plugin_injected_schema = {
                 description = "priority of plugins by customized order",
                 type = "integer",
             },
+            filter = {
+                description = "filter determines whether the plugin "..
+                                "needs to be executed at runtime",
+                type  = "array",
+            }

Review Comment:
   I think this can be omitted first, it has no effect on apisix, and users can choose by themselves. What do you think?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r922825255


##########
apisix/plugin.lua:
##########
@@ -375,14 +375,15 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
-local function meta_filter(plugin_name, plugin_conf)
+local function meta_filter(route, plugin_name, plugin_conf)

Review Comment:
   The `route` actually can be `route` or `global_rule`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] soulbird commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
soulbird commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r922992325


##########
apisix/schema_def.lua:
##########
@@ -956,6 +956,11 @@ _M.plugin_injected_schema = {
                 description = "priority of plugins by customized order",
                 type = "integer",
             },
+            filter = {
+                description = "filter determines whether the plugin "..
+                                "needs to be executed at runtime",
+                type  = "array",
+            }

Review Comment:
   What is the basis for this (`maxItems = 20`) restriction? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [apisix] spacewander commented on a diff in pull request #7453: feat: supports dynamic control of whether plugins are running

Posted by GitBox <gi...@apache.org>.
spacewander commented on code in PR #7453:
URL: https://github.com/apache/apisix/pull/7453#discussion_r923426539


##########
apisix/plugin.lua:
##########
@@ -371,6 +375,33 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
+local function meta_filter(ctx, plugin_name, plugin_conf)
+    local filter = plugin_conf._meta and plugin_conf._meta.filter
+    if not filter then
+        return true
+    end
+
+    local ex, ok, err
+    if ctx then
+        local key = core.table.concat({plugin_name, ctx.conf_type,

Review Comment:
   Use `..` could be faster, without creating new table



##########
apisix/plugin.lua:
##########
@@ -371,6 +375,33 @@ local function trace_plugins_info_for_debug(ctx, plugins)
     end
 end
 
+local function meta_filter(ctx, plugin_name, plugin_conf)
+    local filter = plugin_conf._meta and plugin_conf._meta.filter
+    if not filter then
+        return true
+    end
+
+    local ex, ok, err
+    if ctx then
+        local key = core.table.concat({plugin_name, ctx.conf_type,
+                                 ctx.conf_id}, "")
+        ex, err = expr_lrucache(key, ctx.conf_version, expr.new, filter)
+    else
+        ex, err = expr.new(filter)
+    end
+    if not ex then
+        core.log.warn("failed to get the 'vars' expression: ",
+                 err or "", " plugin_name: ", plugin_name)
+        return true
+    end
+    ok, err = ex:eval()
+    if err then
+        core.log.warn("failed to run the 'vars' expression: ",
+                 err or "", " plugin_name: ", plugin_name)

Review Comment:
   ```suggestion
                    err, " plugin_name: ", plugin_name)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org