2019-09-03 16:56:31 +02:00
|
|
|
# This tool mimics a subset of the interface of 'pass', but uses
|
|
|
|
# Google Cloud KMS for encryption.
|
|
|
|
#
|
|
|
|
# It is intended to be compatible with how 'kontemplate' invokes
|
|
|
|
# 'pass.'
|
|
|
|
#
|
|
|
|
# Only the 'show' and 'insert' commands are supported.
|
|
|
|
|
2019-11-15 16:26:08 +01:00
|
|
|
{ pkgs, kms, ... }:
|
2019-09-03 16:56:31 +02:00
|
|
|
|
2019-12-09 03:54:35 +01:00
|
|
|
let inherit (pkgs.third_party) google-cloud-sdk tree writeShellScriptBin;
|
2019-11-16 00:25:41 +01:00
|
|
|
in (writeShellScriptBin "pass" ''
|
2019-09-03 16:56:31 +02:00
|
|
|
set -eo pipefail
|
|
|
|
|
|
|
|
CMD="$1"
|
|
|
|
readonly SECRET=$2
|
2019-12-23 13:26:09 +01:00
|
|
|
readonly SECRETS_DIR=${./secrets}
|
2019-09-03 16:56:31 +02:00
|
|
|
readonly SECRET_PATH="$SECRETS_DIR/$SECRET"
|
|
|
|
|
|
|
|
function secret_check {
|
|
|
|
if [[ -z $SECRET ]]; then
|
|
|
|
echo 'Secret must be specified'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ -z $CMD ]]; then
|
|
|
|
CMD="ls"
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$CMD" in
|
|
|
|
ls)
|
|
|
|
${tree}/bin/tree $SECRETS_DIR
|
|
|
|
;;
|
|
|
|
show)
|
|
|
|
secret_check
|
|
|
|
${google-cloud-sdk}/bin/gcloud kms decrypt \
|
2019-11-15 16:26:08 +01:00
|
|
|
--project ${kms.project} \
|
|
|
|
--location ${kms.region} \
|
|
|
|
--keyring ${kms.keyring} \
|
|
|
|
--key ${kms.key} \
|
2019-09-03 16:56:31 +02:00
|
|
|
--ciphertext-file $SECRET_PATH \
|
|
|
|
--plaintext-file -
|
|
|
|
;;
|
|
|
|
insert)
|
|
|
|
secret_check
|
|
|
|
${google-cloud-sdk}/bin/gcloud kms encrypt \
|
2019-11-15 16:26:08 +01:00
|
|
|
--project ${kms.project} \
|
|
|
|
--location ${kms.region} \
|
|
|
|
--keyring ${kms.keyring} \
|
|
|
|
--key ${kms.key} \
|
2019-09-03 16:56:31 +02:00
|
|
|
--ciphertext-file $SECRET_PATH \
|
|
|
|
--plaintext-file -
|
|
|
|
echo "Inserted secret '$SECRET'"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Usage: pass show/insert <secret>"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
2019-11-16 00:25:41 +01:00
|
|
|
'') // { meta.enableCI = true; }
|