Esempio n. 1
0
class ExecutionLoggingInitiatorTest(unittest.TestCase):
    def test_start_logging_on_execution_start(self):
        execution_id = self.executor_service.start_script(
            create_config_model('my_script'),
            {},
            'userX',
            create_audit_names(ip='localhost'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper.finish(0)

        entry = self.logging_service.find_history_entry(execution_id, 'userX')
        self.assertIsNotNone(entry)

    def test_logging_values(self):
        param1 = create_script_param_config('p1')
        param2 = create_script_param_config('p2', param='-x')
        param3 = create_script_param_config('p3', param='-y', no_value=True)
        param4 = create_script_param_config('p4', param='-z', type='int')
        config_model = create_config_model(
            'my_script', script_command='echo', parameters=[param1, param2, param3, param4])

        execution_id = self.executor_service.start_script(
            config_model,
            {'p1': 'abc', 'p3': True, 'p4': 987},
            'userX',
            create_audit_names(ip='localhost', auth_username='******'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper._write_script_output('some text\n')
        executor.process_wrapper._write_script_output('another text')
        executor.process_wrapper.finish(0)

        wait_observable_close_notification(executor.get_anonymized_output_stream(), 2)

        entry = self.logging_service.find_history_entry(execution_id, 'userX')
        self.assertIsNotNone(entry)
        self.assertEqual('userX', entry.user_id)
        self.assertEqual('sandy', entry.user_name)
        self.assertEqual('my_script', entry.script_name)
        self.assertEqual('echo abc -y -z 987', entry.command)
        self.assertEqual('my_script', entry.script_name)

        log = self.logging_service.find_log(execution_id)
        self.assertEqual('some text\nanother text', log)

    def test_exit_code(self):
        config_model = create_config_model(
            'my_script', script_command='ls', parameters=[])

        execution_id = self.executor_service.start_script(
            config_model,
            {},
            'userX',
            create_audit_names(ip='localhost'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper._write_script_output('some text\n')
        executor.process_wrapper._write_script_output('another text')
        executor.process_wrapper.finish(14)

        wait_observable_close_notification(executor.get_anonymized_output_stream(), 2)

        entry = self.logging_service.find_history_entry(execution_id, 'userX')
        self.assertEqual(14, entry.exit_code)

    def setUp(self):
        test_utils.setup()

        executor._process_creator = _MockProcessWrapper

        authorizer = Authorizer([], [], [], EmptyGroupProvider())
        self.logging_service = ExecutionLoggingService(test_utils.temp_folder, LogNameCreator(), authorizer)
        self.executor_service = ExecutionService(_IdGeneratorMock())

        self.controller = ExecutionLoggingController(self.executor_service, self.logging_service)
        self.controller.start()

    def tearDown(self):
        test_utils.cleanup()

        executions = self.executor_service.get_active_executions('userX')
        for execution_id in executions:
            try:
                self.executor_service.kill_script(execution_id)
                self.executor_service.cleanup_execution(execution_id)
            except:
                traceback.print_exc()
class ExecutionServiceAuthorizationTest(unittest.TestCase):
    owner_user = User('user_x', {audit_utils.AUTH_USERNAME: '******'})

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', AccessProhibitedException),
                           ('history_user', AccessProhibitedException)])
    def test_get_active_executor(self, user_id, expected_exception):
        self._assert_throws_exception(
            expected_exception, self.executor_service.get_active_executor,
            self.execution_id, User(user_id, {}))

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', AccessProhibitedException),
                           ('history_user', AccessProhibitedException)])
    def test_stop_script(self, user_id, expected_exception):
        self._assert_throws_exception(expected_exception,
                                      self.executor_service.stop_script,
                                      self.execution_id,
                                      User(user_id, {}),
                                      has_results=False)

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', AccessProhibitedException),
                           ('history_user', AccessProhibitedException)])
    def test_kill_script(self, user_id, expected_exception):
        self._assert_throws_exception(expected_exception,
                                      self.executor_service.kill_script,
                                      self.execution_id,
                                      User(user_id, {}),
                                      has_results=False)

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', None), ('history_user', None)])
    def test_is_running(self, user_id, expected_exception):
        self._assert_throws_exception(expected_exception,
                                      self.executor_service.is_running,
                                      self.execution_id, User(user_id, {}))

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', AccessProhibitedException),
                           ('history_user', AccessProhibitedException)])
    def test_get_config(self, user_id, expected_exception):
        self._assert_throws_exception(expected_exception,
                                      self.executor_service.get_config,
                                      self.execution_id, User(user_id, {}))

    @parameterized.expand([(owner_user.user_id, None),
                           ('another_user', AccessProhibitedException),
                           ('admin_user', AccessProhibitedException),
                           ('history_user', AccessProhibitedException)])
    def test_cleanup(self, user_id, expected_exception):
        self.executor_service.stop_script(self.execution_id, self.owner_user)

        self._assert_throws_exception(expected_exception,
                                      self.executor_service.cleanup_execution,
                                      self.execution_id,
                                      User(user_id, {}),
                                      has_results=False)

        self.script_cleaned = True

    def _assert_throws_exception(self,
                                 expected_exception,
                                 func,
                                 *parameters,
                                 has_results=True):
        try:
            result = func(*parameters)
            if expected_exception:
                self.fail('Should throw ' + str(expected_exception) +
                          ', but did not')
            if has_results:
                self.assertIsNotNone(result)

        except Exception as e:
            self.assertIsInstance(e, expected_exception)

    def setUp(self) -> None:
        super().setUp()

        def create_process(executor, command, working_directory,
                           env_variables):
            return _MockProcessWrapper(executor, command, working_directory,
                                       env_variables)

        executor._process_creator = create_process

        authorizer = Authorizer([ANY_USER], ['admin_user'], ['history_user'],
                                [], EmptyGroupProvider())
        self.executor_service = ExecutionService(authorizer,
                                                 _IdGeneratorMock())

        self.execution_id = _start(self.executor_service,
                                   self.owner_user.user_id)

        self.script_cleaned = False

    def tearDown(self) -> None:
        super().tearDown()

        executor._process_creator = create_process_wrapper

        if not self.script_cleaned:
            self.executor_service.kill_script(self.execution_id,
                                              self.owner_user)
            self.executor_service.cleanup_execution(self.execution_id,
                                                    self.owner_user)
