<?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: How do you test a modular input on the command line with a valid session_key? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/358727#M65474</link>
    <description>&lt;P&gt;In case this helps someone in the future, I worked around the problem by cheating. I wrote a small script to manually authenticate and generate a session key. Then I replace the session key in the modinput config and pass it to the script in question.&lt;/P&gt;

&lt;P&gt;So, to run it on the command line, I type:&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;"C:\Program Files\Splunk\bin\splunk" cmd splunkd print-modinput-config simpleinput simpleinput://evc | "c:\Program Files (x86)\Python36-32\python" C:\Utils\session_replacer.py | "C:\Program Files\Splunk\bin\splunk" cmd python "c:\Program Files\Splunk\etc\apps\simpleinput\bin\simpleinput.py"&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;Here's the content of the file session_replacer.py:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/env python

import sys
import re
import requests
import urllib3
urllib3.disable_warnings()

splunk_url = 'https://localhost:8089/services/auth/login'
username = 'admin'
password = 'changeme'

urllib3.disable_warnings()
session = requests.Session()
session.verify = False

form = {'username': username, 'password': password}
response = session.post(url=splunk_url, data=form, verify=False)
matches = re.search('&amp;lt;sessionKey&amp;gt;(.+)&amp;lt;/sessionKey&amp;gt;', response.text, re.MULTILINE|re.DOTALL)
if matches is not None:
    new_key = matches.group(1)

input = sys.stdin.read()
output = re.sub('&amp;lt;session_key&amp;gt;(.+)&amp;lt;/session_key&amp;gt;','&amp;lt;session_key&amp;gt;{}&amp;lt;/session_key&amp;gt;'.format(new_key), input, re.MULTILINE|re.DOTALL)
print(output)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 13 Nov 2017 06:22:44 GMT</pubDate>
    <dc:creator>envancleve</dc:creator>
    <dc:date>2017-11-13T06:22:44Z</dc:date>
    <item>
      <title>How do you test a modular input on the command line with a valid session_key?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/358726#M65473</link>
      <description>&lt;P&gt;I'm writing a modular input (simpleinput) which stores its password using StoragePasswords facility offered by the Splunk Python SDK.&lt;/P&gt;

&lt;P&gt;The script works just fine when its called by Splunk itself. However, when I try to run the module on the command line for testing purposes, the call always fails saying the "Session is not logged in".&lt;/P&gt;

&lt;P&gt;Here's how I run the script on the command line:&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;$SPLUNK_HOME\bin\splunk" cmd splunkd print-modinput-config simpleinput simpleinput://evc | "C:\Program Files\Splunk\bin\splunk" cmd python "c:\Program Files\Splunk\etc\apps\simpleinput\bin\simpleinput.py"&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;It appears that the SessionKey produced by the print-modinput-config command is invalid. Below, I've shown a session which attempts to use the SessionKey generated by the print-modinput-config to authenticate to the /services/server/info endpoint. After it fails, I grab a key manually by authenticating with a username and password, which is successful.&lt;/P&gt;

&lt;H2&gt;How do other developers run their modular inputs while testing? Is there a way to get a valid sessionkey out of the print-modinput-config command?&lt;/H2&gt;

&lt;P&gt;&lt;EM&gt;$SPLUNK_HOME\bin\splunk" cmd splunkd print-modinput-config simpleinput simpleinput://evc&lt;/EM&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;input&amp;gt;
  &amp;lt;server_host&amp;gt;215CN72&amp;lt;/server_host&amp;gt;
  &amp;lt;server_uri&amp;gt;https://127.0.0.1:8089&amp;lt;/server_uri&amp;gt;
  &amp;lt;session_key&amp;gt;session_key_from_print_modinput_config&amp;lt;/session_key&amp;gt;
  &amp;lt;checkpoint_dir&amp;gt;C:\Program Files\Splunk\var\lib\splunk\modinputs\simpleinput&amp;lt;/checkpoint_dir&amp;gt;
  &amp;lt;configuration&amp;gt;
    &amp;lt;stanza name="simpleinput://evc" app="launcher"&amp;gt;
      &amp;lt;param name="disabled"&amp;gt;0&amp;lt;/param&amp;gt;
      &amp;lt;param name="first_name"&amp;gt;Eric&amp;lt;/param&amp;gt;
      &amp;lt;param name="host"&amp;gt;215CN72&amp;lt;/param&amp;gt;
      &amp;lt;param name="index"&amp;gt;default&amp;lt;/param&amp;gt;
      &amp;lt;param name="interval"&amp;gt;60&amp;lt;/param&amp;gt;
    &amp;lt;/stanza&amp;gt;
  &amp;lt;/configuration&amp;gt;
