From 6ddd411be8bd0e39ea5c4b5e9103e8cac77e6ebb Mon Sep 17 00:00:00 2001 From: deeplow Date: Thu, 21 Jul 2022 11:44:01 +0100 Subject: [PATCH] add type get_container_runtime & handle no runtime There was no code to handle if at this stage the runtime existed. This caused issues with type hints since `shutil.which()` can return None, which had not previously been accounted for. We did not use the opportunity to consolidate all the code for detecting the runtime, to make this review easier. --- dangerzone/container.py | 6 ++++++ dangerzone/global_common.py | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dangerzone/container.py b/dangerzone/container.py index 930de7a..cd7eb8d 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -52,11 +52,17 @@ def exec(args, stdout_callback=None): def exec_container(command, extra_args=[], stdout_callback=None): if container_tech == "podman": container_runtime = shutil.which("podman") + if container_runtime is None: + raise Exception(f"podman is not installed") + platform_args = [] security_args = ["--security-opt", "no-new-privileges"] security_args += ["--userns", "keep-id"] else: container_runtime = shutil.which("docker") + if container_runtime is None: + raise Exception(f"docker is not installed") + platform_args = ["--platform", "linux/amd64"] security_args = ["--security-opt=no-new-privileges:true"] diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 35f4da9..a979ff9 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -386,11 +386,15 @@ class GlobalCommon(object): ) print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") - def get_container_runtime(self): + def get_container_runtime(self) -> str: if platform.system() == "Linux": - return shutil.which("podman") + runtime_name = "podman" else: - return shutil.which("docker") + runtime_name = "docker" + runtime = shutil.which(runtime_name) + if runtime is None: + raise Exception(f"{runtime_name} is not installed") + return runtime def get_resource_path(self, filename: str) -> str: if getattr(sys, "dangerzone_dev", False):