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_construction(self):
     database = dataBaseDriver.dataBaseDriver('localhost', 'root',
                                              'jotaquest', 'accontrol')
     try:
         mysqlConnector_instance = dataBaseDriver.mysqlConnector(database)
     except pymysql.OperationalError:
         self.fail('Could not connect to database.')
     else:
         self.assertIsInstance(mysqlConnector_instance,
                               dataBaseDriver.mysqlConnector)
 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_query_execution(self):
     TEST_QUERRY = "SELECT `id`,`name`, `MAC`, `username`,`group_number` FROM `users`"
     database = dataBaseDriver.dataBaseDriver('localhost', 'root',
                                              'jotaquest', 'accontrol')
     try:
         mysqlConnector_instance = dataBaseDriver.mysqlConnector(database)
     except pymysql.OperationalError:
         self.fail('Could not connect to database.')
     else:
         try:
             querry_result = mysqlConnector_instance.execute_query(
                 TEST_QUERRY)
         except:
             self.fail('Could not execute querry.')
         else:
             self.assertIsNotNone(querry_result)
Beispiel #5
0
 def __init__(self, username, password):
     self.db_driver = dataBaseDriver.dataBaseDriver('localhost', username,
                                                    password, 'accontrol')
Beispiel #6
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)