在日志中脱敏敏感数据
数据脱敏是一种数据保护技术,旨在防止敏感信息在各种环境中暴露,从而在不损害隐私的前提下支持应用程序的安全测试和数据分析。
API7 企业版提供的内置 data-mask 插件可以帮助您删除或替换 URL 编码请求体、请求头、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: 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:
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: 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
- name: file-logger
enable: true
config:
include_req_body: true
path: /tmp/mask-urlencoded-body.log
将配置应用到您的 Kubernetes 集群:
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"
}
}
补充资源
- 快速入门
- 插件中心