hide username and password in curl command shell script - bash

I use the below curl command to disable the alert which works fine.
curl -k -u admin:password https://URL -X POST
But i am trying to hide the username and password in the below shell script but getting unauthorized exception,
SCRIPT_DIR=/tmp
USER=$(cat $SCRIPT_DIR/.nonprodusr.txt)
PWD=$(cat $SCRIPT_DIR/.nonprod.txt)
curl -k -u $USER:$PWD https://url -X POST

You can use a netrc file : https://everything.curl.dev/usingcurl/netrc
/tmp/netrc :
login admin
password Passw0rd
And use it with this option :
curl -k --netrc-file /tmp/.netrc https://url -X POST

One can use a configuration file to add user/password as well as other configurations.
curl --config /home/me/curl-configuration.txt <url>
Contents of "/home/me/curl-configuration.txt":
--user <username>#<password>

Related

Sending email using cURL with a password with special character in bash script

I have an SMTP configuration like the follows: (everything is dummy data)
SMTP_USERNAME="fakenews#yahoo.comg"
SMTP_SERVER="smtp.office365.com"
SMTP_PASSWORD='rdjsllskdslsds!'
SMTP_PORT="587"
EMAIL_FROM="support#fakernews.com"
EMAIL_TO="rfakeuser#fakernews.com"
Note the "!" mark at the end of the password. This cannot be changed.
When I run the below command with everything hardcoded, it works fine, and an email is sent.
curl --url smtp://smtp.office365.com:587 --mail-from support#fakernews.com --mail-rcpt rfakeuser#fakernews.com --upload-file tempemailbody_1.txt --user fakenews#yahoo.comg:rdjsllskdslsds! --ssl
However, when I replace them with variables, as below, I keep getting access denied.
curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" --mail-from $EMAIL_FROM --mail-rcpt $EMAIL_TO --upload-file tempemailbody_1.txt --user "$SMTP_USERNAME:$SMTP_PASSWORD" --ssl
The error message is :
curl: (9) remote access denied: 501
I think that this is because somehow the variable interpretation is causing the issue, but I cant figure out what is it. What should I change in the command to make it work ?

Jira API GET all users from Jira project with specific status

In my Ruby app I'm trying to get all agent users from my service desk board. It means all users with status: 'ServiceDesk'. Is it possible using only base auth?
In curl I was trying something like:
curl -D -u USERNAME:PASSWORD -X GET -H "Content-Type: application/json" https://company_name.atlassian.net/rest/api/2/user/assignable/search?project=SERVICEDESK
But all what I get is an error:
Warning: The file name argument '-u' looks like a flag.
curl: (3) URL using bad/illegal format or missing URL
{"errorMessages":["Internal server error"],"errors":{}}%
Is there any way to get those data with basic auth?
I think there's an issue with the curl command, try this
curl -D- -u USERNAME:PASSWORD https://company_name.atlassian.net/rest/api/2/user/assignable/search?project=SERVICEDESK

Curl command on windows not passing user:pass correctly

I'm using CURL to execute some API calls against a bitbucket server. The command is like this:
curl https://bitbucket.myserver.com/rest/api/1.0/projects/PRJ/repos/repo-slug/tags \
-u 'user:pass' \
-X POST \
-H 'Content-type: application/json' \
-d '{"name" : "test-tag", "message": "Test tag from curl", "startPoint": "master" }'
This is expected to create a tag on the master branch in the repo. This however fails complaining of incorrect username/password.
I then copy/paste the command into git-bash prompt - and it works as expected, completing successfully. I tried this with multiple user accounts. I also tried specifying only the username and then entering the password on command line - with the same results.
How can I get curl on windows to pass correct username/password to the server?

Why I can't use API for common user integrated with OIDC in ICP(IBM cloud private)

https://www.ibm.com/support/knowledgecenter/SSBS6K_2.1.0/apis/auth_manage_api.html
I try to use the API for common user integrated with OIDC, but the error msg shows:
{"error_description":"invalid_resource_owner_credential","error":"server_error"}
command as the following
curl -k -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -d "grant_type=password&username=abc\#test\.com&password=ChangeMe\!\#\#&scope=openid" https://<cluster_access_ip>:8443/idprovider/v1/auth/identitytoken --insecure
But it is working fine for the administrator: admin/admin, so strange.
Issue is with the special character "!" which is used for history expansions in command line prompt.
You can use below command which works...
curl -k -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -d "grant_type=password&username=abc#test.com&password=ChangeMe"'!'"##&scope=openid" https://<cluster_access_ip>:8443/idprovider/v1/auth/identitytoken --insecure
Have you configured the LDAP, created Teams and added users to the team? Did you check the logs on Master node /var/log/containers for platform-identity-manager, _platform-auth-service, *platform-identity-provider?

Get Jenkins job status by using curl

I know, that there is a way to enable or disable jenkins job by using commands
curl -u user:password -X POST http://server/job/jobname/enable
curl -u user:password -X POST http://server/job/jobname/disable
But what I need - is to get the status of job "enabled/disables" and write it to the bash $status variable.
Is there a way to do it?
You can check if job is enabled or disabled using the API
http://server:port/job/jobname/api/xml?xpath=*/buildable
So, with crumb, you can use something like this:
CRUMB=$(curl -s 'http://USER:PASSWORD#SERVER:PORT/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
myStatus=$(curl -X POST -H "$CRUMB" "http://USER:PASSWORD#SERVER:PORT/job/jobname/api/xml?xpath=*/buildable")
And in variable myStatus you get
<buildable>true</buildable>
or
<buildable>false</buildable>

Resources