def _read_repeat_period(incoming_schedule_config): period = model_helper.read_int_from_config('repeat_period', incoming_schedule_config, default=1) if period <= 0: raise InvalidScheduleException('repeat_period should be > 0') return period
def __init__(self, oauth_authorize_url, oauth_token_url, oauth_scope, params_dict): super().__init__() self.oauth_token_url = oauth_token_url self.oauth_scope = oauth_scope self.client_id = model_helper.read_obligatory(params_dict, 'client_id', ' for OAuth') secret_value = model_helper.read_obligatory(params_dict, 'secret', ' for OAuth') self.secret = model_helper.resolve_env_vars(secret_value, full_match=True) self._client_visible_config['client_id'] = self.client_id self._client_visible_config['oauth_url'] = oauth_authorize_url self._client_visible_config['oauth_scope'] = oauth_scope self.group_support = read_bool_from_config('group_support', params_dict, default=True) self.auth_info_ttl = params_dict.get('auth_info_ttl') self.session_expire = read_int_from_config( 'session_expire_minutes', params_dict, default=0) * 60 self.dump_file = params_dict.get('state_dump_file') if self.dump_file: self._validate_dump_file(self.dump_file) self._users = {} # type: Dict[str, _UserState] self._user_locks = defaultdict(lambda: threading.Lock()) self.timer = None if self.dump_file: self._restore_state() self._schedule_dump_task()
def from_json(conf_path, temp_folder): if os.path.exists(conf_path): file_content = file_utils.read_file(conf_path) else: file_content = "{}" config = ServerConfig() json_object = json.loads(file_content) address = "0.0.0.0" port = 5000 ssl = json_object.get("ssl") if ssl is not None: key_path = model_helper.read_obligatory(ssl, 'key_path', ' for ssl') cert_path = model_helper.read_obligatory(ssl, 'cert_path', ' for ssl') config.ssl = True config.ssl_key_path = key_path config.ssl_cert_path = cert_path port = 5443 if json_object.get("address"): address = json_object.get("address") config.address = address if json_object.get("port"): port = json_object.get("port") config.port = port if json_object.get('title'): config.title = json_object.get('title') access_config = json_object.get('access') if access_config: allowed_users = access_config.get('allowed_users') user_groups = model_helper.read_dict(access_config, 'groups') else: allowed_users = None user_groups = {} auth_config = json_object.get('auth') if auth_config: config.authenticator = create_authenticator(auth_config, temp_folder) auth_type = config.authenticator.auth_type if auth_type == 'google_oauth' and allowed_users is None: raise Exception('auth.allowed_users field is mandatory for ' + auth_type) def_trusted_ips = [] def_admins = [] else: def_trusted_ips = ['127.0.0.1', '::1'] def_admins = def_trusted_ips if access_config: config.trusted_ips = strip( read_list(access_config, 'trusted_ips', default=def_trusted_ips)) admin_users = _parse_admin_users(access_config, default_admins=def_admins) else: config.trusted_ips = def_trusted_ips admin_users = def_admins config.allowed_users = _prepare_allowed_users(allowed_users, admin_users, user_groups) config.alerts_config = parse_alerts_config(json_object) config.logging_config = parse_logging_config(json_object) config.user_groups = user_groups config.admin_users = admin_users config.max_request_size_mb = read_int_from_config('max_request_size', json_object, default=10) return config
def test_normal_int_value(self): value = model_helper.read_int_from_config('abc', {'abc': 123}) self.assertEqual(123, value)
def test_default_value_when_empty_string(self): value = model_helper.read_int_from_config('my_key', {'my_key': ' '}, default=9999) self.assertEqual(9999, value)
def from_json(conf_path, temp_folder): if os.path.exists(conf_path): file_content = file_utils.read_file(conf_path) else: file_content = "{}" config = ServerConfig() json_object = json.loads(file_content) address = "0.0.0.0" port = 5000 ssl = json_object.get("ssl") if ssl is not None: key_path = model_helper.read_obligatory(ssl, 'key_path', ' for ssl') cert_path = model_helper.read_obligatory(ssl, 'cert_path', ' for ssl') config.ssl = True config.ssl_key_path = key_path config.ssl_cert_path = cert_path port = 5443 if json_object.get("address"): address = json_object.get("address") config.address = address if json_object.get("port"): port = json_object.get("port") config.port = port if json_object.get('title'): config.title = json_object.get('title') config.enable_script_titles = read_bool_from_config('enable_script_titles', json_object, default=True) access_config = json_object.get('access') if access_config: allowed_users = access_config.get('allowed_users') user_groups = model_helper.read_dict(access_config, 'groups') user_header_name = access_config.get('user_header_name') else: allowed_users = None user_groups = {} user_header_name = None auth_config = json_object.get('auth') if auth_config: config.authenticator = create_authenticator(auth_config, temp_folder) auth_type = config.authenticator.auth_type if auth_type == 'google_oauth' and allowed_users is None: raise Exception('access.allowed_users field is mandatory for ' + auth_type) def_trusted_ips = [] def_admins = [] else: def_trusted_ips = ['127.0.0.1', '::1'] def_admins = def_trusted_ips if access_config: trusted_ips = strip( read_list(access_config, 'trusted_ips', default=def_trusted_ips)) admin_users = _parse_admin_users(access_config, default_admins=def_admins) full_history_users = _parse_history_users(access_config) code_editor_users = _parse_code_editor_users(access_config, admin_users) else: trusted_ips = def_trusted_ips admin_users = def_admins full_history_users = [] code_editor_users = def_admins security = model_helper.read_dict(json_object, 'security') config.allowed_users = _prepare_allowed_users(allowed_users, admin_users, user_groups) config.alerts_config = json_object.get('alerts') config.callbacks_config = json_object.get('callbacks') config.logging_config = parse_logging_config(json_object) config.user_groups = user_groups config.admin_users = admin_users config.full_history_users = full_history_users config.code_editor_users = code_editor_users config.user_header_name = user_header_name config.ip_validator = TrustedIpValidator(trusted_ips) config.max_request_size_mb = read_int_from_config('max_request_size', json_object, default=10) config.secret_storage_file = json_object.get( 'secret_storage_file', os.path.join(temp_folder, 'secret.dat')) config.xsrf_protection = _parse_xsrf_protection(security) return config
def test_default_value(self): value = model_helper.read_int_from_config('my_key', {'abc': 100}) self.assertIsNone(value)
def test_default_value_explicit(self): value = model_helper.read_int_from_config('my_key', {'abc': 100}, default=5) self.assertEqual(5, value)
def test_zero_int_value(self): value = model_helper.read_int_from_config('abc', {'abc': 0}) self.assertEqual(0, value)
def test_string_value(self): value = model_helper.read_int_from_config('abc', {'abc': '-666'}) self.assertEqual(-666, value)
def from_json(conf_path, temp_folder): if os.path.exists(conf_path): file_content = file_utils.read_file(conf_path) else: file_content = "{}" config = ServerConfig() json_object = json.loads(file_content) address = "0.0.0.0" port = 5000 ssl = json_object.get("ssl") if ssl is not None: key_path = model_helper.read_obligatory(ssl, 'key_path', ' for ssl') cert_path = model_helper.read_obligatory(ssl, 'cert_path', ' for ssl') config.ssl = True config.ssl_key_path = key_path config.ssl_cert_path = cert_path port = 5443 if json_object.get("address"): address = json_object.get("address") config.address = address if json_object.get("port"): port = json_object.get("port") config.port = port if json_object.get('title'): config.title = json_object.get('title') access_config = json_object.get('access') if access_config: allowed_users = access_config.get('allowed_users') user_groups = model_helper.read_dict(access_config, 'groups') else: allowed_users = None user_groups = {} auth_config = json_object.get('auth') if auth_config: config.authenticator = create_authenticator(auth_config, temp_folder) auth_type = config.authenticator.auth_type if auth_type == 'google_oauth' and allowed_users is None: raise Exception('auth.allowed_users field is mandatory for ' + auth_type) def_trusted_ips = [] def_admins = [] else: def_trusted_ips = ['127.0.0.1', '::1'] def_admins = def_trusted_ips if access_config: config.trusted_ips = strip(read_list(access_config, 'trusted_ips', default=def_trusted_ips)) admin_users = _parse_admin_users(access_config, default_admins=def_admins) else: config.trusted_ips = def_trusted_ips admin_users = def_admins config.allowed_users = _prepare_allowed_users(allowed_users, admin_users, user_groups) config.alerts_config = json_object.get('alerts') config.callbacks_config = json_object.get('callbacks') config.logging_config = parse_logging_config(json_object) config.user_groups = user_groups config.admin_users = admin_users config.max_request_size_mb = read_int_from_config('max_request_size', json_object, default=10) return config
def test_normal_int_value(self): value = model_helper.read_int_from_config('abc', {'abc': 123}) self.assertEqual(123, value)
def test_default_value_when_empty_string(self): value = model_helper.read_int_from_config('my_key', {'my_key': ' '}, default=9999) self.assertEqual(9999, value)
def test_default_value_explicit(self): value = model_helper.read_int_from_config('my_key', {'abc': 100}, default=5) self.assertEqual(5, value)
def test_default_value(self): value = model_helper.read_int_from_config('my_key', {'abc': 100}) self.assertIsNone(value)
def test_string_value(self): value = model_helper.read_int_from_config('abc', {'abc': '-666'}) self.assertEqual(-666, value)
def test_zero_int_value(self): value = model_helper.read_int_from_config('abc', {'abc': 0}) self.assertEqual(0, value)