-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathloggersdk.py
More file actions
313 lines (246 loc) · 8.83 KB
/
loggersdk.py
File metadata and controls
313 lines (246 loc) · 8.83 KB
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
#!/usr/bin/env python
"""Unofficial Python SDK for Arcsight Logger."""
import json
import time
import requests
def post(host, url, data, verify=False, disable_warning=True):
"""Request implementation, to simplify error handling.
Args:
host (string): Hostname of Logger
url (string): URL for the API Endpoint
data (array): Payload of the body
verify (bool, optional): SSL Verification
disable_warning (bool, optional): Disable SSL warnings
Returns:
json: An array upon success, or empty if HTTP 204.
If an error is caught the error will be printed
and the application will exit.
"""
headers = {'accept': 'application/json', 'Content-Type': 'application/json'}
if disable_warning:
requests.packages.urllib3.disable_warnings()
try:
response = requests.post(
'https://' + host + url, json=data, headers=headers, verify=verify)
response.raise_for_status()
if response.status_code == 200:
if response.text:
response = json.loads(response.text)
else:
response = response.text
except requests.exceptions.HTTPError as err:
print(response.text)
print(err)
exit(1)
return response
def login(host, username, password):
"""Authenticate with the ArcSight Logger.
Args:
host (string): Hostname of Logger
username (string): Username to authenticate with
password (string): Password related to the user account
Returns:
json: On successful login it will return the related authtoken
"""
url = '/core-service/rest/LoginService/login'
payload = {
"log.login": {
"log.login": username,
"log.password": password
}
}
response = post(host, url, data=payload)
return response
def logout(host, authtoken):
"""Close the provided authtoken.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
Returns:
Null: Returns an empty HTTP 204 upon success
"""
url = '/core-service/rest/LoginService/logout'
payload = {
"log.logout": {
"log.authToken": authtoken,
}
}
response = post(host, url, data=payload)
return response
def search(host, authtoken, query, search_id, **kwargs):
"""Start a background search job.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
query (string): Which query to run
search_id (int): The search_id to be generated for the search
**kwargs: All arguments marked as optional in documentation
Returns:
json: If successful returns a sessionId, this ID is only
related if you want to find the running search
on the ArcSight Logger web interface. This is not related
to the search_id provided by the user.
"""
url = '/server/search'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
'query': query,
**kwargs
}
response = post(host, url, data=payload)
return response
def status(host, authtoken, search_id):
"""Retrieve status of an existing search.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
Returns:
json: An array showing the current status of a search
"""
url = '/server/search/status'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
}
response = post(host, url, data=payload)
return response
def wait(host, authtoken, search_id):
"""Wait until a search is finalized, checking every 5 seconds.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
Returns:
json: An array showing the current status of a search
"""
response = status(host, authtoken, search_id)
while response['status'] != 'complete':
response = status(host, authtoken, search_id)
if response['status'] != 'complete':
time.sleep(5.0)
return response
def events(host, authtoken, search_id, **kwargs):
"""Retrieve events related to a running or finalized search.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
**kwargs: All arguments marked as optional in documentation
Returns:
json: Array including the events for the related search_id
"""
url = '/server/search/events'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
**kwargs
}
response = post(host, url, data=payload)
return response
def raw_events(host, authtoken, search_id, row_ids):
"""Retrieve the raw CEF formatted events from search_id.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
row_ids (array): An array of IDs which raw events should be retrieved from
Returns:
json: An array of CEF formatted raw events related to the row_ids
"""
url = '/server/search/raw_events'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
'row_ids': [row_ids]
}
response = post(host, url, data=payload)
return response
def histogram(host, authtoken, search_id):
"""Retrieve data from a related search_id in a histogram format.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
Returns:
json: An array with bucket counts, width
and the relevant count for each bucket
"""
url = '/server/search/histogram'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
}
response = post(host, url, data=payload)
return response
def drilldown(host, authtoken, search_id, start_time, end_time):
"""Drill down a finalized search, to retrieve events from a smaller time.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
start_time (string): The starttime for the drilldown
end_time (string): The endtime for the drilldown
Returns:
Null: Returns an empty HTTP 204 response upon success
"""
url = '/server/search/drilldown'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
'start_time': start_time,
'end_time': end_time,
}
response = post(host, url, data=payload)
return response
def chart_data(host, authtoken, search_id, **kwargs):
"""Return results to be used in charts or statistics.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
**kwargs: All arguments marked as optional in documentation
Returns:
json: An array of aggregated data based on the specific search_id
"""
url = '/server/search/chart_data'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
**kwargs
}
response = post(host, url, data=payload)
return response
def stop(host, authtoken, search_id):
"""Halts an ongoing search while keeping the currently found results.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
Returns:
null: An empty HTTP 204 response upon success
"""
url = '/server/search/stop'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
}
response = post(host, url, data=payload)
return response
def close(host, authtoken, search_id):
"""Close down an ongoing or finished search, and delete it's current results.
Args:
host (string): Hostname of Logger
authtoken (string): Token for the current session
search_id (int): The search_id for the search to take action on
Returns:
null: An empty HTTP 204 response upon success
"""
url = '/server/search/close'
payload = {
'search_session_id': search_id,
'user_session_id': authtoken,
}
response = post(host, url, data=payload)
return response