Splunk® Enterprise

Module System Reference

Acrobat logo Download manual as PDF


Splunk Enterprise version 7.0 is no longer supported as of October 23, 2019. See the Splunk Software Support Policy for details. For information about upgrading to a supported version, see How to upgrade Splunk Enterprise.
This documentation does not apply to the most recent version of Splunk® Enterprise. For documentation on the most recent version, go to the latest release.
Acrobat logo Download topic as PDF

Decorators

Important notice: As part of Advanced XML deprecation, the Module System is officially deprecated beginning with Splunk Enterprise 6.3. For more information, see Advanced XML Deprecation.

@expose_page()

The @expose_page() decorator exposes the decorated method, providing authentication, SSO verification, and CSRF protection. The @expose_page() decorator is often used with the @route() decorator.

Note: Any controller method exposed to the user should use the @expose_page() decorator. Very few methods should ever be exposed without authentication.

Synopsis

@expose_page( must_login, handle_api, methods, verify_session, verify_sso, trim_spaces )

Parameters

must_login Boolean Login requirement control:
True = (Default) Must be logged in to expose page.
False = Login not required.
handle_api Boolean, Constant API handler control:
True = Requests beginning with /api are sent to the handler.
False = (Default) All requests are sent to the handler.
ONLY_API = Only requests beginning with /api are sent to the handler.
methods String Comma-separated list of method names s to apply the decorator to. Default = None.
verify_session Boolean POST verification control:
True = (Default) POSTs are verified to prevent CSRF.
False = POSTs are not verified.
verify_sso Boolean SSO IP address verification control:
True = (Default) In SSO mode, verify the SSO IP address.
False = Not in SSO mode, do not verify the SSO IP address.
trim_spaces Boolean Keyword and value trim spaces control:
True = Trim spaces from keywords and values.
False = (Default) Do not trim spaces from keywords and values.

Example

@route('/:namespace/:action=fields') @expose_page(must_login=True, handle_api=True, methods=['GET', 'POST'])

def fields(self, namespace, action, operation=None, **kwargs):

   return self.render_admin_template('admin/fields.html', {
       'namespace'  : namespace,
       'breadcrumbs'     : self.generateBreadcrumbs(namespace, 'fields'),
   })

See Also

@route()

@lock_session()

Use the @lock_session() decorator to acquire an exclusive lock on a CherryPy session for the duration of the current request. (See: CherryPy v3.2.0 documentation, Reference Manual.) This protects against deadlocks and race conditions between methods concurrently making changes to session data.

Splunk implements custom session lock behaviour by default, where CherryPy acquires a shared lock instead of an exclusive lock. A local shared lock protects against race conditions between threads and a file level lock protects against races between processes.

Synopsis

@lock_session

Example

@expose_page(must_login=False, methods=['GET','POST'], verify_session=False)@lock_session @set_cache_level('never')

def login(self, username=None, password=None, return_to=None, cval=None, **kwargs):

   ... elided ...

@route()

The @route() decorator permits URI targets to be routed to specific controller methods. The @route() decorator is often used with the @expose_page() decorator.

Note: Every controller method which needs to be exposed by the Splunk Web hierarchy at a target, other than the method name itself, should be preceded by an @route() decorator.

A route is specified using the following syntax:

<controller_name>/

   The implied base path.
       e.g. my_controller/

@route('/:var')

   Any value for <controller_name> within 1 path segment
       e.g. my_controller/setup
           my_controller/foobar
           my_controller/*

@route('/:action=setup', methods='GET')

   Only requests that begin with /setup - the action kwarg will always be 'setup'
       e.g. my_controller/setup?action=foobar 
           unittest.assertEqual(action, 'setup')

@route('/:var/*category/:action', methods='POST')

   Adds a keyword argument called category that greedily matches path segments, but only for the POST verb
       e.g my_controller/setup/step1/confirm/areusure/doublecheck
           unittest.assertEqual(var, 'setup')
           unittest.assertEqual(category, '/step1/confirm/areusure')
           unittest.assertEqual(action, 'doublecheck')

Synopsis

@route( route, methods, leave_exposed )

Parameters

route String Route to expose for this method. Default = None.
methods String Comma-separated string of HTTP methods/verbs to route for. Default = None.
leave_exposed Boolean Method exposure control:
True = Leave method exposed at method name.
False = Do not leave method exposed at method name.

Example

The controller config.py has several methods, but only two are accessible using SplunkWeb:

  • http://<splunk-server>:<splunk-port>/config
  • http://<splunk-server>:<splunk-port>/config/<VAR>

where VAR is a valid key exposed by the config endpoint.

This is because of the use of the @route() decorator in the controller:

... elided ...

@route('/') @expose_page(must_login=False, methods='GET') def index(self, autoload=False, namespace=None, asDict=False):

... elided ...

@route('/:var') @expose_page(methods='GET') def getvar(self, var, **kw):

... elided ...

The @route() decoration that precedes index() routes the root endpoint of the controller to index(), while the @route() decorator that precedes getvar() routes any target below the root endpoint to the getvar(). Note that the var variable in the later decorator is passed to getvar() as a keyword argument.

Thus, navigating to /en-US/config/VERSION_LABEL results in the getvar() method being called with the parameter var='VERSION_LABEL' passed to it.

See Also

@expose_page()

@set_cache_level()

The @set_cache_level() decorator permits consumers to set specific cache headers within responses. This decorator is typically used to either set cache level to never, to defeat caching for certain controllers, or to etag to take advantage of HTTP Response Code 304.

This decorator wraps the appserver/lib/util.set_cache_level() utility.

Synopsis

@set_cache_level( cache_level )

Parameters

cache_level String Cache level literal:

never = Explicitly defeat client-side caching.

etag = Apply an ETag header by MD5 hashing the body.
always = Set max caching.

Example

@route('/:namespace/*endpoint_path/', methods=['GET'])@expose_page(must_login=True) @set_cache_level('never') def listEntities(self, namespace, endpoint_path, **kwargs):

   ... elided ...

Last modified on 12 August, 2019
PREVIOUS
SplunkQuerySet
  NEXT
Templates

This documentation applies to the following versions of Splunk® Enterprise: 7.0.0, 7.0.1, 7.0.2, 7.0.3, 7.0.4, 7.0.5, 7.0.6, 7.0.7, 7.0.8, 7.0.9, 7.0.10, 7.0.11, 7.0.13, 7.1.0, 7.1.1, 7.1.2, 7.1.3, 7.1.4, 7.1.5, 7.1.6, 7.1.7, 7.1.8, 7.1.9, 7.1.10, 7.2.0, 7.2.1, 7.2.2, 7.2.3, 7.2.4, 7.2.5, 7.2.6, 7.2.7, 7.2.8, 7.2.9, 7.2.10, 7.3.0, 7.3.1, 7.3.2, 7.3.3, 7.3.4, 7.3.5, 7.3.6, 7.3.7, 7.3.8, 7.3.9


Was this documentation topic helpful?


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

Please try to keep this discussion focused on the content covered in this documentation topic. If you have a more general question about Splunk functionality or are experiencing a difficulty with Splunk, consider posting a question to Splunkbase Answers.

0 out of 1000 Characters