Logstash配置:带有Base 64编码头的http输出

时间:2015-03-31 15:58:23

标签: http http-headers logstash grok logstash-grok

我使用带文件输入的Logstash& Http输出到本地服务,需要凭据(username:password)作为Base64 encoded值发送。下面是我的Logstash配置。目前我可以通过Base 64 encoded发送headers值但是我正在寻找一种方法将String(可通过环境变量用于Logstash)编码为Base 64 encoded字符串值。如果有人能对此有所了解,我将非常感激。

input {
 file {
  path => "/home/tom/testData/test2.log"
  type => "log"
 }
}

filter {
 if [type] == "log" {
  grok {
    match => ["message", "%{TIMESTAMP_ISO8601:time} %{LOG:level} %{GREEDYDATA:lmessage}"]
  }
  ruby {
    code => "require 'base64';
    event['pass'] = Base64.encode64('User:Pwd')"
  }

}
}
output {
stdout { codec => rubydebug }
http {
http_method => "post"
  url => "http://10.1.2.3:1123/HomeService?User=Tom"
  format => "message"
  headers => {"Authorization" => "Basic %{pass}"}      
  content_type => "application/json"
  message => '{"Outer": { "Inner": [ {"doc": { "Timestamp": "%{time}", "Single": "%{level}","Double": "%{lmessage}" } } ] } }'
 }
}

1 个答案:

答案 0 :(得分:3)

您可以从环境中获取内容,base64将其编码为:

    ruby {
            code => "
                    require 'base64';
                    event['password'] = Base64.encode64(ENV['USER_PASS']).sub(/\n/,'')
            "
    }

然后使用它:

 export USER_PASS="dog:cat"
 echo '{"test":1}' |  bin/logstash agent -f logstash.conf

使用这样的配置文件:

input {
 stdin { codec => json }
}

filter {
    ruby {
        code => "
            require 'base64';
            event['password'] = Base64.encode64(ENV['USER_PASS'])
        "
    }
}
output {
    stdout { codec => rubydebug }
    http {
        http_method => "post"
        url => "http://localhost:1123"
        format => "message"
        headers => {"Authorization" => "Basic %{password}"}
        content_type => "application/json"
        message => '{"whatever": 1 }'
    }
}

你可以通过运行nc -l 1123看到它做对了,看看:

POST  HTTP/1.1
host: localhost
connection: keep-alive
authorization: Basic ZG9nOmNhdA==
content-type: application/json
content-length: 15

{"whatever": 1}

哪个是正确的值:

echo ZG9nOmNhdA== | base64 -D
dog:cat