From 40082456c1a9bad8a52b393313af3d1469cf45b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 23 Feb 2018 16:57:46 +0100 Subject: [PATCH] Don't even import disabled plugins We want to avoid disabled plugins interfering with our application in any way at all. Hence disabled plugins are now treated the same way as blacklisted ones. The system will only create a dummy entry for them and not import them at all. As a consequence, a restart of the platform will be necessary for disabling plugins that were disabled. The Plugin Manager has been adjusted accordingly. Note that by no longer even importing the base file, plugin name, version, description and other such metadata defined through control properties won't be accessible. Closes #2442 --- src/octoprint/__init__.py | 7 +- src/octoprint/plugin/core.py | 69 +++++++++++-------- .../plugins/pluginmanager/__init__.py | 21 +++--- .../pluginmanager/static/js/pluginmanager.js | 9 +-- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index f04a963f1..c67c05280 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -322,12 +322,9 @@ def init_pluginsystem(settings, safe_mode=False, ignore_blacklist=True, connecti plugin_validators = [] if safe_mode: def validator(phase, plugin_info): - if phase == "after_load": + if phase in ("before_import", "before_load", "before_enable"): setattr(plugin_info, "safe_mode_victim", not plugin_info.bundled) - setattr(plugin_info, "safe_mode_enabled", False) - elif phase == "before_enable": if not plugin_info.bundled: - setattr(plugin_info, "safe_mode_enabled", True) return False return True plugin_validators.append(validator) @@ -440,7 +437,7 @@ def get_plugin_blacklist(settings, connectivity_checker=None): result.append((entry["plugin"], version)) else: logger.debug("Blacklisted plugin: {}".format(entry["plugin"])) - result.append(entry["key"]) + result.append(entry["plugin"]) return result diff --git a/src/octoprint/plugin/core.py b/src/octoprint/plugin/core.py index 2cce7fe1a..1ceffcc36 100644 --- a/src/octoprint/plugin/core.py +++ b/src/octoprint/plugin/core.py @@ -144,6 +144,7 @@ class PluginInfo(object): self.origin = None self.enabled = True self.blacklisted = False + self.forced_disabled = False self.bundled = False self.loaded = False self.managable = True @@ -159,7 +160,10 @@ class PluginInfo(object): def validate(self, phase, additional_validators=None): result = True - if phase == "before_load": + if phase == "before_import": + result = not self.forced_disabled and not self.blacklisted and result + + elif phase == "before_load": # if the plugin still uses __plugin_init__, log a deprecation warning and move it to __plugin_load__ if hasattr(self.instance, self.__class__.attr_init): if not hasattr(self.instance, self.__class__.attr_load): @@ -193,7 +197,7 @@ class PluginInfo(object): if additional_validators is not None: for validator in additional_validators: - result = result and validator(phase, self) + result = validator(phase, self) and result return result @@ -602,14 +606,13 @@ class PluginManager(object): # plugin is already defined, ignore it continue - plugin = self._import_plugin_from_module(key, folder=folder) + bundled = flagged_readonly + + plugin = self._import_plugin_from_module(key, folder=folder, bundled=bundled) if plugin: plugin.origin = FolderOrigin("folder", folder) plugin.managable = not flagged_readonly and not actual_readonly - plugin.bundled = flagged_readonly - plugin.enabled = False - result[key] = plugin except: self.logger.exception("Error processing folder entry {!r} from folder {}".format(entry, folder)) @@ -676,6 +679,7 @@ class PluginManager(object): plugin = self._import_plugin_from_module(key, **kwargs) if plugin: plugin.origin = EntryPointOrigin("entry_point", group, module_name, package_name, version) + plugin.enabled = False # plugin is manageable if its location is writable and OctoPrint # is either not running from a virtual env or the plugin is @@ -685,19 +689,19 @@ class PluginManager(object): plugin.managable = os.access(plugin.location, os.W_OK) \ and (not self._python_virtual_env or is_sub_path_of(plugin.location, self._python_prefix) - or is_editable_install(self._python_install_dir, - package_name, - module_name, - plugin.location)) + or is_editable_install(self._python_install_dir, + package_name, + module_name, + plugin.location)) - plugin.enabled = False result[key] = plugin except: self.logger.exception("Error processing entry point {!r} for group {}".format(entry_point, group)) return result - def _import_plugin_from_module(self, key, folder=None, module_name=None, name=None, version=None, summary=None, author=None, url=None, license=None): + def _import_plugin_from_module(self, key, folder=None, module_name=None, name=None, version=None, summary=None, + author=None, url=None, license=None, bundled=False): # TODO error handling try: if folder: @@ -710,14 +714,34 @@ class PluginManager(object): self.logger.warn("Could not locate plugin {key}".format(key=key)) return None + # Create a simple dummy entry first ... + plugin = PluginInfo(key, module[1], None, name=name, version=version, description=summary, author=author, + url=url, license=license) + plugin.bundled = bundled + + if self._is_plugin_disabled(key): + self.logger.info("Plugin {} is disabled.".format(plugin)) + plugin.forced_disabled = True + if self._is_plugin_blacklisted(key) or (version is not None and self._is_plugin_version_blacklisted(key, version)): - plugin = PluginInfo(key, module[1], None, name=name, version=version, description=summary, author=author, url=url, license=license) + self.logger.warn("Plugin {} is blacklisted.".format(plugin)) plugin.blacklisted = True - self.logger.warn("Plugin {} is blacklisted. Not importing it, only registering a dummy entry.".format(plugin)) + + if not plugin.validate("before_import", additional_validators=self.plugin_validators): return plugin - plugin = self._import_plugin(key, *module, name=name, version=version, summary=summary, author=author, url=url, license=license) - if plugin is None: + # ... then create and return the real one + return self._import_plugin(key, *module, + name=name, version=version, summary=summary, author=author, url=url, + license=license, bundled=bundled) + + def _import_plugin(self, key, f, filename, description, name=None, version=None, summary=None, author=None, url=None, license=None, bundled=False): + try: + instance = imp.load_module(key, f, filename, description) + plugin = PluginInfo(key, filename, instance, name=name, version=version, description=summary, author=author, url=url, license=license) + plugin.bundled = bundled + except: + self.logger.exception("Error loading plugin {key}".format(key=key)) return None if plugin.check(): @@ -726,15 +750,6 @@ class PluginManager(object): self.logger.warn("Plugin \"{plugin}\" did not pass check".format(plugin=str(plugin))) return None - - def _import_plugin(self, key, f, filename, description, name=None, version=None, summary=None, author=None, url=None, license=None): - try: - instance = imp.load_module(key, f, filename, description) - return PluginInfo(key, filename, instance, name=name, version=version, description=summary, author=author, url=url, license=license) - except: - self.logger.exception("Error loading plugin {key}".format(key=key)) - return None - def _is_plugin_disabled(self, key): return key in self.plugin_disabled_list or key.endswith('disabled') @@ -765,7 +780,7 @@ class PluginManager(object): # 1st pass: loading the plugins for name, plugin in plugins.items(): try: - if not plugin.blacklisted: + if not plugin.blacklisted and not plugin.forced_disabled: self.load_plugin(name, plugin, startup=startup, initialize_implementation=initialize_implementations) except PluginNeedsRestart: pass @@ -779,7 +794,7 @@ class PluginManager(object): # 2nd pass: enabling those plugins that need enabling for name, plugin in plugins.items(): try: - if plugin.loaded and not self._is_plugin_disabled(name): + if plugin.loaded and not plugin.forced_disabled: if plugin.blacklisted: self.logger.warn("Plugin {} is blacklisted. Not enabling it.".format(plugin)) continue diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 2ee271bb4..e15c987f9 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -561,13 +561,14 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, if plugin.key == "pluginmanager": return make_response("Can't enable/disable Plugin Manager", 400) + pending = ((command == "disable" and plugin.key in self._pending_enable) or (command == "enable" and plugin.key in self._pending_disable)) + safe_mode_victim = getattr(plugin, "safe_mode_victim", False) + needs_restart = self._plugin_manager.is_restart_needing_plugin(plugin) needs_refresh = plugin.implementation and isinstance(plugin.implementation, octoprint.plugin.ReloadNeedingPlugin) needs_reconnect = self._plugin_manager.has_any_of_hooks(plugin, self._reconnect_hooks) and self._printer.is_operational() - pending = ((command == "disable" and plugin.key in self._pending_enable) or (command == "enable" and plugin.key in self._pending_disable)) - safe_mode_victim = getattr(plugin, "safe_mode_victim", False) - needs_restart_api = (needs_restart or safe_mode_victim) and not pending + needs_restart_api = (needs_restart or safe_mode_victim or plugin.forced_disabled) and not pending needs_refresh_api = needs_refresh and not pending needs_reconnect_api = needs_reconnect and not pending @@ -677,12 +678,12 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, self._settings.global_set(["plugins", "_disabled"], disabled_list) self._settings.save(force=True) - if not needs_restart and not getattr(plugin, "safe_mode_victim", False): + if not needs_restart and not plugin.forced_disabled and not getattr(plugin, "safe_mode_victim", False): self._plugin_manager.enable_plugin(plugin.key) else: if plugin.key in self._pending_disable: self._pending_disable.remove(plugin.key) - elif (not plugin.enabled and not getattr(plugin, "safe_mode_enabled", False)) and plugin.key not in self._pending_enable: + elif not plugin.enabled and plugin.key not in self._pending_enable: self._pending_enable.add(plugin.key) def _mark_plugin_disabled(self, plugin, needs_restart=False): @@ -692,12 +693,12 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, self._settings.global_set(["plugins", "_disabled"], disabled_list) self._settings.save(force=True) - if not needs_restart and not getattr(plugin, "safe_mode_victim", False): + if not needs_restart and not plugin.forced_disabled and not getattr(plugin, "safe_mode_victim", False): self._plugin_manager.disable_plugin(plugin.key) else: if plugin.key in self._pending_enable: self._pending_enable.remove(plugin.key) - elif (plugin.enabled or getattr(plugin, "safe_mode_enabled", False)) and plugin.key not in self._pending_disable: + elif (plugin.enabled or plugin.forced_disabled or getattr(plugin, "safe_mode_victim", False)) and plugin.key not in self._pending_disable: self._pending_disable.add(plugin.key) def _fetch_all_data(self, async=False): @@ -920,10 +921,10 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, managable=plugin.managable, enabled=plugin.enabled, blacklisted=plugin.blacklisted, + forced_disabled=plugin.forced_disabled, safe_mode_victim=getattr(plugin, "safe_mode_victim", False), - safe_mode_enabled=getattr(plugin, "safe_mode_enabled", False), - pending_enable=(not plugin.enabled and not getattr(plugin, "safe_mode_enabled", False) and plugin.key in self._pending_enable), - pending_disable=((plugin.enabled or getattr(plugin, "safe_mode_enabled", False)) and plugin.key in self._pending_disable), + pending_enable=(not plugin.enabled and not getattr(plugin, "safe_mode_victim", False) and plugin.key in self._pending_enable), + pending_disable=((plugin.enabled or getattr(plugin, "safe_mode_victim", False)) and plugin.key in self._pending_disable), pending_install=(self._plugin_manager.is_plugin_marked(plugin.key, "installed")), pending_uninstall=(self._plugin_manager.is_plugin_marked(plugin.key, "uninstalled")), origin=plugin.origin.type, diff --git a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js index d658aa1d0..19842dcb6 100644 --- a/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js +++ b/src/octoprint/plugins/pluginmanager/static/js/pluginmanager.js @@ -222,7 +222,7 @@ $(function() { self.enableToggle = function(data) { var command = self._getToggleCommand(data); - var not_safemode_victim = !data.safe_mode_victim || data.safe_mode_enabled; + var not_safemode_victim = !data.safe_mode_victim; var not_blacklisted = !data.blacklisted; return self.enableManagement() && (command == "disable" || (not_safemode_victim && not_blacklisted)) && data.key != 'pluginmanager'; }; @@ -462,7 +462,7 @@ $(function() { }; if (self._getToggleCommand(data) == "enable") { - if (data.safe_mode_victim && !data.safe_mode_enabled) return; + if (data.safe_mode_victim) return; OctoPrint.plugins.pluginmanager.enable(data.key) .done(onSuccess) .fail(onError); @@ -857,7 +857,8 @@ $(function() { }; self._getToggleCommand = function(data) { - var disable = (data.enabled || data.pending_enable || (data.safe_mode_victim && data.safe_mode_enabled)) && !data.pending_disable; + var disable = (data.enabled || (data.safe_mode_victim && !data.forced_disabled) || data.pending_enable) + && !data.pending_disable; return disable ? "disable" : "enable"; }; @@ -873,7 +874,7 @@ $(function() { if (command == "enable") { if (data.blacklisted) { return gettext("Blacklisted"); - } else if (data.safe_mode_victim && !data.safe_mode_enabled) { + } else if (data.safe_mode_victim) { return gettext("Disabled due to active safe mode"); } else { return gettext("Enable Plugin");