Skip to content

Commit

Permalink
Merge pull request #50 from mattsb42-aws/dev-29
Browse files Browse the repository at this point in the history
adding support for input encoding and output decoding
  • Loading branch information
mattsb42-aws authored Nov 13, 2017
2 parents 4156364 + b379b1a commit a448bfc
Show file tree
Hide file tree
Showing 12 changed files with 1,021 additions and 62 deletions.
36 changes: 36 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,40 @@ references to other configuration files.
aws-crypto @my-encrypt -i $INPUT -o $OUTPUT
Encoding
--------
By default, ``aws-crypto`` will always output raw binary data and expect raw binary data
as input. However, there are some cases where you might not want this to be the case.

Sometimes this might be for convenience:

* Accepting ciphertext through stdin from a human.
* Presenting ciphertext through stdout to a human.

Sometimes it might be out of necessity:

* Saving ciphertext output to a shell variable.

* Most shells apply a system encoding to any data stored in a variable. As a result, this
often results in corrupted data if binary data is stored without additional encoding.

* Piping ciphertext in PowerShell.

* Similar to the above, all data passed through a PowerShell pipe is encoded using the
system encoding.

In order to address these scenarios, we provide two optional arguments:

* ``--decode`` : Base64-decode input before processing.
* ``--encode`` : Base64-encode output after processing.

These can be used independently or together, on any valid input or output.

Be aware, however, that if you target multiple files either through a path expansion or by
targetting a directory, the requested decoding/encoding will be applied to all files.


Execution
=========

Expand Down Expand Up @@ -381,6 +415,8 @@ Execution
-o OUTPUT, --output OUTPUT
Output file or directory for encrypt/decrypt
operation, or - for stdout.
--encode Base64-encode output after processing
--decode Base64-decode input before processing
-c ENCRYPTION_CONTEXT [ENCRYPTION_CONTEXT ...], --encryption-context ENCRYPTION_CONTEXT [ENCRYPTION_CONTEXT ...]
key-value pair encryption context values (encryption
only). Must a set of "key=value" pairs. ex: -c
Expand Down
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Modules
aws_encryption_sdk_cli
aws_encryption_sdk_cli.internal
aws_encryption_sdk_cli.internal.arg_parsing
aws_encryption_sdk_cli.internal.encoding
aws_encryption_sdk_cli.internal.identifiers
aws_encryption_sdk_cli.internal.io_handling
aws_encryption_sdk_cli.internal.master_key_parsing
Expand Down
22 changes: 17 additions & 5 deletions src/aws_encryption_sdk_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ def process_cli_request(
recursive, # type: bool
interactive, # type: bool
no_overwrite, # type: bool
suffix=None # type: Optional[str]
suffix=None, # type: Optional[str]
decode_input=False, # type: Optional[bool]
encode_output=False # type: Optional[bool]
):
# type: (...) -> None
"""Maps the operation request to the appropriate function based on the type of input and output provided.
Expand All @@ -123,6 +125,8 @@ def process_cli_request(
:param bool interactive: Should prompt before overwriting existing files
:param bool no_overwrite: Should never overwrite existing files
:param str suffix: Suffix to append to output filename (optional)
:param bool decode_input: Should input be base64 decoded before operation (optional)
:param bool encode_output: Should output be base64 encoded after operation (optional)
"""
_catch_bad_destination_requests(destination)
_catch_bad_stdin_stdout_requests(source, destination)
Expand All @@ -134,7 +138,9 @@ def process_cli_request(
source=source,
destination=destination,
interactive=interactive,
no_overwrite=no_overwrite
no_overwrite=no_overwrite,
decode_input=decode_input,
encode_output=encode_output
)
return

Expand All @@ -154,7 +160,9 @@ def process_cli_request(
destination=_destination,
interactive=interactive,
no_overwrite=no_overwrite,
suffix=suffix
suffix=suffix,
decode_input=decode_input,
encode_output=encode_output
)

elif os.path.isfile(_source):
Expand All @@ -172,7 +180,9 @@ def process_cli_request(
source=_source,
destination=_destination,
interactive=interactive,
no_overwrite=no_overwrite
no_overwrite=no_overwrite,
decode_input=decode_input,
encode_output=encode_output
)


Expand Down Expand Up @@ -236,7 +246,9 @@ def cli(raw_args=None):
recursive=args.recursive,
interactive=args.interactive,
no_overwrite=args.no_overwrite,
suffix=args.suffix
suffix=args.suffix,
decode_input=args.decode,
encode_output=args.encode
)
return None
except AWSEncryptionSDKCLIError as error:
Expand Down
11 changes: 11 additions & 0 deletions src/aws_encryption_sdk_cli/internal/arg_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ def _build_parser():
help='Output file or directory for encrypt/decrypt operation, or - for stdout.'
)

parser.add_argument(
'--encode',
action='store_true',
help='Base64-encode output after processing'
)
parser.add_argument(
'--decode',
action='store_true',
help='Base64-decode input before processing'
)

parser.add_argument(
'-c',
'--encryption-context',
Expand Down
Loading

0 comments on commit a448bfc

Please sign in to comment.