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 2021/11/25 16:18:10 UTC

[GitHub] [apisix] bisakhmondal opened a new pull request #5616: feat: faas plugin refactoring with url path forwarding

bisakhmondal opened a new pull request #5616:
URL: https://github.com/apache/apisix/pull/5616


   ### 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. -->
   
   This PR is based on the idea of creating a single generic upstream where each faas plugin implements an authorization schema and perform the header processing.
   
   GOAL: Reduce the same code over multiple plugins (mainly faas)
   
   **How developers should write new (faas) plugins:**
   1. They should define plugin specific authorization schema & metadata schema(if required)
   2. Write a `header_processor` function on the plugin lua file that take `plugin conf, ngx context and the URL params (that contains headers, body, urlpath, query params etc. etc.)". The function can perform a series of operation including update/add the necessary authorization headers, perform encoding/decoding (base64 or anything) on the body.
   3. return just updated header table back from the function (header table might be blank [highly unlikely] so local changes must reflect on the HTTP request).
   4. And yes, that's it. (see the azure-functions lua code for example)
   
   ### Pre-submission checklist:
   
   <!--
   Please follow the requirements:
   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. Use "request review" to notify the reviewer once you have resolved the review
   -->
   
   * [x] Did you explain what problem does this PR solve? Or what new features have been added?
   * [x] Have you added corresponding test cases?
   * [ ] 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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then

Review comment:
       Agree. I was using `**` that's why the extra arg was received through curr_req_matched["*"]. Updated




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then
+            local end_path = ctx.curr_req_matched["*"]
+
+            if path:sub(-1, -1) == "/" or end_path:sub(1, 1) == "/" then

Review comment:
       Using `:byte` would be better.
   BTW, it is not a good idea to refactor & add feature in the same PR. 




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +393,100 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/*"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            ngx.say(body)
+
+            local code, _, body = t("/azure/httptrigger", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+            ngx.print(body)
+        }
+    }
+--- response_body
+passed
+invocation /api/httptrigger successful
+
+
+
+=== TEST 11: check multilevel url path forwarding

Review comment:
       Next time, we can use a table drive test for those cases. See https://github.com/apache/apisix/blob/7242216076f1df4225ab5d2d742ed7fb23383306/t/plugin/gzip.t#L498




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then
+            local end_path = ctx.curr_req_matched["*"]
+
+            if path:sub(-1, -1) == "/" or end_path:sub(1, 1) == "/" then

Review comment:
       Like `if path:byte(-1) == string.byte("/") or end_path:byte(1) == string.byte("/") then`.
   
   We can accept this one now.
   Next time, please don't mix up refactor & new feature.




-- 
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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"

Review comment:
       Done

##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            ngx.say(body)
+
+            local code, _, body = t("/azure/httptrigger", "GET")

Review comment:
       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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then

Review comment:
       Agree. I was using `**` that's why the extra arg was received through curr_req_matched. Updated




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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


   


-- 
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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then
+            local end_path = ctx.curr_req_matched["*"]
+
+            if path:sub(-1, -1) == "/" or end_path:sub(1, 1) == "/" then

Review comment:
       for byte you mean like this 
   `if path:byte(-1, -1) == string.byte"/" or end_path:byte(1, 1) == string.byte"/" then`
   
   The PRs are interdependent and require one PR to be merged before another. Do you think I should split it up into 2 PRs?




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            ngx.say(body)
+
+            local code, _, body = t("/azure/httptrigger", "GET")

Review comment:
       Let's add another test which contains multiple levels of paths, like `/azure/http/trigger`.




-- 
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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then
+            local end_path = ctx.curr_req_matched["*"]
+
+            if path:sub(-1, -1) == "/" or end_path:sub(1, 1) == "/" then

Review comment:
       Cool! Thanks boss




-- 
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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"
+                }]]
+            )
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            ngx.say(body)
+
+            local code, _, body = t("/azure/httptrigger", "GET")

Review comment:
       As discussed offline the following cases have been covered
   multiple levels of paths
   multiple slashes in the paths
   the wildcard matches an empty part, like /azure




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"

Review comment:
       ```suggestion
                           "uri": "/azure/*"
   ```
   
   The wildcard match only uses one `*`.




-- 
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 #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: t/plugin/azure-functions.t
##########
@@ -375,3 +381,43 @@ ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
 passed
 passed
 Authz-Header - metadata_key
+
+
+
+=== TEST 10: check if url path being forwarded correctly by creating a semi correct path uri
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            -- creating a semi path route
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": "http://localhost:8765/api"
+                            }
+                        },
+                        "uri": "/azure/**"

Review comment:
       ```suggestion
                           "uri": "/azure/*"
   ```

##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then

Review comment:
       We should use `:ext` instead of `*`




-- 
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] bisakhmondal commented on a change in pull request #5616: feat: faas plugin refactoring with url path forwarding

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



##########
File path: apisix/plugins/serverless/generic-upstream.lua
##########
@@ -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.
+
+local ngx  = ngx
+local require = require
+local type = type
+
+return function(plugin_name, version, priority, request_processor, authz_schema, metadata_schema)
+    local core = require("apisix.core")
+    local http = require("resty.http")
+    local url = require("net.url")
+
+    if request_processor and type(request_processor) ~= "function" then
+        return "Failed to generate plugin due to invalid header processor type, " ..
+                    "expected: function, received: " .. type(request_processor)
+    end
+
+    local schema = {
+        type = "object",
+        properties = {
+            function_uri = {type = "string"},
+            authorization = authz_schema,
+            timeout = {type = "integer", minimum = 100, default = 3000},
+            ssl_verify = {type = "boolean", default = true},
+            keepalive = {type = "boolean", default = true},
+            keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
+            keepalive_pool = {type = "integer", minimum = 1, default = 5}
+        },
+        required = {"function_uri"}
+    }
+
+    local _M = {
+        version = version,
+        priority = priority,
+        name = plugin_name,
+        schema = schema,
+        metadata_schema = metadata_schema
+    }
+
+    function _M.check_schema(conf, schema_type)
+        if schema_type == core.schema.TYPE_METADATA then
+            return core.schema.check(metadata_schema, conf)
+        end
+        return core.schema.check(schema, conf)
+    end
+
+    function _M.access(conf, ctx)
+        local uri_args = core.request.get_uri_args(ctx)
+        local headers = core.request.headers(ctx) or {}
+
+        local req_body, err = core.request.get_body()
+
+        if err then
+            core.log.error("error while reading request body: ", err)
+            return 400
+        end
+
+        -- forward the url path came through the matched uri
+        local url_decoded = url.parse(conf.function_uri)
+        local path = url_decoded.path or "/"
+
+        if ctx.curr_req_matched and ctx.curr_req_matched["*"] then
+            local end_path = ctx.curr_req_matched["*"]
+
+            if path:sub(-1, -1) == "/" or end_path:sub(1, 1) == "/" then

Review comment:
       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