Curl command in shell script returning error {"Error":"bpapigw-300 Cannot authorize access to resource" - shell

I am trying to execute curl using this shell script:
#!/bin/bash
curl -k -H "Content-Type:application/json" -d '{"username":"admin","password":"adminpw", "tenant":"master"}' https://localhost/tron/api/v1/tokens > /tmp/token.data
grep -Po '{\"token\":\"\K[^ ]...................' /tmp/token.data > /tmp/token
tokendetails=`cat /tmp/token`
for token in $tokendetails
do
TOKEN=`echo $token`
done
userdetails=`cat /tmp/curloutput.txt | sed 's/{"clientInactivityTime"/\n{"clientInactivityTime"/g' | sed 's/\(.*\).\("firstName":[^,]*\)\(.*\)\("lastName":[^,]*\)\(.*\)\("email":[^,]*\)\(.*\)\("username":[^,]*\)\(.*\)/\2,\4,\6,\8/g' | grep username`
for user in $userdetails
do
firstName=`echo $user | sed 's/,/\n/g' | grep firstName | sed 's/.*:"\([^"]*\).*/\1/g'`
lastName=`echo $user | sed 's/,/\n/g' | grep lastName | sed 's/.*:"\([^"]*\).*/\1/g'`
email=`echo $user | sed 's/,/\n/g' | grep email | sed 's/.*:"\([^"]*\).*/\1/g'`
username=`echo $user | sed 's/,/\n/g' | grep username | sed 's/.*:"\([^"]*\).*/\1/g'`
curl -k -X POST "https://haxsne09/tron/api/v1/users" -H "accept: application/json" -H "Authorization: Bearer =${TOKEN}" -H "Content-Type: application/x-www-form-urlencoded" -d "first_name=${firstName}\&last_name=${lastName}\&email=${email}\&password=Tata123^\&username=${username}\&is_active=true"
echo $RESPONSE
done
I am getting ths error:
{"Error":"bpapigw-300 Cannot authorize access to resource: Could not authorize path for user identifier: Failed to get Roles for identifier: REST operation failed 0 times: '[GET /api/v1/current-user][401] currentUserListUnauthorized \u0026{Detail:Invalid token}'. This user is unauthenticated?"}
Do I need to add any syntax before executing curl -k -X POST?

What I see is that -H "Authorization: Bearer =${TOKEN}" contains an = sign which shouldn't be there...
It should be: -H "Authorization: Bearer ${TOKEN}"
More, in a command you use /tmp/curloutput.txt file, which is never created by your script...

The Authorization header you are using is not working. Maybe the syntax is not Bearer =aAbBcCdDeEfF0123456 but something else for the server running on haxsne09, maybe without the = like #MarcoS suggests.
Alternatively, your grep command may be returning one too many characters (a rogue quote maybe).
I rewrote your code below to be more readable. You will notice that I:
Changed your matching groups in sed to capture only the needed parts and put them in variables using read. I also used the -E flag to avoid having to use \( and \)
Removed the useless for loops
Quoted all variable expansions properly
Added some line breaks for readability
Removed some temporary files and associated useless uses of cat
Here is the updated script:
#!/bin/bash
curl -k -H 'Content-Type:application/json' -d \
'{"username":"admin","password":"adminpw", "tenant":"master"}' \
https://localhost/tron/api/v1/tokens > /tmp/token.data
token=$(grep -Po '{"token":"\K[^ ]...................' /tmp/token.data)
IFS=, read -r firstName lastName email username < <(
</tmp/curloutput.txt sed 's/{"clientInactivityTime"/\n&/' |
sed -nE 's/.*."firstName":"([^"]*)".*"lastName":"([^"]*)").*"email":"([^"]*).*"username":"([^"]*)".*/\1,\2,\3,\4/p'
)
curl -k -X POST 'https://haxsne09/tron/api/v1/users' -H 'accept: application/json' \
-H "Authorization: Bearer $token" -H "Content-Type: application/x-www-form-urlencoded" -d \
"first_name=$firstName&last_name=$lastName&email=$email&password=Tata123^&username=$username&is_active=true"
echo

Related

how to put 2 elements from jq variables in one request?

There is a part of the script where each request receives a response and is written to a variable. How to do it in one request with writing to variables?
boolStatus=$(curl -X 'GET' \
"https://tsit-app1/api/v2/workItems/$case?versionNumber=0" \
-H 'accept: application/json' \
-H "Authorization: $apiKey" | jq '.isAutomated')
echo $boolStatus
name=$(curl -X 'GET' \
"https://tsit-app1/api/v2/workItems/$case?versionNumber=0" \
-H 'accept: application/json' \
-H "Authorization: $apiKey" | jq '.name')
echo $name
I tried
curl -X 'GET' \
"https://tsit-app1/api/v2/workItems/$case?versionNumber=0" \
-H 'accept: application/json' \
-H "Authorization: $apiKey" | jq '"boolStatus=\(.isAutomated)", "name=\(.name)"'
but in echo i get
"boolStatus=true",
"name=bla bla"
need to
echo $boolStatus
true
echo $name
bla bla
One way would be to use the #sh string interpolation and then use your shell's eval.
Using the string interpolation would output something like:
boolStatus=true
name='abc'
which can then be fed to eval:
vars="$(curl ... | jq -r '#sh "boolStatus=\(.isAutomated)", #sh "name=\(.name)"')"
eval "$vars"
or explicitly output the line break:
jq -r '#sh "boolStatus=\(.isAutomated)\nname=\(.name)"'
Disclaimer: Note that this will evaluate any shell code and might open your system to malicious code (#sh escapes the values, but it's always a good idea to be aware of this).
Use process substitution to allow two uses of read to read from the output of jq.
{ read boolStatus; read name; } < <(curl ... | jq -r '.isAutomated, .name')
(assuming the name does not contain any newlines).
At the very least, you can save the output to process with jq twice.
response=$(curl ...)
name=$(echo "$response" | jq -r .name)
boolStatus=$(echo "$response" | jq -r .isAutomated)

concat a string variable in jq bash script

here is my bash script
test.sh
host='select((.tag_name!="dev")
curl -H "Authorization: token <token>" https:<git release url> | jq -r '[.[] | <iwant host variable placed here> ][0].assets[0].browser_download_url'
I want to do something like below
curl -H "Authorization: token <token>" https:<git release url> | jq -r '[.[] | select(.tag_name=="dev" )][0].assets[0].browser_download_url'
Try this: $(host)
host='select(.tag_name=="dev")'
curl -H "Authorization: token <token>" https:<git release url> | jq -r '[.[] | $(host) ][0].assets[0].browser_download_url'
... | jq -r '[.[] | '"$host"' ][0].assets[0].browser_download_url'

Bash - for looping a curl gives no output

so, I am currently trying to connect Data from a Consul Cluster with those from FNT. I get the Data I need by curling for it in the Consul API and the returning Server names shall be checked against FNT to get the Server owner.
Following is the Consul curl:
gethosts=$(curl -s -H "Authorization: Bearer <TOKEN>" <CONSUL URL> | jq -cr '.[] | select(.NodeMeta.type == "physical") | .ServiceAddress')
Following is the FNT curl:
curl -s -H "Content-Type: application/json" -k -X POST -d '{}' "<FNT URL>" | jq '.returnData[] | select(.cFqdn == "<FQDN>") | .cResponsible + "/" + .cFqdn'
Both work perfectly fine on their own. The Consul Curl gets me every FQDN from every physical (hardware) hosts and if i paste one of those FQDNs into the FNT curl it gets me the FQDN again + the responsible Owner for that server.
Now i wanted to combine those in a loop to get every single FQDN from Consul checked against FNT with the following:
 for i in $gethosts; do curl -s -H "Content-Type: application/json" -k -X POST -d '{}' "<FNT URL>" | jq '.returnData[] | select(.cFqdn == $i) | .cResponsible + " " + .cFqdn'; done
But it simply doesnt work. There is no error or anything i can work with. Just no output at all.
Does anyone of you see the mistake in my for loop? cause I definitely can't, probably already code blind after all those hours of troubleshooting :D
Thanks in advance!
P.S.:
I also tried
for i in $gethosts; do $(curl -s -H "Content-Type: application/json" -k -X POST -d '{}' "<FNT URL>" | jq '.returnData[] | select(.cFqdn == $i) | .cResponsible + " " + .cFqdn'); done
or
for i in $gethosts; do curl -s -H "Content-Type: application/json" -k -X POST -d '{}' "<FNT URL>" | jq '.returnData[] | select(.cFqdn == <FQDN>) | .cResponsible + " " + .cFqdn'; done
For my understanding, the last one should always have the Same outpout but as many times as hosts are in $gethosts. I did this to see if $i in .cFqdn is the problem, but it seems like it isnt.
I fixed it.
for a in $gethosts; do curl -s -H "Content-Type: application/json" -k -X POST -d '{}' "<FNTURL>" | jq "[.returnData[] | select(.cFqdn == \"$a\") | .cResponsible + \";\" + .cFqdn] | .[]"; done
Guess I had some quoting issues.

Strange behavior when parsing result from curl + awk

Using curl on ubuntu I am trying to fetch the Jenkins version inspired by:
https://wiki.jenkins.io/display/JENKINS/Remote+access+API
In a bash script I do:
VERSION=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" $URL | grep -Fi X-Jenkins: | awk '{print $2}')
echo "__A__[${VERSION}]__B__"
But when I run the script I get:
]__B__2.89.2
So for some reason the prefix: __A__[ gets swallowed and the suffix gets turned into a prefix.
I have also tried to trim the output with:
VERSION=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" $URL | grep -Fi X-Jenkins: | awk '{print $2}' | sed -e 's/^[ \t]*//')
But it gives the same result.
As suggested below I have also tried with:
echo '__A__['"${VERSION}"']__B__'
But still gives the same/wrong result.
A few other things I have tried (giving the same result)
Same/wrong output
VERSION=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" $URL | grep -i X-Jenkins: | awk '{print $2}')
echo '__A__['"${VERSION}"']__B__'
Same/wrong output
VERSION=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" $URL | grep X-Jenkins: | awk '{print $2}')
echo '__A__['"${VERSION}"']__B__'
Based on below suggestion I have now tried:
echo $VERSION|od -ax
Which gives:
0000000 2 . 8 9 . 2 cr nl
2e32 3938 322e 0a0d
0000010
If I compare that with:
VERSION_TEMP="2.89.2"
echo $VERSION_TEMP|od -ax
I get:
0000000 2 . 8 9 . 2 nl
2e32 3938 322e 000a
0000007
So looks like its the cr in the VERSION var that is causing the issue (not sure how that explains the whole reversing of prefix/suffix as described above).
SOLVED: Based on input from Romeo I now got it to work with adding |tr -d '\r' :
VERSION=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" $URL | grep X-Jenkins: | awk '{print $2}'|tr -d '\r')
Apparently the output contains a DOS carriage return.
Try adding tr -d '\015':
version=$(curl -k -i -X GET --insecure --silent --header \"Authorization: Bearer $TOKEN \" "$URL" |
tr -d '\015' |
awk 'tolower($0) !~ /x-jenkins:/{print $2}')
echo "__A__[$version]__B__"
Uppercase variable names are reserved for system use, so I changed yours to lower case, too, and removed the useless grep.

How to get a specific string from a variable using Shell Script

I have a code which returns an access token string.
#!/bin/bash
token=$(curl --globoff -X POST "https://example.com/token" -F 'grant_type=client_credentials' -F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' -F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934')
echo "$token" > Token.txt
The output given by this command is :
{
"access_token": "5048e7d38e73b4a809a4fcb219b63ae34f34f43f83d6663ffd777203afb5654ab",
"token_type": "bearer",
"expires_in": 7200
}
ie. the variable token contains the above result. My question is how to get the access token 5048e7d38e73b4a809a4fcb219b63ae34f34f43f83d6663ffd777203afb5654ab alone from this and save to another variable.
First try jq, if not possible, this is a workaround using awk:
access_token=$(curl --globoff -X POST "https://example.com/token" \
-F 'grant_type=client_credentials' \
-F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' \
-F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934'|\
awk -F\" '/access_token/{print $4}')
The way to do this in jq would be to do
access_token=$(curl --globoff -X POST "https://example.com/token" \
-F 'grant_type=client_credentials' \
-F 'client_id=d86fc9b690963b8dda602bd26f33454r457e9024a4ecccf4c3bbf66ffcbc241b' \
-F 'client_code'='ff148c56118728b62c9f2ew3e4r009a7a1c645276b70611fa32fa055b9944934'|\
jq -r '.access_token' )
I am using this in bash to get and use the token ...
access_tok=$(curl -s -X POST -d "client_id=$(< "$LOGON_HOME/client_id" )&client_secret=$(< "$LOGON_HOME/client_secret")&grant_type=client_credentials" "${TOKEN_URL}" )
access_tok=${access_tok#*access_token\":\"}
access_tok=${access_tok%%\"*}
and later
curl -l -s -O -H "Authorization: Bearer ${access_tok}" "${FILE_URL}"

Resources