Last active
February 10, 2023 17:30
-
-
Save taxilian/6f34a8c1d799d88aed16387276d7bcaf to your computer and use it in GitHub Desktop.
Check kubernetes certificate expiration
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 | |
CONTEXT=$1 | |
ALLCERTS=$(kubectl --context $CONTEXT get secret --field-selector type=kubernetes.io/tls -A | tail +2 | awk '{print $1 ":" $2}') | |
TODAY=$(date +%s) | |
COL1=30 | |
COL2=10 | |
COL3=15 | |
COLPATTERN="%-${COL1}s %-${COL2}s %-${COL3}s / %s\n" | |
echo "Context: $CONTEXT" | |
printf "$COLPATTERN" "EXPIRES" "DAYS LEFT" "NAMESPACE" "TLS SECRET" | |
for certLine in $ALLCERTS; do | |
IFS=":" read -r certNS secretName <<< "$certLine" | |
EXPINFO=$(kubectl --context "${CONTEXT}" -n "${certNS}" get secret "${secretName}" -o "jsonpath={.data['tls\.crt']}" | base64 -D | openssl x509 -enddate -noout) | |
EXPDATE="${EXPINFO#*notAfter=}" | |
EXP_TS=$(date -jf '%b %d %H:%M:%S %Y %Z' "${EXPDATE}" +%s) | |
EXP_DAYS=$(((EXP_TS - TODAY) / 86400)) | |
printf "$COLPATTERN" "${EXPINFO#notAfter=}" "$EXP_DAYS" "${certNS}" "${secretName}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment