class AuthManager(TortugaObjectManager, Singleton): def __init__(self): super(AuthManager, self).__init__() self._configManager = ConfigManager() self.__principals = {} self.__loadPrincipals() def cryptPassword(self, cleartext, salt="$1$"): \ # pylint: disable=no-self-use """ Return crypted password.... """ return crypt.crypt(cleartext, salt) def reloadPrincipals(self): """ This is used to reload the principals in auth manager """ self.__principals.clear() self.__loadPrincipals() def __loadPrincipals(self): """ Load principals for config manager and datastore """ # Create builtin cfm principal cfmUser = AuthPrincipal( self._configManager.getCfmUser(), self.cryptPassword(self._configManager.getCfmPassword()), {'roles': 'cfm'}) # Add cfm user self.__principals[cfmUser.getName()] = cfmUser # Add users from DB if self._configManager.isInstaller(): for admin in getAdminApi().getAdminList(): self.__principals[admin.getUsername()] = AuthPrincipal( admin.getUsername(), admin.getPassword(), attributeDict={'id': admin.getId()}) def getPrincipal(self, username, password): """ Get a principal based on a username and password """ principal = self.__principals.get(username) if principal and principal.getPassword() == crypt.crypt( password, principal.getPassword()): return principal return None
class AuthManager(TortugaObjectManager): def __init__(self, *, session: Session): super(AuthManager, self).__init__() self.session = session self._configManager = ConfigManager() self.__principals = {} self.__loadPrincipals() def cryptPassword(self, cleartext): \ # pylint: disable=no-self-use """ Return crypted password """ return pbkdf2_sha256.hash(cleartext) def reloadPrincipals(self): """ This is used to reload the principals in auth manager """ self.__principals.clear() self.__loadPrincipals() def __loadPrincipals(self): """ Load principals for config manager and datastore """ from tortuga.admin.api import AdminApi # Create built-in cfm principal cfmUser = AuthPrincipal( self._configManager.getCfmUser(), self.cryptPassword(self._configManager.getCfmPassword()), {'roles': 'cfm'}) # Add cfm user self.__principals[cfmUser.get_name()] = cfmUser # Add users from DB if self._configManager.isInstaller(): for admin in AdminApi().getAdminList(self.session): self.__principals[admin.getUsername()] = AuthPrincipal( admin.getUsername(), admin.getPassword(), attributes={'id': admin.getId()}) def get_principal(self, username: str) -> AuthPrincipal: """ Get a principal by username. :param str username: the username of the principal to lookup :return AuthPrincipal: the principal, if found, otherwise None """ principal: AuthPrincipal = self.__principals.get(username) if not principal: principal = None return principal