Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen: Refactor command line arguments #2092

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions python/topology/ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@
get_ca_cert_file_path,
get_ca_private_key_file_path,
)
from topology.common import ArgsTopoConfig


class CAGenArgs(ArgsTopoConfig):
pass


class CAGenerator(object):
def __init__(self, topo_config):
self.topo_config = topo_config
def __init__(self, args):
"""
:param CAGenArgs args: Contains the passed command line
arguments and the topo config.
"""
self.args = args
self.ca_key_pairs = {}
self.ca_certs = defaultdict(dict)
self.ca_private_key_files = defaultdict(dict)
Expand All @@ -45,7 +54,7 @@ def generate(self):
return self.ca_private_key_files, self.ca_cert_files, self.ca_certs

def _iterate(self, f):
for ca_name, ca_config in self.topo_config["CAs"].items():
for ca_name, ca_config in self.args.config["CAs"].items():
f(ca_name, ca_config)

def _gen_ca_key(self, ca_name, ca_config):
Expand Down
17 changes: 12 additions & 5 deletions python/topology/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
get_online_key_file_path,
)
from lib.errors import SCIONParseError
from topology.common import TopoID
from topology.common import ArgsTopoConfig, TopoID

INITIAL_CERT_VERSION = 1
INITIAL_TRC_VERSION = 1
Expand All @@ -60,11 +60,18 @@
DEFAULT_KEYGEN_ALG = 'ed25519'


class CertGenArgs(ArgsTopoConfig):
pass


class CertGenerator(object):
def __init__(self, topo_config, ca_certs):
self.topo_config = topo_config
def __init__(self, args):
"""
:param CertGenArgs args: Contains the passed command line
arguments and the parsed topo config.
"""
self.args = args
self.core_count = defaultdict(int)
self.ca_certs = ca_certs
self.sig_priv_keys = {}
self.sig_pub_keys = {}
self.enc_priv_keys = {}
Expand Down Expand Up @@ -99,7 +106,7 @@ def _self_sign_keys(self):
self.enc_pub_keys[topo_id], self.enc_priv_keys[topo_id] = generate_enc_keypair()

def _iterate(self, f):
for isd_as, as_conf in self.topo_config["ASes"].items():
for isd_as, as_conf in self.args.config["ASes"].items():
f(TopoID(isd_as), as_conf)

def _count_cores(self, topo_id, as_conf):
Expand Down
28 changes: 28 additions & 0 deletions python/topology/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@
)


class ArgsBase:
def __init__(self, args):
for k, v in vars(args).items():
setattr(self, k, v)


class ArgsTopoConfig(ArgsBase):
def __init__(self, args, topo_config):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_config: The parsed topology config.
"""
super().__init__(args)
self.config = topo_config


class ArgsTopoDicts(ArgsBase):
def __init__(self, args, topo_dicts, port_gen=None):
"""
:param object args: Contains the passed command line arguments as named attributes.
:param dict topo_dicts: The generated topo dicts from TopoGenerator.
:param PortGenerator port_gen: The port generator
"""
super().__init__(args)
self.topo_dicts = topo_dicts
self.port_gen = port_gen


class TopoID(ISD_AS):
def ISD(self):
return "ISD%s" % self.isd_str()
Expand Down
Loading