Splunk Dev

Is it possible to use TLS client certs to connect to Splunk REST API?

vinzentalves
Observer

I need to use TLS client certs to connect to Splunk REST API.

It seems this is not possible out of the box with client.connet() as per the docs here: docs.splunk.com/DocumentationStatic/PythonSDK/1.6.5/client.html#splunklib.client.connect - there is no cert/key param.

Is it somehow possible to use TLS client certs with the Splunk Python SDK ?

Labels (3)
0 Karma

harsmarvania57
Ultra Champion

Hi,

You can achieve this using splunklib.six module. Below is sample script which connects to splunk server and retrieve sessionKey and then you can use that session_key to do other work in same script.

import sys
sys.path.append('splunk-sdk-python-1.6.5')
import splunklib.six as six
import urllib
from xml.etree import ElementTree
import getpass

HOST = raw_input("Enter splunk server hostname/ip: ")
PORT = 8089

splunkUser = raw_input("Enter Splunk Username: ")
splunkPassword = getpass.getpass("Enter Password: ")

connection = six.moves.http_client.HTTPSConnection(HOST, PORT)
body = urllib.urlencode({'username': splunkUser, 'password': splunkPassword})
headers = {'Content-Type': "application/x-www-form-urlencoded",
           'Host': HOST
          }

connection.request("POST", "/services/auth/login", body, headers)
response = connection.getresponse()
content = response.read()
connection.close()

session_key = ElementTree.XML(content).findtext("./sessionKey")

As per my knowledge (I am not at advanced level of python) in above script six.moves.http_client.HTTPSConnection is using httplib module which has key_file and cert_file parameter so you can use those parameter for TLS connections.

Help on class HTTPSConnection in splunklib.six.moves.http_client:

splunklib.six.moves.http_client.HTTPSConnection = class HTTPSConnection(HTTPConnection)
 |  This class allows communication via SSL.
 |
 |  Methods defined here:
 |
 |  __init__(self, host, port=None, key_file=None, cert_file=None, strict=None, timeout=<object object>, source_address=None, context=None, check_hostname=None)
 |
 |  connect(self)
 |      Connect to a host on a given (SSL) port.
 |
0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...