<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using a scripted input's passAuth token for CLI access from the scripted input in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101549#M21293</link>
    <description>&lt;P&gt;Note for Windows users: the USERPROFILE, HOMEDRIVE and HOMEPATH env variables also need to be updated.&lt;/P&gt;

&lt;P&gt;this was tested on 6.3.3 so is still relevant as of May 2016.&lt;/P&gt;</description>
    <pubDate>Thu, 12 May 2016 22:42:45 GMT</pubDate>
    <dc:creator>anz_leycurav</dc:creator>
    <dc:date>2016-05-12T22:42:45Z</dc:date>
    <item>
      <title>Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101546#M21290</link>
      <description>&lt;P&gt;I am trying to create a scripted input that can run some Splunk CLI commands.  Using &lt;CODE&gt;passAuth&lt;/CODE&gt; in &lt;CODE&gt;inputs.conf&lt;/CODE&gt; I was able to pass in (and read from stdin) an auth token.  But I can't find how to successfully take that token and pass it back in on a CLI command line.  I tried &lt;CODE&gt;-auth &amp;lt;token&amp;gt;&lt;/CODE&gt; and got a "Login failed".  Any idea what CLI argument (if any) allows me to authenticate from an auth token directly?&lt;/P&gt;

&lt;P&gt;Related question at &lt;A href="http://splunk-base.splunk.com/answers/6707/splunk-admin-credentials-in-scripted-input"&gt;http://splunk-base.splunk.com/answers/6707/splunk-admin-credentials-in-scripted-input&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2011 03:11:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101546#M21290</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2011-05-13T03:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101547#M21291</link>
      <description>&lt;P&gt;There isn't a CLI argument to do that. You would likely be better served by going through the REST API if possible.&lt;/P&gt;

&lt;P&gt;When you run a splunk CLI search, it will look in &lt;CODE&gt;~/.splunk&lt;/CODE&gt; for an existing authToken, and prompts you for credentials if a valid authToken is not found. Typically that file is written when you run &lt;CODE&gt;splunk login&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;There's nothing to prevent you from writing that file out yourself before calling the CLI, but give careful consideration to the implications of doing so. First, any user who could read that directory could use those credentials (which is already true). Second, if you also run CLI commands directly as root (or splunk's service account), either the interactive user or the scripted input could get the wrong credentials.&lt;/P&gt;

&lt;P&gt;If you absolutely must call CLI commands directly, there's a workaround. Use &lt;CODE&gt;mktemp&lt;/CODE&gt; to create a temporary directory, and make sure the permissions are restricted. Write the authToken to &lt;CODE&gt;$TMPDIR/.splunk/authToken_&amp;lt;hostname&amp;gt;_&amp;lt;portnum&amp;gt;&lt;/CODE&gt;.  Then, set the $HOME environment variable to your &lt;CODE&gt;$TMPDIR&lt;/CODE&gt; before calling the CLI command(s). Once you're done, delete the authToken and the temporary directory.&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2011 16:14:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101547#M21291</guid>
      <dc:creator>southeringtonp</dc:creator>
      <dc:date>2011-05-13T16:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101548#M21292</link>
      <description>&lt;P&gt;Yup, making your own authToken file works.  The caveats still apply, but it got me around my specific issue here.&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2011 18:42:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101548#M21292</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2011-05-13T18:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101549#M21293</link>
      <description>&lt;P&gt;Note for Windows users: the USERPROFILE, HOMEDRIVE and HOMEPATH env variables also need to be updated.&lt;/P&gt;

&lt;P&gt;this was tested on 6.3.3 so is still relevant as of May 2016.&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 22:42:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101549#M21293</guid>
      <dc:creator>anz_leycurav</dc:creator>
      <dc:date>2016-05-12T22:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101550#M21294</link>
      <description>&lt;P&gt;While there isn't a CLI command line option, there is an environment variable you can set to pass an authentication token directly to the Splunk CLI.   You can use the &lt;CODE&gt;$SPLUNK_TOK&lt;/CODE&gt; variable for this.  There is very little documentation on this, if any, but it allows you to use the CLI directly, without creating an auth token file.  Similarly, you can also pass this token in on the Authorization header via a curl command to call arbitrary REST endpoints.  Example below:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/bin/bash
