Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ HOSTNAMES=example.com
# NEXTMTA_HOST=smtp.ethereal.email
# NEXTMTA_PORT=587
# NEXTMTA_USER=gsblpjxjdvhnqkgr@ethereal.email
# NEXTMTA_PASS=zDZpDnSsFf11Zfvtv5
# NEXTMTA_PASS=zDZpDnSsFf11Zfvtv5

# External MongoDB configuration (optional - if set, will use external MongoDB instead of docker container)
# Format: mongodb://[username:password@]host[:port][/database][?options]
# Examples:
# mongodb://mongodb.example.com:27017/wildduck
# mongodb://user:pass@mongodb.example.com:27017/wildduck?authSource=admin
# mongodb://mongodb.example.com:27017/wildduck?replicaSet=rs0
# MONGO_URL=mongodb://mongodb.example.com:27017/wildduck
59 changes: 59 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,65 @@ else
echo "NextMTA configuration not found in .env file, skipping nextMTA setup"
fi

# Configure external MongoDB from environment variables if set
if [ -n "$MONGO_URL" ]; then
echo "Configuring external MongoDB from environment variables"

# Escape special characters in MONGO_URL for sed
MONGO_URL_ESCAPED=$(echo "$MONGO_URL" | sed 's/[[\.*^$()+?{|]/\\&/g')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The sed command to escape MONGO_URL has a syntax error. The character class [["\.*^$()+?{|] is not terminated with a ]. This will cause the script to fail with an unterminated [ error when MONGO_URL is set.

Additionally, this command is attempting to escape regex metacharacters. For a sed replacement string, you should instead escape &, \, and the delimiter (in this case |).

Suggested change
MONGO_URL_ESCAPED=$(echo "$MONGO_URL" | sed 's/[[\.*^$()+?{|]/\\&/g')
MONGO_URL_ESCAPED=$(echo "$MONGO_URL" | sed -e 's/\\/\\\\/g' -e 's/|/\\|/g' -e 's/&/\\&/g')


# Update Wildduck dbs.toml
WILDDUCK_DBS="./config-generated/config-generated/wildduck/dbs.toml"
sed -i "s|mongo = \"mongodb://mongo:27017/wildduck\"|mongo = \"$MONGO_URL_ESCAPED\"|" "$WILDDUCK_DBS"

# Update Zone-MTA dbs-production.toml
ZONEMTA_DBS_PROD="./config-generated/config-generated/zone-mta/dbs-production.toml"
sed -i "s|mongo = \"mongodb://mongo:27017/wildduck\"|mongo = \"$MONGO_URL_ESCAPED\"|" "$ZONEMTA_DBS_PROD"

# Update Zone-MTA dbs-development.toml (extract database name from URL if needed)
ZONEMTA_DBS_DEV="./config-generated/config-generated/zone-mta/dbs-development.toml"
# Extract database name from MONGO_URL (default to wildduck if not specified)
MONGO_DB_NAME=$(echo "$MONGO_URL" | sed -n 's|.*/\([^?]*\).*|\1|p')
if [ -z "$MONGO_DB_NAME" ]; then
MONGO_DB_NAME="wildduck"
fi
# Replace the database name in the connection string for development
MONGO_URL_DEV=$(echo "$MONGO_URL" | sed "s|/[^/]*$|/$MONGO_DB_NAME|")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The sed command used to generate MONGO_URL_DEV incorrectly strips query parameters from the original MONGO_URL. The regex s|/[^/]*$|... matches from the last / to the end of the string, which includes any ?options part. For example, if MONGO_URL is mongodb://.../db?authSource=admin, MONGO_URL_DEV will become mongodb://.../db, losing the authSource option. This is likely to cause connection issues.

Suggested change
MONGO_URL_DEV=$(echo "$MONGO_URL" | sed "s|/[^/]*$|/$MONGO_DB_NAME|")
MONGO_URL_DEV=$(echo "$MONGO_URL" | sed 's#/[^/?]*#/'"$MONGO_DB_NAME"'#')

MONGO_URL_DEV_ESCAPED=$(echo "$MONGO_URL_DEV" | sed 's/[[\.*^$()+?{|]/\\&/g')
sed -i "s|mongo = \"mongodb://mongo:27017/zone-mta\"|mongo = \"$MONGO_URL_DEV_ESCAPED\"|" "$ZONEMTA_DBS_DEV"

# Update Haraka wildduck.yaml
HARAKA_WILDDUCK="./config-generated/config-generated/haraka/wildduck.yaml"
sed -i "s|url: \"mongodb://mongo:27017/wildduck\"|url: \"$MONGO_URL_ESCAPED\"|" "$HARAKA_WILDDUCK"

echo "External MongoDB configured: $MONGO_URL"

# Remove mongo from depends_on in docker-compose.yml if using external MongoDB
DOCKER_COMPOSE="./config-generated/docker-compose.yml"
echo "Removing mongo service dependencies from docker-compose.yml"

# Remove mongo from depends_on lists (keep redis)
# Handle different indentation levels
sed -i '/depends_on:/,/^ [a-z]/ { /^[[:space:]]*- mongo$/s/^/#/; }' "$DOCKER_COMPOSE"

# Comment out the mongo service definition
# Find the mongo service block and comment it out
awk '
/^ mongo:/ { in_mongo=1 }
in_mongo && /^ [a-z]/ && !/^ mongo/ { in_mongo=0 }
in_mongo { print "#" $0; next }
{ print }
' "$DOCKER_COMPOSE" > "$DOCKER_COMPOSE.tmp" && mv "$DOCKER_COMPOSE.tmp" "$DOCKER_COMPOSE"

# Comment out mongo volume
sed -i 's/^ mongo:$/# mongo: # Removed: using external MongoDB/' "$DOCKER_COMPOSE"
Comment on lines +219 to +231

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The sed and awk commands used to modify docker-compose.yml are brittle. They rely on specific indentation (e.g., exactly two spaces) and assumptions about service naming (/^ [a-z]/). If the formatting of docker-compose.yml changes, these commands may fail or have unintended side effects. The note at the end of the script to manually review the file indicates awareness of this fragility.

For more robust modifications of YAML files, consider using a YAML-aware command-line tool like yq. This would make the script more reliable against formatting changes.


echo "MongoDB service and dependencies removed from docker-compose.yml"
echo "Note: Please review docker-compose.yml and ensure mongo dependencies are properly removed"
else
echo "MONGO_URL not set in .env file, using default MongoDB container"
fi

# Wildduck
sed -i "s/hostname=\"email.example.com\"/hostname=\"$HOSTNAME\"/" ./config-generated/config-generated/wildduck/imap.toml
sed -i "s/hostname=\"email.example.com\"/hostname=\"$HOSTNAME\"/" ./config-generated/config-generated/wildduck/pop3.toml
Expand Down