Skip to content

Commit

Permalink
fix(api-response): check response header format (#7238)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Jun 14, 2022
1 parent eb62ac0 commit 75107ab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion apisix/plugins/api-breaker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ local schema = {
type = "string",
minLength = 1
}
}
},
required = {"key", "value"},
}
},
max_breaker_sec = {
Expand Down
2 changes: 1 addition & 1 deletion docs/en/latest/plugins/api-breaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ In an unhealthy state, if the Upstream service responds with a status code from
|-------------------------|----------------|----------|---------|-----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| break_response_code | integer | True | | [200, ..., 599] | HTTP error code to return when Upstream is unhealthy. |
| break_response_body | string | False | | | Body of the response message to return when Upstream is unhealthy. |
| break_response_headers | array[object] | False | | | Headers of the response message to return when Upstream is unhealthy. Can only be configured when the `break_response_body` attribute is configured. The values can contain Nginx variables. For example, `$remote_addr` and `$balancer_ip`. |
| break_response_headers | array[object] | False | | [{"key":"header_name","value":"can contain Nginx $var"}] | Headers of the response message to return when Upstream is unhealthy. Can only be configured when the `break_response_body` attribute is configured. The values can contain APISIX variables. For example, we can use `{"key":"X-Client-Addr","value":"$remote_addr:$remote_port"}`. |
| max_breaker_sec | integer | False | 300 | >=3 | Maximum time in seconds for circuit breaking. |
| unhealthy.http_statuses | array[integer] | False | [500] | [500, ..., 599] | Status codes of Upstream to be considered unhealthy. |
| unhealthy.failures | integer | False | 3 | >=1 | Number of consecutive failures for the Upstream service to be considered unhealthy. |
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/latest/plugins/api-breaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ title: api-breaker
| ----------------------- | -------------- | ------ | ---------- | --------------- | -------------------------------- |
| break_response_code | integer | 必须 || [200, ..., 599] | 不健康返回错误码 |
| break_response_body | string | 可选 || | 不健康返回报文 |
| break_response_headers | array[object] | 可选 || | 不健康返回报文头,这里可以设置多个。这个值能够以 `$var` 的格式包含 Nginx 变量,比如 `$remote_addr $balancer_ip`。该字段仅在 `break_response_body` 被配置时生效 |
| break_response_headers | array[object] | 可选 || [{"key":"header_name","value":"can contain Nginx $var"}] | 不健康返回报文头,这里可以设置多个。该字段仅在 `break_response_body` 被配置时生效。这个值能够以 `$var` 的格式包含 APISIX 变量,比如 `{"key":"X-Client-Addr","value":"$remote_addr:$remote_port"}` |
| max_breaker_sec | integer | 可选 | 300 | >=3 | 最大熔断持续时间 |
| unhealthy.http_statuses | array[integer] | 可选 | {500} | [500, ..., 599] | 不健康时候的状态码 |
| unhealthy.failures | integer | 可选 | 3 | >=1 | 触发不健康状态的连续错误请求次数 |
Expand Down
33 changes: 33 additions & 0 deletions t/plugin/api-breaker.t
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,36 @@ phase_func(): breaker_time: 10
--- response_body
{"500":4,"502":16}
--- timeout: 25



=== TEST 20: reject invalid schema
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
for _, case in ipairs({
{input = {
break_response_code = 200,
break_response_headers = {{["content-type"] = "application/json"}}
}},
}) do
local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
{
id = "1",
plugins = {
["api-breaker"] = case.input
}
}
)
ngx.print(require("toolkit.json").decode(body).error_msg)
end
}
}
--- request
GET /t
--- response_body eval
qr/failed to check the configuration of plugin api-breaker err: property \"break_response_headers\" validation failed: failed to validate item 1: property \"(key|value)\" is required/
--- no_error_log
[error]

0 comments on commit 75107ab

Please sign in to comment.