How can I use Curl to interact with websudo in Jira or Confluence?

David Corley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2012

I want to be able to kick of the re-indexing processes in Jira and Confluence from the command line as part of our sandbox refresh automation.

For this, I'm using bash and curl.

However I cannot figure out how to deal with the websude redirect that occurs when you request any admin page in either of the applications. My script for Confluence is below (based on similar Jira script I saw on confluence.atlassian.com)

#!/bin/sh
### SETTINGS ###
read -p "Enter LDAP user: " USERNAME
read -s -p "Enter LDAP password: " PASSWORD
DASHBOARD_PAGE_URL=https://<SERVER>/dashboard.action
INDEX_PAGE_URL=https://<SERVER>/admin/search-indexes.action
WEBSUDO_REDIRECT=https://<SERVER>/authenticate.action?destination=/admin/search-indexes.action
COOKIE_FILE_LOCATION=confluencecoookie
### COMMANDS ###
curl -v -u $USERNAME:$PASSWORD --cookie-jar $COOKIE_FILE_LOCATION --output /dev/null $DASHBOARD_PAGE_URL
#curl -v --cookie-jar $COOKIE_FILE_LOCATION --cookie $COOKIE_FILE_LOCATION --header "X-Atlassian-Token: no-check" -d "password=$PASSWORD" --output /dev/null $WEBSUDO_REDIRECT
curl -v --location --cookie $COOKIE_FILE_LOCATION --header "X-Atlassian-Token: no-check" -d "webSudoPassword=$PASSWORD" -d "rebuild-search-index-form=" --output /dev/null $INDEX_PAGE_URL
rm $COOKIE_FILE_LOCATION

Any ideas here?

5 answers

2 votes
EddieW
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 25, 2013

You can authenticate, save the cookie, and post to the websudo page specifying the destination as the protected action to run.

curl -o /dev/null -k -c cookies.txt -d "os_username=${USERNAME}&os_password=${PASSWORD}" https://host/jira/login.jsp

token=`tail -1 cookies.txt | tr -s "\t" " " | cut -d" " -f7`

curl -o a.html -kL -b cookies.txt -d "webSudoPassword=${PASSWORD}&webSudoDestination=/your/target/action&atl_token=${token}" "https://host/jira/secure/admin/WebSudoAuthenticate.jspa"

MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 5, 2013

Thanks, that helped. I had to also pass the header:

--header "X-Atlassian-Token: no-check"

Vineetha February 21, 2014

Hi ,

Did you make it work for confluence ? If yes, please could you post it here ?We have been trying to automate index rebuild for confluence ( ver 4.3.7) . The script reaches the search indexes screen - but it is not able to trigger the reindex action. Our script goes like this :

"

curl -k -c cookies.txt -d "os_username=<user>&os_password=<password>&os_destination=/dashboard.action&login=Log In" https://<server>/confluence/dologin.action<br< a="">>
cookieID=`tail -1 cookies.txt | tr -s "\t" " " | cut -d" " -f7`

echo ${cookieID}

curl -o b.html -kL -b cookies.txt
http://<server>/confluence/authenticate.action?destination=/admin/search-indexes.action<br< a="">>
curl -o c.html -kL -b cookies.txt -d "password=<password>&authenticate=Confirm"
http://<server>/confluence/doauthenticate.action?destination=/admin/search-indexes.action<br< a="">>
#works till the above curl.

atl_token=`cat c.html | grep -i atlassian-token | cut -d'=' -f4 | tr -d ">" | tr -d "\""`

echo ${atl_token}

curl -o d.html -kL -b cookies.txt -d "atl_token=${atl_token}&submit=Build"
http://<server>/confluence/admin/reindex.action<br< a="">>
rm cookies.txt

"

What could be the issue with the last curl to call reindex.action ? We tried with referer

curl -o d.html -kL -b cookies.txt -d "atl_token=${atl_token}&submit=Build" -e http://<server> /confluence/admin/search-indexes.action http://<server>/confluence/admin/reindex.action

and also this redirect :

curl -v -o d.html -kL -b cookies.txt -d "password=<password>" http://<server>/confluence/doauthenticate.action?destination=/admin/reindex.action

But none worked.

Thanks

Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

Vineetha, were you ever able to figure out a solution? I am also stuck where you were at.

1 vote
Vineetha April 15, 2014

Hi,

