askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
docker-entrypoint.sh · 4.0 KB · 94 lines
| 1 | #!/bin/sh |
| 2 | # |
| 3 | # DBBASIC Object Server — container entrypoint. |
| 4 | # |
| 5 | # Mirrors scripts/install.sh's first-boot admin-token behavior for the |
| 6 | # container path: if DBBASIC_ADMIN_TOKEN is not set in the environment, |
| 7 | # generate one once, persist it under the mounted data directory so it |
| 8 | # survives restarts and image rebuilds, and print it clearly to stdout on |
| 9 | # that first boot only. An existing persisted token is never overwritten, |
| 10 | # and no token is ever baked into the image — only into the runtime-only |
| 11 | # data volume. |
| 12 | # |
| 13 | # On later boots, if DBBASIC_ADMIN_TOKEN still is not set explicitly, the |
| 14 | # persisted token file is read instead of generating a new one. |
| 15 | # |
| 16 | # Started as root (the image's default), it first does the one thing that |
| 17 | # needs root: start the sandbox launcher (object_sandbox_launcher.py), which |
| 18 | # runs user objects as the separate `dbbasic-sandbox` user. Then it re-runs |
| 19 | # itself as `dbbasic` (setpriv: real/effective/saved ids, no supplementary |
| 20 | # groups) and everything below, the server included, runs unprivileged. |
| 21 | # Docker's default capabilities (SETUID, SETGID, CHOWN, KILL) are enough. |
| 22 | # Started as any other user (`--user`, Kubernetes runAsNonRoot) it skips the |
| 23 | # launcher, and user objects are refused with a reason in |
| 24 | # `object_sandbox.py --status`; admin, system and package objects run. |
| 25 | # |
| 26 | # One launcher per container: the object-daemon service runs this same |
| 27 | # entrypoint and so gets its own. |
| 28 | set -eu |
| 29 | |
| 30 | if [ "$(id -u)" = "0" ]; then |
| 31 | SOCKET="${DBBASIC_SANDBOX_LAUNCHER_SOCKET:-/run/dbbasic-sandbox/launcher.sock}" |
| 32 | if [ "${DBBASIC_SANDBOX_LAUNCHER:-on}" != "off" ]; then |
| 33 | # -I -S: root's interpreter ignores the environment, the current |
| 34 | # directory and site-packages (nothing the server user can write is |
| 35 | # ever on its path). The launcher is stdlib only. The loop |
| 36 | # restarts it if it ever exits; until it is back, user objects are |
| 37 | # refused (its workers die with it), never run without it. |
| 38 | PYTHON="$(command -v python)" |
| 39 | ( |
| 40 | while :; do |
| 41 | "${PYTHON}" -I -S /usr/local/lib/dbbasic/object_sandbox_launcher.py \ |
| 42 | --socket "${SOCKET}" --server-user dbbasic --sandbox-user dbbasic-sandbox \ |
| 43 | --python "${PYTHON}" --worker /opt/dbbasic-object-server/object_sandbox.py || true |
| 44 | echo "[dbbasic] the sandbox launcher exited; restarting it in 5 seconds" >&2 |
| 45 | sleep 5 |
| 46 | done |
| 47 | ) & |
| 48 | tries=0 |
| 49 | while [ ! -S "${SOCKET}" ] && [ "${tries}" -lt 50 ]; do |
| 50 | sleep 0.1 |
| 51 | tries=$((tries + 1)) |
| 52 | done |
| 53 | if [ ! -S "${SOCKET}" ]; then |
| 54 | echo "[dbbasic] the sandbox launcher did not start; user objects will be refused" >&2 |
| 55 | fi |
| 56 | fi |
| 57 | exec setpriv --reuid=dbbasic --regid=dbbasic --clear-groups -- "$0" "$@" |
| 58 | fi |
| 59 | |
| 60 | DATA_DIR="${DBBASIC_DATA_DIR:-/data/state}" |
| 61 | TOKEN_FILE="${DATA_DIR}/admin_token" |
| 62 | |
| 63 | mkdir -p "${DATA_DIR}" |
| 64 | # The sandbox user must not traverse, stat or chmod anything in here. |
| 65 | # Volumes created before this rule may be 0755; the server re-checks too. |
| 66 | for dir in "${DATA_DIR}" "${DBBASIC_OBJECTS_DIR:-}"; do |
| 67 | if [ -n "${dir}" ] && [ -d "${dir}" ] && [ -O "${dir}" ]; then |
| 68 | chmod 0700 "${dir}" |
| 69 | fi |
| 70 | done |
| 71 | |
| 72 | if [ -z "${DBBASIC_ADMIN_TOKEN:-}" ]; then |
| 73 | if [ -f "${TOKEN_FILE}" ]; then |
| 74 | DBBASIC_ADMIN_TOKEN="$(cat "${TOKEN_FILE}")" |
| 75 | else |
| 76 | DBBASIC_ADMIN_TOKEN="$(python -c 'import secrets; print(secrets.token_urlsafe(32))')" |
| 77 | umask 077 |
| 78 | printf '%s' "${DBBASIC_ADMIN_TOKEN}" >"${TOKEN_FILE}" |
| 79 | printf '\n' |
| 80 | printf '============================================================\n' |
| 81 | printf ' DBBASIC: generated a new admin token on first boot.\n' |
| 82 | printf ' Save it now -- it is only printed here, once:\n' |
| 83 | printf '\n %s\n\n' "${DBBASIC_ADMIN_TOKEN}" |
| 84 | printf ' Persisted at (inside the container): %s\n' "${TOKEN_FILE}" |
| 85 | printf ' To pin it explicitly (recommended once you have a real\n' |
| 86 | printf ' deployment, e.g. Coolify-managed environment variables),\n' |
| 87 | printf ' set DBBASIC_ADMIN_TOKEN yourself and it will be used instead.\n' |
| 88 | printf '============================================================\n\n' |
| 89 | fi |
| 90 | fi |
| 91 | |
| 92 | export DBBASIC_ADMIN_TOKEN |
| 93 | |
| 94 | exec "$@" |