Logstash配置:带有Base 64编码标头的http输出 [英] Logstash configuration: http output with Base 64 encoded headers

查看:112
本文介绍了Logstash配置:带有Base 64编码标头的http输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use Logstash with File Input & Http Output to a homegrown service which requires credentials (username:password) to be sent as Base64 encoded value. Below is my Logstash configuration. Currently I can send the Base 64 encoded value via headers however I am looking for a way to encode a String (which would be available to Logstash via environment variable) to a Base 64 encoded String value. Would highly appreciate if anyone can throw some light on this.

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}" } } ] } }'
 }
}

推荐答案

您可以从环境中提取某些内容,并对它进行base64编码,如下所示:

You can pull in something from the environment and base64 encode it like this:

    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可以看到它做对了,

You can see that it does the right thing by running nc -l 1123 and see:

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

这篇关于Logstash配置:带有Base 64编码标头的http输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