在日志中脱敏敏感数据
数据脱敏是一种数据保护技术,旨在防止敏感信息在各种环境中暴露,从而在不损害隐私的前提下支持应用程序的安全测试和数据分析。
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": "urlencoded","name": "password","type": "body"},{"action": "replace","body_format": "urlencoded","name": "token","type": "body","value": "*****"},{"action": "regex","body_format": "urlencoded","name": "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: token
type: body
value: "*****"
- action: regex
body_format: urlencoded
name: 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 插件:
- Gateway API
- APISIX CRD
httpbin-route.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: api7
name: mask-data-plugin-config
spec:
plugins:
- name: data-mask
config:
request:
- action: remove
body_format: urlencoded
name: password
type: body
- action: replace
body_format: urlencoded
name: token
type: body
value: "*****"
- action: regex
body_format: urlencoded
name: card
regex: "(\\d+)\\-\\d+\\-\\d+\\-(\\d+)"
type: body
value: "$1-****-****-$2"
- name: file-logger
config:
include_req_body: true
path: /tmp/mask-urlencoded-body.log
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: api7
name: httpbin-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: mask-data-plugin-config
backendRefs:
- name: httpbin
port: 80
httpbin-route.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: api7
name: httpbin
spec:
ingressClassName: apisix
http:
- name: httpbin-route
match:
paths:
- /anything
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: token
type: body
value: "*****"
- action: regex
body_format: urlencoded
name: 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 容器或 Kubernetes Pod 中查看 /tmp/mask-urlencoded-body.log 的文件内容。应看到类似以下内容的日志条目:
{
"request": {
"uri": "/anything",
"body": "token=*****&card=1234-****-****-1234",
"method": "POST",
"url": "http://127.0.0.1:9080/anything"
}
}
补充资源
- 快速入门
- 插件中心