@@ -499,13 +499,48 @@ log_token_err() {
499499}
500500
501501keycloak_token () {
502+ local keycloak_pass=$1
503+
504+ # Log the start of token retrieval
505+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Starting Keycloak token retrieval\" }" >> " $TMP_DIR /gather_token.log"
506+
502507 client_secret=$( oc -n " ${RHDH_NAMESPACE} " get secret keycloak-client-secret-backstage -o template --template=' {{.data.CLIENT_SECRET}}' | base64 -d)
503- curl -s -k " $( keycloak_url) /realms/backstage/protocol/openid-connect/token" \
508+
509+ local token_url=" $( keycloak_url) /realms/backstage/protocol/openid-connect/token"
510+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" token_url\" :\" $token_url \" ,\" message\" :\" Making Keycloak token request\" }" >> " $TMP_DIR /gather_token.log"
511+
512+ # Capture both response and HTTP status
513+ local token_response=$( curl -s -k -w " HTTPSTATUS:%{http_code}" " $token_url " \
504514 -d username=guru \
505- -d " password=$1 " \
515+ -d " password=$keycloak_pass " \
506516 -d ' grant_type=password' \
507517 -d ' client_id=backstage' \
508- -d " client_secret=$client_secret " | jq -r " .expires_in_timestamp = $( python3 -c ' from datetime import datetime, timedelta; t_add=int(30); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))' ) "
518+ -d " client_secret=$client_secret " )
519+
520+ # Extract HTTP status code
521+ local http_code=$( echo " $token_response " | tr -d ' \n' | sed -e ' s/.*HTTPSTATUS://' )
522+
523+ # Extract response body
524+ local response_body=$( echo " $token_response " | sed -e ' s/HTTPSTATUS:.*//g' )
525+
526+ # Log the complete token response
527+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" http_code\" :\" $http_code \" ,\" token_url\" :\" $token_url \" ,\" response_body\" :$( echo " $response_body " | jq -c ' .' 2> /dev/null || echo " \" $response_body \" " ) }" >> " $TMP_DIR /gather_token.log"
528+
529+ # Check for error status codes
530+ if [ " $http_code " -eq 401 ]; then
531+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" http_code\" :\" $http_code \" ,\" message\" :\" UNAUTHORIZED - Invalid credentials for Keycloak token\" ,\" token_url\" :\" $token_url \" }" >> " $TMP_DIR /gather_token.log"
532+ elif [ " $http_code " -eq 403 ]; then
533+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" http_code\" :\" $http_code \" ,\" message\" :\" FORBIDDEN - Client not authorized for Keycloak token\" ,\" token_url\" :\" $token_url \" }" >> " $TMP_DIR /gather_token.log"
534+ elif [ " $http_code " -eq 400 ]; then
535+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" http_code\" :\" $http_code \" ,\" message\" :\" BAD REQUEST - Invalid token request parameters\" ,\" token_url\" :\" $token_url \" }" >> " $TMP_DIR /gather_token.log"
536+ elif [ " $http_code " -ge 400 ]; then
537+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" http_code\" :\" $http_code \" ,\" message\" :\" HTTP ERROR - Keycloak token request failed\" ,\" token_url\" :\" $token_url \" }" >> " $TMP_DIR /gather_token.log"
538+ else
539+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Keycloak token response received\" ,\" http_code\" :\" $http_code \" }" >> " $TMP_DIR /gather_token.log"
540+ fi
541+
542+ # Process the response
543+ echo " $response_body " | jq -r " .expires_in_timestamp = $( python3 -c ' from datetime import datetime, timedelta; t_add=int(30); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))' ) "
509544}
510545
511546rhdh_token () {
@@ -516,29 +551,56 @@ rhdh_token() {
516551 REALM=" backstage"
517552 CLIENTID=" backstage"
518553
554+ # Log the start of RHDH token retrieval
555+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Starting RHDH token retrieval\" ,\" auth_provider\" :\" $AUTH_PROVIDER \" }" >> " $TMP_DIR /gather_token.log"
556+
519557 if [[ " ${AUTH_PROVIDER} " != " keycloak" ]]; then
520- # Corrected jq command for non-keycloak provider
521- ACCESS_TOKEN=$( curl -s -k --cookie " $COOKIE " --cookie-jar " $COOKIE " " $( backstage_url) /api/auth/guest/refresh" | jq -r " .backstageIdentity | .expires_in_timestamp = $( python3 -c ' from datetime import datetime, timedelta; t_add=int(50*60); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))' ) " )
558+ # Log guest refresh attempt
559+ local guest_url=" $( backstage_url) /api/auth/guest/refresh"
560+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Using guest authentication\" ,\" refresh_url\" :\" $guest_url \" }" >> " $TMP_DIR /gather_token.log"
561+
562+ ACCESS_TOKEN=$( curl -s -k --cookie " $COOKIE " --cookie-jar " $COOKIE " " $guest_url " | tee -a " $TMP_DIR /get_rhdh_token.log" | jq -r " .backstageIdentity | .expires_in_timestamp = $( python3 -c ' from datetime import datetime, timedelta; t_add=int(50*60); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))' ) " )
563+
564+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Guest token retrieved\" ,\" response_body\" :$( echo " $ACCESS_TOKEN " | jq -c ' .' 2> /dev/null || echo " \" $ACCESS_TOKEN \" " ) }" >> " $TMP_DIR /gather_token.log"
522565 echo " $ACCESS_TOKEN "
523566 return
524567 fi
525568
569+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Making refresh URL call\" ,\" refresh_url\" :\" $REFRESH_URL \" }" >> " $TMP_DIR /gather_token.log"
570+
526571 LOGIN_URL=$( curl -I -k -sSL --dump-header " $TMP_DIR /login_url_headers.log" --cookie " $COOKIE " --cookie-jar " $COOKIE " " $REFRESH_URL " )
527572 state=$( echo " $LOGIN_URL " | grep -oE ' state=[^&]+' | grep -oE ' [^=]+$' | sed ' s/%2F/\//g;s/%3A/:/g' )
528573
574+ if [ -z " $state " ]; then
575+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Failed to extract state from refresh URL response\" ,\" refresh_url\" :\" $REFRESH_URL \" }" >> " $TMP_DIR /gather_token.log"
576+ else
577+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Extracted state from refresh response\" ,\" state\" :\" $state \" }" >> " $TMP_DIR /gather_token.log"
578+ fi
579+
580+ local keycloak_auth_url=" $( keycloak_url) /realms/$REALM /protocol/openid-connect/auth"
581+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Making Keycloak auth URL call\" ,\" auth_url\" :\" $keycloak_auth_url \" }" >> " $TMP_DIR /gather_token.log"
582+
529583 AUTH_URL=$( curl -k -sSL --dump-header " $TMP_DIR /auth_url_headers.log" --get --cookie " $COOKIE " --cookie-jar " $COOKIE " \
530584 --data-urlencode " client_id=${CLIENTID} " \
531585 --data-urlencode " state=${state} " \
532586 --data-urlencode " redirect_uri=${REDIRECT_URL} " \
533587 --data-urlencode " scope=openid email profile" \
534588 --data-urlencode " response_type=code" \
535- " $( keycloak_url) /realms/$REALM /protocol/openid-connect/auth" 2>&1 | tee " $TMP_DIR /auth_url.log" | grep -oE ' action="[^"]+"' | grep -oE ' "[^"]+"' | tr -d ' "' )
589+ " $keycloak_auth_url " 2>&1 | tee " $TMP_DIR /auth_url.log" | grep -oE ' action="[^"]+"' | grep -oE ' "[^"]+"' | tr -d ' "' )
590+
591+ if [ -z " $AUTH_URL " ]; then
592+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Failed to get auth URL from Keycloak\" ,\" auth_url\" :\" $keycloak_auth_url \" }" >> " $TMP_DIR /gather_token.log"
593+ else
594+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Received auth URL from Keycloak\" }" >> " $TMP_DIR /gather_token.log"
595+ fi
536596
537597 execution=$( echo " $AUTH_URL " | grep -oE ' execution=[^&]+' | grep -oE ' [^=]+$' )
538598 tab_id=$( echo " $AUTH_URL " | grep -oE ' tab_id=[^&]+' | grep -oE ' [^=]+$' )
539599 # shellcheck disable=SC2001
540600 AUTHENTICATE_URL=$( echo " $AUTH_URL " | sed -e ' s/\&/\&/g' )
541601
602+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Making authentication call\" }" >> " $TMP_DIR /gather_token.log"
603+
542604 CODE_URL=$( curl -k -sS --dump-header " $TMP_DIR /code_url_headers.log" --cookie " $COOKIE " --cookie-jar " $COOKIE " \
543605 --data-raw " username=${USERNAME} &password=${PASSWORD} &credentialId=" \
544606 --data-urlencode " client_id=${CLIENTID} " \
@@ -550,14 +612,33 @@ rhdh_token() {
550612 code=$( echo " $CODE_URL " | grep -oE ' code=[^&]+' | grep -oE ' [^=]+$' )
551613 session_state=$( echo " $CODE_URL " | grep -oE ' session_state=[^&]+' | grep -oE ' [^=]+$' )
552614
615+ if [ -z " $code " ]; then
616+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Failed to extract authorization code\" }" >> " $TMP_DIR /gather_token.log"
617+ else
618+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Extracted authorization code\" }" >> " $TMP_DIR /gather_token.log"
619+ fi
620+
553621 # shellcheck disable=SC2001
554622 CODE_URL=$( echo " $CODE_URL " | sed -e ' s/\&/\&/g' )
555623
624+ # Log final token exchange call
625+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Making final token exchange\" }" >> " $TMP_DIR /gather_token.log"
626+
556627 ACCESS_TOKEN=$( curl -k -sSL --dump-header " $TMP_DIR /get_rhdh_token_headers.log" --cookie " $COOKIE " --cookie-jar " $COOKIE " \
557628 --data-urlencode " code=$code " \
558629 --data-urlencode " session_state=$session_state " \
559630 --data-urlencode " state=$state " \
560631 " $CODE_URL " | tee -a " $TMP_DIR /get_rhdh_token.log" | jq " .backstageIdentity | .expires_in_timestamp = $( python3 -c ' from datetime import datetime, timedelta; t_add=int(30*60); print(int((datetime.now() + timedelta(seconds=t_add)).timestamp()))' ) " )
632+
633+ # Log the complete RHDH token response
634+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" RHDH token exchange complete\" ,\" response_body\" :$( echo " $ACCESS_TOKEN " | jq -c ' .' 2> /dev/null || echo " \" $ACCESS_TOKEN \" " ) }" >> " $TMP_DIR /gather_token.log"
635+
636+ if echo " $ACCESS_TOKEN " | jq -e ' .token // .access_token' > /dev/null 2>&1 ; then
637+ echo " {\" level\" :\" info\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" RHDH token successfully retrieved\" }" >> " $TMP_DIR /gather_token.log"
638+ else
639+ echo " {\" level\" :\" error\" ,\" ts\" :\" $( date -u -Ins) \" ,\" message\" :\" Failed to retrieve valid RHDH token\" }" >> " $TMP_DIR /gather_token.log"
640+ fi
641+
561642 echo " $ACCESS_TOKEN "
562643}
563644
0 commit comments