openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

fix(provider): fallback to config.api_key when auth-profiles.json has no key

Open ccpty opened this issue 6 months ago • 4 comments

When a custom provider's API key is set in config.toml but not saved through the UI into auth-profiles.json, the HTTP request would be sent without an Authorization header, causing authentication failures.

This change adds a fallback in lookup_key_for_slug() that reads from the top-level config.api_key field (which is auto-decrypted by the config loader from the enc2:-prefixed value in config.toml).

Before: api_key bytes=0 (key not found, requests fail) After: api_key bytes=51 (key found via fallback, requests succeed)

Summary by CodeRabbit

  • New Features
    • Enhanced API key configuration resolution with additional fallback support, allowing authentication credentials to be sourced from multiple configuration locations.

Review Change Stack

ccpty avatar May 27 '26 02:05 ccpty

📝 Walkthrough

Walkthrough

The PR adds a fallback authentication-key resolution mechanism in lookup_key_for_slug: when the auth-profiles.json lookup returns an empty key, the function now checks config.api_key from config.toml and returns it if non-empty, before falling back to an empty string.

Changes

Auth Key Resolution Fallback

Layer / File(s) Summary
Config fallback in key lookup
src/openhuman/inference/provider/factory.rs
The lookup_key_for_slug function now checks config.api_key when auth-profiles.json lookup yields an empty key, returning the trimmed value if non-empty.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related PRs

  • tinyhumansai/openhuman#2372: Both PRs adjust how provider API keys are resolved—main PR adds a config.api_key fallback in lookup_key_for_slug, while the retrieved PR trims and validates the resolved key via OpenRouter /key before model probing.
  • tinyhumansai/openhuman#2265: Both PRs modify lookup_key_for_slug in src/openhuman/inference/provider/factory.rs—the retrieved PR routes the openai slug to lookup_openai_bearer_token, and the main PR adds a config.api_key fallback when the prior auth-profiles lookup yields an empty key.

Suggested labels

working

Poem

🐰 A fallback path, so neat and fine,
When profiles fail, config will shine,
One trim, one check, the key is found,
Authentication logic, safe and sound! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding a fallback mechanism to use config.api_key when auth-profiles.json lacks an authentication key for a provider.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 27 '26 02:05 coderabbitai[bot]

@ccpty unresolved review feedback — please address before we review.

graycyrus avatar May 27 '26 19:05 graycyrus

Unresolved review feedback from coderabbitai[bot] — please address before we review.

graycyrus avatar May 27 '26 19:05 graycyrus

@ccpty — heads up. I've re-reviewed this and the credential leakage issue from my prior REQUEST_CHANGES hasn't been addressed, and there's a CI failure on Windows (test / Rust Core Tests) that needs to be sorted out first.

The problem: the new fallback at line 651 returns config.api_key for any provider slug without checking if it's the right provider. If you have multiple cloud providers configured (openai, azure, custom), all would incorrectly receive the same credential.

The fix: scope the fallback to only apply when the slug corresponds to the provider using config.inference_url. Something like:

  • Find the cloud provider entry that matches config.inference_url (by endpoint)
  • Only apply the fallback if that entry's slug matches the incoming slug parameter

Once you fix the credential scoping AND resolve the Windows CI failure, I'll review the updated code and we can move forward. Let me know if you need clarification on the fix.

graycyrus avatar May 27 '26 20:05 graycyrus