class ExecutionLoggingInitiatorTest(unittest.TestCase):
    def test_start_logging_on_execution_start(self):
        execution_id = self.executor_service.start_script(
            create_config_model('my_script'),
            {},
            'userX',
            create_audit_names(ip='localhost'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper.finish(0)

        entry = self.logging_service.find_history_entry(execution_id)
        self.assertIsNotNone(entry)

    def test_logging_values(self):
        param1 = create_script_param_config('p1')
        param2 = create_script_param_config('p2', param='-x')
        param3 = create_script_param_config('p3', param='-y', no_value=True)
        param4 = create_script_param_config('p4', param='-z', type='int')
        config_model = create_config_model(
            'my_script', script_command='echo', parameters=[param1, param2, param3, param4])

        execution_id = self.executor_service.start_script(
            config_model,
            {'p1': 'abc', 'p3': True, 'p4': 987},
            'userX',
            create_audit_names(ip='localhost', auth_username='******'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper._write_script_output('some text\n')
        executor.process_wrapper._write_script_output('another text')
        executor.process_wrapper.finish(0)

        wait_observable_close_notification(executor.get_anonymized_output_stream(), 2)

        entry = self.logging_service.find_history_entry(execution_id)
        self.assertIsNotNone(entry)
        self.assertEqual('userX', entry.user_id)
        self.assertEqual('sandy', entry.user_name)
        self.assertEqual('my_script', entry.script_name)
        self.assertEqual('echo abc -y -z 987', entry.command)
        self.assertEqual('my_script', entry.script_name)

        log = self.logging_service.find_log(execution_id)
        self.assertEqual('some text\nanother text', log)

    def test_exit_code(self):
        config_model = create_config_model(
            'my_script', script_command='ls', parameters=[])

        execution_id = self.executor_service.start_script(
            config_model,
            {},
            'userX',
            create_audit_names(ip='localhost'))

        executor = self.executor_service.get_active_executor(execution_id)
        executor.process_wrapper._write_script_output('some text\n')
        executor.process_wrapper._write_script_output('another text')
        executor.process_wrapper.finish(14)

        wait_observable_close_notification(executor.get_anonymized_output_stream(), 2)

        entry = self.logging_service.find_history_entry(execution_id)
        self.assertEqual(14, entry.exit_code)

    def setUp(self):
        test_utils.setup()

        executor._process_creator = _MockProcessWrapper

        self.logging_service = ExecutionLoggingService(test_utils.temp_folder, LogNameCreator())
        self.executor_service = ExecutionService(_IdGeneratorMock())

        self.controller = ExecutionLoggingController(self.executor_service, self.logging_service)
        self.controller.start()

    def tearDown(self):
        test_utils.cleanup()

        executions = self.executor_service.get_active_executions('userX')
        for execution_id in executions:
            try:
                self.executor_service.kill_script(execution_id)
                self.executor_service.cleanup_execution(execution_id)
            except:
                traceback.print_exc()