Esempio n. 1
0
def parse(raw_args=None):
    """Process input arguments and runs requested operations.

    :param list raw_args: List of arguments
    :returns: parsed arguments
    :rtype: argparse.Namespace
    """
    parser = _build_parser()
    mrcrypt_args = parser.parse_args(raw_args)
    if mrcrypt_args.command is None:
        return parser.format_help()

    try:
        if mrcrypt_args.command == 'encrypt':
            if mrcrypt_args.encryption_context is not None and not isinstance(mrcrypt_args.encryption_context, dict):
                return 'Invalid dictionary in encryption context argument'

        aws_encryption_sdk_cli.setup_logger(
            verbosity=mrcrypt_args.verbose,
            quiet=False
        )

        encryption_cli_args = _transform_args(mrcrypt_args)
        crypto_materials_manager = _build_crypto_materials_manager(encryption_cli_args)
        stream_args = aws_encryption_sdk_cli.stream_kwargs_from_args(encryption_cli_args, crypto_materials_manager)

        aws_encryption_sdk_cli.process_cli_request(
            stream_args=stream_args,
            parsed_args=encryption_cli_args
        )

        # NOTE: If any post-processing of the files created as a result of this operation
        # is desired, the best way to approach this would be to use the output metadata to
        # collect a listing of all written files.
        # http://aws-encryption-sdk-cli.readthedocs.io/en/latest/#output-metadata

        return None
    except AWSEncryptionSDKCLIError as error:
        return error.args[0]
    except Exception as error:  # pylint: disable=broad-except
        message = os.linesep.join([
            'Encountered unexpected error: increase verbosity to see details.',
            '{cls}({args})'.format(
                cls=error.__class__.__name__,
                args=', '.join(['"{}"'.format(arg) for arg in error.args])
            )
        ])
        _LOGGER.debug(message)
        # copy.deepcopy can't handle raw exc_info objects, so format it first
        formatted_traceback = traceback.format_exc()
        _LOGGER.debug(formatted_traceback)
        return message
def test_stream_kwargs_from_args(args, stream_args):
    assert aws_encryption_sdk_cli.stream_kwargs_from_args(
        args, sentinel.materials_manager) == stream_args