LLM Key Ring (lkr)
A secure CLI tool for managing LLM API keys via macOS Keychain. No more plaintext keys in .env files.
$ lkr set openai:prod
Enter API key for openai:prod: ****
Stored openai:prod (kind: runtime)
$ lkr get openai:prod
Copied to clipboard (auto-clears in 30s)
sk-p...3xYz (runtime)
$ lkr exec -- python script.py # Keys injected as env vars (safest)
$ lkr gen .env.example -o .env # Generate config from template
Why?
| Problem | lkr Solution |
|---|---|
API keys sitting in .env files |
Encrypted in macOS Keychain |
| Keys leaked via shell history | Interactive prompt input (never CLI args) |
| AI agents extracting keys via pipe | TTY guard blocks ALL non-TTY get access (v0.2.0) |
| Keys lingering in clipboard | Auto-clears after 30 seconds |
| Keys lingering in process memory | zeroize wipes memory on drop |
Install
# Homebrew (recommended)
# From source
Requires macOS (uses native Keychain). Source build requires Rust 1.85+.
Note: After upgrading (
brew upgrade lkrorcargo install --force), runlkr hardento refresh Keychain ACL for the new binary.
Usage
Store a key
|
Key names use provider:label format (e.g., openai:prod, anthropic:main).
Retrieve a key
v0.2.0: In non-interactive environments (pipes, agent subprocesses),
lkr getis blocked by default. Use--json(masked values) or--force-plain(raw, at your risk) to override. Preferlkr execfor automation.
List keys
Run a command with keys as env vars (recommended)
Keys are mapped to conventional env var names (e.g., openai:prod → OPENAI_API_KEY) and injected into the child process. Only runtime keys are injected — admin keys are excluded by design. Keys never appear in stdout, files, or clipboard — this is the safest way to pass secrets to programs. Prefer exec over gen whenever possible.
Generate config from template
Use gen when the target program requires a config file and cannot accept env vars.
In non-interactive environments, --force is required (v0.2.0):
.env.example format — keys are auto-resolved by exact env var name match:
OPENAI_API_KEY=your-key-here # ← resolved from openai:* in Keychain
ANTHROPIC_API_KEY= # ← resolved from anthropic:*
JSON template format — use explicit {{lkr:provider:label}} placeholders:
Generated files are written with 0600 permissions. A warning is shown if the output file is not in .gitignore.
When multiple runtime keys exist for the same provider (e.g., openai:prod and openai:stg), the alphabetically first key is used. A warning lists alternatives. Use {{lkr:provider:label}} placeholders for explicit control.
Migrate keys
v0.3.0: Copies keys from login.keychain to the custom keychain with Legacy ACL applied.
Requires lkr init first. Safe to run multiple times (skips existing keys).
Delete a key
Check API usage costs
Requires an Admin API key registered with --kind admin:
Global flags
Supported Providers
lkr gen auto-resolves keys for these providers:
| Provider | Env Variable | Key Name Example |
|---|---|---|
| OpenAI | OPENAI_API_KEY |
openai:prod |
| Anthropic | ANTHROPIC_API_KEY |
anthropic:main |
GOOGLE_API_KEY |
google:dev |
|
| Mistral | MISTRAL_API_KEY |
mistral:api |
| Cohere | COHERE_API_KEY |
cohere:prod |
| Groq | GROQ_API_KEY |
groq:prod |
| DeepSeek | DEEPSEEK_API_KEY |
deepseek:api |
| xAI | XAI_API_KEY |
xai:prod |
| And more... |
Any provider:label name works with set/get/rm. The provider list above is used for auto-resolution in lkr gen.
Security
Threat Model
See docs/SECURITY.md for the full threat model. Key protections:
| Threat | Mitigation |
|---|---|
| Plaintext key files | Keys stored in macOS Keychain (encrypted at rest) |
| Shell history exposure | lkr set reads from prompt, never from CLI arguments |
| Clipboard residual | 30s auto-clear via SHA-256 hash comparison |
| Terminal shoulder-surfing | Masked by default (sk-p...3xYz) |
| AI agent exfiltration | TTY guard blocks ALL non-TTY get/gen access (v0.2.0) |
| Memory forensics | zeroize::Zeroizing<String> zeroes memory on drop |
| Admin key in templates | lkr gen only resolves runtime keys |
| Accidental git commit | .gitignore coverage check on generated files |
Agent IDE Attack Protection (v0.2.0)
AI coding assistants (Cursor, Copilot, Claude Code, etc.) can be tricked via prompt injection
into running commands that exfiltrate secrets. lkr v0.2.0 comprehensively blocks this:
# Non-TTY: ALL get access blocked by default (exit code 2)
| | |
# Allowed alternatives in non-TTY:
| |
# gen is also blocked in non-TTY:
| |
# exec always works (safest path — keys never in stdout):
Architecture
llm-key-ring/
├── crates/
│ ├── lkr-core/ # Library: KeyStore trait, Keychain, templates, usage API
│ │ ├── src/
│ │ │ ├── keymanager.rs # KeychainStore + keychain_raw FFI (CRUD)
│ │ │ ├── custom_keychain.rs # Keychain lifecycle (create/open/unlock/lock) [v0.3.0]
│ │ │ ├── acl.rs # Legacy ACL builder (SecAccessCreate) [v0.3.0]
│ │ │ └── error.rs # Error types + OSStatus constants
│ │ └── tests/
│ │ └── keychain_integration.rs # Tier 2 contract tests [v0.3.0]
│ └── lkr-cli/ # Binary: clap CLI (init/set/get/list/rm/gen/usage/exec/migrate/harden/lock)
├── docs/
│ ├── SECURITY.md # Threat model + attack surface
│ └── design-v030.md # v0.3.0 design document
├── LICENSE-MIT
└── LICENSE-APACHE
All business logic lives in lkr-core. The CLI is a thin wrapper. v0.3.0 adds three key modules:
custom_keychain: Dedicated keychain lifecycle management via Security.framework FFIacl: Legacy ACL builder usingSecAccessCreate+SecTrustedApplicationCreateFromPathkeychain_raw: Low-level item CRUD viaSecKeychainItemCreateFromContent(with initial ACL)
Platform Support
Currently macOS only (uses native Keychain via security-framework / security-framework-sys direct FFI). The KeyStore trait abstraction is designed for future backend support (Linux libsecret, Windows Credential Manager).
Keychain Storage
| Field | Value |
|---|---|
| Keychain | ~/Library/Keychains/lkr.keychain-db (v0.3.0+, NOT in search list) |
| Service | com.llm-key-ring |
| Account | {provider}:{label} |
| Password | {"value":"sk-...","kind":"runtime"} |
| ACL | SecAccessRef trusting only the lkr binary (cdhash-based, v0.3.0+) |
| Synchronizable | false (v0.2.0+, no iCloud sync) |
| Accessible | WhenUnlocked (v0.2.0+) |
Upgrading from v0.1.x
Breaking changes in v0.2.0
-
lkr getis blocked in non-interactive environments (exit code 2).- Previously only
--plain/--showwere blocked; now baregetis also blocked. - Use
--json(masked values) or--force-plain(raw) to override. - Recommended: switch to
lkr execfor automation.
- Previously only
-
lkr genis blocked in non-interactive environments (exit code 2).- Add
--forceto CI/CD scripts that uselkr gen.
- Add
-
lkr execstderr output changed:- TTY: silent by default (was verbose). Use
--verboseto see injected keys. - Non-TTY: 1-line warning.
- TTY: silent by default (was verbose). Use
Migration steps
# 1. Update lkr
# 2. Migrate existing keys (adds iCloud sync protection + lock protection)
# 3. Update CI/CD scripts (if applicable)
# Before: lkr get openai:prod --plain | ...
# After: lkr exec -- ...
# Or: lkr get openai:prod --force-plain | ...
# Or: lkr gen .env.example --force
Upgrading to v0.3.0
v0.3.0 is a breaking change. Keys are moved from login.keychain to a dedicated Custom Keychain with 3-layer defense against
security find-generic-passwordattacks.
What changes
| Before (v0.2.x) | After (v0.3.0) |
|---|---|
| Keys in login.keychain (auto-unlocked at login) | Keys in lkr.keychain-db (separate password, auto-lock 5min) |
security find-generic-password can read keys |
Blocked — search list isolation + Legacy ACL |
| Binary replacement is undetected | Detected — cdhash-based integrity check |
| No setup step required | lkr init required before first use |
All operations via security-framework crate |
Pure FFI: SecKeychainItemCreateFromContent + SecAccessCreate |
Upgrade steps
# 1. Update lkr
# 2. Create dedicated keychain (new in v0.3.0)
# 3. Move keys from login.keychain → lkr.keychain-db
# 4. After future binary updates (cargo install --force):
Note: lkr migrate copies keys (does not delete from login.keychain). Legacy keys
remain readable via v0.2.x fallback until you manually remove them.
New commands in v0.3.0
| Command | Purpose |
|---|---|
lkr init |
Create lkr.keychain-db, set password, enable lock-on-sleep + 5min auto-lock |
lkr migrate |
Copy keys from login.keychain → custom keychain (with ACL) |
lkr harden |
Re-register binary fingerprint after cargo install --force |
lkr lock |
Explicitly lock lkr.keychain-db |
Exit Codes
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General error (key not found, Keychain error, etc.) |
| 2 | TTY guard violation (non-interactive environment blocked) |
Roadmap
| Version | Theme | Key Changes |
|---|---|---|
| v0.2.2 | Docs & Roadmap | Roadmap update, v0.3.0 upgrade guide preview |
| v0.3.0 | Security: 3-Layer Defense | Custom Keychain (lkr.keychain-db) + Legacy ACL via Pure FFI + cdhash. Breaking change — see below |
| v0.3.1 | Security Hardening | ACL fail-closed, keychain_path() safety, StoredEntry zeroize-on-drop, -25308 auto-diagnosis |
| v0.3.2 (current) | Operational Quality | List N+1 fix, CLI module split, unsafe SAFETY docs, Homebrew tap |
| v0.3.3 | Diagnostics | lkr doctor (Keychain health check) |
| v0.4.0 | MCP Server | IDE integration for secure key access |
Development
# Build
# Test
# Clippy
# Run without installing
License
Dual-licensed under MIT or Apache-2.0, at your option.