Anurag commited on
Commit
08da72a
·
unverified ·
2 Parent(s): 931d91e882b83c

Merge pull request #199 from anurag008w/codex/investigate-key-rotator-issue

Browse files

Improve host resolution & sync-rotation handling; avoid persisting provider secrets to config by default

Files changed (1) hide show
  1. start.sh +51 -31
start.sh CHANGED
@@ -571,6 +571,18 @@ CONFIG_JSON=$(jq \
571
  | .logging.consoleLevel = $consoleLevel
572
  | .logging.consoleStyle = $consoleStyle' <<<"$CONFIG_JSON")
573
 
 
 
 
 
 
 
 
 
 
 
 
 
574
  # Optional: dynamic custom OpenAI-compatible provider registration
575
  CUSTOM_PROVIDER_NAME="${CUSTOM_PROVIDER_NAME:-}"
576
  CUSTOM_BASE_URL="${CUSTOM_BASE_URL:-}"
@@ -610,6 +622,10 @@ if [ -n "$CUSTOM_PROVIDER_NAME" ] || [ -n "$CUSTOM_BASE_URL" ] || [ -n "$CUSTOM_
610
 
611
  if [ "$CUSTOM_PROVIDER_OK" = "true" ]; then
612
  echo "Registering custom provider: $CUSTOM_PROVIDER_NAME -> $CUSTOM_BASE_URL_NORMALIZED"
 
 
 
 
613
  CONFIG_JSON=$(jq \
614
  --arg provider "$CUSTOM_PROVIDER_NAME" \
615
  --arg baseUrl "$CUSTOM_BASE_URL_NORMALIZED" \
@@ -619,10 +635,10 @@ if [ -n "$CUSTOM_PROVIDER_NAME" ] || [ -n "$CUSTOM_BASE_URL" ] || [ -n "$CUSTOM_
619
  --arg modelName "$CUSTOM_MODEL_NAME" \
620
  --argjson contextWindow "$CUSTOM_CONTEXT_WINDOW" \
621
  --argjson maxTokens "$CUSTOM_MAX_TOKENS" \
 
622
  '.models.mode = "merge" |
623
- .models.providers[$provider] = {
624
  "baseUrl": $baseUrl,
625
- "apiKey": $apiKey,
626
  "api": $apiType,
627
  "models": [{
628
  "id": $modelId,
@@ -630,7 +646,13 @@ if [ -n "$CUSTOM_PROVIDER_NAME" ] || [ -n "$CUSTOM_BASE_URL" ] || [ -n "$CUSTOM_
630
  "contextWindow": $contextWindow,
631
  "maxTokens": $maxTokens
632
  }]
633
- }' <<<"$CONFIG_JSON")
 
 
 
 
 
 
634
 
635
  if [[ "$LLM_MODEL" != "$CUSTOM_PROVIDER_NAME/"* ]]; then
636
  echo "Warning: custom provider registered, but LLM_MODEL='$LLM_MODEL' does not start with '$CUSTOM_PROVIDER_NAME/'."
@@ -669,11 +691,9 @@ _DEFAULT_HUGGINGFACE_MODELS="huggingface/deepseek-ai/DeepSeek-R1,huggingface/moo
669
  _DEFAULT_GITHUB_COPILOT_MODELS="github-copilot/gpt-5,github-copilot/gpt-4.1,github-copilot/gpt-4.1-mini"
670
 
671
  INJECTED_MODELS_PROVIDERS='{}'
672
- # Tracks providers configured with a key pool (GEMINI_API_KEYS etc.).
673
- # These providers must NOT have a static apiKey in the OpenClaw config —
674
- # the provider key rotator injects the correct rotated key per-request.
675
- # On restore, any stale apiKey saved from a previous single-key run is cleared.
676
- POOL_API_KEY_PROVIDERS='[]'
677
  inject_provider_models_from_env() {
678
  local provider="$1"
679
  local models_env="$2"
@@ -711,10 +731,11 @@ inject_provider_models_from_env() {
711
  | sed 's/_GITHUB_TOKEN$//')_BASE_URL
712
  local resolved_base_url="${!base_url_env_name:-$default_base_url}"
713
 
714
- # Only inject apiKey when NOT using a pool (pool rotation is handled by OpenClaw
715
- # reading the pool env var directly; injecting a static key would bypass rotation).
 
716
  local inject_api_key=""
717
- if [ -z "$pool_keys" ] && [ -n "$single_key" ]; then
718
  inject_api_key="$single_key"
719
  fi
720
 
@@ -759,9 +780,9 @@ inject_provider_models_from_env() {
759
  map(normalize_provider_model)
760
  | map({id: ., name: .})
761
  | unique_by(.id)')
762
- # Build provider patch: always inject models; conditionally inject apiKey, baseUrl, api.
763
- # Existing saved config wins on merge (see config-patch jq below), so this only fills
764
- # in missing fields — it never overwrites what the user already configured manually.
765
  CONFIG_JSON=$(jq \
766
  --arg provider "$provider" \
767
  --argjson models "$models_json" \
@@ -782,11 +803,11 @@ inject_provider_models_from_env() {
782
  --argjson models "$models_json" \
783
  '.[$provider] = ((.[$provider] // {}) + {models: $models})' <<<"$INJECTED_MODELS_PROVIDERS")
784
 
785
- # Track providers using key-pool so restored config's stale apiKey can be cleared.
786
- if [ -n "$pool_keys" ]; then
787
- POOL_API_KEY_PROVIDERS=$(jq \
788
  --arg provider "$provider" \
789
- '. + [$provider] | unique' <<<"$POOL_API_KEY_PROVIDERS")
790
  fi
791
  }
792
 
@@ -1204,13 +1225,14 @@ if [ -f "$EXISTING_CONFIG" ]; then
1204
  --arg consoleStyle "$OPENCLAW_CONSOLE_LOG_STYLE" \
1205
  --argjson desired "$CONFIG_JSON" \
1206
  --argjson injectedModelsProviders "$INJECTED_MODELS_PROVIDERS" \
1207
- --argjson poolApiKeyProviders "$POOL_API_KEY_PROVIDERS" \
1208
  --argjson fileLogConfigured "$OPENCLAW_FILE_LOG_LEVEL_CONFIGURED" \
1209
  --argjson consoleLogConfigured "$OPENCLAW_CONSOLE_LOG_LEVEL_CONFIGURED" \
1210
  --argjson consoleStyleConfigured "$OPENCLAW_CONSOLE_LOG_STYLE_CONFIGURED" \
1211
  --argjson whatsappEnabled "$WHATSAPP_CONFIG_ENABLED" \
1212
  --argjson telegramConfigured "$TELEGRAM_CONFIG_ENABLED" \
1213
  --argjson browserEnabled "$BROWSER_SHOULD_ENABLE" \
 
1214
  '(.channels.whatsapp // {}) as $existingWhatsapp
1215
  | (.channels.telegram // {}) as $existingTelegram
1216
  | .gateway.auth.token = $token
@@ -1253,18 +1275,18 @@ if [ -f "$EXISTING_CONFIG" ]; then
1253
  | if (($desired.models.providers // {} | length) > 0) then
1254
  reduce ($desired.models.providers // {} | to_entries)[] as $pe (.;
1255
  # Propagate custom/new providers from desired config that are absent in existing.
1256
- # For known providers that already exist, merge in baseUrl/apiKey/api from env.
1257
- # ENV WINS over existing config: if an env var supplies a value it always takes
1258
- # effect (e.g. rotating an API key in HF Secrets is immediately reflected).
1259
- # If the env var is unset/empty the existing config value is preserved.
1260
  if .models.providers[$pe.key] == null then
1261
  .models.providers[$pe.key] = $pe.value
1262
  else
1263
  .models.providers[$pe.key] = (
1264
  (.models.providers[$pe.key] // {})
1265
  * (
1266
- {"baseUrl": $pe.value.baseUrl, "apiKey": $pe.value.apiKey, "api": $pe.value.api}
1267
- | with_entries(select(.value != null and .value != ""))
 
1268
  )
1269
  )
1270
  end
@@ -1272,12 +1294,10 @@ if [ -f "$EXISTING_CONFIG" ]; then
1272
  else
1273
  .
1274
  end
1275
- | if (($poolApiKeyProviders | length) > 0) then
1276
- # BUG FIX: Pool providers must NOT have a static apiKey in the OpenClaw config.
1277
- # The provider key rotator injects the correct rotated key per-request.
1278
- # If a previous single-key run saved an apiKey for this provider, clear it now
1279
- # so OpenClaw does not keep using that one key and ignoring the rotation pool.
1280
- reduce $poolApiKeyProviders[] as $prov (.;
1281
  if .models.providers[$prov] != null then
1282
  del(.models.providers[$prov].apiKey)
1283
  else . end
 
571
  | .logging.consoleLevel = $consoleLevel
572
  | .logging.consoleStyle = $consoleStyle' <<<"$CONFIG_JSON")
573
 
574
+ # Security: provider API keys and HF owner tokens come from runtime env/Space
575
+ # secrets and must not be persisted into /home/node/.openclaw/openclaw.json.
576
+ # The rotator/env provider discovery injects credentials at request time. A
577
+ # legacy opt-in exists only for users who explicitly accept writing secrets to
578
+ # the synced config file.
579
+ OPENCLAW_CONFIG_SECRETS_ALLOWED=false
580
+ if [[ "${HUGGINGCLAW_ALLOW_CONFIG_SECRETS:-}" =~ ^(1|true|yes|on)$ ]]; then
581
+ OPENCLAW_CONFIG_SECRETS_ALLOWED=true
582
+ echo "Warning: HUGGINGCLAW_ALLOW_CONFIG_SECRETS is enabled; provider apiKey values may be written to openclaw.json."
583
+ fi
584
+ ENV_API_KEY_PROVIDERS='[]'
585
+
586
  # Optional: dynamic custom OpenAI-compatible provider registration
587
  CUSTOM_PROVIDER_NAME="${CUSTOM_PROVIDER_NAME:-}"
588
  CUSTOM_BASE_URL="${CUSTOM_BASE_URL:-}"
 
622
 
623
  if [ "$CUSTOM_PROVIDER_OK" = "true" ]; then
624
  echo "Registering custom provider: $CUSTOM_PROVIDER_NAME -> $CUSTOM_BASE_URL_NORMALIZED"
625
+ if [ "$OPENCLAW_CONFIG_SECRETS_ALLOWED" != "true" ] && [ -n "$CUSTOM_API_KEY" ]; then
626
+ echo "Warning: custom provider apiKey was not written to openclaw.json; set HUGGINGCLAW_ALLOW_CONFIG_SECRETS=true to opt into legacy persistence."
627
+ fi
628
+
629
  CONFIG_JSON=$(jq \
630
  --arg provider "$CUSTOM_PROVIDER_NAME" \
631
  --arg baseUrl "$CUSTOM_BASE_URL_NORMALIZED" \
 
635
  --arg modelName "$CUSTOM_MODEL_NAME" \
636
  --argjson contextWindow "$CUSTOM_CONTEXT_WINDOW" \
637
  --argjson maxTokens "$CUSTOM_MAX_TOKENS" \
638
+ --argjson allowSecrets "$OPENCLAW_CONFIG_SECRETS_ALLOWED" \
639
  '.models.mode = "merge" |
640
+ .models.providers[$provider] = ({
641
  "baseUrl": $baseUrl,
 
642
  "api": $apiType,
643
  "models": [{
644
  "id": $modelId,
 
646
  "contextWindow": $contextWindow,
647
  "maxTokens": $maxTokens
648
  }]
649
+ } + (if $allowSecrets and $apiKey != "" then {"apiKey": $apiKey} else {} end))' <<<"$CONFIG_JSON")
650
+
651
+ if [ "$OPENCLAW_CONFIG_SECRETS_ALLOWED" != "true" ] && [ -n "$CUSTOM_API_KEY" ]; then
652
+ ENV_API_KEY_PROVIDERS=$(jq \
653
+ --arg provider "$CUSTOM_PROVIDER_NAME" \
654
+ '. + [$provider] | unique' <<<"$ENV_API_KEY_PROVIDERS")
655
+ fi
656
 
657
  if [[ "$LLM_MODEL" != "$CUSTOM_PROVIDER_NAME/"* ]]; then
658
  echo "Warning: custom provider registered, but LLM_MODEL='$LLM_MODEL' does not start with '$CUSTOM_PROVIDER_NAME/'."
 
691
  _DEFAULT_GITHUB_COPILOT_MODELS="github-copilot/gpt-5,github-copilot/gpt-4.1,github-copilot/gpt-4.1-mini"
692
 
693
  INJECTED_MODELS_PROVIDERS='{}'
694
+ # Tracks providers configured from runtime env/Space secrets. These providers
695
+ # must not persist static apiKey values in openclaw.json; credentials stay in env
696
+ # and the key rotator/provider discovery injects them per request.
 
 
697
  inject_provider_models_from_env() {
698
  local provider="$1"
699
  local models_env="$2"
 
731
  | sed 's/_GITHUB_TOKEN$//')_BASE_URL
732
  local resolved_base_url="${!base_url_env_name:-$default_base_url}"
733
 
734
+ # Do not persist env/Space secrets into openclaw.json. The rotator and
735
+ # provider env discovery use the runtime env vars directly. Legacy config
736
+ # secret persistence requires an explicit opt-in.
737
  local inject_api_key=""
738
+ if [ "$OPENCLAW_CONFIG_SECRETS_ALLOWED" = "true" ] && [ -z "$pool_keys" ] && [ -n "$single_key" ]; then
739
  inject_api_key="$single_key"
740
  fi
741
 
 
780
  map(normalize_provider_model)
781
  | map({id: ., name: .})
782
  | unique_by(.id)')
783
+ # Build provider patch: always inject models/base URL/API metadata. apiKey is
784
+ # included only when the user explicitly opts into config secret persistence.
785
+ # Existing saved config is sanitized later for env-managed providers.
786
  CONFIG_JSON=$(jq \
787
  --arg provider "$provider" \
788
  --argjson models "$models_json" \
 
803
  --argjson models "$models_json" \
804
  '.[$provider] = ((.[$provider] // {}) + {models: $models})' <<<"$INJECTED_MODELS_PROVIDERS")
805
 
806
+ # Track env-configured providers so restored config's stale apiKey can be cleared.
807
+ if [ "$OPENCLAW_CONFIG_SECRETS_ALLOWED" != "true" ] && { [ -n "$single_key" ] || [ -n "$pool_keys" ]; }; then
808
+ ENV_API_KEY_PROVIDERS=$(jq \
809
  --arg provider "$provider" \
810
+ '. + [$provider] | unique' <<<"$ENV_API_KEY_PROVIDERS")
811
  fi
812
  }
813
 
 
1225
  --arg consoleStyle "$OPENCLAW_CONSOLE_LOG_STYLE" \
1226
  --argjson desired "$CONFIG_JSON" \
1227
  --argjson injectedModelsProviders "$INJECTED_MODELS_PROVIDERS" \
1228
+ --argjson envApiKeyProviders "$ENV_API_KEY_PROVIDERS" \
1229
  --argjson fileLogConfigured "$OPENCLAW_FILE_LOG_LEVEL_CONFIGURED" \
1230
  --argjson consoleLogConfigured "$OPENCLAW_CONSOLE_LOG_LEVEL_CONFIGURED" \
1231
  --argjson consoleStyleConfigured "$OPENCLAW_CONSOLE_LOG_STYLE_CONFIGURED" \
1232
  --argjson whatsappEnabled "$WHATSAPP_CONFIG_ENABLED" \
1233
  --argjson telegramConfigured "$TELEGRAM_CONFIG_ENABLED" \
1234
  --argjson browserEnabled "$BROWSER_SHOULD_ENABLE" \
1235
+ --argjson allowSecrets "$OPENCLAW_CONFIG_SECRETS_ALLOWED" \
1236
  '(.channels.whatsapp // {}) as $existingWhatsapp
1237
  | (.channels.telegram // {}) as $existingTelegram
1238
  | .gateway.auth.token = $token
 
1275
  | if (($desired.models.providers // {} | length) > 0) then
1276
  reduce ($desired.models.providers // {} | to_entries)[] as $pe (.;
1277
  # Propagate custom/new providers from desired config that are absent in existing.
1278
+ # For known providers that already exist, merge in baseUrl/api metadata from env.
1279
+ # apiKey is merged only under explicit legacy opt-in; otherwise env/Space
1280
+ # secrets stay in process env and are stripped below for env-managed providers.
 
1281
  if .models.providers[$pe.key] == null then
1282
  .models.providers[$pe.key] = $pe.value
1283
  else
1284
  .models.providers[$pe.key] = (
1285
  (.models.providers[$pe.key] // {})
1286
  * (
1287
+ ({"baseUrl": $pe.value.baseUrl, "api": $pe.value.api}
1288
+ + (if $allowSecrets then {"apiKey": $pe.value.apiKey} else {} end)
1289
+ | with_entries(select(.value != null and .value != "")))
1290
  )
1291
  )
1292
  end
 
1294
  else
1295
  .
1296
  end
1297
+ | if (($envApiKeyProviders | length) > 0) then
1298
+ # Env/Space secrets must not be persisted in openclaw.json. If a
1299
+ # previous boot wrote apiKey for an env-managed provider, clear it now.
1300
+ reduce $envApiKeyProviders[] as $prov (.;
 
 
1301
  if .models.providers[$prov] != null then
1302
  del(.models.providers[$prov].apiKey)
1303
  else . end