Skip to content
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

Add script to sign arbitrary files using the key stored in Secrets Manager #3

Merged
merged 3 commits into from
May 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions sign.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
set -euo pipefail

if [[ "${2:-}" == "" ]]; then
echo "Usage: sign.sh ARTIFACTTYPE FILE [FILE...]" >&2
echo "">&2
echo "Creates detached signature as FILE.sig." >&2
exit 1
fi

tmpdir=$(mktemp -d)
trap "shred $tmpdir/* && rm -rf $tmpdir" EXIT

SECRET=CDK/$1/SigningKey

# Use secrets manager to obtain the key and passphrase into a JSON file
echo "Retrieving key $SECRET..." >&2
aws secretsmanager get-secret-value --secret-id "$SECRET" --output text --query SecretString > $tmpdir/secret.txt

value-from-secret() {
node -e "console.log(JSON.parse(require('fs').readFileSync('$tmpdir/secret.txt', { encoding: 'utf-8' })).$1)"
}

passphrase=$(value-from-secret Passphrase)

echo "Importing key..." >&2
gpg --homedir $tmpdir --import <(value-from-secret PrivateKey)

while [[ "${2:-}" != "" ]]; do
echo "Signing $2..." >&2
echo $passphrase | gpg \
--homedir $tmpdir \
--local-user [email protected] \
--batch --yes \
--passphrase-fd 0 \
--output $2.sig \
--detach-sign $2
shift
done

echo "Done!" >&2