splunk / splunk-sdk-python

Splunk Software Development Kit for Python
http://dev.splunk.com
Apache License 2.0
687 stars 369 forks source link

does not handle http 303 redirect "See Other" #347

Closed naisanzaa closed 2 years ago

naisanzaa commented 4 years ago

the python sdk isn't handling http 303 redirects

related: https://github.com/splunk/splunk-sdk-python/issues/311#issuecomment-696247004

splunk-sdk==1.6.13

code: https://github.com/TheShellLand/automon-core/blob/master/automon/integrations/splunk/client.py#L9-L13

import splunklib.client as client

client.connect(
            host=self.config.host,
            port=self.config.port,
            username=self.config.username,
            password=self.config.password)

Error:

>>> client.connect(
...             host=self.config.host,
...             port=self.config.port,
...             username=self.config.username,
...             password=self.config.password)
Traceback (most recent call last):
  File "/snap/pycharm-community/211/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 1, in <module>
  File "/home/jon/venv/python3/lib/python3.8/site-packages/splunklib/client.py", line 331, in connect
    s.login()
  File "/home/jon/venv/python3/lib/python3.8/site-packages/splunklib/binding.py", line 886, in login
    session = XML(body).findtext("./sessionKey")
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1320, in XML
    parser.feed(text)
  File "<string>", line None
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0

splunk config:

class SplunkConfig:
    def __init__(self, host: str = os.getenv('SPLUNK_HOST'),
                 port: int = os.getenv('SPLUNK_PORT'),
                 username: str = os.getenv('SPLUNK_USERNAME'),
                 password: str = os.getenv('SPLUNK_PASSWORD')):

        self.host = host if host else 'splunkcloud.com'
        self.port = port if port else 8090
        self.username = username if username else ''
        self.password = password if password else ''

The response body isn't xml, it's html: b'<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><meta http-equiv="refresh" content="1;url=https://server/en-US/services/auth/login"><title>303 See Other</title></head><body><h1>See Other</h1><p>The resource has moved temporarily <a href="https://server/en-US/services/auth/login">here</a>.</p></body></html>\r\n'

body = response.body.read()
session = XML(body).findtext("./sessionKey")
naisanzaa commented 4 years ago

solution: https://github.com/splunk/splunk-sdk-python/issues/217#issuecomment-421004504

though, it could have a better error messeage

flavianmissi commented 3 years ago

From the response body you're getting it looks like you had the same problem I did: using the UI port will redirect you to a path containing the displya language.

What fixed it for me was to use the management port instead (8089 in my case).

ashah-splunk commented 3 years ago

Hi @naisanzaa , could you please verify which port and scheme are being set in the configuration before calling the connect service. The connect service works properly when port = 8089 and scheme=https. Default value for scheme is 'https' if not set explicitly. Please try using the port = 8089 and scheme=https setting and let us know if you still face the error

naisanzaa commented 3 years ago

Hi @naisanzaa , could you please verify which port and scheme are being set in the configuration before calling the connect service. The connect service works properly when port = 8089 and scheme=https. Default value for scheme is 'https' if not set explicitly. Please try using the port = 8089 and scheme=https setting and let us know if you still face the error

I'll double check and add in the scheme explicitly

ashah-splunk commented 2 years ago

@naisanzaa Closing this issue due to no response on it. Please reopen if this is still an issue.