-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathGenerateAsBuilt.py
972 lines (829 loc) · 30 KB
/
GenerateAsBuilt.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
import demistomock as demisto
from CommonServerPython import *
from CommonServerUserPython import *
import jinja2
"""
GenerateAsBuilt
Uses the XSOAR API to query for custom content, configuration, and statistics, then generates
a HTML and Markdown output based on this.
"""
HTML_TABLE_TEMPLATE = """
<h3>{{ name }}</h3>
<table class="table">
{% if headers %}
<tr>
{% for header in headers %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in rows %}
<tr>
{% for header in headers %}
<td>{{ row[header] }}</td>
{% endfor %}
</tr>
{% endfor %}
{% else %}
{% for row in rows %}
{% for k,v in row.items() %}
<tr>
<td>{{ k }}</td>
<td>{{ v }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endif %}
</table>
"""
USECASE_MD_DOCUMENT_TEMPLATE = """
# {{ playbook_name }}
{{ data.playbooks.as_markdown() }}
{{ data.integrations.as_markdown() }}
{{ data.incidentfields.as_markdown() }}
{{ data.automations.as_markdown() }}
"""
MD_DOCUMENT_TEMPLATE = """
{{ open_incidents }}
{{ closed_incidents }}
{{ reports }}
{{ dashboards }}
{{ playbook_stats }}
{{ integrations_table }}
{{ installed_packs_table }}
{{ playbooks_table }}
{{ automations_table }}
{{ system_config }}
"""
USECASE_HTML_DOCUMENT_TEMPLATE = """
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"
media='all'>
</head>
<style>
td {
font-size: small;
}
.border-cortex {
border-color: #fa582d!important;
}
.text-cortex {
color: #fa582d!important;
}
</style>
<body>
<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom border-cortex">
<div class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none"
style="min-width:500px;">
<span class="fs-4">{{ playbook_name }}</span>
</div>
<div>
<img src="https://www.paloaltonetworks.com/content/dam/pan/en_US/images/logos/brand/primary-company-logo/PANW_Parent_Brand_Primary_Logo_RGB.png?imbypass=on"
height="35px">
</div>
</header>
<div class="row mb-2">
<div class="col">
<small class="text-secondary">Prepared by: {{ data.author }}</small>
</div>
<div class="col text-center">
<small class="text-secondary">Prepared For: {{ data.customer }}</small>
</div>
<div class="col text-end">
<small class="text-secondary">Prepared at: {{ data.date }}</small>
</div>
</div>
<div class="row mb-2">
<div class="col">
<h1 class="text-cortex">Purpose</h1>
<p>This Document covers all of the custom configuration and content that has been deployed following
the engagement of Palo Alto professional services.<br><br>
This document specifically covers the implementation of the provided use case/playbook, and collates
the dependencies and artefacts implemented to complete it.
<br><br>
This document only includes the dependencies used by the playbook, which may not include other configurations
such as:
</p>
<ul>
<li>Incident Classifiers</li>
<li>Incident Mappers</li>
</ul>
<h1 class="text-cortex">Document Overview</h1>
<p>
This document has been auto-generated using the XSOAR Automation <i>!GenerateAsBuilt</i>, with a
specific
playbook ({{ playbook_name }}) provided.
</p>
<h1 class="text-cortex">
Project Details
</h1>
<textarea style="width:100%;border:none;"
placeholder="Click here to complete this section with any relevant details, for example customer
contat details, project scope, etc."></textarea>
<h1 class="text-cortex">
Contact Details
</h1>
<p>
<b>Corporate Headquarters</b><br>
Palo Alto Networks<br>
3000 Tannery Way<br>
Santa Clara, CA 95054<br>
</p>
</div>
</div>
<p style="page-break-after: always;"></p>
{{ data.playbooks.as_html(["name","pack"]) }}
<p>
Playbook dependencies are subplaybooks consumed by the parent use case.
</p>
<p style="page-break-after: always;"></p>
{% if data.integrations %}
{{ data.integrations.as_html(["name","pack"]) }}
<p>
Integration dependencies are those that implement commands required by this use case.
<br><br>
The above integrations may not all be configured in this environment, but are referenced by the use case.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if data.incidenttypes %}
{{ data.incidenttypes.as_html(["name","pack"]) }}
<p>
Incident types are the types of incidents generated or used by this use-case.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if data.automations %}
{{ data.automations.as_html(["name","pack"]) }}
<p>
Automations are scripts used to perform tasks. They may be custom, or OOTB.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if data.incidentfields %}
{{ data.incidentfields.as_html(["name","pack"]) }}
<p>
Incident fields are the fields that this use case relies on to populate or consume information.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
</div>
</body>
""" # noqa: E501
HTML_DOCUMENT_TEMPLATE = """
<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"
media='all'>
</head>
<style>
td {
font-size: small;
}
.border-cortex {
border-color: #fa582d!important;
}
.text-cortex {
color: #fa582d!important;
}
</style>
<body>
<div class="container">
<header class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom border-cortex">
<div class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none"
style="min-width:500px;">
<span class="fs-4 display-1">XSOAR as built</span>
</div>
<div>
<img src="https://www.paloaltonetworks.com/content/dam/pan/en_US/images/logos/brand/primary-company-logo/PANW_Parent_Brand_Primary_Logo_RGB.png?imbypass=on"
height="35px">
</div>
</header>
<div class="row mb-2">
<div class="col">
<small class="text-secondary">Prepared by: {{ author }}</small>
</div>
<div class="col text-center">
<small class="text-secondary">Prepared For: {{ customer }}</small>
</div>
<div class="col text-end">
<small class="text-secondary">Prepared at: {{ date }}</small>
</div>
</div>
<div class="row mb-2">
<div class="col">
<h1 class="text-cortex">Purpose</h1>
<p>This Document covers all of the custom configuration and content that has been deployed following
the engagement of Palo Alto professional services.<br>
</p>
<h1 class="text-cortex">Document Overview</h1>
<p>
This document has been auto-generated using the XSOAR Automation <i>!GenerateAsBuilt</i>.
</p>
<h1 class="text-cortex">
Project Details
</h1>
<textarea style="width:100%;border:none;"
placeholder="Click here to complete this section with any relevant details, for example customer
contat details, project scope, etc."></textarea>
<h1 class="text-cortex">
Contact Details
</h1>
<p>
<b>Corporate Headquarters</b><br>
Palo Alto Networks<br>
3000 Tannery Way<br>
Santa Clara, CA 95054<br>
</p>
</div>
</div>
<p style="page-break-after: always;"></p>
<h1 class="text-cortex text-center">Project Statistics Summary</h1>
<div class="row mb-2">
<div class="col text-center p-2 border-bottom border-cortex">
{{ open_incidents }}
</div>
<div class="col text-center p-2 border-bottom border-cortex">
{{ closed_incidents }}
</div>
</div>
{{ playbook_stats }}
<p>
The playbook statistics represent how many incidents have been ingested and associated with
a given playbook.
<br>This is a good indicator of how each use case is being consumed.
</p>
<p style="page-break-after: always;"></p>
<h1 class="text-cortex text-center">All Installed Content</h1>
{{ integrations_table }}
<p>The system configuration above represents the server configuration of XSOAR, including advanced
server config parameters such as HTTP proxy. This configuration is accessible via
settings->about->troubleshooting.</p>
<p style="page-break-after: always;"></p>
{% if installed_packs_table %}
{{ installed_packs_table }}
<p>The installed packs are all the content packs currently installed on the server. Content packs
include playbooks, automations, and in some cases, integrations.</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if playbooks_table %}
{{ playbooks_table }}
<p>
Custom playbooks are written specifically for this deployment, or adapted from existing OOTB playbooks.
The tasks in each playbook represent the overall size and complexity of each developed playbook.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if automations_table %}
{{ automations_table }}
<p>
Custom automations are used to add additional logic and support to playbooks and use cases.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if reports %}
{{ reports }}
<p>
Custom reports can be scheduled or manually initiated reports that report any statistic available through
XSOAR.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{% if dashboards %}
{{ dashboards }}
<p>
Custom dashboards are a dynamic way to visualize statistics within XSOAR.<br><br>
XSOAR ships with a number of Out Of the Box dashboards, the above respresent only
those that have been created as part of this PS engagement.
</p>
<p style="page-break-after: always;"></p>
{% endif %}
{{ system_config }}
<p>
The system configuration changes the behavior of the XSOAR server/application itself, including
keys and the published hostname.
</p>
<p style="page-break-after: always;"></p>
</div>
</body>
""" # noqa: E501
class TableData:
def __init__(self, data, name):
self.data = data
self.name = name
def as_markdown(self, headers=None):
if not headers:
return tableToMarkdown(self.name, self.data, removeNull=True)
else:
return tableToMarkdown(self.name, self.data, headers=headers, removeNull=True)
def as_html(self, headers=None):
template = jinja2.Template(HTML_TABLE_TEMPLATE)
rows = self.data
if type(self.data) is dict:
rows = []
for k, v in self.data.items():
rows.append({k: v})
return template.render(headers=headers, rows=rows, name=self.name)
def total(self):
return len(self.data)
def search(self, search_key, search_value):
for row in self.data:
if search_key in row:
value = row.get(search_key)
if value == search_value:
return row
return None
class SortedTableData(TableData):
def __init__(self, data, name, sort_key):
sorted_data = sorted(data, key=lambda i: i[sort_key].lower())
super().__init__(sorted_data, name)
class SingleFieldData:
def __init__(self, name, data):
self.data = data
self.name = name
def __bool__(self):
return True
def as_markdown(self):
return f"### {self.name}\n{self.data}"
def as_html(self):
return f"""<h3>{self.name}</h3><span class="display-5">{self.data}</span>"""
class NoneTableData:
"""
Empty data type, returns e
"""
def __bool__(self):
return False
def as_markdown(self, *args, **kwargs): # noqa: F841
return ""
def as_html(self, *args, **kwargs): # noqa: F841
return ""
class UseCaseDocument:
"""
Generates a "use case" document, that is, a document that collates the dependencies and requirements of a
given playbook ("use case") within a running XSOAR environment.
"""
def __init__(
self,
playbook_name,
dependencies,
author,
customer
): # pragma: no cover
self.playbook_name = playbook_name
self.automations = dependencies.get("automation", NoneTableData())
self.integrations = dependencies.get("integration", NoneTableData())
self.playbooks = dependencies.get("playbook", NoneTableData())
self.incidentfields = dependencies.get("incidentfield", NoneTableData())
self.incidenttypes = dependencies.get("incidenttype", NoneTableData())
self.author = author
self.date = datetime.now().strftime("%m/%d/%Y")
self.customer = customer
def markdown(self):
template = jinja2.Template(USECASE_MD_DOCUMENT_TEMPLATE)
return template.render(
playbook_name=self.playbook_name,
data=self
)
def html(self):
template = jinja2.Template(USECASE_HTML_DOCUMENT_TEMPLATE)
return template.render(
playbook_name=self.playbook_name,
data=self
)
class Document:
"""
General Platform as-built document - designed to collate all of the configuration and settings of a running XSOAR
instance and is not Specific t any given use case.
"""
def __init__(
self,
template,
integrations_table,
installed_packs_table,
playbooks_table,
automations_table,
system_config,
open_incidents,
closed_incidents,
playbook_stats,
reports,
dashboards,
author,
customer
): # pragma: no cover
self.template = template
self.integrations_table = integrations_table
self.installed_packs_table = installed_packs_table
self.playbooks_table = playbooks_table
self.automations_table = automations_table
self.system_config = system_config
self.open_incidents = open_incidents
self.closed_incidents = closed_incidents
self.playbook_stats = playbook_stats
self.author = author
self.date = datetime.now().strftime("%m/%d/%Y")
self.customer = customer
self.reports = reports
self.dashboards = dashboards
def html(self):
template = jinja2.Template(HTML_DOCUMENT_TEMPLATE)
return template.render(
integrations_table=self.integrations_table.as_html(["name", "brand"]),
installed_packs_table=self.installed_packs_table.as_html(["name", "currentVersion"]),
playbooks_table=self.playbooks_table.as_html(["name", "TotalTasks"]),
automations_table=self.automations_table.as_html(["name", "comment"]),
system_config=self.system_config.as_html(),
open_incidents=self.open_incidents.as_html(),
closed_incidents=self.closed_incidents.as_html(),
author=self.author,
date=self.date,
customer=self.customer,
playbook_stats=self.playbook_stats.as_html(headers=["playbook", "incidents"]),
reports=self.reports.as_html(headers=["name", "type"]),
dashboards=self.dashboards.as_html(headers=["name", "shared"])
)
def markdown(self):
template = jinja2.Template(MD_DOCUMENT_TEMPLATE)
return template.render(
integrations_table=self.integrations_table.as_markdown(["name", "brand"]),
installed_packs_table=self.installed_packs_table.as_markdown(["name", "currentVersion"]),
playbooks_table=self.playbooks_table.as_markdown(["name", "TotalTasks"]),
automations_table=self.automations_table.as_markdown(["name", "comment"]),
system_config=self.system_config.as_markdown(),
open_incidents=self.open_incidents.as_markdown(),
closed_incidents=self.closed_incidents.as_markdown(),
playbook_stats=self.playbook_stats.as_markdown(headers=["playbook", "incidents"]),
reports=self.reports.as_markdown(headers=["name", "type"]),
dashboards=self.dashboards.as_markdown(headers=["name", "shared"]),
)
def build_document(
template,
integrations_table,
installed_packs_table,
playbooks_table,
automations_table,
system_config
):
template = jinja2.Template(template)
return template.render(
integrations_table=integrations_table.as_html(),
installed_packs_table=installed_packs_table,
playbooks_table=playbooks_table,
automations_table=automations_table,
system_config=system_config
)
def post_api_request(url, body):
"""Post API request.
Args:
url (str): request url path
body (Dict): integration command / script arguments
Returns:
Dict: dictionary representation of the response
"""
api_args = {
"uri": url,
"body": body
}
raw_res = demisto.executeCommand("core-api-post", api_args)
try:
res = raw_res[0]['Contents']['response']
return res
except KeyError:
return_error(f'API Request failed, no response from API call to {url}')
except TypeError:
return_error(f'API Request failed, failed to {raw_res}')
def get_api_request(url):
"""Get API request.
Args:
url (str): request url path
Returns:
Dict: dictionary representation of the response
"""
api_args = {
"uri": url
}
raw_res = demisto.executeCommand("core-api-get", api_args)
try:
res = raw_res[0]['Contents']['response']
# If it's a string and not an object response, means this command has failed.
if type(res) is str:
return None
return res
except KeyError:
return_error(f'API Request failed, no response from API call to {url}')
def get_all_incidents(days=7, size=1000):
"""Get all incidents from API request.
Args:
days (int): number of days. Defaults to 7.
size (int): number of incidents. Defaults to 1000.
Returns:
Dict: incidents returned from the API request
"""
body = {
"userFilter": False,
"filter": {
"page": 0,
"size": int(size),
"query": "-category:job",
"sort": [
{
"field": "id",
"asc": False
}
],
"period": {
"by": "day",
"fromValue": int(days)
}
}
}
r = post_api_request("/incidents/search", body)
return r.get("data")
def get_open_incidents(days=7, size=1000):
"""Get open incidents from API.
Args:
days (int): number of days. Defaults to 7.
size (int): number of incidents. Defaults to 1000.
Returns:
SingleFieldData: SingleFieldData object representing open incidents
"""
body = {
"userFilter": False,
"filter": {
"page": 0,
"size": int(size),
"query": "-status:closed -category:job",
"sort": [
{
"field": "id",
"asc": False
}
],
"period": {
"by": "day",
"fromValue": int(days)
}
}
}
r = post_api_request("/incidents/search", body)
total = r.get("total")
rd = SingleFieldData(f"Open Incidents {days} days", total)
return rd
def get_closed_incidents(days=7, size=1000):
"""Get closed incidents from API.
Args:
days (int): number of days. Defaults to 7.
size (int): number of incidents. Defaults to 1000.
Returns:
SingleFieldData: SingleFieldData object representing closed incidents
"""
body = {
"userFilter": False,
"filter": {
"page": 0,
"size": int(size),
"query": "status:closed -category:job",
"sort": [
{
"field": "id",
"asc": False
}
],
"period": {
"by": "day",
"fromValue": int(days)
}
}
}
r = post_api_request("/incidents/search", body)
total = r.get("total")
rd = SingleFieldData(f"Closed Incidents {days} days", total)
return rd
def get_enabled_integrations(max_request_size: int):
"""Retrieve all the running instances.
Args:
max_request_size (int): maximum number of instances to retrieve.
Returns:
SortedTableData: TableData object with the enabled instances.
"""
r = post_api_request("/settings/integration/search", {"size": max_request_size})
instances = r.get("instances")
enabled_instances = []
for instance in instances:
if instance.get("enabled"):
enabled_instances.append(instance)
rd = SortedTableData(enabled_instances, "Enabled Instances", "name")
return rd
def get_installed_packs():
"""Get all the installed Content Packs
Returns:
SortedTableData: TableData object with the installed Content Packs.
"""
# if tis doesn't work, return nothing.
r = get_api_request("/contentpacks/metadata/installed")
if not r:
return NoneTableData()
else:
return SortedTableData(r, "Installed Content Packs", "name")
def get_custom_playbooks():
"""Return all the custom playbooks installed in XSOAR
Returns:
SortedTableData: TableData object with the custom playbooks.
"""
r = post_api_request("/playbook/search", {"query": "system:F AND hidden:F"}).get("playbooks")
for pb in r:
pb["TotalTasks"] = len(pb.get("tasks", []))
rd = SortedTableData(r, "Custom Playbooks", "name")
return rd
def get_custom_reports():
"""
Return all the custom reports installed in XSOAR.
:return: TableData
"""
r = get_api_request("/reports")
reports = []
for report in r:
# Check it's not an inbuilt (system) report
if not report.get("system"):
reports.append(report)
rd = TableData(reports, "Custom Reports")
return rd
def get_custom_dashboards():
"""Return all the custom dashboards configured in XSOAR
Returns:
TableData: TableData object with the custom dashboards.
"""
r = get_api_request("/dashboards")
dashboards = []
for dashboard in r.values():
# Check it's not an inbuilt (system) dashboard
if not dashboard.get("system"):
dashboards.append(dashboard)
rd = TableData(dashboards, "Custom dashboards")
return rd
def get_all_playbooks():
"""Return all the custom playbooks installed in XSOAR
Returns:
TableData: TableData object with the custom playbooks.
"""
r = post_api_request("/playbook/search", {"query": "hidden:F"}).get("playbooks")
for pb in r:
pb["TotalTasks"] = len(pb.get("tasks", []))
rd = TableData(r, "All Playbooks")
return rd
def get_playbook_stats(playbooks, days=7, size=1000):
"""Pull all the incident types and associated playbooks,
then join this with the incident stats to determine how often each playbook has been used.
Args:
playbooks (SortedTableData): TableData object with the custom playbooks.
days (int, optional): max number of days. Defaults to 7.
size (int, optional): max request size. Defaults to 1000.
Returns:
Dict: Dictionary of playbook stats.
"""
# incident_types = get_api_request(DEMISTO_INCIDENT_TYPE_PATH)
incidents = get_all_incidents(days, size)
playbook_stats = {}
for incident in incidents:
playbook = incident.get("playbookId")
if playbook not in playbook_stats:
playbook_stats[playbook] = 0
playbook_stats[playbook] = playbook_stats[playbook] + 1
table = []
for playbook, count in playbook_stats.items():
# Try to join this with the playbooks we previously retrieved to populate
# more info.
playbook_data = playbooks.search("id", playbook)
if playbook_data:
table.append({
"playbook": playbook_data.get("name"),
"incidents": count
})
else:
table.append({
"playbook": playbook,
"incidents": count
})
td = TableData(table, "Playbook Stats")
return td
def get_playbook_dependencies(playbook_name):
"""Given a playbook name, searches for all dependencies.
Args:
playbook_name (string): name of the playbook.
Returns:
Dict[SortedTableData]: Dictionary of TableData objects.
"""
playbooks = get_all_playbooks()
playbook = playbooks.search("name", playbook_name)
if not playbook:
return_error(f"Playbook {playbook_name} not found.")
playbook_id = playbook.get("id")
body = {
"items": [
{
"id": f"{playbook_id}",
"type": "playbook"
}
],
"dependencyLevel": "optional"
}
dependencies = post_api_request("/itemsdependencies", body).get("existing").get("playbook").get(playbook_id)
if not dependencies:
return_error(f"Failed to retrieve dependencies for {playbook_id}")
types: dict = {}
for dependency in dependencies:
d_type = dependency.get("type")
if d_type not in types:
types[d_type] = []
pack = dependency.get("packID", "Custom")
if not pack:
pack = "Custom"
types[d_type].append({
"type": d_type,
"name": dependency.get("name"),
"pack": pack
})
result_table_datas = {}
for k, v in types.items():
if v:
td = SortedTableData(v, f"{k}s", sort_key="name")
result_table_datas[k] = td
return result_table_datas
def get_custom_automations():
r = post_api_request("/automation/search", {"query": "system:F AND hidden:F"}).get("scripts")
rd = SortedTableData(r, "Custom Automations", "name")
return rd
def get_system_config():
r = get_api_request("/system/config").get("defaultMap")
rd = TableData(r, "System Configuration")
return rd
def playbook_use_case(playbook: str, author: str, customer: str):
"""Given a playbook, generate a use case document.
Args:
playbook (str): playbook name
author (str): author name
customer (str): company name
"""
r = get_playbook_dependencies(playbook)
doc = UseCaseDocument(
playbook_name=playbook,
dependencies=r,
author=author,
customer=customer,
)
fr = fileResult("usecase.html", doc.html(), file_type=EntryType.ENTRY_INFO_FILE)
return_results(fr)
return_results(CommandResults(readable_output=doc.markdown(),))
def platform_as_built_use_case(max_request_size: int, max_days: int, author: str, customer: str):
"""Generate a platform as built use case document.
Args:
max_request_size (int): max request size
max_days (int): max number of days
author (str): author name
customer (str): company name
"""
open_incidents = get_open_incidents(max_days, max_request_size)
closed_incidents = get_closed_incidents(max_days, max_request_size)
system_config = get_system_config()
integrations = get_enabled_integrations(max_request_size)
installed_packs = get_installed_packs()
playbooks = get_custom_playbooks()
automations = get_custom_automations()
playbook_stats = get_playbook_stats(playbooks, max_days, max_request_size)
reports = get_custom_reports()
dashboards = get_custom_dashboards()
d = Document(
MD_DOCUMENT_TEMPLATE,
system_config=system_config,
integrations_table=integrations,
installed_packs_table=installed_packs,
playbooks_table=playbooks,
automations_table=automations,
open_incidents=open_incidents,
closed_incidents=closed_incidents,
playbook_stats=playbook_stats,
reports=reports,
dashboards=dashboards,
author=author,
customer=customer
)
fr = fileResult("asbuilt.html", d.html(), file_type=EntryType.ENTRY_INFO_FILE)
return_results(CommandResults(
readable_output=d.markdown(),
))
return_results(fr)
def main(): # pragma: no cover
args = demisto.args()
author = args.get("author")
customer = args.get("customer")
# Given a playbook is passed, we generate a use case document, instead of the platform as build.
if playbook := args.get("playbook"):
playbook_use_case(playbook, author, customer)
else:
# If no playbook is passed, we generate a platform as built.
max_request_size = args.get("size", 1000)
max_days = args.get("days", 7)
platform_as_built_use_case(max_request_size, max_days, author, customer)
if __name__ in ('__builtin__', 'builtins'):
main()