def _get_x509_certificate(cls, buffer: AnyStr, buffer_type: int): try: return crypto.load_certificate(buffer_type, cls._bytes(buffer)) except crypto.Error as crypto_error: format_name = {crypto.FILETYPE_PEM: 'PEM', crypto.FILETYPE_ASN1: 'ASN1'}[buffer_type] log.get_file_logger(cls).exception(f'Failed to initialize certificate: Invalid {format_name} contents') raise ValueError(f'Not a valid {format_name} certificate content') from crypto_error
def _log_cli_invoke_completed(cls, action_name: str, started_at: float, exit_status: int): seconds = int(time.time() - started_at) duration = time.strftime('%M:%S', time.gmtime(seconds)) msg = f'Completed {cls.__name__} {action_name} in {duration} with status code {exit_status}' file_logger = log.get_file_logger(cls) file_logger.debug(Colors.MAGENTA('-' * len(msg))) file_logger.debug(Colors.MAGENTA(msg)) file_logger.debug(Colors.MAGENTA('-' * len(msg)))
def _log_exec_completed(self): duration = time.strftime('%M:%S', time.gmtime(self.duration)) file_logger = log.get_file_logger(self.__class__) file_logger.debug('STDOUT: %s', self.stdout) file_logger.debug('STDERR: %s', self.stderr) self.logger.debug( f'Completed "{self.safe_form}" with returncode {self.returncode} in {duration}' )
def _log_cli_invoke_started(cls): exec_line = f'Execute {" ".join(map(shlex.quote, sys.argv))}' install_line = f'From {pathlib.Path(__file__).parent.parent.resolve()}' version_line = f'Using Python {platform.python_version()} on {platform.system()} {platform.release()}' separator = '-' * max(len(exec_line), len(version_line), len(install_line)) file_logger = log.get_file_logger(cls) file_logger.debug(Colors.MAGENTA(separator)) file_logger.debug(Colors.MAGENTA(exec_line)) file_logger.debug(Colors.MAGENTA(install_line)) file_logger.debug(Colors.MAGENTA(version_line)) file_logger.debug(Colors.MAGENTA(separator))
def _handle_generic_exception(cls, action_name: str) -> int: logger = log.get_logger(cls) message = ( f'Executing {cls.__name__} action {action_name} failed unexpectedly. ' f'Detailed logs are available at "{log.get_log_path()}". ' f'To see more details about the error, add `--verbose` command line option.' ) logger.warning(Colors.RED(message)) file_logger = log.get_file_logger(cls) file_logger.exception('Exception traceback:') return 9
def _get_pkey(cls, buffer: AnyStr, passphrase: bytes): try: return crypto.load_privatekey(crypto.FILETYPE_PEM, cls._bytes(buffer), passphrase) except crypto.Error as crypto_error: file_logger = log.get_file_logger(cls) for reason in crypto_error.args[0]: if 'bad decrypt' in reason: file_logger.exception('Failed to initialize private key: Invalid password') raise ValueError('Invalid private key passphrase') from crypto_error file_logger.exception('Failed to initialize private key: Invalid PEM contents') raise ValueError('Invalid private key PEM content') from crypto_error