-
-
Notifications
You must be signed in to change notification settings - Fork 67
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(casaos-update-database-password): Add script to update database password #21
base: master
Are you sure you want to change the base?
Conversation
…e password This commit adds a new script `run.sh` that allows users to update the database password for their CasaOS application. The script supports both PostgreSQL and MySQL/MariaDB databases, and it provides instructions for updating the password in the `docker-compose.yml` file. The key features of this script include: - Checking if the script is run as root - Displaying a user-friendly introduction and instructions - Allowing the user to select the database container to update - Prompting the user to enter a new password - Updating the password in the database container - Updating the `.env` file if it exists - Providing an option to edit the `docker-compose.yml` file using the user's preferred editor (nano or vim) - Applying the changes using the `casaos-cli` tool This script aims to simplify the process of updating the database password for CasaOS users, making it more accessible and user-friendly.
WalkthroughThe changes introduce a new script, Changes
Possibly related PRs
Warning Rate limit exceeded@dragonfire1119 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 28 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Removes the functionality to update the .env file with the new database password. This change simplifies the script and focuses on the core functionality of updating the database password.
env file update This change updates the README file for the casaos-update-database- password script to remove the mention of automatically updating the corresponding .env file. The script now only updates the root/admin password for the running database containers.
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.
Actionable comments posted: 5
🧹 Outside diff range and nitpick comments (3)
casaos-update-database-password/run.sh (3)
5-8
: Remove unused color variablesThe
GREEN
andYELLOW
color variables are defined but never used in the script.# Set text colors RED='\033[0;31m' -GREEN='\033[0;32m' -YELLOW='\033[1;33m' NC='\033[0m' # No Color🧰 Tools
🪛 Shellcheck
[warning] 6-6: GREEN appears unused. Verify use (or export if used externally).
(SC2034)
[warning] 7-7: YELLOW appears unused. Verify use (or export if used externally).
(SC2034)
27-27
: Define version as a variableConsider defining the version number as a variable at the top of the script for easier maintenance.
+# Script version +VERSION="0.0.1" + # Function to display menu show_intro() { clear - print_header "BigBearCasaOS Database Password Update Tool V0.0.1" + print_header "BigBearCasaOS Database Password Update Tool V${VERSION}"
42-43
: Enhance error handlingConsider adding
set -u
to exit on undefined variables and catch potential issues early.# Set error handling set -e +set -u
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
casaos-update-database-password/README.md
(1 hunks)casaos-update-database-password/run.sh
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- casaos-update-database-password/README.md
🧰 Additional context used
🪛 Shellcheck
casaos-update-database-password/run.sh
[warning] 6-6: GREEN appears unused. Verify use (or export if used externally).
(SC2034)
[warning] 7-7: YELLOW appears unused. Verify use (or export if used externally).
(SC2034)
[warning] 85-85: container_name is referenced but not assigned.
(SC2154)
🔇 Additional comments (1)
casaos-update-database-password/run.sh (1)
10-14
: LGTM! Root privilege check is properly implemented.
The script correctly verifies root privileges which is essential for Docker operations.
fi | ||
|
||
# Show instructions before editing | ||
show_instructions "$container_name" |
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.
Fix undefined variable container_name
The container_name
variable is referenced but not assigned. This should be container
based on the selection logic.
- show_instructions "$container_name"
+ show_instructions "$container"
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
show_instructions "$container_name" | |
show_instructions "$container" |
🧰 Tools
🪛 Shellcheck
[warning] 85-85: container_name is referenced but not assigned.
(SC2154)
edit_docker_compose() { | ||
local service_name=$1 | ||
local editor_choice=$2 | ||
local docker_compose_path="${APPS_DIR}/${service_name}/docker-compose.yml" |
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.
Fix undefined variable APPS_DIR
The APPS_DIR
variable is used but never defined. This could cause the script to fail.
Add this at the beginning of the script:
+# Configuration
+APPS_DIR="/var/lib/casaos/apps" # Adjust this path according to your CasaOS installation
Committable suggestion skipped: line range outside the PR's diff.
docker exec -it $container psql -U postgres -c "ALTER USER postgres WITH PASSWORD '$NEW_PASSWORD';" | ||
elif [[ $container == *"mysql"* ]] || [[ $container == *"mariadb"* ]]; then | ||
echo "Updating MySQL/MariaDB password..." | ||
docker exec -it $container mysql -u root -pDB_PASSWORD_CHANGEME -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';" |
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.
Remove hardcoded default password and add error handling
The script uses a hardcoded default password 'DB_PASSWORD_CHANGEME' which is insecure. Also, add error handling for the password update operation.
- docker exec -it $container mysql -u root -pDB_PASSWORD_CHANGEME -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';"
+ # First try with empty password (common default)
+ if ! docker exec -it $container mysql -u root -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';" 2>/dev/null; then
+ # If that fails, prompt for current password
+ read -sp "Enter current MySQL root password: " CURRENT_PASSWORD
+ echo
+ if ! docker exec -it $container mysql -u root -p"$CURRENT_PASSWORD" -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';"; then
+ echo "Failed to update MySQL password. Please verify your current password."
+ exit 1
+ fi
+ fi
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
docker exec -it $container mysql -u root -pDB_PASSWORD_CHANGEME -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';" | |
# First try with empty password (common default) | |
if ! docker exec -it $container mysql -u root -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';" 2>/dev/null; then | |
# If that fails, prompt for current password | |
read -sp "Enter current MySQL root password: " CURRENT_PASSWORD | |
echo | |
if ! docker exec -it $container mysql -u root -p"$CURRENT_PASSWORD" -e "ALTER USER 'root'@'%' IDENTIFIED BY '$NEW_PASSWORD';"; then | |
echo "Failed to update MySQL password. Please verify your current password." | |
exit 1 | |
fi | |
fi |
read -sp "Enter new password: " NEW_PASSWORD | ||
echo |
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.
🛠️ Refactor suggestion
Add password validation
The script should validate password complexity to ensure security. Consider adding checks for minimum length, special characters, etc.
+# Password validation function
+validate_password() {
+ local pass="$1"
+ if [[ ${#pass} -lt 8 ]]; then
+ echo "Password must be at least 8 characters long"
+ return 1
+ fi
+ if ! [[ "$pass" =~ [0-9] ]]; then
+ echo "Password must contain at least one number"
+ return 1
+ fi
+ if ! [[ "$pass" =~ [A-Z] ]]; then
+ echo "Password must contain at least one uppercase letter"
+ return 1
+ fi
+ return 0
+}
# Get new password
-read -sp "Enter new password: " NEW_PASSWORD
+while true; do
+ read -sp "Enter new password: " NEW_PASSWORD
+ echo
+ if validate_password "$NEW_PASSWORD"; then
+ break
+ fi
+done
-echo
Committable suggestion skipped: line range outside the PR's diff.
edit_docker_compose() { | ||
local service_name=$1 | ||
local editor_choice=$2 | ||
local docker_compose_path="${APPS_DIR}/${service_name}/docker-compose.yml" | ||
|
||
if [[ ! -f "$docker_compose_path" ]]; then | ||
echo "Error: The docker-compose.yml file does not exist for the service: $service_name" | ||
exit 1 | ||
fi | ||
|
||
# Check if the chosen editor is installed | ||
if ! command -v "$editor_choice" &> /dev/null; then | ||
echo "Error: $editor_choice is not installed. Please install it or choose another editor." | ||
exit 1 | ||
fi | ||
|
||
# Show instructions before editing | ||
show_instructions "$container_name" | ||
|
||
# Open the editor | ||
"$editor_choice" "$docker_compose_path" | ||
|
||
# Apply the changes using casaos-cli | ||
if casaos-cli app-management apply "$service_name" --file="$docker_compose_path"; then | ||
echo "Changes applied successfully." | ||
else | ||
echo "Error: Failed to apply changes. Please check the docker-compose file for errors." | ||
exit 1 | ||
fi | ||
} |
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.
🛠️ Refactor suggestion
Add safety measures for docker-compose editing
Consider adding these safety features:
- Backup the docker-compose file before editing
- Validate the docker-compose file after editing
edit_docker_compose() {
local service_name=$1
local editor_choice=$2
local docker_compose_path="${APPS_DIR}/${service_name}/docker-compose.yml"
+ # Create backup
+ cp "$docker_compose_path" "${docker_compose_path}.bak"
+ echo "Created backup at ${docker_compose_path}.bak"
+
if [[ ! -f "$docker_compose_path" ]]; then
echo "Error: The docker-compose.yml file does not exist for the service: $service_name"
exit 1
fi
# Check if the chosen editor is installed
if ! command -v "$editor_choice" &> /dev/null; then
echo "Error: $editor_choice is not installed. Please install it or choose another editor."
exit 1
fi
# Show instructions before editing
show_instructions "$container"
# Open the editor
"$editor_choice" "$docker_compose_path"
+ # Validate docker-compose file
+ if ! docker-compose -f "$docker_compose_path" config > /dev/null 2>&1; then
+ echo "Error: Invalid docker-compose file. Restoring backup..."
+ cp "${docker_compose_path}.bak" "$docker_compose_path"
+ exit 1
+ fi
+
# Apply the changes using casaos-cli
if casaos-cli app-management apply "$service_name" --file="$docker_compose_path"; then
echo "Changes applied successfully."
+ rm "${docker_compose_path}.bak"
else
echo "Error: Failed to apply changes. Please check the docker-compose file for errors."
+ echo "Restoring backup..."
+ cp "${docker_compose_path}.bak" "$docker_compose_path"
exit 1
fi
}
Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 Shellcheck
[warning] 85-85: container_name is referenced but not assigned.
(SC2154)
This is a work in progress
This pull request adds a new script
run.sh
that allows users to update the database password for their CasaOS application. The script supports both PostgreSQL and MySQL/MariaDB databases, and it provides instructions for updating the password in thedocker-compose.yml
file.The key features of this script include:
docker-compose.yml
file using the user's preferred editor (nano or vim)casaos-cli
toolThis script aims to simplify the process of updating the database password for CasaOS users, making it more accessible and user-friendly.
Summary by CodeRabbit
New Features
Documentation