From 7029486814dd6674097a504430eddd2f5d774caf Mon Sep 17 00:00:00 2001 From: Massimiliano Adamo <maxadamo@gmail.com> Date: Mon, 17 Feb 2025 18:43:26 +0100 Subject: [PATCH] refactor: remove unused imports and clean up code formatting --- wile_coyote/__init__.py | 2 -- wile_coyote/common/__init__.py | 9 ------ wile_coyote/common/combine.py | 7 +++-- wile_coyote/tools/__init__.py | 32 ++++++++++---------- wile_coyote/tools/consul_kit.py | 52 +++++++++++++++++---------------- 5 files changed, 48 insertions(+), 54 deletions(-) diff --git a/wile_coyote/__init__.py b/wile_coyote/__init__.py index a395e3a..3ef15a1 100644 --- a/wile_coyote/__init__.py +++ b/wile_coyote/__init__.py @@ -1,5 +1,3 @@ """Wile Coyote is a Python library for creating and managing virtual environments.""" -import pkgutil - __version__ = "0.8.3" diff --git a/wile_coyote/common/__init__.py b/wile_coyote/common/__init__.py index caeb043..f631c68 100644 --- a/wile_coyote/common/__init__.py +++ b/wile_coyote/common/__init__.py @@ -1,10 +1 @@ """ Common package for Wile Coyote project. """ - -import pkgutil - - -__all__ = [] -for loader, module_name, is_pkg in pkgutil.walk_packages(__path__): - __all__.append(module_name) - _module = loader.find_module(module_name).load_module(module_name) - globals()[module_name] = _module diff --git a/wile_coyote/common/combine.py b/wile_coyote/common/combine.py index f570e5c..52effd2 100644 --- a/wile_coyote/common/combine.py +++ b/wile_coyote/common/combine.py @@ -1,7 +1,8 @@ """Module providing a function to combine certificate, CA and private key""" import os -import wile_coyote.tools +import configparser +import wile_coyote.common.constants def keys(cert_path, provider, key_path, outpath): @@ -13,7 +14,9 @@ def keys(cert_path, provider, key_path, outpath): else: raise NotImplementedError("OS not supported") - ca_file = wile_coyote.tools.PROVIDERS_CA[provider] + config = configparser.RawConfigParser() + config.read(wile_coyote.common.constants.CRED_CONF, encoding="utf8") + ca_file = config.get("ca", provider) ca_path = os.path.join(ssl_dir, ca_file) file_names = [cert_path, ca_path, key_path] diff --git a/wile_coyote/tools/__init__.py b/wile_coyote/tools/__init__.py index c96ef0f..5a1cb2e 100644 --- a/wile_coyote/tools/__init__.py +++ b/wile_coyote/tools/__init__.py @@ -14,21 +14,21 @@ for loader, module_name, is_pkg in pkgutil.walk_packages(__path__): config = configparser.RawConfigParser() config.read(wile_coyote.common.constants.CRED_CONF, encoding="utf8") - ATOMIC_MSG = 'Atomic Check:' - VAULT_HOST = config.get('acme', 'vault_host') - REDIS_HOST = config.get('acme', 'redis_host') - REDIS_TOKEN = config.get('acme', 'redis_token') - CONSUL_SERVERS = config.get('acme', 'consul_servers') - CONSUL_TOKEN = config.get('acme', 'consul_token') - WEB_BASE = config.get('acme', 'web_base') - ACME_PROVIDERS = config.get('acme', 'acme_providers') - PROVIDERS_CA = l_eval(config.get('acme', 'providers_ca')) + ATOMIC_MSG = "Atomic Check:" + VAULT_HOST = config.get("acme", "vault_host") + REDIS_HOST = config.get("acme", "redis_host") + REDIS_TOKEN = config.get("acme", "redis_token") + CONSUL_SERVERS = config.get("acme", "consul_servers") + CONSUL_TOKEN = config.get("acme", "consul_token") + WEB_BASE = config.get("acme", "web_base") + ACME_PROVIDERS = config.get("acme", "acme_providers") + PROVIDERS_CA = l_eval(config.get("acme", "providers_ca")) # these parameters only work in test - if 'unit-test' in config.sections(): - VAULT_ROOT_TOKEN = config.get('acme', 'vault_token_root') - MOUNT_POINTS_V1 = l_eval(config.get('unit-test', 'mount_points_v1')) - MOUNT_POINTS_V2 = l_eval(config.get('unit-test', 'mount_points_v2')) - REDIS_KEYS = sorted(l_eval(config.get('unit-test', 'redis_keys'))) - VAULT_KEYS = sorted(l_eval(config.get('unit-test', 'vault_keys'))) - CONSUL_KEYS = sorted(l_eval(config.get('unit-test', 'consul_keys'))) + if "unit-test" in config.sections(): + VAULT_ROOT_TOKEN = config.get("acme", "vault_token_root") + MOUNT_POINTS_V1 = l_eval(config.get("unit-test", "mount_points_v1")) + MOUNT_POINTS_V2 = l_eval(config.get("unit-test", "mount_points_v2")) + REDIS_KEYS = sorted(l_eval(config.get("unit-test", "redis_keys"))) + VAULT_KEYS = sorted(l_eval(config.get("unit-test", "vault_keys"))) + CONSUL_KEYS = sorted(l_eval(config.get("unit-test", "consul_keys"))) diff --git a/wile_coyote/tools/consul_kit.py b/wile_coyote/tools/consul_kit.py index 28feb71..0e6e5a5 100644 --- a/wile_coyote/tools/consul_kit.py +++ b/wile_coyote/tools/consul_kit.py @@ -13,9 +13,9 @@ import wile_coyote.tools def delete(key, leader): - """ Download key from consul """ + """Download key from consul""" token = wile_coyote.tools.CONSUL_TOKEN - c_client = Consul(host=leader, port='443', token=token, scheme='https') + c_client = Consul(host=leader, port="443", token=token, scheme="https") try: c_client.kv.delete(key) @@ -24,42 +24,42 @@ def delete(key, leader): def get(keyname, log_file, leader): - """ Get key from consul """ + """Get key from consul""" token = wile_coyote.tools.CONSUL_TOKEN - c_client = Consul(host=leader, port='443', token=token, scheme='https') + c_client = Consul(host=leader, port="443", token=token, scheme="https") try: _, data = c_client.kv.get(keyname) except Exception as err: # pylint: disable=w0703 - log.handler( - f'could not connect to Consul {leader}', log_file, True) + log.handler(f"could not connect to Consul {leader}", log_file, True) return err if data: - return data['Value'].decode('utf-8') + return data["Value"].decode("utf-8") - return 'BOFH' + return "BOFH" def get_leader(log_file): - """ detect consul leader to ensure that we - are picking a node which is up & running """ + """detect consul leader to ensure that we + are picking a node which is up & running""" consul_servers = wile_coyote.tools.CONSUL_SERVERS ctx = ssl.create_default_context() - server_list = consul_servers.replace(' ', '').split(',') + server_list = consul_servers.replace(" ", "").split(",") leader = None for server in server_list: try: _leader = urllib.request.urlopen( - f'https://{server}/v1/status/leader', context=ctx) + f"https://{server}/v1/status/leader", context=ctx + ) except Exception: # pylint: disable=W0703 print(111) else: - leader = _leader.read().decode('utf-8').split(':')[0].replace('"', '') + leader = _leader.read().decode("utf-8").split(":")[0].replace('"', "") break if not leader: - log.handler('could not find a Consul leader', log_file, True) + log.handler("could not find a Consul leader", log_file, True) log.handler(wile_coyote.common.constants.GIVEUP, log_file) os._exit(1) @@ -67,31 +67,33 @@ def get_leader(log_file): def put(key, value, log_file, leader): - """ Upload key to consul """ + """Upload key to consul""" token = wile_coyote.tools.CONSUL_TOKEN - c_client = Consul(host=leader, port='443', token=token, scheme='https') + c_client = Consul(host=leader, port="443", token=token, scheme="https") try: c_client.kv.put(key, value) except Exception as err: # pylint: disable=W0703 - log.handler(f'could not write key {key} to {leader}: {err}', - log_file, True) + log.handler(f"could not write key {key} to {leader}: {err}", log_file, True) os._exit(1) def test(log_file, leader): - """ Check consul connection """ + """Check consul connection""" atomic_msg = wile_coyote.tools.ATOMIC_MSG token = wile_coyote.tools.CONSUL_TOKEN - rnd = ''.join([random.choice(string.ascii_letters) for _ in range(8)]) - c_client = Consul(host=leader, port='443', token=token, scheme='https') + rnd = "".join([random.choice(string.ascii_letters) for _ in range(8)]) + c_client = Consul(host=leader, port="443", token=token, scheme="https") try: - _ = c_client.kv.put('do_not_delete', rnd) + _ = c_client.kv.put("do_not_delete", rnd) except Exception as err: # pylint: disable=w0703 - log.handler(f'{atomic_msg} could not write test key to {leader}: {err}', - log_file, True) + log.handler( + f"{atomic_msg} could not write test key to {leader}: {err}", log_file, True + ) log.handler(wile_coyote.common.constants.GIVEUP, log_file) os._exit(1) else: - log.handler(f'{atomic_msg} successfully wrote Consul test key to {leader}', log_file) + log.handler( + f"{atomic_msg} successfully wrote Consul test key to {leader}", log_file + ) -- GitLab