Curl an API inside the same container on a container start - docker

I'm trying to execute curl to add a value to the API within the same container where API is starting.
Dockerfile command which I'm using to get that to work looks like
CMD java -jar ./my-api.jar ; wait 30; curl -X POST 'http://localhost:8080/user/' -H 'Content-Type: application/json' --data-raw '{ "username: "admin", "data": {"email": "admin#test.com", "password" : "somepassword"}}'
but the admin used was not added after 30 seconds when API got started.
The same curl command works fine when I will execute it within the container itself.
Any thought on that?

Got it solved with moving all to the scrip and then execute it with the CMD
Script looks like:
#!/bin/bash
set -m
java -java -jar ./my-api.jar &
sleep 20
curl -X POST 'http://localhost:8080/user/' -H 'Content-Type: application/json' --data-raw '{ "username: "admin", "data": {"email": "admin#test.com", "password" : "somepassword"}}'
fg %1

Related

Simple and double quotes in cURL in groovy

I have a Jenkins pipeline where this command works and send me a notification through google chat :
script {
sh 'curl -k "https://chat.googleapis.com/v1/spaces/AAAABHT3HT0/messages?key=*****&token=******" -d "#chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
But if I enter the url in a variable, that does not work any more :
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh 'curl -k ${url} -d "#chat_notification.json" -XPOST -H "Content-Type: application/json; charset=UTF-8"'
}
With the error :
curl -k -d #chat_notification.json -XPOST -H 'Content-Type: application/json; charset=UTF-8'
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
It's probably a quote issue ?
Yes, if you are using single quotes in Groovy, that means you cannot interpolate the string with variables using the $ syntax.
See https://groovy-lang.org/syntax.html#_string_interpolation .
Same thing applies to shell code, by the way, or sh in this case.
But since you are not using any shell variables in your code, simply swapping the quotes, i.e. quoting your Groovy string with ", and using ' within the curl command would probably work.
Yes it is a quote issue try to use "" instead of '' to use the value of url variable in curl command. You should also seperate -X and POST (you have -XPOST in your script)
script {
url = "https://chat.googleapis.com/v1/spaces/AAAAfF9CGEQ/messages?key=******&token=******"
sh "curl -k ${url} -d #chat_notification.json -X POST -H Content-Type: application/json; charset=UTF-8"
}

How to hide sensitive data from pipeline for sh commands

I have Jenkinsfile that creates mt object and passes vaultToken to psl library:
Jenkinsfile:
#Library('shared-library#psl')
Maintenance mt = new Maintenance()
mt.setVaultToken(config.vaultToken)
mt.setApiUrl(config.apiUrl)
pslService.createMaintenance(mt)
pslService:
String createMaintenance(Maintenance mt){
dockerBuildHelper.getDockerImage(dockerBuildHelper.getWdBuildDockerImageName()).inside('-u root'){
String cmd = "curl -X POST '${mt.getApiUrl()}'" +
" -H 'Content-Type: application/json'" +
" -H 'Authorization: Api-Token ${mt.getVaultToken()}'" +
" -d ${mt.getPayload()} | jq -r '.id'"
return sh(script: cmd, returnStdout: true).trim()
}
}
But this prints curl command and exposes vault token in the pipeline.
Does anyone know how I can hide the sensitive info and/or entire curl command?
I do not want to store this in credentials store, unless I have no choice.
I heard I can use set +x. But I am not sure how to use it and if it helps. Any thoughts?
Try to use mask password plugin. Or create secrets in jenkins and call them in environment block of pipeline.
I went with set +x
String cmd = """set +x
curl -X POST '${mt.getApiUrl()}' -H 'Content-Type: application/json' -H 'Authorization: Api-Token ${mt.getVaultToken()}' -d ${mt.getPayload()} | jq -r '.id'
"""
return sh(script: cmd, returnStdout: true).trim()

How to fix Jenkins script running on multinode network, curl commands in script do not work and output `Authorization Required`

I have a 4 node network running and would like to execute curl commands on top of the running network. Running on my machine and ssh-ing on to 1 node is sufficient enough to complete the test however when moving the commands to a Jenkins script I get this response
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>openresty/1.13.6.1</center>
</body>
</html>
OAuth is enabled on 1 of 4 machines. How does one solve this?
My network runs on aws nodes
curl -X POST "http://<node1_IP>/bloc/v2.2/users/<account>/<address1>/send?resolve=true" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{ \"toAddress\": \"<address2>\", \"value\": \"1\", \"password\": \"<password>\", \"metadata\": null, \"txParams\": null}"
transactionHash=$(curl -X POST "http://<node1_IP>:8080/bloc/v2.2/users/t1/<address1>/send?resolve=true" -H "accept: application/json;charset=utf-8" -H "Content-Type: application/json;charset=utf-8" -d "{ \"toAddress\": \"<address2>\", \"value\": \"1\", \"password\": \"t1\", \"metadata\": null, \"txParams\": null}" | jq -r '.txResult' | jq -r '.transactionHash')
echo "transactionHash: $transactionHash"
transactionHash=$transactionHash
I expect the first one to return data in json format, and for the second curl I expect a string to be stored in the transactionHash.

Cloud Speech API error: RecognitionAudio not set

While trying to follow the [quick start] (https://cloud.google.com/speech/docs/getting-started) for cloud speed API, after execute the "curl" command in command prompt, error occurred depicted as below:
{
"error": {
"code": 400,
"message": "RecognitionAudio not set.",
"status": "INVALID_ARGUMENT"
}
}
Why the RecognitionAudio is not set in the API itself? The sync-request.json used is same as the one in the quick start:
{
"config": {
"encoding":"FLAC",
"sampleRateHertz": 16000,
"languageCode": "en-US",
"enableWordTimeOffsets": false
},
"audio": {
"uri":"gs://cloud-samples-tests/speech/brooklyn.flac"
}
}
I'm not sure exactly what you're doing wrong but I was able to use the request as-is from the documentation without issue.
Did you get an access token for a Google Cloud project that had the speech API enabled? The following command generates the access token that can be used as a Bearer:
gcloud auth application-default print-access-token
It was helpful for me to use put the following into a script file (req.sh)
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer <output>" \
https://speech.googleapis.com/v1/speech:recognize \
-d #sync-request.json
I then just used the output from print-access-token with the script.
I had exactly the same problem and I solved it adding quotes in the -d value, like this:
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer <your-access-token>" \
https://speech.googleapis.com/v1/speech:recognize \
-d "#sync-request.json"
I was getting this error because I was not running the command in the cli in the same directory as my sync-request.json file.
Once I changed directories, I used the command from the documentation with my access token, and it worked fine.
I had the same problem and it worked for me when I omitted the #sync- from the curl command. This command worked for me:
curl -s -H "Content-Type: application/json" \
-H "Authorization: Bearer " \
https://speech.googleapis.com/v1/speech:recognize \
-d #request.json
Not sure what the exact function is of #sync ?

How to get lines of code within post trigger using Xcode Server?

My current shell script is following:
#!/bin/sh
LINES_OF_CODE=10792
curl -i -X POST -H "Content-Type:application/json" http://a:bc#lb.mycompany.org/api/public/metrics/ -d '{"project_public_id":"myprojectid", "type":"loc", "value":'$LINES_OF_CODE', "platform":"ios"}'
but it is hardcoded... is there any way to count number of lines dynamically?

Resources