Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Colors
- NC='\033[0m' # Text Reset
- RED='\033[0;31m' # Red
- GRE='\033[0;32m' # Green
- YEL='\033[0;33m' # Yellow
- BLU='\033[0;34m' # Blue
- PUR='\033[0;35m' # Purple
- CYA='\033[0;36m' # Cyan
- WHI='\033[0;37m' # White
- DG='\033[0;90m' # Dark grey
- while [[ $# -gt 1 ]]
- do
- key="$1"
- case $key in
- -u|--user)
- R_USER="$2"
- shift # past argument
- ;;
- -h|--host)
- R_HOST="$2"
- shift # past argument
- ;;
- -p|--password)
- R_PASSWORD="$2"
- shift # past argument
- ;;
- esac
- shift # past argument or value
- done
- [[ -z $R_PASSWORD ]] && R_PASSWORD="IAMNOTAPASSWORD"
- [[ -z $R_USER ]] && R_USER="IAMNOTAUSER"
- [[ -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
- REG_URL="https://$R_HOST/v2/_catalog"
- # Save the response headers of our first request to the registry to get the Www-Authenticate header
- TEMPFILE="/tmp/mytemp"
- curl -k --dump-header $TEMPFILE $REG_URL > /dev/null 2>&1
- AUTHSTR=$(cat $TEMPFILE | grep "Www-Authenticate")
- OLDIFS=$IFS
- IFS=","
- for part in echo $AUTHSTR;do
- [[ $(printf "$part" | grep "^Www-Authenticate: Bearer realm=") ]] && AUTHREALM=$(echo $part | cut -d"\"" -f2)
- [[ $(printf "$part" | egrep "^service=") ]] && AUTHSVC=$(echo $part | cut -d"\"" -f2)
- [[ $(printf "$part" | egrep "^scope=") ]] && AUTHSCOPE=$(echo $part | cut -d"\"" -f2)
- i=$(( $i + 1 ))
- done
- IFS=$OLDIFS
- printf "\nQuerying token with : ${DG}curl -ks -H \"Authorization: Basic $(echo -n \"$R_USER:$R_PASSWORD\" | base64)\" \"$AUTHREALM?service=$AUTHSVC&scope=$AUTHSCOPE\"${NC}\n"
- 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"
- MYTOK=$(curl -ks -H "Authorization: Basic $(echo -n \"$R_USER:$R_PASSWORD\" | base64)" "$AUTHREALM?service=$AUTHSVC&scope=$AUTHSCOPE")
- MYTOK=$(echo "$MYTOK" | grep token | cut -d"\"" -f4)
- [[ -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