This is an automated email from the ASF dual-hosted git repository.

leslie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 704645f61 feat: support pass json to wasm plugin (#10072)
704645f61 is described below

commit 704645f6114ce6f2a4fe79857a8049e693f3878c
Author: Sn0rt <[email protected]>
AuthorDate: Mon Aug 28 15:25:49 2023 +0800

    feat: support pass json to wasm plugin (#10072)
---
 apisix/wasm.lua          |  29 ++++++--
 t/wasm/fault-injection.t |  98 +++++++++++++++++++++++++++
 t/wasm/request-body.t    |  49 ++++++++++++++
 t/wasm/route.t           | 173 +++++++++++++++++++++++++++++++++++++----------
 4 files changed, 306 insertions(+), 43 deletions(-)

diff --git a/apisix/wasm.lua b/apisix/wasm.lua
index a27641504..940f4e2ec 100644
--- a/apisix/wasm.lua
+++ b/apisix/wasm.lua
@@ -15,6 +15,8 @@
 -- limitations under the License.
 --
 local core = require("apisix.core")
+local nkeys = require("table.nkeys")
+local type = type
 local support_wasm, wasm = pcall(require, "resty.proxy-wasm")
 local ngx_var = ngx.var
 
@@ -22,10 +24,7 @@ local ngx_var = ngx.var
 local schema = {
     type = "object",
     properties = {
-        conf = {
-            type = "string",
-            minLength = 1,
-        },
+        conf = {},
     },
     required = {"conf"}
 }
@@ -33,7 +32,19 @@ local _M = {}
 
 
 local function check_schema(conf)
-    return core.schema.check(schema, conf)
+    if type(conf.conf) ~= "table" and type(conf.conf) ~= "string" then
+        return false, "invalid conf type"
+    end
+
+    if type(conf.conf) == "string" and conf.conf == "" then
+        return false, "empty conf"
+    end
+
+    if type(conf.conf) == "table" and nkeys(conf.conf) == 0 then
+        return false, "empty conf"
+    end
+
+    return true, ""
 end
 
 
@@ -51,7 +62,13 @@ local function fetch_plugin_ctx(conf, ctx, plugin)
     local plugin_ctx = ctxs[key]
     local err
     if not plugin_ctx then
-        plugin_ctx, err = wasm.on_configure(plugin, conf.conf)
+        if type(conf.conf) == "table" then
+            plugin_ctx, err = wasm.on_configure(plugin, 
core.json.encode(conf.conf))
+        elseif type(conf.conf) == "string" then
+            plugin_ctx, err = wasm.on_configure(plugin, conf.conf)
+        else
+            return nil, "invalid conf type"
+        end
         if not plugin_ctx then
             return nil, err
         end
diff --git a/t/wasm/fault-injection.t b/t/wasm/fault-injection.t
index e1cf2a43f..f690e9ea2 100644
--- a/t/wasm/fault-injection.t
+++ b/t/wasm/fault-injection.t
@@ -180,3 +180,101 @@ GET /hello
 --- error_code: 401
 --- response_body_like eval
 qr/<title>401 Authorization Required<\/title>/
+
+
+
+=== TEST 7: fault injection
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm_fault_injection": {
+                            "conf": {
+                                "http_status": 401,
+                                "body": "HIT\n"
+                            }
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 8: hit
+--- request
+GET /hello
+--- error_code: 401
+--- response_body
+HIT
+
+
+
+=== TEST 9: fault injection, with 0 percentage
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm_fault_injection": {
+                            "conf": {
+                                "http_status": 401,
+                                "percentage": 0
+                            }
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- ret_code: 401
+--- response_body
+passed
+
+
+
+=== TEST 10: hit
+--- request
+GET /hello
+--- response_body
+hello world
diff --git a/t/wasm/request-body.t b/t/wasm/request-body.t
index e45db39b5..83837684b 100644
--- a/t/wasm/request-body.t
+++ b/t/wasm/request-body.t
@@ -200,3 +200,52 @@ hello
 qr/request get body: \w+/
 --- grep_error_log_out
 request get body: ell
+
+
+
+=== TEST 8: invalid conf type no set conf
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm-request-body": {
+                            "setting": {"processReqBody":true, "start":1, 
"size":3}
+                        }
+                    }
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- error_code: 400
+--- response_body_like eval
+qr/invalid conf type/
+
+
+
+=== TEST 9: hit
+--- request
+POST /hello
+hello
+--- grep_error_log eval
+qr/request get body: \w+/
+--- grep_error_log_out
+request get body: ell
diff --git a/t/wasm/route.t b/t/wasm/route.t
index 6e949dd6b..a905b9c87 100644
--- a/t/wasm/route.t
+++ b/t/wasm/route.t
@@ -51,48 +51,147 @@ run_tests();
 
 __DATA__
 
