def test_authenticate_admin_invalid_password(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config( p_user_list=CONFIG_USER_LIST)) self.assertFalse( handler.authenticate(p_username=USER_1_UID, p_password=USER_1_PASSWORD + "x"))
def create_dummy_unix_user_handler(cls): config = unix_user_handler.UnixUserHandlerConfigModel() config.admin_username = ADMIN_USER config.admin_password = ADMIN_PASSWORD config.user_list = CONFIG_USER_LIST return unix_user_handler.UnixUserHandler(p_config=config)
def test_get_valid_uid_without_user_list(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config()) self.assertEqual(int(USER_1_UID_NUMBER), handler.get_uid(p_username=USER_1_UID)) self.assertEqual(int(USER_2_UID_NUMBER), handler.get_uid(p_username=USER_2_UID))
def test_list_users_without_user_list(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config()) users = handler.list_users() self.assertIsNotNone(users) self.assertEqual(len(users), 2) self.assertIn(USER_1_UID, users) self.assertIn(USER_2_UID, users)
def create_dummy_status_server(self, p_process_handlers=None): # TODO: Add rule set configs as parameters again and migrate them into the datamodel if p_process_handlers is None: p_process_handlers = {} _persistence = test_persistence.TestPersistence.create_dummy_persistence( self._logger) _rule_handler = test_rule_handler.TestRuleHandler.create_dummy_rule_handler( p_persistence=_persistence) master_connector_config = master_connector.MasterConnectorConfigModel() _master_connector = master_connector.MasterConnector( p_config=master_connector_config) app_control_config = app_control.AppControlConfigModel() _app_control = app_control.AppControl( p_config=app_control_config, p_debug_mode=False, p_process_handlers=p_process_handlers, p_device_handler=None, p_prometheus_client=None, p_persistence=_persistence, p_rule_handler=_rule_handler, p_notification_handlers=[], p_master_connector=_master_connector, p_login_mapping=test_data.LOGIN_MAPPING, p_locale_helper=locale_helper.LocaleHelper()) status_server_config = status_server.StatusServerConfigModel() status_server_config.admin_username = ADMIN_USERNAME status_server_config.admin_password = ADMIN_PASSWORD status_server_config.app_secret = "secret!" status_server_config.port = int(os.getenv("STATUS_SERVER_PORT", "5555")) user_handler_config = unix_user_handler.BaseUserHandlerConfigModel() user_handler_config.admin_username = ADMIN_USERNAME user_handler_config.admin_password = ADMIN_PASSWORD user_handler = unix_user_handler.UnixUserHandler( p_config=user_handler_config) _status_server = status_server.StatusServer( p_config=status_server_config, p_package_name=app.PACKAGE_NAME, p_app_control=_app_control, p_master_connector=_master_connector, p_persistence=_persistence, p_is_master=True, p_user_handler=user_handler, p_locale_helper=locale_helper.LocaleHelper()) return _status_server
def prepare_services(self, p_full_startup=True): super().prepare_services(p_full_startup=p_full_startup) # TODO: Activate in memory sqlite backend for slaves self._persistence = persistence.Persistence( p_config=self._config[persistence.SECTION_NAME]) if not p_full_startup: return if self.is_master(): self._rule_handler = rule_handler.RuleHandler( p_config=self._config[rule_handler.SECTION_NAME], p_persistence=self._persistence) self._master_connector = master_connector.MasterConnector( p_config=self._config[master_connector.SECTION_NAME]) config = self._config[audio_handler.SECTION_NAME] if config.is_active(): self._notification_handlers.append( audio_handler.AudioHandler(p_config=config)) config = self._config[popup_handler.SECTION_NAME] if config.is_active(): self._notification_handlers.append( popup_handler.PopupHandler(p_config=config)) self.check_migrations() process_handler = client_process_handler.ClientProcessHandler( p_config=self._config[client_process_handler.SECTION_NAME], p_process_iterator_factory=ProcessIteratorFactory()) self._client_device_handler = client_device_handler.ClientDeviceHandler( p_config=self._config[client_device_handler.SECTION_NAME], p_persistence=self._persistence) self._process_handlers = { process_handler.id: process_handler, self._client_device_handler.id: self._client_device_handler } config = self._config[prometheus.SECTION_NAME] if config.is_active(): self._prometheus_client = prometheus.PrometheusClient( p_logger=self._logger, p_config=config) unix_user_handler_config = self._config[unix_user_handler.SECTION_NAME] status_server_config = self._config[status_server.SECTION_NAME] self.init_babel(p_localeselector=self.get_request_locale) localedir = os.path.join(os.path.dirname(__file__), "translations") a_locale_helper = locale_helper.LocaleHelper( p_locale_selector=self.get_request_locale, p_locale_dir=localedir) self.add_locale_helper(a_locale_helper) if self.is_master(): if status_server_config.is_active(): if status_server_config.admin_password is not None: msg = "admin_user and admin_password in section [StatusSever] " \ "should be moved to section [UnixUserHandler]" self._logger.warning(msg) if not unix_user_handler_config.is_active(): unix_user_handler_config.admin_username = status_server_config.admin_username unix_user_handler_config.admin_password = status_server_config.admin_password if self.is_master( ) and not unix_user_handler_config.is_active(): msg = "admin_user and admin_password must be supplied in section [UnixUserHandler]" raise configuration.ConfigurationException(msg) if unix_user_handler_config.is_active(): self._user_handler = unix_user_handler.UnixUserHandler( p_config=unix_user_handler_config, p_exclude_user_list=[constants.APPLICATION_USER]) else: self._user_handler = unix_user_handler.UnixUserHandler( p_config=unix_user_handler_config) self._login_mapping = login_mapping.LoginMapping() self._login_mapping.read_from_configuration( p_login_mapping_section_handler=self._login_mapping_section_handler ) self._app_control = app_control.AppControl( p_config=self._config[app_control.SECTION_NAME], p_debug_mode=self._app_config.debug_mode, p_process_handlers=self._process_handlers, p_device_handler=self._client_device_handler, p_persistence=self._persistence, p_rule_handler=self._rule_handler, p_notification_handlers=self._notification_handlers, p_master_connector=self._master_connector, p_prometheus_client=self._prometheus_client, p_user_handler=self._user_handler, p_locale_helper=self.locale_helper, p_login_mapping=self._login_mapping) if self._config[app_control.SECTION_NAME].scan_active: task = base_app.RecurringTask( p_name="app_control.scan_processes(ProcessHandler)", p_handler_method=lambda: self._app_control.scan_processes( p_process_handler=process_handler), p_interval=process_handler.check_interval) self.add_recurring_task(p_recurring_task=task) else: fmt = "Process scanning for this host has been deactivated in configuration" self._logger.warning(fmt) if self._client_device_handler: task = base_app.RecurringTask( p_name="app_control.scan_processes(DeviceHandler)", p_handler_method=lambda: self._app_control.scan_processes( p_process_handler=self._client_device_handler), p_interval=self._client_device_handler.check_interval) self.add_recurring_task(p_recurring_task=task) if status_server_config.is_active(): self._status_server = status_server.StatusServer( p_config=self._config[status_server.SECTION_NAME], p_package_name=PACKAGE_NAME, p_app_control=self._app_control, p_master_connector=self._master_connector, p_persistence=self._persistence, p_is_master=self.is_master(), p_locale_helper=self._locale_helper, p_base_gettext=self.gettext, p_languages=constants.LANGUAGES, p_user_handler=self._user_handler) elif self.is_master(): msg = "Master instance requires port number for web server" raise configuration.ConfigurationException(msg) else: msg = "Slave instance will not start web server due to missing port number" self._logger.warn(msg) task = base_app.RecurringTask( p_name="app_control.check", p_handler_method=self._app_control.check, p_interval=self._app_control.check_interval) self.add_recurring_task(p_recurring_task=task)
def test_is_invalid_uid_super_branch(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config( p_user_list=CONFIG_USER_LIST)) self.assertFalse(handler.is_valid_uid(p_uid=100, p_username="******"))
def test_is_invalid_admin(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config( p_user_list=CONFIG_USER_LIST)) self.assertFalse(handler.is_admin(p_username=USER_2_UID))
def test_get_invalid_uid_without_user_list(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config()) self.assertIsNone(handler.get_uid(p_username="******"))
def test_get_invalid_uid(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config( p_user_list=CONFIG_USER_LIST)) self.assertIsNone(handler.get_uid(p_username="******"))
def test_instantiation_invalid_user_list(self): config = self.get_config(p_user_list="xxx:yyy") with self.assertRaises(configuration.ConfigurationException): unix_user_handler.UnixUserHandler(p_config=config)
def test_instantiation(self): handler = unix_user_handler.UnixUserHandler(p_config=self.get_config()) self.assertIsNotNone(handler)