Skip to content

Commit

Permalink
šŸ“ docs: support use custom host in docker-compose setup script & reā€¦
Browse files Browse the repository at this point in the history
ā€¦generate secrets (lobehub#4576)
  • Loading branch information
cy948 authored Dec 5, 2024
1 parent d50d376 commit c0439c0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docker-compose/local/init_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"name": "Logo",
"visible": true,
"label": "",
"customCss": ".login-logo-box {}",
"customCss": ".login-logo-box {} \n.panel-logo {\n width: 80px;\n}",
"placeholder": "",
"rule": "None",
"isCustom": false
Expand Down
134 changes: 128 additions & 6 deletions docker-compose/local/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ LANGUAGE="en_US"
# Determine the source URL to download files
SOURCE_URL="https://raw.githubusercontent.com/lobehub/lobe-chat/main"

# Arg: --host
# Determine the server host
HOST=""

# 2. Parse script arguments
while getopts "fl:-:" opt; do
case $opt in
Expand All @@ -40,8 +44,12 @@ while getopts "fl:-:" opt; do
SOURCE_URL="${!OPTIND}"
OPTIND=$(($OPTIND + 1))
;;
host)
HOST="${!OPTIND}"
OPTIND=$(($OPTIND + 1))
;;
*)
echo "Usage: $0 [-f] [-l language|--lang language] [--url source]" >&2
echo "Usage: $0 [-f] [-l language|--lang language] [--url source] [--host serverhost]" >&2
exit 1
;;
esac
Expand Down Expand Up @@ -122,13 +130,43 @@ show_message() {
;;
esac
;;
security_secrect_regenerate)
case $LANGUAGE in
zh_CN)
echo "重ꖰē”Ÿęˆå®‰å…ØåƆ钄..."
;;
*)
echo "Regenerate security secrets..."
;;
esac
;;
security_secrect_regenerate_failed)
case $LANGUAGE in
zh_CN)
echo "ę— ę³•é‡ę–°ē”Ÿęˆå®‰å…ØåƆ钄ļ¼š"
;;
*)
echo "Failed to regenerate security secrets: "
;;
esac
;;
security_secrect_regenerate_report)
case $LANGUAGE in
zh_CN)
echo "安å…ØåƆ钄ē”Ÿęˆē»“ęžœå¦‚äø‹ļ¼š"
;;
*)
echo "Security secret generation results are as follows:"
;;
esac
;;
tips_run_command)
case $LANGUAGE in
zh_CN)
echo "ę‚Øå·²ē»å®Œęˆäŗ†ę‰€ęœ‰é…ē½®ę–‡ä»¶ēš„äø‹č½½ć€‚čÆ·čæč”Œä»„äø‹å‘½ä»¤åÆåŠØLobeChatļ¼š"
echo "ę‚Øå·²ē»å®Œęˆäŗ†ę‰€ęœ‰é…ē½®ć€‚čÆ·čæč”Œä»„äø‹å‘½ä»¤åÆåŠØLobeChatļ¼š"
;;
*)
echo "You have completed downloading all configuration files. Please run this command to start LobeChat:"
echo "You have completed all configurations. Please run this command to start LobeChat:"
;;
esac
;;
Expand All @@ -155,10 +193,10 @@ show_message() {
tips_warning)
case $LANGUAGE in
zh_CN)
echo "č­¦å‘Šļ¼šäøč¦åœØē”Ÿäŗ§ēŽÆ境äø­ä½æē”Øę­¤ę¼”ē¤ŗåŗ”ē”Øē؋åŗļ¼ļ¼ļ¼"
echo "č­¦å‘Šļ¼šå¦‚ęžœä½ ę­£åœØē”Ÿäŗ§ēŽÆ境äø­ä½æē”Øļ¼ŒčÆ·åœØę—„åæ—äø­ę£€ęŸ„åƆ钄ę˜Æ否已ē»ē”Ÿęˆļ¼ļ¼ļ¼"
;;
*)
echo "Warning: do not use this demo application in production!!!"
echo "Warning: If you are using it in a production environment, please check if the keys have been generated in the logs!!!"
;;
esac
;;
Expand Down Expand Up @@ -235,7 +273,91 @@ download_file "$SOURCE_URL/${FILES[3]}" "s3_data.tar.gz"
extract_file "s3_data.tar.gz" "."
rm s3_data.tar.gz

# Display final message
# ==========================
# === Regenerate Secrets ===
# ==========================

generate_key() {
if [[ -z "$1" ]]; then
echo "Usage: generate_key <length>"
return 1
fi
echo $(openssl rand -hex $1 | tr -d '\n' | fold -w $1 | head -n 1)
}

echo $(show_message "security_secrect_regenerate")

# Generate CASDOOR_SECRET
CASDOOR_SECRET=$(generate_key 32)
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "CASDOOR_SECRET"
else
# Search and replace the value of CASDOOR_SECRET in .env
sed -i "s#^AUTH_CASDOOR_SECRET=.*#AUTH_CASDOOR_SECRET=${CASDOOR_SECRET}#" .env
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "AUTH_CASDOOR_SECRET in \`.env\`"
fi
# replace `clientSecrect` in init_data.json
sed -i "s#dbf205949d704de81b0b5b3603174e23fbecc354#${CASDOOR_SECRET}#" init_data.json
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "AUTH_CASDOOR_SECRET in \`init_data.json\`"
fi
fi

# Generate Casdoor User
CASDOOR_USER="admin"
CASDOOR_PASSWORD=$(generate_key 6)
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "CASDOOR_PASSWORD"
else
# replace `password` in init_data.json
sed -i "s/"123"/${CASDOOR_PASSWORD}/" init_data.json
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "CASDOOR_PASSWORD in \`init_data.json\`"
fi
fi

# Generate Minio S3 access key
# Temporarily disable key gen for minio because
# minio can not start with a access key in envs
#S3_SECRET_ACCESS_KEY=$(generate_key 32)
#if [ $? -ne 0 ]; then
# echo $(show_message "security_secrect_regenerate_failed") "S3_SECRET_ACCESS_KEY"
#else
# # Search and replace the value of S3_SECRET_ACCESS_KEY in .env
# sed -i "s#^S3_SECRET_ACCESS_KEY=.*#S3_SECRET_ACCESS_KEY=${S3_SECRET_ACCESS_KEY}#" .env
# if [ $? -ne 0 ]; then
# echo $(show_message "security_secrect_regenerate_failed") "S3_SECRET_ACCESS_KEY in \`.env\`"
# fi
#fi

# Modify the .env file if the host is specified
if [ -n "$HOST" ]; then
# Modify env
sed -i "s/localhost/$HOST/g" .env
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "HOST in \`.env\`"
fi
# Modify casdoor init data
sed -i "s/localhost/$HOST/g" init_data.json
if [ $? -ne 0 ]; then
echo $(show_message "security_secrect_regenerate_failed") "HOST in \`init_data.json\`"
fi
fi

# Display configuration reports

echo $(show_message "security_secrect_regenerate_report")

if [ -n "$HOST" ]; then
echo -e "Server Host: $HOST"
fi
echo -e "Casdoor: \n - Username: admin\n - Password: ${CASDOOR_PASSWORD}\n - Client Secret: ${CASDOOR_SECRET}"

# ===========================
# == Display final message ==
# ===========================

printf "\n%s\n\n" "$(show_message "tips_run_command")"
print_centered "docker compose up -d" "green"
printf "\n%s" "$(show_message "tips_show_documentation")"
Expand Down

0 comments on commit c0439c0

Please sign in to comment.