Skip to content

Instantly share code, notes, and snippets.

@taxilian
Last active February 10, 2023 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taxilian/6f34a8c1d799d88aed16387276d7bcaf to your computer and use it in GitHub Desktop.
Save taxilian/6f34a8c1d799d88aed16387276d7bcaf to your computer and use it in GitHub Desktop.
Check kubernetes certificate expiration
#!/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