|
2 | 2 | "This script helps you to get mysql.user pillar from existent mysql server"
|
3 | 3 |
|
4 | 4 | import argparse
|
5 |
| -import MySQLdb |
6 | 5 | import re
|
7 | 6 |
|
| 7 | +try: |
| 8 | + import MySQLdb |
| 9 | +except ImportError: |
| 10 | + raise Exception("MySQLdb not found. Install MySQL-python package.") |
| 11 | + |
8 | 12 | __author__ = "Egor Potiomkin"
|
9 | 13 | __version__ = "1.0"
|
10 | 14 |
|
11 | 15 |
|
12 | 16 | parser = argparse.ArgumentParser()
|
13 | 17 | parser.add_argument('host', metavar='IP', help='host where you want to get users')
|
14 | 18 | parser.add_argument('user', metavar='user', help='mysql user that can show grants')
|
15 |
| -parser.add_argument('password', metavar='password', help='user password') |
| 19 | +parser.add_argument('-p', '--password', metavar='password', help='user password', required=False, default=None) |
16 | 20 | args = parser.parse_args()
|
17 | 21 |
|
18 |
| -# PARSE GRANTS |
19 |
| -mysqlcon = MySQLdb.connect(host=args.host,user=args.user,passwd=args.password,db="mysql",use_unicode=True, charset='utf8') |
| 22 | +# PARSE GRANTS |
| 23 | +connection_config = { |
| 24 | + "host": args.host, |
| 25 | + "user": args.user, |
| 26 | + "db": "mysql", |
| 27 | + "use_unicode": True, |
| 28 | + "charset": 'utf8' |
| 29 | +} |
| 30 | + |
| 31 | +if args.password: # some mysql environments (developer ones) use no password |
| 32 | + connection_config['passwd'] = args.password |
| 33 | + |
| 34 | +mysqlcon = MySQLdb.connect( |
| 35 | + **connection_config |
| 36 | +) |
20 | 37 | mysqlCur = mysqlcon.cursor(MySQLdb.cursors.DictCursor)
|
21 | 38 |
|
22 | 39 | mysqlCur.execute(r'''select user,host from mysql.user;''')
|
23 | 40 | rows = mysqlCur.fetchall()
|
24 | 41 | users = []
|
25 | 42 |
|
26 | 43 | for row in rows:
|
27 |
| - users.append({'name': row['user'], 'host': row['host']}); |
| 44 | + users.append({'name': row['user'], 'host': row['host']}) |
28 | 45 |
|
29 | 46 | mysqlCur = mysqlcon.cursor()
|
30 | 47 | grants = []
|
31 | 48 | for user in users:
|
32 |
| - q = r'''show grants for '%s'@'%s';''' % (user['name'], user['host']) |
33 |
| - try: |
34 |
| - user['grants'] = [] |
35 |
| - mysqlCur.execute(q) |
36 |
| - rows = mysqlCur.fetchall() |
37 |
| - for row in rows: |
38 |
| - mpass = re.search( |
39 |
| - r"""GRANT USAGE ON \*\.\* TO .* IDENTIFIED BY PASSWORD '(\*[A-F0-9]*)\'""", |
40 |
| - row[0]) |
41 |
| - if mpass is None: |
42 |
| - mgrant = re.search( |
43 |
| - r"""GRANT ([\s,A-Z]+) ON `?([a-zA-Z0-9_\-*\\]*)`?\.`?([a-zA-Z0-9_\-*\\]*)`? TO .*""", |
44 |
| - row[0]) |
45 |
| - if mgrant is not None: |
46 |
| - user['grants'].append({'grant': [x.strip() for x in mgrant.group(1).split(',')], 'database': mgrant.group(2).replace('\\',''), 'table': mgrant.group(3).replace('\\','')}) |
47 |
| - else: |
48 |
| - print "ERROR: CAN NOT PARSE GRANTS: ",row[0] |
49 |
| - else: |
50 |
| - user['password'] = mpass.group(1) |
| 49 | + q = r'''show grants for '%s'@'%s';''' % (user['name'], user['host']) |
| 50 | + try: |
| 51 | + user['grants'] = [] |
| 52 | + mysqlCur.execute(q) |
| 53 | + rows = mysqlCur.fetchall() |
| 54 | + for row in rows: |
| 55 | + mpass = re.search( |
| 56 | + r"""GRANT USAGE ON \*\.\* TO .* IDENTIFIED BY PASSWORD '(\*[A-F0-9]*)\'""", |
| 57 | + row[0]) |
| 58 | + if mpass is None: |
| 59 | + mgrant = re.search( |
| 60 | + r"""GRANT ([\s,A-Z_]+) ON `?([a-zA-Z0-9_\-*\\]*)`?\.`?([a-zA-Z0-9_\-*\\]*)`? TO .*""", |
| 61 | + row[0]) |
| 62 | + if mgrant is not None: |
| 63 | + user['grants'].append( |
| 64 | + { |
| 65 | + 'grant': [x.strip() for x in mgrant.group(1).split(',')], |
| 66 | + 'database': mgrant.group(2).replace('\\', ''), |
| 67 | + 'table': mgrant.group(3).replace('\\', '') |
| 68 | + } |
| 69 | + ) |
| 70 | + else: |
| 71 | + print("ERROR: CAN NOT PARSE GRANTS: ", row[0]) |
| 72 | + else: |
| 73 | + user['password'] = mpass.group(1) |
51 | 74 |
|
52 |
| - except MySQLdb.DatabaseError: |
53 |
| - print "Error while getting grants for '%s'@'%s'" % (user['name'], user['host']) |
54 |
| -#raise SystemExit |
55 |
| -# PRINT RESULT |
56 |
| -""" PRINT EXAMPLE |
57 |
| -mysql: |
58 |
| - user: |
59 |
| - username: |
60 |
| - host: host |
61 |
| - password_hash: '*2792A97371B2D17789364A22A9B35D180166571A' |
62 |
| - databases: |
63 |
| - - database: testbase |
64 |
| - table: table1 |
65 |
| - grants: ['select'] |
66 |
| -""" |
67 |
| -print "mysql:" |
68 |
| -print " user:" |
69 |
| -for user in users: |
70 |
| - print " %s:" % user['name'] |
71 |
| - print " host: '%s'" % user['host'] |
72 |
| - if ('password' in user): |
73 |
| - print " password_hash: '%s'" % user['password'] |
74 |
| - print " databases:" |
75 |
| - for grant in user['grants']: |
76 |
| - print " - database: '%s'" % grant['database'] |
77 |
| - print " table: '%s'" % grant['table'] |
78 |
| - print " grants: ['%s']" % "','".join(grant['grant']).lower() |
| 75 | + except MySQLdb.DatabaseError: |
| 76 | + print( |
| 77 | + "Error while getting grants for '%s'@'%s'" % (user['name'], user['host']) |
| 78 | + ) |
| 79 | + |
| 80 | + """ PRINT EXAMPLE |
| 81 | + mysql: |
| 82 | + user: |
| 83 | + username: |
| 84 | + host: host |
| 85 | + password_hash: '*2792A97371B2D17789364A22A9B35D180166571A' |
| 86 | + databases: |
| 87 | + - database: testbase |
| 88 | + table: table1 |
| 89 | + grants: ['select'] |
| 90 | + """ |
| 91 | + print("mysql:") |
| 92 | + print(" user:") |
| 93 | + for user in users: |
| 94 | + print(" %s:" % user['name']) |
| 95 | + print(" host: '%s'" % user['host']) |
| 96 | + if ('password' in user): |
| 97 | + print(" password_hash: '%s'" % user['password']) |
| 98 | + print(" databases:") |
| 99 | + for grant in user['grants']: |
| 100 | + print(" - database: '%s'" % grant['database']) |
| 101 | + print(" table: '%s'" % grant['table']) |
| 102 | + print(" grants: ['%s']" % "','".join(grant['grant']).lower()) |
0 commit comments