Repository navigation
Expand file tree
/
Copy pathpretty_yaml.py
More file actions
executable file
·175 lines (146 loc) · 6.54 KB
/
Copy pathpretty_yaml.py
File metadata and controls
executable file
·175 lines (146 loc) · 6.54 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
#!/bin/python
from os import path, walk
import sys
import argparse
import yaml
import re
def parse_data_models_from_search(search):
match = re.search(r'from\sdatamodel\s?=\s?([^\s.]*)', search)
if match is not None:
return match.group(1)
return False
def pretty_yaml_detections(REPO_PATH, VERBOSE, content_part):
manifest_files = []
types = ["endpoint", "application", "cloud", "deprecated", "experimental", "network", "web"]
for t in types:
for root, dirs, files in walk(REPO_PATH + "/" + content_part + '/' + t):
for file in files:
if file.endswith(".yml"):
manifest_files.append((path.join(root, file)))
for manifest_file in manifest_files:
pretty_yaml = dict()
if VERBOSE:
print("processing manifest {0}".format(manifest_file))
with open(manifest_file, 'r') as stream:
try:
object = list(yaml.safe_load_all(stream))[0]
except yaml.YAMLError as exc:
print(exc)
print("Error reading {0}".format(manifest_file))
error = True
continue
pretty_yaml['name'] = object['name']
pretty_yaml['id'] = object['id']
pretty_yaml['version'] = object['version']
pretty_yaml['date'] = object['date']
pretty_yaml['author'] = object['author']
pretty_yaml['type'] = object['type']
pretty_yaml['datamodel'] = object['datamodel']
pretty_yaml['description'] = object['description']
pretty_yaml['search'] = object['search']
if 'how_to_implement' in object:
pretty_yaml['how_to_implement'] = object['how_to_implement']
else:
pretty_yaml['how_to_implement'] = ''
pretty_yaml['known_false_positives'] = object['known_false_positives']
if 'references' in object:
pretty_yaml['references'] = object['references']
else:
pretty_yaml['references'] = []
pretty_yaml['tags'] = {key: value for key, value in sorted(object['tags'].items())}
with open(manifest_file, 'w') as file:
documents = yaml.dump(pretty_yaml, file, sort_keys=False)
return manifest_files
def pretty_yaml_deployments(REPO_PATH, VERBOSE, content_part):
manifest_files = []
for root, dirs, files in walk(REPO_PATH + "/" + content_part + '/'):
for file in files:
if file.endswith(".yml"):
manifest_files.append((path.join(root, file)))
for manifest_file in manifest_files:
pretty_yaml = dict()
if VERBOSE:
print("processing manifest {0}".format(manifest_file))
with open(manifest_file, 'r') as stream:
try:
object = list(yaml.safe_load_all(stream))[0]
except yaml.YAMLError as exc:
print(exc)
print("Error reading {0}".format(manifest_file))
error = True
continue
pretty_yaml['name'] = object['name']
pretty_yaml['id'] = object['id']
pretty_yaml['date'] = object['date']
pretty_yaml['author'] = object['author']
pretty_yaml['description'] = object['description']
pretty_yaml['scheduling'] = object['scheduling']
if 'alert_action' in object:
pretty_yaml['alert_action'] = object['alert_action']
pretty_yaml['tags'] = {key: value for key, value in sorted(object['tags'].items())}
with open(manifest_file, 'w') as file:
documents = yaml.dump(pretty_yaml, file, sort_keys=False)
return manifest_files
def pretty_yaml_stories(REPO_PATH, VERBOSE, content_part):
manifest_files = []
for root, dirs, files in walk(REPO_PATH + "/" + content_part + '/'):
for file in files:
if file.endswith(".yml"):
manifest_files.append((path.join(root, file)))
for manifest_file in manifest_files:
pretty_yaml = dict()
if VERBOSE:
print("processing manifest {0}".format(manifest_file))
with open(manifest_file, 'r') as stream:
try:
object = list(yaml.safe_load_all(stream))[0]
except yaml.YAMLError as exc:
print(exc)
print("Error reading {0}".format(manifest_file))
error = True
continue
pretty_yaml['name'] = object['name']
pretty_yaml['id'] = object['id']
pretty_yaml['version'] = object['version']
pretty_yaml['date'] = object['date']
pretty_yaml['author'] = object['author']
pretty_yaml['description'] = object['description']
pretty_yaml['narrative'] = object['narrative']
if 'references' in object:
pretty_yaml['references'] = object['references']
else:
pretty_yaml['references'] = []
pretty_yaml['tags'] = {key: value for key, value in sorted(object['tags'].items())}
with open(manifest_file, 'w') as file:
documents = yaml.dump(pretty_yaml, file, sort_keys=False)
return manifest_files
def pretty_yaml(REPO_PATH, VERBOSE, content_part):
#for root, dirs, files in walk(REPO_PATH + "/"):
manifest_files = []
if content_part == 'detections':
manifest_files = pretty_yaml_detections(REPO_PATH, VERBOSE, content_part)
elif content_part == 'deployments':
manifest_files = pretty_yaml_deployments(REPO_PATH, VERBOSE, content_part)
elif content_part == 'stories':
manifest_files = pretty_yaml_stories(REPO_PATH, VERBOSE, content_part)
return len(manifest_files)
def main(args):
parser = argparse.ArgumentParser(description="keeps yamls in security_content sorted and pretty printed with custom sort keys, \
meant to run quitely for CI, use -v flag to make it bark")
parser.add_argument("-p", "--path", required=True, help="path to security_content repo")
parser.add_argument("-v", "--verbose", required=False, default=False, action='store_true', help="prints verbose output")
# parse them
args = parser.parse_args()
REPO_PATH = args.path
VERBOSE = args.verbose
output = []
pretty_yaml_objects = ['macros','lookups','stories','detections','response_tasks','responses','deployments']
for pretty_yaml_object in pretty_yaml_objects:
touch_count = pretty_yaml(REPO_PATH, VERBOSE, pretty_yaml_object)
if VERBOSE:
output.append("made {0} {1} pretty".format(touch_count, pretty_yaml_object))
for o in output:
print(o)
print("finished successfully!")
if __name__ == "__main__":
main(sys.argv[1:])