Skip to content

Commit 2d1eb37

Browse files
authored
RONDB-747: Remove hard-coded MySQL user (#93)
* Don't hard-code users in Dockerfile * Use host uid/gid when creating docker-compose file * Moved ndbmtd defaults to defaults
1 parent 6d13246 commit 2d1eb37

File tree

10 files changed

+30
-120
lines changed

10 files changed

+30
-120
lines changed

Dockerfile

+15-15
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ RUN mkdir $DOWNLOADS_CACHE_DIR
4747
# Copying bare minimum of Hopsworks cloud environment for now
4848
FROM rondb_runtime_dependencies as cloud_preparation
4949
ARG RONDB_VERSION=21.04.16
50-
RUN groupadd mysql && adduser mysql --ingroup mysql
5150
ENV HOPSWORK_DIR=/srv/hops
5251
ENV RONDB_BIN_DIR=$HOPSWORK_DIR/mysql-$RONDB_VERSION
5352
RUN mkdir -p $RONDB_BIN_DIR
@@ -64,8 +63,7 @@ RUN case "$TARGETARCH" in \
6463

6564
RUN --mount=type=bind,source=.,target=/context \
6665
. /env.sh \
67-
&& tar xfz /context/${TARBALL_PATH} -C $RONDB_BIN_DIR --strip-components=1 \
68-
&& chown mysql:mysql -R $RONDB_BIN_DIR
66+
&& tar xfz /context/${TARBALL_PATH} -C $RONDB_BIN_DIR --strip-components=1
6967

7068
# Get RonDB tarball from remote url & unpack it
7169
FROM cloud_preparation as remote_tarball
@@ -78,8 +76,7 @@ RUN case "$TARGETARCH" in \
7876
esac \
7977
&& wget $TARBALL_URL -O ./temp_tarball.tar.gz \
8078
&& tar xfz ./temp_tarball.tar.gz -C $RONDB_BIN_DIR --strip-components=1 \
81-
&& rm ./temp_tarball.tar.gz \
82-
&& chown mysql:mysql -R $RONDB_BIN_DIR
79+
&& rm ./temp_tarball.tar.gz
8380

8481
FROM ${RONDB_TARBALL_LOCAL_REMOTE}_tarball
8582

@@ -116,32 +113,35 @@ ENV MYSQL_UNIX_PORT=$RONDB_DATA_DIR/mysql.sock
116113

117114
RUN mkdir -p $LOG_DIR $RONDB_SCRIPTS_DIR $BACKUP_DATA_DIR $DISK_COLUMNS_DIR
118115

119-
COPY --chown=mysql:mysql ./resources/rondb_scripts $RONDB_SCRIPTS_DIR
116+
COPY ./resources/rondb_scripts $RONDB_SCRIPTS_DIR
120117
ENV PATH=$RONDB_SCRIPTS_DIR:$PATH
121118

122-
# So the path survives changing user to mysql
123-
RUN echo "export PATH=$PATH" >> /home/mysql/.profile
124-
125119
RUN touch $MYSQL_UNIX_PORT
126120

127121
# We expect this image to be used as base image to other
128122
# images with additional files specific to Docker
129-
COPY --chown=mysql:mysql ./resources/entrypoints ./docker/rondb_standalone/entrypoints
130-
COPY --chown=mysql:mysql ./resources/healthcheck.sh ./docker/rondb_standalone/healthcheck.sh
123+
COPY ./resources/entrypoints ./docker/rondb_standalone/entrypoints
124+
COPY ./resources/healthcheck.sh ./docker/rondb_standalone/healthcheck.sh
131125

132126
# Can be used to mount SQL init scripts
133127
ENV SQL_INIT_SCRIPTS_DIR=$HOPSWORK_DIR/docker/rondb_standalone/sql_init_scripts
134128
RUN mkdir $SQL_INIT_SCRIPTS_DIR
135129

136130
# Creating benchmarking files/directories
137131
# When using load balancers, "sysbench" can be used for both _single and _multi
138-
ENV BENCHMARKS_DIR=/home/mysql/benchmarks
132+
ENV BENCHMARKS_DIR=$HOPSWORK_DIR/benchmarks
139133
RUN mkdir $BENCHMARKS_DIR && cd $BENCHMARKS_DIR \
140134
&& mkdir -p sysbench sysbench_single sysbench_multi dbt2_single dbt2_multi dbt2_data
141135

142-
# Avoid changing files if they are already owned by mysql; otherwise image size doubles
143-
RUN chown mysql:mysql --from=root:root -R $HOPSWORK_DIR /home/mysql
136+
# These directories have to have 777 permissions if we want to
137+
# run RonDB containers with arbitrary users
138+
RUN chmod 777 -R $RONDB_DATA_DIR \
139+
&& chmod 777 -R $HOPSWORK_DIR/docker \
140+
&& chmod 777 -R $BENCHMARKS_DIR
141+
142+
# Remove awkward message when using arbitrary user
143+
RUN echo "PS1='${debian_chroot:+(\$debian_chroot)}\h:\w\$ '" >> /etc/bash.bashrc
144144

145-
ENTRYPOINT ["./docker/rondb_standalone/entrypoints/entrypoint.sh"]
145+
ENTRYPOINT ["./docker/rondb_standalone/entrypoints/main.sh"]
146146
EXPOSE 3306 33060 11860 1186 4406 5406
147147
CMD ["mysqld"]

build_run_docker.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ if [ "$NUM_MYSQLD_NODES" -gt 0 ]; then
379379
fi
380380

381381
DATA_DIR="/srv/hops/mysql-cluster"
382-
BENCH_DIR="/home/mysql/benchmarks"
382+
BENCH_DIR="/srv/hops/benchmarks"
383383

384384
#######################
385385
#######################
@@ -433,6 +433,7 @@ service-template() {
433433
%s:
434434
image: %s
435435
container_name: %s
436+
user: $(id -u):$(id -g)
436437
" "$SERVICE_NAME" "$RONDB_IMAGE_ID" "$SERVICE_NAME";
437438
}
438439

resources/config_templates/autobench_dbt2.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ NDB_MGMD_NODES="%s"
5656

5757
DBT2_TIME="30"
5858
DBT2_WAREHOUSES="%s"
59-
DBT2_DATA_DIR=/home/mysql/benchmarks/dbt2_data
59+
DBT2_DATA_DIR=/srv/hops/benchmarks/dbt2_data

resources/config_templates/config.ini

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
[NDBD DEFAULT]
2+
3+
DataDir=/srv/hops/mysql-cluster/log
4+
FileSystemPath=/srv/hops/mysql-cluster/ndb_data
5+
FileSystemPathDD=/srv/hops/mysql-cluster/ndb_disk_columns
6+
BackupDataDir=/srv/hops/mysql-cluster/ndb/backups
7+
28
# RonDB default configuration is intended for execution in Cloud VMs with
39
# around 8 GB per VCPU. In this case it is sufficient to set nothing since
410
# AutomaticThreadConfig=true is default and also AutomaticMemoryConfig=true

resources/config_templates/config_ndbd.ini

-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,3 @@ NodeActive=%s
55
LocationDomainId=0
66
HostName=%s
77
ServerPort=%s
8-
DataDir=/srv/hops/mysql-cluster/log
9-
FileSystemPath=/srv/hops/mysql-cluster/ndb_data
10-
FileSystemPathDD=/srv/hops/mysql-cluster/ndb_disk_columns
11-
BackupDataDir=/srv/hops/mysql-cluster/ndb/backups

resources/entrypoints/entrypoint.sh

-26
This file was deleted.

resources/entrypoints/main.sh

-53
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,10 @@
1616
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1717
set -e
1818

19-
# Let group members access files created by us. This is to allow the host user
20-
# (outside the container) to access mounted volumes. The umask will be inherited
21-
# by child processes, so this is the only place we need to set it.
22-
umask 0002
23-
2419
# https://stackoverflow.com/a/246128/9068781
2520
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2621

2722
if [ "$1" = 'mysqld' ]; then
28-
29-
# In order to make use of the umask, we need to set the environment
30-
# variables that controls the creation file mode for mysqld. These
31-
# variables are confusingly named UMASK and UMASK_DIR - despite their
32-
# names, they are used as modes, not masks. The default UMASK and
33-
# UMASK_DIR values are 0640 and 0750, respectively. We want an effect
34-
# similar to `chmod g=u`, so we'll set them to 0660 and 0770. The
35-
# prefixed 0 causes mysqld to interpret these as octal numbers. Note
36-
# that this configuration does not affect the file creation mode mysqld
37-
# uses for files containing cryptographic key (*.pem). This only means
38-
# the host user cannot read private keys, which is not a problem. Since
39-
# the host user has write permission to all directories, everything can
40-
# still be deleted.
41-
export UMASK=0660
42-
export UMASK_DIR=0770
43-
4423
"$SCRIPT_DIR/mysqld.sh" "$@"
4524
else
4625
# "set" lets us set the arguments to the current script.
@@ -62,38 +41,6 @@ else
6241
echo "[entrypoints/main.sh] Starting ndb_mgmd"
6342
set -- "$@" -f "$RONDB_DATA_DIR/config.ini" --configdir="$RONDB_DATA_DIR/log"
6443
elif [ "$1" == "ndbmtd" ]; then
65-
66-
# ndbmtd has several hard-coded file creation modes that cannot
67-
# be configured. Permissions can be removed from such hard-coded
68-
# modes using umask, but there is no way to add permissions to
69-
# them. As a workaround, this is a very hacky background process
70-
# that every 5 seconds makes sure that the group's permissions
71-
# equal the owner's.
72-
ensure-group-permissions() {
73-
# Find all files owned by the current user, print their
74-
# modestring and path, null-terminated.
75-
find /srv/hops/mysql-cluster -user "$USER" -printf '%m %p\0' |
76-
# Remove all null-terminated items that begin with two
77-
# equal characters (where the group's permissions
78-
# already equals the user's) and then remove the
79-
# modestring.
80-
sed -zr '/^(.)\1/d; s/^... //;' |
81-
# xargs: Run chmod with an efficient number of file
82-
# arguments to correct the group's permissions.
83-
xargs -r0 chmod -f g=u ||
84-
# Make sure the process does not exit due to some
85-
# failure.
86-
true
87-
}
88-
while true; do
89-
ensure-group-permissions
90-
sleep 5
91-
done &
92-
93-
# If ndbmtd exits within 5 seconds of creating a file, we need
94-
# to make sure to set group permissions correctly.
95-
trap ensure-group-permissions EXIT
96-
9744
echo "[entrypoints/main.sh] Starting ndbmtd"
9845
# Command for more verbosity with ndbmtds: `set -- "$@" --verbose=TRUE`
9946

resources/entrypoints/mysqld_configure.sh

+1-14
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,10 @@ _get_config() {
2525
"$@" --verbose --help 2>/dev/null | grep "^$conf" | awk '$1 == "'"$conf"'" { print $2; exit }'
2626
}
2727

28-
# Check if entrypoint (and the container) is running as root
29-
# Important: Distinguish between MYSQLD_USER and MYSQL_BENCH_USER
30-
if [ "$(id --user)" = "0" ]; then
31-
echo "[entrypoints/mysqld_configure.sh] We are running as root; setting MYSQLD_USER to 'mysql'"
32-
is_root=1
33-
install_devnull="install /dev/null -m0600 -omysql -gmysql"
34-
MYSQLD_USER=mysql
35-
else
36-
echo "[entrypoints/mysqld_configure.sh] Setting MYSQLD_USER to current non-root user"
37-
install_devnull="install /dev/null -m0600"
38-
MYSQLD_USER=$(id --user --name)
39-
fi
40-
4128
# Make sure that "--defaults-file" is always run as second argument
4229
# Otherwise there is a risk that it might not be read
4330
shift
44-
set -- mysqld --defaults-file="$RONDB_DATA_DIR/my.cnf" --user="$MYSQLD_USER" "$@"
31+
set -- mysqld --defaults-file="$RONDB_DATA_DIR/my.cnf" "$@"
4532
echo "[entrypoints/mysqld_configure.sh] \$@: $*"
4633

4734
# Test that the server can start. We redirect stdout to /dev/null so

sample_files/docker_compose.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ services:
193193
image: rondb:22.10.4-dev
194194
container_name: bench_1
195195
command: >
196-
bash -c "sleep 5 && bench_run.sh --verbose --default-directory /home/mysql/benchmarks/sysbench_multi "
196+
bash -c "sleep 5 && bench_run.sh --verbose --default-directory /srv/hops/benchmarks/sysbench_multi "
197197
deploy:
198198
resources:
199199
limits:
@@ -204,16 +204,16 @@ services:
204204
volumes:
205205
- type: bind
206206
source: <path-to-repo>/autogenerated_files/v22104_m1_g1_r2_my2_ra1_bn1/volumes/sysbench_single
207-
target: /home/mysql/benchmarks/sysbench_single
207+
target: /srv/hops/benchmarks/sysbench_single
208208
- type: bind
209209
source: <path-to-repo>/autogenerated_files/v22104_m1_g1_r2_my2_ra1_bn1/volumes/dbt2_single
210-
target: /home/mysql/benchmarks/dbt2_single
210+
target: /srv/hops/benchmarks/dbt2_single
211211
- type: bind
212212
source: <path-to-repo>/autogenerated_files/v22104_m1_g1_r2_my2_ra1_bn1/volumes/sysbench_multi
213-
target: /home/mysql/benchmarks/sysbench_multi
213+
target: /srv/hops/benchmarks/sysbench_multi
214214
- type: bind
215215
source: <path-to-repo>/autogenerated_files/v22104_m1_g1_r2_my2_ra1_bn1/volumes/dbt2_multi
216-
target: /home/mysql/benchmarks/dbt2_multi
216+
target: /srv/hops/benchmarks/dbt2_multi
217217
environment:
218218
- HOST_GROUP_ID=20
219219
- MYSQL_BENCH_PASSWORD=Abc123?e

sample_files/my.cnf

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
default-character-set = utf8
66

77
[mysqld]
8-
user = mysql
98
basedir = /srv/hops/mysql
109
datadir = /srv/hops/mysql-cluster/mysql
1110
port = 3306

0 commit comments

Comments
 (0)