Skip to content

Commit 091da78

Browse files
committed
fix: add required properties to config schema, add tests for config checks
1 parent 530f088 commit 091da78

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

src/config.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"$schema": "http://json-schema.org/draft-07/schema",
33
"type": "object",
4+
"required": [
5+
"allowlist",
6+
"compartments_rules",
7+
"instances"
8+
],
49
"properties": {
510
"allowlist": {
611
"type": "object",

src/mispguard.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,21 @@ def configure(self, updated):
9797
with open("config.schema.json", "r") as file:
9898
schema = json.load(file)
9999
self.config = json.load(open(ctx.options.config))
100-
101-
# create instances_host_mapping dictionary
102-
self.config["instances_host_mapping"] = {}
103-
for instance_id, instance in self.config["instances"].items():
104-
self.config["instances_host_mapping"][instance["host"]] = instance_id
105-
self.config["instances_host_mapping"][instance["ip"]] = instance_id
106100

107101
validate(
108102
instance=self.config,
109103
schema=schema,
110104
format_checker=Draft202012Validator.FORMAT_CHECKER,
111105
)
112106

107+
# create instances_host_mapping dictionary
108+
self.config["instances_host_mapping"] = {}
109+
for instance_id, instance in self.config["instances"].items():
110+
self.config["instances_host_mapping"][
111+
instance["host"]
112+
] = instance_id
113+
self.config["instances_host_mapping"][instance["ip"]] = instance_id
114+
113115
except Exception as e:
114116
logger.error("failed to load config file: %s" % str(e))
115117
exit(1)
@@ -323,7 +325,11 @@ def process_request(self, flow: MISPHTTPFlow) -> None:
323325
rules = self.get_rules(flow)
324326
return self.process_sightings(rules, sightings, flow)
325327

326-
if flow.is_push and flow.is_analyst_data and not flow.is_analyst_data_minimal_index:
328+
if (
329+
flow.is_push
330+
and flow.is_analyst_data
331+
and not flow.is_analyst_data_minimal_index
332+
):
327333
try:
328334
analyst_data = flow.request.json()
329335
logger.debug(analyst_data)
@@ -395,7 +401,11 @@ def process_response(self, flow: MISPHTTPFlow) -> None:
395401
rules = self.get_rules(flow)
396402
return self.process_sightings(rules, sightings, flow)
397403

398-
if flow.is_pull and flow.is_analyst_data and not flow.is_analyst_data_minimal_index:
404+
if (
405+
flow.is_pull
406+
and flow.is_analyst_data
407+
and not flow.is_analyst_data_minimal_index
408+
):
399409
try:
400410
analyst_data = flow.response.json()
401411
except Exception as ex:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

src/test/test_misp_guard.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ async def test_rules_pull(self, scenario: dict, caplog):
356356
), f"Expected status code {scenario['expected_status_code']} but got {flow.response.status_code} for scenario {scenario['name']}"
357357
assert "MispGuard initialized" in caplog.text
358358
for expected_log in scenario["expected_logs"]:
359+
# debug logs
359360
# print(caplog.text)
360361
assert (
361362
expected_log in caplog.text
@@ -451,3 +452,25 @@ async def test_pull_XUserOrgUUID_mismatch(self, scenario: str, caplog):
451452
in caplog.text
452453
)
453454
assert flow.response.status_code == 403
455+
456+
def test_no_config_file(self, caplog) -> mispguard.MispGuard:
457+
mg = mispguard.MispGuard()
458+
caplog.set_level("INFO")
459+
460+
with taddons.context(mg) as tctx:
461+
try:
462+
tctx.configure(mg, config="./test/not-found.json")
463+
self.tctx = tctx
464+
except SystemExit:
465+
assert "failed to load config file, use: `--set config=config.json`" in caplog.text
466+
467+
def test_invalid_config_file(self, caplog) -> mispguard.MispGuard:
468+
mg = mispguard.MispGuard()
469+
caplog.set_level("INFO")
470+
471+
with taddons.context(mg) as tctx:
472+
try:
473+
tctx.configure(mg, config="./test/fixtures/test_invalid_config.json")
474+
self.tctx = tctx
475+
except SystemExit:
476+
assert "failed to load config file: " in caplog.text

0 commit comments

Comments
 (0)