Example #1
0
    def setUp(self):
        super().setUp()

        self.socket = None

        application = tornado.web.Application(
            [(r'/scripts/([^/]*)', ScriptConfigSocket)],
            login_url='/login.html',
            cookie_secret='12345')
        application.auth = TornadoAuth(None)
        application.authorizer = Authorizer(ANY_USER, [], [],
                                            EmptyGroupProvider())
        application.identification = IpBasedIdentification(
            TrustedIpValidator(['127.0.0.1']), None)
        application.config_service = ConfigService(application.authorizer,
                                                   test_utils.temp_folder)

        server = httpserver.HTTPServer(application)
        socket, self.port = testing.bind_unused_port()
        server.add_socket(socket)

        test_utils.setup()

        for dir in ['x', 'y', 'z']:
            for file in range(1, 4):
                filename = dir + str(file) + '.txt'
                test_utils.create_file(
                    os.path.join('test1_files', dir, filename))

        test1_files_path = os.path.join(test_utils.temp_folder, 'test1_files')
        test_utils.write_script_config(
            {
                'name':
                'Test script 1',
                'script_path':
                'ls',
                'parameters': [
                    test_utils.create_script_param_config('text 1',
                                                          required=True),
                    test_utils.create_script_param_config(
                        'list 1', type='list', allowed_values=['A', 'B', 'C']),
                    test_utils.create_script_param_config(
                        'file 1',
                        type='server_file',
                        file_dir=test1_files_path),
                    test_utils.create_script_param_config(
                        'list 2',
                        type='list',
                        values_script='ls ' + test1_files_path + '/${file 1}')
                ]
            }, 'test_script_1')
Example #2
0
    def test_static_include_simple(self):
        included_path = test_utils.write_script_config(
            {'script_path': 'ping google.com'}, 'included')
        config_model = _create_config_model('main_conf',
                                            config={'include': included_path})

        self.assertEqual('ping google.com', config_model.script_command)
Example #3
0
    def test_xsrf_protection_when_token_failed(self):
        self.start_server(12345, '127.0.0.1')

        test_utils.write_script_config({
            'name': 's1',
            'script_path': 'ls'
        }, 's1', self.runners_folder)
        response = self._user_session.get('http://127.0.0.1:12345/scripts')

        start_response = self._user_session.post(
            'http://127.0.0.1:12345/executions/start',
            data={'__script_name': 's1'},
            files=[('notafile', None)],
            headers={'X-Requested-With': 'XMLHttpRequest'},
            cookies=response.cookies)
        self.assertEqual(start_response.status_code, 403)
Example #4
0
    def test_update_script_config(self):
        self.start_server(12345, '127.0.0.1')

        xsrf_token = self.get_xsrf_token(self._admin_session)

        script_path = test_utils.create_file('my_script.py')
        test_utils.write_script_config(
            {
                'name': 's1',
                'script_path': script_path
            }, 's1', self.runners_folder)

        response = self._admin_session.put(
            'http://127.0.0.1:12345/admin/scripts',
            data={
                'filename':
                's1.json',
                'config':
                json.dumps({
                    'name': 'new name',
                    'script': {
                        'mode': 'new_code',
                        'path': script_path,
                        'code': 'abcdef'
                    }
                })
            },
            headers={'X-XSRFToken': xsrf_token},
        )

        self.assertEqual(200, response.status_code)

        conf_response = self.request(
            'get', 'http://127.0.0.1:12345/admin/scripts/new%20name',
            self._admin_session)
        self.assertEqual(
            {
                'config': {
                    'name': 'new name',
                    'script_path': script_path
                },
                'filename': 's1.json'
            }, conf_response)

        script_content = file_utils.read_file(script_path)
        self.assertEqual('abcdef', script_content)
Example #5
0
    def prepare_config_model_with_included(self, included_params, static_param_name):
        included_path = test_utils.write_script_config({'parameters': included_params}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': '${' + static_param_name + '}',
            'parameters': [create_script_param_config(static_param_name)]})
        config_model.set_param_value(static_param_name, included_path)

        return (config_model, included_path)
