.
This commit is contained in:
@@ -56,8 +56,7 @@ def check_credentials(username, password, host="127.0.0.1", port="3306"):
|
||||
|
||||
salt '*' mysql.check_credentials root secret
|
||||
"""
|
||||
cmd = f"mysql --host={host} --port={port} --user={username} --password={password} --execute=;"
|
||||
return _execute(cmd)["result"]
|
||||
return execute(username, password, ";" , host=host, port=port)["result"]
|
||||
|
||||
|
||||
def set_root_password(password):
|
||||
@@ -70,6 +69,65 @@ def set_root_password(password):
|
||||
|
||||
salt '*' mysql.set_root_password secret
|
||||
"""
|
||||
ret= {}
|
||||
cmd = f"mysqladmin password {password}"
|
||||
return _execute(cmd)
|
||||
LOG.debug(f"Executing: {cmd}")
|
||||
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = process.communicate()
|
||||
if stdout:
|
||||
ret["out"] = stdout
|
||||
if stderr:
|
||||
ret["err"] = stderr
|
||||
LOG.debug(f"Exit code: { process.returncode }")
|
||||
if process.returncode == 0:
|
||||
ret["result"] = True
|
||||
else:
|
||||
ret["result"] = False
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
def execute(user, password, sql, host='127.0.0.1', port='3306'):
|
||||
"""
|
||||
Execute an SQL statement
|
||||
|
||||
:param str user: The user to connect with
|
||||
|
||||
:param str password: The user password
|
||||
|
||||
:param str host: the mysql server address
|
||||
|
||||
:param str sql: The SQL statement to be executed
|
||||
|
||||
:param str port: the mysql server port
|
||||
|
||||
CLI Example
|
||||
|
||||
salt '*' mysql.execute root secret "SHOW DATABASES;"
|
||||
"""
|
||||
|
||||
ret = {}
|
||||
cmd = [ "mysql",
|
||||
"--skip-column-names",
|
||||
"--silent",
|
||||
f"--user={user}",
|
||||
f"--password={password}",
|
||||
f"--host={host}",
|
||||
f"--port={port}",
|
||||
f"--execute={sql}"
|
||||
]
|
||||
|
||||
LOG.debug(f"Executing: {' '.join(cmd)}")
|
||||
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = process.communicate()
|
||||
if stdout:
|
||||
ret["out"] = stdout
|
||||
if stderr:
|
||||
ret["err"] = stderr
|
||||
LOG.debug(f"Exit code: { process.returncode }")
|
||||
if process.returncode == 0:
|
||||
ret["result"] = True
|
||||
else:
|
||||
ret["result"] = False
|
||||
return ret
|
||||
|
||||
|
@@ -27,3 +27,45 @@ def root_password(name, password):
|
||||
ret["changes"].update({"root password": {"old": "######", "new": "******"}})
|
||||
ret["result"]=True
|
||||
return ret
|
||||
|
||||
|
||||
def user(name, host, password):
|
||||
"""
|
||||
Set the mysql/mariadb root password
|
||||
|
||||
password
|
||||
the password to user for root
|
||||
"""
|
||||
ret = {
|
||||
"name": name,
|
||||
"changes": {},
|
||||
"result": False,
|
||||
"comment": ""
|
||||
}
|
||||
|
||||
#check if user@host exists
|
||||
result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'SELECT User, Host FROM mysql.user WHERE User="{name}" AND Host="{host}";')
|
||||
if not result["result"]:
|
||||
ret["comment"] = result["err"]
|
||||
return ret
|
||||
if "out" in result:
|
||||
#User exists check password
|
||||
if __salt__["mysql.check_credentials"](name, password):
|
||||
ret["comment"]=f"{name}@{host} is in correct state"
|
||||
ret["result"] = True
|
||||
return ret
|
||||
result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'ALTER USER "{name}"@"{host}" IDENTIFIED BY "{password}";')
|
||||
if not result["result"]:
|
||||
ret["comment"] = result["err"]
|
||||
return ret
|
||||
ret["changes"].update({"Password Updated": f"{name}@{host}"})
|
||||
ret["result"]=True
|
||||
return ret
|
||||
#Create user
|
||||
result = __salt__["mysql.execute"]("root", __pillar__['mysql']['root_password'],f'CREATE USER "{name}"@"{host}" IDENTIFIED BY "{password}";')
|
||||
if not result["result"]:
|
||||
ret["comment"] = result["err"]
|
||||
return ret
|
||||
ret["changes"].update({"Created user": f"{name}@{host}"})
|
||||
ret["result"]=True
|
||||
return ret
|
||||
|
Reference in New Issue
Block a user