island, tests: Pass schema as arg to generate reverse schema instead of generating reverse schema at runtime

This commit is contained in:
Shreya Malviya 2021-09-27 20:20:04 +05:30
parent c2c5710dfa
commit afedde8c05
3 changed files with 52 additions and 58 deletions

View File

@ -1,9 +1,7 @@
from typing import Dict, List
from monkey_island.cc.services.config_schema.config_schema import SCHEMA
def get_config_schema_per_attack_technique() -> Dict[str, Dict[str, List[str]]]:
def get_config_schema_per_attack_technique(schema: Dict) -> Dict[str, Dict[str, List[str]]]:
"""
:return: dictionary mapping each attack technique to relevant config fields; example -
{
@ -17,7 +15,7 @@ def get_config_schema_per_attack_technique() -> Dict[str, Dict[str, List[str]]]:
"""
reverse_schema = {}
definitions = SCHEMA["definitions"]
definitions = schema["definitions"]
for definition in definitions:
definition_type = definitions[definition]["title"]
for field in definitions[definition]["anyOf"]:
@ -37,6 +35,3 @@ def _add_config_field_to_reverse_schema(
reverse_schema[attack_technique].setdefault(definition_type, []).append(config_field)
else:
reverse_schema[attack_technique] = {definition_type: [config_field]}
CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE = get_config_schema_per_attack_technique()

View File

@ -2,51 +2,6 @@ from monkey_island.cc.services.config_schema.config_schema_per_attack_technique
get_config_schema_per_attack_technique,
)
FAKE_SCHEMA = {
"definitions": {
"definition_type_1": {
"title": "Definition Type 1",
"anyOf": [
{
"title": "Config Option 1",
"attack_techniques": ["T0000", "T0001"],
},
{
"title": "Config Option 2",
"attack_techniques": ["T0000"],
},
{
"title": "Config Option 3",
"attack_techniques": [],
},
{
"title": "Config Option 4",
},
],
},
"definition_type_2": {
"title": "Definition Type 2",
"anyOf": [
{
"title": "Config Option 5",
"attack_techniques": ["T0000", "T0001"],
},
{
"title": "Config Option 6",
"attack_techniques": ["T0000"],
},
{
"title": "Config Option 7",
"attack_techniques": [],
},
{
"title": "Config Option 8",
},
],
},
}
}
REVERSE_FAKE_SCHEMA = {
"T0000": {
"Definition Type 1": ["Config Option 1", "Config Option 2"],
@ -59,9 +14,5 @@ REVERSE_FAKE_SCHEMA = {
}
def test_get_config_schema_per_attack_technique(monkeypatch):
monkeypatch.setattr(
"monkey_island.cc.services.config_schema.config_schema_per_attack_technique.SCHEMA",
FAKE_SCHEMA,
)
assert get_config_schema_per_attack_technique() == REVERSE_FAKE_SCHEMA
def test_get_config_schema_per_attack_technique(monkeypatch, fake_schema):
assert get_config_schema_per_attack_technique(fake_schema) == REVERSE_FAKE_SCHEMA

View File

@ -20,3 +20,51 @@ def config(monkeypatch, IPS, PORT):
monkeypatch.setattr(Environment, "_ISLAND_PORT", PORT)
config = ConfigService.get_default_config(True)
return config
@pytest.fixture
def fake_schema():
return {
"definitions": {
"definition_type_1": {
"title": "Definition Type 1",
"anyOf": [
{
"title": "Config Option 1",
"attack_techniques": ["T0000", "T0001"],
},
{
"title": "Config Option 2",
"attack_techniques": ["T0000"],
},
{
"title": "Config Option 3",
"attack_techniques": [],
},
{
"title": "Config Option 4",
},
],
},
"definition_type_2": {
"title": "Definition Type 2",
"anyOf": [
{
"title": "Config Option 5",
"attack_techniques": ["T0000", "T0001"],
},
{
"title": "Config Option 6",
"attack_techniques": ["T0000"],
},
{
"title": "Config Option 7",
"attack_techniques": [],
},
{
"title": "Config Option 8",
},
],
},
}
}