feat: support secrets-manager retrieval for Gemini API key (macOS Keychain, 1Password, etc.)
Problem
graphify currently discovers the Gemini backend key exclusively from two environment variables: GEMINI_API_KEY and GOOGLE_API_KEY. Security-conscious teams keep API keys out of shell environments entirely — storing them in macOS Keychain, 1Password, AWS Secrets Manager, HashiCorp Vault, or similar. These users either fall back to the (slower, more expensive) host-agent path on every run, or must break their secrets policy to get the Gemini fast path.
On macOS the idiomatic retrieval is:
```bash security find-generic-password -s "GEMINI_API_KEY" -w ```
The key exists in Keychain but never appears in the environment, so graphify never sees it.
Proposed solution
Add a GRAPHIFY_GEMINI_KEY_CMD environment variable (or --gemini-key-cmd CLI flag) whose value is a shell command. When set and GEMINI_API_KEY/GOOGLE_API_KEY are absent, graphify runs the command and uses stdout as the key:
```bash export GRAPHIFY_GEMINI_KEY_CMD="security find-generic-password -s GEMINI_API_KEY -w"
or: export GRAPHIFY_GEMINI_KEY_CMD="op read op://Personal/Gemini/credential"
```
This pattern is established in other tools (e.g. Git's credential.helper, Docker's credHelpers, aws --profile). It keeps the raw secret out of the environment while allowing graphify to retrieve it at invocation time.
Alternative
A ~/.config/graphify/config.toml (or .graphifyrc) that specifies a gemini_key_command field, evaluated at startup. This avoids even the intermediate env var.
Context
Discovered while running /graphify on two long-running research projects. The host-agent subagent path works correctly — this is purely a usability/cost issue for users following a secrets-manager discipline.
Hi, Can I work on this issue
Following up with the exact patch site, in case it helps whoever picks this up (@AlphaRex-pixel offered above).
Read against the installed graphifyy 0.9.25 wheel.
There is exactly one credential resolver
graphify/llm.py:1079:
def _get_backend_api_key(backend: str) -> str:
"""Return the first configured API key for backend, or an empty string."""
for env_key in _backend_env_keys(backend):
value = os.environ.get(env_key)
if value:
return value
return ""
Every consumer routes through it — cli.py:2886, dedup.py:627, and the two error sites at llm.py:1704 and llm.py:2498. So a key-command hook belongs here and nowhere else; it is a single function body.
It generalizes for free
_backend_env_keys() (llm.py:1067) reads BACKENDS[backend]["env_keys"], so the resolver is already backend-generic. Adding a per-backend key_command entry to the BACKENDS table — or a GRAPHIFY_<BACKEND>_KEY_CMD convention — is the same size of change as the Gemini-only version in the original report, but fixes Anthropic, OpenAI, Kimi, DeepSeek and Azure at the same time. Worth widening the scope of this issue accordingly.
There is currently no stopgap at all
extract_corpus_parallel() accepts api_key= (llm.py:1655), so the library can be handed a key by a caller. The CLI exposes no --api-key or --api-key-file, so from the command line the environment is the only channel that exists. Even users willing to pass a key per-invocation from their secrets manager have no way to do it. A --api-key-file flag is a few lines and would unblock scripted use immediately, independently of the helper-command work.
Why the environment is a materially worse channel for graphify specifically
graphify spawns subprocesses as part of normal operation — git clone for the GitHub-URL flow, document converters, Whisper transcription, and host-agent subagents. Each inherits the parent environment, so an env-var key is handed to every one of them, including third-party binaries the user did not choose. A helper command is read once, in-process, and never propagates.
Notes for the implementation
- Run the helper with
subprocess.run(shlex.split(cmd), shell=False)rather thanshell=True— the value is user config, but a shell adds an injection surface for no benefit. .strip()the stdout;security find-generic-password -wandop readboth emit a trailing newline, and a key with\nappended fails with an opaque 401.- Never log the helper's stdout, including at debug level, and keep it out of the exception text on non-zero exit — report the exit status and stderr only.
- On non-zero exit, fail loudly rather than falling through to the next backend; a silent downgrade to a different provider is worse than an error.