Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jenkins version example gives error "TypeError: string indices must be integers" #5

Closed
manox opened this issue Jun 7, 2022 · 10 comments

Comments

@manox
Copy link
Contributor

manox commented Jun 7, 2022

When running the sync Jenkins version example (https://github.com/pbelskiy/ujenkins#usage) I get the following errror. What am i doing wrong?

Traceback (most recent call last):
  File "/root/test.py", line 8, in <module>
    example()
  File "/root/test.py", line 5, in example
    version = client.system.get_version()
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/endpoints/system.py", line 40, in get_version
    return self.jenkins._request('GET', '/', callback=callback)
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/adapters/sync.py", line 173, in _request
    self.crumb = self._get_crumb()
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/adapters/sync.py", line 152, in _get_crumb
    self.crumb = {response['crumbRequestField']: response['crumb']}
TypeError: string indices must be integers
@pbelskiy
Copy link
Owner

pbelskiy commented Jun 7, 2022

Hello! Thanks for bugreport, which version of Jenkins and package are you using?

@manox
Copy link
Contributor Author

manox commented Jun 7, 2022

Jenkins 2.332.3 and ujenkins 0.3.0

@pbelskiy
Copy link
Owner

pbelskiy commented Jun 7, 2022

@manox I've installed same versions using docker, but all is okay. Could you please add print to 152 line of
/usr/local/lib/python3.9/dist-packages/ujenkins/adapters/sync.py and write here output?

response = self._http_request('GET', CRUMB_ISSUER_URL)
print(response)

And to ``/usr/local/lib/python3.9/dist-packages/ujenkins/core.py` in 43 line

print(response.headers)
if 'application/json' in response.headers.get('Content-Type', ''):

It seems somehow Jenkins returns header without json and it's unpacked from str to json.

Where is your Jenkins server, maybe some proxy is using?

@manox
Copy link
Contributor Author

manox commented Jun 8, 2022

First of all, thanks for the help. The Jenkins server is local on the network and I access it via a local IP. I used a Jenkins image that I believe has a reverse proxy built in.
Here is the output with the prints.

{'Date': 'Wed, 08 Jun 2022 09:37:41 GMT', 'Server': 'Apache', 'Location': 'https://192.168.178.73/crumbIssuer/api/json', 'Content-Length': '251', 'Keep-Alive': 'timeout=5, max=100', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=iso-8859-1'}
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://192.168.178.73/crumbIssuer/api/json">here</a>.</p>
</body></html>

Traceback (most recent call last):
  File "/root/test.py", line 8, in <module>
    example()
  File "/root/test.py", line 5, in example
    version = client.system.get_version()
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/endpoints/system.py", line 40, in get_version
    return self.jenkins._request('GET', '/', callback=callback)
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/adapters/sync.py", line 174, in _request
    self.crumb = self._get_crumb()
  File "/usr/local/lib/python3.9/dist-packages/ujenkins/adapters/sync.py", line 153, in _get_crumb
    self.crumb = {response['crumbRequestField']: response['crumb']}
TypeError: string indices must be integers

Edit: I just noticed that it works when attaching the port (8080) to the server address. Problem is therefore solved. Thanks!

@manox manox closed this as completed Jun 9, 2022
@pbelskiy
Copy link
Owner

pbelskiy commented Jun 9, 2022

@manox anyway there is bug in package, it's because of redirect is not processed, could you share please your reverse proxy config of apache?

@pbelskiy pbelskiy reopened this Jun 9, 2022
@manox
Copy link
Contributor Author

manox commented Jun 9, 2022

OK, sure.

root@jenkins ~# cat /etc/apache2/sites-enabled/jenkins.conf 
ServerName localhost

<VirtualHost *:80>
    UseCanonicalName Off
    RewriteEngine on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
</VirtualHost>

<VirtualHost *:443>
    SSLEngine on
    UseCanonicalName Off
    ProxyRequests Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:8080/ nocanon
    ProxyPassReverse / http://localhost:8080/
    RequestHeader set X-Forwarded-Proto "https"
    RequestHeader set X-Forwarded-Port "443"
</VirtualHost>

pbelskiy added a commit that referenced this issue Jun 9, 2022
pbelskiy added a commit that referenced this issue Jun 9, 2022
@pbelskiy
Copy link
Owner

pbelskiy commented Jun 9, 2022

@manox fixed in https://github.com/pbelskiy/ujenkins/releases/tag/v0.3.3 please check it

@manox
Copy link
Contributor Author

manox commented Jun 9, 2022

At first it was not possible to install the package from the sources. I had to add the encoding in ujenkins/setup.py in line 18.

with open('README.rst', encoding='utf-8') as readme_file:

Now when I try to reach the server without specifying the port the following happens. Since it is only a local test setup I have not installed an SSl certificate.

Traceback (most recent call last):
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
    conn.connect()
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\connection.py", line 414, in connect
    self.sock = ssl_wrap_socket(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\util\ssl_.py", line 453, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\util\ssl_.py", line 495, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "C:\Users\marka\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\marka\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1071, in _create
    self.do_handshake()
  File "C:\Users\marka\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1342, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:997)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\adapters.py", line 440, in send
    resp = conn.urlopen(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\urllib3\util\retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='192.168.178.73', port=443): Max retries exceeded with url: /crumbIssuer/api/json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:997)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\marka\jenkins_agent_manager\manager.py", line 14, in <module>
    connect()
  File "c:\Users\marka\jenkins_agent_manager\manager.py", line 5, in connect
    version = client.system.get_version()
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\ujenkins\endpoints\system.py", line 40, in get_version
    return self.jenkins._request('GET', '/', callback=callback)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\ujenkins\adapters\sync.py", line 172, in _request
    self.crumb = self._get_crumb()
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\ujenkins\adapters\sync.py", line 150, in _get_crumb
    response = self._http_request('GET', CRUMB_ISSUER_URL)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\ujenkins\adapters\sync.py", line 134, in _http_request
    response = self.session.request(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\sessions.py", line 667, in send
    history = [resp for resp in gen]
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\sessions.py", line 667, in <listcomp>
    history = [resp for resp in gen]
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\sessions.py", line 237, in resolve_redirects
    resp = self.send(
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\marka\jenkins_agent_manager\.venv\lib\site-packages\requests\adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='192.168.178.73', port=443): Max retries exceeded with url: /crumbIssuer/api/json (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:997)')))

@pbelskiy
Copy link
Owner

pbelskiy commented Jun 9, 2022

Thanks for another issue. About your problem, you can disable ssl check using Jenkins(..., verify=False)

@manox
Copy link
Contributor Author

manox commented Jun 9, 2022

Thanks, it works when disabling ssl checks.

@pbelskiy pbelskiy closed this as completed Jun 9, 2022
pbelskiy added a commit to pbelskiy/aiojenkins that referenced this issue Jun 12, 2022
* enable http redirects by default

pbelskiy/ujenkins#5

* disable restart test

* ignore location existss

* disable test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants