-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add do
command to update the authentication plugin of MySQL users
#1097
base: nightly
Are you sure you want to change the base?
Changes from all commits
820cebe
4d0042c
19802ea
a9e987e
d266f4b
24e8872
d56029f
a497c29
c872809
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- [Improvement] Add a do command to update the authentication plugin of existing MySQL users from mysql_native_password to caching_sha2_password for compatibility with MySQL v8.4.0 and above. (by @Danyal-Faheem) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
This module provides utility methods for tutor `do` commands | ||
|
||
Methods: | ||
- `get_mysql_change_authentication_plugin_query`: Generates MySQL queries to update the authentication plugin for MySQL users. | ||
""" | ||
|
||
from typing import List | ||
|
||
from tutor import exceptions, fmt | ||
from tutor.types import Config, ConfigValue | ||
|
||
|
||
def get_mysql_change_authentication_plugin_query( | ||
config: Config, users: List[str], all_users: bool | ||
) -> str: | ||
""" | ||
Generates MySQL queries to update the authentication plugin for MySQL users. | ||
|
||
This method constructs queries to change the authentication plugin to | ||
`caching_sha2_password`. User credentials must be provided in the tutor | ||
configuration under the keys `<user>_MYSQL_USERNAME` and `<user>_MYSQL_PASSWORD`. | ||
|
||
Args: | ||
config (Config): Tutor configuration object | ||
users (List[str]): List of specific MySQL users to update. | ||
all_users (bool): Flag indicating whether to include ROOT and OPENEDX users | ||
in addition to those specified in the `users` list. | ||
|
||
Returns: | ||
str: A string containing the SQL queries to execute. | ||
|
||
Raises: | ||
TutorError: If any user in the `users` list does not have corresponding | ||
username or password entries in the configuration. | ||
""" | ||
|
||
host = "%" | ||
query = "" | ||
|
||
def generate_mysql_authentication_plugin_update_query( | ||
username: ConfigValue, password: ConfigValue, host: str | ||
) -> str: | ||
fmt.echo_info( | ||
f"Authentication plugin of user {username} will be updated to caching_sha2_password" | ||
) | ||
return f"ALTER USER '{username}'@'{host}' IDENTIFIED with caching_sha2_password BY '{password}';" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is extremely confusing, and there are many ways in which it can fail. In order to improve it, I'd like to understand if it's possible to automatically detect all mysql users that were created with the legacy authentication plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only way to do that would be to get the output of a MySQL query inside tutor codebase. I am not aware of any method that we can use to get the output of a command run inside a container inside the tutor codebase. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming we could get the output of a mysql query in tutor, what would be that query? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something similar to this:
We need to limit the host to '%' as Tutor uses that host value by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey Regis, any thoughts on how we should proceed with this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure. basically, we need to perform all tasks inside a single templated bash script -- including the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So maybe something similar to a stored procedure? Something akin to what we did in #1079? We could get the names of all the users and update them inside that procedure. Although, I'd have to look into how we would manage passwords then. |
||
|
||
def generate_user_queries(users: List[str]) -> str: | ||
query = "" | ||
for user in users: | ||
user_uppercase = user.upper() | ||
if not ( | ||
f"{user_uppercase}_MYSQL_USERNAME" in config | ||
and f"{user_uppercase}_MYSQL_PASSWORD" in config | ||
): | ||
fmt.echo_warning( | ||
f"Username or Password for User {user} not found in config. Skipping update process for User {user}." | ||
) | ||
continue | ||
|
||
query += generate_mysql_authentication_plugin_update_query( | ||
config[f"{user_uppercase}_MYSQL_USERNAME"], | ||
config[f"{user_uppercase}_MYSQL_PASSWORD"], | ||
host, | ||
) | ||
return query | ||
|
||
if not all_users: | ||
return generate_user_queries(users) | ||
|
||
query += generate_mysql_authentication_plugin_update_query( | ||
config["MYSQL_ROOT_USERNAME"], config["MYSQL_ROOT_PASSWORD"], host | ||
) | ||
query += generate_mysql_authentication_plugin_update_query( | ||
config["OPENEDX_MYSQL_USERNAME"], config["OPENEDX_MYSQL_PASSWORD"], host | ||
) | ||
|
||
return query + generate_user_queries(users) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,14 @@ def alert(text: str) -> str: | |
return click.style("⚠️ " + text, fg="yellow", bold=True) | ||
|
||
|
||
def warning(text: str) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alert prints to standard error and this seemed like a situation where we just wanted to display a warning rather than an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep an alert. We don't want to write to stdout because it might mangle output from other commands, such as |
||
return click.style(text, fg="bright_yellow") | ||
|
||
|
||
def echo_warning(text: str) -> None: | ||
echo(warning(text)) | ||
|
||
|
||
def echo(text: str, err: bool = False) -> None: | ||
if os.environ.get("_TUTOR_COMPLETE"): | ||
if os.environ.get("COMP_WORDS") or os.environ.get("COMP_CWORD"): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return earlier if there is no user: