How to get and use Rundeck JSESSIONID

2k views Asked by At

I am trying to get the a valid session id but so far with no success.

I am trying to work with the rundeck API, with password authentication:

To submit authentication, submit a POST request to the URL: $RUNDECK_SERVER_URL/j_security_check

With these parameters: j_username: rundeck username j_password: password

Like the docs suggest, I am running:

>>> import request
>>> jsessionid = requests.post("http://localhost:4440/j_security_check", data=
{"j_username": "admin", "j_password": "admin" }, 
allow_redirects=False).cookies['JSESSIONID']

Resulting the log:

09/04/2017 03:22:59 PM - DEBUG - Starting new HTTP connection (1): 
10.200.101.21
09/04/2017 03:22:59 PM - DEBUG - http://localhost:4440 "POST 
/j_security_check?j_username=admin&j_password=admin HTTP/1.1" 302 0

But the jsessionid is never valid for the next api calls, for example:

>>> response = requests.post("http://localhost:4440/api/14/project/my_project/hi
story" , headers={ "Content-Type": 'application/xml', 'X-Rundeck-
Auth-Token': jsessionid })
>>> response.content
"<result error='true' apiversion='20'><error code='unauthorized'>
<message>(Token:lhefo****) is not authorized for: /api/14/project/weekly_builds/history</message></error></result>"

However, if for jsessionid I am using the token I can see in the Rundeck UI (in http://localhost:4440/user/profile), then the API call works good and I get the history.

Any ideas of what I am doing wrong? Or what I am missing?

Thanks.

1

There are 1 answers

0
Yang On BEST ANSWER

Don't worry about the JSESSIONID. You should use requests.Seesion()

import requests
s = requests.Session()
r = s.post("URL:PORT/j_security_check", data={"j_username": "user", "j_password": "pass"})
r.status_code # this should be 200
r.url # this should be rundeck home

r = s.get("URL:PORT/api/11/projects",headers = {'Accept': 'application/json'})
r.json() # you should see all your project