-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_table.py
75 lines (61 loc) · 1.9 KB
/
gen_table.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
import os
import yaml
from itertools import chain
from pathlib import Path
from typing import List
SEVERITY_MAP = {
'INFO': "ℹ️",
'WARNING': "⚠️",
'ERROR': "❌",
None: "🌫️",
}
IMPACT_MAP = {
'LOW': "🟢",
'MEDIUM': "🟡",
'HIGH': "🔴",
None: "",
}
CONFIDENCE_MAP = {
'LOW': "🌕",
'MEDIUM': "🌗",
'HIGH': "🌘",
None: "",
}
def list_rules(directory: str) -> List[tuple[str, str]]:
"""
List all rule files in the given directory
"""
path = Path(directory)
result : List[tuple[str, str]] = []
if not path.is_dir():
raise ValueError(f'{directory} is not a directory')
for rule in chain(path.glob('**/*.yaml'), path.glob('**/*.yml')):
name = rule.stem
parent = rule.parent
test_file = os.path.join(parent, f'{name}.move')
if not os.path.exists(test_file):
raise ValueError(f'Test case for {name} does not exist')
result.append((str(rule), test_file))
return result
def generate_rule_table(rules: List[tuple[str, str]]):
"""
Generate a rule table for the given rules
"""
print('ID | Severity | IMPACT | Confidence | Message')
print(':-:|:--------:|:------:|:----------:|--------')
for rule, test in rules:
with open(rule, 'r') as f:
rules = yaml.safe_load(f).get('rules')
for r in rules:
id = r.get('id')
message = r.get('message')
severity = r.get('severity')
meta = r.get('metadata')
impact = meta.get('impact')
confidence = meta.get('confidence')
print(f'`{id}` | {SEVERITY_MAP[severity]} | {IMPACT_MAP[impact]} | {CONFIDENCE_MAP[confidence]} | {message}')
def main():
rules = list_rules('rules')
generate_rule_table(rules)
if __name__ == '__main__':
main()