Skip to content

Commit 5e21304

Browse files
committed
Refactor MySQL auth to support restacking
This change refactors the MySQL/MariaDB configuration to: - Allow both unix_socket and mysql_native_password authentication using the MariaDB 'IDENTIFIED VIA ... OR ...' syntax. This enables restacking without needing to reset authentication in unstack.sh. - Add use_mariadb_socket_auth helper variable to simplify the complex conditional logic (addresses TODO comment). - Fix missing DATABASE_USER@'%' creation for modern Debian/Ubuntu with MariaDB socket auth. - Fix inconsistent distro checks that were missing trixie. - Remove dead Oracle Linux code since it's not in SUPPORTED_DISTROS. Oracle Linux is still handled as RHEL-like via is_fedora(). Generated-By: Cursor claude-opus-4.5 Change-Id: I4becbfe6325bcb29deef8e50e9a9f05678f47802 Signed-off-by: Sean Mooney <work@seanmooney.info>
1 parent 2df8cce commit 5e21304

2 files changed

Lines changed: 25 additions & 38 deletions

File tree

functions-common

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,17 +517,6 @@ function is_arch {
517517
[[ "$(uname -m)" == "$1" ]]
518518
}
519519

520-
# Determine if current distribution is an Oracle distribution
521-
# is_oraclelinux
522-
function is_oraclelinux {
523-
if [[ -z "$os_VENDOR" ]]; then
524-
GetOSVersion
525-
fi
526-
527-
[ "$os_VENDOR" = "OracleServer" ]
528-
}
529-
530-
531520
# Determine if current distribution is a Fedora-based distribution
532521
# (Fedora, RHEL, CentOS, Rocky, etc).
533522
# is_fedora

lib/databases/mysql

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ register_database mysql
1818

1919
if [[ -z "$MYSQL_SERVICE_NAME" ]]; then
2020
MYSQL_SERVICE_NAME=mysql
21-
if is_fedora && ! is_oraclelinux; then
21+
if is_fedora; then
2222
MYSQL_SERVICE_NAME=mariadb
2323
elif [[ "$DISTRO" =~ trixie|bookworm|bullseye ]]; then
2424
MYSQL_SERVICE_NAME=mariadb
@@ -44,15 +44,9 @@ function cleanup_database_mysql {
4444
apt_get purge -y mysql* mariadb*
4545
sudo rm -rf /var/lib/mysql
4646
sudo rm -rf /etc/mysql
47-
return
48-
elif is_oraclelinux; then
49-
uninstall_package mysql-community-server
50-
sudo rm -rf /var/lib/mysql
5147
elif is_fedora; then
5248
uninstall_package mariadb-server
5349
sudo rm -rf /var/lib/mysql
54-
else
55-
return
5650
fi
5751
}
5852

@@ -68,8 +62,6 @@ function configure_database_mysql {
6862

6963
if is_ubuntu; then
7064
my_conf=/etc/mysql/my.cnf
71-
elif is_oraclelinux; then
72-
my_conf=/etc/my.cnf
7365
elif is_fedora; then
7466
my_conf=/etc/my.cnf
7567
local cracklib_conf=/etc/my.cnf.d/cracklib_password_check.cnf
@@ -101,13 +93,20 @@ function configure_database_mysql {
10193
restart_service $MYSQL_SERVICE_NAME
10294
fi
10395

96+
# MariaDB 10.4+ on modern Debian/Ubuntu uses unix_socket auth by default
97+
# See https://mariadb.org/authentication-in-mariadb-10-4/
98+
local use_mariadb_socket_auth=False
99+
if is_ubuntu && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
100+
if [[ ! "$DISTRO" =~ bookworm|bullseye ]]; then
101+
use_mariadb_socket_auth=True
102+
fi
103+
fi
104+
104105
# Set the root password - only works the first time. For Ubuntu, we already
105106
# did that with debconf before installing the package, but we still try,
106107
# because the package might have been installed already. We don't do this
107-
# for Ubuntu 22.04+ because the authorization model change in
108-
# version 10.4 of mariadb. See
109-
# https://mariadb.org/authentication-in-mariadb-10-4/
110-
if ! (is_ubuntu && [[ ! "$DISTRO" =~ trixie|bookworm|bullseye ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
108+
# for MariaDB with socket auth because the root password is managed differently.
109+
if [[ "$use_mariadb_socket_auth" != "True" ]]; then
111110
sudo mysqladmin -u root password $DATABASE_PASSWORD || true
112111
fi
113112

@@ -129,19 +128,20 @@ function configure_database_mysql {
129128
restart_service $MYSQL_SERVICE_NAME
130129
fi
131130

132-
# In mariadb e.g. on Ubuntu socket plugin is used for authentication
133-
# as root so it works only as sudo. To restore old "mysql like" behaviour,
134-
# we need to change auth plugin for root user
135-
# TODO(frickler): simplify this logic
136-
if is_ubuntu && [[ ! "$DISTRO" =~ bookworm|bullseye ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]; then
137-
# For Ubuntu 22.04+ we follow the model outlined in
138-
# https://mariadb.org/authentication-in-mariadb-10-4/
139-
sudo mysql -e "ALTER USER $DATABASE_USER@localhost IDENTIFIED VIA mysql_native_password USING PASSWORD('$DATABASE_PASSWORD');"
131+
# Configure database user authentication
132+
if [[ "$use_mariadb_socket_auth" == "True" ]]; then
133+
# Allow both unix_socket (for sudo mysql) and password auth
134+
# Using OR allows restacking without needing to reset auth in unstack
135+
sudo mysql -e "ALTER USER $DATABASE_USER@localhost IDENTIFIED VIA unix_socket OR mysql_native_password USING PASSWORD('$DATABASE_PASSWORD');"
140136
fi
141-
if ! (is_ubuntu && [[ ! "$DISTRO" =~ bookworm|bullseye ]] && [ "$MYSQL_SERVICE_NAME" == "mariadb" ]); then
142-
# Create DB user if it does not already exist
137+
138+
# Create remote access user and grant privileges (needed for all distros)
139+
if [[ "$use_mariadb_socket_auth" == "True" ]]; then
140+
# Use sudo mysql since we have socket auth
141+
sudo mysql -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
142+
sudo mysql -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
143+
else
143144
sudo mysql $cmd_args -e "CREATE USER IF NOT EXISTS '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';"
144-
# Update the DB to give user '$DATABASE_USER'@'%' full control of the all databases:
145145
sudo mysql $cmd_args -e "GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'%';"
146146
fi
147147

@@ -226,9 +226,7 @@ EOF
226226
fi
227227
# Install mysql-server
228228
if [[ "$INSTALL_DATABASE_SERVER_PACKAGES" == "True" ]]; then
229-
if is_oraclelinux; then
230-
install_package mysql-community-server
231-
elif is_fedora; then
229+
if is_fedora; then
232230
install_package mariadb-server mariadb-devel mariadb
233231
sudo systemctl enable $MYSQL_SERVICE_NAME
234232
elif is_ubuntu; then

0 commit comments

Comments
 (0)