# Make sure passAuth=user is setup in your scripted input stanza, or this script will hang waiting for input.
read tok

# Do whatever CLI call you want to do
SPLUNK_TOK=$tok $SPLUNK_HOME/bin/splunk list forward-server

# Or make whatever REST call via CURL, like so:
curl -k -H "Authorization: Splunk $tok" &lt;A href="https://localhost:8089/servicesNS/-/-/data/inputs/script" target="test_blank"&gt;https://localhost:8089/servicesNS/-/-/data/inputs/script&lt;/A&gt;

# Or restart Splunkd, ....
curl -k -H "Authorization: Splunk $tok" -X POST &lt;A href="https://localhost:8089/services/server/control/restart" target="test_blank"&gt;https://localhost:8089/services/server/control/restart&lt;/A&gt;
...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;All of southeringtonp's warnings about security still apply.  It's possible for other processes to look at another processes env variable (in the case of the Splunk CLI), and to see the command line options (in the case of curl).   However, this may be considered "safer" than writing auth tokens to the file system.&lt;/P&gt;

&lt;P&gt;One positive security note is that Splunk appears to expire the token after the script completes, so that should reduce the window for any potential abuse of these credentials for quick scripts.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 22:08:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101550#M21294</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2017-03-02T22:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using a scripted input's passAuth token for CLI access from the scripted input</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101551#M21295</link>
      <description>&lt;P&gt;Here is something simple and similar to the above answer, but is all cli.&lt;BR /&gt;&lt;BR /&gt;
Replace localhost with your host ip/hostname&lt;BR /&gt;
Replace testdude with your username      which you must know the password for.&lt;BR /&gt;
Then run it.&lt;/P&gt;

&lt;P&gt;read -s -p "Enter Password: " mypassword;t=&lt;CODE&gt;curl -k  https://localhost:8089/services/auth/login --data-urlencode username=testdude --data-urlencode password=$mypassword&lt;/CODE&gt;;mytoken=&lt;CODE&gt;echo $t|grep -Po '&amp;lt;.*?&amp;gt;\K.*?(?=&amp;lt;.*?&amp;gt;)'|sed '/^\s$/d'&lt;/CODE&gt;;curl -k -H "Authorization: Splunk $mytoken" &lt;A href="https://localhost:8089/services/search/jobs/export" target="_blank"&gt;https://localhost:8089/services/search/jobs/export&lt;/A&gt; -d output_mode=csv  -d search="search index=_internal |head 10";mypassword="";mytoken="";t=""&lt;/P&gt;

&lt;P&gt;Here it is broken down.&lt;BR /&gt;
read -s -p "Enter Password: " mypassword;                                                                    allows you to enter password with entering it on command line and stores it in a variable, which is cleaned up at end.&lt;BR /&gt;
t=&lt;CODE&gt;curl -k  https://localhost:8089/services/auth/login --data-urlencode username=testdude --data-urlencode password=$mypassword&lt;/CODE&gt;;                          stores tokenized output in variable, which is cleaned up at end.&lt;BR /&gt;
mytoken=&lt;CODE&gt;echo $t|grep -Po '&amp;lt;.*?&amp;gt;\K.*?(?=&amp;lt;.*?&amp;gt;)'|sed '/^\s$/d'&lt;/CODE&gt;;                           uses above variable/output and strips it down to just token and stores it in variable&lt;BR /&gt;
curl -k -H "Authorization: Splunk $mytoken" &lt;A href="https://localhost:8089/services/search/jobs/export" target="_blank"&gt;https://localhost:8089/services/search/jobs/export&lt;/A&gt; -d output_mode=csv  -d search="search index=_internal |head 10";                uses token to run search&lt;BR /&gt;
mypassword="";mytoken="";t=""                        cleaned up of variables used.&lt;/P&gt;

&lt;P&gt;P.S.  Please shoot holes/refine it. I think it will make it more useful for everyone.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:59:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Using-a-scripted-input-s-passAuth-token-for-CLI-access-from-the/m-p/101551#M21295</guid>
      <dc:creator>JackNobrega</dc:creator>
      <dc:date>2020-09-29T20:59:35Z</dc:date>
    </item>
  </channel>
</rss>

