You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/04/04 09:33:55 UTC

[apisix] branch master updated: feat: suppot header injection for fault-injection plugin (#9039)

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

monkeydluffy 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 c86c05b72 feat: suppot header injection for fault-injection plugin (#9039)
c86c05b72 is described below

commit c86c05b72322a2267289c366eafc999107bb047c
Author: Tristan <ji...@foxmail.com>
AuthorDate: Tue Apr 4 17:33:47 2023 +0800

    feat: suppot header injection for fault-injection plugin (#9039)
---
 apisix/plugins/fault-injection.lua        | 23 ++++++++++++++
 docs/en/latest/plugins/fault-injection.md |  1 +
 docs/zh/latest/plugins/fault-injection.md |  1 +
 t/plugin/fault-injection2.t               | 50 +++++++++++++++++++++++++++++++
 4 files changed, 75 insertions(+)

diff --git a/apisix/plugins/fault-injection.lua b/apisix/plugins/fault-injection.lua
index 9089b4168..34ca05e81 100644
--- a/apisix/plugins/fault-injection.lua
+++ b/apisix/plugins/fault-injection.lua
@@ -20,6 +20,9 @@ local expr = require("resty.expr.v1")
 local sleep = core.sleep
 local random = math.random
 local ipairs = ipairs
+local ngx = ngx
+local pairs = pairs
+local type = type
 
 local plugin_name   = "fault-injection"
 
@@ -32,6 +35,18 @@ local schema = {
             properties = {
                 http_status = {type = "integer", minimum = 200},
                 body = {type = "string", minLength = 0},
+                headers = {
+                    type = "object",
+                    minProperties = 1,
+                    patternProperties = {
+                        ["^[^:]+$"] = {
+                            oneOf = {
+                                { type = "string" },
+                                { type = "number" }
+                            }
+                        }
+                    }
+                },
                 percentage = {type = "integer", minimum = 0, maximum = 100},
                 vars = {
                     type = "array",
@@ -144,6 +159,14 @@ function _M.rewrite(conf, ctx)
     end
 
     if conf.abort and sample_hit(conf.abort.percentage) and abort_vars then
+        if conf.abort.headers then
+            for header_name, header_value in pairs(conf.abort.headers) do
+                if type(header_value) == "string" then
+                    header_value = core.utils.resolve_var(header_value, ctx.var)
+                end
+                ngx.header[header_name] = header_value
+            end
+        end
         return conf.abort.http_status, core.utils.resolve_var(conf.abort.body, ctx.var)
     end
 end
diff --git a/docs/en/latest/plugins/fault-injection.md b/docs/en/latest/plugins/fault-injection.md
index 39b48153c..0e8977ca8 100644
--- a/docs/en/latest/plugins/fault-injection.md
+++ b/docs/en/latest/plugins/fault-injection.md
@@ -42,6 +42,7 @@ The `delay` attribute delays a request and executes of the subsequent Plugins.
 |-------------------|---------|-------------|---------|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | abort.http_status | integer | required    |         | [200, ...] | HTTP status code of the response to return to the client.                                                                                                   |
 | abort.body        | string  | optional    |         |            | Body of the response returned to the client. Nginx variables like `client addr: $remote_addr\n` can be used in the body.                                    |
+| abort.headers     | object  | optional    |         |            | Headers of the response returned to the client. The values in the header can contain Nginx variables like `$remote_addr`. |
 | abort.percentage  | integer | optional    |         | [0, 100]   | Percentage of requests to be aborted.                                                                                                                       |
 | abort.vars        | array[] | optional    |         |            | Rules which are matched before executing fault injection. See [lua-resty-expr](https://github.com/api7/lua-resty-expr) for a list of available expressions. |
 | delay.duration    | number  | required    |         |            | Duration of the delay. Can be decimal.                                                                                                                      |
diff --git a/docs/zh/latest/plugins/fault-injection.md b/docs/zh/latest/plugins/fault-injection.md
index 1569fca1f..5e4ddf975 100644
--- a/docs/zh/latest/plugins/fault-injection.md
+++ b/docs/zh/latest/plugins/fault-injection.md
@@ -38,6 +38,7 @@ description: 本文介绍了关于 Apache APISIX `fault-injection` 插件的基
 | ----------------- | ------- | ---- |  ---------- | -------------------------- |
 | abort.http_status | integer | 是   |  [200, ...] | 返回给客户端的 HTTP 状态码 |
 | abort.body        | string  | 否   |             | 返回给客户端的响应数据。支持使用 NGINX 变量,如 `client addr: $remote_addr\n`|
+| abort.headers     | object  | 否   |            |  返回给客户端的响应头,可以包含 NGINX 变量, 如 `$remote_addr` |
 | abort.percentage  | integer | 否   |  [0, 100]   | 将被中断的请求占比         |
 | abort.vars        | array[] | 否   |             | 执行故障注入的规则,当规则匹配通过后才会执行故障注。`vars` 是一个表达式的列表,来自 [lua-resty-expr](https://github.com/api7/lua-resty-expr#operator-list)。 |
 | delay.duration    | number  | 是   |             | 延迟时间,可以指定小数     |
diff --git a/t/plugin/fault-injection2.t b/t/plugin/fault-injection2.t
index 68b756bb0..0ba23f536 100644
--- a/t/plugin/fault-injection2.t
+++ b/t/plugin/fault-injection2.t
@@ -90,3 +90,53 @@ Fault Injection!
 GET /hello?name=jack&age=18
 --- response_body
 hello world
+
+
+
+=== TEST 4: inject header config
+--- 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": {
+                               "fault-injection": {
+                                   "abort": {
+                                        "http_status": 200,
+                                        "headers" : {
+                                            "h1": "v1",
+                                            "h2": 2,
+                                            "h3": "$uri"
+                                        }
+                                    }
+                               }
+                           },
+                           "upstream": {
+                               "nodes": {
+                                   "127.0.0.1:1980": 1
+                               },
+                               "type": "roundrobin"
+                           },
+                           "uri": "/hello"
+                   }]=]
+                   )
+               if code >= 300 then
+                   ngx.status = code
+               end
+               ngx.say(body)
+           }
+       }
+--- response_body
+passed
+
+
+
+=== TEST 5: inject header
+--- request
+GET /hello
+--- response_headers
+h1: v1
+h2: 2
+h3: /hello