-
Notifications
You must be signed in to change notification settings - Fork 48.1k
feat(secrets): add env:VAR SecretRef support and secrets audit CLI #59516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| git | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12812,11 +12812,28 @@ def main(): | |
|
|
||
| _secrets_cli.register_cli(secrets_bw) | ||
|
|
||
| # ``hermes secrets audit`` — scan for plaintext keys | ||
| audit_parser = secrets_subparsers.add_parser( | ||
| "audit", | ||
| help="Scan for plaintext API keys in skills, memory, and config", | ||
| ) | ||
| audit_parser.add_argument( | ||
| "--check", action="store_true", | ||
| help="Exit non-zero if plaintext keys are found (CI/CD mode)", | ||
| ) | ||
| audit_parser.add_argument( | ||
| "--fix", action="store_true", | ||
| help="Suggest SecretRef replacements for detected plaintext keys", | ||
| ) | ||
| audit_parser.set_defaults(func=lambda a: _secrets_cli.cmd_audit(a)) | ||
|
|
||
| def _dispatch_secrets(args): # noqa: ANN001 | ||
| sub = getattr(args, "secrets_command", None) | ||
| bw_sub = getattr(args, "secrets_bw_command", None) | ||
| if sub in ("bitwarden", "bw") and bw_sub is not None: | ||
| return args.func(args) | ||
| if sub == "audit": | ||
| return _secrets_cli.cmd_audit(args) | ||
| secrets_parser.print_help() | ||
| return 0 | ||
|
|
||
|
|
@@ -14138,10 +14155,12 @@ def cmd_sessions(args): | |
| return | ||
|
|
||
| # Execute the command | ||
| rc = 0 | ||
| if hasattr(args, "func"): | ||
| args.func(args) | ||
| rc = args.func(args) or 0 | ||
| else: | ||
| parser.print_help() | ||
| sys.exit(rc) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not change generic |
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import argparse | ||
| import json | ||
| import os | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
@@ -598,3 +599,96 @@ def _resolve_server_url( | |
| ) | ||
| return custom | ||
| console.print(f" [red]Out of range — pick 1-{custom_idx}.[/red]") | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern matches the stock placeholders |
||
| _AUDIT_KEY_PATTERNS: list[tuple[str, str, str]] = [ | ||
| # (regex pattern, source_label, fix_suggestion_hint) | ||
| (r"(?i)\b(sk-[a-z0-9]{20,})\b", "OpenAI-style sk-", "env:OPENAI_API_KEY"), | ||
| (r"(?i)\b(xai-[a-zA-Z0-9]{20,})\b", "xAI key", "env:XAI_API_KEY"), | ||
| (r"(?i)\b(ds-[a-z0-9]{20,})\b", "DeepSeek key", "env:DEEPSEEK_API_KEY"), | ||
| (r"(?i)\b(nvapi-[a-z0-9-]{20,})\b", "NVIDIA key", "env:NVIDIA_API_KEY"), | ||
| (r"(?i)\b(Bearer\s+[a-zA-Z0-9_-]{20,})\b", "Bearer token in header value", "env:VAR_NAME"), | ||
| ] | ||
|
|
||
|
|
||
| def _scan_file_for_keys( | ||
| file_path: Path, | ||
| console: Console, | ||
| ) -> list[dict]: | ||
| """Scan a single skill/memory file for plaintext API key patterns. | ||
|
|
||
| Returns a list of dicts: {path, line, match, source, fix_hint}. | ||
| """ | ||
| findings: list[dict] = [] | ||
| try: | ||
| text = file_path.read_text(encoding="utf-8", errors="replace") | ||
| except (OSError, UnicodeDecodeError): | ||
| return findings | ||
|
|
||
| for lineno, line in enumerate(text.splitlines(), 1): | ||
| for pattern, source, fix_hint in _AUDIT_KEY_PATTERNS: | ||
| for m in re.finditer(pattern, line): | ||
| # Skip lines that already use SecretRef format | ||
| if "${env:" in line or "${file:" in line: | ||
| continue | ||
| findings.append({ | ||
| "path": str(file_path), | ||
| "line": lineno, | ||
| "match": m.group(0)[:40], | ||
| "source": source, | ||
| "fix_hint": fix_hint, | ||
| }) | ||
| break # one finding per line is enough | ||
| return findings | ||
|
|
||
|
|
||
| def cmd_audit(args: argparse.Namespace) -> int: | ||
| """``hermes secrets audit`` — scan for plaintext API keys.""" | ||
| console = Console() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
|
||
| # Load .env first so SecretRef references resolve cleanly during scan | ||
| from hermes_cli.env_loader import load_hermes_dotenv | ||
|
|
||
| load_hermes_dotenv() | ||
|
|
||
| hermes_home = Path(os.environ.get("HERMES_HOME", os.path.expanduser("~/.hermes"))) | ||
| scan_dirs = [hermes_home / "skills", hermes_home / "memories"] | ||
| config_path = hermes_home / "config.yaml" | ||
|
|
||
| findings: list[dict] = [] | ||
|
|
||
| # Scan skills and memories | ||
| for scan_dir in scan_dirs: | ||
| if not scan_dir.is_dir(): | ||
| continue | ||
| for f in sorted(scan_dir.rglob("*.md")): | ||
| findings.extend(_scan_file_for_keys(f, console)) | ||
|
|
||
| # Scan config.yaml | ||
| if config_path.is_file(): | ||
| findings.extend(_scan_file_for_keys(config_path, console)) | ||
|
|
||
| if not findings: | ||
| console.print("[green]✓ No plaintext API keys found in skills, memory, or config.[/green]") | ||
| return 0 | ||
|
|
||
| console.print(f"[yellow]Found {len(findings)} potential plaintext key(s):[/yellow]\\n") | ||
| for f in findings: | ||
| console.print( | ||
| f" {f['path']}:{f['line']} " | ||
| f"[red]{f['source']}[/red] " | ||
| f"…{f['match']}…" | ||
| ) | ||
| if args.fix: | ||
| console.print( | ||
| f" → hint: replace with [cyan]${{{f['fix_hint']}}}[/cyan]" | ||
| ) | ||
|
|
||
| console.print( | ||
| f"\\n Hint: move secrets to ~/.hermes/.env and reference them as " | ||
| f"[cyan]${{env:VAR_NAME}}[/cyan] in config.yaml" | ||
| ) | ||
|
|
||
| if args.check: | ||
| return 1 # exit non-zero for CI/CD | ||
| return 0 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This unrelated install-marker file is not part of the described feature. Please remove it from the PR.