From b94424411a72b183e3be1cc99fa00ac26f98a56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 12 Nov 2020 14:14:26 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Better=20handling=20of=20corrupt?= =?UTF-8?q?ed=20user=20storage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Missing users.yaml should already trigger the wizard to pop up again, but corrupted users.yaml so far made the user manager to mark itself as customized and THEN fall on its face. We now first make sure we can actually load stuff (and if not raise an exception and a critical error in the log), and only then mark the user manager as successfully customized. Implements #3804 --- src/octoprint/access/users.py | 20 ++++++++++++++++++-- src/octoprint/cli/user.py | 11 +++++++++-- src/octoprint/server/__init__.py | 2 ++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/octoprint/access/users.py b/src/octoprint/access/users.py index 5e3115d15..a7340bfed 100644 --- a/src/octoprint/access/users.py +++ b/src/octoprint/access/users.py @@ -502,6 +502,8 @@ class FilebasedUserManager(UserManager): def __init__(self, group_manager, path=None, settings=None): UserManager.__init__(self, group_manager, settings=settings) + self._logger = logging.getLogger(__name__) + if path is None: path = self._settings.get(["accessControl", "userfile"]) if path is None: @@ -517,10 +519,19 @@ class FilebasedUserManager(UserManager): def _load(self): if os.path.exists(self._userfile) and os.path.isfile(self._userfile): - self._customized = True + # noinspection PyBroadException with io.open(self._userfile, "rt", encoding="utf-8") as f: data = yaml.safe_load(f) + if not data or not isinstance(data, dict): + self._logger.fatal( + "{} does not contain a valid map of users. Fix " + "the file, or remove it, then restart OctoPrint.".format( + self._userfile + ) + ) + raise CorruptUserStorage() + for name, attributes in data.items(): if not isinstance(attributes, dict): continue @@ -570,6 +581,7 @@ class FilebasedUserManager(UserManager): if self._dirty: self._save() + self._customized = True else: self._customized = False @@ -1008,10 +1020,14 @@ class UnknownUser(Exception): class UnknownRole(Exception): - def _init_(self, role): + def __init__(self, role): Exception.__init__(self, "Unknown role: %s" % role) +class CorruptUserStorage(Exception): + pass + + ##~~ Refactoring helpers diff --git a/src/octoprint/cli/user.py b/src/octoprint/cli/user.py index 9e9a24f67..548fd88bb 100644 --- a/src/octoprint/cli/user.py +++ b/src/octoprint/cli/user.py @@ -11,7 +11,12 @@ import click from octoprint import init_settings from octoprint.access.groups import FilebasedGroupManager -from octoprint.access.users import FilebasedUserManager, UnknownUser, UserAlreadyExists +from octoprint.access.users import ( + CorruptUserStorage, + FilebasedUserManager, + UnknownUser, + UserAlreadyExists, +) from octoprint.cli import get_ctx_obj_option from octoprint.util import get_class, sv @@ -63,6 +68,8 @@ def user(ctx): try: clazz = get_class(name) user_manager = clazz(group_manager=group_manager, settings=settings) + except CorruptUserStorage: + raise except Exception: click.echo( "Could not instantiate user manager {}, falling back to FilebasedUserManager!".format( @@ -76,7 +83,7 @@ def user(ctx): except Exception: click.echo("Could not instantiate user manager", err=True) - return + ctx.exit(-1) @user.command(name="list") diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 6f908f24f..1cfb0a578 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -547,6 +547,8 @@ class Server(object): try: clazz = octoprint.util.get_class(user_manager_name) userManager = clazz(groupManager) + except octoprint.access.users.CorruptUserStorage: + raise except Exception: self._logger.exception( "Could not instantiate user manager {}, "