From d3bd8ee57ecfedf1928b44b4d1a40555f011225f Mon Sep 17 00:00:00 2001 From: Allie Crevier Date: Tue, 14 Jan 2020 15:52:53 -0800 Subject: [PATCH] add start-vm and printer-preflight check --- securedrop_export/entrypoint.py | 3 +- securedrop_export/exceptions.py | 1 + securedrop_export/export.py | 2 ++ securedrop_export/main.py | 8 ++++- securedrop_export/print/actions.py | 52 ++++++++++++++++++++++++------ 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/securedrop_export/entrypoint.py b/securedrop_export/entrypoint.py index ed1d69b..d19bbbe 100755 --- a/securedrop_export/entrypoint.py +++ b/securedrop_export/entrypoint.py @@ -58,8 +58,9 @@ def start(): main.__main__(my_sub) # Delete extracted achive from tempfile shutil.rmtree(my_sub.tmpdir) - except Exception: + except Exception as e: # exit with 0 return code otherwise the os will attempt to open # the file with another application + logger.error(e) msg = "ERROR_GENERIC" my_sub.exit_gracefully(msg) diff --git a/securedrop_export/exceptions.py b/securedrop_export/exceptions.py index 1c14bc6..e144a16 100644 --- a/securedrop_export/exceptions.py +++ b/securedrop_export/exceptions.py @@ -22,6 +22,7 @@ class ExportStatus(Enum): USB_DISK_ERROR = 'USB_DISK_ERROR' # Printer preflight related errors + ERROR_MULTIPLE_PRINTERS_FOUND = 'ERROR_MULTIPLE_PRINTERS_FOUND' ERROR_PRINTER_NOT_FOUND = 'ERROR_PRINTER_NOT_FOUND' ERROR_PRINTER_NOT_SUPPORTED = 'ERROR_PRINTER_NOT_SUPPORTED' ERROR_PRINTER_DRIVER_UNAVAILABLE = 'ERROR_PRINTER_DRIVER_UNAVAILABLE' diff --git a/securedrop_export/export.py b/securedrop_export/export.py index dce797e..814a034 100755 --- a/securedrop_export/export.py +++ b/securedrop_export/export.py @@ -23,11 +23,13 @@ class Metadata(object): METADATA_FILE = "metadata.json" SUPPORTED_EXPORT_METHODS = [ + "start-vm", "usb-test", # general preflight check "disk", "disk-test", # disk preflight test "printer", "printer-test", # print test page + "printer-preflight" ] SUPPORTED_ENCRYPTION_METHODS = ["luks"] diff --git a/securedrop_export/main.py b/securedrop_export/main.py index b68dce1..bbdb25f 100755 --- a/securedrop_export/main.py +++ b/securedrop_export/main.py @@ -2,7 +2,8 @@ from securedrop_export import export from securedrop_export.exceptions import ExportStatus -from securedrop_export.print.actions import PrintExportAction, PrintTestPageAction +from securedrop_export.print.actions import PrintExportAction, PrintTestPageAction, \ + PrintPreflightAction from securedrop_export.disk.actions import DiskTestAction, DiskExportAction, USBTestAction logger = logging.getLogger(__name__) @@ -19,12 +20,17 @@ def __main__(submission): if not submission.archive_metadata.is_valid(): submission.exit_gracefully(ExportStatus.ERROR_ARCHIVE_METADATA.value) + if submission.archive_metadata.export_method == "start-vm": + submission.exit_gracefully('') + if submission.archive_metadata.export_method == "usb-test": action = USBTestAction(submission) elif submission.archive_metadata.export_method == "disk": action = DiskExportAction(submission) elif submission.archive_metadata.export_method == "disk-test": action = DiskTestAction(submission) + elif submission.archive_metadata.export_method == "printer-preflight": + action = PrintPreflightAction(submission) elif submission.archive_metadata.export_method == "printer": action = PrintExportAction(submission) elif submission.archive_metadata.export_method == "printer-test": diff --git a/securedrop_export/print/actions.py b/securedrop_export/print/actions.py index d237252..9f2a026 100644 --- a/securedrop_export/print/actions.py +++ b/securedrop_export/print/actions.py @@ -50,6 +50,36 @@ def wait_for_print(self): self.submission.exit_gracefully(ExportStatus.ERROR_PRINT.value) return True + def check_printer_setup(self) -> None: + try: + logger.info('Searching for printer') + output = subprocess.check_output(["sudo", "lpinfo", "-v"]) + printers = [x for x in output.decode('utf-8').split() if "usb://" in x] + if not printers: + logger.info('No usb printers connected') + self.submission.exit_gracefully(ExportStatus.ERROR_PRINTER_NOT_FOUND.value) + + supported_printers = \ + [p for p in printers if any(sub in p for sub in ("Brother", "LaserJet"))] + if not supported_printers: + logger.info('{} are unsupported printers'.format(printers)) + self.submission.exit_gracefully(ExportStatus.ERROR_PRINTER_NOT_SUPPORTED.value) + + if len(supported_printers) > 1: + logger.info('Too many usb printers connected') + self.submission.exit_gracefully(ExportStatus.ERROR_MULTIPLE_PRINTERS_FOUND.value) + + printer_uri = printers[0] + + logger.info('Installing printer drivers') + printer_ppd = self.install_printer_ppd(printer_uri) + + logger.info('Setting up printer') + self.setup_printer(printer_uri, printer_ppd) + except subprocess.CalledProcessError as e: + logger.error(e) + self.submission.exit_gracefully(ExportStatus.ERROR_GENERIC.value) + def get_printer_uri(self): # Get the URI via lpinfo and only accept URIs of supported printers printer_uri = "" @@ -204,14 +234,8 @@ def __init__(self, *args, **kwargs): def run(self): logger.info('Export archive is printer') + self.check_printer_setup() # prints all documents in the archive - logger.info('Searching for printer') - printer_uri = self.get_printer_uri() - logger.info('Installing printer drivers') - printer_ppd = self.install_printer_ppd(printer_uri) - logger.info('Setting up printer') - self.setup_printer(printer_uri, printer_ppd) - logger.info('Printing files') self.print_all_files() @@ -220,8 +244,16 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def run(self): + logger.info('Export archive is printer-test') + self.check_printer_setup() # Prints a test page to ensure the printer is functional - printer_uri = self.get_printer_uri() - printer_ppd = self.install_printer_ppd(printer_uri) - self.setup_printer(printer_uri, printer_ppd) self.print_test_page() + + +class PrintPreflightAction(PrintAction): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def run(self): + logger.info('Export archive is printer-preflight') + self.check_printer_setup()