对日志进行数据脱敏
数据脱敏是一种数据保护技术,旨在防止在各种环境中暴露敏感信息,从而在不影响隐私的情况下支持安全应用程序测试和数据分析。
API7 企业版提供的内置 data-mask 插件可以帮助删除或替换请求头、请求体和 URL 查询参数中的敏感信息。
本指南将向你展示如何使用 API7 企业版隐藏 URL 编码的请求体中的敏感信息。示例中用于记录日志的 file-logger 插件仅用于展示信息已成功脱敏。请根据你的使用场景进行相应调整。
下面是一个交互式演示,提供隐藏日志中敏感数据的动手实践介绍。
前置条件
启用 data-mask 和 file-logger 插件
- Dashboard
- 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"
}- 点击 新增。
使用 data-mask 和 file-logger 插件更新 ADC 配置文件:
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"
}
}
额外资源
- 快速入门
- 插件中心