如何在 ingest pipeline 脱敏手机号?
解读
面试官想知道你是否能把“手机号”这一典型敏感字段在写入 Elasticsearch 之前就完成不可逆脱敏,同时保证后续检索、聚合、可视化不受影响。国内监管(《个人信息保护法》《数据安全法》)要求“最小可用”“可审计”,因此必须做到:
- 脱敏逻辑在服务端 ingest pipeline 内完成,避免客户端绕开;
- 脱敏后字段仍保留格式一致性(11 位数字→11 位数字),方便后续日志追踪;
- 支持多租户场景下不同脱敏策略(有的业务要中间四位打码,有的要全哈希);
- 必须可回滚:原始值写入另一个仅限高权 role 访问的字段,或直接丢弃;
- 性能损耗在 5% 以内,不影响每日 TB 级日志吞吐量。
知识点
- Ingest pipeline 执行顺序:processor 链式执行,可条件判断、on_failure 分支。
- Painless 脚本安全沙箱:国内大部分金融、运营商集群关闭动态脚本,需提前在 config/scripts 目录静态注册。
- 国内手机号规则:1xx xxxx xxxx,号段持续更新(19x、16x、14x),正则需兼容。
- 哈希选择:SHA-256 单哈希+salt 可防彩虹表;若需可逆,用 AES/GCM,密钥放 Elasticsearch Keystore。
- RBAC:通过 field-level security 把原始 mobile.raw 限定在 data_admin 角色,脱敏后 mobile 对所有角色可见。
- 审计:打开 xpack.security.audit.enabled,记录 pipeline 调用与 _update_by_query 重写动作,满足等保 2.0 三级要求。
- 性能:script processor 每核 1 万 doc/s 左右,若吞吐量更高,可改用 fingerprint processor 或提前在 Logstash 完成。
答案
下面给出一条可直接落地的 pipeline,兼顾“国内号段兼容”“不可逆哈希”“字段级权限”“可审计”四点。
步骤 1:在集群每个节点 config/scripts 下新建文件 mobile_hash.painless
String mobile = ctx.mobile;
if (mobile == null || mobile.length() != 11) { return; }
if (!Pattern.matches('^1[3-9]\\d{9}$', mobile)) { return; }
String salt = params.salt;
ctx.mobile_anonymous = org.elasticsearch.common.hash.MessageDigests.toHexString(
org.elasticsearch.common.hash.MessageDigests.sha256().digest((mobile + salt).getBytes(StandardCharsets.UTF_8))
).substring(0, 11); // 取前 11 位十六进制,保证长度一致
步骤 2:注册并重启节点,使脚本静态加载。
步骤 3:创建 pipeline
PUT _ingest/pipeline/mobile_mask_pipeline
{
"description": "国内手机号脱敏,SHA256+salt 取前 11 位",
"processors": [
{
"script": {
"id": "mobile_hash",
"params": { "salt": "cn2024#salt" }
}
},
{
"remove": {
"field": ["mobile"],
"if": "ctx.mobile_anonymous != null"
}
}
],
"on_failure": [
{
"set": {
"field": "_ingest.failure_reason",
"value": "{{_ingest.on_failure_message}}"
}
}
]
}
步骤 4:索引模板绑定 pipeline
PUT _index_template/log_mobile_template
{
"index_patterns": ["app-log-*"],
"template": {
"settings": {
"default_pipeline": "mobile_mask_pipeline"
},
"mappings": {
"properties": {
"mobile_anonymous": { "type": "keyword", "doc_values": true },
"mobile": { "type": "keyword", "doc_values": false }
}
}
}
}
步骤 5:RBAC 示例
POST _security/role/data_admin
{
"cluster": ["monitor"],
"indices": [
{
"names": ["app-log-*"],
"privileges": ["read"],
"field_security": { "grant": ["*"] }
}
]
}
POST _security/role/analyst
{
"indices": [
{
"names": ["app-log-*"],
"privileges": ["read"],
"field_security": { "grant": ["mobile_anonymous"], "except": ["mobile"] }
}
]
}
结果:写入阶段自动完成脱敏,analyst 角色在 Kibana Discover 只能看到 11 位哈希串,无法还原原始手机号;data_admin 如需回溯,可在 ingest 阶段把原始值写入 mobile.raw 并只对 data_admin 授权,满足“可用不可见”的合规要求。
拓展思考
- 可逆脱敏场景:客服系统需根据哈希反查真实手机号。可把 AES/GCM 密钥放 Elasticsearch Keystore,用 script processor 做加密而非哈希,再配套解密 API,但需额外做密钥轮换与 KMS 集成。
- 号段白名单:某些内部测试号(165/167)不需要脱敏,可在 script 里加白名单列表,或通过 Enrich processor 动态查询号段表。
- 存量数据重写:已有索引用 _update_by_query 附加 pipeline,注意集群水位,建议按时间分段、slice scroll 并行,避免 IO 打满。
- 多租户不同策略:利用 Spaces+index pattern 隔离,结合 pipeline 传入 metadata._tenant 参数,动态选择 salt 或掩码规则。
- 性能极限优化:若每日 50 TB、峰值 50 万 doc/s,可在 Beats/Logstash 端用 Lua/Java 做脱敏,彻底去掉 script processor,把 CPU 留给 Elasticsearch 查询;同时打开 indexing pressure 监控,确保队列不堆积。