&amp;lt;/input&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;EM&gt;curl -k "&lt;A href="https://localhost:8089/services/server/info" target="_blank"&gt;https://localhost:8089/services/server/info&lt;/A&gt;" -H "Authorization: Splunk session_key_from_print_modinput_config"&lt;/EM&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;response&amp;gt;
  &amp;lt;messages&amp;gt;
    &amp;lt;msg type="WARN"&amp;gt;call not properly authenticated&amp;lt;/msg&amp;gt;
  &amp;lt;/messages&amp;gt;
&amp;lt;/response&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;EM&gt;curl -k "&lt;A href="https://localhost:8089/services/auth/login" target="_blank"&gt;https://localhost:8089/services/auth/login&lt;/A&gt;" -d "username=admin&amp;amp;password=changeme"&lt;/EM&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;response&amp;gt;
  &amp;lt;sessionKey&amp;gt;session_key_from_admin_login&amp;lt;/sessionKey&amp;gt;
&amp;lt;/response&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;EM&gt;curl -k "&lt;A href="https://localhost:8089/services/server/info" target="_blank"&gt;https://localhost:8089/services/server/info&lt;/A&gt;" -H "Authorization: Splunk session_key_from_admin_login"&lt;/EM&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!--This is to override browser formatting; see server.conf[httpServer] to disable. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .--&amp;gt;
&amp;lt;?xml-stylesheet type="text/xml" href="/static/atom.xsl"?&amp;gt;
&amp;lt;feed xmlns="http://www.w3.org/2005/Atom" xmlns:s="http://dev.splunk.com/ns/rest" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"&amp;gt;
  &amp;lt;title&amp;gt;server-info&amp;lt;/title&amp;gt;
  &amp;lt;id&amp;gt;https://localhost:8089/services/server/info&amp;lt;/id&amp;gt;
snip snip snip snip
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:41:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/358726#M65473</guid>
      <dc:creator>envancleve</dc:creator>
      <dc:date>2020-09-29T16:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: How do you test a modular input on the command line with a valid session_key?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/358727#M65474</link>
      <description>&lt;P&gt;In case this helps someone in the future, I worked around the problem by cheating. I wrote a small script to manually authenticate and generate a session key. Then I replace the session key in the modinput config and pass it to the script in question.&lt;/P&gt;

&lt;P&gt;So, to run it on the command line, I type:&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;"C:\Program Files\Splunk\bin\splunk" cmd splunkd print-modinput-config simpleinput simpleinput://evc | "c:\Program Files (x86)\Python36-32\python" C:\Utils\session_replacer.py | "C:\Program Files\Splunk\bin\splunk" cmd python "c:\Program Files\Splunk\etc\apps\simpleinput\bin\simpleinput.py"&lt;/EM&gt;&lt;/P&gt;

&lt;P&gt;Here's the content of the file session_replacer.py:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/env python

import sys
import re
import requests
import urllib3
urllib3.disable_warnings()

splunk_url = 'https://localhost:8089/services/auth/login'
username = 'admin'
password = 'changeme'

urllib3.disable_warnings()
session = requests.Session()
session.verify = False

form = {'username': username, 'password': password}
response = session.post(url=splunk_url, data=form, verify=False)
matches = re.search('&amp;lt;sessionKey&amp;gt;(.+)&amp;lt;/sessionKey&amp;gt;', response.text, re.MULTILINE|re.DOTALL)
if matches is not None:
    new_key = matches.group(1)

input = sys.stdin.read()
output = re.sub('&amp;lt;session_key&amp;gt;(.+)&amp;lt;/session_key&amp;gt;','&amp;lt;session_key&amp;gt;{}&amp;lt;/session_key&amp;gt;'.format(new_key), input, re.MULTILINE|re.DOTALL)
print(output)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Nov 2017 06:22:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/358727#M65474</guid>
      <dc:creator>envancleve</dc:creator>
      <dc:date>2017-11-13T06:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do you test a modular input on the command line with a valid session_key?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/553305#M91736</link>
      <description>&lt;P&gt;I found another workaround.&lt;/P&gt;&lt;P&gt;Just launch command&lt;FONT face="courier new,courier"&gt; $SPLUNK_HOME/bin/splunk login&lt;/FONT&gt; and provide requested credentials (ideally admin level). It will create a session key within a file named &lt;FONT face="courier new,courier"&gt;$HOME/.splunk/auth*&lt;/FONT&gt;. Within this file, you will find a valid session key which should work for your modular input (until the session timeout value, I think it's 2h by default).&lt;/P&gt;&lt;P&gt;Caution that the session key is between &amp;lt;sessionkey&amp;gt; tags and your modular input expects &amp;lt;session_key&amp;gt; instead&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 11:10:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-test-a-modular-input-on-the-command-line-with-a-valid/m-p/553305#M91736</guid>
      <dc:creator>yoho</dc:creator>
      <dc:date>2021-05-27T11:10:26Z</dc:date>
    </item>
  </channel>
</rss>

