Skip to content

Commit

Permalink
Execute pre-commit-config and apply it
Browse files Browse the repository at this point in the history
  • Loading branch information
TheophileDiot committed Mar 7, 2024
1 parent 84d8c41 commit 2a0f956
Show file tree
Hide file tree
Showing 29 changed files with 256 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ repos:
hooks:
- id: flake8
name: Flake8 Python Linter
args: ["--max-line-length=160", "--ignore=E266,E402,E722,W503"]
args: ["--max-line-length=160", "--ignore=E266,E402,E501,E722,W503"]

- repo: https://github.com/dosisod/refurb
rev: a9a4edd45687e664dee0905ba1c848bda227d1d6 # frozen: v1.28.0
Expand Down
6 changes: 5 additions & 1 deletion src/autoconf/DockerController.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ def apply_config(self) -> bool:
)

def __process_event(self, event):
return "Actor" in event and "Attributes" in event["Actor"] and ("bunkerweb.INSTANCE" in event["Actor"]["Attributes"] or "bunkerweb.SERVER_NAME" in event["Actor"]["Attributes"])
return (
"Actor" in event
and "Attributes" in event["Actor"]
and ("bunkerweb.INSTANCE" in event["Actor"]["Attributes"] or "bunkerweb.SERVER_NAME" in event["Actor"]["Attributes"])
)

def process_events(self):
self._set_autoconf_load_db()
Expand Down
6 changes: 5 additions & 1 deletion src/autoconf/IngressController.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def __init__(self):
self.__networkingv1 = client.NetworkingV1Api()

def _get_controller_instances(self) -> list:
return [pod for pod in self.__corev1.list_pod_for_all_namespaces(watch=False).items if (pod.metadata.annotations and "bunkerweb.io/INSTANCE" in pod.metadata.annotations)]
return [
pod
for pod in self.__corev1.list_pod_for_all_namespaces(watch=False).items
if (pod.metadata.annotations and "bunkerweb.io/INSTANCE" in pod.metadata.annotations)
]

def _to_instances(self, controller_instance) -> List[dict]:
instance = {}
Expand Down
2 changes: 1 addition & 1 deletion src/bw/lua/bunkerweb/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ utils.get_phases = function()
"log_stream",
"log_default",
"timer",
"init_workers"
"init_workers",
}
end

Expand Down
8 changes: 6 additions & 2 deletions src/common/cli/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,19 @@ def __init__(self):
self.__use_redis = False

