From b2571a8c305c4f9d69401d8c50c53ec64358d090 Mon Sep 17 00:00:00 2001 From: Peter Jones Date: Wed, 2 Jan 2019 16:09:31 -0700 Subject: [PATCH] Add a script to help manage secrets Current features: * Mount a directory of encrypted secrets into a memory file system * Unmount the file system created by the mount command --- bin/secrets.sh | 252 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100755 bin/secrets.sh diff --git a/bin/secrets.sh b/bin/secrets.sh new file mode 100755 index 0000000..6f08357 --- /dev/null +++ b/bin/secrets.sh @@ -0,0 +1,252 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i bash -p bash utillinux gawk gnupg +# shellcheck shell=bash + +################################################################################ +[ -n "${DEBUG:-}" ] && set -x + +set -e +set -u + +################################################################################ +usage() { +cat < $dest_file" + + if [ -n "$symmetric_key" ]; then + gpg --batch \ + --quiet \ + --decrypt \ + --passphrase-fd 0 \ + --pinentry-mode loopback \ + "$file" > "$dest_file" \ + <<<"$symmetric_key" + else + gpg --use-agent \ + --quiet \ + --decrypt \ + --quiet \ + "$file" > "$dest_file" + fi +} + +################################################################################ +mount_secrets() { + local option_secrets="" + local option_mount_point="" + local option_symmetric_key_file="" + local symmetric_key="" + + while getopts "hd:m:s:" o; do + case "${o}" in + h) mount_usage + exit + ;; + + d) option_secrets=$OPTARG + ;; + + m) option_mount_point=$OPTARG + ;; + + s) option_symmetric_key_file=$OPTARG + ;; + + *) exit 1 + ;; + esac + done + + shift $((OPTIND-1)) + + if [ -z "$option_secrets" ]; then + >&2 echo "ERROR: missing -d option to mount" + exit 1 + fi + + if [ -z "$option_mount_point" ]; then + option_mount_point=$(calculate_mount_point "$option_secrets") + fi + + if [ -n "$option_symmetric_key_file" ]; then + symmetric_key=$(read_symmetric_key_file "$option_symmetric_key_file") + fi + + if ! findmnt "$option_mount_point" > /dev/null 2>&1; then + mkdir -p "$option_mount_point" + echo "==> Enter sudo password to mount tmpfs" + sudo mount -t tmpfs \ + -o size="$(calculate_fs_size "$option_secrets")" \ + tmpfs "$option_mount_point" + fi + + while IFS= read -r -d '' file; do + decrypt_file "$file" "$option_secrets" "$option_mount_point" "$symmetric_key" + done < <(find "$option_secrets" -type f -print0) +} + +################################################################################ +unmount_secrets() { + local option_secrets="" + local option_mount_point="" + + while getopts "hd:m:" o; do + case "${o}" in + h) umount_usage + exit + ;; + + d) option_secrets=$OPTARG + ;; + + m) option_mount_point=$OPTARG + ;; + + *) exit 1 + ;; + esac + done + + shift $((OPTIND-1)) + + if [ -z "$option_mount_point" ] && [ -n "$option_secrets" ]; then + option_mount_point=$(calculate_mount_point "$option_secrets") + elif [ -z "$option_mount_point" ]; then + >&2 echo "ERROR: give -d or -m" + exit 1 + fi + + echo "==> Enter sudo password for unmounting" + sudo umount "$option_mount_point" + rmdir "$option_mount_point" +} + +################################################################################ +if [ $# -lt 1 ]; then + usage + exit 1 +fi + +command=$1 +shift + +case "$command" in + mount) + mount_secrets "$@" + ;; + + unmount|umount) + unmount_secrets "$@" + ;; + + *) + usage + exit 1 + ;; +esac + +# Local Variables: +# mode: sh +# sh-shell: bash +# End: