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/01/19 04:16:54 UTC

[GitHub] [apisix] bzp2010 opened a new pull request #6145: feat: add public api plugin

bzp2010 opened a new pull request #6145:
URL: https://github.com/apache/apisix/pull/6145


   ### What this PR does / why we need it:
   <!--- Why is this change required? What problem does it solve? -->
   <!--- If it fixes an open issue, please link to the issue here. -->
   
   Implement: #6137 
   
   ### Pre-submission checklist:
   
   <!--
   Please follow the PR manners:
   1. Use Draft if the PR is not ready to be reviewed
   2. Test is required for the feat/fix PR, unless you have a good reason
   3. Doc is required for the feat PR
   4. Use a new commit to resolve review instead of `push -f`
   5. If you need to resolve merge conflicts after the PR is reviewed, please merge master but do not rebase
   6. Use "request review" to notify the reviewer once you have resolved the review
   7. Only reviewer can click "Resolve conversation" to mark the reviewer's review resolved
   -->
   
   * [x] Did you explain what problem does this PR solve? Or what new features have been added?
   * [x] Have you added corresponding test cases?
   * [x] Have you modified the corresponding document?
   * [x] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
   


-- 
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 change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789428610



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,219 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            local code, body, jwt = t("/gen_token?key=user-key", ngx.HTTP_GET, "", nil, {apikey = "testkey"})
+            if code >= 300 then
+                ngx.status = code
+            end
+
+            local header = string.sub(jwt, 1, 36)
+
+            if header == "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" or
+               header == "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" then
+                ngx.say("passed")
+                return
+            end
+
+            ngx.say("failed")
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: hit route (direct-wolf-rbac-userinfo)
+--- request
+GET /apisix/plugin/wolf-rbac/user_info
+--- error_code: 401
+--- error_log
+direct-wolf-rbac-userinfo was triggered
+
+
+
+=== TEST 5: missing route
+--- request
+GET /apisix/plugin/balalbala
+--- error_code: 404

Review comment:
       Let's add more test that the uri in public API plugin matches nothing.




-- 
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] shuaijinchao commented on pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
shuaijinchao commented on pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#issuecomment-1018400176


   I don't see the problem in the code and test cases, but I don't see the documentation for the plugin.


-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r790282220



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       ## Update
   After checking again, I think there may not be a duplicate calls problem.
   
   ## Details
   ![image](https://user-images.githubusercontent.com/8078418/150679359-bce58cbb-fad4-4f0a-9418-b23df3fb921b.png)
   
   There are **4** calls to `run_global_rules` executed in the current code. One of them is in `api_router`, which is controlled by the configuration file (this is the router used by the public API), and the remaining ones are executed in the `http_access_phase` of `init`.
   
   1. both HTTP and public API do not match, will run and return a 404 status code
   2. run after HTTP matched
   3. run in `common_phase` for `before_proxy`
   
   In fact I didn't add `global_rules` to init.lua, they are still being executed according to the original logic.
   
   If I have missed anything, please help point it out. Thanks.
   
   ping @spacewander 




-- 
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 #6145: feat: add public api plugin

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


   


-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789309852



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       https://github.com/apache/apisix/blob/64c47df50c5607e9bf9921021391bb32c5f4d0b2/apisix/init.lua#L376-L445 
   
   Hi, @spacewander.
   
   Based on this part of the code, we can presume to have such a situation.
   
   1. HTTP match, and `public-api` plugin is enabled.
   This will be controlled by the configuration file whether to enforce global rules for public API calls. After the match, the subsequent request processing flow will be completely taken over by the public API handler, bypassing the rest of the code execution in the access phase.
   2. HTTP not matched, public API route matched.
   This will also be controlled by the configuration file whether to enforce global rules or not. 
   3. Both HTTP and public APIs do not match
   It will force the global rule to run and return 404 Not Route.
   
   ----------
   
   Also, as you said in this issue https://github.com/apache/apisix/issues/6137#issuecomment-1016016565, we will adjust the public API to not be exposed by default, which will be done by completely removing the public API route match in the access phase. (Currently HTTP matching is performed followed by public API matching as a backup) In that case, the matching and processing of the public API will be done by the public-api plugin only, and if a match is made the global rules will be enforced according to the configuration.




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789431479



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,219 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            local code, body, jwt = t("/gen_token?key=user-key", ngx.HTTP_GET, "", nil, {apikey = "testkey"})
+            if code >= 300 then
+                ngx.status = code
+            end
+
+            local header = string.sub(jwt, 1, 36)
+
+            if header == "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" or
+               header == "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" then
+                ngx.say("passed")
+                return
+            end
+
+            ngx.say("failed")
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: hit route (direct-wolf-rbac-userinfo)
+--- request
+GET /apisix/plugin/wolf-rbac/user_info
+--- error_code: 401
+--- error_log
+direct-wolf-rbac-userinfo was triggered
+
+
+
+=== TEST 5: missing route
+--- request
+GET /apisix/plugin/balalbala
+--- error_code: 404

Review comment:
       got it, adding




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789294464



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri

Review comment:
       fixed




-- 
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] bzp2010 commented on pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#issuecomment-1018573650


   > I don't see the problem in the code and test cases, but I don't see the documentation for the plugin.
   
   @shuaijinchao 
   
   According this comment https://github.com/apache/apisix/pull/6145#discussion_r789309852, there is a next phase of development work after this PR is completed, which will be added when everything is done.


