mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
* fix(docker): re-own the configured persistent path, not only /app/data The entrypoint's root phase re-owned /app/data, while the writability probe and the boot-time check both use FLOCI_STORAGE_PERSISTENT_PATH. A named volume mounted at any other path arrived root-owned, the chown never reached it, and with non-memory storage the container refused to start. The root phase now re-owns the same directory the probe checks. The path is resolved physically first and never re-owned when it resolves to /, since it now comes from an environment variable. * test(docker): make the chown helper prove the root phase completed An empty chown log also results from a root phase that stops before the privilege drop, so the 'skips a missing dir' case could pass for the wrong reason. The helper now reports when the drop was never reached. * fix(docker): resolve the persistent path without CDPATH A relative FLOCI_STORAGE_PERSISTENT_PATH was resolved with cd, which searches CDPATH and prints the directory it picks, so a set CDPATH could send the chown to another directory or corrupt its argument. The resolution now clears it. The test stub also records chown's full arguments, so the owner and the recursive flag are asserted, not only the target. * fix(docker): compare the persistent path to / by inode bash keeps a leading // through cd -P and pwd -P, so the string test let FLOCI_STORAGE_PERSISTENT_PATH=// reach a recursive chown on the native images, which run bash as sh. -ef compares device and inode and holds in every shell, and chown --preserve-root refuses / on its own as a second guard. The test harness can now run the entrypoint under a chosen shell, and a bash case pins the // spelling that dash alone cannot reach.
99 lines
4.9 KiB
Bash
Executable File
99 lines
4.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# Starts as root, reads the bind-mounted Docker socket's group so the
|
|
# unprivileged `floci` user can reach it on any host, then re-executes this
|
|
# script as `floci` via coreutils `chroot --userspec`, which also grants that
|
|
# group as a supplementary group. The second invocation falls through to exec
|
|
# the user's command. No gosu, no usermod: both would cost a package layer the
|
|
# base image does not otherwise need.
|
|
#
|
|
# Why: on native Linux Docker, /var/run/docker.sock is owned by
|
|
# root:docker with mode 660 and the docker GID varies by distro. On
|
|
# Docker Desktop (macOS/Windows) the socket is typically root:root (GID
|
|
# 0). Without the group fix-up below, floci (uid 1001, group 0) can open
|
|
# the socket on Docker Desktop but not on native Linux — which breaks
|
|
# ECR, Lambda, and RDS service emulation there. Discovering the GID at
|
|
# runtime handles every host transparently.
|
|
|
|
set -eu
|
|
|
|
if [ "$(id -u)" = '0' ]; then
|
|
groups='0'
|
|
if [ -S /var/run/docker.sock ]; then
|
|
sock_gid="$(stat -c '%g' /var/run/docker.sock)"
|
|
if [ "$sock_gid" != '0' ]; then
|
|
groups="0,$sock_gid"
|
|
fi
|
|
fi
|
|
|
|
# Re-own the state dir for the case where a volume or host bind-mount
|
|
# arrives with ownership the floci user cannot write to. This is the same
|
|
# directory the writability probe below checks, so a volume mounted at a
|
|
# custom FLOCI_STORAGE_PERSISTENT_PATH is fixed up too. The path is resolved
|
|
# physically first, then compared by inode rather than by string: bash keeps a leading //
|
|
# (POSIX leaves it implementation-defined), so a string test would let // through.
|
|
# --preserve-root is the second guard. Ignore errors
|
|
# (read-only mounts, unusual filesystems) so the container still starts.
|
|
state_dir="${FLOCI_STORAGE_PERSISTENT_PATH:-/app/data}"
|
|
if [ -d "$state_dir" ]; then
|
|
# CDPATH is cleared so a relative path cannot be looked up elsewhere (and echoed).
|
|
state_dir_physical="$(CDPATH= cd -P -- "$state_dir" 2>/dev/null && pwd -P)" || state_dir_physical=''
|
|
if [ "$state_dir_physical" -ef / ]; then
|
|
echo "WARNING: not changing ownership of $state_dir, it resolves to /." >&2
|
|
elif [ -n "$state_dir_physical" ]; then
|
|
chown --preserve-root -R floci:root "$state_dir_physical" 2>/dev/null || true
|
|
fi
|
|
fi
|
|
|
|
# `chroot /` changes nothing but the identity: uid 1001, primary gid 0, plus the socket's
|
|
# group. Supplementary groups are set by number, so the group needs no /etc/group entry.
|
|
# --skip-chdir keeps the working directory (/app, where relative data paths resolve); GNU
|
|
# chroot would otherwise chdir to the new root.
|
|
if [ "${FLOCI_RUN_AS_ROOT:-false}" != 'true' ]; then
|
|
exec chroot --userspec=1001:0 --groups="$groups" --skip-chdir / "$0" "$@"
|
|
fi
|
|
fi
|
|
|
|
if [ "${LOCALSTACK_PARITY:-true}" != "false" ]; then
|
|
. /usr/local/bin/localstack-parity.sh
|
|
fi
|
|
|
|
# Probe the state dir as the unprivileged user and warn loudly when it is not
|
|
# writable (read-only or root-owned bind mounts the chown above could not fix).
|
|
# The container still starts: with the default in-memory storage the dir is
|
|
# unused, and non-memory modes fail fast at boot with an actionable error.
|
|
data_dir="${FLOCI_STORAGE_PERSISTENT_PATH:-/app/data}"
|
|
if [ -d "$data_dir" ]; then
|
|
probe="$data_dir/.floci-write-probe.$$"
|
|
if ! ( touch "$probe" && rm -f "$probe" ) 2>/dev/null; then
|
|
cat >&2 <<EOF
|
|
**************************************************************************
|
|
WARNING: $data_dir is not writable by $(id -un) (uid $(id -u)).
|
|
Persisted state cannot be saved there. If FLOCI_STORAGE_MODE (or any
|
|
per-service storage mode) is not 'memory', Floci will refuse to start.
|
|
Fix the volume mount permissions (it may be read-only or root-owned) or
|
|
set FLOCI_STORAGE_PERSISTENT_PATH to a writable directory.
|
|
**************************************************************************
|
|
EOF
|
|
fi
|
|
fi
|
|
|
|
# Fall back to the image's default command when invoked with no arguments.
|
|
# Some tooling re-executes this entrypoint directly with an empty argv —
|
|
# Testcontainers' LocalStackContainer does exactly that via its starter
|
|
# script — and `exec "$@"` with no argv would make the script reach EOF
|
|
# and exit 0 without ever starting the emulator. LocalStack's entrypoint
|
|
# ignores its argv entirely, so this keeps drop-in parity.
|
|
# The default matches the CMD of the image variant: native images ship
|
|
# /app/application, JVM images ship /app/quarkus-app/quarkus-run.jar.
|
|
# Both listen on 0.0.0.0 so a published port reaches Floci, which it only
|
|
# accepts with explicit consent. The JVM reads -D options only before -jar.
|
|
if [ $# -eq 0 ]; then
|
|
if [ -x /app/application ]; then
|
|
set -- /app/application -Dquarkus.http.host=0.0.0.0 -Dfloci.security.allow-unsafe-network-exposure=true
|
|
else
|
|
set -- java -Dquarkus.http.host=0.0.0.0 -Dfloci.security.allow-unsafe-network-exposure=true -jar /app/quarkus-app/quarkus-run.jar
|
|
fi
|
|
fi
|
|
|
|
exec "$@"
|