if self.__use_redis:
self.__logger.info(f"Connected to redis sentinel cluster, getting master with the following parameters:\n{sentinel_master=}\n{redis_db=}\n{username=}\n{password=}")
self.__logger.info(
f"Connected to redis sentinel cluster, getting master with the following parameters:\n{sentinel_master=}\n{redis_db=}\n{username=}\n{password=}"
)
self.__redis = sentinel.master_for(
sentinel_master,
db=redis_db,
username=username,
password=password,
)
else:
self.__logger.info(f"Connecting to redis with the following parameters:\n{redis_host=}\n{redis_port=}\n{redis_db=}\n{username=}\n{password=}\n{redis_timeout=}\nmax_connections={redis_keepalive_pool}\n{redis_ssl=}")
self.__logger.info(
f"Connecting to redis with the following parameters:\n{redis_host=}\n{redis_port=}\n{redis_db=}\n{username=}\n{password=}\n{redis_timeout=}\nmax_connections={redis_keepalive_pool}\n{redis_ssl=}"
)
self.__redis = StrictRedis(
host=redis_host,
port=redis_port,
Expand Down
8 changes: 6 additions & 2 deletions src/common/core/customcert/jobs/custom-cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def check_cert(cert_path: str, key_path: str, first_server: str) -> bool:
else:
logger.info(f"No change for certificate {cert_path}")
elif not cert_path or not key_path:
logger.warning("Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ...")
logger.warning(
"Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ..."
)
cert_cache_path = Path(
sep,
"var",
Expand Down Expand Up @@ -188,7 +190,9 @@ def check_cert(cert_path: str, key_path: str, first_server: str) -> bool:
f"No change for certificate {cert_path}",
)
elif not cert_path or not key_path:
logger.warning("Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ...")
logger.warning(
"Both variables CUSTOM_SSL_CERT and CUSTOM_SSL_KEY (or CUSTOM_SSL_CERT_DATA and CUSTOM_SSL_KEY_DATA) have to be set to use custom certificates, clearing cache ..."
)
cert_cache_path = Path(
sep,
"var",
Expand Down
11 changes: 10 additions & 1 deletion src/common/core/letsencrypt/jobs/certbot-new.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,16 @@ def certbot_new(domains: str, email: str, use_letsencrypt_staging: bool = False)
domains_sever_names = {server_names[0]: all_domains}

proc = run(
[CERTBOT_BIN, "certificates", "--config-dir", LETS_ENCRYPT_PATH.joinpath("etc").as_posix(), "--work-dir", LETS_ENCRYPT_WORK_DIR, "--logs-dir", LETS_ENCRYPT_LOGS_DIR],
[
CERTBOT_BIN,
"certificates",
"--config-dir",
LETS_ENCRYPT_PATH.joinpath("etc").as_posix(),
"--work-dir",
LETS_ENCRYPT_WORK_DIR,
"--logs-dir",
LETS_ENCRYPT_LOGS_DIR,
],
stdin=DEVNULL,
stdout=PIPE,
stderr=STDOUT,
Expand Down
16 changes: 12 additions & 4 deletions src/common/core/pro/jobs/download-pro-plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ def install_plugin(plugin_dir: str, db, preview: bool = True) -> bool:
break

if old_version == metadata["version"]:
logger.warning(f"Skipping installation of {'preview version of ' if preview else ''}Pro plugin {metadata['id']} (version {metadata['version']} already installed)")
logger.warning(
f"Skipping installation of {'preview version of ' if preview else ''}Pro plugin {metadata['id']} (version {metadata['version']} already installed)"
)
return False

logger.warning(f"{'Preview version of ' if preview else ''}Pro plugin {metadata['id']} is already installed but version {metadata['version']} is different from database ({old_version}), updating it...")
logger.warning(
f"{'Preview version of ' if preview else ''}Pro plugin {metadata['id']} is already installed but version {metadata['version']} is different from database ({old_version}), updating it..."
)
rmtree(PRO_PLUGINS_DIR.joinpath(metadata["id"]), ignore_errors=True)

# Copy the plugin
Expand Down Expand Up @@ -180,9 +184,13 @@ def install_plugin(plugin_dir: str, db, preview: bool = True) -> bool:

if not metadata["is_pro"]:
if metadata["pro_overlapped"]:
message = f"You have exceeded the number of services allowed by your BunkerWeb Pro license: {metadata['pro_services']} (current: {data['service_number']}"
message = (
f"You have exceeded the number of services allowed by your BunkerWeb Pro license: {metadata['pro_services']} (current: {data['service_number']}"
)
elif pro_license_key:
message = "Your BunkerWeb Pro license " + (STATUS_MESSAGES.get(metadata["pro_status"], "is not valid or has expired") if not error else "is not valid or has expired")
message = "Your BunkerWeb Pro license " + (
STATUS_MESSAGES.get(metadata["pro_status"], "is not valid or has expired") if not error else "is not valid or has expired"
)
else:
logger.info("If you wish to purchase a BunkerWeb Pro license, please visit https://panel.bunkerweb.io/")
message = "No BunkerWeb Pro license key provided"
Expand Down
4 changes: 3 additions & 1 deletion src/common/core/selfsigned/jobs/self-signed.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def generate_cert(first_server: str, days: str, subj: str, self_signed_path: Pat
if sorted(attribute.rfc4514_string() for attribute in certificate.subject) != sorted(v for v in subj.split("/") if v):
logger.warning(f"Subject of self-signed certificate for {first_server} is different from the one in the configuration, regenerating ...")
elif certificate.not_valid_after - certificate.not_valid_before != timedelta(days=int(days)):
logger.warning(f"Expiration date of self-signed certificate for {first_server} is different from the one in the configuration, regenerating ...")
logger.warning(
f"Expiration date of self-signed certificate for {first_server} is different from the one in the configuration, regenerating ..."
)
else:
return True, 0

Expand Down
15 changes: 12 additions & 3 deletions src/common/gen/Configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def __validate_plugin(self, plugin: dict) -> Tuple[bool, str]:
return (False, f"missing keys for setting {setting} in plugin {plugin['id']}, must have context, default, help, id, label, regex and type")

if not self.__setting_id_rx.match(setting):
return (False, f"Invalid setting name for setting {setting} in plugin {plugin['id']} (Can only contain capital letters and underscores (min 1 characters and max 256))")
return (
False,
f"Invalid setting name for setting {setting} in plugin {plugin['id']} (Can only contain capital letters and underscores (min 1 characters and max 256))",
)
elif data["context"] not in ("global", "multisite"):
return (False, f"Invalid context for setting {setting} in plugin {plugin['id']} (Must be global or multisite)")
elif len(data["default"]) > 4096:
Expand All @@ -297,7 +300,10 @@ def __validate_plugin(self, plugin: dict) -> Tuple[bool, str]:

if "multiple" in data:
if not self.__name_rx.match(data["multiple"]):
return (False, f"Invalid multiple for setting {setting} in plugin {plugin['id']} (Can only contain numbers, letters, underscores and hyphens (min 1 characters and max 128))")
return (
False,
f"Invalid multiple for setting {setting} in plugin {plugin['id']} (Can only contain numbers, letters, underscores and hyphens (min 1 characters and max 128))",
)

for select in data.get("select", []):
if len(select) > 256:
Expand All @@ -310,7 +316,10 @@ def __validate_plugin(self, plugin: dict) -> Tuple[bool, str]:
if not self.__name_rx.match(job["name"]):
return (False, f"Invalid name for job {job['name']} in plugin {plugin['id']}")
elif not self.__job_file_rx.match(job["file"]):
return (False, f"Invalid file for job {job['name']} in plugin {plugin['id']} (Can only contain numbers, letters, underscores, hyphens and slashes (min 1 characters and max 256))")
return (
False,
f"Invalid file for job {job['name']} in plugin {plugin['id']} (Can only contain numbers, letters, underscores, hyphens and slashes (min 1 characters and max 256))",
)
elif job["every"] not in ("once", "minute", "hour", "day", "week"):
return (False, f"Invalid every for job {job['name']} in plugin {plugin['id']} (Must be once, minute, hour, day or week)")
elif job["reload"] is not True and job["reload"] is not False:
Expand Down
8 changes: 6 additions & 2 deletions src/common/gen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
# Parse arguments
parser = ArgumentParser(description="BunkerWeb config generator")
parser.add_argument("--settings", default=join(sep, "usr", "share", "bunkerweb", "settings.json"), type=str, help="file containing the main settings")
parser.add_argument("--templates", default=join(sep, "usr", "share", "bunkerweb", "confs"), type=str, help="directory containing the main template files")
parser.add_argument(
"--templates", default=join(sep, "usr", "share", "bunkerweb", "confs"), type=str, help="directory containing the main template files"
)
parser.add_argument("--core", default=join(sep, "usr", "share", "bunkerweb", "core"), type=str, help="directory containing the core plugins")
parser.add_argument("--plugins", default=join(sep, "etc", "bunkerweb", "plugins"), type=str, help="directory containing the external plugins")
parser.add_argument("--pro-plugins", default=join(sep, "etc", "bunkerweb", "pro", "plugins"), type=str, help="directory containing the pro plugins")
Expand Down Expand Up @@ -106,7 +108,9 @@

# Compute the config
logger.info("Computing config ...")
config: Dict[str, Any] = Configurator(str(settings_path), str(core_path), str(plugins_path), str(pro_plugins_path), str(variables_path), logger).get_config()
config: Dict[str, Any] = Configurator(
str(settings_path), str(core_path), str(plugins_path), str(pro_plugins_path), str(variables_path), logger
).get_config()
else:
if join(sep, "usr", "share", "bunkerweb", "db") not in sys_path:
sys_path.append(join(sep, "usr", "share", "bunkerweb", "db"))
Expand Down
12 changes: 9 additions & 3 deletions src/common/gen/save_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def get_instance_configs_and_apis(instance: Any, db, _type="Docker"):
),
}
)
logger.info(f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}")
logger.info(
f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}"
)
else:
tmp_config[split[0]] = split[1]

