-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CheckPointDome9.py
2211 lines (1724 loc) · 80.7 KB
/
CheckPointDome9.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
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import demistomock as demisto # noqa: F401
from CommonServerPython import * # noqa: F401
# type: ignore
# mypy: ignore-errors
from typing import Any, Dict, Tuple
TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
class Client(BaseClient):
def __init__(self, base_url: str, key_id: str, key_secret: str, proxy: bool, verify: bool):
self.base_url = base_url
headers = {'Content-Type': 'application/json'}
auth = (key_id, key_secret)
super().__init__(base_url=base_url, auth=auth, verify=verify, headers=headers, proxy=proxy)
def access_lease_list_request(self) -> Dict[str, Any]:
""" Get Access lease list.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('GET', 'AccessLease')
def access_lease_delete_request(self, lease_id: str) -> str:
""" Delete an access lease by ID.
Args:
lease_id (str): The access lease ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('DELETE', f'AccessLease/{lease_id}', resp_type='text')
def access_lease_invitation_list_request(self, invitation_id: str = None) -> Dict[str, Any]:
""" Get Access lease invitation list.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(invitation_id)
response = self._http_request('GET', f'AccessLeaseInvitation{url_prefix}')
return response
def access_lease_invitation_delete_request(self, invitation_id: str) -> str:
""" Delete an access lease invitation by ID.
Args:
invitation_id (str): The access lease invitation ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('DELETE',
f'AccessLeaseInvitation/{invitation_id}',
resp_type='text')
def findings_search_request(self,
max_fetch: int,
alert_severity: List[str] = None,
alert_region: List[str] = None,
alert_entity_type: List[str] = None,
alert_acknowledged: bool = None) -> Dict[str, Any]:
""" Search findings.
Filter findings by account, region, VPC, IP, or instance name.
Returns:
Dict[str, Any]: API response from Dome9.
"""
fields = []
for severity in (alert_severity or []):
fields.append({"name": "severity", "value": severity})
for region in (alert_region or []):
fields.append({"name": "region", "value": region})
for entity_type in (alert_entity_type or []):
fields.append({"name": "entityTypeByEnvironmentType", "value": entity_type})
if alert_acknowledged:
fields.append({"name": "acknowledged", "value": alert_acknowledged})
data = {
"pageSize": max_fetch,
"sorting": {
"fieldName": "createdTime",
"direction": 1
},
"filter": {
"fields": fields
}
}
response = self._http_request('POST', 'Compliance/Finding/search', json_data=data)
return response
def ip_list_create_request(self,
name: str,
description: str,
items: List[Dict[str, Any]] = None) -> Dict[str, Any]:
""" Create a new IP list.
Args:
name (str): The new IP list name.
description (str): IP list description.
items (list, optional): List of IP address and IP comment.
Returns:
Dict[str, Any]: API response from Dome9.
"""
items = items or ''
data = {"description": description, "items": items, "name": name}
return self._http_request('POST', 'IpList', json_data=data)
def ip_list_update_request(self,
list_id: str,
description: str,
items: List[Dict[str, Any]] = None) -> str:
""" Update exist IP list.
Args:
list_id (str): The IP list ID.
description (str): The new IP list description.
items (list, optional): The new IP-list items (IP address and comment).
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"description": description, "items": items}
response = self._http_request('PUT', f'IpList/{list_id}', json_data=data, resp_type='text')
return response
def ip_list_get_request(self, list_id: str) -> Dict[str, Any]:
""" Get an IP List by ID.
Args:
list_id (str): The IP-list ID to fetch.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(list_id)
return self._http_request(method='GET', url_suffix=f'IpList{url_prefix}')
def ip_list_delete_request(self, list_id: str) -> str:
""" Delete an IP List by id.
Args:
list_id (str): The IP-list ID to delete.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('DELETE', f'IpList/{list_id}', resp_type='text')
def ip_list_metadata_list_request(self) -> Dict[str, Any]:
""" Get all IP addresses metadata.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('GET', 'IpAddressMetadata')
def ip_list_metadata_create_request(
self,
cidr: str,
name: str,
classification: str,
) -> Dict[str, Any]:
""" Add a new IP address metadata. An Ip Address metadata must contain CIDR, Name and Classification.
Classification can be External or Unsafe or Dmz or InternalVpc or InternalDc or NoClassification.
Args:
cidr (str): The IP address CIDR.
name (str): The IP address Name.
classification (str): The IP address classification.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"cidr": cidr, "classification": classification, "name": name}
response = self._http_request('POST', 'IpAddressMetadata', json_data=data)
return response
def ip_list_metadata_update_request(
self,
list_id: str,
classification: str,
name: str,
) -> Dict[str, Any]:
""" Update an existing IP address metadata.
Classification can only be External or Unsafe or Dmz or InternalVpc or InternalDc or NoClassification.
Args:
list_id (str): The IP address internal ID.
classification (str): The IP address classification.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"classification": classification, "id": list_id, "name": name}
response = self._http_request('PUT', 'IpAddressMetadata', json_data=data)
return response
def ip_list_metadata_delete_request(
self,
account_id: str,
address: str,
mask: int,
) -> str:
""" Delete an IP address metadata with a specific CIDR.
Args:
account_id (str): The account ID.
address (str): The IP address to delete.
mask (int): The subnet mask.
Returns:
Dict[str, Any]: API response from Dome9.
"""
params = {"accountId": account_id, "address": address, "mask": mask}
response = self._http_request('DELETE',
'IpAddressMetadata',
params=params,
resp_type='text')
return response
def compliance_remediation_get_request(self) -> Dict[str, Any]:
""" Get a list of remediation for the account.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('GET', 'ComplianceRemediation')
def compliance_remediation_create_request(self, ruleset_id: str, rule_logic_hash: str,
comment: str, cloudbots: list) -> Dict[str, Any]:
""" Add a new remediation.
Args:
ruleset_id (str): Ruleset ID to apply remediation on.
rule_logic_hash (str): Hash for the rule logic.
comment (str): Comment text.
cloudbots (list): Cloud bots execution expressions.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {
"rulesetId": ruleset_id,
"ruleLogicHash": rule_logic_hash,
"cloudBots": cloudbots,
"comment": comment
}
return self._http_request('POST', 'ComplianceRemediation', json_data=data)
def compliance_remediation_update_request(
self,
remediation_id: str,
ruleset_id: str,
rule_logic_hash: str,
comment: str,
cloudbots: list,
) -> Dict[str, Any]:
""" Update a remediation.
Args:
remediation_id (str): Remediation ID.
ruleset_id (str): Ruleset ID.
rule_logic_hash (str): Hash for the rule logic.
comment (str): Comment text.
cloudbots (list): Cloud bots execution expressions.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {
"cloudBots": cloudbots,
"comment": comment,
"id": remediation_id,
"rulesetId": ruleset_id,
"ruleLogicHash": rule_logic_hash
}
response = self._http_request('PUT', 'ComplianceRemediation', json_data=data)
return response
def compliance_remediation_delete_request(self, remediation_id: str) -> str:
""" Delete a remediation by ID.
Args:
remediation_id (str): Remediation ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('DELETE',
f'ComplianceRemediation/{remediation_id}',
resp_type='text')
def compliance_ruleset_list_request(self) -> Dict[str, Any]:
""" Get all rulesets for the account.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('GET', 'Compliance/Ruleset/view')
def compliance_ruleset_rule_list_request(self, rule_id: int) -> Dict[str, Any]:
""" Get rule details (get rule logic hash).
Args:
rule_id (int): The Ruleset ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
return self._http_request('GET', f'Compliance/Ruleset/{rule_id}')
def security_group_attach_request(
self,
instance_id: str,
sg_id: str,
nic_name: str,
) -> Dict[str, Any]:
""" Attach security Group to an AWS EC2 Instance.
Args:
instance_id (str): AWS Instance ID.
sg_id (str): AWS security group ID.
nic_name (str): The instance niCs name.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"groupid": sg_id, "nicname": nic_name}
response = self._http_request('POST',
f'cloudinstance/{instance_id}/sec-groups',
json_data=data)
return response
def instance_list_request(self, instance_id: str) -> Dict[str, Any]:
""" Get an AWS EC2 Instances list.
Args:
instance_id (str): AWS Instance ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(instance_id)
response = self._http_request('GET', f'cloudinstance{url_prefix}')
return response
def security_group_service_delete_request(self, sg_id: str, service_id: str) -> str:
""" Delete security group service by ID.
Args:
sg_id (str): Security group ID.
service_id (str): Service ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('DELETE',
f'cloudsecuritygroup/{sg_id}/services/Inbound/{service_id}',
resp_type='text')
return response
def security_group_tags_update_request(self, sg_id: str, key: str,
value: str) -> Dict[str, Any]:
""" Create and Update a security group tag.
Args:
sg_id (str): Security group ID.
key (str): The tag key to add.
value (str): The tag value to add.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {'tags': {key: value}}
response = self._http_request('POST', f'cloudsecuritygroup/{sg_id}/tags', json_data=data)
return response
def security_group_service_create_request(
self,
sg_id: str,
name: str,
protocol_type: str,
port: int,
policy_type: str = None,
open_for_all: bool = None,
description: str = None,
data_id: str = None,
data_name: str = None,
scope_type: str = None,
is_valid: bool = None,
inbound: bool = None,
icmptype: str = None,
icmpv6type: str = None,
) -> Dict[str, Any]:
""" Create new security group service.
Args:
sg_id (str): AWS Security group ID.
name (str): Service name.
protocol_type (str): The Service protocol type.
port (int): The service port, indicates a port range.
open_for_all (bool, optional): Indicates if the service is open to all ports. Defaults to None.
description (str, optional): Service description. Defaults to None.
data_id (str, optional): IP List ID to attach. Defaults to None.
data_name (str, optional): IP List name to attach. Defaults to None.
scope_type (str, optional): Scope type (CIDR / IPList) to attach. Defaults to None.
is_valid (bool, optional): _description_. Defaults to None.
inbound (bool, optional): _description_. Defaults to None.
icmptype (str, optional): ICMP type (when protocol is ICMP). Defaults to None.
icmpv6type (str, optional): ICMP V6 type (when protocol is ICMPV6). Defaults to None.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = remove_empty_elements({
"description":
description,
"icmpType":
icmptype,
"icmpv6Type":
icmpv6type,
"inbound":
inbound,
"name":
name,
"openForAll":
open_for_all,
"port":
port,
"protocolType":
protocol_type,
"scope": [{
"data": {
"id": data_id,
"name": data_name
},
"isValid": is_valid,
"type": scope_type
}]
})
response = self._http_request('POST',
f'cloudsecuritygroup/{sg_id}/services/{policy_type}',
json_data=data)
return response
def security_group_service_update_request(
self,
sg_id: str,
service_name: str,
protocol_type: str,
port: int,
policy_type: str,
open_for_all: bool = None,
description: str = None,
data_id: str = None,
data_name: str = None,
scope_type: str = None,
is_valid: bool = None,
inbound: bool = None,
icmptype: str = None,
icmpv6type: str = None,
) -> Dict[str, Any]:
""" Update security group service.
Args:
sg_id (str): AWS Security group ID.
service_name (str): Service name.
protocol_type (str): The Service protocol type.
port (int): The service port, indicates a port range.
open_for_all (bool, optional): Indicates if the service is open to all ports. Defaults to None.
description (str, optional): Service description. Defaults to None.
data_id (str, optional): IP List ID to attach. Defaults to None.
data_name (str, optional): IP List name to attach. Defaults to None.
scope_type (str, optional): Scope type (CIDR / IPList) to attach. Defaults to None.
is_valid (bool, optional): _description_. Defaults to None.
inbound (bool, optional): _description_. Defaults to None.
icmptype (str, optional): ICMP type (when protocol is ICMP). Defaults to None.
icmpv6type (str, optional): ICMP V6 type (when protocol is ICMPV6). Defaults to None.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = remove_empty_elements({
"description":
description,
"icmpType":
icmptype,
"icmpv6Type":
icmpv6type,
"inbound":
inbound,
"name":
service_name,
"openForAll":
open_for_all,
"port":
port,
"protocolType":
protocol_type,
"scope": [{
"data": {
"id": data_id,
"name": data_name
},
"isValid": is_valid,
"type": scope_type
}]
})
response = self._http_request('PUT',
f'cloudsecuritygroup/{sg_id}/services/{policy_type}',
json_data=data)
return response
def security_group_instance_detach_request(
self,
instance_id: str,
sg_id: str,
nic_name: str,
) -> Dict[str, Any]:
""" Detach security Group from an AWS EC2 Instance.
Args:
instance_id (str): AWS Instance ID.
sg_id (str): AWS security group ID.
nic_name (str): The instance niCs name.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"groupid": sg_id, "nicname": nic_name}
response = self._http_request('DELETE',
f'cloudinstance/{instance_id}/sec-groups',
json_data=data)
return response
def protection_mode_update_request(self, sg_id: str, protection_mode: str) -> Dict[str, Any]:
""" Change the protection mode for an AWS security group (FullManage or ReadOnly).
Args:
sg_id (str): The security group ID.
protection_mode (str): The protection mode to update.
Returns:
Dict[str, Any]: API response from Dome9.
"""
data = {"protectionMode": protection_mode}
response = self._http_request('POST',
f'cloudsecuritygroup/{sg_id}/protection-mode',
json_data=data)
return response
def cloud_accounts_list_request(self, account_id: str = None) -> Dict[str, Any]:
""" Get cloud accounts list.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(account_id)
response = self._http_request('GET', f'CloudAccounts{url_prefix}')
return response
def check_ip_list_security_group_attach_request(self, sg_id: str = None) -> Dict[str, Any]:
""" Get AWS cloud accounts for a specific security group and region and
check if there is an IP-list that attach to a security group.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(sg_id)
response = self._http_request('GET', f'CloudSecurityGroup{url_prefix}')
return response
def security_group_list_request(self) -> Dict[str, Any]:
""" Get security group list.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('GET', 'AwsSecurityGroup')
return response
def global_search_get_request(self) -> Dict[str, Any]:
""" Get top results for each service.
Returns:
Dict[str, Any]: API response from Dome9.
"""
params = {"freeText": "String"}
response = self._http_request('GET', 'GlobalSearch', params=params)
return response
def cloud_trail_get_request(self) -> Dict[str, Any]:
""" Get Cloud Trail events for a Dome9 user.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('GET', 'CloudTrail')
return response
def findings_bundle_get_request(self, bundle_id: str, rule_logic_hash: str) -> Dict[str, Any]:
""" Get the findings for a specific rule in a bundle, for all of the user's accounts.
Args:
bundle_id (str): The bundle ID.
rule_logic_hash (str): MD5 hash of the rule GSL string.
Returns:
Dict[str, Any]: API response from Dome9.
"""
params = {"ruleLogicHash": rule_logic_hash, "pageSize": '100', "pageNumber": '1'}
response = self._http_request('GET',
f'Compliance/Finding/bundle/{bundle_id}',
params=params)
return response
def finding_get_request(self, finding_id: str) -> Dict[str, Any]:
""" Get a findings by its ID.
Args:
finding_id (str): The findings ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('GET', f'Compliance/Finding/{finding_id}')
return response
def organizational_unit_view_get_request(self) -> Dict[str, Any]:
""" Get organizational unit view entities.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('GET', 'organizationalunit/view')
return response
def organizational_unit_flat_get_request(self) -> Dict[str, Any]:
""" Get all organizational units flat.
Returns:
Dict[str, Any]: API response from Dome9.
"""
response = self._http_request('GET', 'organizationalunit/GetFlatOrganizationalUnits')
return response
def organizational_unit_get_request(self, unit_id: str) -> Dict[str, Any]:
""" Get an organizational unit by its ID.
Args:
unit_id (str): The organizational unit ID.
Returns:
Dict[str, Any]: API response from Dome9.
"""
url_prefix = create_url_prefix(unit_id)
response = self._http_request('GET', f'OrganizationalUnit{url_prefix}')
return response
def attach_comment_to_ip(ip_list: List[str],
comment_list: List[str],
description: str = None) -> List:
""" Insure comment_list has the same length as ip_list
and description or ip is specified.
Args:
ip_list (_type_): The IP list.
comment_list (_type_): The comment list.
description (_type_): List description.
Raises:
ValueError: Description or ip must be provided.
Returns:
List: Items List.
"""
items = []
if ip_list:
while len(ip_list) > len(comment_list):
comment_list.append('')
for ip, comment in zip(ip_list, comment_list):
items.append({'ip': ip, 'comment': comment})
else:
if not description:
raise ValueError('description or ip must be provided.')
return items
def validate_pagination_arguments(page: int = None, page_size: int = None, limit: int = None):
""" Validate pagination arguments according to their default.
Args:
page (int, optional): Page number of paginated results.
page_size (int, optional): Number of ip-list per page.
limit (int, optional): The maximum number of records to retrieve.
Raises:
ValueError: Appropriate error message.
"""
if page_size:
if page_size < 1 or page_size > 50:
raise ValueError('page size argument must be greater than 1 and smaller than 50.')
if page:
if page < 1:
raise ValueError('page argument must be greater than 0.')
if limit:
if limit < 1 or limit > 50:
raise ValueError('limit argument must be greater than 1.')
def pagination(response: dict, args: Dict[str, Any]) -> Tuple:
""" Executing Manual Pagination (using the page and page size arguments)
or Automatic Pagination (display a number of total results).
Args:
response (dict): API response.
page (int, optional): Page number of paginated results.
page_size (int, optional): Number of ip-list per page.
limit (int, optional): The maximum number of records to retrieve.
Returns:
dict: output and pagination message for Command Results.
"""
page = arg_to_number(args.get('page'))
page_size = arg_to_number(args.get('page_size'))
limit = arg_to_number(args.get('limit'))
validate_pagination_arguments(page, page_size, limit)
output = response
if page and page_size:
if page_size < len(response):
first_item = page_size * (page - 1)
output = response[first_item:first_item + page_size]
else:
output = response[:page_size]
pagination_message = f'Showing page {page}. \n Current page size: {page_size}'
else:
output = response[:limit]
pagination_message = f'Showing {len(output)} rows out of {len(response)}.'
return output, pagination_message
def arg_to_boolean(arg: str) -> Optional[bool]:
""" Retrieve arg boolean value if it's not none.
Args:
arg (str): Boolean argument.
Returns:
Optional[bool]: The argument boolean value.
"""
return argToBoolean(arg) if arg else None
def get_service_type_and_data(service: list) -> list:
""" Get security group service type and data.
Args:
service (list): Inbound or Outbound service.
Returns:
str: service type and data.
"""
service_type = service_data = service_scope = ''
if service:
service_scope = service[0]['scope']
if service_scope:
service_type = service_scope[0]['type']
service_data = service_scope[0]['data']
return service_type, service_data
def create_url_prefix(path_variable: str) -> str:
""" Create url prefix for request.
Args:
path_variable (str): The path variable.
Returns:
str: URL prefix.
"""
url_prefix = f'/{path_variable}' if path_variable else ''
return url_prefix
def create_sg_list(fix_output: list) -> list:
""" Create Security Group list according to a general template.
Args:
fix_output (list): Security group list to edit.
Returns:
list: A new Security Group list according to function format.
"""
security_group_list = []
for sg in fix_output:
inbound_services = sg['services']['inbound']
inbound_services_type, inbound_services_data = get_service_type_and_data(inbound_services)
outbound_services = sg['services']['outbound']
outbound_services_type, outbound_services_data = get_service_type_and_data(
outbound_services)
security_group_list.append({
'cloud_account_id': sg['cloudAccountId'],
'cloud_account_name': sg['cloudAccountName'],
'region_id': sg['regionId'],
'security_group_external_id': sg['externalId'],
'security_group_id': sg['securityGroupId'],
'security_group_name': sg['securityGroupName'],
'isProtected': sg['isProtected'],
'inbound_scope_type': inbound_services_type,
'inbound_scope_data': inbound_services_data,
'outbound_scope_type': outbound_services_type,
'outbound_scope_data': outbound_services_data,
'description': sg['description'],
'vpc_id': sg['vpcId']
})
return security_group_list
def access_lease_list_command(client: Client, args: Dict[str, Any]) -> CommandResults:
""" Get a access lease list.
Args:
client (Client): Dome9 API client.
args (Dict[str, Any]): Command arguments from XSOAR.
Returns:
CommandResults: outputs, readable outputs and raw response for XSOAR.
"""
response = client.access_lease_list_request()
output = response['aws']
fix_output, pagination_message = pagination(output, args)
readable_output = tableToMarkdown(
name='Access Lease:',
metadata=pagination_message,
t=fix_output,
headers=['id', 'name', 'ip', 'user', 'region', 'length', 'created'],
headerTransform=string_to_table_header)
command_results = CommandResults(readable_output=readable_output,
outputs_prefix='CheckPointDome9.AccessLease',
outputs_key_field='id',
outputs=output,
raw_response=response)
return command_results
def access_lease_delete_command(client: Client, args: Dict[str, Any]) -> CommandResults:
""" Delete access lease by ID.
Args:
client (Client): Dome9 API client.
args (Dict[str, Any]): Command arguments from XSOAR.
Returns:
CommandResults: outputs, readable outputs and raw response for XSOAR.
"""
lease_id = args.get('lease_id')
response = client.access_lease_delete_request(lease_id)
command_results = CommandResults(readable_output="Access Lease Deleted successfully",
outputs_prefix='CheckPointDome9.AccessLease',
outputs_key_field='',
outputs=response,
raw_response=response)
return command_results
def access_lease_invitation_list_command(client: Client, args: Dict[str, Any]) -> CommandResults:
""" Get a specific lease invitation.
Args:
client (Client): Dome9 API client.
args (Dict[str, Any]): Command arguments from XSOAR.
Returns:
CommandResults: outputs, readable outputs and raw response for XSOAR.
"""
invitation_id = args.get('invitation_id')
response = client.access_lease_invitation_list_request(invitation_id)
fix_output, pagination_message = pagination(response, args)
readable_output = tableToMarkdown(
name='Access Lease invitation',
metadata=pagination_message,
t=fix_output,
headers=['id', 'issuerName', 'recipientName', 'length', 'created'],
headerTransform=string_to_table_header)
command_results = CommandResults(readable_output=readable_output,
outputs_prefix='CheckPointDome9.AccessLease.Invitation',
outputs_key_field='id',
outputs=response,
raw_response=response)
return command_results
def access_lease_invitation_delete_command(client: Client, args: Dict[str, Any]) -> CommandResults:
""" Delete a lease invitation.
Args:
client (Client): Dome9 API client.
args (Dict[str, Any]): Command arguments from XSOAR.
Returns:
CommandResults: outputs, readable outputs and raw response for XSOAR.
"""
invitation_id = args.get('invitation_id')
response = client.access_lease_invitation_delete_request(invitation_id)
command_results = CommandResults(readable_output="Access Lease Invitation Deleted successfully",
outputs_prefix='CheckPointDome9.AccessLease.Invitation',
outputs_key_field='',
outputs=response,
raw_response=response)
return command_results
def findings_search_command(client: Client, args: Dict[str, Any]) -> CommandResults:
""" Search findings for the account.
Args:
client (Client): Dome9 API client.
args (Dict[str, Any]): Command arguments from XSOAR.
Returns:
CommandResults: outputs, readable outputs and raw response for XSOAR.
"""
severity = argToList(args.get('severity'))
acknowledged = arg_to_boolean(args.get('acknowledged'))
entity_type = argToList(args.get('entity_type'))
region = argToList(args.get('region'))
page_size = arg_to_number(args.get('limit'))
response = client.findings_search_request(page_size, severity, region, entity_type,
acknowledged)
output = response['findings']
fix_output, pagination_message = pagination(output, args)
readable_output = tableToMarkdown(name='Findings:',
metadata=pagination_message,
t=fix_output,