With confluence, we automated the re-indexing by passing the ds_01a and myacinfo cookies, plus atlassian token. The below script worked for us.

##Variable declarations

USERNAME=&lt;username&gt;

PASSWORD=&lt;password&gt;

COOKIE_DS01_A=&lt;value&gt; # got from inspect

COOKIE_MYACINFO=&lt;value&gt; # got from inspect

LOGIN_PAGE_URL=https://&lt;server&gt;/confluence/dologin.action

ADMIN_AUTHENTICATE_PAGE_URL=http://&lt;server&gt;/confluence/authenticate.action?destination=/admin/search-indexes.action

ADMIN_DOAUTHENTICATE_PAGE_URL=http://&lt;server&gt;/confluence/doauthenticate.action?destination=/admin/search-indexes.action

REINDEX_PAGE_URL=http://&lt;server&gt;/confluence/admin/reindex.action

 

##Commands

curl -k  -c cookies.txt  -d "os_username=${USERNAME}&amp;os_password=${PASSWORD}&amp;os_destination=/dashboard.action&amp;login=Log In" $LOGIN_PAGE_URL

 

curl -o b.html -kL -b cookies.txt  -c cookies.txt   $ADMIN_AUTHENTICATE_PAGE_URL

 

curl -o c.html -kL  -b cookies.txt  -c cookies.txt  -d "password=${PASSWORD}&amp;authenticate=Confirm"    $ADMIN_DOAUTHENTICATE_PAGE_URL

 

atl_token=`cat c.html | grep -i atlassian-token | cut -d'=' -f4 | tr -d "&gt;"  | tr -d "\""`

 

echo ${atl_token}

 

isRebuild=`grep build-search-index-button c.html | grep -i rebuild`

isRebuildTaskInProgress=`grep reindex-task-in-progress c.html`

 

if [ -z "$isRebuildTaskInProgress" ] 

then

if [ -n "$isRebuild" ] 

then

echo "Rebuilding indexes"

curl -o d.html -kL -b cookies.txt -b "ds01_a=${COOKIE_DS01_A}; myacinfo=${COOKIE_MYACINFO}"   -d "atl_token=${atl_token}&amp;submit=Rebuild"  $REINDEX_PAGE_URL

else

echo "Building indexes"

curl -o d.html -kL -b cookies.txt    -d "atl_token=${atl_token}&amp;submit=Build"  $REINDEX_PAGE_URL

fi

fi

rm cookies.txt *.html


Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

Thanks! But would you mind putting your code into a code block? :)

Vineetha April 15, 2014

Code block it is :)

Like li michael likes this
Abdul Rehman February 25, 2019

Thanks Vineetha

1 vote
Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2012

Not sure with curl. However, renderRequest action from JIRA Command Line Interface will do a websudo for you automatically if you are accessing a /secure url.

0 votes
brianashe October 24, 2019

I came here in 2019 and was able to use this page to help me make a script that will download the database graphs from /secure/admin/monitor_database.jspa. The script is running on macOS High Sierra (10.13) and our Jira server is 7.13.1.

#!/bin/sh

curl "https://YOUR.JIRA.SERVER/jira/login.jsp?os_username=YOUR_USERNAME_HERE&os_password=YOUR_PASSWORD_HERE" -o /dev/null -c cookies.txt

token=`tail -1 cookies.txt | tr -s "\t" " " | cut -d" " -f7`

curl --header "X-Atlassian-Token: no-check" -kL -b cookies.txt -d "webSudoPassword=YOUR_PASSWORD_HERE&amp;webSudoDestination=%2fsecure%2fadmin%2fmonitor_database.jspa&amp;atl_token=${token}" "https://YOUR.JIRA.SERVER/jira/secure/admin/WebSudoAuthenticate.jspa?webSudoDestination=%2fsecure%2fadmin%2fmonitor_database.jspa" | grep "data&colon;image/png;base64" > db.html

The result is an HTML page with the two images embedded in it.

You can change db.html to `date +%Y-%m-%d_%H-%M-%S`.html to create a timestamped file and then put this script into cron to save the graphs periodically.

0 votes
Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 15, 2014

I wasn't able to get Eddie's sample code to work properly. However, I did arrive at a similar solution, located here: https://answers.atlassian.com/questions/284007/how-to-call-canned-builtin-script-runner-scripts-from-a-script?page=1

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events