added automatic Key Generation for Empty .env Keys

This commit is contained in:
2025-12-03 20:38:40 +01:00
parent 61d94a264f
commit 14b3eb4559
9 changed files with 106 additions and 329 deletions

11
bin/create_key.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
# This generates an environmental variable safe key for use in various applications.
KEY_LENGTH=$1
if [[ -z "$KEY_LENGTH" ]]; then
KEY_LENGTH=32
fi
KEY=$(head -c $KEY_LENGTH /dev/urandom | base64 | tr -d '=+/ ' | cut -c1-$KEY_LENGTH)
echo $KEY

5
bin/generate_key.sh Normal file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
# This script generates a random 32-character alphanumeric key
KEY=$(tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 32)
echo "$KEY"

View File

@@ -1,5 +1,7 @@
#!/bin/bash
SETUP_DIR=$PWD
# check if FAST_PATH is set, if not exit with a message
if [ -z "$FAST_PATH" ]; then
echo "FAST_PATH is not set."
@@ -15,7 +17,7 @@ cd $FAST_PATH
ENV_FILE=".env"
# Define the keywords to look for (case-insensitive search for the value part)
KEYWORDS="(key|user|password|email)"
KEYWORDS="(KEY|USER|PASSWORD|EMAIL)"
# Define the secure placeholder value for non-interactive mode
SECURE_PLACEHOLDER="REPLACED_BY_SCRIPT"
@@ -63,7 +65,7 @@ while IFS= read -r line; do
VAR_VALUE_LOWER=$(echo "$VAR_VALUE_CLEANED" | tr '[:upper:]' '[:lower:]')
# 3. Check if the value contains a sensitive keyword
if [[ "$VAR_VALUE_LOWER" =~ $KEYWORDS ]]; then
if [[ "$VAR_NAME" =~ $KEYWORDS ]]; then
echo -e "\n⚠ Sensitive variable found: **$VAR_NAME**"
echo "Current value: $VAR_VALUE_RAW"
@@ -77,8 +79,15 @@ while IFS= read -r line; do
# if user entered an empty value, use the original value
if [ -z "$NEW_VALUE" ]; then
NEW_VALUE="$VAR_VALUE_CLEANED"
echo "No input provided. Keeping original value."
if [ -z "$VAR_VALUE_CLEANED" ]; then
echo "Original value is empty. Generating a new secure key using bin/create_key..."
NEW_VALUE=$($SETUP_DIR/bin/create_key.sh)
echo "Generated Key: $NEW_VALUE"
else
NEW_VALUE="$VAR_VALUE_CLEANED"
echo "No input provided. Keeping original value."
fi
fi
# Add quotes if value contains spaces

35
bin/setup_gitea.sh Executable file
View File

@@ -0,0 +1,35 @@
# clone the repository
git clone https://git.nxs.solutions/Fast/gitea.git lib
# ask the user for the location where to setup Fast
read -p "Enter the full path where you want to set up Fast (e.g., /opt/gitea): " GITEA_PATH
# if the user input is empty, use /opt/fast as default
if [ -z "$GITEA_PATH" ]; then
GITEA_PATH="/opt/gitea"
fi
# create the directory if it doesn't exist
sudo mkdir -p "$GITEA_PATH"
sudo cp -r lib/* $GITEA_PATH
sudo rm -r lib
sudo chown -R $USER:root $GITEA_PATH
echo "Gitea has been set up at $GITEA_PATH"
# write GITEA_PATH to fast .env
source $USER/.bashrc
if [ -z "$FAST_PATH" ]; then
echo "FAST_PATH is not set. Please run setup_fast.sh first."
exit 1
else
echo "GITEA_PATH=$GITEA_PATH" >> $FAST_PATH/.env
fi
cd $GITEA_PATH
./setup.sh
echo "Gitea setup script completed."