mirror of
https://github.com/saltstack-formulas/mysql-formula.git
synced 2025-04-10 23:01:44 +00:00
import_users: PEP8, allow optional password via -p flag. Fix an issue
with trailing semicolon. Give more helpful warning if mysql-python package is missing.
This commit is contained in:
parent
bc2d65bd74
commit
a58db1fa4a
1 changed files with 75 additions and 51 deletions
126
import_users.py
126
import_users.py
|
@ -2,9 +2,13 @@
|
||||||
"This script helps you to get mysql.user pillar from existent mysql server"
|
"This script helps you to get mysql.user pillar from existent mysql server"
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import MySQLdb
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
try:
|
||||||
|
import MySQLdb
|
||||||
|
except ImportError:
|
||||||
|
raise Exception("MySQLdb not found. Install MySQL-python package.")
|
||||||
|
|
||||||
__author__ = "Egor Potiomkin"
|
__author__ = "Egor Potiomkin"
|
||||||
__version__ = "1.0"
|
__version__ = "1.0"
|
||||||
__email__ = "eg13reg@gmail.com"
|
__email__ = "eg13reg@gmail.com"
|
||||||
|
@ -12,11 +16,24 @@ __email__ = "eg13reg@gmail.com"
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('host', metavar='IP', help='host where you want to get users')
|
parser.add_argument('host', metavar='IP', help='host where you want to get users')
|
||||||
parser.add_argument('user', metavar='user', help='mysql user that can show grants')
|
parser.add_argument('user', metavar='user', help='mysql user that can show grants')
|
||||||
parser.add_argument('password', metavar='password', help='user password')
|
parser.add_argument('-p', '--password', metavar='password', help='user password', required=False, default=None)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# PARSE GRANTS
|
# PARSE GRANTS
|
||||||
mysqlcon = MySQLdb.connect(host=args.host,user=args.user,passwd=args.password,db="mysql",use_unicode=True, charset='utf8')
|
connection_config = {
|
||||||
|
"host": args.host,
|
||||||
|
"user": args.user,
|
||||||
|
"db": "mysql",
|
||||||
|
"use_unicode": True,
|
||||||
|
"charset": 'utf8'
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.password: # some mysql environments (developer ones) use no password
|
||||||
|
connection_config['passwd'] = args.password
|
||||||
|
|
||||||
|
mysqlcon = MySQLdb.connect(
|
||||||
|
**connection_config
|
||||||
|
)
|
||||||
mysqlCur = mysqlcon.cursor(MySQLdb.cursors.DictCursor)
|
mysqlCur = mysqlcon.cursor(MySQLdb.cursors.DictCursor)
|
||||||
|
|
||||||
mysqlCur.execute(r'''select user,host from mysql.user;''')
|
mysqlCur.execute(r'''select user,host from mysql.user;''')
|
||||||
|
@ -24,55 +41,62 @@ rows = mysqlCur.fetchall()
|
||||||
users = []
|
users = []
|
||||||
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
users.append({'name': row['user'], 'host': row['host']});
|
users.append({'name': row['user'], 'host': row['host']})
|
||||||
|
|
||||||
mysqlCur = mysqlcon.cursor()
|
mysqlCur = mysqlcon.cursor()
|
||||||
grants = []
|
grants = []
|
||||||
for user in users:
|
for user in users:
|
||||||
q = r'''show grants for '%s'@'%s';''' % (user['name'], user['host'])
|
q = r'''show grants for '%s'@'%s';''' % (user['name'], user['host'])
|
||||||
try:
|
try:
|
||||||
user['grants'] = []
|
user['grants'] = []
|
||||||
mysqlCur.execute(q)
|
mysqlCur.execute(q)
|
||||||
rows = mysqlCur.fetchall()
|
rows = mysqlCur.fetchall()
|
||||||
for row in rows:
|
for row in rows:
|
||||||
mpass = re.search(
|
mpass = re.search(
|
||||||
r"""GRANT USAGE ON \*\.\* TO .* IDENTIFIED BY PASSWORD '(\*[A-F0-9]*)\'""",
|
r"""GRANT USAGE ON \*\.\* TO .* IDENTIFIED BY PASSWORD '(\*[A-F0-9]*)\'""",
|
||||||
row[0])
|
row[0])
|
||||||
if mpass is None:
|
if mpass is None:
|
||||||
mgrant = re.search(
|
mgrant = re.search(
|
||||||
r"""GRANT ([\s,A-Z]+) ON `?([a-zA-Z0-9_\-*\\]*)`?\.`?([a-zA-Z0-9_\-*\\]*)`? TO .*""",
|
r"""GRANT ([\s,A-Z_]+) ON `?([a-zA-Z0-9_\-*\\]*)`?\.`?([a-zA-Z0-9_\-*\\]*)`? TO .*""",
|
||||||
row[0])
|
row[0])
|
||||||
if mgrant is not None:
|
if mgrant is not None:
|
||||||
user['grants'].append({'grant': [x.strip() for x in mgrant.group(1).split(',')], 'database': mgrant.group(2).replace('\\',''), 'table': mgrant.group(3).replace('\\','')})
|
user['grants'].append(
|
||||||
else:
|
{
|
||||||
print "ERROR: CAN NOT PARSE GRANTS: ",row[0]
|
'grant': [x.strip() for x in mgrant.group(1).split(',')],
|
||||||
else:
|
'database': mgrant.group(2).replace('\\', ''),
|
||||||
user['password'] = mpass.group(1)
|
'table': mgrant.group(3).replace('\\', '')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
print("ERROR: CAN NOT PARSE GRANTS: ", row[0])
|
||||||
|
else:
|
||||||
|
user['password'] = mpass.group(1)
|
||||||
|
|
||||||
except MySQLdb.DatabaseError:
|
except MySQLdb.DatabaseError:
|
||||||
print "Error while getting grants for '%s'@'%s'" % (user['name'], user['host'])
|
print(
|
||||||
#raise SystemExit
|
"Error while getting grants for '%s'@'%s'" % (user['name'], user['host'])
|
||||||
# PRINT RESULT
|
)
|
||||||
""" PRINT EXAMPLE
|
|
||||||
mysql:
|
""" PRINT EXAMPLE
|
||||||
user:
|
mysql:
|
||||||
username:
|
user:
|
||||||
host: host
|
username:
|
||||||
password_hash: '*2792A97371B2D17789364A22A9B35D180166571A'
|
host: host
|
||||||
databases:
|
password_hash: '*2792A97371B2D17789364A22A9B35D180166571A'
|
||||||
- database: testbase
|
databases:
|
||||||
table: table1
|
- database: testbase
|
||||||
grants: ['select']
|
table: table1
|
||||||
"""
|
grants: ['select']
|
||||||
print "mysql:"
|
"""
|
||||||
print " user:"
|
print("mysql:")
|
||||||
for user in users:
|
print(" user:")
|
||||||
print " %s:" % user['name']
|
for user in users:
|
||||||
print " host: '%s'" % user['host']
|
print(" %s:" % user['name'])
|
||||||
if ('password' in user):
|
print(" host: '%s'" % user['host'])
|
||||||
print " password_hash: '%s'" % user['password']
|
if ('password' in user):
|
||||||
print " databases:"
|
print(" password_hash: '%s'" % user['password'])
|
||||||
for grant in user['grants']:
|
print(" databases:")
|
||||||
print " - database: '%s'" % grant['database']
|
for grant in user['grants']:
|
||||||
print " table: '%s'" % grant['table']
|
print(" - database: '%s'" % grant['database'])
|
||||||
print " grants: ['%s']" % "','".join(grant['grant']).lower()
|
print(" table: '%s'" % grant['table'])
|
||||||
|
print(" grants: ['%s']" % "','".join(grant['grant']).lower())
|
||||||
|
|
Loading…
Add table
Reference in a new issue