-
Notifications
You must be signed in to change notification settings - Fork 1
/
stixelk.py
51 lines (48 loc) · 1.32 KB
/
stixelk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from cobstix2 import *
import requests
import json
from config import settings
def query(value):
try:
query_type = settings('kb')['kb_type']
except KeyError:
print "[cobstix2] Could not read kb_type from kb settings in config.ini"
sys.exit(0)
if query_type == 'elk':
_index = USER
endpoint = ELK + '%s/_search' % _index
payload = '{"query":{"query_string":{"query": "%s"}}}' % value
try:
r = requests.post(endpoint, payload)
json_content = r.json()
except requests.exceptions.RequestException as e:
print e
return False
try:
hit_list = json_content['hits']['hits']
obj_list = []
for hit in hit_list:
new_obj = dict_to_obj(hit["_source"])
obj_list.append(new_obj)
return obj_list
except KeyError:
return False
else:
return False
def put_elk(*_payloads):
results = []
for _payload in _payloads:
#print _payload
if isinstance(_payload, SDO) or isinstance(_payload, Bundle):
_index = USER
_type = _payload.type
_id = _payload.id.split('--')[1]
else:
return None
endpoint = ELK + '%s/%s/%s' % (_index, _type, _id)
try:
r = requests.put(endpoint, data=str(_payload))
results.append(r.content)
except requests.exceptions.RequestException as e:
print e
return results