Splunk Enterprise Security

How to connect the SEP api using python?

SumukhVenugopal
New Member

Hi 
I am trying to connect the SEP api via python and my code is as follows - 


# encoding = utf-8

import os
import sys
import time
import datetime
import json
import requests

import base64

'''
IMPORTANT
Edit only the validate_input and collect_events functions.
Do not edit any other part in this file.
This file is generated only once when creating the modular input.
'''
'''
# For advanced users, if you want to create single instance mod input, uncomment this method.
def use_single_instance_mode():
return True
'''

def validate_input(helper, definition):
"""Implement your own validation logic to validate the input stanza configurations"""
# This example accesses the modular input variable
# text = definition.parameters.get('text', None)
# text_1 = definition.parameters.get('text_1', None)
pass

def collect_events(helper, ew):

 opt_clientid = helper.get_arg('clientid')
 opt_clientsecret = helper.get_arg('clientsecret')
 opt_customerid = helper.get_arg('customerid')
 opt_domainid = helper.get_arg('domainid')
 opt_apihost = helper.get_arg('apihost')
 
 tokenUrl = "https://" + opt_apihost + "/v1/oauth2/tokens"
 post = []
files = []

s = requests.Session()
e = (opt_clientid + ':' + opt_clientsecret)
en = e.encode('utf-8')
en64 = base64.urlsafe_b64encode(en)
s.headers.update({ 'Accept': 'application/json' })
s.headers.update({ 'Authorization': 'Basic ' + str(en64.decode()) })
s.headers.update({ 'Content-Type': 'application/x-www-form-urlencoded' })
s.headers.update({ 'Host': opt_apihost })

f = s.post(tokenUrl, data=post, files=files, verify=False)


r = json.loads(f.text)
access_token = r['access_token']

url = "https://" + opt_apihost + "/v1/devices"
parameters = {"authorization":access_token}
final_result = []
# The following examples send rest requests to some endpoint.
response = helper.send_http_request(url, 'GET', parameters=None, payload=None,headers=parameters, cookies=None, verify=True, cert=None,timeout=None, use_proxy=True)
# get the response headers
#r_headers = response.headers
# get the response body as text
#r_text = response.text
# get response body as json. If the body text is not a json string, raise a ValueError
r_json = response.json()
for devices in r_json["devices"]:
state = helper.get_check_point(str(devices["id"]))
if state is None:
final_result.append(devices)
helper.save_check_point(str(devices["id"]), "Indexed")
event = helper.new_event(json.dumps(final_result), time=None, host=None, index=None, source=None, sourcetype=None, done=True, unbroken=False)
ew.write_event(event)

The test code works fine, but

1. The events are being indexed, each value is not appearing as a seperate entity but is rather grouped 

2. The data is not being updated in the interval mentioned in the beginning 

 

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!

A Four-Part Event Series: Full Stack Observability For the AI Era

As AI reshapes applications, infrastructure, and the way teams operate, the traditional boundaries of ...

SOC4Kafka - New Kafka Connector Powered by OpenTelemetry

The new SOC4Kafka connector, built on OpenTelemetry, enables the collection of Kafka messages and forwards ...

Event Series: Level up your SOC: Advancing with Splunk Enterprise Security

AI has fundamentally raised the stakes for security operations, and this three-part series is your guide to ...