nickovs / unificontrol

A high-level Python interface to the Unifi controller software
Apache License 2.0
96 stars 41 forks source link

Certificate pinning error on Windows #7

Closed timbersson closed 4 years ago

timbersson commented 4 years ago

On Windows, atttempting to use the api results in an exception for any call:

>>> unifi = UnifiClient(host="unifi",username=UNIFI_USER,password=UNIFI_PASSWORD)
>>> sites = unifi.list_sites()
Traceback (most recent call last):
  File "...\NetworkUtils\lib\site-packages\urllib3\util\ssl_.py", line 336, in ssl_wrap_socket
    context.load_verify_locations(ca_certs, ca_cert_dir)
PermissionError: [Errno 13] Permission denied

The cause of this is that on Windows a NamedTemporaryFile - which is used to store the pinned certificate - cannot be re-opened, and so the permission error is thrown when the ssl library tries to load the pinned certificate.

From python docs here:

Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).

It's possible to work around this by setting delete=False when creating the NamedTemporaryFile, although in that case the temporary file needs to be cleaned up by the code that creates it. I'm happy to provide a PR that does this.

nickovs commented 4 years ago

Thanks for raising this. I don't have a Windows machine on which to test this but if you want to submit a PR I'll take a look.