Read a file for token and use in it config file


(Giridharan Manoharan) #1

I've a token generated by a shell script and stored in a file(api_calls.log)). I wanted the logstash config file to read the file and use the token in the request in http_poller. The token has to be used in Authorization => "Bearer %{token}"

Really appreciate if any body can help in this regard. Below is the config file.

input
{
file {
path => "/home/gmanoharan155/Tool/logstash-5.5.1/api_calls.log"
start_position => "beginning"
}
http_poller{
urls => {
test2 => {
method => get
url => "https://xyz.co.uk/pa/1/testRuns?jobTemplate=jobTemplates/66620"
headers => {
Authorization => "Bearer %{token}"
Realm => "626367"
}
}
}
request_timeout => 1200
interval => 1200
codec => json
}
}
filter {

split {
field => "results"
}
}

output {
stdout { }
file {
path => "/home/gmanoharan155/Tool/logstash-5.5.1/output.log"

}

}


(Magnus Bäck) #2

Either generate your configuration file (so that it contains the correct token from the start) or inject the token via an environment variable.


(Giridharan Manoharan) #3

The token is valid for 24hrs, hence have to generate the token everyday and Logstash config had to read the file to fetch the token. When you said inject the token via an environment variable, could you please elaborate bit more for me.


(Magnus Bäck) #4

The token is valid for 24hrs, hence have to generate the token everyday and Logstash config had to read the file to fetch the token.

So generate the configuration file every 24 hours and have Logstash's auto config reload pick up the new file.

When you said inject the token via an environment variable, could you please elaborate bit more for me.

https://www.elastic.co/guide/en/logstash/current/environment-variables.html

Injecting the token via en environment variable obviously requires Logstash to be restarted if you want it to pick up a new token.


(Giridharan Manoharan) #5

Thank you very much for the solution. I choose the first option to create the config file everyday and use the config reload option to automatically load the config file.


(system) #6

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.