Example #6
0
    def test_static_include_hidden_config(self):
        included_path = test_utils.write_script_config({
            'script_path': 'ping google.com',
            'hidden': True},
            'included')
        config_model = _create_config_model('main_conf', config={'include': included_path})

        self.assertEqual('ping google.com', config_model.script_command)
    def prepare_config_model_with_included(self, included_params, static_param_name):
        included_path = test_utils.write_script_config({'parameters': included_params}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': '${' + static_param_name + '}',
            'parameters': [create_script_param_config(static_param_name)]})
        config_model.set_param_value(static_param_name, included_path)

        return (config_model, included_path)
Example #8
0
    def test_xsrf_protection_when_token(self):
        self.start_server(12345, '127.0.0.1')

        test_utils.write_script_config({
            'name': 's1',
            'script_path': 'ls'
        }, 's1', self.runners_folder)
        response = self._user_session.get('http://127.0.0.1:12345/scripts')

        xsrf_token = response.cookies.get('_xsrf')
        start_response = self._user_session.post(
            'http://127.0.0.1:12345/executions/start',
            data={'__script_name': 's1'},
            files=[('notafile', None)],
            headers={'X-XSRFToken': xsrf_token},
            cookies=response.cookies)
        self.assertEqual(start_response.status_code, 200)
        self.assertEqual(start_response.content, b'3')
Example #9
0
    def test_dynamic_include_replace(self):
        included_path1 = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param_X')
        ]}, 'included1')

        included_path2 = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param_Y')
        ]}, 'included2')

        config_model = _create_config_model('main_conf', config={
            'include': '${p1}',
            'parameters': [create_script_param_config('p1')]})
        config_model.set_param_value('p1', included_path1)
        config_model.set_param_value('p1', included_path2)

        self.assertEqual(2, len(config_model.parameters))
        self.assertEqual('p1', config_model.parameters[0].name)
        self.assertEqual('included_param_Y', config_model.parameters[1].name)
Example #10
0
    def test_static_include_precedence(self):
        included_path = test_utils.write_script_config({
            'script_path': 'ping google.com',
            'working_directory': '123'},
            'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'working_directory': 'abc'})

        self.assertEqual('abc', config_model.working_directory)
Example #11
0
    def test_get_script_code(self):
        self.start_server(12345, '127.0.0.1')

        script_path = test_utils.create_file('my_script.py')
        test_utils.write_script_config(
            {
                'name': 's1',
                'script_path': script_path
            }, 's1', self.runners_folder)

        response = self.request(
            'get', 'http://127.0.0.1:12345/admin/scripts/s1/code',
            self._admin_session)

        self.assertEqual(
            {
                'code': 'test text',
                'file_path': os.path.abspath(script_path)
            }, response)
Example #12
0
    def test_xsrf_protection_when_header(self):
        self.start_server(12345,
                          '127.0.0.1',
                          xsrf_protection=XSRF_PROTECTION_HEADER)

        test_utils.write_script_config({
            'name': 's1',
            'script_path': 'ls'
        }, 's1', self.runners_folder)
        self._user_session.get('http://127.0.0.1:12345/scripts')

        start_response = self._user_session.post(
            'http://127.0.0.1:12345/executions/start',
            data={'__script_name': 's1'},
            files=[('notafile', None)],
            headers={'X-Requested-With': 'XMLHttpRequest'},
        )
        self.assertEqual(start_response.status_code, 200)
        self.assertEqual(start_response.content, b'3')
Example #13
0
    def test_static_include_precedence(self):
        included_path = test_utils.write_script_config({
            'script_path': 'ping google.com',
            'working_directory': '123'},
            'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'working_directory': 'abc'})

        self.assertEqual('abc', config_model.working_directory)
Example #14
0
    def test_static_include_ignore_same_parameter(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('param1', type='int', required=False)
        ]}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'parameters': [create_script_param_config('param1', type='text', required=True)]})

        self.assertEqual(1, len(config_model.parameters))
        param1 = config_model.parameters[0]
        self.assertEqual('text', param1.type)
        self.assertEqual(True, param1.required)
Example #15
0
    def test_dynamic_include_add_parameter(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param')
        ]}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': '${p1}',
            'parameters': [create_script_param_config('p1')]})
        config_model.set_param_value('p1', included_path)

        self.assertEqual(2, len(config_model.parameters))
        included_param = config_model.parameters[1]
        self.assertEqual('included_param', included_param.name)
Example #16
0
    def test_static_include_ignore_same_parameter(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('param1', type='int', required=False)
        ]}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'parameters': [create_script_param_config('param1', type='text', required=True)]})

        self.assertEqual(1, len(config_model.parameters))
        param1 = config_model.parameters[0]
        self.assertEqual('text', param1.type)
        self.assertEqual(True, param1.required)
    def create_config(self, name, scheduling_enabled=True, parameters=None):
        if parameters is None:
            parameters = [
                {
                    'name': 'p1'
                },
                {
                    'name': 'param_2',
                    'type': 'multiselect',
                    'values': ['hello', 'world', '1', '2', '3']
                },
            ]

        test_utils.write_script_config(
            {
                'name': name,
                'script_path': 'echo 1',
                'parameters': parameters,
                'scheduling': {
                    'enabled': scheduling_enabled
                }
            }, name)
Example #18
0
    def test_dynamic_include_replace_with_missing_file(self):
        included_path1 = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param_X')
        ]}, 'included1')

        config_model = _create_config_model('main_conf', config={
            'include': '${p1}',
            'parameters': [create_script_param_config('p1')]})
        config_model.set_param_value('p1', included_path1)
        config_model.set_param_value('p1', 'a/b/c/some.txt')

        self.assertEqual(1, len(config_model.parameters))
        self.assertEqual('p1', config_model.parameters[0].name)
Example #19
0
    def test_dynamic_include_replace(self):
        (config_model, included_path1) = self.prepare_config_model_with_included([
            create_script_param_config('included_param_X')
        ], 'p1')

        included_path2 = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param_Y')
        ]}, 'included2')

        config_model.set_param_value('p1', included_path2)

        self.assertEqual(2, len(config_model.parameters))
        self.assertEqual('p1', config_model.parameters[0].name)
        self.assertEqual('included_param_Y', config_model.parameters[1].name)
    def test_dynamic_include_replace(self):
        (config_model,
         included_path1) = self.prepare_config_model_with_included(
             [create_script_param_config('included_param_X')], 'p1')

        included_path2 = test_utils.write_script_config(
            {'parameters': [create_script_param_config('included_param_Y')]},
            'included2')

        config_model.set_param_value('p1', included_path2)

        self.assertEqual(2, len(config_model.parameters))
        self.assertEqual('p1', config_model.parameters[0].name)
        self.assertEqual('included_param_Y', config_model.parameters[1].name)
Example #21
0
    def test_set_all_values_for_included(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param1'),
            create_script_param_config('included_param2')
        ]}, 'included')
        config_model = _create_config_model(
            'main_conf',
            config={
                'include': '${p1}',
                'parameters': [create_script_param_config('p1')]})

        values = {'p1': included_path, 'included_param1': 'X', 'included_param2': 123}
        config_model.set_all_param_values(values)

        self.assertEqual(values, config_model.parameter_values)
Example #22
0
    def test_set_all_values_for_included(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param1'),
            create_script_param_config('included_param2')
        ]}, 'included')
        config_model = _create_config_model(
            'main_conf',
            config={
                'include': '${p1}',
                'parameters': [create_script_param_config('p1')]})

        values = {'p1': included_path, 'included_param1': 'X', 'included_param2': 123}
        config_model.set_all_param_values(values)

        self.assertEqual(values, config_model.parameter_values)
    def test_create_with_schedulable_true_and_included_secure_parameter(self):
        config_model = _create_config_model('some-name', config={
            'scheduling': {'enabled': True},
            'include': '${p1}',
            'parameters': [{'name': 'p1', 'secure': False}]
        })
        another_path = test_utils.write_script_config(
            {'parameters': [{'name': 'p2', 'secure': True}]},
            'another_config')

        self.assertTrue(config_model.schedulable)

        config_model.set_param_value('p1', another_path)

        self.assertFalse(config_model.schedulable)
Example #24
0
    def test_static_include_single_parameter(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('param2', type='int')
        ]}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'parameters': [create_script_param_config('param1', type='text')]})

        self.assertEqual(2, len(config_model.parameters))
        param1 = config_model.parameters[0]
        self.assertEqual('param1', param1.name)
        self.assertEqual('text', param1.type)

        param2 = config_model.parameters[1]
        self.assertEqual('param2', param2.name)
        self.assertEqual('int', param2.type)
Example #25
0
    def test_static_include_single_parameter(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('param2', type='int')
        ]}, 'included')
        config_model = _create_config_model('main_conf', config={
            'include': included_path,
            'parameters': [create_script_param_config('param1', type='text')]})

        self.assertEqual(2, len(config_model.parameters))
        param1 = config_model.parameters[0]
        self.assertEqual('param1', param1.name)
        self.assertEqual('text', param1.type)

        param2 = config_model.parameters[1]
        self.assertEqual('param2', param2.name)
        self.assertEqual('int', param2.type)
Example #26
0
    def test_dynamic_include_relative_path(self):
        folder = os.path.join(test_utils.temp_folder, 'inner', 'confs')

        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param')
        ]}, 'included', folder)
        included_folder = os.path.dirname(included_path)
        config_model = _create_config_model(
            'main_conf',
            path=os.path.join(folder, 'mainConf.json'),
            config={
                'include': '${p1}',
                'working_directory': included_folder,
                'parameters': [create_script_param_config('p1')]})
        config_model.set_param_value('p1', 'included.json')

        self.assertEqual(2, len(config_model.parameters))
Example #27
0
    def test_dynamic_include_relative_path(self):
        folder = os.path.join(test_utils.temp_folder, 'inner', 'confs')

        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param')
        ]}, 'included', folder)
        included_folder = os.path.dirname(included_path)
        config_model = _create_config_model(
            'main_conf',
            path=os.path.join(folder, 'mainConf.json'),
            config={
                'include': '${p1}',
                'working_directory': included_folder,
                'parameters': [create_script_param_config('p1')]})
        config_model.set_param_value('p1', 'included.json')

        self.assertEqual(2, len(config_model.parameters))
Example #28
0
    def test_dynamic_include_remove_multiple_parameters(self):
        included_path = test_utils.write_script_config({'parameters': [
            create_script_param_config('included_param1'),
            create_script_param_config('included_param2'),
            create_script_param_config('included_param3')
        ]}, 'included')
        config_model = _create_config_model(
            'main_conf',
            config={
                'include': '${p1}',
                'parameters': [create_script_param_config('p1')]},
            parameter_values={'p1': included_path})

        config_model.set_param_value('p1', '')

        self.assertEqual(1, len(config_model.parameters))
        included_param = config_model.parameters[0]
        self.assertEqual('p1', included_param.name)
Example #29
0
    def test_get_scripts(self):
        self.start_server(12345, '127.0.0.1')

        test_utils.write_script_config({'name': 's1'}, 's1',
                                       self.runners_folder)
        test_utils.write_script_config({
            'name': 's2',
            'group': 'Xyz'
        }, 's2', self.runners_folder)
        test_utils.write_script_config({'name': 's3'}, 's3',
                                       self.runners_folder)

        response = self.request('GET', 'http://127.0.0.1:12345/scripts')
        self.assertCountEqual([{
            'name': 's1',
            'group': None
        }, {
            'name': 's2',
            'group': 'Xyz'
        }, {
            'name': 's3',
            'group': None
        }], response['scripts'])