Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

3.1 KB
Raw Blame
#!/bin/sh
# ODS Dashboard Entrypoint
# Configures nginx with API key injection at runtime
#
# B1 fix: Reads API key from /data/dashboard-api-key.txt if DASHBOARD_API_KEY env is empty.
# This file is generated by dashboard-api on first start.

set -e

NGINX_CONF="/etc/nginx/conf.d/default.conf"
NGINX_TEMPLATE="/etc/nginx/conf.d/default.conf.template"
API_KEY="${DASHBOARD_API_KEY:-}"
KEY_FILE="/data/dashboard-api-key.txt"
PREVIEW_PORT="${PIXEL_PREVIEW_PORT:-9437}"

case "$PREVIEW_PORT" in
    ''|*[!0-9]*)
        echo "[dashboard] ERROR: PIXEL_PREVIEW_PORT must be an integer from 1 to 65535"
        exit 1
        ;;
esac
if [ "$PREVIEW_PORT" -lt 1 ] || [ "$PREVIEW_PORT" -gt 65535 ]; then
    echo "[dashboard] ERROR: PIXEL_PREVIEW_PORT must be an integer from 1 to 65535"
    exit 1
fi
if [ ! -r "$NGINX_TEMPLATE" ]; then
    echo "[dashboard] ERROR: nginx configuration template is missing"
    exit 1
fi
if ! grep -qF '__PIXEL_PREVIEW_PORT__' "$NGINX_TEMPLATE"; then
    echo "[dashboard] ERROR: Pixel preview CSP placeholder is missing"
    exit 1
fi
if ! grep -qF '__ODS_LOCAL_LISTENER__' "$NGINX_TEMPLATE"; then
    echo "[dashboard] ERROR: dashboard sign-in boundary placeholder is missing"
    exit 1
fi

# The admin API skips sign-in only for a browser on this machine, and only
# while port 3001 is published on loopback. When BIND_ADDRESS exposes it to
# the network (or is unknown), every request must sign in.
case "${ODS_DASHBOARD_BIND:-}" in
    127.*|localhost|::1|\[::1\])
        LOCAL_LISTENER=3001
        echo "[dashboard] Local browsers on this machine use the dashboard without sign-in"
        ;;
    *)
        LOCAL_LISTENER=off
        echo "[dashboard] Dashboard is reachable from the network; sign-in is required (run: ods dashboard-login)"
        ;;
esac

cp "$NGINX_TEMPLATE" "$NGINX_CONF"
sed -i "s|__PIXEL_PREVIEW_PORT__|${PREVIEW_PORT}|g" "$NGINX_CONF"
sed -i "s|__ODS_LOCAL_LISTENER__|${LOCAL_LISTENER}|g" "$NGINX_CONF"

# If no env var, try to read from file (generated by dashboard-api)
if [ -z "$API_KEY" ] && [ -f "$KEY_FILE" ]; then
    API_KEY=$(cat "$KEY_FILE" 2>/dev/null || echo "")
    if [ -n "$API_KEY" ]; then
        echo "[dashboard] Read API key from $KEY_FILE"
    fi
fi

# If API key is available, ensure nginx config has the auth header
# The template already includes the header with ${DASHBOARD_API_KEY} placeholder
# We need to substitute it with the actual value
if [ -n "$API_KEY" ]; then
    if ! printf '%s' "$API_KEY" | grep -Eq '^[A-Za-z0-9._~+/=-]+$'; then
        echo "[dashboard] ERROR: DASHBOARD_API_KEY must be a bearer-token-safe value"
        exit 1
    fi
    echo "[dashboard] Configuring nginx with API key auth injection"
    # The allowlist above excludes sed/nginx control characters. Replace the
    # placeholder (envsubst may already have rendered it as an empty value).
    sed -i "s|Bearer \${DASHBOARD_API_KEY}|Bearer ${API_KEY}|g" "$NGINX_CONF"
    sed -i "s|Bearer \"\"|Bearer \"${API_KEY}\"|g" "$NGINX_CONF"
else
    echo "[dashboard] WARNING: No DASHBOARD_API_KEY found in env or $KEY_FILE"
    echo "[dashboard] API calls will fail with 401 until key is configured"
fi

# Start nginx
exec nginx -g 'daemon off;'