docs: translate the document of request block plugin to English. (#150)

This commit is contained in:
Kent Dong
2023-01-31 10:13:04 +08:00
committed by GitHub
parent c8d84a0ad5
commit a5ef4cd482
11 changed files with 137 additions and 43 deletions

View File

@@ -89,20 +89,20 @@ spec:
- default/foo
# default 命名空间下名为 foo 的 ingress 会执行下面这个配置
config:
block_bodys:
block_bodies:
- "foo"
- ingress:
- default/bar
# default 命名空间下名为 bar 的 ingress 会执行下面这个配置
config:
block_bodys:
block_bodies:
- "bar"
# 域名级生效配置
- domain:
- "*.example.com"
# 若请求匹配了上面的域名, 会执行下面这个配置
config:
block_bodys:
block_bodies:
- "foo"
- "bar"
url: oci://<your_registry_hub>/request-block:1.0.0

View File

@@ -83,20 +83,20 @@ spec:
- default/foo
# the ingress foo in namespace default will use this config
config:
block_bodys:
block_bodies:
- "foo"
- ingress:
- default/bar
# the ingress bar in namespace default will use this config
config:
block_bodys:
block_bodies:
- "bar"
# domain-level takes effect
- domain:
- "*.example.com"
# if the request's domain matched, this config will be used
config:
block_bodys:
block_bodies:
- "foo"
- "bar"
url: oci://<your_registry_hub>/request-block:1.0.0

View File

@@ -5,9 +5,9 @@
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- | -------- |
| block_urls | array of string | 选填,`block_urls`,`block_headers`,`block_bodys` 中至少必填一项 | - | 配置用于匹配需要屏蔽 URL 的字符串 |
| block_headers | array of string | 选填,`block_urls`,`block_headers`,`block_bodys` 中至少必填一项 | - | 配置用于匹配需要屏蔽请求 Header 的字符串 |
| block_bodys | array of string | 选填,`block_urls`,`block_headers`,`block_bodys` 中至少必填一项 | - | 配置用于匹配需要屏蔽请求 Body 的字符串 |
| block_urls | array of string | 选填,`block_urls`,`block_headers`,`block_bodies` 中至少必填一项 | - | 配置用于匹配需要屏蔽 URL 的字符串 |
| block_headers | array of string | 选填,`block_urls`,`block_headers`,`block_bodies` 中至少必填一项 | - | 配置用于匹配需要屏蔽请求 Header 的字符串 |
| block_bodies | array of string | 选填,`block_urls`,`block_headers`,`block_bodies` 中至少必填一项 | - | 配置用于匹配需要屏蔽请求 Body 的字符串 |
| blocked_code | number | 选填 | 403 | 配置请求被屏蔽时返回的 HTTP 状态码 |
| blocked_message | string | 选填 | - | 配置请求被屏蔽时返回的 HTTP 应答 Body |
| case_sensitive | bool | 选填 | true | 配置匹配时是否区分大小写,默认区分 |
@@ -45,7 +45,7 @@ curl http://exmaple.com -H 'my-header: example-value'
## 屏蔽请求 body
```yaml
block_bodys:
block_bodies:
- "hello world"
case_sensitive: false
```
@@ -65,7 +65,7 @@ _rules_:
- _match_route_:
- route-a
- route-b
block_bodys:
block_bodies:
- "hello world"
# 规则二:按域名匹配生效
- _match_domain_:
@@ -73,7 +73,7 @@ _rules_:
- test.com
block_urls:
- "swagger.html"
block_bodys:
block_bodies:
- "hello world"
```
此例 `_match_route_` 中指定的 `route-a``route-b` 即在创建网关路由时填写的路由名称,当匹配到这两个路由时,将使用此段配置;
@@ -82,5 +82,5 @@ _rules_:
# 请求 Body 大小限制
当配置了 `block_bodys` 时,仅支持小于 32 MB 的请求 Body 进行匹配。若请求 Body 大于此限制,并且不存在匹配到的 `block_urls``block_headers` 项时,不会对该请求执行屏蔽操作
当配置了 `block_bodys` 时,若请求 Body 超过全局配置 DownstreamConnectionBufferLimits将返回 `413 Payload Too Large`
当配置了 `block_bodies` 时,仅支持小于 32 MB 的请求 Body 进行匹配。若请求 Body 大于此限制,并且不存在匹配到的 `block_urls``block_headers` 项时,不会对该请求执行屏蔽操作
当配置了 `block_bodies` 时,若请求 Body 超过全局配置 DownstreamConnectionBufferLimits将返回 `413 Payload Too Large`

View File

@@ -41,7 +41,7 @@ type RequestBlockConfig struct {
caseSensitive bool
blockUrls []string
blockHeaders []string
blockBodys []string
blockBodies []string
}
func parseConfig(json gjson.Result, config *RequestBlockConfig, log wrapper.Log) error {
@@ -75,19 +75,19 @@ func parseConfig(json gjson.Result, config *RequestBlockConfig, log wrapper.Log)
config.blockHeaders = append(config.blockHeaders, strings.ToLower(header))
}
}
for _, item := range json.Get("block_bodys").Array() {
for _, item := range json.Get("block_bodies").Array() {
body := item.String()
if body == "" {
continue
}
if config.caseSensitive {
config.blockBodys = append(config.blockBodys, body)
config.blockBodies = append(config.blockBodies, body)
} else {
config.blockBodys = append(config.blockBodys, strings.ToLower(body))
config.blockBodies = append(config.blockBodies, strings.ToLower(body))
}
}
if len(config.blockUrls) == 0 && len(config.blockHeaders) == 0 &&
len(config.blockBodys) == 0 {
len(config.blockBodies) == 0 {
return errors.New("there is no block rules")
}
return nil
@@ -131,7 +131,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config RequestBlockConfig, lo
}
}
}
if len(config.blockBodys) == 0 {
if len(config.blockBodies) == 0 {
ctx.DontReadRequestBody()
}
return types.ActionContinue
@@ -142,7 +142,7 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config RequestBlockConfig, body
if !config.caseSensitive {
bodyStr = strings.ToLower(bodyStr)
}
for _, blockBody := range config.blockBodys {
for _, blockBody := range config.blockBodies {
if strings.Contains(bodyStr, blockBody) {
proxywasm.SendHttpResponse(config.blockedCode, nil, []byte(config.blockedMessage), -1)
return types.ActionContinue