🥅 Better handling of corrupted user storage

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
This commit is contained in:
Gina Häußge committed 2020-11-12 14:14:26 +01:00
1 parent fabf331197
commit b94424411a
3 files changed
+29 -4

No files matched your search

+18 -2
View File
@@ -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
+9 -2
View File
@@ -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")
+2
View File
@@ -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 {}, "