Refactored configuration parser to pull configs, apply template and submit them instead of loading configs from file.

This commit is contained in:
VakarisZ 2021-03-02 15:14:33 +02:00
parent 8ca72bbf31
commit aaab827e32
2 changed files with 28 additions and 13 deletions

View File

@ -1,18 +1,30 @@
import json
import os
import dpath.util
from typing_extensions import Type
from envs.monkey_zoo.blackbox.island_client.monkey_island_client import MonkeyIslandClient
from envs.monkey_zoo.blackbox.island_configs.config_template import ConfigTemplate
class IslandConfigParser(object):
def __init__(self, config_filename):
self.config_raw = open(IslandConfigParser.get_conf_file_path(config_filename), 'r').read()
self.config_json = json.loads(self.config_raw)
def get_ips_of_targets(self):
return self.config_json['basic_network']['scope']['subnet_scan_list']
class IslandConfigParser:
@staticmethod
def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"island_configs",
conf_file_name)
def get_raw_config(config_template: Type[ConfigTemplate],
island_client: MonkeyIslandClient) -> str:
response = island_client.get_config()
config = IslandConfigParser.apply_template_to_config(config_template, response['configuration'])
return json.dumps(config)
@staticmethod
def apply_template_to_config(config_template: Type[ConfigTemplate],
config: dict) -> dict:
for path, value in config_template.config_values.items():
dpath.util.set(config, path, value, '.')
return config
@staticmethod
def get_ips_of_targets(raw_config):
return dpath.util.get(json.loads(raw_config),
"basic_network.scope.subnet_scan_list",
'.')

View File

@ -24,6 +24,9 @@ class MonkeyIslandClient(object):
def get_api_status(self):
return self.requests.get("api")
def get_config(self):
return json.loads(self.requests.get("api/configuration/island").content)
@avoid_race_condition
def import_config(self, config_contents):
_ = self.requests.post("api/configuration/island", data=config_contents)