forked from p15670423/monkey
Refactored configuration parser to pull configs, apply template and submit them instead of loading configs from file.
This commit is contained in:
parent
8ca72bbf31
commit
aaab827e32
|
@ -1,18 +1,30 @@
|
||||||
import json
|
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):
|
class IslandConfigParser:
|
||||||
|
|
||||||
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']
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_conf_file_path(conf_file_name):
|
def get_raw_config(config_template: Type[ConfigTemplate],
|
||||||
return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
island_client: MonkeyIslandClient) -> str:
|
||||||
"island_configs",
|
response = island_client.get_config()
|
||||||
conf_file_name)
|
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",
|
||||||
|
'.')
|
||||||
|
|
|
@ -24,6 +24,9 @@ class MonkeyIslandClient(object):
|
||||||
def get_api_status(self):
|
def get_api_status(self):
|
||||||
return self.requests.get("api")
|
return self.requests.get("api")
|
||||||
|
|
||||||
|
def get_config(self):
|
||||||
|
return json.loads(self.requests.get("api/configuration/island").content)
|
||||||
|
|
||||||
@avoid_race_condition
|
@avoid_race_condition
|
||||||
def import_config(self, config_contents):
|
def import_config(self, config_contents):
|
||||||
_ = self.requests.post("api/configuration/island", data=config_contents)
|
_ = self.requests.post("api/configuration/island", data=config_contents)
|
||||||
|
|
Loading…
Reference in New Issue