Skip to content

feat(auth): add ~/.infer/auth.json as userspace provider key fallback - #1169

Merged
edenreich merged 8 commits into
mainfrom
fix/issue-1167
Sep 3, 2026
Merged

edenreich merged 8 commits into
mainfrom
fix/issue-1167

Conversation

@inference-gateway-maintainer

@inference-gateway-maintainer inference-gateway-maintainer Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1167

Summary

Adds ~/.infer/auth.json as a userspace fallback for provider API keys when they are not supplied by a higher-precedence source. Keys now resolve per key with first hit winning: system environment, project .env, then ~/.infer/auth.json (a flat JSON map of provider key env vars). The read is implemented once in config/ and shared by all three resolution paths, so the fallback behaves identically for the gateway container, gateway binary mode, and A2A agent containers.

Changes

  • New config/auth.go: AuthFilePath() and LoadAuthKeys() - reads ~/.infer/auth.json as map[string]string. Missing/unreadable/empty files degrade silently (no keys, no warnings); malformed JSON yields no keys (nothing partially parsed) plus a warning; permissions broader than 0600 warn. config cannot import the platform logger (the logger imports config), so warnings are returned for callers to log.
  • internal/gateway/manager.go runContainer() (container mode): the whole .env still goes via --env-file; -e is now also passed for keys resolved from the auth.json fallback, but never for keys present in .env (so auth.json cannot override the project .env, and the system env -e still wins over --env-file).
  • internal/gateway/manager.go loadEnvironment() (binary mode): explicit precedence via first-hit dedup - system env, then .env lines for keys not already set, then auth.json. This also makes binary mode deterministic instead of relying on the child process' duplicate-env handling, matching container mode.
  • internal/agent/application/agent_manager.go: auth.json is the final fallback when resolving env vars for A2A agent containers (after .env, system environment, and the agent's own config value; empty config values fall through to the fallback). The resolution loop is extracted into resolveAgentEnv so the order is unit-testable without docker.
  • Tests: config/auth_test.go (loader cases: missing, empty, malformed, valid, broad perms, unreadable), internal/gateway/manager_env_test.go (binary-mode precedence: system > .env > auth.json, empty values skipped, missing/malformed degrade), TestResolveAgentEnv (A2A resolution order).
  • Docs: README Quick Start and docs/configuration-reference.md now document ~/.infer/auth.json and the precedence order.

Behavior change

Binary mode previously appended .env lines after the system environment, and Go's exec keeps the last duplicate, so .env silently overrode system env vars. This PR flips binary mode to system-wins, matching container mode and the documented order.

Notes

  • No changes to sandbox ProtectedPaths: auth.json was already protected (agents cannot read or edit it).
  • infer auth login / keychain integration intentionally out of scope (follow-up); the file is trivially hand-editable.

Follow-ups

provider api keys now resolve per key with first hit winning: system environment, project .env, then the new ~/.infer/auth.json fallback (a flat json map of provider key env vars). the loader lives in config/ and is shared by all three resolution paths: gateway container run, gateway binary mode, and a2a agent containers. missing, empty, or unreadable files degrade silently; malformed json or permissions broader than 0600 are logged as warnings by the callers.
Comment thread config/auth.go Resolved
Comment thread internal/gateway/manager_env_test.go Resolved
Comment thread internal/gateway/manager.go Resolved
Comment thread internal/gateway/manager.go Resolved
@edenreich
edenreich merged commit cbb0348 into main Sep 3, 2026
1 check passed
@edenreich
edenreich deleted the fix/issue-1167 branch September 3, 2026 13:26
@edenreich edenreich moved this from Todo to Done in 2026 Sep 3, 2026
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.189.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Sep 4, 2026
edenreich added a commit that referenced this pull request Sep 18, 2026
auth.yaml is the only provider-key file the CLI looks at. The legacy read,
its constant, the two fallback test cases and the auth.json protected_paths
entry are gone, along with the migration prose in the README and docs.

Also sweeps the stale auth.json references left behind by #1169 in
internal/gateway and internal/agent/application, and switches the gateway
env fixtures from JSON bodies to the YAML users actually write.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

[FEATURE] Fallback to ~/.infer/auth.json for provider keys when no project .env

1 participant