Skip to content

Commit a7ea113

Browse files
cut option maria_role as it as nothing to do with roles
This was introduced in ansible-collections#189. To my knowledge, there is no difference between MySQL and MariaDB regarding roles or when you call a user by its name alone. Both works if the host it '%'. Same for roles.
1 parent 033b4c7 commit a7ea113

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

plugins/module_utils/user.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def is_hash(password):
201201

202202
def user_mod(cursor, user, host, host_all, password, encrypted,
203203
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):
205205
changed = False
206206
msg = "User unchanged"
207207
grant_option = False
@@ -323,7 +323,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
323323

324324
# Handle privileges
325325
if new_priv is not None:
326-
curr_priv = privileges_get(cursor, user, host, maria_role)
326+
curr_priv = privileges_get(cursor, user, host)
327327

328328
# If the user has privileges on a db.table that doesn't appear at all in
329329
# the new specification, then revoke all privileges on it.
@@ -337,7 +337,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
337337
msg = "Privileges updated"
338338
if module.check_mode:
339339
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)
341341
changed = True
342342

343343
# 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,
348348
msg = "New privileges granted"
349349
if module.check_mode:
350350
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)
352352
changed = True
353353

354354
# 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,
390390
if module.check_mode:
391391
return {'changed': True, 'msg': msg, 'password_changed': password_changed}
392392
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)
394394
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)
396396

397397
# 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)
399399
changed = changed or (curr_priv != after_priv)
400400

401401
if role:
@@ -454,7 +454,7 @@ def user_get_hostnames(cursor, user):
454454
return hostnames
455455

456456

457-
def privileges_get(cursor, user, host, maria_role=False):
457+
def privileges_get(cursor, user, host):
458458
""" MySQL doesn't have a better method of getting privileges aside from the
459459
SHOW GRANTS query syntax, which requires us to then parse the returned string.
460460
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):
465465
The dictionary format is the same as that returned by privileges_unpack() below.
466466
"""
467467
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)
472470
grants = cursor.fetchall()
473471

474472
def pick(x):
@@ -478,10 +476,10 @@ def pick(x):
478476
return x
479477

480478
for grant in grants:
481-
if not maria_role:
479+
if get_server_type(cursor) == 'mariadb':
482480
res = re.match("""GRANT (.+) ON (.+) TO (['`"]).*\\3@(['`"]).*\\4( IDENTIFIED BY PASSWORD (['`"]).+\\6)? ?(.*)""", grant[0])
483481
else:
484-
res = re.match("""GRANT (.+) ON (.+) TO (['`"]).*\\3""", grant[0])
482+
res = re.match("""GRANT (.+) ON (.+) TO (['`"]).*\\3 ?(.*)""", grant[0])
485483

486484
if res is None:
487485
# If a user has roles assigned, we'll have one of priv tuples looking like
@@ -490,7 +488,7 @@ def pick(x):
490488
# As we use the mysql_role module to manipulate roles
491489
# we just ignore such privs below:
492490
res = re.match("""GRANT (.+) TO (['`"]).*""", grant[0])
493-
if not maria_role and res:
491+
if res:
494492
continue
495493

496494
raise InvalidPrivsError('unable to parse the MySQL grant string: %s' % grant[0])
@@ -505,10 +503,14 @@ def pick(x):
505503
# Determine if there's a case similar to the above:
506504
privileges = normalize_col_grants(privileges)
507505

508-
if not maria_role:
506+
if get_server_type(cursor) == 'mariadb':
509507
if "WITH GRANT OPTION" in res.group(7):
510508
privileges.append('GRANT')
509+
else:
510+
if "WITH GRANT OPTION" in res.group(4):
511+
privileges.append('GRANT')
511512
db = res.group(2)
513+
512514
output.setdefault(db, []).extend(privileges)
513515
return output
514516

@@ -684,48 +686,33 @@ def privileges_unpack(priv, mode, column_case_sensitive, ensure_usage=True):
684686
return output
685687

686688

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):
688690
# Escape '%' since mysql db.execute() uses a format string
689691
db_table = db_table.replace('%', '%%')
690692
if grant_option:
691693
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")
697695
query = ' '.join(query)
698696
cursor.execute(query, (user, host))
699697
priv_string = ",".join([p for p in priv if p not in ('GRANT', )])
700698

701699
if priv_string != "":
702700
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")
711702
query = ' '.join(query)
712-
cursor.execute(query, params)
703+
cursor.execute(query, (user, host))
713704
cursor.execute("FLUSH PRIVILEGES")
714705

715706

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):
717708
# Escape '%' since mysql db.execute uses a format string and the
718709
# specification of db and table often use a % (SQL wildcard)
719710
db_table = db_table.replace('%', '%%')
720711
priv_string = ",".join([p for p in priv if p not in ('GRANT', )])
721712
query = ["GRANT %s ON %s" % (priv_string, db_table)]
722713

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)
729716

730717
if tls_requires and impl.use_old_user_mgmt(cursor):
731718
query, params = mogrify_requires(" ".join(query), params, tls_requires)
@@ -851,8 +838,7 @@ def limit_resources(module, cursor, user, host, resource_limits, check_mode):
851838
module.fail_json(msg="The server version does not match the requirements "
852839
"for resource_limits parameter. See module's documentation.")
853840

854-
cursor.execute("SELECT VERSION()")
855-
if 'mariadb' not in cursor.fetchone()[0].lower():
841+
if get_server_type(cursor) != 'mariadb':
856842
if 'MAX_STATEMENT_TIME' in resource_limits:
857843
module.fail_json(msg="MAX_STATEMENT_TIME resource limit is only supported by MariaDB.")
858844

@@ -879,8 +865,8 @@ def limit_resources(module, cursor, user, host, resource_limits, check_mode):
879865

880866
def get_impl(cursor):
881867
global impl
882-
cursor.execute("SELECT VERSION()")
883-
if 'mariadb' in cursor.fetchone()[0].lower():
868+
869+
if get_server_type(cursor) == 'mariadb':
884870
from ansible_collections.community.mysql.plugins.module_utils.implementations.mariadb import user as mariauser
885871
impl = mariauser
886872
else:

plugins/modules/mysql_role.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,7 @@ def add(self, users, privs, check_mode=False, admin=False,
789789
if privs:
790790
for db_table, priv in iteritems(privs):
791791
privileges_grant(self.cursor, self.name, self.host,
792-
db_table, priv, tls_requires=None,
793-
maria_role=self.is_mariadb)
792+
db_table, priv, tls_requires=None)
794793

795794
return True
796795

@@ -932,7 +931,7 @@ def update(self, users, privs, check_mode=False,
932931
result = user_mod(self.cursor, self.name, self.host,
933932
None, None, None, None, None, None,
934933
privs, append_privs, subtract_privs, None,
935-
self.module, role=True, maria_role=self.is_mariadb)
934+
self.module, role=True)
936935
changed = result['changed']
937936

938937
if admin:

0 commit comments

Comments
 (0)