Skip to content

Commit d074691

Browse files
committed
cache root pw in file outside of mounted volumes
Don't write root pw in /var/lib/mysql which is volume mounted. Also, instead of using /etc or /etc/my.cnf.d, we dont want a mysql-owned writable file in /etc which is also considered to be server configuration so since we don't actually need mariadb tools to find this file, instead write to an arbitrary file in a non-mounted directory like /var/local/mysql_pw_cache.cnf.
1 parent 680e7bf commit d074691

File tree

4 files changed

+61
-28
lines changed

4 files changed

+61
-28
lines changed

templates/galera/bin/mysql_root_auth.sh

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ MARIADB_API="apis/mariadb.openstack.org/v1beta1"
1414

1515
GALERA_INSTANCE="{{.galeraInstanceName}}"
1616

17-
MY_CNF="$HOME/.my.cnf"
17+
PW_CACHE_FILE="/var/local/mysql_pw_cache.cnf"
1818
MYSQL_SOCKET=/var/lib/mysql/mysql.sock
1919

2020
CREDENTIALS_CHECK_TIMEOUT=4
@@ -30,9 +30,9 @@ else
3030
fi
3131

3232
# Check if we have cached credentials
33-
if [ "${MYSQL_ROOT_AUTH_BYPASS_CHECKS}" != "true" ] && [ -f "${MY_CNF}" ]; then
33+
if [ "${MYSQL_ROOT_AUTH_BYPASS_CHECKS}" != "true" ] && [ -f "${PW_CACHE_FILE}" ]; then
3434
# Read the password from .my.cnf
35-
PASSWORD=$(grep '^password=' "${MY_CNF}" | cut -d= -f2-)
35+
PASSWORD=$(grep '^password=' "${PW_CACHE_FILE}" | cut -d= -f2-)
3636

3737
# Validate credentials if MySQL is accessible
3838
if [ -n "${PASSWORD}" ]; then
@@ -153,15 +153,43 @@ fi
153153
MYSQL_PWD="${PASSWORD}"
154154
DB_ROOT_PASSWORD="${PASSWORD}"
155155

156-
# Cache credentials to /root/.my.cnf in MySQL client format
157-
cat > "${MY_CNF}" <<EOF
156+
# Cache credentials to $PW_CACHE_FILE.
157+
# we use .my.cnf format, however as this file is not in an official my.cnf
158+
# location or filename, it's not actually consumed directly by mysql client
159+
# tools.
160+
#
161+
# rationale:
162+
#
163+
# 1. all the client tools are called with -uroot -p${PASSWORD}, so we don't
164+
# actually need this file to be consumed by mariadb client applications
165+
# 2. we don't want a mysql-owned/writable server configuration file in
166+
# /etc/my.cnf.d, all other files in /etc/my.cnf.d/ are root owned /
167+
# read-only
168+
# 3. we don't want to be overwriting such a file either (in case it had
169+
# other actual server conf in it)
170+
# 4. we dont want the root password in a long-lived, volume-mounted file like
171+
# /var/lib/mysql/.my.cnf
172+
# 5. we don't want to mess around with $MARIADB_HOME, $MYSQL_HOME, as
173+
# this is unnecessary due to item 1 above
174+
#
175+
if ! cat > "${PW_CACHE_FILE}" <<EOF 2>/dev/null
158176
[client]
159177
user=root
160178
password=${PASSWORD}
161179
EOF
180+
then
181+
# we are called for the first time from detect_gcomm_and_start.sh which is
182+
# called **before** kolla can set directory permissions; so when writing
183+
# the file, proceed even if we can't write the file yet
184+
echo "WARNING: Failed to write to ${PW_CACHE_FILE} due to permissions; will try again later" >&2
185+
fi
162186

163-
# Set restrictive permissions on .my.cnf
164-
chmod 600 "${MY_CNF}"
187+
# Set restrictive permissions on .my.cnf (only if file was successfully written)
188+
if [ -f "${PW_CACHE_FILE}" ]; then
189+
if ! chmod 600 "${PW_CACHE_FILE}" 2>/dev/null; then
190+
echo "WARNING: Failed to set permissions on ${PW_CACHE_FILE}; will try again later" >&2
191+
fi
192+
fi
165193

166194
export MYSQL_PWD
167195
export DB_ROOT_PASSWORD

templates/galera/config/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
"path": "/var/log/mariadb",
6161
"owner": "mysql:mysql",
6262
"recurse": "true"
63+
},
64+
{
65+
"path": "/var/local",
66+
"owner": "mysql:mysql",
67+
"recurse": "false"
6368
}
6469
]
6570
}

test/chainsaw/tests/root-auth-cache/chainsaw-test.yaml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,55 @@ spec:
1515
- assert:
1616
file: ../../common/galera-assert.yaml
1717

