Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Colors
  4. NC='\033[0m'            # Text Reset
  5. RED='\033[0;31m'        # Red
  6. GRE='\033[0;32m'        # Green
  7. YEL='\033[0;33m'        # Yellow
  8. BLU='\033[0;34m'        # Blue
  9. PUR='\033[0;35m'        # Purple
  10. CYA='\033[0;36m'        # Cyan
  11. WHI='\033[0;37m'        # White
  12. DG='\033[0;90m'         # Dark grey
  13.  
  14.  
  15. while [[ $# -gt 1 ]]
  16. do
  17. key="$1"
  18. case $key in
  19.     -u|--user)
  20.     R_USER="$2"
  21.     shift # past argument
  22.     ;;
  23.     -h|--host)
  24.     R_HOST="$2"
  25.     shift # past argument
  26.     ;;
  27.     -p|--password)
  28.     R_PASSWORD="$2"
  29.     shift # past argument
  30.     ;;
  31. esac
  32. shift # past argument or value
  33. done
  34.  
  35. [[ -z $R_PASSWORD ]] && R_PASSWORD="IAMNOTAPASSWORD"
  36. [[ -z $R_USER ]] && R_USER="IAMNOTAUSER"
  37. [[ -z $R_HOST ]] && printf "\nYou ${RED}MUST${NC} use the -h option to specify an host (DO NOT include any URI nor http:// nor https:// part)\n" && exit 1
  38.  
  39. REG_URL="https://$R_HOST/v2/_catalog"
  40.  
  41. # Save the response headers of our first request to the registry to get the Www-Authenticate header
  42. TEMPFILE="/tmp/mytemp"
  43. curl -k --dump-header $TEMPFILE $REG_URL > /dev/null 2>&1
  44.  
  45.  
  46. AUTHSTR=$(cat $TEMPFILE | grep "Www-Authenticate")
  47.  
  48. OLDIFS=$IFS
  49. IFS=","
  50. for part in echo $AUTHSTR;do
  51.         [[ $(printf "$part" | grep "^Www-Authenticate: Bearer realm=") ]] && AUTHREALM=$(echo $part | cut -d"\"" -f2)
  52.         [[ $(printf "$part" | egrep "^service=") ]] && AUTHSVC=$(echo $part | cut -d"\"" -f2)
  53.         [[ $(printf "$part" | egrep "^scope=") ]] && AUTHSCOPE=$(echo $part | cut -d"\"" -f2)
  54.         i=$(( $i + 1 ))
  55. done
  56. IFS=$OLDIFS
  57.  
  58. printf "\nQuerying token with : ${DG}curl -ks -H \"Authorization: Basic $(echo -n \"$R_USER:$R_PASSWORD\" | base64)\" \"$AUTHREALM?service=$AUTHSVC&scope=$AUTHSCOPE\"${NC}\n"
  59. printf "\n${YEL}$(echo -n \"$R_USER:$R_PASSWORD\" | base64)${NC} is the base64 for the user and password (defaults if you didn't specify anything) : ${YEL}$R_USER:$R_PASSWORD${NC}\n"
  60. MYTOK=$(curl -ks -H "Authorization: Basic $(echo -n \"$R_USER:$R_PASSWORD\" | base64)" "$AUTHREALM?service=$AUTHSVC&scope=$AUTHSCOPE")
  61. MYTOK=$(echo "$MYTOK" | grep token | cut -d"\"" -f4)
  62.  
  63. [[ -z $MYTOK ]] && printf "\n${RED}No token. Did you fail at typing the URL ?${NC}\n" || printf "\nJust run the following (add -k if you use insecure registry. -v for verbose): \ncurl -H \"Authorization: Bearer $MYTOK\" $REG_URL\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement