Skip to content

Commit e987c14

Browse files
committed
Update default mysql user script
1 parent 47639bf commit e987c14

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

Dockerfile

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,5 @@ RUN dnf update -y
1919
RUN dnf install mariadb105 -y
2020

2121
# Copy scripts to the appropriate directory and make them executable
22-
COPY ./menu.sh /usr/local/bin/
23-
COPY ./check-connections.sh /usr/local/bin/
24-
COPY ./check-envs.sh /usr/local/bin/
25-
COPY ./ecs-utils.sh /usr/local/bin/
26-
COPY ./lambda-utils.sh /usr/local/bin/
27-
COPY ./check-db.sh /usr/local/bin/
28-
COPY ./db-cloning.sh /usr/local/bin/
29-
COPY ./migrate-utils.sh /usr/local/bin/
30-
31-
RUN chmod +x /usr/local/bin/menu.sh \
32-
/usr/local/bin/check-connections.sh \
33-
/usr/local/bin/check-envs.sh \
34-
/usr/local/bin/ecs-utils.sh \
35-
/usr/local/bin/lambda-utils.sh \
36-
/usr/local/bin/check-db.sh \
37-
/usr/local/bin/db-cloning.sh \
38-
/usr/local/bin/migrate-utils.sh
22+
COPY ./*.sh /usr/local/bin/
23+
RUN chmod +x /usr/local/bin/*.sh

menu.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ show_menu() {
1111
echo "4. Manage ECS Services"
1212
echo "5. Manage Lambda Functions"
1313
echo "6. Clone Database"
14-
echo "7. Exit"
14+
echo "7. Create Default MySQL Users (xxx_app, xxx_readonly)"
15+
echo "8. Exit"
1516
echo "========================================"
1617
}
1718

@@ -92,6 +93,9 @@ while true; do
9293
db-cloning.sh $source_cluster_name $target_cluster_name $target_instance_name $target_region $db_subnet_group_name $vpc_security_group_ids
9394
;;
9495
7)
96+
mysql-user-creator.sh
97+
;;
98+
8)
9599
echo "Exiting..."
96100
exit 0
97101
;;

mysql-user-creator.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
# Prompt or use env
4+
MYSQL_HOST=${MYSQL_HOST:-$(read -p "Enter MySQL host: " tmp && echo "$tmp")}
5+
MYSQL_USER=${MYSQL_USER:-$(read -p "Enter MySQL admin user: " tmp && echo "$tmp")}
6+
MYSQL_PASSWORD=${MYSQL_PASSWORD:-$(read -sp "Enter MySQL admin password: " tmp && echo "$tmp" && echo)}
7+
TARGET_DB=${MYSQL_DB:-$(read -p "Enter target database name: " tmp && echo "$tmp")}
8+
USER_PREFIX=${USER_PREFIX:-$(read -p "Enter product name (e.g. 'icp'): " tmp && echo "$tmp")}
9+
10+
# SSL certificates
11+
SSL_CA="/ssl_cert/ca-cert.pem"
12+
13+
# Verify certificate files exist
14+
if [[ ! -f "$SSL_CA" ]]; then
15+
echo "❌ Required certificate not found: $SSL_CA"
16+
exit 1
17+
fi
18+
19+
# Construct usernames
20+
READONLY_USER="${USER_PREFIX}_readonly"
21+
APP_USER="${USER_PREFIX}_app"
22+
23+
# Generate random passwords (16 chars)
24+
generate_password() {
25+
head -c 16 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 16
26+
}
27+
28+
# Flags
29+
CREATED_ANY_USER=false
30+
31+
# Function to create user if not exists
32+
create_user_if_not_exists() {
33+
local USERNAME=$1
34+
local PASSWORD=$2
35+
local GRANTS=$3
36+
local USER_EXISTS=$(mysql \
37+
-h "$MYSQL_HOST" \
38+
-u "$MYSQL_USER" \
39+
-p"$MYSQL_PASSWORD" \
40+
--ssl-ca="$SSL_CA" \
41+
-sN -e "SELECT COUNT(*) FROM mysql.user WHERE user = '$USERNAME';")
42+
43+
if [[ "$USER_EXISTS" == "0" ]]; then
44+
mysql \
45+
-h "$MYSQL_HOST" \
46+
-u "$MYSQL_USER" \
47+
-p"$MYSQL_PASSWORD" \
48+
--ssl-ca="$SSL_CA" <<EOF
49+
CREATE USER '$USERNAME'@'%' IDENTIFIED BY '$PASSWORD';
50+
$GRANTS
51+
FLUSH PRIVILEGES;
52+
EOF
53+
echo "✅ Created user: $USERNAME"
54+
echo " Password: $PASSWORD"
55+
echo ""
56+
CREATED_ANY_USER=true
57+
fi
58+
}
59+
60+
# Create readonly user if not exists
61+
READONLY_PASSWORD=$(generate_password)
62+
create_user_if_not_exists "$READONLY_USER" "$READONLY_PASSWORD" "GRANT SELECT ON \`$TARGET_DB\`.* TO '$READONLY_USER'@'%';"
63+
64+
# Create app user if not exists
65+
APP_PASSWORD=$(generate_password)
66+
create_user_if_not_exists "$APP_USER" "$APP_PASSWORD" "GRANT ALL PRIVILEGES ON \`$TARGET_DB\`.* TO '$APP_USER'@'%';"
67+
68+
if [ "$CREATED_ANY_USER" = false ]; then
69+
echo "ℹ️ No new users created. Both users already exist."
70+
fi

0 commit comments

Comments
 (0)