Skip to content

Commit

Permalink
Add OceanBase support for creating user and setting password
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhangbj committed Dec 20, 2024
1 parent 022ed60 commit 3825d09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion plugins/module_utils/implementations/mysql/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def use_old_user_mgmt(cursor):

return LooseVersion(version) < LooseVersion("5.7")


def use_oceanbase(cursor):
version = get_server_version(cursor)
return 'oceanbase' in version.lower()

def supports_identified_by_password(cursor):
version = get_server_version(cursor)
return LooseVersion(version) < LooseVersion("8")
Expand Down
12 changes: 11 additions & 1 deletion plugins/module_utils/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def user_add(cursor, user, host, host_all, password, encrypted,
# Determine what user management method server uses
impl = get_user_implementation(cursor)
old_user_mgmt = impl.use_old_user_mgmt(cursor)
# whether the server is oceanbase
use_oceanbase = impl.use_oceanbase(cursor)

mogrify = do_not_mogrify_requires if old_user_mgmt else mogrify_requires

Expand Down Expand Up @@ -202,7 +204,7 @@ def user_add(cursor, user, host, host_all, password, encrypted,
else:
query_with_args = "CREATE USER %s@%s IDENTIFIED WITH mysql_native_password AS %s", (user, host, password)
elif password and not encrypted:
if old_user_mgmt:
if old_user_mgmt or use_oceanbase:
query_with_args = "CREATE USER %s@%s IDENTIFIED BY %s", (user, host, password)
else:
cursor.execute("SELECT CONCAT('*', UCASE(SHA1(UNHEX(SHA1(%s)))))", (password,))
Expand Down Expand Up @@ -272,6 +274,8 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
# Determine what user management method server uses
impl = get_user_implementation(cursor)
old_user_mgmt = impl.use_old_user_mgmt(cursor)
# whether the server is oceanbase
use_oceanbase = impl.use_oceanbase(cursor)

if host_all and not role:
hostnames = user_get_hostnames(cursor, user)
Expand Down Expand Up @@ -321,6 +325,9 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
else:
cursor.execute("SELECT CONCAT('*', UCASE(SHA1(UNHEX(SHA1(%s)))))", (password,))
encrypted_password = cursor.fetchone()[0]
# oceanbase encrypted password string are stored in lower case, so need to be converted to lower case for comparison
if use_oceanbase:
encrypted_password = encrypted_password.lower()

Check warning on line 330 in plugins/module_utils/user.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/user.py#L330

Added line #L330 was not covered by tests

if current_pass_hash != encrypted_password:
password_changed = True
Expand All @@ -329,6 +336,9 @@ def user_mod(cursor, user, host, host_all, password, encrypted,
if old_user_mgmt:
cursor.execute("SET PASSWORD FOR %s@%s = %s", (user, host, encrypted_password))
msg = "Password updated (old style)"
elif use_oceanbase:
cursor.execute("SET PASSWORD FOR %s = PASSWORD(%s)", (user, encrypted_password))
msg = "Password updated (OceanBase style)"

Check warning on line 341 in plugins/module_utils/user.py

View check run for this annotation

Codecov / codecov/patch

plugins/module_utils/user.py#L340-L341

Added lines #L340 - L341 were not covered by tests
else:
try:
cursor.execute("ALTER USER %s@%s IDENTIFIED WITH mysql_native_password AS %s", (user, host, encrypted_password))
Expand Down

0 comments on commit 3825d09

Please sign in to comment.