Skip to content
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 105 additions & 13 deletions scripts/nemoclaw-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,106 @@ verify_config_integrity() {
fi
}

_read_gateway_token() {
python3 - <<'PYTOKEN'
import json
try:
with open('/sandbox/.openclaw/openclaw.json') as f:
cfg = json.load(f)
print(cfg.get('gateway', {}).get('auth', {}).get('token', ''))
except Exception:
print('')
PYTOKEN
}

export_gateway_token() {
local token
token="$(_read_gateway_token)"
local marker_begin="# nemoclaw-gateway-token begin"
local marker_end="# nemoclaw-gateway-token end"

if [ -z "$token" ]; then
# Remove any stale marker blocks from rc files so revoked/old tokens
# are not re-exported in later interactive sessions.
for rc_file in "${_SANDBOX_HOME}/.bashrc" "${_SANDBOX_HOME}/.profile"; do
if [ -f "$rc_file" ] && grep -qF "$marker_begin" "$rc_file" 2>/dev/null; then
local tmp
tmp="$(mktemp)"
awk -v b="$marker_begin" -v e="$marker_end" \
'$0==b{s=1;next} $0==e{s=0;next} !s' "$rc_file" >"$tmp"
cat "$tmp" >"$rc_file"
rm -f "$tmp"
fi
done
return
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
fi
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
export OPENCLAW_GATEWAY_TOKEN="$token"

# Persist to .bashrc/.profile so interactive sessions (openshell sandbox
# connect) also see the token — same pattern as the proxy config above.
local snippet
snippet="${marker_begin}
export OPENCLAW_GATEWAY_TOKEN=\"${token}\"
${marker_end}"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

for rc_file in "${_SANDBOX_HOME}/.bashrc" "${_SANDBOX_HOME}/.profile"; do
if [ -f "$rc_file" ] && grep -qF "$marker_begin" "$rc_file" 2>/dev/null; then
local tmp
tmp="$(mktemp)"
awk -v b="$marker_begin" -v e="$marker_end" \
'$0==b{s=1;next} $0==e{s=0;next} !s' "$rc_file" >"$tmp"
printf '%s\n' "$snippet" >>"$tmp"
cat "$tmp" >"$rc_file"
rm -f "$tmp"
elif [ -w "$rc_file" ] || [ -w "$(dirname "$rc_file")" ]; then
printf '\n%s\n' "$snippet" >>"$rc_file"
fi
done
}

install_configure_guard() {
# Installs a shell function that intercepts `openclaw configure` inside the
# sandbox. The config is Landlock read-only — atomic writes to
# /sandbox/.openclaw/ fail with EACCES. Instead of a cryptic error, guide
# the user to the correct host-side workflow.
local marker_begin="# nemoclaw-configure-guard begin"
local marker_end="# nemoclaw-configure-guard end"
local snippet
read -r -d '' snippet <<'GUARD' || true
# nemoclaw-configure-guard begin
openclaw() {
case "$1" in
configure)
echo "Error: 'openclaw configure' cannot modify config inside the sandbox." >&2
echo "The sandbox config is read-only (Landlock enforced) for security." >&2
echo "" >&2
echo "To change your configuration, exit the sandbox and run:" >&2
echo " nemoclaw onboard --resume" >&2
echo "" >&2
echo "This rebuilds the sandbox with your updated settings." >&2
return 1
;;
esac
command openclaw "$@"
}
# nemoclaw-configure-guard end
GUARD

for rc_file in "${_SANDBOX_HOME}/.bashrc" "${_SANDBOX_HOME}/.profile"; do
if [ -f "$rc_file" ] && grep -qF "$marker_begin" "$rc_file" 2>/dev/null; then
local tmp
tmp="$(mktemp)"
awk -v b="$marker_begin" -v e="$marker_end" \
'$0==b{s=1;next} $0==e{s=0;next} !s' "$rc_file" >"$tmp"
printf '%s\n' "$snippet" >>"$tmp"
cat "$tmp" >"$rc_file"
rm -f "$tmp"
elif [ -w "$rc_file" ] || [ -w "$(dirname "$rc_file")" ]; then
printf '\n%s\n' "$snippet" >>"$rc_file"
fi
done
}

validate_openclaw_symlinks() {
local entry name target expected
for entry in /sandbox/.openclaw/*; do
Expand Down Expand Up @@ -205,19 +305,7 @@ configure_messaging_channels() {
print_dashboard_urls() {
local token chat_ui_base local_url remote_url

token="$(
python3 - <<'PYTOKEN'
import json
import os
path = '/sandbox/.openclaw/openclaw.json'
try:
cfg = json.load(open(path))
except Exception:
print('')
else:
print(cfg.get('gateway', {}).get('auth', {}).get('token', ''))
PYTOKEN
)"
token="$(_read_gateway_token)"

chat_ui_base="${CHAT_UI_URL%/}"
local_url="http://127.0.0.1:${PUBLIC_PORT}/"
Expand Down Expand Up @@ -410,6 +498,8 @@ if [ "$(id -u)" -ne 0 ]; then
echo "[SECURITY] Config integrity check failed — refusing to start (non-root mode)" >&2
exit 1
fi
export_gateway_token
install_configure_guard
configure_messaging_channels
validate_openclaw_symlinks
write_auth_profile
Expand Down Expand Up @@ -441,6 +531,8 @@ fi

# Verify config integrity before starting anything
verify_config_integrity
export_gateway_token
install_configure_guard

# Inject messaging channel config if provider tokens are present.
# Must run AFTER integrity check (to detect build-time tampering) and
Expand Down
Loading