Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Printer preflight #48

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion securedrop_export/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions securedrop_export/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions securedrop_export/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
8 changes: 7 additions & 1 deletion securedrop_export/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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":
Expand Down
52 changes: 42 additions & 10 deletions securedrop_export/print/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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()


Expand All @@ -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()