Skip to content

Commit eee823f

Browse files
Initial attempt
1 parent 24a4137 commit eee823f

File tree

3 files changed

+36
-39
lines changed

3 files changed

+36
-39
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from distutils.version import LooseVersion
2+
from ansible_collections.community.mysql.plugins.module_utils.mysql import get_server_version
3+
4+
5+
def use_old_user_mgmt(cursor):
6+
version = get_server_version(cursor)
7+
8+
return LooseVersion(version) < LooseVersion("10.2")
9+
10+
11+
def supports_identified_by_password(cursor):
12+
return True
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from distutils.version import LooseVersion
2+
from ansible_collections.community.mysql.plugins.module_utils.mysql import get_server_version
3+
4+
5+
def use_old_user_mgmt(cursor):
6+
version = get_server_version(cursor)
7+
8+
return LooseVersion(version) < LooseVersion("5.7")
9+
10+
11+
def supports_identified_by_password(cursor):
12+
version = get_server_version(cursor)
13+
return LooseVersion(version) < LooseVersion("8")

plugins/modules/mysql_user.py

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -351,40 +351,6 @@ class InvalidPrivsError(Exception):
351351
#
352352

353353

354-
# User Authentication Management changed in MySQL 5.7 and MariaDB 10.2.0
355-
def use_old_user_mgmt(cursor):
356-
cursor.execute("SELECT VERSION()")
357-
result = cursor.fetchone()
358-
version_str = result[0]
359-
version = version_str.split('.')
360-
361-
if 'mariadb' in version_str.lower():
362-
# Prior to MariaDB 10.2
363-
if int(version[0]) * 1000 + int(version[1]) < 10002:
364-
return True
365-
else:
366-
return False
367-
else:
368-
# Prior to MySQL 5.7
369-
if int(version[0]) * 1000 + int(version[1]) < 5007:
370-
return True
371-
else:
372-
return False
373-
374-
375-
def supports_identified_by_password(cursor):
376-
"""
377-
Determines whether the 'CREATE USER %s@%s IDENTIFIED BY PASSWORD %s' syntax is supported. This was dropped in
378-
MySQL 8.0.
379-
"""
380-
version_str = get_server_version(cursor)
381-
382-
if 'mariadb' in version_str.lower():
383-
return True
384-
else:
385-
return LooseVersion(version_str) < LooseVersion('8')
386-
387-
388354
def get_mode(cursor):
389355
cursor.execute('SELECT @@GLOBAL.sql_mode')
390356
result = cursor.fetchone()
@@ -443,7 +409,7 @@ def do_not_mogrify_requires(query, params, tls_requires):
443409

444410
def get_tls_requires(cursor, user, host):
445411
if user:
446-
if not use_old_user_mgmt(cursor):
412+
if not impl.use_old_user_mgmt(cursor):
447413
query = "SHOW CREATE USER '%s'@'%s'" % (user, host)
448414
else:
449415
query = "SHOW GRANTS for '%s'@'%s'" % (user, host)
@@ -485,12 +451,12 @@ def user_add(cursor, user, host, host_all, password, encrypted,
485451
return True
486452

487453
# Determine what user management method server uses
488-
old_user_mgmt = use_old_user_mgmt(cursor)
454+
old_user_mgmt = impl.use_old_user_mgmt(cursor)
489455

490456
mogrify = do_not_mogrify_requires if old_user_mgmt else mogrify_requires
491457

492458
if password and encrypted:
493-
if supports_identified_by_password(cursor):
459+
if impl.supports_identified_by_password(cursor):
494460
query_with_args = "CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password)
495461
else:
496462
query_with_args = "CREATE USER %s@%s IDENTIFIED WITH mysql_native_password AS %s", (user, host, password)
@@ -537,7 +503,7 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
537503
grant_option = False
538504

539505
# Determine what user management method server uses
540-
old_user_mgmt = use_old_user_mgmt(cursor)
506+
old_user_mgmt = impl.use_old_user_mgmt(cursor)
541507

542508
if host_all:
543509
hostnames = user_get_hostnames(cursor, user)
@@ -857,7 +823,7 @@ def privileges_grant(cursor, user, host, db_table, priv, tls_requires):
857823
query = ["GRANT %s ON %s" % (priv_string, db_table)]
858824
query.append("TO %s@%s")
859825
params = (user, host)
860-
if tls_requires and use_old_user_mgmt(cursor):
826+
if tls_requires and impl.use_old_user_mgmt(cursor):
861827
query, params = mogrify_requires(" ".join(query), params, tls_requires)
862828
query = [query]
863829
if 'REQUIRESSL' in priv and not tls_requires:
@@ -1096,6 +1062,12 @@ def main():
10961062
if not sql_log_bin:
10971063
cursor.execute("SET SQL_LOG_BIN=0;")
10981064

1065+
cursor.execute("SELECT VERSION()")
1066+
if 'mariadb' in cursor.fetchone()[0].lower():
1067+
from ansible_collections.community.mysql.plugins.implementations.mariadb import user as impl
1068+
else:
1069+
from ansible_collections.community.mysql.plugins.implementations.mysql import user as impl
1070+
10991071
if priv is not None:
11001072
try:
11011073
mode = get_mode(cursor)

0 commit comments

Comments
 (0)