Skip to content

Commit 704645f

Browse files
authored
feat: support pass json to wasm plugin (#10072)
1 parent 89c1858 commit 704645f

File tree

4 files changed

+306
-43
lines changed

4 files changed

+306
-43
lines changed

apisix/wasm.lua

+23-6
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,36 @@
1515
-- limitations under the License.
1616
--
1717
local core = require("apisix.core")
18+
local nkeys = require("table.nkeys")
19+
local type = type
1820
local support_wasm, wasm = pcall(require, "resty.proxy-wasm")
1921
local ngx_var = ngx.var
2022

2123

2224
local schema = {
2325
type = "object",
2426
properties = {
25-
conf = {
26-
type = "string",
27-
minLength = 1,
28-
},
27+
conf = {},
2928
},
3029
required = {"conf"}
3130
}
3231
local _M = {}
3332

3433

3534
local function check_schema(conf)
36-
return core.schema.check(schema, conf)
35+
if type(conf.conf) ~= "table" and type(conf.conf) ~= "string" then
36+
return false, "invalid conf type"
37+
end
38+
39+
if type(conf.conf) == "string" and conf.conf == "" then
40+
return false, "empty conf"
41+
end
42+
43+
if type(conf.conf) == "table" and nkeys(conf.conf) == 0 then
44+
return false, "empty conf"
45+
end
46+
47+
return true, ""
3748
end
3849

3950

@@ -51,7 +62,13 @@ local function fetch_plugin_ctx(conf, ctx, plugin)
5162
local plugin_ctx = ctxs[key]
5263
local err
5364
if not plugin_ctx then
54-
plugin_ctx, err = wasm.on_configure(plugin, conf.conf)
65+
if type(conf.conf) == "table" then
66+
plugin_ctx, err = wasm.on_configure(plugin, core.json.encode(conf.conf))
67+
elseif type(conf.conf) == "string" then
68+
plugin_ctx, err = wasm.on_configure(plugin, conf.conf)
69+
else
70+
return nil, "invalid conf type"
71+
end
5572
if not plugin_ctx then
5673
return nil, err
5774
end

t/wasm/fault-injection.t

+98
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,101 @@ GET /hello
180180
--- error_code: 401
181181
--- response_body_like eval
182182
qr/<title>401 Authorization Required<\/title>/
183+
184+
185+
186+
=== TEST 7: fault injection
187+
--- config
188+
location /t {
189+
content_by_lua_block {
190+
local t = require("lib.test_admin").test
191+
local code, body = t('/apisix/admin/routes/1',
192+
ngx.HTTP_PUT,
193+
[[{
194+
"uri": "/hello",
195+
"upstream": {
196+
"type": "roundrobin",
197+
"nodes": {
198+
"127.0.0.1:1980": 1
199+
}
200+
},
201+
"plugins": {
202+
"wasm_fault_injection": {
203+
"conf": {
204+
"http_status": 401,
205+
"body": "HIT\n"
206+
}
207+
}
208+
}
209+
}]]
210+
)
211+
212+
if code >= 300 then
213+
ngx.status = code
214+
ngx.say(body)
215+
return
216+
end
217+
218+
ngx.say(body)
219+
}
220+
}
221+
--- response_body
222+
passed
223+
224+
225+
226+
=== TEST 8: hit
227+
--- request
228+
GET /hello
229+
--- error_code: 401
230+
--- response_body
231+
HIT
232+
233+
234+
235+
=== TEST 9: fault injection, with 0 percentage
236+
--- config
237+
location /t {
238+
content_by_lua_block {
239+
local t = require("lib.test_admin").test
240+
local code, body = t('/apisix/admin/routes/1',
241+
ngx.HTTP_PUT,
242+
[[{
243+
"uri": "/hello",
244+
"upstream": {
245+
"type": "roundrobin",
246+
"nodes": {
247+
"127.0.0.1:1980": 1
248+
}
249+
},
250+
"plugins": {
251+
"wasm_fault_injection": {
252+
"conf": {
253+
"http_status": 401,
254+
"percentage": 0
255+
}
256+
}
257+
}
258+
}]]
259+
)
260+
261+
if code >= 300 then
262+
ngx.status = code
263+
ngx.say(body)
264+
return
265+
end
266+
267+
ngx.say(body)
268+
}
269+
}
270+
--- ret_code: 401
271+
--- response_body
272+
passed
273+
274+
275+
276+
=== TEST 10: hit
277+
--- request
278+
GET /hello
279+
--- response_body
280+
hello world

t/wasm/request-body.t

+49
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,52 @@ hello
200200
qr/request get body: \w+/
201201
--- grep_error_log_out
202202
request get body: ell
203+
204+
205+
206+
=== TEST 8: invalid conf type no set conf
207+
--- config
208+
location /t {
209+
content_by_lua_block {
210+
local t = require("lib.test_admin").test
211+
local code, body = t('/apisix/admin/routes/1',
212+
ngx.HTTP_PUT,
213+
[[{
214+
"uri": "/hello",
215+
"upstream": {
216+
"type": "roundrobin",
217+
"nodes": {
218+
"127.0.0.1:1980": 1
219+
}
220+
},
221+
"plugins": {
222+
"wasm-request-body": {
223+
"setting": {"processReqBody":true, "start":1, "size":3}
224+
}
225+
}
226+
}]]
227+
)
228+
229+
if code >= 300 then
230+
ngx.status = code
231+
ngx.say(body)
232+
return
233+
end
234+
235+
ngx.say(body)
236+
}
237+
}
238+
--- error_code: 400
239+
--- response_body_like eval
240+
qr/invalid conf type/
241+
242+
243+
244+
=== TEST 9: hit
245+
--- request
246+
POST /hello
247+
hello
248+
--- grep_error_log eval
249+
qr/request get body: \w+/
250+
--- grep_error_log_out
251+
request get body: ell

0 commit comments

Comments
 (0)