Expand Down Expand Up @@ -175,7 +177,9 @@ def get_instance_configs_and_apis(instance: Any, db, _type="Docker"):
),
}
)
logger.info(f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}")
logger.info(
f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}"
)

db = Database(logger, config_files.get("DATABASE_URI", None), pool=False)
else:
Expand Down Expand Up @@ -206,7 +210,9 @@ def get_instance_configs_and_apis(instance: Any, db, _type="Docker"):
),
}
)
logger.info(f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}")
logger.info(
f"Found custom conf env var {'for service ' + custom_conf[0] if custom_conf[0] else 'without service'} with type {custom_conf[1]} and name {custom_conf[2]}"
)
else:
tmp_config[split[0]] = split[1]

Expand Down
8 changes: 6 additions & 2 deletions src/common/utils/ConfigCaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ConfigCaller:
def __init__(self):
self.__logger = setup_logger("Config", "INFO")
self._settings = loads(Path(sep, "usr", "share", "bunkerweb", "settings.json").read_text(encoding="utf-8"))
for plugin in glob(join(sep, "usr", "share", "bunkerweb", "core", "*", "plugin.json")) + glob(join(sep, "etc", "bunkerweb", "plugins", "*", "plugin.json")):
for plugin in glob(join(sep, "usr", "share", "bunkerweb", "core", "*", "plugin.json")) + glob(
join(sep, "etc", "bunkerweb", "plugins", "*", "plugin.json")
):
try:
self._settings.update(loads(Path(plugin).read_text(encoding="utf-8"))["settings"])
except KeyError:
Expand All @@ -36,7 +38,9 @@ def _is_setting_context(self, setting: str, context: Union[Literal["global"], Li
return self._settings[setting]["context"] == context
elif match(r"^.+_\d+$", setting):
multiple_setting = "_".join(setting.split("_")[:-1])
return self._is_setting(multiple_setting) and self._settings[multiple_setting]["context"] == context and "multiple" in self._settings[multiple_setting]
return (
self._is_setting(multiple_setting) and self._settings[multiple_setting]["context"] == context and "multiple" in self._settings[multiple_setting]
)
return False

def _full_env(self, env_instances: Dict[str, Any], env_services: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
Loading

0 comments on commit 2a0f956

Please sign in to comment.