Skip to content

Commit

Permalink
Fix exception creating key file under Python3
Browse files Browse the repository at this point in the history
This should fix #55
Also, remove stale dependencies for Python 2.  Hey all, if you haven't
already, you may want to backup your octoprint settings (recent versions
have a feature for that) and make a new OctoPi 0.18 image for your pi.
It'll move you to Python 3 and give you a fresh OS too.
  • Loading branch information
markwal committed Jan 28, 2021
1 parent 1f75d4d commit 58d96a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
30 changes: 21 additions & 9 deletions octoprint_polarcloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,26 +302,36 @@ def _system(self, command_line):
self._logger.exception("Failed to run system command: {}".format(command_line))
return (1, "")


def _get_keys(self):
data_folder = self.get_plugin_data_folder()
key_filename = os.path.join(data_folder, 'p3d_key')
self._logger.debug('key_filename: {}'.format(key_filename))
if not os.path.isfile(key_filename):
self._logger.debug('Generating key pair')
def _generate_key(self, key_filename):
try:
self._logger.info('Generating key pair')
key = crypto.PKey()
key.generate_key(crypto.TYPE_RSA, 2048)
with open(key_filename, 'w') as f:
with open(key_filename, 'wb') as f:
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
if sys.platform != 'win32':
os.chmod(key_filename, stat.S_IRUSR | stat.S_IWUSR)
except:
self._logger.exception("Unable to generate and save new private key")

def _get_keys(self, force_regen = False):
data_folder = self.get_plugin_data_folder()
key_filename = os.path.join(data_folder, 'p3d_key')
self._logger.debug('key_filename: {}'.format(key_filename))
if force_regen or not os.path.isfile(key_filename):
self._generate_key(key_filename)
try:
with open(key_filename) as f:
key = f.read()
if force_regen and len(key) <= 0:
self._logger.warn("Found zero length key, generating a new key")
self._generate_key(key_filename)
with open(key_filename) as f:
key = f.read()
self._key = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
except:
self._key = None
self._logger.error("Unable to generate or access key.")
self._logger.exception("Unable to generate or access key.")
return

if hasattr(self._key, 'dump_publickey'):
Expand Down Expand Up @@ -795,6 +805,8 @@ def _on_register_response(self, response, *args, **kwargs):

def _register(self, email, pin):
self._get_keys()
if not self._key:
self._get_keys(True)
if not self._key:
self._logger.info("Can't register because unable to generate signing key")
self._plugin_manager.send_plugin_message(self._identifier, {
Expand Down
11 changes: 1 addition & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
plugin_identifier = "polarcloud"
plugin_package = "octoprint_polarcloud"
plugin_name = "OctoPrint-PolarCloud"
plugin_version = "1.12"
plugin_version = "1.13"
plugin_description = """Connects OctoPrint to the PolarCloud so you can easily monitor and control outside of your local network"""
plugin_author = "Mark Walker"
plugin_author_email = "markwal@hotmail.com"
Expand Down Expand Up @@ -61,15 +61,6 @@

if int(setuptools.__version__.split('.')[0]) < 40:
print("May not be able to successfully install with setuptools earlier than 40.0.0. If this fails, upgrade setup tools with 'pip install --upgrade setuptools'.")
else:
plat = distutils.util.get_platform().replace('.', '_').replace('-', '_')
if sys.version_info[0:2] == (2, 7) and plat in ['linux_armv7l', 'linux_armv6l'] and not hasattr(sys, 'pypy_version_info'):
plugin_requires = [
"cryptography @ https://markwal.github.io/wheelhouse/cryptography-3.0-cp27-none-" + plat + ".whl",
"cffi @ https://markwal.github.io/wheelhouse/cffi-1.12.1-cp27-none-" + plat + ".whl",
"Pillow @ https://markwal.github.io/wheelhouse/Pillow-5.4.1-cp27-none-" + plat + ".whl",
] + plugin_requires
add_pillow = False
except:
import traceback
traceback.print_exc()
Expand Down

0 comments on commit 58d96a4

Please sign in to comment.