bashhttpcurlmesospherequoting

curl PUT using auth token header to mesosphere fails without eval


EDIT:

I have managed to make it work with

response=$(
  curl -k -X PUT -d "$marathon_payload" --write-out %{http_code} --silent --output "$tmp"\
       -H "Authorization: token=$dcos_token" -H "$header_content_type"  $app_id_url
)

The single quotes were causing the problem. It took a few gyrations but all good.

MORAL: quotes inside the value don't matter if the value is properly quoted UNLESS you eval the whole thing, and I should have known that. Occam's wins again.

end edit

I am initiating Mesosphere microservice deployments with curl, but it won't succeed without using eval. Since I recently inherited this code I've been trying to scrub the eval out of it just as a matter of habit, but it's thwarting me.

The script initiates the deployment with

response=$(
 eval curl -k -X PUT -d "'$marathon_payload'" --write-out %{http_code} --silent --output $tmp\
       -H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $app_id_url
)

If it gets a 200 or a 201, it loops a curl to effectively screen-scrape the deployments page till the request disappears.

chkDeploy() { rm -f $tmp;
  eval curl -k -X GET --silent --write-out %{http_code} --silent --output $tmp\
            -H "'Authorization: token=$dcos_token'" -H "'$header_content_type'" $deployments_url
}
response=$( chkDeploy )

$dcos_token is a base64 encoded string.

It then checks the service with another curl loop to the info page so it can verify the version number. This one is working fine with no eval.

chkCode() {
  curl -k -X GET --write-out %{http_code} --silent --output $tmp $info_url;
}
response=$( chkCode )

The first two return 401, authentication failure. I'm guessing the auth token quoting is off.


Solution

  • There's no reason to use eval here; you just need to quote the arguments to -H properly.

    response=$(
      curl -k -X PUT -d "$marathon_payload" \
         --write-out %{http_code} \
         --silent --output "$tmp" \
         -H "Authorization: token=$dcos_token" \
         -H "$header_content_type" "$app_id_url"
    )