forked from logpoint/SearchAPI
/
LogPointSearcher.py
333 lines (278 loc) · 10.9 KB
/
LogPointSearcher.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import json
import time
import Config
import requests
from Config import Config
from LogPoint import LogPoint
from Device import Device
from SearchJob import SearchJob
from Repos import Repo
from LiveSearch import LiveSearch
from Error import Error
class LogPointSearcher:
'''
Searcher class to search for following
'''
def __init__(self):
config = Config()
self.ip = config.ip
self.username = config.username
self.secert_key = config.secret_key
self.request_type = config.request_type
def get_log_points(self, raw_data = False):
'''
Returns list of LogPoint object
'''
"""
send requests to given IP using username and secret_key for type = "logpoints"
our webserver should return [{"name": "", "ip":""}]
"""
logpoints = []
response = self._get_allowed_data('loginspects', raw_data=raw_data)
if isinstance(response, Error):
return response
else:
if not response.get('success'):
return Error(response.get('message'));
allowed_logpoint = response['allowed_loginspects'];
for data in allowed_logpoint:
logpoints.append(LogPoint(data["ip"], data["name"]))
return logpoints
def get_repos(self, logpoint_object=None):
'''
Search for repos for which the user have permission to access
according to credential provided
If no parameter passed, it searches for the repos within
all logpoints
If logpoint object is passed as a parameter,
it searches for the repos within the logpoint referenced
by the logpoint object
Returns list of Repo object
'''
if not logpoint_object:
logpoint_object = []
repos = []
logpoint = {}
logpoint_list = []
for logpoint_row in logpoint_object:
logpoint_list.append({'name':logpoint_row.get_name(),'ip':logpoint_row.get_ip()})
response = self._get_allowed_data("logpoint_repos", True, logpoint_list)
if isinstance(response, Error):
return response
if not response.get('success'):
return Error(response.get('message'))
allowed_repos = response.get('allowed_repos')
response_logpoint_string = response.get('logpoint')
if response_logpoint_string:
'''
get all the logpoints
if only results have logpoint key and its value
if it is not contained, most probable the logpoint
object list is passed in the method
'''
for logpt in response_logpoint_string:
logpoint_ip = logpt.get('ip')
logpoint_name = logpt.get('name')
#TODO
# what if, instead of logpoint object,
# logpoint name is provided?
logpoint[logpoint_ip] = LogPoint(logpoint_ip, logpoint_name)
else:
'''
if logpoint objects were passed in method parameter
generate logpoint list
'''
for logpt in logpoint_object:
logpoint[logpt.get_ip()] = logpt
'''
get repo_name and its ip, and reference back to logpoint
'''
for repo in allowed_repos:
address, repo_name = repo.get('address'), repo.get('repo')
if ':' in address:
logpoint_ip, port = address.split(':')
else:
logpoint_ip = address
'''Only include the repos from logpoint list'''
if logpoint_list:
for l in logpoint_list:
if l['ip'] in logpoint_ip:
repos.append(Repo(logpoint[logpoint_ip], repo_name))
else:
repos.append(Repo(logpoint[logpoint_ip], repo_name))
return repos
def get_devices(self, logpoint_object=None):
'''
Search for devices for which the user have permission to access
according to credential provided
If no parameter passed, it searches for the devices within
all logpoints
If logpoint object is passed as a parameter,
it searches for the devices within the logpoint referenced
by the logpoint object
Returns list of Device object
'''
if not logpoint_object:
logpoint_object = []
devices = []
logpoint = {}
logpoint_list = []
for logpoint_row in logpoint_object:
logpoint_list.append({'name':logpoint_row.get_name(),'ip':logpoint_row.get_ip()})
response = self._get_allowed_data('devices', True, logpoint_list)
if isinstance(response, Error):
return response
if not response.get('success'):
return Error(response.get('message'))
allowed_devices = response['allowed_devices'];
logpoints = response['logpoint']
'''
get all the logpoints
'''
for logpt in logpoints:
logpoint_ip = logpt.get('ip')
logpoint_name = logpt.get('name')
logpoint[logpoint_ip] = LogPoint(logpoint_ip, logpoint_name)
for data in allowed_devices:
if type(data) is dict:
for row in data:
lp, ip = row.split('/')
devices.append(Device(ip, data[row], logpoint[lp]))
return devices
def get_live_searches(self):
'''
Returns list of LiveSearch object
'''
live_searches_lists = []
try:
live_searches = self._get_allowed_data('livesearches')
if live_searches.get("success"):
for live_search in live_searches["livesearches"]:
live_searches_lists.append(LiveSearch(live_search["life_id"],live_search["searchname"],live_search["query"]))#,"livesearch"))
return live_searches_lists
else:
return Error(live_searches["message"])
except Exception, e:
return Error(str(e))
def get_timezone(self):
'''
Returns timezone of user according to credential provided
'''
"""
webserver should return UTC or Asia/Kathmandu
"""
time_zone = self._get_allowed_data('user_preference')
if isinstance(time_zone, Error):
return time_zone
else:
if not time_zone.get("success"):
return Error(time_zone.get("message"))
else:
return time_zone.get("timezone")
def search(self, query, timerange=None, repo=None, timeout=30, limit=100):
'''
Returns SearchJob object
'''
return self._get_search_job(query, timerange, repo, timeout, limit)
def _get_allowed_data(self, data_type, raw_data=False, logpoints=''):
url = "%s://%s/%s" % (self.request_type, self.ip, "getalloweddata")
if logpoints and isinstance(logpoints, list):
logpoints = json.dumps(logpoints)
data = {
"username": self.username,
"secret_key": self.secert_key,
"type": data_type,
"logpoints": logpoints
}
try:
ack = requests.post(url, data=data, timeout=10.0, verify=False)
if raw_data:
print '\n\nurl=%s' % url
print 'Raw Contents\n============\n%s' % ack.content
print '\nFormatted Contents\n=====================\n'
except Exception, e:
return Error(str(e))
# print resp
# return resp
ret = ''
try:
ret = json.loads(ack.content)
except:
pass
# print ack.content
return ret
def _get_search_job(self, query, timerange=None, repo=None, timeout=None, limit=None):
SEARCH_QUERY = query
if not timerange:
SEARCH_TIME_RANGE = 'Last 10 minutes'
else:
SEARCH_TIME_RANGE = timerange
if not repo:
# TODO
# check if repo is list of string or repo object
SEARCH_REPOS = []
else:
SEARCH_REPOS = repo
if not timeout:
timeout = 10
if not limit:
RESULT_LIMIT = 30
else:
RESULT_LIMIT = limit
url = "%s://%s/%s" % (self.request_type, self.ip, "getsearchlogs")
data = {
"username":self.username,
"secret_key": self.secert_key,
"requestData": json.dumps({
"timeout": 30,
"client_name": "gui",
"repos": SEARCH_REPOS,
"starts": {},
"limit": RESULT_LIMIT,
"time_range": SEARCH_TIME_RANGE,
"query": SEARCH_QUERY
})
}
try:
ack = requests.post(url, data=data, timeout=10.0, verify=False)#verify = True => SSL certificate will be verified.
except Exception, e:
return Error(str(e))
response = json.loads(ack.content)
return SearchJob(response)
def get_response(self, search_id, version=None):
if not version:
version = 0
url = "%s://%s/%s" % (self.request_type, self.ip, "getsearchlogs")
if search_id:
data = {
"username":self.username,
"secret_key": self.secert_key,
"requestData": json.dumps({
"search_id": search_id,
"waiter_id": "%s" % time.time(),
"seen_version": version
})
}
start_time = time.time()
response = {}
while time.time() - start_time < 10.0:
try:
ack = requests.post(url, data=data, timeout=10.0, verify=False)
#TODO
# check the error if any occoured in ack.
res = json.loads(ack.content)
# print ack.content
if res.get('success'):
response = res
else:
# print res
return Error(res.get("message"))
if res['final'] == True:
break
except Exception, e:
# print 'LPS line 321', str(e)
return Error(str(e))
if not response:
return Error('No data from merger')
# print response
return response