-
Notifications
You must be signed in to change notification settings - Fork 51
Add AWS KMS decryption support for environment variables #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Lowercases
wants to merge
2
commits into
master
Choose a base branch
from
add-kms-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
Changes we want | ||
=============== | ||
Curently ssm-env recognises variables beginning with ssm://, containing an SSM parameter | ||
store key after this prefix, and substitutes them with their stored value by making calls | ||
to the AWS SSM API. It then executes a child process with this new environment. | ||
|
||
We want it to also recognise KMS-encrypted parameters and have them substituted into | ||
their plaintext value in the child process environment. These variables are of the format | ||
|
||
!kms '<encoded value>' | ||
|
||
where <encoded value> stands in for the base64-encoded KMS-encrypted secure test. | ||
|
||
Example | ||
======= | ||
Given the following example environment | ||
|
||
VAR1=VALUE1 | ||
VAR2=ssm:///omnes/caeli | ||
VAR3=!kms 'ABCDEFG==' | ||
|
||
currently SSM would detect that VAR2 has the appropriate prefix, query the SSM Parameter | ||
Database for the /omnes/caeli key and change VAR2 into the plain value it returns, while | ||
leaving VAR1 and VAR3 unchanged since they don't have the ssm:// prefix. Supposing the | ||
stored value of said key is 'plainvalue' (without quotes), the environment of the child | ||
process will be | ||
|
||
VAR1=VALUE1 | ||
VAR2=plainvalue | ||
VAR3=!kms 'ABCDEFG==' | ||
|
||
After the changes, for that sample enviroment we want ssm-env to detect that VAR3 has | ||
the appropriate format and query the AWS KMS service to decrypt the kms-encrypted value, | ||
getting the following modified environment for its child process (assuming that ABCDEFG== | ||
is the base64 kms-encrypted value, under certain key, of 'muchacha' (without quotes): | ||
|
||
VAR1=VALUE | ||
VAR2=plainvalue | ||
VAR3=muchacha | ||
|
||
Note that the behaviour for neither VAR1 nor VAR2 is changed from the current | ||
implementation. | ||
|
||
Considerations | ||
============== | ||
* Express the new KMS-encrypted value format as a regular expression. | ||
* Attempt to mimic the current implementation as much as possible, substituting SSM API | ||
calls for the necessary KMS API calls but keeping the larger structure and spirit | ||
faithful to the project. | ||
* Add tests akin to the current ones, in which we mock the KMS service. | ||
|
||
Existing implementation | ||
======================= | ||
The wanted extra functionality is currently provided by a shell function: | ||
|
||
kms_env() { | ||
env -0 | while IFS== read -r -d '' name value | ||
do | ||
if echo "$value" | grep -q '^!kms ' | ||
then | ||
cipher="$(echo "$value" | sed -e 's/!kms //' | sed -e "s/'//g")" | ||
# Don't quote cipher since it could already be quoted, and it's expected | ||
# to be a base64-encrypted value | ||
plain="$(aws kms decrypt --ciphertext-blob fileb://<(echo $cipher | base64 -d) --output text --query Plaintext | base64 -d)" \ | ||
|| return 1 | ||
echo "export $name=$plain" | ||
fi | ||
done | ||
} | ||
|
||
$(kms_env) || error_exit 'Error decrypting environment with kms_env' | ||
|
||
where the last line represents the replacing of the environment for the next | ||
commands. Note that the behaviour is not exactly the same -- the idea of the | ||
kms_env() function is to be run in a shell script previous to the commands | ||
that will inherit the new environment, while ssm-env is expected to be run | ||
and passing the subsequent commands as parameters, which it will then exec. | ||
|
||
However, it's useful as a reference for the format and the means in which | ||
the KMS-encrypted values are received and expanded. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lazy initialization of the KMS client in lazyKMSClient is not thread-safe. Consider adding synchronization (e.g., a mutex) if expandEnviron might be called concurrently.
Copilot uses AI. Check for mistakes.