gurumitts/pylutron-caseta

async_pair: [Errno13] Permission Denied

Closed this issue · 3 comments

Problem

Calling await async_pair(SERVER_ADDRESS) always returns a permission error regardless of the server address validity. However using get_lutron_cert.py with the same address works perfectly fine.

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 446, in result
    return self.__get_result()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 391, in __get_result
    raise self._exception
  File "<console>", line 1, in <module>
  File "C:\Users\TGN-PC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pylutron_caseta\pairing.py", line 63, in async_pair
    csr, key_bytes_pem, ssl_context = await loop.run_in_executor(
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\TGN-PC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pylutron_caseta\pairing.py", line 205, in _generate_csr_with_ssl_context
    ssl_context.load_cert_chain(lap_cert_temp_file.name, lap_key_temp_file.name)
PermissionError: [Errno 13] Permission denied

Steps to reproduce

  1. Open python REPL with asyncio module
$ python -m asyncio
  1. Import async_pair
>>> from pylutron_caseta.pairing import async_pair
  1. Call async_pair with a valid or invalid server address
>>> await async_pair("192.168.1.255")

Information

OS: Windows 10 Pro
OS Version: 21H1
Python: 3.10.2

Yeah I just noticed this a few days ago. async_pair does not work on Windows because it uses NamedTempFile to work around limitations of Python's very basic SSL implementation and NamedTempFile is practically useless on Windows.

Yeah I just noticed this a few days ago. async_pair does not work on Windows because it uses NamedTempFile to work around limitations of Python's very basic SSL implementation and NamedTempFile is practically useless on Windows.

Ah, I'm fairly new to Python so I'm not very familiar with it's ins and outs, but is there any specific reason to use NamedTemporaryFile as opposed to just manually writing and deleting a file given it's problems with Windows? If not I'd be happy to submit a PR

NamedTemporaryFile is basically just a context manager that automatically generates a temporary file and deletes the file at the end without having to write the try+finally. On non-Windows systems, it's much easier to use than doing it by hand, but on Windows because the filesystem doesn't work the same way you end up not being able to use the temporary file unless you set delete=false, in which case you need to delete it yourself when you're done.