def test_parse_args_wrapping_keys_required(): args = "-d -S -o - -i -" with pytest.raises(SystemExit) as excinfo: arg_parsing.parse_args(shlex.split(args)) assert excinfo.value.args == (2,)
def test_parse_args_error_raised_in_post_processing( patch_build_parser, patch_process_wrapping_key_provider_configs, patch_process_encryption_context, patch_process_caching_config, ): patch_build_parser.return_value.parse_args.return_value = MagicMock( version=False, dummy_redirect=None, required_encryption_context_keys=None ) patch_process_caching_config.side_effect = ParameterParseError arg_parsing.parse_args() patch_build_parser.return_value.error.assert_called_once_with()
def test_parser_fromfile(tmpdir, argstring, attribute, value): argfile = tmpdir.join("argfile") if "--discovery" not in argstring: argstring += " --discovery=true" argfile.write(argstring) parsed = arg_parsing.parse_args(["@{}".format(argfile)]) assert getattr(parsed, attribute) == value
def test_parse_args( patch_build_parser, patch_process_master_key_provider_configs, patch_process_encryption_context, patch_process_caching_config, ): mock_parsed_args = MagicMock( master_keys=sentinel.raw_keys, encryption_context=sentinel.raw_encryption_context, required_encryption_context_keys=None, caching=sentinel.raw_caching, action=sentinel.action, version=False, dummy_redirect=None, ) patch_build_parser.return_value.parse_args.return_value = mock_parsed_args test = arg_parsing.parse_args(sentinel.raw_args) patch_build_parser.assert_called_once_with() patch_build_parser.return_value.parse_args.assert_called_once_with(args=sentinel.raw_args) patch_process_master_key_provider_configs.assert_called_once_with(sentinel.raw_keys, sentinel.action) assert test.master_keys is patch_process_master_key_provider_configs.return_value patch_process_encryption_context.assert_called_once_with( action=sentinel.action, raw_encryption_context=sentinel.raw_encryption_context, raw_required_encryption_context_keys=None, ) assert test.encryption_context is sentinel.encryption_context assert test.required_encryption_context_keys is sentinel.required_keys patch_process_caching_config.assert_called_once_with(sentinel.raw_caching) assert test.caching is patch_process_caching_config.return_value assert test is mock_parsed_args
def test_parse_args_no_caching_config( patch_build_parser, patch_process_wrapping_key_provider_configs, patch_process_encryption_context, patch_process_caching_config, ): patch_build_parser.return_value.parse_args.return_value = MagicMock(caching=None) test = arg_parsing.parse_args() assert not patch_process_caching_config.called assert test.caching is None
def test_parse_args_dummy_redirect( patch_build_parser, patch_process_master_key_provider_configs, patch_process_encryption_context, patch_process_caching_config, ): mock_parsed_args = MagicMock( master_keys=sentinel.raw_keys, encryption_context=sentinel.raw_encryption_context, caching=sentinel.raw_caching, action=sentinel.action, version=False, dummy_redirect="-invalid-argument", ) patch_build_parser.return_value.parse_args.return_value = mock_parsed_args arg_parsing.parse_args(sentinel.raw_args) patch_build_parser.return_value.error.assert_called_once_with( 'Found invalid argument "-invalid-argument". Did you mean "--invalid-argument"?' )
def test_parse_args( patch_build_parser, patch_process_wrapping_key_provider_configs, patch_process_encryption_context, patch_process_caching_config, ): mock_parsed_args = MagicMock( wrapping_keys=sentinel.raw_keys, discovery=sentinel.discovery, discovery_account=sentinel.discovery_account, discovery_partition=sentinel.discovery_partition, encryption_context=sentinel.raw_encryption_context, required_encryption_context_keys=None, caching=sentinel.raw_caching, action=sentinel.action, version=False, dummy_redirect=None, commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT, ) patch_build_parser.return_value.parse_args.return_value = mock_parsed_args test = arg_parsing.parse_args(sentinel.raw_args) patch_build_parser.assert_called_once_with() patch_build_parser.return_value.parse_args.assert_called_once_with( args=sentinel.raw_args) patch_process_wrapping_key_provider_configs.assert_called_once_with( sentinel.raw_keys, sentinel.action, sentinel.discovery, { "account_ids": sentinel.discovery_account, "partition": sentinel.discovery_partition }, ) assert test.wrapping_keys is patch_process_wrapping_key_provider_configs.return_value patch_process_encryption_context.assert_called_once_with( action=sentinel.action, raw_encryption_context=sentinel.raw_encryption_context, raw_required_encryption_context_keys=None, ) assert test.encryption_context is sentinel.encryption_context assert test.required_encryption_context_keys is sentinel.required_keys patch_process_caching_config.assert_called_once_with(sentinel.raw_caching) assert test.caching is patch_process_caching_config.return_value assert test is mock_parsed_args
def cli(raw_args=None): # type: (List[str]) -> Union[str, None] """CLI entry point. Processes arguments, sets up the key provider, and processes requested action. :returns: Execution return value intended for ``sys.exit()`` """ try: args = parse_args(raw_args) setup_logger(args.verbosity, args.quiet) # pylint: disable=no-member _LOGGER.debug("Encryption mode: %s", args.action) # pylint: disable=no-member _LOGGER.debug("Encryption source: %s", args.input) # pylint: disable=no-member _LOGGER.debug("Encryption destination: %s", args.output) # pylint: disable=no-member _LOGGER.debug("Wrapping key provider configuration: %s", args.wrapping_keys) # pylint: disable=no-member _LOGGER.debug("Suffix requested: %s", args.suffix) # pylint: disable=no-member _warn_deprecated_python() crypto_materials_manager = build_crypto_materials_manager_from_args( key_providers_config=args.wrapping_keys, caching_config=args.caching ) stream_args = stream_kwargs_from_args(args, crypto_materials_manager) process_cli_request(stream_args, args) 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_invalid_discovery(argstring): with pytest.raises(SystemExit) as excinfo: arg_parsing.parse_args(shlex.split(argstring)) assert excinfo.value.args == (2,)
def test_parse_args_fail(args): with pytest.raises(SystemExit) as excinfo: arg_parsing.parse_args(shlex.split(args)) assert excinfo.value.args == (2,)
def test_parser_fromfile(tmpdir, argstring, attribute, value): argfile = tmpdir.join("argfile") argfile.write(argstring) parsed = arg_parsing.parse_args(["@{}".format(argfile)]) assert getattr(parsed, attribute) == value
def test_parser_from_shell(argstring, attribute, value): parsed = arg_parsing.parse_args(shlex.split(argstring)) assert getattr(parsed, attribute) == value