alexdlaird / pyngrok

A Python wrapper for ngrok
https://pyngrok.readthedocs.io
MIT License
421 stars 59 forks source link

Permission Error During `ngrok` Install #48

Closed alexdlaird closed 4 years ago

alexdlaird commented 4 years ago

Describe the Bug When using OS X's system Python (possibly other root-owned interpreters as well), a permission error is seen after the ngrok binary is downloaded and installation is attempted.

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pyngrok/installer.py", line 101, in install_ngrok
    _install_ngrok_zip(ngrok_path, download_path)
  File "/usr/local/lib/python3.7/site-packages/pyngrok/installer.py", line 117, in _install_ngrok_zip
    zip_ref.extractall(os.path.dirname(ngrok_path))
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1636, in extractall
    self._extract_member(zipinfo, path, pwd)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1690, in _extract_member
    open(targetpath, "wb") as target:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.7/site-packages/pyngrok/bin/ngrok'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/ngrok", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/pyngrok/ngrok.py", line 338, in main
    run(sys.argv[1:])
  File "/usr/local/lib/python3.7/site-packages/pyngrok/ngrok.py", line 329, in run
    ensure_ngrok_installed(conf.DEFAULT_NGROK_PATH)
  File "/usr/local/lib/python3.7/site-packages/pyngrok/ngrok.py", line 82, in ensure_ngrok_installed
    install_ngrok(ngrok_path)
  File "/usr/local/lib/python3.7/site-packages/pyngrok/installer.py", line 103, in install_ngrok
    raise PyngrokNgrokInstallError("An error occurred while downloading ngrok from {}: {}".format(url, e))
pyngrok.exception.PyngrokNgrokInstallError: An error occurred while downloading ngrok from https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip: [Errno 13] Permission denied: '/usr/local/lib/python3.7/site-packages/pyngrok/bin/ngrok'

This is because /usr/local/lib/python3.7/site-packages/ is owned by root and we're not executing with sudo.

Steps to Reproduce Install pyngrok using the system's Python / pip:

pip install pyngrok

Then try to execute any ngrok command:

ngrok --help

Typically the first thing you'd see here is ngrok being downloaded then installed. The download will succeed, but the above exception will be output on the installation step since bin/ngrok in site-packages will be owned by root.

Expected Behavior The installation should complete without an exception.

Environment

alexdlaird commented 4 years ago

After clearing out the site-packages folder for pyngrok, I'm unable to reproduce this issue. In prior testing, I had being doing work validating different means of installing pyngrok, including using sudo when calling pip to install. My guess is a previous iteration of that created a root bin folder in pyngrok's site-packages, and that was where the permissions error came from. However, I'm unable to get that folder to be generated as root again, so doesn't appear to be an ongoing issue.

If it is seen, the workaround is to chown the bin folder to be owned by you, or execute sudo ngrok the first time (thereafter ngrok will be installed and sudo is no longer needed), or simply clear out your pyngrok installation and start fresh.