#!/bin/bash set -euo pipefail if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then cat <&2 echo "szecret: this machine is already enrolled (${identity})" keys_in "${identity_pub}" exit 0 fi mkdir -p "$(dirname "${identity}")" chmod 700 "$(dirname "${identity}")" tmp="$(mktemp "${identity}.XXXXXX")" trap 'rm -f "${tmp}"' EXIT rm -f "${tmp}" case "${kind}" in file) (umask 077 && age-keygen -o "${tmp}" 2>/dev/null) pub="$(age-keygen -y "${tmp}")" ;; tpm) exists age-plugin-tpm || die "--tpm needs age-plugin-tpm, which is not installed; no key made" [[ -e /dev/tpmrm0 ]] || die "--tpm: no TPM here (/dev/tpmrm0); no key made" (umask 077 && age-plugin-tpm --generate -o "${tmp}") || die "age-plugin-tpm could not make a key; no key made" pub="$(age-plugin-tpm -y "${tmp}")" ;; se) exists age-plugin-se || die "--se needs age-plugin-se, which is not installed; no key made" # tag: an age1tag1 key, which age 1.3 encrypts to without the plugin, # so the hosts that write this machine's buckets need none. (umask 077 && age-plugin-se keygen --access-control=none --recipient-type=tag -o "${tmp}") \ || die "age-plugin-se could not make a key (is there a Secure Enclave?); no key made" pub="$(age-plugin-se recipients --recipient-type=tag -i "${tmp}")" ;; esac [[ "${pub}" =~ ^age1[0-9a-z]+$ ]] || die "the new key's public half doesn't look like one: ${pub:-nothing}; no key made" chmod 600 "${tmp}" echo "${pub}" > "${identity_pub}.tmp" # Whatever the umask (sg gives 002): the key it names must be the one here. chmod 644 "${identity_pub}.tmp" mv "${identity_pub}.tmp" "${identity_pub}" mv "${tmp}" "${identity}" >&2 echo "szecret: enrolled; add this machine's public key to cloud-setup's szecret/hosts:" echo "${pub}"