-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r790282220



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       ## Update
   After checking again, I think there may not be a duplicate calls problem.
   
   ## Details
   ![image](https://user-images.githubusercontent.com/8078418/150679359-bce58cbb-fad4-4f0a-9418-b23df3fb921b.png)
   
   There are **4** calls to `run_global_rules` executed in the current code. One of them is in `api_router`, which is controlled by the configuration file (this is the router used by the public API), and the remaining ones are executed in the `http_access_phase` of `init`.
   
   1. both HTTP and public API do not match, will run and return a 404 status code, stop progress
   2. run after HTTP matched
   3. run in `common_phase` for `before_proxy`
   
   In fact I didn't add `global_rules` to init.lua, they are still being executed according to the original logic.
   
   If I have missed anything, please help point it out. Thanks.
   
   ping @spacewander 




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789717240



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       I will try to fix it.




-- 
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 change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789287724



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,134 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- request
+GET /gen_token?key=user-key

Review comment:
       Let's add assertion to make sure the jwt sign is working

##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       We don't need to run global rules twice?

##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri

Review comment:
       ```suggestion
       ctx.var.uri = conf.uri or ctx.var.uri
   ```

##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,134 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- request
+GET /gen_token?key=user-key
+--- error_log
+direct-wolf-rbac-userinfo was triggered
+
+
+
+=== TEST 4: hit route (direct-wolf-rbac-userinfo)
+--- request
+GET /apisix/plugin/wolf-rbac/user_info
+--- error_code: 401
+--- error_log
+custom-jwt-sign was triggered

Review comment:
       Let's add more cases:
   1. public api not match
   2. match without optional uri
   3. add auth to protect public api




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789469685



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,219 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            local code, body, jwt = t("/gen_token?key=user-key", ngx.HTTP_GET, "", nil, {apikey = "testkey"})
+            if code >= 300 then
+                ngx.status = code
+            end
+
+            local header = string.sub(jwt, 1, 36)
+
+            if header == "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" or
+               header == "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" then
+                ngx.say("passed")
+                return
+            end
+
+            ngx.say("failed")
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: hit route (direct-wolf-rbac-userinfo)
+--- request
+GET /apisix/plugin/wolf-rbac/user_info
+--- error_code: 401
+--- error_log
+direct-wolf-rbac-userinfo was triggered
+
+
+
+=== TEST 5: missing route
+--- request
+GET /apisix/plugin/balalbala
+--- error_code: 404

Review comment:
       added




-- 
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 change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789427352



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       > In that case, the matching and processing of the public API will be done by the public-api plugin only, and if a match is made the global rules will be enforced according to the configuration.
   
   We can adjust it when we retire the router? The current implementation will run global rules twice in some cases.




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r790282220



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       ## Update
   After checking again, I think there may not be a duplicate calls problem.
   
   ## Details
   ![image](https://user-images.githubusercontent.com/8078418/150679359-bce58cbb-fad4-4f0a-9418-b23df3fb921b.png)
   
   There are **4** calls to `run_global_rules` executed in the current code. One of them is in `api_router`, which is controlled by the configuration file (this is the router used by the public API), and the remaining ones are executed in the `http_access_phase` of `init`.
   
   1. both HTTP and public API do not match, will run and return a 404 status code, stop progress
   ![image](https://user-images.githubusercontent.com/8078418/150683062-2ae8396b-f356-4911-bd06-16e5be8bcb46.png)
   
   2. run after HTTP matched
   ![image](https://user-images.githubusercontent.com/8078418/150684151-1d259db2-a0ea-4a6b-86e5-091bf0e78194.png)
   
   3. run in `common_phase` for `before_proxy`
   
   In fact I didn't add `global_rules` to init.lua, they are still being executed according to the original logic.
   
   If I have missed anything, please help point it out. Thanks.
   
   ping @spacewander 




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r790282220



##########
File path: apisix/plugins/public-api.lua
##########
@@ -0,0 +1,58 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core   = require("apisix.core")
+local router = require("apisix.router")
+
+local schema = {
+    type = "object",
+    properties = {
+        uri = {type = "string"},
+    },
+}
+
+
+local _M = {
+    version = 0.1,
+    priority = 501,
+    name = "public-api",
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+function _M.access(conf, ctx)
+    local local_conf = core.config.local_conf()
+
+    -- overwrite the uri in the ctx when the user has set the target uri
+    ctx.var.uri = conf.uri and conf.uri or ctx.var.uri
+    local skip = local_conf and local_conf.apisix.global_rule_skip_internal_api

Review comment:
       ## Update
   After checking again, I think there may not be a duplicate calls problem.
   
   ## Details
   ![image](https://user-images.githubusercontent.com/8078418/150679359-bce58cbb-fad4-4f0a-9418-b23df3fb921b.png)
   
   There are **4** calls to `run_global_rules` executed in the current code. One of them is in `api_router`, which is controlled by the configuration file (this is the router used by the public API), and the remaining ones are executed in the `http_access_phase` of `init`.
   
   1. both HTTP and public API do not match, will run and return a 404 status code
   2. run after HTTP matched
   3. run in `common_phase` for `before_proxy`
   
   In fact I didn't add `global_rules` to init.lua, they are still being executed according to the original logic.
   
   If I have missed anything, please help point it out. Thanks.




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789317556



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,134 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- request
+GET /gen_token?key=user-key

Review comment:
       added, It will check the first part of the JWT in the response body, however, because json encode cannot guarantee the order of the data in it, it matches both cases.




-- 
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] bzp2010 commented on a change in pull request #6145: feat: add public api plugin

Posted by GitBox <gi...@apache.org>.
bzp2010 commented on a change in pull request #6145:
URL: https://github.com/apache/apisix/pull/6145#discussion_r789360317



##########
File path: t/plugin/public-api.t
##########
@@ -0,0 +1,134 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
+        $block->set_value("no_error_log", "[error]");
+    }
+
+    if (!defined $block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local test_cases = {
+                {uri = "/apisix/plugin/jwt/sign"},
+                {uri = 3233}
+            }
+            local plugin = require("apisix.plugins.public-api")
+
+            for _, case in ipairs(test_cases) do
+                local ok, err = plugin.check_schema(case)
+                ngx.say(ok and "done" or err)
+            end
+        }
+    }
+--- response_body
+done
+property "uri" validation failed: wrong type: expected string, got number
+
+
+
+=== TEST 2: set route
+--- config
+    location /t {
+        content_by_lua_block {
+            local datas = {
+                {
+                    uri = "/apisix/admin/consumers",
+                    data = [[{
+                        "username": "alice",
+                        "plugins": {
+                            "jwt-auth": {
+                                "key": "user-key",
+                                "algorithm": "HS256"
+                            }
+                        }
+                    }]]
+                },
+                {
+                    uri = "/apisix/admin/routes/custom-jwt-sign",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {
+                                "uri": "/apisix/plugin/jwt/sign"
+                            },
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"custom-jwt-sign was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/gen_token"
+                    }]],
+                },
+                {
+                    uri = "/apisix/admin/routes/direct-wolf-rbac-userinfo",
+                    data = [[{
+                        "plugins": {
+                            "public-api": {},
+                            "serverless-pre-function": {
+                                "phase": "rewrite",
+                                "functions": ["return function(conf, ctx) require(\"apisix.core\").log.warn(\"direct-wolf-rbac-userinfo was triggered\"); end"]
+                            }
+                        },
+                        "uri": "/apisix/plugin/wolf-rbac/user_info"
+                    }]],
+                },
+            }
+
+            local t = require("lib.test_admin").test
+
+            for _, data in ipairs(datas) do
+                local code, body = t(data.uri, ngx.HTTP_PUT, data.data)
+                ngx.say(code..body)
+            end
+        }
+    }
+--- response_body eval
+"201passed\n" x 3
+
+
+
+=== TEST 3: hit route (custom-jwt-sign)
+--- request
+GET /gen_token?key=user-key
+--- error_log
+direct-wolf-rbac-userinfo was triggered
+
+
+
+=== TEST 4: hit route (direct-wolf-rbac-userinfo)
+--- request
+GET /apisix/plugin/wolf-rbac/user_info
+--- error_code: 401
+--- error_log
+custom-jwt-sign was triggered

Review comment:
       1. added
   2. I didn't understand what you meant, is it a case where there is no uri configuration in the schema? If so it is there now.
   3. added, protect jwt sign by `key-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