Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mysql-formula
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
salt
mysql-formula
Commits
ea83e7a2
Commit
ea83e7a2
authored
10 years ago
by
Nitin Madhok
Browse files
Options
Downloads
Plain Diff
Merge pull request #65 from tony/import_users_formatting
import_users.py tweaks
parents
bc2d65bd
a58db1fa
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
import_users.py
+75
-51
75 additions, 51 deletions
import_users.py
with
75 additions
and
51 deletions
import_users.py
+
75
−
51
View file @
ea83e7a2
...
@@ -2,9 +2,13 @@
...
@@ -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"
...
@@ -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,7 +41,7 @@ rows = mysqlCur.fetchall()
...
@@ -24,7 +41,7 @@ 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
=
[]
...
@@ -40,19 +57,26 @@ for user in users:
...
@@ -40,19 +57,26 @@ for user in users:
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
(
{
'
grant
'
:
[
x
.
strip
()
for
x
in
mgrant
.
group
(
1
).
split
(
'
,
'
)],
'
database
'
:
mgrant
.
group
(
2
).
replace
(
'
\\
'
,
''
),
'
table
'
:
mgrant
.
group
(
3
).
replace
(
'
\\
'
,
''
)
}
)
else
:
else
:
print
"
ERROR: CAN NOT PARSE GRANTS:
"
,
row
[
0
]
print
(
"
ERROR: CAN NOT PARSE GRANTS:
"
,
row
[
0
]
)
else
:
else
:
user
[
'
password
'
]
=
mpass
.
group
(
1
)
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
"""
PRINT EXAMPLE
mysql:
mysql:
user:
user:
...
@@ -64,15 +88,15 @@ mysql:
...
@@ -64,15 +88,15 @@ mysql:
table: table1
table: table1
grants: [
'
select
'
]
grants: [
'
select
'
]
"""
"""
print
"
mysql:
"
print
(
"
mysql:
"
)
print
"
user:
"
print
(
"
user:
"
)
for
user
in
users
:
for
user
in
users
:
print
"
%s:
"
%
user
[
'
name
'
]
print
(
"
%s:
"
%
user
[
'
name
'
]
)
print
"
host:
'
%s
'"
%
user
[
'
host
'
]
print
(
"
host:
'
%s
'"
%
user
[
'
host
'
]
)
if
(
'
password
'
in
user
):
if
(
'
password
'
in
user
):
print
"
password_hash:
'
%s
'"
%
user
[
'
password
'
]
print
(
"
password_hash:
'
%s
'"
%
user
[
'
password
'
]
)
print
"
databases:
"
print
(
"
databases:
"
)
for
grant
in
user
[
'
grants
'
]:
for
grant
in
user
[
'
grants
'
]:
print
"
- database:
'
%s
'"
%
grant
[
'
database
'
]
print
(
"
- database:
'
%s
'"
%
grant
[
'
database
'
]
)
print
"
table:
'
%s
'"
%
grant
[
'
table
'
]
print
(
"
table:
'
%s
'"
%
grant
[
'
table
'
]
)
print
"
grants: [
'
%s
'
]
"
%
"'
,
'"
.
join
(
grant
[
'
grant
'
]).
lower
()
print
(
"
grants: [
'
%s
'
]
"
%
"'
,
'"
.
join
(
grant
[
'
grant
'
]).
lower
()
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment