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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...