Created
November 22, 2020 21:21
-
-
Save red-avtovo/66bcd7bfc6e4588d0df0839cfdd45185 to your computer and use it in GitHub Desktop.
Get main info about Vault certs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
VAULT_URL="http://localhost:8200/v1" | |
TOKEN="s.BfRUIKOyrWtVIJX0rBN1AiSW" | |
res=$(curl -s \ | |
--header "X-Vault-Token: $TOKEN" \ | |
--request LIST \ | |
$VAULT_URL/pki/certs) | |
keys=$(echo $res | jq -r '.data.keys[]') | |
format="%60s | %-9s | %-3s | %-4s | %24s | %5s | %5s | %40s |\n" | |
printf "$format" "SERIAL" "Renewable" "CA" "Rvkd" "EXPIRE" "in30d" "in60d" "SUBJECT" | |
echo "-------------------------------------------------------------+-----------+-----+------+--------------------------+-------+-------+------------------------------------------+" | |
for key in $keys; do | |
# echo "Fetching $key" | |
res=$(curl -s \ | |
--header "X-Vault-Token: $TOKEN" \ | |
$VAULT_URL/pki/cert/$key) | |
# echo $res | jq | |
renewable=$(echo $res | jq -r '.renewable') | |
cert=$(echo $res | jq -r '.data.certificate') | |
# echo $cert | |
revoked=$(echo $res | jq -r '.data.revocation_time') | |
revoked=$(if [ "$revoked" != "0" ]; then echo "*"; else echo ""; fi) | |
cert_file="/tmp/$key.crt" | |
echo $cert | sed -e "s/-----BEGIN CERTIFICATE-----/&\n/" -e "s/-----END CERTIFICATE-----/\n&/" -e "s/\S\{64\}/&\n/g" | sed "s/^\s//g" > $cert_file | |
subject=$(openssl x509 -noout -in $cert_file -subject | cut -d "=" -f3) | |
expire=$(openssl x509 -noout -in $cert_file -enddate | cut -d "=" -f2) | |
ca_string=$(openssl x509 -noout -in $cert_file -purpose | grep -e "^SSL server CA" | cut -d ":" -f2 | xargs) | |
is_ca=$(if [ "$ca_string" == "Yes" ]; then echo "*"; else echo ""; fi) | |
subjects=$(openssl x509 -in $cert_file -noout -text | grep -A1 'Subject Alternative Name' | tail -n1 | tr -d ',' | sed -e "s/DNS://g" | xargs) | |
in30=$(openssl x509 -noout -in $cert_file -checkend 2592000) | |
in30=$(if [ "$in30" == "Certificate will expire" ]; then echo "*"; else echo ""; fi) | |
in60=$(openssl x509 -noout -in $cert_file -checkend 5184000) | |
in60=$(if [ "$in60" == "Certificate will expire" ]; then echo "*"; else echo ""; fi) | |
printf "$format" "$key" "$renewable" "$is_ca" "$revoked" "$expire" "$in30" "$in60" "$subjects" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output example