2012-09-07 268 views
2

我正在嘗試使用jenkinsapi python library與Jenkins一起工作。我的Jenkins設置爲使用LDAP進行身份驗證(使用Active Directory Plugin)。然而,當我運行我的腳本來驗證我失敗(實際上它跑了一次,但自那以後我每次都遇到403 Forbidden錯誤)Jenkins python api身份驗證「403禁止」

它從一開始就失敗:

from jenkinsapi.jenkins import Jenkins 
j = Jenkins("https://qaa.server.com/jenkins", "username", "password") 

Traceback (most recent call last): 
    File "C:\eclipse-SDK-4.2-win32-x86_64\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc\pydevd.py", line 1392, in <module> 
    debugger.run(setup['file'], None, None) 
    File "C:\eclipse-SDK-4.2-win32-x86_64\eclipse\plugins\org.python.pydev_2.6.0.2012062818\pysrc\pydevd.py", line 1085, in run 
    pydev_imports.execfile(file, globals, locals) #execute the script 
    File "C:\work\selenium-project\ses-web\utils\mark_slave_offline.py", line 10, in <module> 
    j = Jenkins("https://qaa.server.com/jenkins", "username", "password") 
    File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkins.py", line 43, in __init__ 
    JenkinsBase.__init__(self, baseurl, formauth=formauth) 
    File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 34, in __init__ 
    self.poll() 
    File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 41, in poll 
    self._data = self._poll() 
    File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\jenkinsbase.py", line 45, in _poll 
    return retry_function(self.RETRY_ATTEMPTS , self.get_data, url) 
    File "C:\Python27\lib\site-packages\jenkinsapi-0.1.11-py2.7.egg\jenkinsapi\utils\retry.py", line 39, in retry_function 
    raise e 
urllib2.HTTPError: HTTP Error 403: Forbidden 

有什麼有趣的是, 下面的代碼只是正常每次

def urlopen(url,username=None, password=None, data=None): 
    '''Open a URL using the urllib2 opener.''' 
    request = urllib2.Request(url, data) 
    base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') 
    request.add_header("Authorization", "Basic %s" % base64string) 
    response = urllib2.urlopen(request) 
    return response 

result = urlopen('https://qaa.server.com/jenkins', "username", "password") 

問題:爲什麼詹金斯代碼失敗?它可能是Apache服務器配置錯誤?

謝謝!

UPDATE:這是我得到的性反應

<html> 
    <head> 
     <meta http-equiv='refresh' content='1;url=/jenkins/loginfrom=%2Fjenkins%2Fapi%2Fpython%2F'/> 
     <script>window.location.replace('/jenkins/login?from=%2Fjenkins%2Fapi%2Fpython%2F'); 
     </script> 
    </head> 
<body style='background-color:white; color:white;'>Authentication required</body> 
</html>  

的Apache的access.log:- [07/Sep/2012:15:24:21 -0400] "GET /jenkins/api/python/ HTTP/1.1" 403 593

UPDATE:error.log中處於調試模式:

..... 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1897): OpenSSL: read 160/160 bytes from BIO#e08150 [mem: e96ea5] (BIO dump follows) 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1830): +-------------------------------------------------------------------------+ 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0000: 64 c2 ac 70 d0 d6 22 02-ef 56 98 32 6e 16 20 53 d..p.."..V.2n. S | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0010: 84 fb 94 c7 51 a4 a8 9c-b3 8f 48 c8 84 1e 46 6b ....Q.....H...Fk | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0020: 15 c9 f4 cb ef cf 38 ed-a1 8b 41 08 a6 3b 95 f0 ......8...A..;.. | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0030: bd b3 05 ef 68 68 e0 fb-52 92 ad fc 7f aa 72 50 ....hh..R.....rP | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0040: 9e d9 7d 98 18 82 eb 07-bb 45 e3 e5 b2 93 6d 34 ..}......E....m4 | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0050: fe 18 b5 d4 5c c9 78 c9-78 0c 6b cb 50 80 96 6d ....\\.x.x.k.P..m | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0060: 49 8a 39 b2 73 b2 ae 76-a4 d3 50 7a 38 a7 54 72 I.9.s..v..Pz8.Tr | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0070: 41 e5 13 b2 1f 71 a1 b0-db 89 81 4f 14 3a 93 18 A....q.....O.:.. | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0080: 5a e0 0e 9e c9 d9 1e f3-01 53 eb 08 dc 23 89 fd Z........S...#.. | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1869): | 0090: a0 13 81 ac 26 55 54 fd-22 64 7e 37 f3 cd 2d af ....&UT."d~7..-. | 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_io.c(1875): +-------------------------------------------------------------------------+ 
[Fri Sep 07 16:52:09 2012] [info] Initial (No.1) HTTPS request received for child 63 (server qaa.server.com:443) 
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(56): proxy: HTTP: canonicalising URL //localhost:8180/jenkins/api/python/ 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(1506): [client 207.***.***.1*2] proxy: http: found worker http://localhost:8180/jenkins for http://localhost:8180/jenkins/api/python/ 
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy.c(1015): Running scheme http handler (attempt 0) 
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1973): proxy: HTTP: serving URL http://localhost:8180/jenkins/api/python/ 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2011): proxy: HTTP: has acquired connection for (localhost) 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2067): proxy: connecting http://localhost:8180/jenkins/api/python/ to localhost:8180 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2193): proxy: connected /jenkins/api/python/ to localhost:8180 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2444): proxy: HTTP: fam 2 socket created to connect to localhost 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2576): proxy: HTTP: connection complete to 127.0.0.1:8180 (localhost) 
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1743): proxy: start body send 
[Fri Sep 07 16:52:09 2012] [debug] mod_proxy_http.c(1847): proxy: end body send 
[Fri Sep 07 16:52:09 2012] [debug] proxy_util.c(2029): proxy: HTTP: has released connection for (localhost) 
[Fri Sep 07 16:52:09 2012] [debug] ssl_engine_kernel.c(1884): OpenSSL: Write: SSL negotiation finished successfully 
[Fri Sep 07 16:52:09 2012] [info] [client 207.***.***.1*2] Connection closed to child 63 with standard shutdown (server qaa.server.com:443) 
+0

什麼是錯誤的根源:Apache的,LDAP或詹金斯?你檢查過日誌嗎? – stark

+0

@stark我已經更新了我的迴應。所以看起來認證甚至沒有嘗試過? –

+0

Apache錯誤日誌裏有什麼? – stark

回答

9

解決此問題的方法是使用不同的python-jenkins module

有了這個模塊,一切工作正常:

import jenkins 
j = jenkins.Jenkins(base_url, username, password) 
+0

非常感謝這樣的快速響應,亞歷克斯。 – KomodoDave

+0

@KomodoDave,沒問題。 jenkinsapi模塊及其身份驗證的深層問題似乎在於他們如何使用auth頭創建請求。但是我更願意研究這個問題。 –

+0

完全可以理解。既然從http:// yourjenkins/api/json簡單地解碼json是如此的微不足道,我決定這麼做。這也使得腳本沒有依賴關係,這是一件好事。但我想了解我是否做了一件愚蠢的事情,因此問你是如何解決問題的。再次感謝。 – KomodoDave