Unwanted [forward] slash in get_info results 404 error in case server url looks like "http://domainname[:port]/myjenkins"

Bug #1501441 reported by eil397
48
This bug affects 9 people
Affects Status Importance Assigned to Milestone
Python Jenkins
Confirmed
Undecided
Unassigned

Bug Description

Function get_info with default value of parameter item uses wrong url to send request to Jenkins server in case server url looks like "http://domainname[:port]/myjenkins"

source code of python-jenkins/jenkins/__init__.py:

 437 def get_info(self, item="", query=None):
.....
.....
 456 url = "/".join((item, INFO))
 457 if query:
 458 url += query
 459 try:
 460 return json.loads(self.jenkins_open(
 461 Request(self._build_url(url))

with INFO='api/json' and item="" works like this:
>>> item = ""
>>> url = "/".join((item, 'api/json'))
>>> print url
/api/json

As a result function _build_url produces wrong url with self.server="http://192.168.0.23:8081/jenkins/" and query="?tree=jobs[url,color,name,jobs]":
>>> from six.moves.urllib.parse import quote, urlencode, urljoin, urlparse
>>> urljoin(self.server, "/api/json?tree=jobs[url,color,name,jobs]")
'http://192.168.0.23:8081/api/json?tree=jobs[url,color,name,jobs]'
>>> print self.server
http://192.168.0.23:8081/jenkins/
>>> urljoin(self.server, "api/json?tree=jobs[url,color,name,jobs]")
'http://192.168.0.23:8081/jenkins/api/json?tree=jobs[url,color,name,jobs]'

Functions get_jobs and get_all_jobs are affected by this issue.

>>>import jenkins
>>>server = jenkins.Jenkins('http://xx.xx.0.23:8081/jenkins/', username='jenkins', password='jenkins')
>>>version = server.get_version()
>>> print version
1.565.3
>>> jobs = server.get_jobs()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 625, in get_jobs
    return self.get_all_jobs(folder_depth=folder_depth)
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 674, in get_all_jobs
    jobs = [(0, "", self.get_info(query=JOBS_QUERY)['jobs'])]
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 461, in get_info
    Request(self._build_url(url))
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 361, in jenkins_open
    raise NotFoundException('Requested item could not be found')
jenkins.NotFoundException: Requested item could not be found
>>> info = server.get_info(query="?tree=jobs[url,color,name,jobs]")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 461, in get_info
    Request(self._build_url(url))
  File "/usr/local/lib/python2.7/site-packages/jenkins/__init__.py", line 361, in jenkins_open
    raise NotFoundException('Requested item could not be found')
jenkins.NotFoundException: Requested item could not be found

Jenkins is accessible by using "http://192.168.0.23:8081/jenkins/" url and credentials "jenkins/jenkins"
As I can see in access logs the 404 code was returned because of wrong address in request :
xx.xx.0.24 - - [29/Sep/2015:17:58:00 +0000] "GET /api/json?tree=jobs[url,color,name,jobs] HTTP/1.1" 404 967
xx.xx.0.24 - - [29/Sep/2015:18:05:33 +0000] "GET /api/json?tree=jobs[url,color,name,jobs] HTTP/1.1" 404 967

It should send request to "GET /jenkins/api/json?tree=jobs[url,color,name,jobs]" but it sends "GET /api/json?tree=jobs[url,color,name,jobs]"

Jenkins is working properly. It was checked by wget --auth-no-challenge --http-user=jenkins --http-password=jenkins http://192.168.0.23:8081/jenkins/api/json?tree=jobs[url,color,name,jobs]
Record from access log:
xx.xx.0.24 - - [29/Sep/2015:18:06:09 +0000] "GET /jenkins/api/json?tree=jobs[url,color,name,jobs] HTTP/1.0" 200 188

I've tested fix and would like to push it back to project:
-bash-4.1$ git diff
diff --git a/jenkins/__init__.py b/jenkins/__init__.py
index ca46fd7..4677bdb 100644
--- a/jenkins/__init__.py
+++ b/jenkins/__init__.py
@@ -453,7 +453,10 @@ class Jenkins(object):
             u'name': u'my_job'}

         """
- url = "/".join((item, INFO))
+ if item != '':
+ url = "/".join((item, INFO))
+ else:
+ url = INFO
         if query:
             url += query
         try:

This fix should not broke functionality "Adds Cloudbees folder plugin support" https://github.com/stackforge/python-jenkins/commit/7267eec4542696b48fea6a07ccdf34c6e887f75f

Project "openstack-infra/jenkins-job-builder" is affected by this issue.

Revision history for this message
Jordan Pittier (jordan-pittier) wrote :

I have the same issue.

My jenkins is at http://ci.localdomain:8081/jenkins/

I got the same stack trace and I confirm that the patch proposed here worked for me.

Changed in python-jenkins:
status: New → Confirmed
eil397 (anton-haldin)
Changed in python-jenkins:
assignee: nobody → Anton Haldin (anton-haldin)
Revision history for this message
eil397 (anton-haldin) wrote :
Revision history for this message
eil397 (anton-haldin) wrote :

bug fix for this regression was already proposed - https://review.openstack.org/#/c/228971/
and with related changes in tests: https://review.openstack.org/#/c/231109/

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Change abandoned on python-jenkins (master)

Change abandoned by Anton Haldin (<email address hidden>) on branch: master
Review: https://review.openstack.org/237916
Reason: bug fix for this regression was already proposed - https://review.openstack.org/#/c/228971/
and with related changes in tests: https://review.openstack.org/#/c/231109/

eil397 (anton-haldin)
Changed in python-jenkins:
assignee: Anton Haldin (anton-haldin) → nobody
Revision history for this message
Peter Eisentraut (petere) wrote :

While the fix is being discussed, I have found that downgrading to 0.4.8 gets past this issue.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.