-=== TEST 1: check schema
+=== TEST 1: scheme check with empty json body
 --- config
     location /t {
         content_by_lua_block {
-            local json = require("toolkit.json")
             local t = require("lib.test_admin").test
-            for _, case in ipairs({
-                {input = {
-                }},
-                {input = {
-                    conf = {}
-                }},
-                {input = {
-                    conf = ""
-                }},
-            }) do
-                local code, body = t('/apisix/admin/routes/1',
-                    ngx.HTTP_PUT,
-                    {
-                        id = "1",
-                        uri = "/echo",
-                        upstream = {
-                            type = "roundrobin",
-                            nodes = {}
-                        },
-                        plugins = {
-                            wasm_log = case.input
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
                         }
-                    }
-                )
-                ngx.say(json.decode(body).error_msg)
+                    },
+                    "plugins": {
+                        "wasm_log": {}
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
             end
+
+            ngx.say(body)
         }
     }
---- response_body
-failed to check the configuration of plugin wasm_log err: property "conf" is 
required
-failed to check the configuration of plugin wasm_log err: property "conf" 
validation failed: wrong type: expected string, got table
-failed to check the configuration of plugin wasm_log err: property "conf" 
validation failed: string too short, expected at least 1, got 0
+--- error_code: 400
+--- error_log eval
+qr/invalid request body/
+
+
+
+=== TEST 2: scheme check with conf type number
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm_log": {"conf": 123}
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- error_code: 400
+--- error_log eval
+qr/invalid request body/
+
+
+
+=== TEST 3: scheme check with conf json type
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm_log": {"conf": {}}}
+                }]]
+            )
 
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- error_code: 400
+--- response_body_like eval
+qr/empty conf/
 
 
-=== TEST 2: sanity
+
+=== TEST 4: scheme check with conf json type
+--- 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,
+                [[{
+                    "uri": "/hello",
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        }
+                    },
+                    "plugins": {
+                        "wasm_log": {"conf": ""}}
+                }]]
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+                return
+            end
+
+            ngx.say(body)
+        }
+    }
+--- error_code: 400
+--- response_body_like eval
+qr/empty conf/
+
+
+
+=== TEST 5: sanity
 --- config
     location /t {
         content_by_lua_block {
@@ -132,7 +231,7 @@ passed
 
 
 
-=== TEST 3: hit
+=== TEST 6: hit
 --- request
 GET /hello
 --- grep_error_log eval
@@ -143,7 +242,7 @@ run plugin ctx 1 with conf zzz in http ctx 2
 
 
 
-=== TEST 4: run wasm plugin in rewrite phase (prior to the one run in access 
phase)
+=== TEST 7: run wasm plugin in rewrite phase (prior to the one run in access 
phase)
 --- extra_yaml_config
 wasm:
     plugins:
@@ -164,7 +263,7 @@ run plugin ctx 1 with conf blahblah in http ctx 2
 
 
 
-=== TEST 5: plugin from service
+=== TEST 8: plugin from service
 --- config
     location /t {
         content_by_lua_block {
@@ -231,7 +330,7 @@ passed
 
 
 
-=== TEST 6: hit
+=== TEST 9: hit
 --- config
     location /t {
         content_by_lua_block {
@@ -262,7 +361,7 @@ run plugin ctx 3 with conf blahblah in http ctx 4
 
 
 
-=== TEST 7: plugin from plugin_config
+=== TEST 10: plugin from plugin_config
 --- config
     location /t {
         content_by_lua_block {
@@ -335,7 +434,7 @@ passed
 
 
 
-=== TEST 8: hit
+=== TEST 11: hit
 --- config
     location /t {
         content_by_lua_block {

Reply via email to