Skip to content

Instantly share code, notes, and snippets.

@caruccio
Created January 18, 2023 15:08
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 caruccio/3fe4cb8949419b093c5c5c9b3ac33631 to your computer and use it in GitHub Desktop.
Save caruccio/3fe4cb8949419b093c5c5c9b3ac33631 to your computer and use it in GitHub Desktop.
Extract secret value
#!/bin/bash
#
# Install: copy to path
# $ cp kubectl-extract /usr/local/bin
#
# Usage:
# $ kubectl extract -n default my-secret
#
usage()
{
echo "Usage: kubectl extract [-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
#echo Namespace: $NAMESPACE
#echo Secrets: ${ARGS[*]}
for secret in ${ARGS[*]}; do
data="$(kubectl get secret ${NAMESPACE:+ -n $NAMESPACE} $secret -o json)"
keys=( $(jq -r '.data | keys | .[]' <<<"$data") )
[ ${#keys[*]} -eq 0 ] && continue
for key in ${keys[*]}; do
echo "$key: "
value=$(jq -r ".data[\"$key\"]" <<<"$data" | base64 -d)
echo "$value"
[ "${value: -1}" == $'\n' ] || echo
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment