def __init__(self, console_id, is_active):
     try:
         print('Please identify yourself.')
         username = input('Username: '******'Password: '******'\n')
         exit(2)
     db_driver = dataBaseDriver.dataBaseDriver('localhost', username,
                                               password, 'accontrol')
     try:
         info = db_driver.retrieve_info_from_username(username)[0]
     except IndexError as iderr:
         print('ERROR: ' + iderr.args)
         exit(1)
     except UnboundLocalError as uberr:
         print('ERROR: ' + uberr.args)
         exit(1)
     else:
         self.current_user = accessControlUser.acsuser(
             info.get('name'), info.get('MAC'), username, password)
         self.is_active = is_active
         self.id = console_id
         self.interpreter = interpreter.interpreter()
         self.interpreter.login(username, password)
         self.logger = logger.acsLogger()
         self.logger.set_warning()
    def test_execute_root_command_as_normal_user(self):
        command_test = "test"
        command_table_test = {"command": ("type", "order")}
        user_test = acs_module.acsuser("", "", "normal_user", "")

        result = executer.execute(None, user_test, command_test, None,
                                  command_table_test)
        assert result == 1
    def test_normal_user_can_run(self):
        command_test = "retrieve_description_from_group"
        user_test = acs_module.acsuser("", "", "normal_user", "")

        i = interpreter()
        i.current_user = user_test

        result = interpreter.user_can_run(i, command_test)
        assert result == True
    def test_normal_user_cannot_run(self):
        command_test = "normal user cannot run commands written anyway"
        user_test = acs_module.acsuser("", "", "normal_user", "")

        i = interpreter()
        i.current_user = user_test

        result = interpreter.user_can_run(i, command_test)
        assert result == False
    def test_user_can_run_as_root(self):
        command_test = "root can run command written in any way"
        user_test = acs_module.acsuser("", "", "root", "")

        i = interpreter()
        i.current_user = user_test

        result = interpreter.user_can_run(i, command_test)
        assert result == True
    def test_print_command_table_as_normal_user(self):
        user_test = acs_module.acsuser("", "", "normal_user", "")

        i = interpreter()
        i.current_user = user_test

        result = interpreter.print_command_table(i)
        assert result == ("check_access ()\n" +
                          "retrieve_description_from_group (number)\n" +
                          "retrieve_my_info ()\n")
    def test_execute_valid_command(self, exc_mock):
        exc_inst = mock.MagicMock()
        exc_inst.execute.return_value = "Mocked output"  # pretty print output
        user_test = acs_module.acsuser("", "", "root", "")
        command_test = "define_new_group(number=1,description=test)"  #just a valid command for parsing to complete

        i = interpreter()
        i.current_user = user_test
        i.command_executer = exc_inst

        result = interpreter.execute(i, command_test)
        assert result == ("Mocked output")
    def test_execute_root_type_command_as_root(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "retrieve_all_users"
        command_table_test = {
            "retrieve_all_users": ("root", "")
        }  #command type = root
        user_test = acs_module.acsuser("", "", "root", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, None, command_table_test)
        assert result == "Mocked Output"
    def test_execute_self_type_command_as_normal_user(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "retrieve_my_info"
        command_table_test = {
            "retrieve_my_info": ("self", "")
        }  #command type = self
        user_test = acs_module.acsuser("", "", "normal_user", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, None, command_table_test)
        assert result == "Mocked Output"
 def login(self, username, password):
     db_driver = dataBaseDriver.dataBaseDriver('localhost', username,
                                               password, 'accontrol')
     try:
         info = db_driver.retrieve_info_from_username(username)[0]
     except IndexError as iderr:
         print('ERROR: ' + iderr.args)
         exit(1)
     except UnboundLocalError as uberr:
         print('ERROR: ' + uberr.args)
         exit(1)
     else:
         self.current_user = accessControlUser.acsuser(
             info.get('name'), info.get('MAC'), username, password)
         self.command_executer = executer.executer(username, password)
    def test_execute_acsgroup_type_command_as_root(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "define_new_group"
        args_test = {"number": 1, "description": "test"}
        command_table_test = {
            "define_new_group": ("acsgroup", "")
        }  #command type = acsgroup
        user_test = acs_module.acsuser("", "", "root", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, args_test,
                           command_table_test)
        assert result == "Mocked Output"
    def test_execute_acsaccess_type_command_as_root(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "give_access"
        args_test = {"group_number": "1", "facility_name": "test"}
        command_table_test = {
            "give_access": ("acsaccess", "")
        }  #command type = acsaccess
        user_test = acs_module.acsuser("", "", "root", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, args_test,
                           command_table_test)
        assert result == "Mocked Output"
    def test_execute_str_type_command_as_normal_user(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "retrieve_description_from_group"
        args_test = {"number": 1}
        command_table_test = {
            "retrieve_description_from_group": ("str", "")
        }  #command type = str
        user_test = acs_module.acsuser("", "", "normal_user", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, args_test,
                           command_table_test)
        assert result == "Mocked Output"
Esempio n. 14
0
    def execute(self, current_user, command, args, command_table):

        try:
            command_arg_type, command_query_order = command_table.get(command)
        except TypeError:
            print('EXECUTER: Command has execution errors.')
            return 1

        if command_arg_type == 'root':
            if current_user.get_username() != 'root':
                return 1
            else:
                db_command_args = None
        elif command_arg_type == 'self':
            db_command_args = current_user.get_MAC()
        elif command_arg_type == 'acsgroup':
            db_command_args = accessControlUser.acsgroup(
                args.get('number'), args.get('description'))
        elif command_arg_type == 'acsuser':
            db_command_args = accessControlUser.acsuser(
                args.get('name'), args.get('MAC'), args.get('username'),
                args.get('password'), args.get('group_number'))
        elif command_arg_type == 'acsfacility':
            db_command_args = accessControlUser.acsfacility(args.get('name'))
        elif command_arg_type == 'acsaccess':
            db_command_args = accessControlUser.acsaccess(
                args.get('group_number'), args.get('facility_name'))
        else:
            # Commands that are not listed above have only one argument
            db_command_args = list(args.values())[0]

        try:
            if db_command_args is not None:
                result = getattr(self.db_driver, command)(db_command_args)
            else:
                result = getattr(self.db_driver, command)()
        except ValueError as verr:
            print('EXECUTER Value Error: ' + str(verr.args))
            return 1
        except TypeError as terr:
            print('EXECUTER Type Error: ' + str(terr.args))
            return 1
        if result is not None:
            return self.pretty_print(result)
    def test_print_command_table_as_root(self):
        user_test = acs_module.acsuser("", "", "root", "")

        i = interpreter()
        i.current_user = user_test

        result = interpreter.print_command_table(i)
        assert result == (
            "add_user_info (name, username, password, MAC)\n" +
            "change_group_description (description, number)\n" +
            "check_access ()\n" + "define_new_group (number, description)\n" +
            "edit_user (name, username, password, group_number, MAC)\n" +
            "give_access (group_number, facility_name)\n" +
            "insert_new_facility (name)\n" +
            "insert_new_user (name, MAC, username, password)\n" +
            "remove_access (group_number, facility_name)\n" +
            "remove_facility (name)\n" + "remove_group (number)\n" +
            "remove_user (MAC, username)\n" + "retrieve_all_users ()\n" +
            "retrieve_description_from_group (number)\n" +
            "retrieve_info_from_username (username)\n" +
            "retrieve_my_info ()\n")
    def test_execute_acsuser_type_command_as_root(self, dbDriver_mock):
        dbDriver_inst = mock.MagicMock()
        command_test = "insert_new_user"
        args_test = {
            "name": "test",
            "username": "******",
            "MAC": "test",
            "password": "******",
            "group_number": "teste"
        }
        command_table_test = {
            "insert_new_user": ("acsuser", "")
        }  #command type = acsuser
        user_test = acs_module.acsuser("", "", "root", "")

        e = skip_init(executer)
        e.db_driver = dbDriver_inst
        e.pretty_print = mock.Mock(return_value="Mocked Output")

        result = e.execute(user_test, command_test, args_test,
                           command_table_test)
        assert result == "Mocked Output"
 def test_construction(self):
     acsuser_instance = accessControlUser.acsuser('Dummy', '5632',
                                                  'dummy123', 'glauberman',
                                                  2, 35)
     self.assertIsInstance(acsuser_instance, accessControlUser.acsuser)
Esempio n. 18
0
def install_data_base():
    print('Setting up database for root.')
    try:
        root_name = input('Your name: ')
        password = getpass.getpass('New password: '******'Exiting script')
        interrupt_handler(sys.exc_info())

    try:
        start_mysql = ['sudo', '/etc/init.d/mysql', 'start']
        subprocess.run(start_mysql)
    except subprocess.CalledProcessError as err:
        build_logger.error('Could not start MYSQL.')
        print_process_error(err)
        exit(1)

    try:
        check_user_config = [
            'mysql', '-N', '-B', '-u', 'root', '-p' + password, '-e',
            '\"SELECT EXISTS(SELECT * FROM mysql.user WHERE user = \'root\');\"'
        ]
        subprocess.run(' '.join(check_user_config),
                       stderr=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       check=True,
                       shell=True)
    except subprocess.CalledProcessError:
        user_is_already_configured = False
    else:
        user_is_already_configured = True

    if not user_is_already_configured:
        SQL_querry = 'DROP USER \'root\'@\'localhost\'; CREATE USER \'root\'@\'localhost\' IDENTIFIED BY \'' + password + '\'; ' + 'GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'localhost\' WITH GRANT OPTION; ' + 'FLUSH PRIVILEGES;'
        root_cmd = 'sudo su -c \"/bin/sh\"'
        mysql_cmd = 'mysql -u root -e \"' + SQL_querry + '\"'
        with subprocess.Popen(root_cmd,
                              stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True) as root:
            root.stdin.write(mysql_cmd.encode())
            root.stdin.close()
            root.wait()

    my_env = os.environ.get('PYTHONPATH')
    SQL_querry = os.path.join(my_env, 'src', 'database_setup', 'setup.sql')
    mysql_cmd = 'mysql -u root -p' + password + ' < ' + SQL_querry
    try:
        setup = subprocess.run(mysql_cmd,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               shell=True)
    except subprocess.CalledProcessError as err:
        print_process_error(err)
    build_logger.info(setup.stdout.decode('utf-8'))

    data_base_connect = dataBaseDriver.dataBaseDriver('localhost', 'root',
                                                      password, 'accontrol')
    root_MAC = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
    root_user = accessControlUser.acsuser(str(root_name), root_MAC, 'root',
                                          str(password))
    data_base_connect.insert_new_user(root_user)
 def test_get_unencrypted_password(self):
     acsuser_instance = accessControlUser.acsuser('Dummy', '5632',
                                                  'dummy123', 'glauberman',
                                                  2, 35)
     self.assertEqual('glauberman',
                      acsuser_instance.get_unencrypted_password())
 def test_get_group_number(self):
     acsuser_instance = accessControlUser.acsuser('Dummy', '5632',
                                                  'dummy123', 'glauberman',
                                                  2, 35)
     self.assertEqual(2, acsuser_instance.get_group_number())