def test_string_parameter_when_value(self): script_config = script_configs.Config() self.add_parameter('param', script_config) valid = model_helper.validate_parameters({'param': 'val'}, script_config) self.assertTrue(valid)
def test_required_parameter_when_none(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.required = True valid = model_helper.validate_parameters({}, script_config) self.assertFalse(valid)
def test_flag_parameter_when_false_bool(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.no_value = True valid = model_helper.validate_parameters({'param': False}, script_config) self.assertTrue(valid)
def test_int_parameter_when_float_string(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' valid = model_helper.validate_parameters({'param': '1.0'}, script_config) self.assertFalse(valid)
def test_int_parameter_when_large_negative_int_string(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' valid = model_helper.validate_parameters({'param': '-1234567890987654321'}, script_config) self.assertTrue(valid)
def test_int_parameter_when_negative_int(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' valid = model_helper.validate_parameters({'param': -100}, script_config) self.assertTrue(valid)
def test_flag_parameter_when_some_string(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.no_value = True valid = model_helper.validate_parameters({'param': 'no'}, script_config) self.assertFalse(valid)
def test_multiple_parameters_when_all_missing(self): script_config = script_configs.Config() for i in range(0, 5): self.add_parameter('param' + str(i), script_config) valid = model_helper.validate_parameters({}, script_config) self.assertTrue(valid)
def test_list_parameter_when_not_matches(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'list' parameter.values = ['val1', 'val2', 'val3'] valid = model_helper.validate_parameters({'param': 'val4'}, script_config) self.assertFalse(valid)
def test_required_int_parameter_when_zero(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' parameter.required = True valid = model_helper.validate_parameters({'param': 0}, script_config) self.assertTrue(valid)
def test_int_parameter_when_larger_than_min(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' parameter.min = 100 valid = model_helper.validate_parameters({'param': 0}, script_config) self.assertFalse(valid)
def test_int_parameter_when_equal_to_min(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' parameter.min = -100 valid = model_helper.validate_parameters({'param': -100}, script_config) self.assertTrue(valid)
def test_int_parameter_when_lower_than_max(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'int' parameter.max = 100 valid = model_helper.validate_parameters({'param': 9}, script_config) self.assertTrue(valid)
def test_required_flag_parameter_when_true_boolean(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.no_value = True parameter.required = True valid = model_helper.validate_parameters({'param': True}, script_config) self.assertTrue(valid)
def test_file_upload_parameter_when_not_exists(self): script_config = script_configs.Config() parameter = self.add_parameter('param', script_config) parameter.type = 'file_upload' uploaded_file = test_utils.create_file('test.xml') valid = model_helper.validate_parameters({'param': uploaded_file + '_'}, script_config) self.assertFalse(valid)
def test_multiple_parameters_when_all_defined(self): script_config = script_configs.Config() values = {} for i in range(0, 5): parameter = self.add_parameter('param' + str(i), script_config) values[parameter.name] = str(i) valid = model_helper.validate_parameters(values, script_config) self.assertTrue(valid)
def test_multiple_required_parameters_when_one_missing(self): script_config = script_configs.Config() values = {} for i in range(0, 5): parameter = self.add_parameter('param' + str(i), script_config) parameter.required = True if i != 4: values[parameter.name] = str(i) valid = model_helper.validate_parameters(values, script_config) self.assertFalse(valid)
def test_multiple_int_parameters_when_one_invalid(self): script_config = script_configs.Config() values = {} for i in range(0, 5): parameter = self.add_parameter('param' + str(i), script_config) parameter.type = 'int' if i != 4: values[parameter.name] = i else: values[parameter.name] = 'val' valid = model_helper.validate_parameters(values, script_config) self.assertFalse(valid)
def post(self): script_name = None audit_name = get_audit_name(self) try: arguments = tornado_utils.get_form_arguments(self) execution_info = external_model.to_execution_info(arguments) script_name = execution_info.script config = load_config(script_name) if not config: message = 'Script with name "' + str( script_name) + '" not found' LOGGER.error(message) respond_error(self, 400, message) return file_upload_feature = self.application.file_upload_feature if self.request.files: for key, value in self.request.files.items(): file_info = value[0] file_path = file_upload_feature.save_file( file_info.filename, file_info.body, audit_name) execution_info.param_values[key] = file_path valid_parameters = model_helper.validate_parameters( execution_info.param_values, config) if not valid_parameters: message = 'Received invalid parameters' LOGGER.error(message) respond_error(self, 400, message) return executor = ScriptExecutor(config, execution_info.param_values, audit_name) audit_command = executor.get_secure_command() LOGGER.info('Calling script: ' + audit_command) LOGGER.info('User info: ' + str(get_all_audit_names(self))) process_id = executor.start() running_scripts[process_id] = executor self.write(str(process_id)) secure_output_stream = executor.get_secure_output_stream() self.start_script_output_logger(audit_name, script_name, secure_output_stream) alerts_config = self.application.alerts_config if alerts_config: self.subscribe_fail_alerter(script_name, alerts_config, audit_name, executor, secure_output_stream) except Exception as e: LOGGER.exception("Error while calling the script") if hasattr(e, "strerror") and e.strerror: error_output = e.strerror else: error_output = "Unknown error occurred, contact the administrator" result = " --- ERRORS --- \n" result += error_output if script_name: script = str(script_name) else: script = "Some script" audit_name = audit_name send_alerts( self.application.alerts_config, script + ' NOT STARTED', "Couldn't start the script " + script + ' by ' + audit_name + '.\n\n' + result) respond_error(self, 500, result)
def post(self): script_name = None audit_name = get_audit_name_from_request(self) try: arguments = tornado_utils.get_form_arguments(self) execution_info = external_model.to_execution_info(arguments) script_name = execution_info.script config = self.application.config_service.load_config(script_name) if not config: message = 'Script with name "' + str(script_name) + '" not found' LOGGER.error(message) respond_error(self, 400, message) return if not can_access_script(config, self): LOGGER.warning('Access to the script "' + script_name + '" is denied for ' + audit_name) respond_error(self, 403, 'Access to the script is denied') return file_upload_feature = self.application.file_upload_feature if self.request.files: for key, value in self.request.files.items(): file_info = value[0] file_path = file_upload_feature.save_file(file_info.filename, file_info.body, audit_name) execution_info.param_values[key] = file_path model_helper.prepare_multiselect_values(execution_info.param_values, config.parameters) valid_parameters = model_helper.validate_parameters(execution_info.param_values, config) if not valid_parameters: message = 'Received invalid parameters' LOGGER.error(message) respond_error(self, 400, message) return user_id = _identify_user(self) all_audit_names = get_all_audit_names(self) LOGGER.info('Calling script ' + script_name + '. User ' + str(all_audit_names)) execution_id = self.application.execution_service.start_script( config, execution_info.param_values, user_id, all_audit_names) self.write(str(execution_id)) except Exception as e: LOGGER.exception("Error while calling the script") if hasattr(e, "strerror") and e.strerror: error_output = e.strerror else: error_output = "Unknown error occurred, contact the administrator" result = " --- ERRORS --- \n" result += error_output if script_name: script = str(script_name) else: script = "Some script" audit_name = audit_name self.application.alerts_service.send_alert( script + ' NOT STARTED', "Couldn't start the script " + script + ' by ' + audit_name + '.\n\n' + result) respond_error(self, 500, result)
def post(self): script_name = None try: request_data = self.request.body execution_info = external_model.to_execution_info(request_data.decode("UTF-8")) script_name = execution_info.script config = load_config(script_name) if not config: respond_error(self, 400, "Script with name '" + str(script_name) + "' not found") return working_directory = config.get_working_directory() if working_directory is not None: working_directory = file_utils.normalize_path(working_directory) script_logger = logging.getLogger("scriptServer") valid_parameters = model_helper.validate_parameters(execution_info.param_values, config) if not valid_parameters: respond_error(self, 400, 'Received invalid parameters') return script_base_command = process_utils.split_command(config.get_script_command(), working_directory) script_args = build_command_args(execution_info.param_values, config) command = script_base_command + script_args audit_script_args = build_command_args( execution_info.param_values, config, model_helper.value_to_str) audit_command = script_base_command + audit_script_args script_logger.info('Calling script: ' + ' '.join(audit_command)) script_logger.info('User info: ' + str(get_all_audit_names(self, script_logger))) run_pty = config.is_requires_terminal() if run_pty and not pty_supported: script_logger.warning( "Requested PTY mode, but it's not supported for this OS (" + sys.platform + "). Falling back to POpen") run_pty = False if run_pty: self.process_wrapper = execution_pty.PtyProcessWrapper(command, config.get_name(), working_directory, config, execution_info) else: self.process_wrapper = execution_popen.POpenProcessWrapper(command, config.get_name(), working_directory, config, execution_info) self.process_wrapper.start() process_id = self.process_wrapper.get_process_id() running_scripts[process_id] = self.process_wrapper self.write(str(process_id)) alerts_config = self.application.alerts_config if alerts_config: self.subscribe_fail_alerter(script_name, script_logger, alerts_config) except Exception as e: script_logger = logging.getLogger("scriptServer") script_logger.exception("Error while calling the script") if hasattr(e, "strerror") and e.strerror: error_output = e.strerror else: error_output = "Unknown error occurred, contact the administrator" result = " --- ERRORS --- \n" result += error_output if script_name: script = str(script_name) else: script = "Some script" audit_name = get_audit_name(self, script_logger) send_alerts(self.application.alerts_config, script + ' NOT STARTED', "Couldn't start the script " + script + ' by ' + audit_name + '.\n\n' + result) respond_error(self, 500, result)
def test_no_parameters(self): script_config = script_configs.Config() valid = model_helper.validate_parameters({}, script_config) self.assertTrue(valid)