我们正在记录请求,当然这些请求也包含授权标头等敏感数据。现在我想最好使用正则表达式来重新记录它。因为我总是发现设计正则表达式非常复杂,所以我想我在这里问。
部分回复如下所示:
Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
["Authorization": "Bearer xxxxxxxxxx", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "xxxxxxxxx"]
}
{
"data" : {
}
正则表达式应按如下方式工作:
找到包含名称
secret
或 authorization
的标头字段,最好不区分大小写,并将其值替换为例如***
。
结果应如下所示:
Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
["Authorization": "***", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "***"]
}
{
"data" : {
}
这应该有效
masked_log=$(echo "$log" | sed -E 's/(Authorization|Secret)":\s*"[^"]*"/\1": "***"/Ig')