-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cert.py
197 lines (158 loc) · 6.63 KB
/
test_cert.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
import json
import logging
import re
from stixorm.module.typedb import TypeDBSink, TypeDBSource
from stixorm.module.typedb_lib.factories.import_type_factory import ImportTypeFactory
from stix2 import (parse)
from pathlib import Path
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s')
logger = logging.getLogger(__name__)
connection = {
"uri": "localhost",
"port": "1729",
"database": "stix",
"user": None,
"password": None
}
def load_personas(file_path='./data/stix_cert_data/stix_cert_persona_dict.json'):
logger.info(f'Loading file {file_path}')
with open(file_path, mode="r", encoding="utf-8") as file:
d = json.load(file)
logger.info(f"Loaded {len(d.keys())} profiles")
return d
def load_template(file_path='./oasis/cert_template.txt'):
logger.info(f'Loading file {file_path}')
with open(file_path, mode="r", encoding="utf-8") as file:
t = file.read()
m = re.findall(r"\[[a-z0-9\.]+\]", t,re.IGNORECASE|re.MULTILINE)
logger.info(f"Loaded {len(m)} profiles")
return t,m
def run_profiles(config: dict,template,tags,out_file):
report = template
# create the database and init
import_type = ImportTypeFactory().get_default_import()
sink_db = TypeDBSink(connection=connection, clear=True,import_type= import_type)
source_db = TypeDBSource(connection=connection, import_type=import_type)
# get all the initial STIX IDs (only markings should be there)
base_ids = sink_db.get_stix_ids()
# markings should be automatically ignored
assert len(base_ids) == 0
logger.info(f'Total default stix objects {len(base_ids)}')
for profile in config.keys():
logger.info(f'Checking profile {profile}')
result = +run_profile(profile,config[profile],sink_db,source_db)
logger.info(result)
for code in result.keys():
if code in tags:
logger.info(f'Asserting flag {code}')
report = report.replace(code,result[code])
# now write the report
with open(out_file,'w') as file:
file.write(report)
def verify_file(file_path: str, sink_db: str):
with open(file_path, mode="r", encoding="utf-8") as file:
check_list = []
logger.info(f"Loading file {file_path}")
json_blob = json.load(file)
if isinstance(json_blob, list):
for json_dict in json_blob:
#stix_obj = parse(item)
sink_db.add(json_dict)
insert_ids = sink_db.get_stix_ids()
tot_insert = len(insert_ids)
if tot_insert == 0 :
logger.error(f"Total STIX ID {tot_insert}")
check_list.append(False)
else:
check_list.append(True)
logger.info(f"Total STIX ID {tot_insert}")
'''
return_dict = source_db.get(stix_obj.id)
return_obj = parse(return_dict)
cmp = StixComparator()
check, p_ok, p_not = cmp.compare(stix_obj, return_obj)
return check
'''
else:
bundle = parse(json_blob)
for stix_obj in bundle.objects:
sink_db.add(stix_obj)
insert_ids = sink_db.get_stix_ids()
logger.info(f"Total STIX ID in bundle {len(insert_ids)}")
check_list.append(True)
'''
return_dict = self._typedbSource.get(stix_obj.id)
return_obj = parse(return_dict)
cmp = StixComparator()
check, p_ok, p_not = cmp.compare(stix_obj, return_obj)
logger.info(f'OK properties {p_ok}')
logger.info(f'KO properties {p_not}')
self.assertTrue(check)
'''
return check_list
def verify_files(directory: str,
sink_db: str,
source_db: str):
""" Load and verify the file
Args:
fullname (): folder path
"""
path = Path(directory)
check_list = []
if path.is_dir():
for file_path in path.iterdir():
file_list = verify_file(file_path,sink_db)
check_list = check_list + file_list
return check_list
else:
logger.error('This is not a folder???')
def run_profile(short,
profile,
sink_db,
source_db):
logger.info(f'Title {profile["title"]}')
results = {}
if 'level1' in profile:
count = 0
for level in profile['level1']:
count +=1
consumer_passed = False
producer_passed = False
sub_dir = Path.cwd()/'data'/'stix_cert_data'/level['dir']/level['sub_dir']
logger.info(f"Test folder {sub_dir}")
checks = verify_files(sub_dir,sink_db,source_db)
if level['sub_dir'] == 'consumer_test':
consumer_passed = all(checks)
elif level['sub_dir'] == 'producer_test':
producer_passed = all(checks)
if consumer_passed: results[f'[{short}.C1]'] = 'Passed'
else: results[f'[{short}.C1]'] = 'Failed'
if producer_passed: results[f'[{short}.P1]'] = 'Passed'
else: results[f'[{short}.P1]'] = 'Failed'
logger.info(f'\tTotal level 1 checks {count}')
if 'level2' in profile:
count += 1
consumer_passed = False
producer_passed = False
sub_dir = Path.cwd() / 'data' / 'stix_cert_data' / level['dir'] / level['sub_dir']
logger.info(f"Test folder {sub_dir}")
checks = verify_files(sub_dir, sink_db, source_db)
if level['sub_dir'] == 'consumer_test':
consumer_passed = all(checks)
elif level['sub_dir'] == 'producer_test':
producer_passed = all(checks)
if consumer_passed: results[f'[{short}.C2]'] = 'Passed'
else:
results[f'[{short}.C2]'] = 'Failed'
if producer_passed: results[f'[{short}.P2]'] = 'Passed'
else:
results[f'[{short}.P2]'] = 'Failed'
logger.info(f'\tTotal level 2 checks {count}')
return results
if __name__ == '__main__':
cwd = Path.cwd()
logger.info(f'Running tests in {cwd}')
tests = load_personas(file_path=Path.joinpath(cwd, 'test/data', 'stix_cert_data', 'stix_cert_persona_dict.json'))
template,tags = load_template(file_path=Path.joinpath(cwd, 'test/oasis', 'cert_template.txt'))
logger.info(f"Profiles: {list(tests.keys())}")
run_profiles(tests, template, tags, out_file=Path.joinpath(cwd, 'test/oasis', 'report.txt'))