logstash mutate 从 URL 中删除 API 密钥

logstash mutate 从 URL 中删除 API 密钥

我将 nginx 日志放入 logstash 中,不幸的是 api 信息是通过 get 发送的。

因此,logstash 中存储 API 信用信息的部分有 2 个。以下是示例

message: 10.120.40.105 - - [29/Jul/2015:16:41:09 +0000] "PUT /v1/resources/scenes/455IrIBcRsa0kkIs6mv9lQ?api_key=11111111111111111&api_secret=2222222222222222222222222 HTTP/1.1" 200 689 "-" "python-requests/2.6.0 CPython/2.7.9 Linux/2.6.32-504.30.3.el6.x86_64" "10.120.40.105" 0.180 0.180
request: /v1/resources/scenes/455IrIBcRsa0kkIs6mv9lQ?api_key=11111111111111111&api_secret=2222222222222222222222222

我正在通过以下方式放弃请求

NGUSERNAME [a-zA-Z\.\@\-\+_%]+
NGUSER %{NGUSERNAME}
NGINXACCESS %{IPORHOST:clientip} %{NGUSER:ident} %{NGUSER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:"(?:%{URI:referrer}|-)"|%{QS:referrer}) %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time} %{NUMBER:upstream_time}
NGINXACCESS %{IPORHOST:clientip} %{NGUSER:ident} %{NGUSER:auth} \[%{HTTPDATE:timestamp}\] "%{WORD:verb} %{URIPATHPARAM:request} HTTP/%{NUMBER:httpversion}" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:"(?:%{URI:referrer}|-)"|%{QS:referrer}) %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time}

我的输入看起来像

    grok {
        match => { "message" => "%{NGINXACCESS}" }
        patterns_dir => ["/etc/logstash/patterns"]
    }
    date {
        match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ]
    }
    geoip {
        source => "clientip"
        target => "geoip"
        database => "/usr/share/GeoIP/GeoLiteCity.dat"
        add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
        add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
    }
    mutate {
        convert => [ "[geoip][coordinates]", "float"]
        convert => [ "request_time", "float"]
        convert => [ "upstream_time", "float"]
    }

是否有任何变异方式可以将 api_secret= 之后的任何内容替换为“xxxxxxxxxxxx”

谢谢!

答案1

这实际上比看起来要难一些,因为gsubfor 字段mutate实际上并没有执行您想要的操作。它似乎没有您想象的那么聪明。

我不得不修改您使用的模式,以捕获requestpre_reqpost_req)之前和之后的所有内容,但这似乎是可能的。
不知道它在性能方面的扩展效果如何,因为这里进行了大量过滤,但它确实有效。

我使用这个配置进行了测试:

input {
  stdin {}
}

filter {
  grok {
    match => [
      "message" , "(?<pre_req>%{IPORHOST:clientip} (?<ident>[a-zA-Z\.\@\-\+_%]+) (?<auth>[a-zA-Z\.\@\-\+_%]+) \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} )%{URIPATHPARAM:request}(?<post_req> HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:\"(?:%{URI:referrer}|-)\"|%{QS:referrer}) %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time} %{NUMBER:upstream_time})",
      "message" , "(?<pre_req>%{IPORHOST:clientip} (?<ident>[a-zA-Z\.\@\-\+_%]+) (?<auth>[a-zA-Z\.\@\-\+_%]+) \[%{HTTPDATE:timestamp}\] \"%{WORD:verb} )%{URIPATHPARAM:request}(?<post_req> HTTP/%{NUMBER:httpversion}\" %{NUMBER:response} (?:%{NUMBER:bytes}|-) (?:\"(?:%{URI:referrer}|-)\"|%{QS:referrer}) %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time})"
      ]
    break_on_match => true
  }
  grok {
    match => { "request" => "(?<request_path>[^?]*)?(?<request_params>.*)"
  }

  }
  mutate {
    gsub => [ "request_params" , "[?]", "" ]
  }
  kv {
    field_split => "&"
    source => "request_params"
    prefix => "request_params_"
  }
  mutate {
    replace => { "request" => "%{request_path}?api_key=%{request_params_api_key}&api_secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" }
    replace => { "message" => "%{pre_req}%{request}%{post_req}" }
    remove_field => [ "request_path", "request_params", "request_params_api_key", "request_params_api_secret", "pre_req", "post_req" ]
  }
}

output {
  stdout { codec => rubydebug }
}

它似乎已经完成了你想要的事情......

# /opt/logstash/bin/logstash -f config.conf
Logstash startup completed
10.120.40.105 - - [29/Jul/2015:16:41:09 +0000] "PUT /v1/resources/scenes/455IrIBcRsa0kkIs6mv9lQ?api_key=11111111111111111&api_secret=2222222222222222222222222 HTTP/1.1" 200 689 "-" "python-requests/2.6.0 CPython/2.7.9 Linux/2.6.32-504.30.3.el6.x86_64" "10.120.40.105" 0.180 0.180
{
          "message" => "10.120.40.105 - - [29/Jul/2015:16:41:09 +0000] \"PUT /v1/resources/scenes/455IrIBcRsa0kkIs6mv9lQ?api_key=11111111111111111&api_secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX HTTP/1.1\" 200 689 \"-\" \"python-requests/2.6.0 CPython/2.7.9 Linux/2.6.32-504.30.3.el6.x86_64\" \"10.120.40.105\" 0.180 0.180",
         "@version" => "1",
       "@timestamp" => "2015-07-29T19:21:14.678Z",
             "host" => "elk.example.com",
         "clientip" => "10.120.40.105",
            "ident" => "-",
             "auth" => "-",
        "timestamp" => "29/Jul/2015:16:41:09 +0000",
             "verb" => "PUT",
          "request" => "/v1/resources/scenes/455IrIBcRsa0kkIs6mv9lQ?api_key=11111111111111111&api_secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
      "httpversion" => "1.1",
         "response" => "200",
            "bytes" => "689",
            "agent" => "\"python-requests/2.6.0 CPython/2.7.9 Linux/2.6.32-504.30.3.el6.x86_64\"",
    "xforwardedfor" => "\"10.120.40.105\"",
     "request_time" => "0.180",
    "upstream_time" => "0.180"
}

相关内容