Knowledge Management

Why am I getting errors trying to access kvstore endpoint from a django view?

DanielAden
Explorer

I am trying to add to/delete from a key/value store I created called kvstore for an app called kvtest. The idea is the user enters info into a django form, and then through a post, a save view gets the user information and makes a call to kvstore's rest endpoint. However, every time I try out my code I get this error:

2015-02-23 12:42:12,516 ERROR runwsgiserver:185 - Internal Server Error: /dj/en-us/kvtest/save/
Traceback (most recent call last):
  File "/Applications/Splunk/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Applications/Splunk/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Applications/Splunk/etc/apps/kvtest/django/kvtest/views.py", line 52, in save
    owner='nobody', app='kvtest')
  File "/Applications/Splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 235, in wrapper
    return request_fun(self, *args, **kwargs)
  File "/Applications/Splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 61, in new_f
    val = f(*args, **kwargs)
  File "/Applications/Splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 719, in request
    'body': body})
  File "/Applications/Splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 1099, in request
    raise HTTPError(response)
  File "/Applications/Splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 879, in __init__
    detail = XML(body).findtext("./messages/msg")
  File "/Applications/Splunk/lib/python2.7/xml/etree/ElementTree.py", line 1300, in XML
    parser.feed(text)
  File "/Applications/Splunk/lib/python2.7/xml/etree/ElementTree.py", line 1642, in feed
    self._raiseerror(v)
  File "/Applications/Splunk/lib/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
    raise err
ParseError: syntax error: line 1, column 0
:set nonu

Here is my view code:

  def save(request):
      service = request.service
      headers = [('Content-Type', 'application/json')]

      form = SetupForm(request.POST)

      form.is_valid()
      data = form.cleaned_data


      payload = {
          'name': data['name'],
          'username': data['username'],
          'interval': data['interval']
      }

      payload = json.dumps(payload)    
      r = service.request('/storage/collections/data/kvstore', method='POST', headers=headers, body=payload,
                 owner='nobody', app='kvtest')


      return HttpResponseRedirect('/dj/en-us/kvtest/home/')

Everything works up until the service.request call, which is where the error seems to be coming from.
This is just a test, so ignore the lack of error checking, but I am at a loss as to what is going wrong.

0 Karma
1 Solution

dgladkikh_splun
Splunk Employee
Splunk Employee

Which version of splunk python sdk are you using?

I used this script to verify python sdk using latest from https://github.com/splunk/splunk-sdk-python

from splunklib.binding import httperror
import splunklib.client as client
import splunklib.results as results
import json

# admin user settings
splunk_user = 'admin'
splunk_pswd = 'changeme'
splunk_host = 'localhost'
splunk_port = 8089
splunk_app = 'search'
splunk_owner = 'nobody'

collections_conf_endpoint = 'storage/collections/config/'
collections_data_endpoint = 'storage/collections/data/'

collection_name = 'kvstore'

admin_service = client.connect(
    host = splunk_host,
    port = splunk_port,
    username = splunk_user,
    password = splunk_pswd,
    app = splunk_app,
    owner = splunk_owner
)

payload = {
    'name': 'name',
    'username': 'username',
    'interval': 'interval'
}

payload = json.dumps(payload) 

# as an 'nobody' add data to the kv store (app 'search')
request2 = admin_service.request(
    collections_data_endpoint + collection_name,
    method='post',
    headers=[('content-type', 'application/json')], 
    body=payload,
    owner='nobody',
    app='search'
)

print request2

it is working as expected

$ python kvstore.py
{'status': 201, 'headers': [('content-length', '35'), ('cache-control', 'no-store, no-cache, must-revalidate, max-age=0'), ('content-type', 'application/json; charset=UTF-8'), ('x-content-type-options', 'nosniff'), ('expires', 'Thu, 26 Oct 1978 00:00:00 GMT'), ('date', 'Tue, 24 Feb 2015 17:15:16 GMT'), ('server', 'Splunkd'), ('connection', 'Close'), ('vary', 'Authorization'), ('x-frame-options', 'SAMEORIGIN')], 'reason': 'Created', 'body': <splunklib.binding.ResponseReader object at 0x10abf2f50>}