18-
- name: verify .my.cnf created
19-
description: Verify that .my.cnf is created after first mysql_root_auth.sh invocation
18+
- name: verify mysql_pw_cache.cnf created
19+
description: Verify that mysql_pw_cache.cnf is created after first mysql_root_auth.sh invocation
2020
try:
2121
- script:
2222
content: |
2323
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
2424
source /var/lib/operator-scripts/mysql_root_auth.sh
25-
test -f $HOME/.my.cnf
25+
test -f /var/local/mysql_pw_cache.cnf
2626
'
2727
28-
- name: verify .my.cnf format
29-
description: Verify .my.cnf has proper MySQL client format
28+
- name: verify mysql_pw_cache.cnf format
29+
description: Verify mysql_pw_cache.cnf has proper MySQL client format
3030
try:
3131
- script:
3232
content: |
3333
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
34-
grep -q "^\[client\]" $HOME/.my.cnf &&
35-
grep -q "^user=root" $HOME/.my.cnf &&
36-
grep -q "^password=" $HOME/.my.cnf
34+
grep -q "^\[client\]" /var/local/mysql_pw_cache.cnf &&
35+
grep -q "^user=root" /var/local/mysql_pw_cache.cnf &&
36+
grep -q "^password=" /var/local/mysql_pw_cache.cnf
3737
'
3838
39-
- name: verify .my.cnf permissions
40-
description: Verify .my.cnf has secure permissions (600)
39+
- name: verify mysql_pw_cache.cnf permissions
40+
description: Verify mysql_pw_cache.cnf has secure permissions (600)
4141
try:
4242
- script:
4343
content: |
4444
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
45-
perms=$(stat -c "%a" $HOME/.my.cnf)
45+
perms=$(stat -c "%a" /var/local/mysql_pw_cache.cnf)
4646
test "$perms" = "600"
4747
'
4848
49-
- name: verify mysql works without explicit credentials
50-
description: Verify MySQL commands work using .my.cnf without MYSQL_PWD env var
49+
- name: verify we can in theory use the file like a my.cnf file
50+
description: Verify MySQL commands work using mysql_pw_cache.cnf without MYSQL_PWD env var
5151
try:
5252
- script:
5353
content: |
5454
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
5555
unset MYSQL_PWD
56-
mysql -e "SELECT 1" > /dev/null
56+
mysql --defaults-extra-file=/var/local/mysql_pw_cache.cnf -e "SELECT 1" > /dev/null
5757
'
5858
59-
- name: verify mysqladmin works without explicit credentials
60-
description: Verify mysqladmin ping works using .my.cnf
59+
- name: verify we can in theory use the file like a my.cnf file with mysqladmin
60+
description: Verify mysqladmin ping works using mysql_pw_cache.cnf
6161
try:
6262
- script:
6363
content: |
6464
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
6565
unset MYSQL_PWD
66-
mysqladmin ping > /dev/null
66+
mysqladmin --defaults-extra-file=/var/local/mysql_pw_cache.cnf ping > /dev/null
6767
'
6868
6969
- name: verify caching works
@@ -90,13 +90,13 @@ spec:
9090
'
9191
9292
- name: verify cache refresh on invalid credentials
93-
description: Verify that invalid credentials in .my.cnf trigger a refresh
93+
description: Verify that invalid credentials in mysql_pw_cache.cnf trigger a refresh
9494
try:
9595
- script:
9696
content: |
9797
oc exec -n ${NAMESPACE} -c galera openstack-galera-0 -- /bin/sh -c '
98-
# Write invalid credentials to .my.cnf
99-
echo -e "[client]\nuser=root\npassword=wrongpassword" > $HOME/.my.cnf
98+
# Write invalid credentials to mysql_pw_cache.cnf
99+
echo -e "[client]\nuser=root\npassword=wrongpassword" > /var/local/mysql_pw_cache.cnf
100100
# Source mysql_root_auth.sh - should detect invalid creds and refresh
101101
source /var/lib/operator-scripts/mysql_root_auth.sh
102102
# Verify MySQL works now (credentials were refreshed)

test/chainsaw/tests/update-root-pw/chainsaw-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ spec:
6868
echo "Testing login on $pod..."
6969
oc exec -n ${NAMESPACE} -c galera $pod -- /bin/sh -c '
7070
# Clear the cached credentials to force using new password
71-
rm -f $HOME/.my.cnf
71+
rm -f /var/local/mysql_pw_cache.cnf
7272
source /var/lib/operator-scripts/mysql_root_auth.sh
7373
if [ "$MYSQL_PWD" != "newrootpassword123" ]; then
7474
echo "ERROR: password != 'newrootpassword123' (actual: $MYSQL_PWD)"

0 commit comments

Comments
 (0)