ox-it / requests-negotiate

GSSAPI auth support for Python's requests library
BSD 3-Clause "New" or "Revised" License
4 stars 4 forks source link

Improve server_name handling #9

Closed dolfinus closed 3 years ago

dolfinus commented 3 years ago

In some cases an error is raised while trying to determine hostname by IP address:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/venv/lib64/python3.6/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/user/venv/lib64/python3.6/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/user/venv/lib64/python3.6/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/user/venv/lib64/python3.6/site-packages/requests/sessions.py", line 662, in send
    r = dispatch_hook('response', hooks, r, **kwargs)
  File "/home/user/venv/lib64/python3.6/site-packages/requests/hooks.py", line 31, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "/home/user/venv/lib64/python3.6/site-packages/requests_negotiate/__init__.py", line 79, in handle_401
    host = self.get_hostname(response)
  File "/home/user/venv/lib64/python3.6/site-packages/requests_negotiate/__init__.py", line 42, in get_hostname
    return socket.gethostbyaddr(sock.getpeername()[0])[0]
socket.herror: [Errno 0] Resolver Error 0 (no error)

gethostbyaddr returns a hostname only if DNS PTR record is properly set. But it can be not set, and this is quite frequent error (https://mail.python.org/pipermail/tutor/2015-July/106010.html, https://lists.isc.org/pipermail/bind-users/2003-October/046673.html). It such a case user will face with an exception.

Even if service_name (in HTTP@host format) is already passed, user will still see an exception because hostname is still being checked, but there is no need to do that.

In this PR I've made few small changes:

dolfinus commented 3 years ago

@alexsdutton Could you please take a look at this PR?

alexdutton commented 3 years ago

Hi @dolfinus. I can have a look and provide a review, but I no longer work for the University of Oxford IT Services, so someone there would need to merge and release. It might be a week before I get a chance to look at it out of work hours.

dolfinus commented 3 years ago

I've migrated to requests-kerberos