After that I verified that objects are exist (I run this script manually few times, so I have more than one object)

$ curl -k -u admin:changeme https://outcoldx:8089/servicesNS/nobody/search/storage/collections/data/kvstore/
[ { "username" : "Username", "interval" : "Interval", "name" : "Name", "_user" : "nobody", "_key" : "54ecb0e22dbbb62b95054241" }, { "interval" : "Interval", "username" : "Username", "name" : "Name", "_user" : "nobody", "_key" : "54ecb1a42dbbb62b95054242" } ]%

View solution in original post

0 Karma

dgladkikh_splun
Splunk Employee
Splunk Employee

Which version of splunk python sdk are you using?

I used this script to verify python sdk using latest from https://github.com/splunk/splunk-sdk-python

from splunklib.binding import httperror
import splunklib.client as client
import splunklib.results as results
import json

# admin user settings
splunk_user = 'admin'
splunk_pswd = 'changeme'
splunk_host = 'localhost'
splunk_port = 8089
splunk_app = 'search'
splunk_owner = 'nobody'

collections_conf_endpoint = 'storage/collections/config/'
collections_data_endpoint = 'storage/collections/data/'

collection_name = 'kvstore'

admin_service = client.connect(
    host = splunk_host,
    port = splunk_port,
    username = splunk_user,
    password = splunk_pswd,
    app = splunk_app,
    owner = splunk_owner
)

payload = {
    'name': 'name',
    'username': 'username',
    'interval': 'interval'
}

payload = json.dumps(payload) 

# as an 'nobody' add data to the kv store (app 'search')
request2 = admin_service.request(
    collections_data_endpoint + collection_name,
    method='post',
    headers=[('content-type', 'application/json')], 
    body=payload,
    owner='nobody',
    app='search'
)

print request2

it is working as expected

$ python kvstore.py
{'status': 201, 'headers': [('content-length', '35'), ('cache-control', 'no-store, no-cache, must-revalidate, max-age=0'), ('content-type', 'application/json; charset=UTF-8'), ('x-content-type-options', 'nosniff'), ('expires', 'Thu, 26 Oct 1978 00:00:00 GMT'), ('date', 'Tue, 24 Feb 2015 17:15:16 GMT'), ('server', 'Splunkd'), ('connection', 'Close'), ('vary', 'Authorization'), ('x-frame-options', 'SAMEORIGIN')], 'reason': 'Created', 'body': <splunklib.binding.ResponseReader object at 0x10abf2f50>}

After that I verified that objects are exist (I run this script manually few times, so I have more than one object)

$ curl -k -u admin:changeme https://outcoldx:8089/servicesNS/nobody/search/storage/collections/data/kvstore/
[ { "username" : "Username", "interval" : "Interval", "name" : "Name", "_user" : "nobody", "_key" : "54ecb0e22dbbb62b95054241" }, { "interval" : "Interval", "username" : "Username", "name" : "Name", "_user" : "nobody", "_key" : "54ecb1a42dbbb62b95054242" } ]%
0 Karma

DanielAden
Explorer

That seems pretty similar to what I am trying to do, but you have the username and password hardcoded into your script. I am trying to perform this from a django view without hardcoding the username and password. Do you know if this is possible?

0 Karma

dgladkikh_splun
Splunk Employee
Splunk Employee

Could you try in request "service.request('/storage/collections/data/kvstore' ... )" remove first "/" from url, so it will be

r = service.request('/storage/collections/data/kvstore', method='POST', headers=headers, body=payload,
                  owner='nobody', app='kvtest')

Just guessing that this could be the issue, as you actually asks to make a call to the root of splunk services.

DanielAden
Explorer

Hey that worked. Of course it would be a small mistake. Thanks for the help!

0 Karma
Get Updates on the Splunk Community!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...