1

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
Nathan
  • 23
  • @steeldriver I have, I attempted using the Array, Setting this as a function, but did not use eval as there's some "warnings" to using it that I deemed as not a good work around. – Nathan Nov 02 '22 at 18:00
  • @Nathan, err, so, what have you tried, then? Can you show how you used that array? – ilkkachu Nov 02 '22 at 18:03
  • That would just be login=(-H "Authorization: ApiKey $a") and then curl "${login[@]}" ... and you're done. – ilkkachu Nov 02 '22 at 18:03
  • @ilkkachu using exactly what you had.
    Using 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:08
  • @Nathan, [edit] your question to show the code you run and the output. The exact code. If you like, run it with set -x set at the start, to see what the shell actually runs. – ilkkachu Nov 02 '22 at 18:10
  • @ilkkachu I actually went back and (reformatted) what you I had, You were correct. Its a matter of formatting the login=() vice using login="".

    So the question is WHY does that work like that?

    – Nathan Nov 02 '22 at 18:12
  • @Nathan, the part about login="-H \"Authorization: ApiKey $a\"" with curl ... $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
  • @ilkkachu, I’m gonna post that an grab a packet capture of what each sent. Then credit it to you. Not sure how I missed the initial () instead of “”. Thank you – Nathan Nov 02 '22 at 18:28
  • With the code as shown above, the argument to the -H option would be "Authorization:, and the following ApiKey and the actual apikey itself (plus the trailing ") would come as separate arguments, probably ones that curl would treat as URLs to fetch. – ilkkachu Nov 02 '22 at 18:32

0 Answers0