Skip to content

Commit

Permalink
fix: add required properties to config schema, add tests for config c…
Browse files Browse the repository at this point in the history
…hecks
  • Loading branch information
righel committed Jan 14, 2025
1 parent 530f088 commit 091da78
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/config.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"allowlist",
"compartments_rules",
"instances"
],
"properties": {
"allowlist": {
"type": "object",
Expand Down
26 changes: 18 additions & 8 deletions src/mispguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,21 @@ def configure(self, updated):
with open("config.schema.json", "r") as file:
schema = json.load(file)
self.config = json.load(open(ctx.options.config))

# create instances_host_mapping dictionary
self.config["instances_host_mapping"] = {}
for instance_id, instance in self.config["instances"].items():
self.config["instances_host_mapping"][instance["host"]] = instance_id
self.config["instances_host_mapping"][instance["ip"]] = instance_id

validate(
instance=self.config,
schema=schema,
format_checker=Draft202012Validator.FORMAT_CHECKER,
)

# create instances_host_mapping dictionary
self.config["instances_host_mapping"] = {}
for instance_id, instance in self.config["instances"].items():
self.config["instances_host_mapping"][
instance["host"]
] = instance_id
self.config["instances_host_mapping"][instance["ip"]] = instance_id

except Exception as e:
logger.error("failed to load config file: %s" % str(e))
exit(1)
Expand Down Expand Up @@ -323,7 +325,11 @@ def process_request(self, flow: MISPHTTPFlow) -> None:
rules = self.get_rules(flow)
return self.process_sightings(rules, sightings, flow)

if flow.is_push and flow.is_analyst_data and not flow.is_analyst_data_minimal_index:
if (
flow.is_push
and flow.is_analyst_data
and not flow.is_analyst_data_minimal_index
):
try:
analyst_data = flow.request.json()
logger.debug(analyst_data)
Expand Down Expand Up @@ -395,7 +401,11 @@ def process_response(self, flow: MISPHTTPFlow) -> None:
rules = self.get_rules(flow)
return self.process_sightings(rules, sightings, flow)

if flow.is_pull and flow.is_analyst_data and not flow.is_analyst_data_minimal_index:
if (
flow.is_pull
and flow.is_analyst_data
and not flow.is_analyst_data_minimal_index
):
try:
analyst_data = flow.response.json()
except Exception as ex:
Expand Down
1 change: 1 addition & 0 deletions src/test/fixtures/test_invalid_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
23 changes: 23 additions & 0 deletions src/test/test_misp_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ async def test_rules_pull(self, scenario: dict, caplog):
), f"Expected status code {scenario['expected_status_code']} but got {flow.response.status_code} for scenario {scenario['name']}"
assert "MispGuard initialized" in caplog.text
for expected_log in scenario["expected_logs"]:
# debug logs
# print(caplog.text)
assert (
expected_log in caplog.text
Expand Down Expand Up @@ -451,3 +452,25 @@ async def test_pull_XUserOrgUUID_mismatch(self, scenario: str, caplog):
in caplog.text
)
assert flow.response.status_code == 403

def test_no_config_file(self, caplog) -> mispguard.MispGuard:
mg = mispguard.MispGuard()
caplog.set_level("INFO")

with taddons.context(mg) as tctx:
try:
tctx.configure(mg, config="./test/not-found.json")
self.tctx = tctx
except SystemExit:
assert "failed to load config file, use: `--set config=config.json`" in caplog.text

def test_invalid_config_file(self, caplog) -> mispguard.MispGuard:
mg = mispguard.MispGuard()
caplog.set_level("INFO")

with taddons.context(mg) as tctx:
try:
tctx.configure(mg, config="./test/fixtures/test_invalid_config.json")
self.tctx = tctx
except SystemExit:
assert "failed to load config file: " in caplog.text

0 comments on commit 091da78

Please sign in to comment.