# Read a file for token and use in it config file

**URL:** <https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504>\
**Category:** Logstash\
**Created:** [September 14, 2017, 9:52am UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504 "2017-09-14T09:52:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giridharan78](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giridharan78/32/22100_2.png) [@giridharan78](https://discuss.elastic.co/u/giridharan78)\
**Post date:** [September 14, 2017, 9:52am UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/1 "2017-09-14T09:52:51Z")

</div>

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](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"

}

}

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 14, 2017, 9:59am UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/2 "2017-09-14T09:59:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giridharan78](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giridharan78/32/22100_2.png) [@giridharan78](https://discuss.elastic.co/u/giridharan78)\
**Post date:** [September 14, 2017, 10:05am UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/3 "2017-09-14T10:05:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 14, 2017, 10:12am UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/4 "2017-09-14T10:12:32Z")

</div>

> 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.

> **[Using environment variables | Logstash Reference \[8.11\] | Elastic](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.

---

<div class="post-metadata">

**Author:** ![giridharan78](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giridharan78/32/22100_2.png) [@giridharan78](https://discuss.elastic.co/u/giridharan78)\
**Post date:** [September 14, 2017, 2:29pm UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/5 "2017-09-14T14:29:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 12, 2017, 2:29pm UTC](https://discuss.elastic.co/t/read-a-file-for-token-and-use-in-it-config-file/100504/6 "2017-10-12T14:29:40Z")

</div>

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