对日志中的敏感数据脱敏
数据脱敏是一种数据保护技术,可防止敏感信息在不同环境中暴露,让应用测试和数据分析得以在保护隐私的前提下进行。
API7 企业版内置的 data-mask 插件可以移除或替换请求头、请求体和 URL 查询参数中的敏感信息。
本指南介绍如何在 API7 企业版中对 URL 编码的请求体进行脱敏。示例中的 file-logger 插件仅用于验证脱敏是否成功,请根据实际场景调整。
以下互动演示介绍如何对日志中的敏感数据脱敏。
前提条件
启用 data-mask 和 file-logger 插件
- 控制台
- ADC
- Ingress Controller
-
从侧边栏选择网关组的已发布服务,然后点击要修改的服务,例如未设置版本的
httpbin服务。 -
在该已发布服务的侧边栏中选择路由。
-
选择目标路由,例如
/anything。 -
点击 + 添加插件。
-
搜索
data-mask插件。 -
点击添加。
-
在对话框中完成以下操作。
- 将以下配置添加到 JSON 编辑器。
{"request": [{"action": "remove","body_format": "json","name": "$.password","type": "body"},{"action": "replace","body_format": "json","name": "users[*].token","type": "body","value": "*****"},{"action": "regex","body_format": "json","name": "$.users[*].credit.card","regex": "(\\d+)\\-\\d+\\-\\d+\\-(\\d+)","type": "body","value": "$1-****-****-$2"}]}- 点击添加。
-
在同一条路由下,点击 + 添加插件。
-
搜索
file-logger插件。 -
点击添加。
-
在对话框中完成以下操作。
- 将以下配置添加到 JSON 编辑器。
{"include_req_body": true,"path": "/tmp/mask-urlencoded-body.log"}- 点击添加。
更新 ADC 配置文件,添加 data-mask 和 file-logger 插件。
adc.yaml
services:
- name: httpbin
upstream:
name: default
scheme: http
nodes:
- host: httpbin.org
port: 80
weight: 100
routes:
- uris:
- /anything
name: getting-started-anything
methods:
- GET
plugins:
data-mask:
request:
- action: remove
body_format: urlencoded
name: $.password
type: body
- action: replace
body_format: urlencoded
name: users[*].token
type: body
value: "*****"
- action: regex
body_format: urlencoded
name: $.users[*].credit.card
regex: (\d+)\-\d+\-\d+\-(\d+)
type: body
value: $1-****-****-$2
file-logger:
include_req_body: true
path: /tmp/mask-urlencoded-body.log
将配置同步到 API7 企业版。
adc sync -f adc.yaml
更新所选路由的 Kubernetes 清单文件,添加 data-mask 和 file-logger 插件。
httpbin-route.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: httpbin-route
# namespace: api7 # 替换为你的命名空间
spec:
http:
- name: httpbin-route
match:
paths:
- /anything
methods:
- GET
backends:
- serviceName: httpbin
servicePort: 80
plugins:
- name: data-mask
enable: true
config:
request:
- action: remove
body_format: urlencoded
name: $.password
type: body
- action: replace
body_format: urlencoded
name: "users[*].token"
type: body
value: "*****"
- action: regex
body_format: urlencoded
name: $.users[*].credit.card
regex: (\\d+)\\-\\d+\\-\\d+\\-(\\d+)
type: body
value: $1-****-****-$2
- name: file-logger
enable: true
config:
include_req_body: true
path: /tmp/mask-urlencoded-body.log
将配置应用到集群。
kubectl apply -f httpbin-route.yaml
验证
- 向路由发送请求进行验证。
curl -i "http://127.0.0.1:9080/anything" \
--data-urlencode "password=abc" \
--data-urlencode "token=xyz" \
--data-urlencode "card=1234-1234-1234-1234"
你应该会收到 HTTP/1.1 200 OK 响应。
- 在 Docker 容器中查看
/tmp/mask-urlencoded-body.log的内容。你应该会看到类似以下示例的日志条目。
{
"request": {
"uri": "/anything",
"body": "token=*****&card=1234-****-****-1234",
"method": "POST",
"url": "http://127.0.0.1:9080/anything"
}
}
相关阅读
- 快速开始
- 插件中心