44from modules import util
55from loguru import logger
66
7- from .. import site_config
87from . import analytics_config
98
109
11- def generate_analytic ():
10+ def generate_analytic_overview ():
1211 """Verify analytic directory and generate analytic index markdown."""
1312 # Create content pages directory if does not already exist
1413 util .buildhelpers .create_content_pages_dir ()
@@ -21,14 +20,13 @@ def generate_analytic():
2120 os .mkdir (analytics_config .analytic_markdown_path )
2221
2322 # Generate markdown files used for page generation
24- analytic_generated = generate_markdown_files ()
23+ analytic_generated = generate_markdown_file ()
2524 if not analytic_generated :
2625 util .buildhelpers .remove_module_from_menu (analytics_config .module_name )
2726
2827
29- def generate_markdown_files ():
30- """Generate shared data for all analytics and markdown for analytic pages."""
31- data = {}
28+ def generate_markdown_file ():
29+ """Generate table data for all analytics."""
3230 has_analytics = False
3331
3432 analytics = util .relationshipgetters .get_analytic_list ()
@@ -40,24 +38,15 @@ def generate_markdown_files():
4038 logger .debug ("No analytics found" )
4139
4240 if has_analytics :
43- notes = util .relationshipgetters .get_objects_using_notes ()
44-
4541 # Generate sidebar data
46- sidebar_data = util .buildhelpers .get_side_menu_data (
47- "Analytics" , "/analytics/" , active_analytics
48- )
49- data ["sidebar_data" ] = sidebar_data
50-
51- data ["total_count" ] = str (len (active_analytics ))
52- data ["analytics_table" ] = get_analytic_table (active_analytics )
53-
42+ data = {
43+ "sidebar_data" : util .buildhelpers .get_side_menu_data ("Analytics" , "/analytics/" , []),
44+ "total_count" : str (len (active_analytics )),
45+ "analytics_table" : get_analytic_table (active_analytics ),
46+ }
5447 subs = analytics_config .analytic_index_md + json .dumps (data )
5548 with open (os .path .join (analytics_config .analytic_markdown_path , "overview.md" ), "w" , encoding = "utf8" ) as md_file :
5649 md_file .write (subs )
57-
58- # Create markdown for analytics
59- for analytic in analytics :
60- generate_analytic_md (analytic , sidebar_data , notes )
6150
6251 return has_analytics
6352
@@ -80,54 +69,14 @@ def get_analytic_table(analytics):
8069 "domains" : domain_names ,
8170 "description" : analytic .get ("description" , "" ),
8271 "deprecated" : analytic .get ("x_mitre_deprecated" , False ),
72+ "detection_strategy" : get_related_detection_strategies (analytic ["id" ]),
8373 "url" : util .buildhelpers .get_analytic_url (analytic ),
8474 }
8575 analytic_table .append (row )
8676
8777 # sort by id
8878 return sorted (analytic_table , key = lambda k : k ["id" ])
8979
90- def generate_analytic_md (analytic , sidebar_data , notes ):
91- """Generate markdown for individual analytic pages."""
92- attack_id = util .buildhelpers .get_attack_id (analytic )
93- if not attack_id :
94- logger .debug (f"No attack ID found on analytic: { analytic ['id' ]} " )
95- return
96-
97- # build reference list
98- reference_list = { "current_number" : 0 }
99- reference_list = util .buildhelpers .update_reference_list (reference_list , analytic )
100-
101- dates = util .buildhelpers .get_created_and_modified_dates (analytic )
102- domains = analytic .get ("x_mitre_domains" , [])
103- domain_names = [util .buildhelpers .get_domain_display_name (domain ) for domain in domains ]
104-
105- data = {
106- "attack_id" : attack_id ,
107- "name" : analytic .get ("name" ),
108- "notes" : notes .get (analytic ["id" ]),
109- "created" : dates .get ("created" ),
110- "modified" : dates .get ("modified" ),
111- "version" : analytic .get ("x_mitre_version" ),
112- "description" : analytic .get ("description" ),
113- "deprecated" : analytic .get ("x_mitre_deprecated" , False ),
114- "detection_strategies" : get_related_detection_strategies (analytic ["id" ]),
115- "mutable_elements" : analytic .get ("x_mitre_mutable_elements" , []),
116- "platforms" : analytic .get ("x_mitre_platforms" , []),
117- "log_sources" : build_log_source_table (analytic ),
118- "domains" : domain_names ,
119- "citations" : reference_list ,
120- "sidebar_data" : sidebar_data ,
121- "versioning_feature" : site_config .check_versions_module (),
122- }
123-
124- subs = analytics_config .analytic_md .substitute (data )
125- subs += json .dumps (data )
126-
127- # Write the markdown file
128- with open (os .path .join (analytics_config .analytic_markdown_path , attack_id + ".md" ), "w" , encoding = "utf8" ) as md_file :
129- md_file .write (subs )
130-
13180
13281def get_related_detection_strategies (analytic_ref ):
13382 related_dets = util .stixhelpers .get_related_detection_strategies (analytic_ref )
@@ -138,36 +87,7 @@ def get_related_detection_strategies(analytic_ref):
13887 for det in related_dets :
13988 attack_id = util .buildhelpers .get_attack_id (det )
14089 det_data .append ({
141- "attack_id" : attack_id ,
142- "name" : det ["name" ],
90+ "id" : attack_id ,
14391 "url" : f"/detectionstrategies/{ attack_id } "
14492 })
14593 return det_data
146-
147-
148- def build_log_source_table (analytic ):
149- log_source_table = []
150-
151- log_sources = analytic .get ("x_mitre_log_source_references" )
152- if not log_sources :
153- logger .debug (f"No log source references found on Analytic { analytic ['id' ]} " )
154- return log_source_table
155-
156- for log_source in log_sources :
157- log_source_data = {
158- "name" : log_source .get ("name" , "" ),
159- "channel" : log_source .get ("channel" , "" ),
160- }
161- datacomponent_ref = log_source .get ("x_mitre_data_component_ref" , None )
162- datacomponent = util .stixhelpers .get_datacomponent_from_list (datacomponent_ref )
163- if not datacomponent :
164- logger .debug (f"Log source data component not found: { log_source } " )
165- log_source_data ["data_component_not_found" ] = True
166- else :
167- datacomponent_id = util .buildhelpers .get_attack_id (datacomponent )
168- log_source_data ["data_component_name" ] = datacomponent .get ("name" )
169- log_source_data ["data_component_id" ] = datacomponent_id
170- log_source_data ["data_component_url" ] = f"/datacomponents/{ datacomponent_id } "
171-
172- log_source_table .append (log_source_data )
173- return log_source_table
0 commit comments