Last active
September 19, 2024 14:08
-
-
Save caruccio/6d2cf0373508323c373b86c98562a1ba to your computer and use it in GitHub Desktop.
Show decoded contents of a secret
This file contains hidden or 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 | |
usage() | |
{ | |
echo "Usage: kubectl show-secret [-n namespace] secret [...secret]" | |
exit | |
} | |
while [ $# -gt 0 ]; do | |
while getopts n: opt; do | |
case $opt in | |
n) NAMESPACE=$OPTARG;; | |
h) usage | |
esac | |
done | |
[ $? -eq 0 ] || exit 1 | |
[ $OPTIND -gt $# ] && break # we reach end of parameters | |
shift $[$OPTIND - 1] # free processed options so far | |
OPTIND=1 # we must reset OPTIND | |
ARGS[${#ARGS[*]}]=$1 # save first non-option argument (a.k.a. positional argument) | |
shift # remove saved arg | |
done | |
print_data() | |
{ | |
local key=$1 | |
shift | |
local jq_filters="$*" | |
echo -e "${COLOR_BOLD}--- $key ---${COLOR_RESET}" | |
jq -r ".data[\"$key\"]$jq_filters" <<<$"$data" | xargs printf "%b" | |
echo | |
echo -e "${COLOR_BOLD}--- end $key ---${COLOR_RESET}" | |
} | |
#echo Namespace: $NAMESPACE | |
#echo Secrets: ${ARGS[*]} | |
for secret in ${ARGS[*]}; do | |
data="$(command kubectl get secret ${NAMESPACE:+ -n $NAMESPACE} $secret -o json)" | |
# .stringData | |
keys=( $(jq -r '(.stringData // empty) | keys | .[]' <<<"$data") ) | |
first=true | |
if [ ${#keys[*]} -gt 0 ]; then | |
for key in ${keys[*]}; do | |
$first || echo; first=false | |
print_data "$key" | |
done | |
fi | |
# .data | |
keys=( $(jq -r '(.data // empty) | keys | .[]' <<<"$data") ) | |
first=true | |
if [ ${#keys[*]} -gt 0 ]; then | |
for key in ${keys[*]}; do | |
$first || echo; first=false | |
print_data "$key" "|@base64d" | |
done | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've made a simplified version using go-template. Take a look at my version.