From d66af442b324435271cd3e446d1677803c8afbfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Tue, 26 Nov 2024 17:19:34 +0100 Subject: [PATCH] Add a --debug flag to the CLI to help retrieve more logs. When the flag is set, the `RUNSC_DEBUG=1` environment variable is added to the outer container --- dangerzone/cli.py | 8 +++++++- dangerzone/isolation_provider/base.py | 16 ++++++++++++---- dangerzone/isolation_provider/container.py | 5 +++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/dangerzone/cli.py b/dangerzone/cli.py index 8f68e62..7a0acfb 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -42,6 +42,11 @@ def print_header(s: str) -> None: type=click.UNPROCESSED, callback=args.validate_input_filenames, ) +@click.option( + "--debug", + "debug", + flag_value=True, + help="Run Dangerzone in debug mode, to get logs from gVisor.") @click.version_option(version=get_version(), message="%(version)s") @errors.handle_document_errors def cli_main( @@ -50,6 +55,7 @@ def cli_main( filenames: List[str], archive: bool, dummy_conversion: bool, + debug: bool, ) -> None: setup_logging() @@ -58,7 +64,7 @@ def cli_main( elif is_qubes_native_conversion(): dangerzone = DangerzoneCore(Qubes()) else: - dangerzone = DangerzoneCore(Container()) + dangerzone = DangerzoneCore(Container(debug=debug)) display_banner() if len(filenames) == 1 and output_filename: diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index 7b353b7..59c8b60 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -86,12 +86,20 @@ class IsolationProvider(ABC): Abstracts an isolation provider """ - def __init__(self) -> None: - if getattr(sys, "dangerzone_dev", False) is True: + def __init__(self, debug: bool = False) -> None: + self.debug = debug + if self.should_capture_stderr(): self.proc_stderr = subprocess.PIPE else: self.proc_stderr = subprocess.DEVNULL + def should_capture_stderr(self) -> bool: + return self.debug or getattr(sys, "dangerzone_dev", False) + + @staticmethod + def is_runtime_available() -> bool: + return True + @abstractmethod def install(self) -> bool: pass @@ -344,9 +352,9 @@ class IsolationProvider(ABC): ) # Read the stderr of the process only if: - # * Dev mode is enabled. + # * We're in debug mode # * The process has exited (else we risk hanging). - if getattr(sys, "dangerzone_dev", False) and p.poll() is not None: + if self.should_capture_stderr() and p.poll() is not None: assert p.stderr debug_log = read_debug_text(p.stderr, MAX_CONVERSION_LOG_CHARS) log.info( diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 1a08385..8d3ca05 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -168,6 +168,10 @@ class Container(IsolationProvider): ) -> subprocess.Popen: container_runtime = container_utils.get_runtime() security_args = self.get_runtime_security_args() + debug_args = [] + if self.debug: + debug_args += ["-e", "RUNSC_DEBUG=1"] + enable_stdin = ["-i"] set_name = ["--name", name] prevent_leakage_args = ["--rm"] @@ -177,6 +181,7 @@ class Container(IsolationProvider): args = ( ["run"] + security_args + + debug_args + prevent_leakage_args + enable_stdin + set_name