def test_parse_user_host(self): """Tests parse_user_host method """ self.assertEqual(parse_user_host("user@host"), ("user", "host")) self.assertEqual(parse_user_host("'user'@'host'"), ("user", "host")) with self.assertRaises(GadgetCnxFormatError) as err: parse_user_host("'user'") self.assertIn("Cannot parse", err.exception.errmsg)
def drop(self, new_user=None): """Drop user from the server Attempts to drop the user. :param new_user: MySQL user account name (user@host) to drop. If omitted then the operation is performed using the user name value defined for the User instance. :type new_user: string :return: True if the user was drop successfully and False if an error occurred (could not drop user). :rtype: boolean """ query_str = "DROP USER " if new_user: user, host = parse_user_host(new_user) query_str += "'{0}'@'{1}' ".format(user, host) else: query_str += "'{0}'@'{1}' ".format(self.user, self.host) if self.verbosity > 0: _LOGGER.debug("%s", query_str) try: self.server1.exec_query(query_str, self.query_options) except GadgetError: return False return True
def __init__(self, server1, user, passwd=None, verbosity=0): """Constructor :param server1: Server instance :type server1: Server :param user: MySQL user account name (user@host). :type user: string :param passwd: Password for the user. By default, None (no password). :type passwd: string :param verbosity: Verbosity level to log extra information during operations. By default, 0 (no extra information logged). :type verbosity: integer """ self.server1 = server1 self.sql_mode = '' self.user, self.host = parse_user_host(user) self.host = clean_IPv6(self.host) self.passwd = passwd self.verbosity = verbosity self.current_user = None self.grant_dict = None self.global_grant_dict = None self.grant_list = None self.global_grant_list = None self.query_options = { 'fetch': False }
def create(self, new_user=None, passwd=None, ssl=False, disable_binlog=False): """Create the user Attempts to create the user. :param new_user: MySQL user string (user@host) (optional) If omitted, operation is performed on the class instance user name. :type new_user: string in the form "user@host". :param passwd: password for user to create (optional). :type passwd: string. :param ssl: If True the grant will include 'REQUIRE SSL' (Default False). :type ssl: Boolean, if True set user requires ssl to login. :param disable_binlog: Turn of the binary while creating the user. :type disable_binlog: Boolean, if True turn off the binary log. """ query_str = "CREATE USER IF NOT EXISTS " if new_user: user, host = parse_user_host(new_user) query_str += "'{0}'@'{1}' ".format(user, host) else: query_str += "'{0}'@'{1}' ".format(self.user, self.host) passwd = self.passwd if passwd: query_str += "IDENTIFIED BY '{0}'".format(passwd) if ssl: query_str = "{0} {1}".format(query_str, " REQUIRE SSL") # Turn off binlog binlog_disabled = False if disable_binlog and self.server1.binlog_enabled(): self.server1.toggle_binlog(action="disable") binlog_disabled = True # Create the user at the server query_to_log = query_str if passwd: query_to_log = query_str.replace( "IDENTIFIED BY '{0}'".format(passwd), "IDENTIFIED BY '******'") self.server1.exec_query(query_str, self.query_options, query_to_log=query_to_log) # Turn ON binlog only if it was disabled before if disable_binlog and binlog_disabled: self.server1.toggle_binlog(action="enable")
def exists(self, user_name=None): """Check to see if the user exists :param user_name: MySQL user account name (user@host) to check. If omitted then the operation is performed using the user name value defined for the User instance. :type user_name: string :return: True if user exists, and False if the user does not exist. :rtype: boolean """ user, host = self.user, self.host if user_name: user, host = parse_user_host(user_name) res = self.server1.exec_query("SELECT * FROM mysql.user " "WHERE user = ? and host = ?", {'params': (user, host)}) return res is not None and len(res) >= 1