diff --git a/dangerzone/container_utils.py b/dangerzone/container_utils.py index 88e4565..99fd2f5 100644 --- a/dangerzone/container_utils.py +++ b/dangerzone/container_utils.py @@ -14,6 +14,11 @@ CONTAINER_NAME = "ghcr.io/freedomofpress/dangerzone/dangerzone" log = logging.getLogger(__name__) +def subprocess_run(*args, **kwargs) -> subprocess.CompletedProcess: + """subprocess.run with the correct startupinfo for Windows.""" + return subprocess.run(*args, startupinfo=get_subprocess_startupinfo(), **kwargs) + + def get_runtime_name() -> str: if platform.system() == "Linux": return "podman" @@ -39,9 +44,8 @@ def get_runtime_version() -> Tuple[int, int]: cmd = [runtime, "version", "-f", query] try: - version = subprocess.run( + version = subprocess_run( cmd, - startupinfo=get_subprocess_startupinfo(), capture_output=True, check=True, ).stdout.decode() @@ -144,8 +148,7 @@ def load_image_tarball_from_gzip() -> None: def load_image_tarball_from_tar(tarball_path: str) -> None: cmd = [get_runtime(), "load", "-i", tarball_path] - subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) - + subprocess_run(cmd, check=True) log.info("Successfully installed container image from %s", tarball_path) @@ -156,7 +159,7 @@ def tag_image_by_digest(digest: str, tag: str) -> None: image_id = get_image_id_by_digest(digest) cmd = [get_runtime(), "tag", image_id, tag] log.debug(" ".join(cmd)) - subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) + subprocess_run(cmd, check=True) def get_image_id_by_digest(digest: str) -> str: @@ -172,9 +175,7 @@ def get_image_id_by_digest(digest: str) -> str: "{{.Id}}", ] log.debug(" ".join(cmd)) - process = subprocess.run( - cmd, startupinfo=get_subprocess_startupinfo(), check=True, capture_output=True - ) + process = subprocess_run(cmd, check=True, capture_output=True) # In case we have multiple lines, we only want the first one. return process.stdout.decode().strip().split("\n")[0] @@ -183,7 +184,7 @@ def container_pull(image: str, manifest_digest: str): """Pull a container image from a registry.""" cmd = [get_runtime_name(), "pull", f"{image}@sha256:{manifest_digest}"] try: - subprocess.run(cmd, startupinfo=get_subprocess_startupinfo(), check=True) + subprocess_run(cmd, check=True) except subprocess.CalledProcessError as e: raise errors.ContainerPullException( f"Could not pull the container image: {e}" @@ -199,7 +200,11 @@ def get_local_image_digest(image: str) -> str: cmd = [get_runtime_name(), "images", image, "--format", "{{.Digest}}"] log.debug(" ".join(cmd)) try: - result = subprocess.run(cmd, capture_output=True, check=True) + result = subprocess_run( + cmd, + capture_output=True, + check=True, + ) lines = result.stdout.decode().strip().split("\n") if len(lines) != 1: raise errors.MultipleImagesFoundException(