def _master_keys(self, topo_dicts): for topo_id in topo_dicts: base = topo_id.base_dir(self.args.output_dir) write_file(os.path.join(base, 'keys', 'master0.key'), base64.b64encode(os.urandom(16)).decode()) write_file(os.path.join(base, 'keys', 'master1.key'), base64.b64encode(os.urandom(16)).decode())
def generate_sciond(self): for topo_id, topo in self.args.topo_dicts.items(): base = topo_id.base_dir(self.args.output_dir) sciond_conf = self._build_sciond_conf(topo_id, topo["isd_as"], base) write_file(os.path.join(base, SD_CONFIG_NAME), toml.dumps(sciond_conf))
def _sig_toml(self, topo_id, topo): name = 'sig%s' % topo_id.file_fmt() net = self.args.networks[name][0] log_level = 'debug' ipv = 'ipv4' if ipv not in net: ipv = 'ipv6' sciond_net = self.args.networks["sd" + topo_id.file_fmt()][0] ipv = 'ipv4' if ipv not in sciond_net: ipv = 'ipv6' sciond_ip = sciond_net[ipv] sig_conf = { 'gateway': { 'id': name, 'traffic_policy_file': 'conf/sig.json', 'ctrl_addr': str(net[ipv]), }, 'sciond_connection': { 'address': socket_address_str(sciond_ip, SD_API_PORT), }, 'log': { 'console': { 'level': log_level, } }, 'metrics': { 'prometheus': '0.0.0.0:%s' % SIG_PROM_PORT }, 'features': translate_features(self.args.features), } path = os.path.join(topo_id.base_dir(self.args.output_dir), SIG_CONFIG_NAME) write_file(path, toml.dumps(sig_conf))
def generate_disp(self): if self.args.docker: self._gen_disp_docker() else: elem_dir = os.path.join(self.args.output_dir, "dispatcher") config_file_path = os.path.join(elem_dir, DISP_CONFIG_NAME) write_file(config_file_path, toml.dumps(self._build_disp_conf("dispatcher")))
def _write_disp_file(self): if self.args.docker: return targets_path = os.path.join(self.args.output_dir, "dispatcher", PrometheusGenerator.PROM_DIR, "disp.yml") target_config = [{'targets': [prom_addr_dispatcher(False, None, None, DISP_PROM_PORT, None)]}] write_file(targets_path, yaml.dump(target_config, default_flow_style=False))
def generate_br(self): for topo_id, topo in self.args.topo_dicts.items(): for k, v in topo.get("border_routers", {}).items(): base = topo_id.base_dir(self.args.output_dir) br_conf = self._build_br_conf(topo_id, topo["isd_as"], base, k, v) write_file(os.path.join(base, "%s.toml" % k), toml.dumps(br_conf))
def _write_ca_files(self, topo_dicts, ca_files): isds = set() for topo_id, as_topo in topo_dicts.items(): isds.add(topo_id[0]) for isd in isds: base = os.path.join(self.args.output_dir, "CAS") for path, value in ca_files[int(isd)].items(): write_file(os.path.join(base, path), value.decode())
def _write_elem_conf(self, elem, entry, elem_dir, topo_id=None): config = configparser.ConfigParser(interpolation=None) prog = self._common_entry(elem, entry, elem_dir) if elem.startswith("br"): prog['environment'] += ',GODEBUG="cgocheck=0"' config["program:%s" % elem] = prog text = StringIO() config.write(text) write_file(os.path.join(elem_dir, SUPERVISOR_CONF), text.getvalue())
def _write_as_topos(self): for topo_id, as_topo, base in srv_iter(self.topo_dicts, self.args.output_dir, common=True): path = os.path.join(base, TOPO_FILE) contents_json = json.dumps(self.topo_dicts[topo_id], default=json_default, indent=2) write_file(path, contents_json + '\n')
def test_basic(self, dirname, makedirs, rename): dirname.return_value = "Dir_Name" # Call write_file("File_Path", "Text") # Tests dirname.assert_called_once_with("File_Path") makedirs.assert_called_once_with("Dir_Name", exist_ok=True) builtins.open.assert_called_once_with("File_Path.new", 'w') builtins.open.return_value.write.assert_called_once_with("Text") rename.assert_called_once_with("File_Path.new", "File_Path")
def _gen_disp_docker(self): for topo_id, topo in self.args.topo_dicts.items(): base = topo_id.base_dir(self.args.output_dir) elem_ids = ['sig_%s' % topo_id.file_fmt()] + \ list(topo.get("border_routers", {})) + \ list(topo.get("control_service", {})) + \ ['tester_%s' % topo_id.file_fmt()] for k in elem_ids: disp_id = 'disp_%s' % k disp_conf = self._build_disp_conf(disp_id, topo_id) write_file(os.path.join(base, '%s.toml' % disp_id), toml.dumps(disp_conf))
def _sig_testing_conf(self): text = '' for topo_id in self.args.topo_dicts: net = self.args.networks['tester_%s' % topo_id.file_fmt()][0] ipv = 'ipv4' if ipv not in net: ipv = 'ipv6' ip = net[ipv] text += str(topo_id) + ' ' + str(ip) + '\n' conf_path = os.path.join(self.args.output_dir, 'sig-testing.conf') write_file(conf_path, text)
def generate_control_service(self): for topo_id, topo in self.args.topo_dicts.items(): ca = 'issuing' in topo.get("attributes", []) for elem_id, elem in topo.get("control_service", {}).items(): # only a single Go-BS per AS is currently supported if elem_id.endswith("-1"): base = topo_id.base_dir(self.args.output_dir) bs_conf = self._build_control_service_conf( topo_id, topo["isd_as"], base, elem_id, elem, ca) write_file(os.path.join(base, elem_id, CS_CONFIG_NAME), toml.dumps(bs_conf))
def _gen_disp_docker(self): for topo_id, topo in self.args.topo_dicts.items(): elem = "disp_sig_%s" % topo_id.file_fmt() elem_dir = os.path.join(topo_id.base_dir(self.args.output_dir), elem) disp_conf = self._build_disp_conf(elem, topo_id) write_file(os.path.join(elem_dir, DISP_CONFIG_NAME), toml.dumps(disp_conf)) for k in list(topo.get("border_routers", {})) + list(topo.get("control_service", {})): disp_id = 'disp_%s' % k elem_dir = os.path.join(topo_id.base_dir(self.args.output_dir), disp_id) disp_conf = self._build_disp_conf(disp_id, topo_id) write_file(os.path.join(elem_dir, DISP_CONFIG_NAME), toml.dumps(disp_conf))
def generate(self): self._create_networks() for topo_id, topo in self.args.topo_dicts.items(): base = os.path.join(self.output_base, topo_id.base_dir(self.args.output_dir)) self._gen_topo(topo_id, topo, base) if self.args.sig: self._gen_sig() docker_utils_gen = DockerUtilsGenerator(self._docker_utils_args()) self.dc_conf = docker_utils_gen.generate() write_file(os.path.join(self.args.output_dir, DOCKER_CONF), yaml.dump(self.dc_conf, default_flow_style=False))
def _sig_json(self, topo_id): sig_cfg = {"ConfigVersion": 1, "ASes": {}} for t_id, topo in self.args.topo_dicts.items(): if topo_id == t_id: continue sig_cfg['ASes'][str(t_id)] = {"Nets": []} net = self.args.networks['sig%s' % t_id.file_fmt()][0] sig_cfg['ASes'][str(t_id)]['Nets'].append(net['net']) cfg = os.path.join(topo_id.base_dir(self.args.output_dir), "sig.json") contents_json = json.dumps(sig_cfg, default=json_default, indent=2) write_file(cfg, contents_json + '\n')
def _write_networks_conf(self, networks: Mapping[ip_network, NetworkDescription], out_file: str): config = configparser.ConfigParser(interpolation=None) for net, net_desc in networks.items(): sub_conf = {} for prog, ip_net in net_desc.ip_net.items(): sub_conf[prog] = ip_net.ip config[net] = sub_conf text = StringIO() config.write(text) write_file(os.path.join(self.args.output_dir, out_file), text.getvalue())
def _write_dc_file(self): name = 'prometheus' prom_dc = { 'version': DOCKER_COMPOSE_CONFIG_VERSION, 'services': { name: { 'image': 'prom/prometheus:v2.6.0', 'container_name': name, 'network_mode': 'host', 'volumes': [self.output_base + '/gen:/prom-config:ro'], 'command': ['--config.file', '/prom-config/prometheus.yml'], } } } write_file(os.path.join(self.args.output_dir, PROM_DC_FILE), yaml.dump(prom_dc, default_flow_style=False))
def _write_config_file(self, config_path, job_dict): scrape_configs = [] for job_name, file_paths in job_dict.items(): scrape_configs.append({ 'job_name': job_name, 'file_sd_configs': [{'files': file_paths}], }) config = { 'global': { 'scrape_interval': '1s', 'evaluation_interval': '1s', 'external_labels': { 'monitor': 'scion-monitor' } }, 'scrape_configs': scrape_configs, } write_file(config_path, yaml.dump(config, default_flow_style=False))
def _sig_toml(self, topo_id, topo): name = 'sig%s' % topo_id.file_fmt() net = self.args.networks[name][0] log_level = 'debug' ipv = 'ipv4' if ipv not in net: ipv = 'ipv6' sciond_net = self.args.networks["sd" + topo_id.file_fmt()][0] ipv = 'ipv4' if ipv not in sciond_net: ipv = 'ipv6' sciond_ip = sciond_net[ipv] sig_conf = { 'sig': { 'id': name, 'sig_config': 'conf/cfg.json', 'isd_as': str(topo_id), 'ip': str(net[ipv]), }, 'sciond_connection': { 'address': socket_address_str(sciond_ip, SD_API_PORT), }, 'log': { 'file': { 'level': log_level, 'path': '/share/logs/%s.log' % name }, 'console': { 'level': 'error', } }, 'metrics': { 'prometheus': '0.0.0.0:%s' % SIG_PROM_PORT }, 'features': { 'sig_egress_v2': True, }, } path = os.path.join(topo_id.base_dir(self.args.output_dir), name, SIG_CONFIG_NAME) write_file(path, toml.dumps(sig_conf))
def _write_as_conf(self, topo_id, entries): config = configparser.ConfigParser(interpolation=None) names = [] base = topo_id.base_dir(self.args.output_dir) for elem, entry in sorted(entries, key=lambda x: x[0]): names.append(elem) elem_dir = os.path.join(base, elem) self._write_elem_conf(elem, entry, elem_dir, topo_id) sd_name = "sd%s" % topo_id.file_fmt() names.append(sd_name) conf_dir = os.path.join(base, COMMON_DIR) config["program:%s" % sd_name] = self._sciond_entry(sd_name, conf_dir) config["group:as%s" % topo_id.file_fmt()] = { "programs": ",".join(names) } text = StringIO() config.write(text) conf_path = os.path.join(topo_id.base_dir(self.args.output_dir), SUPERVISOR_CONF) write_file(conf_path, text.getvalue())
def generate_co(self): if not self.args.colibri: return for topo_id, topo in self.args.topo_dicts.items(): for elem_id, elem in topo.get("colibri_service", {}).items(): # only a single Go-CO per AS is currently supported if elem_id.endswith("-1"): base = topo_id.base_dir(self.args.output_dir) co_conf = self._build_co_conf(topo_id, topo["isd_as"], base, elem_id, elem) write_file(os.path.join(base, elem_id, CO_CONFIG_NAME), toml.dumps(co_conf)) traffic_matrix = self._build_co_traffic_matrix(topo_id) write_file(os.path.join(base, elem_id, 'matrix.yml'), yaml.dump(traffic_matrix, default_flow_style=False)) rsvps = self._build_co_reservations(topo_id) write_file(os.path.join(base, elem_id, 'reservations.yml'), yaml.dump(rsvps, default_flow_style=False))
def generate_co(self): for topo_id, topo in self.args.topo_dicts.items(): for elem_id, elem in topo.get("colibri_service", {}).items(): # only a single Go-CO per AS is currently supported if elem_id.endswith("-1"): base = topo_id.base_dir(self.args.output_dir) co_conf = self._build_co_conf(topo_id, topo["isd_as"], base, elem_id, elem) write_file(os.path.join(base, "%s.toml" % elem_id), toml.dumps(co_conf)) capacities = self._build_co_capacities(topo_id) write_file(os.path.join(base, 'capacities.json'), json.dumps(capacities, indent=2)) rsvps = self._build_co_reservations(topo_id) write_file(os.path.join(base, 'reservations.json'), json.dumps(rsvps, indent=2))
def test_rename_error(self, mkdir, rename): rename.side_effect = PermissionError # Call with self.assertRaises(SCIONIOError): write_file("File_Path", "Text")
def _write_as_topo(self, topo_id, _as_conf): path = os.path.join(topo_id.base_dir(self.args.output_dir), TOPO_FILE) contents_json = json.dumps(self.topo_dicts[topo_id], default=json_default, indent=2) write_file(path, contents_json + '\n')
def test_mkdir_error(self, mkdir): mkdir.side_effect = FileNotFoundError # Call with self.assertRaises(SCIONIOError): write_file("File_Path", "Text")
def generate(self): dc_conf = self._generate_dc() os.makedirs(os.path.join(self.local_jaeger_dir, 'data'), exist_ok=True) os.makedirs(os.path.join(self.local_jaeger_dir, 'key'), exist_ok=True) write_file(os.path.join(self.args.output_dir, JAEGER_DC), yaml.dump(dc_conf, default_flow_style=False))
def _write_ifids(self): list_path = os.path.join(self.args.output_dir, IFIDS_FILE) write_file(list_path, yaml.dump(self.ifid_map, default_flow_style=False))
def _write_as_list(self): list_path = os.path.join(self.args.output_dir, AS_LIST_FILE) write_file(list_path, yaml.dump(dict(self.as_list)))
def test_file_error(self, mkdir): builtins.open.side_effect = PermissionError # Call with self.assertRaises(SCIONIOError): write_file("File_Path", "Text")