I believe I am running into a scenario where escaping quotes and utilizing variables as strings is causing some issues and after some hours of troubleshooting this isn't seeming to come to a conclusion.
We created a login script (For reference comparing log files with API times received). We are using this to create some curl commands that will bring down some data from our API.
End State user selects either -l
for login with username password or -a
for given ApiKey, which require different formatted curl commands.
#!/bin/bash
while getopts ":l:u:a:c:" arg; do
case "${arg}" in
l)
l=${OPTARG}
echo "Input Password for User"
read -s -p "Password: " Password
login="-u $l:$Password"
;;
u)
u=${OPTARG}
;;
a)
a=${OPTARG}
login="-H \"Authorization: ApiKey $a\""
;;
\?)
echo "Usage: RunMe.sh [-u] https://URL:9243 [-l] username or RunMe.sh [-u] https://URL:9243 [-a] ApiKey"
;;
c)
c=${OPTARG}
logDirectory=$f
;;
esac
done
TSC="$(curl -s -X GET $login $u/index/_search -k)"
echo "curl -s -X GET $login $u/index/_search -k"
echo $TSC
Running across an issue where I can successfully complete curl requests using -l
so it's just passing "-u USER:PASSWORD
" into the curl.
bash RunMe.sh -l user-u URL.com
output per my code shows a successful formatted call to API with username:password
curl -s -X GET -u USER:PASSWORD https://URL/index/_search -k
BUT when I do
bash RunMe.sh -a Base64 -u URL.com
I get an error that something isn't accepting the API key. However, since I am echoing out the actual command I am using in the previous line, I can copy and paste it back into the console and it works 100%.
echo out to the console on what is being run is
curl -s -X GET -H "Authorization: ApiKey (Base64APiKey==)" https://URL/index/_search -k
So my thought is that when I am converting the input into the curl command the string is being malformed. Any thoughts on this?
I am able to change out the code to do this which makes it work, however, this doesn't fully make sense since I assumed the value to be a string to begin with.
str="curl -s -X GET $login $u/index/_search -k"
TSC=$(eval "$str")
echo $TAC
login=(-H "Authorization: ApiKey $a")
and thencurl "${login[@]}" ...
and you're done. – ilkkachu Nov 02 '22 at 18:03Using the Array just as you put it, outputs exactly what we expect is going on to the echo line. But the command itself fails. If I copy and paste that exact string from the echo line (echo $TSC) it works. NO issues.
Im may need to packet capture to see what is actually being sent at this point
– Nathan Nov 02 '22 at 18:08set -x
set at the start, to see what the shell actually runs. – ilkkachu Nov 02 '22 at 18:10So the question is WHY does that work like that?
– Nathan Nov 02 '22 at 18:12login="-H \"Authorization: ApiKey $a\""
withcurl ... $login ...
not working is described in https://unix.stackexchange.com/q/444946/170373, see the part about "Why it fails". (If you did something else, then it would be easier to see what happens if you [edit] the question to show what exactly you tried...) – ilkkachu Nov 02 '22 at 18:16-H
option would be"Authorization:
, and the followingApiKey
and the actual apikey itself (plus the trailing"
) would come as separate arguments, probably ones thatcurl
would treat as URLs to fetch. – ilkkachu Nov 02 '22 at 18:32