Exemplo n.º 1
0
 def update_attributes(self, username, hostname, user_attrs):
     """Change the attributes of an existing user."""
     LOG.debug("Changing user attributes for user %s." % username)
     user = self._get_user(username, hostname)
     db_access = set()
     grantee = set()
     with LocalSqlClient(get_engine()) as client:
         q = sql_query.Query()
         q.columns = ["grantee", "table_schema"]
         q.tables = ["information_schema.SCHEMA_PRIVILEGES"]
         q.group = ["grantee", "table_schema"]
         q.where = ["privilege_type != 'USAGE'"]
         t = text(str(q))
         db_result = client.execute(t)
         for db in db_result:
             grantee.add(db['grantee'])
             if db['grantee'] == "'%s'@'%s'" % (user.name, user.host):
                 db_name = db['table_schema']
                 db_access.add(db_name)
     with LocalSqlClient(get_engine()) as client:
         uu = sql_query.UpdateUser(user.name, host=user.host,
                                   clear=user_attrs.get('password'),
                                   new_user=user_attrs.get('name'),
                                   new_host=user_attrs.get('host'))
         t = text(str(uu))
         client.execute(t)
         uname = user_attrs.get('name') or username
         host = user_attrs.get('host') or hostname
         find_user = "******" % (uname, host)
         if find_user not in grantee:
             self.grant_access(uname, host, db_access)
Exemplo n.º 2
0
 def _generate_root_password(client):
     """Generate and set a random root password and forget about it."""
     localhost = "localhost"
     uu = sql_query.UpdateUser("root", host=localhost,
                               clear=utils.generate_random_password())
     t = text(str(uu))
     client.execute(t)
Exemplo n.º 3
0
    def enable_root(self, root_password=None):
        """Enable the root user global access and/or
           reset the root password.
        """
        user = models.MySQLRootUser(root_password)
        with self.local_sql_client(self.mysql_app.get_engine()) as client:
            print(client)
            try:
                cu = sql_query.CreateUser(user.name, host=user.host)
                t = text(str(cu))
                client.execute(t, **cu.keyArgs)
            except exc.OperationalError as err:
                # Ignore, user is already created, just reset the password
                # TODO(rnirmal): More fine grained error checking later on
                LOG.debug(err)
        with self.local_sql_client(self.mysql_app.get_engine()) as client:
            print(client)
            uu = sql_query.UpdateUser(user.name,
                                      host=user.host,
                                      clear=user.password)
            t = text(str(uu))
            client.execute(t)

            LOG.debug("CONF.root_grant: %s CONF.root_grant_option: %s." %
                      (CONF.root_grant, CONF.root_grant_option))

            g = sql_query.Grant(permissions=CONF.root_grant,
                                user=user.name,
                                host=user.host,
                                grant_option=CONF.root_grant_option,
                                clear=user.password)

            t = text(str(g))
            client.execute(t)
            return user.serialize()
Exemplo n.º 4
0
 def test_change_host(self):
     username = '******'
     hostname = 'localhost'
     new_host = '%'
     uu = sql_query.UpdateUser(user=username,
                               host=hostname,
                               new_host=new_host)
     self.assertEqual(
         "UPDATE mysql.user SET Host='%' "
         "WHERE User = '******' "
         "AND Host = 'localhost';", str(uu))
Exemplo n.º 5
0
 def test_rename_user(self):
     username = '******'
     hostname = 'localhost'
     new_user = '******'
     uu = sql_query.UpdateUser(user=username,
                               host=hostname,
                               new_user=new_user)
     self.assertEqual(
         "UPDATE mysql.user SET User='******' "
         "WHERE User = '******' "
         "AND Host = 'localhost';", str(uu))
Exemplo n.º 6
0
 def test_change_password(self):
     username = '******'
     hostname = 'localhost'
     new_password = '******'
     uu = sql_query.UpdateUser(user=username,
                               host=hostname,
                               clear=new_password)
     self.assertEqual(
         "UPDATE mysql.user SET "
         "Password=PASSWORD('password123') "
         "WHERE User = '******' "
         "AND Host = 'localhost';", str(uu))
Exemplo n.º 7
0
 def test_change_username_and_hostname(self):
     username = '******'
     hostname = 'localhost'
     new_user = '******'
     new_host = '%'
     uu = sql_query.UpdateUser(user=username,
                               host=hostname,
                               new_host=new_host,
                               new_user=new_user)
     self.assertEqual(
         str(uu), "UPDATE mysql.user SET User='******', "
         "Host='%' "
         "WHERE User = '******' "
         "AND Host = 'localhost';")
Exemplo n.º 8
0
 def change_passwords(self, users):
     """Change the passwords of one or more existing users."""
     LOG.debug("Changing the password of some users.")
     with LocalSqlClient(get_engine()) as client:
         for item in users:
             LOG.debug("Changing password for user %s." % item)
             user_dict = {'_name': item['name'],
                          '_host': item['host'],
                          '_password': item['password']}
             user = models.MySQLUser()
             user.deserialize(user_dict)
             LOG.debug("\tDeserialized: %s." % user.__dict__)
             uu = sql_query.UpdateUser(user.name, host=user.host,
                                       clear=user.password)
             t = text(str(uu))
             client.execute(t)
Exemplo n.º 9
0
 def test_change_username_password_hostname(self):
     username = '******'
     hostname = 'localhost'
     new_user = '******'
     new_password = '******'
     new_host = '%'
     uu = sql_query.UpdateUser(user=username,
                               host=hostname,
                               clear=new_password,
                               new_user=new_user,
                               new_host=new_host)
     self.assertEqual(
         str(uu), "UPDATE mysql.user SET User='******', "
         "Host='%', "
         "Password=PASSWORD('password123') "
         "WHERE User = '******' "
         "AND Host = 'localhost';")