Dashboards & Visualizations

Django Web Framework: How to retrieve the current user session token with the Python SDK?

eirik_talberg
Explorer

Hi! I've been struggling with finding any proper documentation on this issue.

We are creating a Web Framework application to perform a certain kind of search and to display the results in a certain kind of way for the user. This requires us to do some fairly custom stuff that mixes Django Bindings/Web Framework and Splunk.

We are currently routing search parameters through a regular Django view, and the application itself is inside Splunk like any other app. To comply with legal requirements, our application has to implement extensive logging on who is using the application, and to do this I need to access the current user session information for the user that is logged in.

We use LDAP authentication and we have a series of groups in Splunk to determine access level and organization membership. Basically, what I need to do is retrieve the current session token (I'm using the Python SDK) and get the username and Splunk role from this so that it can be logged. I also need the session token so that I can use the current session to launch searches on behalf of that user.

Where is the current session token located? I've tried looking that the request.META information for the HTTP Request object, but I can't find a field value that seems appropriate.

1 Solution

eirik_talberg
Explorer

Yet again I solve my own problem through trial and error.

This whole issue is absurdly simple. My error was trying to establish a session or reestablish the current one manually by using the SDK. However, upon proper reading of this page: http://dev.splunk.com/view/SP-CAAAEMP, I learned that the service object I needed to use was already a part of the HTTP Request object, only it wasn't listed when I tried to print the whole thing.

Basically, to retrieve user info (roles given as an example) and access things like REST endpoints:

@login_required
def my_view(request):
    service = request.service

    # Get current user roles
    roles = service.roles

    # Print them
    for role in roles:
        print role.name

    #Do the stuff you want and return data to a template

You can find more info on how to use REST in the Python SDK documentation: http://dev.splunk.com/python

View solution in original post

0 Karma

eirik_talberg
Explorer

Yet again I solve my own problem through trial and error.

This whole issue is absurdly simple. My error was trying to establish a session or reestablish the current one manually by using the SDK. However, upon proper reading of this page: http://dev.splunk.com/view/SP-CAAAEMP, I learned that the service object I needed to use was already a part of the HTTP Request object, only it wasn't listed when I tried to print the whole thing.

Basically, to retrieve user info (roles given as an example) and access things like REST endpoints:

@login_required
def my_view(request):
    service = request.service

    # Get current user roles
    roles = service.roles

    # Print them
    for role in roles:
        print role.name

    #Do the stuff you want and return data to a template

You can find more info on how to use REST in the Python SDK documentation: http://dev.splunk.com/python

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...