Splunk Dev

XML errore when using python SDK on SPlunk balanced url

maurelio79
Communicator

Hi to all, i get a strange errorr using python SDK when connecting to balanced url instead of single host.

HOST = "splunk.balanced.url"
PORT = 8089
USERNAME = "admin"
PASSWORD = "changeme"
SCHEME = "https"
APP = "my_app"

# Create a Service instance and log in to Splunk

service = client.connect(
          host=HOST,
          port=PORT,
          username=USERNAME,
          password=PASSWORD,
          scheme=SCHEME,
          app=APP
          )

for app in service.apps:
    print app.name

But i got this error:

Traceback (most recent call last):
  File "test_connection.py", line 32, in <module>
    verify=False
  File "/opt/splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/client.py", line 288, in connect
    return Service(**kwargs).login()
  File "/opt/splunk/etc/apps/framework/contrib/splunk-sdk-python/splunklib/binding.py", line 753, in login
    session = XML(body).findtext("./sessionKey")
  File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 964, in XML
    return parser.close()
  File "/usr/lib64/python2.6/xml/etree/ElementTree.py", line 1254, in close
    self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 1, column 0

If i connect directly to the splunk host search head i got correct result: all app name.
Bhind the balanced url there is only one host at the moment (the same host from which i get correct result).
Someone got a similar problem? Could be a certificate issue? It seems that the XML returned is not correct.

Thanks very much and regards

Labels (2)
0 Karma

harsmarvania57
Ultra Champion

When you say balanced URL, do you mean Load-balancer URL ? If yes then have you configured VIP on port 8089 in LB ?

0 Karma

maurelio79
Communicator

Yes LOAD balancer URL and a simple curl with -k it's working.
It seems that the response is an empty body with SDK

0 Karma

harsmarvania57
Ultra Champion

It looks like you are running splunk-sdk-python which ships with splunk itself and while looking at version of that SDK it is very old. I'll suggest you to download latest Splunk Python SDK from https://github.com/splunk/splunk-sdk-python/releases

If it will not work with latest version of Splunk Python SDK then, can you please run below command and check whether you are receiving sessionKey or not.

curl -vk https://your_lb_url:8089/services/auth/login --data-urlencode username=your_username --data-urlencode password='your_password'
0 Karma

maurelio79
Communicator

I tried and i got a session key, so what it means? An old sdk version? But why the problem is present only if i pass via LB URL? It's very strange

0 Karma

harsmarvania57
Ultra Champion

So it worked with new splunk python sdk ?

0 Karma

maurelio79
Communicator

We have python 2.6.6, so i'm not sure i can test the new sdk, it requires python > 2.7

0 Karma

harsmarvania57
Ultra Champion

Maybe it's due to python 2.6 , if you have any other host which is running python 2.7 then I'll suggest to try on that server.

0 Karma

maurelio79
Communicator

For the moment we "solved" the issue modifyng the file binding.py.
Inside function handler we modified the function reqeust from

 def request(url, message, **kwargs):
        scheme, host, port, path = _spliturl(url)
        body = message.get("body", "")
        head = {
            "Content-Length": str(len(body)),
            "Host": host,
            "User-Agent": "splunk-sdk-python/0.1",
            "Accept": "*/*",
        } # defaults
        for key, value in message["headers"]:
            head[key] = value
        method = message.get("method", "GET")

        connection = connect(scheme, host, port)
        try:
            connection.request(method, path, body, head)
            if timeout is not None:
                connection.sock.settimeout(timeout)
            response = connection.getresponse()
        finally:
            connection.close()


        return {
            "status": response.status,
            "reason": response.reason,
            "headers": response.getheaders(),
            "body": ResponseReader(response),
        }

to

 def request(url, message, **kwargs):
        scheme, host, port, path = _spliturl(url)
        body = message.get("body", "")
        head = {
            "Content-Length": str(len(body)),
            "Host": host,
            "User-Agent": "splunk-sdk-python/0.1",
            "Accept": "*/*",
        } # defaults
        for key, value in message["headers"]:
            head[key] = value
        method = message.get("method", "GET")

        connection = connect(scheme, host, port)
        try:
            connection.request(method, path, body, head)
            if timeout is not None:
                connection.sock.settimeout(timeout)
            response = connection.getresponse()
        finally:
        #    connection.close()
            pass

        return {
            "status": response.status,
            "reason": response.reason,
            "headers": response.getheaders(),
            "body": ResponseReader(response),
        }

Basically we removed the connection.close()

0 Karma

maurelio79
Communicator

Still not tried. It worked the curl retreiving the session key. I will try with new SDK and write here the result

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...