spacewander commented on a change in pull request #5479:
URL: https://github.com/apache/apisix/pull/5479#discussion_r751805107



##########
File path: apisix/plugins/azure-functions.lua
##########
@@ -0,0 +1,137 @@
+--
+-- 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 http = require("resty.http")
+local plugin = require("apisix.plugin")
+local ngx  = ngx
+local plugin_name = "azure-functions"
+
+local schema = {
+    type = "object",
+    properties = {
+        function_uri = {type = "string"},
+        authorization = {
+            type = "object",
+            properties = {
+                apikey = {type = "string"},
+                clientid = {type = "string"}
+            }
+        },
+        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 metadata_schema = {
+    type = "object",
+    properties = {
+        master_apikey = {type = "string", default = ""},
+        master_clientid = {type = "string", default = ""}
+    }
+}
+
+local _M = {
+    version = 0.1,
+    priority = -1900,
+    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
+
+    -- set authorization headers if not already set by the client
+    -- we are following not to overwrite the authz keys
+    if not headers["x-functions-key"] and
+            not headers["x-functions-clientid"] then
+        if conf.authorization then
+            headers["x-functions-key"] = conf.authorization.apikey
+            headers["x-functions-clientid"] = conf.authorization.clientid
+        else
+            -- If neither api keys are set with the client request nor inside 
the plugin attributes
+            -- plugin will fallback to the master key (if any) present inside 
the metadata.
+            local metadata = plugin.plugin_metadata(plugin_name)
+            if metadata then
+                headers["x-functions-key"] = metadata.value.master_apikey
+                headers["x-functions-clientid"] = 
metadata.value.master_clientid
+            end
+        end
+    end
+
+    headers["Host"],  headers["host"] = nil, nil

Review comment:
       ```suggestion
       headers["host"] = nil
   ```
   is enough.
   
   The fetched header is in lowercase.

##########
File path: t/plugin/azure-functions.t
##########
@@ -0,0 +1,372 @@
+#
+# 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();
+no_shuffle();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    my $inside_lua_block = $block->inside_lua_block // "";
+    chomp($inside_lua_block);
+    my $http_config = $block->http_config // <<_EOC_;
+
+    server {
+        listen 8765;
+
+        location /httptrigger {
+            content_by_lua_block {
+                ngx.req.read_body()
+                local msg = "faas invoked"
+                ngx.header['Content-Length'] = #msg + 1
+                ngx.header['X-Extra-Header'] = "MUST"
+                ngx.header['Connection'] = "Keep-Alive"
+                ngx.say(msg)
+            }
+        }
+
+        location /azure-demo {
+            content_by_lua_block {
+                $inside_lua_block
+            }
+        }
+    }
+_EOC_
+
+    $block->set_value("http_config", $http_config);
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+    if (!$block->no_error_log && !$block->error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.azure-functions")
+            local conf = {
+                function_uri = "http://some-url.com";
+            }
+            local ok, err = plugin.check_schema(conf)
+            if not ok then
+                ngx.say(err)
+            end
+            ngx.say("done")
+        }
+    }
+--- response_body
+done
+
+
+
+=== TEST 2: function_uri missing
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.azure-functions")
+            local ok, err = plugin.check_schema({})
+            if not ok then
+                ngx.say(err)
+            else
+                ngx.say("done")
+            end
+        }
+    }
+--- response_body
+property "function_uri" is required
+
+
+
+=== TEST 3: create route with azure-function plugin enabled
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "plugins": {
+                            "azure-functions": {
+                                "function_uri": 
"http://localhost:8765/httptrigger";
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/azure"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "plugins": {
+                                "azure-functions": {
+                                    "keepalive": true,
+                                    "timeout": 3000,
+                                    "ssl_verify": true,
+                                    "keepalive_timeout": 60000,
+                                    "keepalive_pool": 5,
+                                    "function_uri": 
"http://localhost:8765/httptrigger";
+                                }
+                            },
+                            "upstream": {
+                                "nodes": {
+                                    "127.0.0.1:1982": 1
+                                },
+                                "type": "roundrobin"
+                            },
+                            "uri": "/azure"
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say("fail")
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 4: Test plugin endpoint
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local core = require("apisix.core")
+
+            local code, _, body, headers = t("/azure", "GET")
+             if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            -- headers proxied 2 times -- one by plugin, another by this test 
case
+            core.response.set_header(headers)
+            ngx.print(body)
+        }
+    }
+--- response_body
+faas invoked
+--- response_headers
+Content-Length: 13
+X-Extra-Header: MUST
+
+
+
+=== TEST 5: http2 check response body and headers
+--- http2
+--- exec
+curl -XGET --http2 --http2-prior-knowledge localhost:1984/azure

Review comment:
       We can use regular `--- request` to send http2 request, see
   
https://github.com/openresty/test-nginx/blob/9d5a20f884d67974f9eafeed5a1540cd32945fb2/lib/Test/Nginx/Socket.pm#L2310




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to