2025-07-21 16:00:46 -07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
if [ -z "$NODEVER" ]; then
|
|
|
|
|
NODEVER="20";
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -z "$environment" ]; then
|
|
|
|
|
environment="PROD"
|
|
|
|
|
fi
|
|
|
|
|
|
2025-07-24 09:22:43 -07:00
|
|
|
# Create user with proper error handling
|
|
|
|
|
if ! id -u $user >/dev/null 2>&1; then
|
|
|
|
|
echo "Creating user $user with UID $uid"
|
|
|
|
|
adduser -u $uid -m -s /bin/bash $user || {
|
|
|
|
|
echo "Failed to create user $user with UID $uid"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
echo "User $user already exists"
|
|
|
|
|
fi
|
2025-07-21 16:00:46 -07:00
|
|
|
|
2026-10-20 15:49:18 -07:00
|
|
|
# ---- root never follows customer symlinks under /home/$user ----
|
|
|
|
|
# /home/$user is the customer's bind mount: they can plant symlinks in it, and
|
|
|
|
|
# root touching/chowning/writing through one (e.g. logs/nginx -> /etc) gives
|
|
|
|
|
# them that target. logs/ is WHP-owned and nginx opens files in it as root, so
|
|
|
|
|
# symlinks there are removed and logged; everything under /home/$user is created/written AS THE CUSTOMER.
|
|
|
|
|
as_user() {
|
|
|
|
|
if command -v setpriv >/dev/null 2>&1; then
|
|
|
|
|
setpriv --reuid="$uid" --regid="$uid" --init-groups -- "$@"
|
|
|
|
|
else
|
|
|
|
|
runuser -u "$user" -- "$@"
|
|
|
|
|
fi
|
|
|
|
|
}
|
2026-10-20 16:04:21 -07:00
|
|
|
# Fail closed: refusing to start only hurts the customer who planted the path,
|
|
|
|
|
# which is preferred to root following it.
|
|
|
|
|
die() {
|
|
|
|
|
echo "entrypoint: FATAL: $* - refusing to start" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
# Unlink as root: rm on the final component never touches the link target and
|
|
|
|
|
# the customer cannot deny it. Still a symlink afterwards -> refuse to start.
|
2026-10-20 15:49:18 -07:00
|
|
|
drop_symlink() {
|
|
|
|
|
if [ -L "$1" ]; then
|
2026-10-20 15:52:01 -07:00
|
|
|
echo "entrypoint: SECURITY: $1 was a symlink to '$(readlink "$1")' - removing" >&2
|
2026-10-20 16:04:21 -07:00
|
|
|
rm -f -- "$1"
|
|
|
|
|
[ -L "$1" ] && die "could not remove symlink $1"
|
2026-10-20 15:49:18 -07:00
|
|
|
fi
|
2026-10-20 16:04:21 -07:00
|
|
|
return 0
|
2026-10-20 15:49:18 -07:00
|
|
|
}
|
|
|
|
|
if [ -L /home/$user ] || [ ! -d /home/$user ]; then
|
2026-10-20 16:04:21 -07:00
|
|
|
die "/home/$user is not a real directory"
|
2026-10-20 15:49:18 -07:00
|
|
|
fi
|
|
|
|
|
chown -h $user:$user /home/$user
|
|
|
|
|
# app/ may legitimately be a symlink (releases/current deploys): it is left
|
|
|
|
|
# alone, and every root-side operation on it below runs as the customer.
|
|
|
|
|
drop_symlink /home/$user/logs
|
2026-10-20 16:04:21 -07:00
|
|
|
if [ -d /home/$user/logs ]; then
|
|
|
|
|
# nginx opens its logs here as root. find without -L never follows symlinks;
|
|
|
|
|
# -delete unlinks the link itself. Re-checked fail-closed before nginx starts.
|
|
|
|
|
find /home/$user/logs -xdev -type l \
|
2026-10-20 15:52:01 -07:00
|
|
|
-printf 'entrypoint: SECURITY: %p was a symlink to %l - removed\n' -delete >&2 || true
|
2026-10-20 15:49:18 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
as_user mkdir -p /home/$user/app /home/$user/logs/nginx /home/$user/logs/nodejs
|
2025-07-21 16:00:46 -07:00
|
|
|
|
|
|
|
|
# Link log directories
|
|
|
|
|
rm -rf /var/log/nginx
|
|
|
|
|
ln -s /home/$user/logs/nginx /var/log/nginx
|
|
|
|
|
ln -s /home/$user/logs/nodejs /var/log/nodejs
|
|
|
|
|
|
|
|
|
|
# Configure nginx for reverse proxy
|
|
|
|
|
/scripts/create-nginx-config.sh
|
|
|
|
|
|
2026-10-20 15:49:18 -07:00
|
|
|
# Set ownership and permissions. GNU chown -R defaults to -P: symlinks are
|
|
|
|
|
# lchown()ed, never followed.
|
2025-07-21 16:00:46 -07:00
|
|
|
chown -R $user:$user /home/$user
|
2026-10-20 12:34:38 -07:00
|
|
|
# Do NOT `chmod -R 755` here. /home/$user is the customer's bind-mounted
|
|
|
|
|
# directory; a recursive 755 on every start made every file (.env, app
|
|
|
|
|
# secrets) world-readable AND executable, and reverted any tighter
|
|
|
|
|
# permissions the customer had set. nginx workers run as `nginx` (not the
|
|
|
|
|
# customer) and serve /static/ straight from /home/$user/app/public/, so they
|
|
|
|
|
# only need to traverse directories. Normalise that and nothing else: open the
|
|
|
|
|
# home dir itself (adduser -m creates it 0700 when it does not exist yet), and
|
|
|
|
|
# add o+x to directories that lack it. Regular files are never touched.
|
2026-10-20 15:49:18 -07:00
|
|
|
# The find runs AS THE CUSTOMER: `-type d` without -L skips symlinks, and if a
|
|
|
|
|
# dir is swapped for a symlink before chmod runs, the customer can only reach
|
|
|
|
|
# what they already own.
|
2026-10-20 12:34:38 -07:00
|
|
|
chmod 755 /home/$user
|
2026-10-20 15:49:18 -07:00
|
|
|
as_user find /home/$user -type d ! -perm -o=x -exec chmod o+x {} + 2>/dev/null || true
|
2025-07-21 16:00:46 -07:00
|
|
|
|
2026-10-20 16:04:21 -07:00
|
|
|
# nginx opens its logs under /home/$user/logs as root: last fail-closed check
|
|
|
|
|
# immediately before it starts. logs/, logs/nginx, logs/nodejs must be real
|
|
|
|
|
# dirs and nothing under logs/ may be other than a regular file or directory.
|
|
|
|
|
for d in /home/$user/logs /home/$user/logs/nginx /home/$user/logs/nodejs; do
|
|
|
|
|
[ -d "$d" ] && [ ! -L "$d" ] || die "$d is not a real directory"
|
|
|
|
|
done
|
|
|
|
|
bad=$(find /home/$user/logs -xdev ! -type f ! -type d -print -quit 2>&1)
|
|
|
|
|
[ -z "$bad" ] || die "unsafe entry under /home/$user/logs: $bad"
|
|
|
|
|
|
2025-07-21 16:00:46 -07:00
|
|
|
# Start nginx
|
|
|
|
|
nginx
|
|
|
|
|
|
|
|
|
|
if [[ $environment == 'DEV' ]]; then
|
|
|
|
|
echo "Starting Dev Deployment"
|
|
|
|
|
|
|
|
|
|
# Ensure microdnf is available for installing additional packages in DEV mode
|
|
|
|
|
if ! command -v microdnf &> /dev/null; then
|
|
|
|
|
echo "microdnf not found, installing with dnf..."
|
|
|
|
|
dnf install -y microdnf && dnf clean all
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Install Memcached for session storage in DEV mode with memory limit
|
|
|
|
|
microdnf install -y memcached
|
|
|
|
|
# Start memcached with 32MB memory limit
|
|
|
|
|
nohup memcached -d -u $user -p 11211 -m 32
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Start cron for log rotation and backups
|
|
|
|
|
/usr/sbin/crond
|
|
|
|
|
|
2025-07-24 09:01:08 -07:00
|
|
|
# Create app directory if it doesn't exist
|
|
|
|
|
if [ ! -d /home/$user/app ]; then
|
|
|
|
|
echo "Creating app directory at /home/$user/app"
|
2026-10-20 15:49:18 -07:00
|
|
|
as_user mkdir -p /home/$user/app
|
2025-07-21 16:00:46 -07:00
|
|
|
fi
|
|
|
|
|
|
2025-07-24 09:01:08 -07:00
|
|
|
# If app directory is empty, copy the simple-website example
|
|
|
|
|
if [ -z "$(ls -A /home/$user/app)" ]; then
|
|
|
|
|
echo "App directory is empty, copying simple-website example..."
|
2026-10-20 15:49:18 -07:00
|
|
|
as_user cp -r /examples/simple-website/. /home/$user/app/
|
2025-07-24 09:01:08 -07:00
|
|
|
echo "Copied simple-website example to provide a working application"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Now there's always an app in the user directory (either user's or example)
|
|
|
|
|
cd /home/$user/app
|
2025-07-24 09:22:43 -07:00
|
|
|
|
|
|
|
|
# Verify user exists and show info
|
|
|
|
|
echo "Verifying user setup:"
|
|
|
|
|
id $user || { echo "ERROR: User $user does not exist!"; exit 1; }
|
|
|
|
|
|
|
|
|
|
# Install dependencies as the user
|
|
|
|
|
echo "Installing npm dependencies as user $user..."
|
2025-07-24 09:01:08 -07:00
|
|
|
su -c "npm install" $user
|
|
|
|
|
|
|
|
|
|
# Check if ecosystem.config.js exists, if not generate it
|
|
|
|
|
if [ ! -f /home/$user/app/ecosystem.config.js ]; then
|
|
|
|
|
echo "No ecosystem.config.js found, generating from package.json..."
|
2026-10-20 15:49:18 -07:00
|
|
|
# As the customer: it reads their package.json and writes into their app dir.
|
|
|
|
|
drop_symlink /home/$user/app/ecosystem.config.js
|
|
|
|
|
as_user /scripts/generate-ecosystem-config.sh "$user" "/home/$user/app"
|
2025-07-24 09:01:08 -07:00
|
|
|
fi
|
|
|
|
|
|
2026-05-05 06:59:52 -07:00
|
|
|
# Mirror logs to docker logs in the background.
|
|
|
|
|
# Use -F (capital) so logrotate-recreated files keep streaming.
|
|
|
|
|
tail -F /home/$user/logs/nginx/access.log \
|
|
|
|
|
/home/$user/logs/nginx/error.log \
|
|
|
|
|
/home/$user/logs/nodejs/app.log \
|
|
|
|
|
/home/$user/logs/nodejs/out.log \
|
|
|
|
|
/home/$user/logs/nodejs/error.log 2>/dev/null &
|
|
|
|
|
|
2026-08-03 07:54:32 -07:00
|
|
|
# Start PM2 under dumb-init so it becomes PID 1 (with proper signal forwarding
|
2026-05-05 06:59:52 -07:00
|
|
|
# and zombie reaping for nginx/crond/memcached children that reparent here).
|
2026-08-03 07:54:32 -07:00
|
|
|
# When pm2 exits (e.g. max_restarts exhausted), dumb-init exits and Docker's
|
2026-05-05 06:59:52 -07:00
|
|
|
# restart policy brings the container back.
|
2026-08-03 07:54:32 -07:00
|
|
|
# (dumb-init replaces tini, which is not packaged in EPEL 10.)
|
2026-08-03 09:06:10 -07:00
|
|
|
#
|
|
|
|
|
# --single-child is required for parity with tini. dumb-init defaults to
|
|
|
|
|
# setsid() plus forwarding signals to the whole process group, whereas tini
|
|
|
|
|
# without -g forwards only to its direct child. Without this flag a
|
|
|
|
|
# `docker stop` would signal the tenant's node process directly and at the
|
|
|
|
|
# same moment as pm2, bypassing pm2's kill_timeout shutdown sequencing.
|
2026-08-03 07:54:32 -07:00
|
|
|
echo "Starting PM2 as user $user (under dumb-init as PID 1)..."
|
2025-07-24 09:53:01 -07:00
|
|
|
cd /home/$user/app
|
2026-08-03 09:06:10 -07:00
|
|
|
exec dumb-init --single-child -- su - $user -c "cd /home/$user/app && NODE_ENV=production pm2 start ecosystem.config.js --no-daemon"
|