fix(docker): run backend as non-root, keep env files out, never silently rotate auth keys (#93)
This commit was merged in pull request #93.
This commit is contained in:
1 parent
cf61b0ac25
commit
2427ca1cda
3 files changed
+38
-13
No files matched your search
+5
-6
@@ -54,12 +54,11 @@ playwright.config.*
|
||||
*~
|
||||
*.log
|
||||
|
||||
# Environment variables (only commit template files)
|
||||
.env
|
||||
.env*.local
|
||||
.env.development
|
||||
.env.test
|
||||
.env.production.local
|
||||
# Environment variables, in any workspace. Next.js inlines NEXT_PUBLIC_* values
|
||||
# into the bundle and the Convex CLI reads .env.local on deploy.
|
||||
**/.env
|
||||
**/.env.*
|
||||
!**/.env.example
|
||||
|
||||
# Docker configuration files (not needed inside build context)
|
||||
Dockerfile*
|
||||
|
||||
+6
-1
@@ -195,7 +195,12 @@ RUN node --version \
|
||||
&& ldd /convex/generate_key \
|
||||
&& ! ldd /convex/convex-local-backend /convex/generate_key | grep -q 'not found'
|
||||
|
||||
RUN useradd --system --user-group --home-dir /app --shell /usr/sbin/nologin otunes
|
||||
# The backend gets its own user so the web server cannot read the database or
|
||||
# instance secret. It joins the otunes group to clean up the web server's HLS
|
||||
# cache in /tmp/otunes.
|
||||
RUN useradd --system --user-group --home-dir /app --shell /usr/sbin/nologin otunes \
|
||||
&& useradd --system --user-group --groups otunes --create-home --home-dir /home/convex \
|
||||
--shell /usr/sbin/nologin convex
|
||||
|
||||
COPY --from=convex-functions /app /opt/otunes/convex
|
||||
COPY docker/generate-auth-keys.mjs /opt/otunes/generate-auth-keys.mjs
|
||||
|
||||
+27
-6
@@ -46,22 +46,34 @@ stop() {
|
||||
}
|
||||
trap 'stop; exit 0' TERM INT
|
||||
|
||||
as_convex=(setpriv --reuid=convex --regid=convex --init-groups env HOME=/home/convex)
|
||||
|
||||
convex_cli() {
|
||||
(
|
||||
cd "$FUNCTIONS_DIR"
|
||||
unset CONVEX_DEPLOYMENT CONVEX_DEPLOY_KEY
|
||||
CONVEX_SELF_HOSTED_URL="$BACKEND_URL" \
|
||||
CONVEX_SELF_HOSTED_ADMIN_KEY="$admin_key" \
|
||||
"$NODE" node_modules/convex/bin/main.js "$@"
|
||||
"${as_convex[@]}" "$NODE" node_modules/convex/bin/main.js "$@"
|
||||
)
|
||||
}
|
||||
|
||||
# Only root (the backend) may read the database and instance secret.
|
||||
# Only the convex user may read the database and instance secret. Volumes from
|
||||
# images that ran the backend as root are still owned by root, and
|
||||
# read_credentials.sh generates a new instance secret if it cannot read the old one.
|
||||
mkdir -p "$CONVEX_DATA_DIR"
|
||||
find "$CONVEX_DATA_DIR" \( ! -user convex -o ! -group convex \) \
|
||||
-exec chown -h convex:convex {} +
|
||||
chmod 700 "$CONVEX_DATA_DIR"
|
||||
|
||||
# The web server writes HLS streams here and a Convex cron deletes stale ones.
|
||||
mkdir -p /tmp/otunes
|
||||
chown otunes:otunes /tmp/otunes
|
||||
chmod -R g+rwX /tmp/otunes
|
||||
chmod g+s,o-rwx /tmp/otunes
|
||||
|
||||
log "Starting Convex backend"
|
||||
(cd /convex && umask 077 && exec ./run_backend.sh) &
|
||||
(cd /convex && umask 077 && exec "${as_convex[@]}" ./run_backend.sh) &
|
||||
backend_pid=$!
|
||||
|
||||
for _ in $(seq 1 120); do
|
||||
@@ -80,17 +92,24 @@ if ! curl -fsS "$BACKEND_URL/version" >/dev/null 2>&1; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
admin_key=$(cd /convex && ./generate_admin_key.sh)
|
||||
admin_key=$(cd /convex && umask 077 && "${as_convex[@]}" ./generate_admin_key.sh)
|
||||
|
||||
log "Deploying Convex functions"
|
||||
convex_cli deploy --typecheck disable --codegen disable
|
||||
|
||||
if [ -z "$(convex_cli env get JWKS 2>/dev/null || true)" ]; then
|
||||
# `env get` exits 0 with no output when the variable is unset. Any other failure
|
||||
# must not be read as "no keys": regenerating them signs every user out.
|
||||
if ! jwks=$(convex_cli env get JWKS); then
|
||||
log "Could not read Convex Auth signing keys"
|
||||
stop
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$jwks" ]; then
|
||||
log "Generating Convex Auth signing keys"
|
||||
"$NODE" /opt/otunes/generate-auth-keys.mjs | convex_cli env set --force
|
||||
fi
|
||||
|
||||
unset admin_key
|
||||
unset admin_key jwks
|
||||
|
||||
log "Starting web server"
|
||||
(
|
||||
@@ -100,6 +119,8 @@ log "Starting web server"
|
||||
# Server-side Convex helpers fall back to this at runtime. The browser bundle
|
||||
# is built without it, so browsers still derive their own URL.
|
||||
export NEXT_PUBLIC_CONVEX_URL="$BACKEND_URL"
|
||||
# Lets the convex group member delete stale HLS streams.
|
||||
umask 0002
|
||||
exec setpriv --reuid=otunes --regid=otunes --init-groups \
|
||||
"$NODE" apps/web/server.js
|
||||
) &
|
||||
|
||||
Reference in new issue
Block a user