Developing Dashboards, Views, and Apps for Splunk Web

 


Setup screen example with user credentials

NOTE - Splunk version 4.x reached its End of Life on October 1, 2013. Please see the migration information.

This documentation does not apply to the most recent version of Splunk. Click here for the latest version.

Setup screen example with user credentials

This example shows the steps needed to create a set up screen that accepts user/password credentials for a python script. The python script uses the credentials supplied in the setup screen.

  1. Create the block in your setup screen that accepts user credentials.
  2. Write a python script that uses the credentials
  3. Create a stanza in inputs.conf for your script.

1. Create a block in setup.xml that accepts user credentials

Add the following block to setup.xml to create the User input fields for your scripts.

<block title="Add new credentials" endpoint="admin/passwords" entity="_new">
  <input field="name">
    <label>Username</label>
    <type>text</type>
  </input>

  <input field="password">
    <label>Password</label>
    <type>password</type>
  </input>

  <!-- Remove the realm input field if your app is not using realm -->
  <input field="realm">
     <label>Realm</label>
     <type>text</type>
  </input>
</block>

2. Write a python script that uses credentials

Here is the path to the example script:

$SPLUNK_HOME/etc/apps/<MyApp>/default/bin/MyInputScript.py


MyInputScript.py

import splunk.entity as entity

. . .

# access the credentials in /servicesNS/nobody/<MyApp>/admin/passwords
def getCredentials(sessionKey):
   myapp = 'name-of-your-app-here'
   try:
      # list all credentials
      entities = entity.getEntities(['admin', 'passwords'], namespace=myapp,
                                    owner='nobody', sessionKey=sessionKey)
   except Exception, e:
      raise Exception("Could not get %s credentials from splunk. Error: %s"
                      % (myapp, str(e)))

   # return first set of credentials
   for i, c in entities.items():
        return c['username'], c['clear_password']

   raise Exception("No credentials have been found")  

def main():
        # read session key sent from splunkd
        sessionKey = sys.stdin.readline().strip()

        if len(sessionKey) == 0:
           sys.stderr.write("Did not receive a session key from splunkd. " +
                            "Please enable passAuth in inputs.conf for this " +
                            "script\n")
           exit(2)

        # now get twitter credentials - might exit if no creds are available
        username, password = getCredentials(sessionKey)

        # use the credentials to access the data source
        . . .

3. Create a stanza in inputs.conf

Here is the path to inputs.conf for the example:

$SPLUNK_HOME/etc/apps/<MyApp>/default/inputs.conf

Add the following stanza to inputs.conf:

[script://./bin/MyInputScript.py]
. . .
passAuth   = splunk-system-user

This documentation applies to the following versions of Splunk: 4.3 , 4.3.1 , 4.3.2 , 4.3.3 , 4.3.4 , 4.3.5 , 4.3.6 , 4.3.7 View the Article History for its revisions.


You must be logged into splunk.com in order to post comments. Log in now.

Was this documentation topic helpful?

If you'd like to hear back from us, please provide your email address:

We'd love to hear what you think about this topic or the documentation as a whole. Feedback you enter here will be delivered to the documentation team.

Feedback submitted, thanks!