|
| 1 | +#!/bin/sh |
| 2 | +# Ensure bind-mounted chains-config is writable by appuser, then drop privileges. |
| 3 | +# FakeMySQL LOCAL INFILE captures are persisted under: |
| 4 | +# chains-config/cache/fake-server-files/ |
| 5 | +# Empty host mounts (./chains-config created as root:root) otherwise yield |
| 6 | +# AccessDeniedException and appear as "cannot read files" in the UI. |
| 7 | +set -eu |
| 8 | + |
| 9 | +APP_USER="${CHAINS_APP_USER:-appuser}" |
| 10 | +APP_GROUP="${CHAINS_APP_GROUP:-appgroup}" |
| 11 | +CONFIG_DIR="${CHAINS_CONFIG_DIR:-/chains/chains-config}" |
| 12 | + |
| 13 | +ensure_dir_writable_by_app() { |
| 14 | + dir="$1" |
| 15 | + mkdir -p "$dir" |
| 16 | + if ! runuser -u "$APP_USER" -- test -w "$dir" 2>/dev/null; then |
| 17 | + chown "$APP_USER:$APP_GROUP" "$dir" || true |
| 18 | + fi |
| 19 | +} |
| 20 | + |
| 21 | +if [ "$(id -u)" = "0" ]; then |
| 22 | + mkdir -p "$CONFIG_DIR" |
| 23 | + # Host bind mounts often create the volume root as root:root (mode 755). |
| 24 | + # Chown the directory inode (not a recursive tree walk) so appuser can create |
| 25 | + # cache/plugin/preset children without rewriting user plugin contents. |
| 26 | + if ! runuser -u "$APP_USER" -- test -w "$CONFIG_DIR" 2>/dev/null; then |
| 27 | + chown "$APP_USER:$APP_GROUP" "$CONFIG_DIR" || true |
| 28 | + fi |
| 29 | + |
| 30 | + ensure_dir_writable_by_app "$CONFIG_DIR/cache" |
| 31 | + ensure_dir_writable_by_app "$CONFIG_DIR/cache/fake-server-files" |
| 32 | + ensure_dir_writable_by_app "$CONFIG_DIR/plugins" |
| 33 | + ensure_dir_writable_by_app "$CONFIG_DIR/presets" |
| 34 | + ensure_dir_writable_by_app "$CONFIG_DIR/plugin-data" |
| 35 | + |
| 36 | + # Previous root-owned captures under cache/ would still be unreadable; reclaim cache only. |
| 37 | + if [ -d "$CONFIG_DIR/cache" ]; then |
| 38 | + chown -R "$APP_USER:$APP_GROUP" "$CONFIG_DIR/cache" 2>/dev/null || true |
| 39 | + fi |
| 40 | + |
| 41 | + if command -v setpriv >/dev/null 2>&1; then |
| 42 | + exec setpriv --reuid="$APP_USER" --regid="$APP_GROUP" --init-groups -- "$@" |
| 43 | + fi |
| 44 | + exec runuser -u "$APP_USER" -- "$@" |
| 45 | +fi |
| 46 | + |
| 47 | +exec "$@" |
0 commit comments