frenky666

kube-secret.sh

Oct 5th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.34 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. _options="c:n:s:k:h"
  4.  
  5. usage() {
  6.   echo """
  7. Usage $0 [-c <context>] [-n <namespace>] [-s <secret>] [-k <key>[
  8. """ >&2
  9. }
  10.  
  11. if test -t 1; then
  12.   # see if it supports colors...
  13.   ncolors=$(tput colors)
  14.  
  15.   if test -n "$ncolors" && test $ncolors -ge 8; then
  16.     bold="$(tput bold)"
  17.     underline="$(tput smul)"
  18.     standout="$(tput smso)"
  19.     normal="$(tput sgr0)"
  20.     black="$(tput setaf 0)"
  21.     red="$(tput setaf 1)"
  22.     green="$(tput setaf 2)"
  23.     yellow="$(tput setaf 3)"
  24.     blue="$(tput setaf 4)"
  25.     magenta="$(tput setaf 5)"
  26.     cyan="$(tput setaf 6)"
  27.     white="$(tput setaf 7)"
  28.   fi
  29. fi
  30.  
  31. while getopts $_options _option; do
  32.   case $_option in
  33.     c )
  34.       _context=$OPTARG
  35.       ;;
  36.     n )
  37.       _namespace=$OPTARG
  38.       ;;
  39.     s )
  40.       _secret=$OPTARG
  41.       ;;
  42.     k )
  43.       _key=$OPTARG
  44.       ;;
  45.  
  46.     h )
  47.       usage
  48.       exit
  49.       ;;
  50.     \? )
  51.       echo "Error. Unknown option -$OPTARG" >&2
  52.       exit 1
  53.       ;;
  54.     : )
  55.       echo "Error. Missing option argument for -$OPTARG" >&2
  56.       exit 1
  57.       ;;
  58.   esac
  59. done
  60.  
  61. _COMMAND="kubectl "
  62.  
  63. [ ! -z "$_context"   ] && _COMMAND=$_COMMAND" --context $_context"
  64. [ ! -z "$_namespace" ] && _COMMAND=$_COMMAND" --namespace $_namespace"
  65. [ ! -z "$_secret"    ] && _COMMAND=$_COMMAND" get secret $_secret"
  66. [ ! -z "$_key"       ] && _COMMAND=$_COMMAND" -o jsonpath=\"{.data.$_key}\" | base64 --decode && echo "  
  67.  
  68. if [ ! -z "$_secret" -a ! -z "$_key" ]; then
  69.   eval $_COMMAND
  70. elif [ ! -z "$_secret" -a -z "$_key" ]; then
  71.  
  72.   while read _key; do
  73.     echo -ne "\t${yellow}${underline}$_key${normal} ${green}${bold}"
  74.     eval $_COMMAND" -o jsonpath=\"{.data.$_key}\"" | base64 --decode && echo "${normal}"
  75.   done < <(eval $_COMMAND" -o jsonpath=\"{.data}\"" | jq --raw-output "keys[]")
  76.  
  77.   echo
  78. elif [ -z "$_secret" -a -z "$_key" ]; then
  79.   while read _secret; do
  80.     echo "${red}${standout}$_secret${normal}"
  81.     while read _key; do
  82.       echo -ne "\t${yellow}${underline}$_key${normal} ${green}${bold}"
  83.       eval $_COMMAND" get secret $_secret -o jsonpath=\"{.data.$_key}\"" | base64 --decode && echo "${normal}"
  84.     done < <(eval $_COMMAND" get secret $_secret -o jsonpath=\"{.data}\"" | jq --raw-output "keys[]")
  85.   done < <(eval $_COMMAND" get secret --field-selector type=Opaque --no-headers --output custom-columns=\":metadata.name\" ")
  86. fi
  87.  
Add Comment
Please, Sign In to add comment