eid-ops/global/post-tasks.d/015cosmos-trust

64 lines
1.8 KiB
Text
Raw Normal View History

2018-05-03 12:48:05 +02:00
#!/bin/bash
2013-09-02 16:01:50 +02:00
2013-10-31 22:20:33 +01:00
if [ -z "$COSMOS_KEYS" ]; then
2013-10-31 22:04:54 +01:00
COSMOS_KEYS=/etc/cosmos/keys
fi
2018-05-03 12:48:05 +02:00
bold='\e[1m'
reset='\e[0m'
red='\033[01;31m'
# Associative array of fingerprints in the GPG keyring
declare -A KEYRING
# Associative array with expired keys in the GPG keyring
declare -A EXPIRED
# associative array with non-expired keys found in $COSMOS_KEYS directory
declare -A SEEN
# Load information about all keys present in the GPG keyring
for line in $(cosmos gpg --with-colons --fingerprint | awk -F: '$1 == "pub" { print $2 ":" $5 }'); do
IFS=':' read -r expired fp <<< $line
KEYRING[$fp]='1'
if [[ $expired == 'e' ]]; then
EXPIRED[$fp]=1
fi
done
2017-08-15 11:37:45 +02:00
# Install new keys discovered in the $COSMOS_KEYS directory
2013-10-31 22:04:54 +01:00
for k in $COSMOS_KEYS/*.pub; do
2018-05-03 12:48:05 +02:00
if [[ ! -s $k ]]; then
# Silently ignore empty files
continue
fi
2021-10-07 15:28:51 +02:00
pubkeys_in_file=$(cosmos gpg --with-colons --with-fingerprint < $k 2>&1 | grep "^pub:")
2018-05-03 12:48:05 +02:00
non_expired_pubkeys_in_file=$(echo ${pubkeys_in_file} | awk -F: '$2 != "e" { print $0 }')
if [[ ! $non_expired_pubkeys_in_file ]]; then
echo -e "$0: ${red}Ignoring file with expired pubkey: ${k}${reset}"
continue
fi
fp=$(echo ${pubkeys_in_file} | awk -F: '{print $5}')
# Remember that we saw fingerprint $fp in file $k
SEEN[$fp]=$k
if [[ ! ${KEYRING[$fp]} ]]; then
echo -e "$0: ${bold}Importing new key ${fp}${reset} from ${k}"
2021-10-07 15:28:51 +02:00
cosmos gpg --no-tty --import < $k
2018-05-03 12:48:05 +02:00
elif [[ ${EXPIRED[$fp]} ]]; then
echo -e "$0: ${bold}Re-importing expired key ${fp}${reset} from ${k}"
2021-10-07 15:28:51 +02:00
cosmos gpg --no-tty --import < $k
2018-05-03 12:48:05 +02:00
fi
2013-09-02 16:01:50 +02:00
done
2018-05-03 12:48:05 +02:00
# Delete keys no longer present (or expired) in $COSMOS_KEYS directory
for fp in ${!KEYRING[@]}; do
if [[ ! ${SEEN[$fp]} ]]; then
echo -e "$0: ${bold}Deleting key${reset} ${fp} not present (or expired) in ${COSMOS_KEYS}"
cosmos gpg --fingerprint $fp
cosmos gpg --yes --batch --delete-key $fp || true
fi
2013-09-02 16:01:50 +02:00
done