forked from mbabinski/InsightIDR4Py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsightIDR4Py.py
More file actions
837 lines (729 loc) · 30.5 KB
/
InsightIDR4Py.py
File metadata and controls
837 lines (729 loc) · 30.5 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
import requests, json, time
from datetime import datetime, timedelta, timezone
def GetDefaultStartTime():
"""
Get default start time for time-based queries.
"""
default_start_time = (datetime.now(timezone.utc) - timedelta(28)).strftime("%Y-%m-%dT%H:%M:%SZ")
return default_start_time
def GetDefaultEndTime():
"""
Get default end time (now) for time-based queries.
"""
default_end_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
return default_end_time
class InsightIDR(object):
def __init__(self, api_key, region=None):
self.session = requests.Session()
self.session.headers = {"X-Api-Key": api_key}
if not region:
self.region = self._get_region()
else:
self.region = region
self.logs_url = "https://{}.rest.logs.insight.rapid7.com/query/logs/".format(self.region)
self.query_url = "https://{}.rest.logs.insight.rapid7.com/query/".format(self.region)
self.log_mgmt_url = "https://{}.api.insight.rapid7.com/log_search/management/logs/".format(self.region)
self.investigations_url = "https://{}.api.insight.rapid7.com/idr/v2/investigations/".format(self.region)
self.comments_url = "https://{}.api.insight.rapid7.com/idr/v1/comments/".format(self.region)
self.threat_url = "https://{}.api.insight.rapid7.com/idr/v1/customthreats/".format(self.region)
self.mgmt_url = "https://{}.rest.logs.insight.rapid7.com/management/".format(self.region)
def _get_region(self):
"""
This method cycles through available API regions, making a call to the log management URL with each
region until a successful call indicates the correct region. If you already know your region, simply
pass that in when creating the InsightIDR object.
"""
self.regions = ["us", "us2", "us3", "eu", "ca", "au", "ap"]
for region in self.regions:
self.response = self.session.get("https://{}.rest.logs.insight.rapid7.com/management/logs".format(region))
if self.response.status_code == 200:
return region
def GetLogInfo(self):
"""Returns metadata about the available log sources."""
response = self.session.get(self.log_mgmt_url).json()["logs"]
return response
def ListLogSetNames(self):
"""Returns a list of logset names as they appear in the InsightIDR console."""
log_info = self.GetLogInfo()
logset_names = list(set([log["logsets_info"][0]["name"] for log in log_info]))
return sorted(logset_names)
def ListLogIdsByLogSetName(self, logset_name):
"""Returns a list of log ID values for a given logset name."""
log_info = self.GetLogInfo()
log_ids = [log["id"] for log in log_info if log["logsets_info"][0]["name"].upper() == logset_name.upper()]
return log_ids
def QueryEvents(self, logset_name, query, time_range="Last 20 Minutes", from_time=None, to_time=None, suppress_msgs=True):
"""
Returns an ordered list of events matching a given timeframe, logset name, and query. Must supply
either a relative time range or from time and to time in the format MM/DD/YYYY Hr:Min:Sec.
"""
# convert from/to times as necessary (string to timestamp with milliseconds)
if not time_range:
from_time = int(datetime.strptime(from_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
to_time = int(datetime.strptime(to_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
# get the relevant Log IDs
log_ids = self.ListLogIdsByLogSetName(logset_name)
# get the time range
if time_range:
during = {"time_range": time_range}
else:
during = {"from": from_time, "to": to_time}
body = {"logs": log_ids,
"leql": {"during": during,
"statement": query}}
# build the first full URL
url = self.logs_url + "?per_page=500"
# retrieve the data
run = True
events = []
cntr = 1
r = self.session.post(url, json=body)
while run:
if r.status_code == 202:
cont = True
while cont:
continue_url = r.json()["links"][0]["href"]
r = self.session.get(continue_url, headers=headers)
if r.status_code != 202:
cont = False
break
elif r.status_code == 200:
events.extend(r.json()["events"])
if "links" in r.json():
continue_url = r.json()["links"][0]["href"]
r = self.session.get(continue_url, headers=headers)
else:
run = False
else:
raise ValueError("Query failed without a normal status code. Status code returned was: " + str(r.status_code))
return
cntr += 1
if not suppress_msgs:
if cntr % 30 == 0:
print("-Gathered {} events.".format(str(len(events))))
# filter the event objects to get just the dictionary representation of the event data
events = [json.loads(event["message"]) for event in events]
return events
def QueryGroups(self, logset_name, query, time_range="Last 20 Minutes", from_time=None, to_time=None, suppress_msgs=True):
"""
Retrieves group values and associated stats. Query must contain a groupby() clause
"""
# validate input query
if not "groupby(" in query.lower():
raise ValueError("Query must contain the groupby() clause!")
# convert from/to times as necessary (string to timestamp with milliseconds)
if not time_range:
from_time = int(datetime.strptime(from_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
to_time = int(datetime.strptime(to_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
# get the relevant Log IDs
log_ids = self.ListLogIdsByLogSetName(logset_name)
# get the time range
if time_range:
during = {"time_range": time_range}
else:
during = {"from": from_time, "to": to_time}
body = {"logs": log_ids,
"leql": {"during": during,
"statement": query}}
# build the first full URL
url = self.logs_url
# retrieve the data
run = True
results = []
cntr = 1
r = self.session.post(url, json=body)
while run:
if r.status_code == 202:
cont = True
while cont:
continue_url = r.json()["links"][0]["href"]
r = self.session.get(continue_url)
if r.status_code != 202:
cont = False
break
elif r.status_code == 200:
if "links" in r.json():
continue_url = r.json()["links"][0]["href"]
r = self.session.get(continue_url)
else:
results.extend(r.json()["statistics"]["groups"])
run = False
else:
raise ValueError("Query failed without a normal status code. Status code returned was: " + str(r.status_code))
return
cntr += 1
if not suppress_msgs:
if cntr % 30 == 0:
print("-Gathered {} groups.".format(str(len(results))))
groups = {}
for result in results:
key = list(result.keys())[0]
value = int(result[key]["count"])
groups[key] = value
return groups
def ListInvestigations(self,
assignee_email=None,
start_time=GetDefaultStartTime(),
end_time=GetDefaultEndTime(),
multi_customer=False,
priorities=["LOW", "MEDIUM", "HIGH", "CRITICAL"],
sort="priority,DESC",
sources=None,
statuses=["OPEN", "INVESTIGATING", "CLOSED"],
tags=None):
"""
Queries InsightIDR investigations based on available filter criteria.
"""
# list to hold investigations
investigations = []
# pre-process parameters
priorities = ", ".join(priorities)
statuses = ",".join(statuses)
if tags:
tags = ", ".join(tags)
params = {
"index": 0,
"size": 100,
"assignee.email": assignee_email,
"start_time": start_time,
"end_time": end_time,
"multi-customer": multi_customer,
"priorities": priorities,
"sort": sort,
"sources": sources,
"statuses": statuses,
"tags": tags
}
# filter the parameters to be only those with a supplied value
params = {key:val for key, val in params.items() if val}
# get the initial set of investigations
url = self.investigations_url
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.get(url, params=params)
result = response.json()
# get the total
total = result["metadata"]["total_data"]
# add the results to the output list
investigations.extend(result["data"])
# iterate through remaining investigations and add them to the output list
while len(investigations) < total:
params["index"] += 100
response = self.session.get(url, params)
result = response.json()
investigations.extend(result["data"])
# return the result
return investigations
def GetInvestigation(self, investigation_id, multi_customer=False):
"""
Retrieves a single investigation by Investigation ID/RRN
"""
url = self.investigations_url + "{}".format(investigation_id)
params = {"multi-customer": multi_customer}
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.get(url, params=params)
result = response.json()
return result
def CreateInvestigation(self, title, assignee_email=None, disposition="UNDECIDED",
priority="LOW", status="OPEN"):
"""
Creates an InsightIDR investigation.
"""
if assignee_email:
assignee = {"email": assignee_email}
else:
assignee = None
data = {
"title": title,
"assignee": assignee,
"disposition": disposition,
"priority": priority,
"status": status
}
# filter the parameters to be only those with a supplied value
data = {key:val for key, val in data.items() if val}
# submit the request
url = self.investigations_url
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.post(url, json=data)
result = response.json()
return result
def CloseInvestigationsInBulk(self, source, from_time=GetDefaultStartTime(),
to_time=GetDefaultEndTime(), alert_type=None,
disposition=None, detection_rule_rrn=None,
max_investigations_to_close=None):
"""
Closes investigations in bulk according to selected criteria.
"""
data = {
"source": source.upper(),
"from": from_time,
"to": to_time,
"alert_type": alert_type,
"disposition": disposition,
"detection_rule_rrn": detection_rule_rrn,
"max_investigations_to_close": max_investigations_to_close
}
# validate input
if source.upper() not in ("ALERT", "MANUAL", "HUNT"):
raise ValueError("Source must be one of [ALERT, MANUAL, or HUNT]!")
if source.upper() == "ALERT" and not alert_type:
raise ValueError("The alert_type parameter is required when source is ALERT!")
if detection_rule_rrn and alert_type != "Attacker Behavior Detected":
raise ValueError("If a detection rule RRN is specified, the alert type must be 'Attacker Behavior Detected'")
# filter the parameters to be only those with a supplied value
data = {key:val for key, val in data.items() if val}
# submit the request
url = self.investigations_url + "bulk_close"
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.post(url, json=data)
result = response.json()
return result
def ListAlertsByInvestigation(self, investigation_id, multi_customer=False):
"""
Retrieves all alerts associated with an investigation. The listed alerts are sorted in descending order by alert create time.
"""
alerts = []
url = self.investigations_url + "{}/alerts".format(investigation_id)
params = {
"index": 0,
"size": 100,
"multi-customer": multi_customer
}
# make initial request
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.get(url, params=params)
result = response.json()
# get the total
total = result["metadata"]["total_data"]
# add the results to the output list
alerts.extend(result["data"])
# iterate through remaining alerts and add them to the output list
while len(alerts) < total:
params["index"] += 100
response = self.session.get(url, params)
result = response.json()
alerts.extend(result["data"])
return alerts
def ListRapid7ProductAlertsByInvestigation(self, investigation_id, multi_customer=False):
"""
Retrieves all Rapid7 product alerts associated with an investigation. These alerts are generated by Rapid7 products other
than InsightIDR that you have an active license for.
"""
product_alerts = []
url = self.investigations_url + "{}/rapid7-product-alerts".format(investigation_id)
params = {"multi-customer": multi_customer}
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.get(url, params=params)
result = response.json()
return result
def UpdateInvestigation(self, investigation_id, multi_customer=False, assignee_email=None, disposition=None, priority=None,
status=None, threat_command_close_reason=None, threat_command_free_text=None, title=None):
"""
Updates multiple fields in a single operation for an investigation, specified by id or rrn.
The investigation will be returned with its changed fields. Null or omitted fields will not have their values
updated in the investigation.
"""
if assignee_email:
assignee = {"email": assignee_email}
else:
assignee = None
params = {"multi-customer": multi_customer}
data = {
"title": title,
"assignee": assignee,
"disposition": disposition,
"priority": priority,
"status": status,
"threat_command_close_reason": threat_command_close_reason,
"threat_command_free_text": threat_command_free_text
}
# submit the request
url = self.investigations_url + investigation_id
self.session.headers["Accept-version"] = "investigations-preview"
response = self.session.patch(url, json=data, params=params)
result = response.json()
return result
def ListCommentsByInvestigation(self, investigation_rrn):
"""
Returns a list of comments filtered by a specific investigation with a given rrn.
"""
comments = []
url = self.comments_url
params = {
"index": 0,
"size": 100,
"target": investigation_rrn
}
# make initial request
self.session.headers["Accept-version"] = "comments-preview"
response = self.session.get(url, params=params)
result = response.json()
# get the total
total = result["metadata"]["total_data"]
# add the results to the output list
comments.extend(result["data"])
# iterate through remaining alerts and add them to the output list
while len(comments) < total:
params["index"] += 100
response = self.session.get(url, params)
result = response.json()
comments.extend(result["data"])
return comments
def CreateComment(self, investigation_rrn, comment_text):
"""
Creates a comment on an investigation.
"""
data = {
"attachments": [], # attachments not yet supported
"body": comment_text,
"target": investigation_rrn
}
url = self.comments_url
self.session.headers["Accept-version"] = "comments-preview"
response = self.session.post(url, json=data)
result = response.json()
return result
def DeleteComment(self, comment_rrn):
"""
Deletes a comment identified by its rrn value.
"""
url = self.comments_url + "{}".format(comment_rrn)
response = self.session.delete(url)
return response
def CreateThreat(self, threat_name, threat_description, indicators={}):
"""
CreateS a private InsightIDR Community Threat and optionally adds indicators to this Community Threat.
Indicator types can include IP addresses, hashes, domain names, or URLs.
"""
data = {
"threat": threat_name,
"note": threat_description,
"indicators": indicators,
}
url = self.threat_url
response = self.session.post(url, json=data)
result = response.json()
return result
def AddIndicatorsToThreat(self, threat_key, ips=None, domains=None, hashes=None, urls=None):
"""
Adds indicators to a threat based off threat key. The threat key can be obtained on the threat page in the InsightIDR GUI.
Only JSON formatted indicators are supported at this time.
"""
params = {"format": "json"}
data = {}
if ips:
data["ips"] = ips
if domains:
data["domain_names"] = domains
if hashes:
data["hashes"] = hashes
if urls:
data["urls"] = urls
url = self.threat_url + "key/{}/indicators/add".format(threat_key)
response = self.session.post(url, params=params, json=data)
result = response.json()
return result
def ReplaceThreatIndicators(self, threat_key, ips=None, domains=None, hashes=None, urls=None):
"""
Replaces indicators in a threat abased off threat key. The threat key can be obtained on the threat page in the InsightIDR GUI.
Only JSON formatted indicators are supported at this time.
"""
params = {"format": "json"}
data = {}
if ips:
data["ips"] = ips
if domains:
data["domain_names"] = domains
if hashes:
data["hashes"] = hashes
if urls:
data["urls"] = urls
url = self.threat_url + "key/{}/indicators/replace".format(threat_key)
response = self.session.post(url, params=params, json=data)
result = response.json()
return result
def DeleteThreat(self, threat_key, reason=""):
"""
Deletes an InsightIDR Community Threat. The threat key can be obtained on the threat page in the InsightIDR GUI.
"""
data = {"reason": reason}
url = self.threat_url + "/key/{}/delete".format(threat_key)
response = self.session.post(url, json=data)
result = response.json()
return result
def ListCustomAlerts(self):
"""
Lists all 'Tags' aka alerts, associated with the account.
"""
url = self.mgmt_url + "tags"
response = self.session.get(url)
result = response.json()
alerts = result["tags"]
return alerts
def GetCustomAlert(self, custom_alert_id):
"""
Retrieve a single custom alert by its ID value
"""
url = self.mgmt_url + "tags/{}".format(custom_alert_id)
response = self.session.get(url)
result = response.json()
alert = result["tag"]
return alert
def CreateCustomAlert(self, name, logsource, query, description="", actions=[], labels=[],
priority="low"):
"""
Create a custom alert. If log source is set to an array of logsource IDs, these will be used. If a logset name is provided,
the log IDs for this log set will be retrieved and used.
"""
# validate input
if not priority.lower() in ("low", "medium", "high", "critical"):
raise ValueError("Priority must be one of [low, medium, high, critical]!")
# map alert priority to numeric value
priority_mapping = {
"low": 1,
"medium": 2,
"high": 3,
"critical": 4
}
priority = priority_mapping[priority.lower()]
# list logsource IDs
if type(logsource) == list:
logsource_ids = [{"id": item} for item in logsource]
else:
logsource_ids = [{"id": item} for item in self.ListLogIdsByLogSetName(logsource)]
# format parameters
data = {
"tag": {
"name": name,
"sources": logsource_ids,
"type": "Alert",
"leql": {"statement": query},
"description": description,
"actions": actions,
"labels": labels,
"priority": priority
}
}
# send request
self.session.headers["Content-type"] = "application/json"
url = self.mgmt_url + "tags"
response = self.session.post(url, json=data)
result = response.json()
return result
def ReplaceCustomAlert(self, alert_id, name, logsource, query, description="", actions=[], labels=[],
priority="low"):
"""
Replaces a custom alert identified by the alert_id value. If log source is set to an array of logsource IDs, these will be used.
If a logset name is provided, the log IDs for this log set will be retrieved and used.
"""
# validate input
if not priority.lower() in ("low", "medium", "high", "critical"):
raise ValueError("Priority must be one of [low, medium, high, critical]!")
# map alert priority to numeric value
priority_mapping = {
"low": 1,
"medium": 2,
"high": 3,
"critical": 4
}
priority = priority_mapping[priority.lower()]
# list logsource IDs
if type(logsource) == list:
logsource_ids = [{"id": item} for item in logsource]
else:
logsource_ids = [{"id": item} for item in self.ListLogIdsByLogSetName(logsource)]
# format parameters
data = {
"tag": {
"name": name,
"sources": logsource_ids,
"type": "Alert",
"leql": {"statement": query},
"description": description,
"actions": actions,
"labels": labels,
"priority": priority
}
}
# send request
self.session.headers["Content-type"] = "application/json"
url = self.mgmt_url + "tags/{}".format(alert_id)
response = self.session.put(url, json=data)
result = response.json()
return result
def UpdateCustomAlert(self, alert_id, name=None, logsource=None, query=None, description=None, actions=None, labels=None,
priority=None):
"""
Updates any user-specified element of a custom alert, identified by the alert_id
"""
if priority:
# validate input
if not priority.lower() in ("low", "medium", "high", "critical"):
raise ValueError("Priority must be one of [low, medium, high, critical]!")
# map alert priority to numeric value
priority_mapping = {
"low": 1,
"medium": 2,
"high": 3,
"critical": 4
}
priority = priority_mapping[priority.lower()]
# list logsource IDs
if logsource:
if type(logsource) == list:
logsource_ids = [{"id": item} for item in logsource]
else:
logsource_ids = [{"id": item} for item in self.ListLogIdsByLogSetName(logsource)]
else:
logsource_ids = None
if query:
leql_obj = {"statement": query}
else:
leql_obj = None
# format parameters
params = {
"name": name,
"sources": logsource_ids,
"type": "Alert",
"leql": leql_obj,
"description": description,
"actions": actions,
"labels": labels,
"priority": priority
}
params = {k:v for k, v in params.items() if v}
data = {"tag": params}
# send request
self.session.headers["Content-type"] = "application/json"
url = self.mgmt_url + "tags/{}".format(alert_id)
response = self.session.patch(url, json=data)
result = response.json()
return result
def DeleteCustomAlert(self, alert_id):
"""
Deletes the custom alert identified by the alert_id
"""
url = self.mgmt_url + "tags/{}".format(alert_id)
response = self.session.delete(url)
return response
def ListSavedQueries(self):
"""
Lists saved queries in the InsightIDR platform.
"""
url = self.query_url + "saved_queries"
response = self.session.get(url)
result = response.json()
queries = result["saved_queries"]
return queries
def GetSavedQuery(self, saved_query_id):
"""
Retrieve details on a single saved query.
"""
url = self.query_url + "saved_queries/{}".format(saved_query_id)
response = self.session.get(url)
result = response.json()["saved_query"]
return result
def CreateSavedQuery(self, name, query, logset_name=None, time_range="Last 20 Minutes", from_time=None, to_time=None):
"""
Creates a Saved Query.
"""
# convert from/to times as necessary (string to timestamp with milliseconds)
if not time_range:
from_time = int(datetime.strptime(from_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
to_time = int(datetime.strptime(to_time, "%m/%d/%Y %H:%M:%S").timestamp()) * 1000
# get the relevant Log IDs
if logset_name:
log_ids = self.ListLogIdsByLogSetName(logset_name)
else:
log_ids = []
# get the time range
if time_range:
during = {"time_range": time_range}
else:
during = {"from": from_time, "to": to_time}
data = {
"saved_query": {
"name": name,
"leql": {
"statement": query,
"during": during
},
"logs": log_ids
}
}
# make the request
url = self.query_url + "saved_queries"
response = self.session.post(url, json=data)
result = response.json()
return result
def ReplaceSavedQuery(self, saved_query_id, name, query, logset_name=None, time_range=None, from_time=None, to_time=None):
"""
Replace an existing saved query with the parameters specified in the input.
"""
# get the relevant Log IDs (updated or keep as existing if not set)
if logset_name:
log_ids = self.ListLogIdsByLogSetName(logset_name)
else:
query_obj = self.GetSavedQuery(saved_query_id)
log_ids = query_obj["logs"]
# get the time range
if time_range:
during = {"time_range": time_range}
elif from_time and to_time:
during = {"from": from_time, "to": to_time}
else:
during = None
data = {
"saved_query": {
"name": name,
"leql": {
"statement": query,
"during": during
},
"logs": log_ids
}
}
# make the reuest
url = self.query_url + "saved_queries/{}".format(saved_query_id)
response = self.session.put(url, json=data)
result = response.json()
return result
def UpdateSavedQuery(self, saved_query_id, name=None, query=None, logset_name=None, time_range=None, from_time=None, to_time=None):
"""
Update an existing saved query with the parameters specified in the input.
"""
# get the relevant Log IDs
if logset_name:
log_ids = self.ListLogIdsByLogSetName(logset_name)
else:
query_obj = self.GetSavedQuery(saved_query_id)
log_ids = query_obj["logs"]
# get the time range
if time_range:
during = {"time_range": time_range}
elif from_time and to_time:
during = {"from": from_time, "to": to_time}
else:
during = None
data = {
"saved_query": {
"name": name,
"leql": {
"statement": query,
"during": during
},
"logs": log_ids
}
}
# make the reuest
url = self.query_url + "saved_queries/{}".format(saved_query_id)
response = self.session.patch(url, json=data)
result = response.json()
return result
def DeleteSavedQuery(self, saved_query_id):
"""
Delete a saved query with the specified saved query ID.
"""
url = self.query_url + "saved_queries/{}".format(saved_query_id)
response = self.session.delete(url)
return response