Package: aws-encryption-sdk-cli
This command line tool can be used to encrypt and decrypt files and directories using theAWS Encryption SDK.The latest full documentation can be found atRead the Docs.Find us onGitHub.Security issue notificationsSeeSupport Policyfor details on the current support status of all major versions of this library.Getting StartedRequired PrerequisitesPython 3.6+aws-encryption-sdk >= 3.1.0InstallationNoteIf you have not already installedcryptography, you might need to install additional prerequisites as
detailed in thecryptography installation guidefor your operating system.As a system package:$ pip install aws-encryption-sdk-cliUsing a virtual environment:Installation using a python virtual environment is recommended to avoid conflicts between system packages and user installed packages.For the latest information on Python virtual environments, refer to thePython.org Virtual Environment DocumentationMacOS/Unix:$cdmy_project$python3-mvenvenv$sourceenv/bin/activate$python-mpipinstallaws-encryption-sdk-cliWindows (PowerShell):>cdmy_project>python3-mvenvenv>.\env\Scripts\Activate.ps1(env)>pipinstallaws-encryption-sdk-cliUsageInput and OutputFor the most part, the behavior ofaws-encryption-cliin handling files is based on that
of GNU CLIs such ascp.  A qualifier to this is that when encrypting a file, if a
directory is provided as the destination, rather than creating the source filename
in the destination directory, a suffix is appended to the destination filename. By
default the suffix is.encryptedwhen encrypting and.decryptedwhen decrypting,
but a custom suffix can be provided by the caller if desired.If a destination file already exists, the contents will be overwritten.Allowed input/output
pairingsoutputstdoutfiledirectoryinputstdinYYsingle fileYYYpattern matchYdirectoryYIf the source includes a directory and the--recursiveflag is set, the entire
tree of the source directory is replicated in the target directory.Parameter ValuesSome arguments accept additional parameter values.  These values must be provided in the
form ofkey=valueas demonstrated below.--encryption-contextkey1=value1key2=value2"key 3=value with spaces"--master-keysprovider=aws-kmskey=$KEY_ID_1key=$KEY_ID_2--cachingcapacity=3max_age=80.0Encryption ContextEncryptTheencryption contextis an optional, but recommended, set of key-value pairs that contain
arbitrary nonsecret data. The encryption context can contain any data you choose, but it
typically consists of data that is useful in logging and tracking, such as data about the file
type, purpose, or ownership.Parameters may be provided usingParameter Values.--encryption-contextkey1=value1key2=value2"key 3=value with spaces"DecryptIf an encryption context is provided on decrypt, it is instead used to require that the message
being decrypted was encrypted using an encryption context that matches the specified requirements.Ifkey=valueelements are provided, the decryption will only continue if the encryption
context found in the encrypted message contains matching pairs.--encryption-contextrequired_key=required_valueclassification=secretIf barekeyelements are provided, the decryption will continue if those keys are found,
regardless of the values.keyandkey=valueelements can be mixed.--encryption-contextrequired_keyclassification=secretWarningIf encryption context requirements are not satisfied by the ciphertext message, the
message will not be decrypted. One side effect of this is that if you chose to write
the plaintext output to a file and that file already exists, it will be deleted when
we stop the decryption.Output MetadataIn addition to the actual output of the operation, there is metadata about the operation
that can be useful. This metadata includes some information about the operation as well as
the complete header data from the ciphertext message.The metadata for each operation is written to the specified file as a single line containing
formatted JSON, so if a single command performs multiple file operations, a separate line
will be written for each operation. There are three operating modes:--metadata-outputFILE: Writes the metadata output toFILE(can be-for stdout
as long as main output is not stdout). Default behavior is to append the metadata entry to
the end ofFILE.--overwrite-metadata: Force overwriting the contents ofFILEwith the new metadata.-S/--suppress-metadata: Output metadata is suppressed.Metadata ContentsThe metadata JSON contains the following fields:"mode":"encrypt"/"decrypt"/"decrypt-unsigned""input": Full path to input file (or"<stdin>"if stdin)"output": Full path to output file (or"<stdout>"if stdout)"header": JSON representation ofmessage header data"header_auth": JSON representation ofmessage header authentication data(only on decrypt)Skipped FilesIf encryption context checks fail when attempting to decrypt a file, the metadata contains
additional fields:skipped:truereason:"Missing encryption context key or value"missing_encryption_context_keys: List of required encryption context keys that were
missing from the message.missing_encryption_context_pairs: List of required encryption context key-value pairs
missing from the message.Master Key ProviderInformation for configuring a master key provider must be provided.Parameters may be provided usingParameter Values.These parameters are common to all master key providers:provider(default: aws-encryption-sdk-cli::aws-kms): Indicator of the master key
provider to use.SeeAdvanced Configurationfor more information on using other master key providers.key(on encrypt: at least one required, many allowed; on decrypt: one of key or discovery is required):
Identifier for a wrapping key to be used in the operation. Must be an identifier understood by the specified master
key provider.The discoveryattribute is only available if you are using anaws-kmsprovider.If usingaws-kmsto decrypt,you must specify either a key or discovery with a value of true.If usingaws-kmsto decrypt and specifying a key, you must use a key ARN; key ids, alias names, and alias
ARNs are not supported.Any additional parameters supplied are collected into lists by parameter name and
passed to the master key provider class when it is instantiated. Custom master key providers
must accept all arguments as prepared. SeeAdvanced Configurationfor more information.Multiple master keys can be defined using multiple instances of thekeyargument.Multiple master key providers can be defined using multiple--wrapping-keysgroups.If multiple master key providers are defined, the first one is treated as the primary.If multiple master keys are defined in the primary master key provider, the first one is treated
as the primary. The primary master key is used to generate the data key.The following logic is used to construct all master key providers. We useStrictAwsKmsMasterKeyProvideras an example.# With parameters:--wrapping-keysprovider=aws-kmskey=$KEY_1key=$KEY_2# KMSMasterKeyProvider is called as:key_provider=StrictAwsKmsMasterKeyProvider(key_ids=[$KEY_1,$KEY_2])# Single KMS CMK--wrapping-keysprovider=aws-kmskey=$KEY_ARN_1# Two KMS CMKs--wrapping-keysprovider=aws-kmskey=$KEY_ARN_1key=$KEY_ARN_2# KMS Alias by name in default region--wrapping-keysprovider=aws-kmskey=$ALIAS_NAME# KMS Alias by name in two specific regions--wrapping-keysprovider=aws-kmskey=$ALIAS_NAMEregion=us-west-2--wrapping-keysprovider=aws-kmskey=$ALIAS_NAMEregion=eu-central-1AWS KMSIf you want to use theaws-kmsmaster key provider, you can either specify that
as the provider or simply not specify a provider and allow the default value to be used.There are some configuration options which are unique to theaws-kmsmaster key provider:profile: Providing this configuration value will use the specifiednamed profilecredentials.discovery(default: false; one of key or discovery with a value of true is required):
Indicates whether this provider should be in “discovery” mode. If true (enabled), the AWS Encryption CLI will attempt
to decrypt ciphertexts encrypted with any AWS KMS CMK. If false (disabled), the AWS Encryption CLI will only attempt
to decrypt ciphertexts encrypted with the key ARNs specified in thekeyattribute.
Any key specified in thekeyattribute that is a KMS CMK Identier other than a key ARN will not
be used for decryption.discovery-account(optional; available only when discovery=true and discovery-partition is also provided):
If discovery is enabled, limits decryption to AWS KMS CMKs in the specified accounts.discovery-partition(optional; available only when discovery=true and discovery-account is also provided):
If discovery is enabled, limits decryption to AWS KMS CMKs in the specified partition, e.g. “aws” or “aws-gov”.region: This allows you to specify the target region.The logic for determining which region to use is shown in the pseudocode below:ifkeyIDisanARN:useregionidentifiedinARNelse:ifregionisspecified:useregionelseifprofileisspecifiedandprofilehasadefinedregion:useregiondefinedinprofileelse:usesystemdefaultregionAdvanced ConfigurationIf you want to use a different master key provider, that provider must register asetuptools entry point. You can find an example of registering this entry point in thesetup.pyfor this package.When a provider name is specifed in a call toaws-encryption-cli, the appropriate entry
point for that name is used.Handling Multiple Entry PointsIf multiple entry points are registered for a given name, you will need to specify the package
that registered the entry point you want to use.In order to specify the package name, use the format:PACKAGE_NAME::ENTRY_POINT.provider=aws-kmsprovider=aws-encryption-sdk-cli::aws-kmsIf you supply only an entry point name and there is only one entry point registered for that
name, that entry point will be used.If you supply only an entry point name and there is more than one entry point registered
for that name, an error will be raised showing you all of the packages that have an entry
point registered for that name.If you supply both a package and an entry point name, that exact entry point will be used.
If it is not accessible, an error will be raised showing you all of the packages that have
an entry point registered for that name.External Master Key ProvidersThe entry point name use must not contain the string::. This is used as a namespace
separator as descibed inHandling Multiple Entry Points.When called, these entry points must return an instance of a master key provider. They must
accept the parameters prepared by the CLI as described inMaster Key Provider.These entry points must be registered in theaws_encryption_sdk_cli.master_key_providersgroup.If the entry point raises aaws_encryption_sdk_cli.exceptions.BadUserArgumentError, the
CLI will present the raised error message to the user to indicate bad user input.Commitment PolicyThe commitment policy controls which algorithms can be used in encryption and decryption.
Versions 2.0.x and later of the AWS Encryption CLI use a default commitment policy ofrequire-encrypt-require-decrypt, which ensures that only algorithms which providekey commitmentcan be used
on both encryption and decryption. If you want to use a different commitment policy, you can do so
with the--commitment-policyparameter.For more details, see theCommitment Policydocumentation.# Use a commitment policy that requires an algorithm which provides key commitment
# on both encryption and decryption--commitment-policyrequire-encrypt-require-decryptData Key CachingData key caching is optional, but if used then the parameters noted as required must
be provided.  For detailed information about using data key caching with the AWS
Encryption SDK, see thedata key caching documentation.Parameters may be provided usingParameter Values.Allowed parameters:capacity(required): Number of entries that the cache will hold.max_age(required):  Determines how long each entry can remain in the cache, beginning when it was added.max_messages_encrypted:  Determines how long each entry can remain in the cache, beginning when it was added.max_bytes_encrypted: Specifies the maximum number of bytes that a cached data key can encrypt.Logging and VerbosityThe-vargument allows you to tune the verbosity of the built-in logging to your desired level.
In short, the more-varguments you supply, the more verbose the output gets.unset :aws-encryption-clilogs all warnings, all dependencies only log critical messages-v:aws-encryption-cliperforms moderate logging, all dependencies only log critical messages-vv:aws-encryption-cliperforms detailed logging, all dependencies only log critical messages-vvv:aws-encryption-cliperforms detailed logging, all dependencies perform moderate logging-vvvv:aws-encryption-cliperforms detailed logging, all dependencies perform detailed loggingpython logging levelsverbosity
flagaws-encryption-clidependenciesunsetWARNINGCRITICAL-vINFOCRITICAL-vvDEBUGCRITICAL-vvvDEBUGINFO-vvvvDEBUGDEBUGConfiguration FilesAs with any CLI where the configuration can get rather complex, you might want to use a configuration
file to define some or all of your desired behavior.Configuration files are supported using Python’s nativeargparse file support, which allows
you to write configuration files exactly as you would enter arguments in the shell. Configuration
file references passed toaws-encryption-cliare identified by the@prefix and the
contents are expanded as if you had included them in line. Configuration files can have any
name you desire.NoteIn PowerShell, you will need to escape the@symbol so that it is sent toaws-encryption-clirather than interpreted by PowerShell.For example, if I wanted to use a common master key configuration for all of my calls, I could
create a filemaster-key.confwith contents detailing my master key configuration.master-key.conf--master-keykey=A_KEYkey=ANOTHER_KEYThen, when callingaws-encryption-cli, I can specify the rest of my arguments and reference
my new configuration file, andaws-encryption-cliwill use the composite configuration.aws-encryption-cli-e-i$INPUT_FILE-o$OUTPUT_FILE@master-key.confTo extend the example, if I wanted a common caching configuration for all of my calls, I could
similarly place my caching configuration in a configuration filecaching.confin this example
and include both files in my call.caching.conf--cachingcapacity=10max_age=60.0max_messages_encrypted=15aws-encryption-cli-e-i$INPUT_FILE-o$OUTPUT_FILE@master-key.conf@caching.confConfiguration files can be referenced anywhere inaws-encryption-cliparameters.aws-encryption-cli-e-i$INPUT_DIR-o$OUTPUT_DIR@master-key.conf@caching.conf--recursiveConfiguration files can have many lines, include comments using#. Escape characters are
platform-specific:\on Linux and MacOS and`on Windows. Configuration files may
also include references to other configuration files.my-encrypt.config--encrypt@master-key.conf# Use existing master key config@caching.conf# Always recurse, but require interactive overwrite.--recursive--interactiveaws-encryption-cli@my-encrypt-i$INPUT-o$OUTPUTEncodingBy default,aws-encryption-cliwill 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.