@@ -201,7 +201,7 @@ def is_hash(password):
201
201
202
202
def user_mod (cursor , user , host , host_all , password , encrypted ,
203
203
plugin , plugin_hash_string , plugin_auth_string , new_priv ,
204
- append_privs , subtract_privs , tls_requires , module , role = False , maria_role = False ):
204
+ append_privs , subtract_privs , tls_requires , module , role = False ):
205
205
changed = False
206
206
msg = "User unchanged"
207
207
grant_option = False
@@ -323,7 +323,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
323
323
324
324
# Handle privileges
325
325
if new_priv is not None :
326
- curr_priv = privileges_get (cursor , user , host , maria_role )
326
+ curr_priv = privileges_get (cursor , user , host )
327
327
328
328
# If the user has privileges on a db.table that doesn't appear at all in
329
329
# the new specification, then revoke all privileges on it.
@@ -337,7 +337,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
337
337
msg = "Privileges updated"
338
338
if module .check_mode :
339
339
return {'changed' : True , 'msg' : msg , 'password_changed' : password_changed }
340
- privileges_revoke (cursor , user , host , db_table , priv , grant_option , maria_role )
340
+ privileges_revoke (cursor , user , host , db_table , priv , grant_option )
341
341
changed = True
342
342
343
343
# If the user doesn't currently have any privileges on a db.table, then
@@ -348,7 +348,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
348
348
msg = "New privileges granted"
349
349
if module .check_mode :
350
350
return {'changed' : True , 'msg' : msg , 'password_changed' : password_changed }
351
- privileges_grant (cursor , user , host , db_table , priv , tls_requires , maria_role )
351
+ privileges_grant (cursor , user , host , db_table , priv , tls_requires )
352
352
changed = True
353
353
354
354
# If the db.table specification exists in both the user's current privileges
@@ -390,12 +390,12 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
390
390
if module .check_mode :
391
391
return {'changed' : True , 'msg' : msg , 'password_changed' : password_changed }
392
392
if len (revoke_privs ) > 0 :
393
- privileges_revoke (cursor , user , host , db_table , revoke_privs , grant_option , maria_role )
393
+ privileges_revoke (cursor , user , host , db_table , revoke_privs , grant_option )
394
394
if len (grant_privs ) > 0 :
395
- privileges_grant (cursor , user , host , db_table , grant_privs , tls_requires , maria_role )
395
+ privileges_grant (cursor , user , host , db_table , grant_privs , tls_requires )
396
396
397
397
# after privilege manipulation, compare privileges from before and now
398
- after_priv = privileges_get (cursor , user , host , maria_role )
398
+ after_priv = privileges_get (cursor , user , host )
399
399
changed = changed or (curr_priv != after_priv )
400
400
401
401
if role :
@@ -454,7 +454,7 @@ def user_get_hostnames(cursor, user):
454
454
return hostnames
455
455
456
456
457
- def privileges_get (cursor , user , host , maria_role = False ):
457
+ def privileges_get (cursor , user , host ):
458
458
""" MySQL doesn't have a better method of getting privileges aside from the
459
459
SHOW GRANTS query syntax, which requires us to then parse the returned string.
460
460
Here's an example of the string that is returned from MySQL:
@@ -465,10 +465,8 @@ def privileges_get(cursor, user, host, maria_role=False):
465
465
The dictionary format is the same as that returned by privileges_unpack() below.
466
466
"""
467
467
output = {}
468
- if not maria_role :
469
- cursor .execute ("SHOW GRANTS FOR %s@%s" , (user , host ))
470
- else :
471
- cursor .execute ("SHOW GRANTS FOR %s" , (user ,))
468
+ query = "SHOW GRANTS FOR '%s'@'%s'" % (user , host )
469
+ cursor .execute (query )
472
470
grants = cursor .fetchall ()
473
471
474
472
def pick (x ):
@@ -478,10 +476,10 @@ def pick(x):
478
476
return x
479
477
480
478
for grant in grants :
481
- if not maria_role :
479
+ if get_server_type ( cursor ) == 'mariadb' :
482
480
res = re .match ("""GRANT (.+) ON (.+) TO (['`"]).*\\ 3@(['`"]).*\\ 4( IDENTIFIED BY PASSWORD (['`"]).+\\ 6)? ?(.*)""" , grant [0 ])
483
481
else :
484
- res = re .match ("""GRANT (.+) ON (.+) TO (['`"]).*\\ 3""" , grant [0 ])
482
+ res = re .match ("""GRANT (.+) ON (.+) TO (['`"]).*\\ 3 ?(.*) """ , grant [0 ])
485
483
486
484
if res is None :
487
485
# If a user has roles assigned, we'll have one of priv tuples looking like
@@ -490,7 +488,7 @@ def pick(x):
490
488
# As we use the mysql_role module to manipulate roles
491
489
# we just ignore such privs below:
492
490
res = re .match ("""GRANT (.+) TO (['`"]).*""" , grant [0 ])
493
- if not maria_role and res :
491
+ if res :
494
492
continue
495
493
496
494
raise InvalidPrivsError ('unable to parse the MySQL grant string: %s' % grant [0 ])
@@ -505,10 +503,14 @@ def pick(x):
505
503
# Determine if there's a case similar to the above:
506
504
privileges = normalize_col_grants (privileges )
507
505
508
- if not maria_role :
506
+ if get_server_type ( cursor ) == 'mariadb' :
509
507
if "WITH GRANT OPTION" in res .group (7 ):
510
508
privileges .append ('GRANT' )
509
+ else :
510
+ if "WITH GRANT OPTION" in res .group (4 ):
511
+ privileges .append ('GRANT' )
511
512
db = res .group (2 )
513
+
512
514
output .setdefault (db , []).extend (privileges )
513
515
return output
514
516
@@ -684,48 +686,33 @@ def privileges_unpack(priv, mode, column_case_sensitive, ensure_usage=True):
684
686
return output
685
687
686
688
687
- def privileges_revoke (cursor , user , host , db_table , priv , grant_option , maria_role = False ):
689
+ def privileges_revoke (cursor , user , host , db_table , priv , grant_option ):
688
690
# Escape '%' since mysql db.execute() uses a format string
689
691
db_table = db_table .replace ('%' , '%%' )
690
692
if grant_option :
691
693
query = ["REVOKE GRANT OPTION ON %s" % db_table ]
692
- if not maria_role :
693
- query .append ("FROM %s@%s" )
694
- else :
695
- query .append ("FROM %s" )
696
-
694
+ query .append ("FROM %s@%s" )
697
695
query = ' ' .join (query )
698
696
cursor .execute (query , (user , host ))
699
697
priv_string = "," .join ([p for p in priv if p not in ('GRANT' , )])
700
698
701
699
if priv_string != "" :
702
700
query = ["REVOKE %s ON %s" % (priv_string , db_table )]
703
-
704
- if not maria_role :
705
- query .append ("FROM %s@%s" )
706
- params = (user , host )
707
- else :
708
- query .append ("FROM %s" )
709
- params = (user ,)
710
-
701
+ query .append ("FROM %s@%s" )
711
702
query = ' ' .join (query )
712
- cursor .execute (query , params )
703
+ cursor .execute (query , ( user , host ) )
713
704
cursor .execute ("FLUSH PRIVILEGES" )
714
705
715
706
716
- def privileges_grant (cursor , user , host , db_table , priv , tls_requires , maria_role = False ):
707
+ def privileges_grant (cursor , user , host , db_table , priv , tls_requires ):
717
708
# Escape '%' since mysql db.execute uses a format string and the
718
709
# specification of db and table often use a % (SQL wildcard)
719
710
db_table = db_table .replace ('%' , '%%' )
720
711
priv_string = "," .join ([p for p in priv if p not in ('GRANT' , )])
721
712
query = ["GRANT %s ON %s" % (priv_string , db_table )]
722
713
723
- if not maria_role :
724
- query .append ("TO %s@%s" )
725
- params = (user , host )
726
- else :
727
- query .append ("TO %s" )
728
- params = (user )
714
+ query .append ("TO %s@%s" )
715
+ params = (user , host )
729
716
730
717
if tls_requires and impl .use_old_user_mgmt (cursor ):
731
718
query , params = mogrify_requires (" " .join (query ), params , tls_requires )
@@ -851,8 +838,7 @@ def limit_resources(module, cursor, user, host, resource_limits, check_mode):
851
838
module .fail_json (msg = "The server version does not match the requirements "
852
839
"for resource_limits parameter. See module's documentation." )
853
840
854
- cursor .execute ("SELECT VERSION()" )
855
- if 'mariadb' not in cursor .fetchone ()[0 ].lower ():
841
+ if get_server_type (cursor ) != 'mariadb' :
856
842
if 'MAX_STATEMENT_TIME' in resource_limits :
857
843
module .fail_json (msg = "MAX_STATEMENT_TIME resource limit is only supported by MariaDB." )
858
844
@@ -879,8 +865,8 @@ def limit_resources(module, cursor, user, host, resource_limits, check_mode):
879
865
880
866
def get_impl (cursor ):
881
867
global impl
882
- cursor . execute ( "SELECT VERSION()" )
883
- if 'mariadb' in cursor . fetchone ()[ 0 ]. lower () :
868
+
869
+ if get_server_type ( cursor ) == 'mariadb' :
884
870
from ansible_collections .community .mysql .plugins .module_utils .implementations .mariadb import user as mariauser
885
871
impl = mariauser
886
872
else :
0 commit comments