Files
xop-botandClaude Code 7660b92d70 v0.5: 描述元数据 + list 免引号搜索 + 元数据操作免闸门
影响: key 多了人/AI 可读的用途说明(存加密 vault 内,不落明文文件);
`xop list` 的搜索不用再打引号; `xop list` / `xop desc` 只碰元数据、
永不吐 value,因此不再弹指纹、不再问口令 —— agent 跑 xop 不会被打断。
118 unit + 40 integration 全绿。

改动:
- 新增 `xop desc KEY [TEXT...]`: 描述存 vault 的 `__meta__` TOML 子表;
  文字用空格 join(不用引号), 真打进 vault 的一层引号会被剥掉;
  不写文字 = 清除描述(不用敲空引号 "")
- `xop list [TERM...]`: 搜索默认改成大小写不敏感的子串匹配, 命中 key 名
  或描述, 不用引号、不用通配符; 含 `*` `?` `[` 的词仍切 glob 语义
  (老写法 `xop list 'git_*'` 行为不变); 多词取并集
- 新增 `Vault::open_metadata` / `open_metadata_at`: 只开元数据路径,
  0600 keyring 静默解锁, 无 keyring 才回落手输 passphrase;
  `get`/`set`/`rm`/`run` 仍走 `Vault::open` 全套闸门
- 新增 `auth::Intent` + `auth::needs_gate(intent)`: 把"哪些操作要闸门"
  写成纯函数; `Vault::open` 与 `open_metadata` 共享 `decrypt_with` 核心
- 新增 `src/glob.rs`: 轻量匹配器, `*` 任意长 / `?` 单字符, 零新依赖;
  搜索用的 `has_meta` / `matches_ignore_case` / `contains_ignore_case` /
  `contains_glob_ignore_case`
- 新增 `src/datadir.rs`: 数据目录 `~/.xop/` → `~/.x/xop/`, 迁移是
  **复制不移动**(旧目录留作备份), 新 vault 已存在则跳过
- `keyring::load_at` / `save_at` 改 pub(路径显式化, 单测不碰全局 HOME)
- `audit.rs` 新增 `vault_desc(name)`: 只记 key 名, 描述文字不入日志(I2)
- 集成测试新增 pty 回归 `list_under_a_tty_never_prompts_for_biometrics`:
  `script -q /dev/null` 起真 pty + 30s 超时, 闸门若回来则失败而非挂死
- 版本 0.4.0 → 0.5.2; AGENTS.md / HANDOFF.md / docs/* 同步;
  examples 里 v0.5 搬家前的 `~/.xop/` 残留路径一并修正

设计: "搜索要打引号"的根因是 shell 通配符(`*` 在 zsh 下没匹配文件直接
`no matches found`), 不是 xop —— 所以正解不是"少打引号", 而是让日常
搜索根本不需要通配符: 默认子串匹配, `*`/`?`/`[` 只在用户显式写了时才
切回 glob。免闸门只给元数据操作: 闸门的意义是"释放真值前确认有人在场",
而 list/desc 永不吐 value; 值路径(get/set/rm/run)一行没动。

Co-Authored-By: Claude Code <[email protected]>
2026-10-22 17:03:12 +08:00

118 lines
4.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# llm.sh — Execute any LLM client with xop-managed credentials, no Touch ID.
#
# ## The technique (read this once)
#
# xop's auth gate (`src/auth/mod.rs::decide`) returns `AllowKeyring` —
# silent unlock via the 0600 file keyring — when stdin is NOT a TTY.
# That branch is checked FIRST, before SSH detection and biometrics.
# So: redirect stdin away from the terminal, and xop skips Touch ID.
#
# Requirements / caveats:
#
# 1. Keyring must already be saved (one-time setup):
# xop init # writes ~/.x/xop/keyring automatically
# xop key --save # only needed for vaults created
# # before v0.2.1 (pre-keyring)
# Without `~/.x/xop/keyring`, headless still asks for the passphrase.
#
# 2. SSH interactive sessions (env `SSH_TTY` / `SSH_CONNECTION` set +
# pty) → xop forces the xop passphrase regardless. The headless
# bypass only works locally OR via `ssh host cmd` (non-pty).
#
# 3. stdin is consumed by the redirect. Don't pipe the LLM prompt via
# stdin — pass it via the client's argv (`-d`, `--data`, etc.).
#
# 4. Three vault keys travel together: `llm_key` `llm_model` `llm_url`.
# They're injected as env vars in the child process. Default mapping
# is generic (`LLM_API_KEY` / `LLM_MODEL` / `LLM_BASE_URL`); override
# with `LLM_ENV_KEY` / `LLM_ENV_MODEL` / `LLM_ENV_URL` for specific
# SDKs (e.g. `LLM_ENV_KEY=ANTHROPIC_API_KEY`).
#
# 5. `--secret ENV_VAR=VAULT_KEY` order matters. ENV_VAR is what the
# child sees; VAULT_KEY is what `xop set` named it. We default to
# `LLM_API_KEY=llm_key` (vault convention from AGENTS.md).
#
# ## One-time setup (Touch ID WILL appear here, that's fine)
#
# xop init
# echo "sk-..." | xop set llm_key
# echo "gpt-4o" | xop set llm_model
# echo "https://api.openai.com/v1"| xop set llm_url
#
# ## Usage
#
# # Generic env mapping (LLM_API_KEY / LLM_BASE_URL / LLM_MODEL):
# ./examples/llm.sh -- curl -s "$LLM_BASE_URL/models" \
# -H "Authorization: Bearer $LLM_API_KEY"
#
# # Anthropic SDK env mapping:
# LLM_ENV_KEY=ANTHROPIC_API_KEY LLM_ENV_URL=ANTHROPIC_BASE_URL \
# ./examples/llm.sh -- python my_agent.py
#
# # Tighten network policy to just the LLM endpoint:
# LLM_ALLOW_NET='api.openai.com:443' ./examples/llm.sh -- python my_agent.py
#
# # Pass xop flags (e.g. --dry-run) BEFORE `--`:
# ./examples/llm.sh --dry-run -- echo "policy OK"
#
# ## Verify the bypass
#
# ./examples/llm.sh --dry-run -- echo "policy OK"
#
# Expected: prints the resolved policy WITHOUT prompting Touch ID.
set -euo pipefail
cd "$(dirname "$0")/.."
# ── Preflight: friendly error if keyring missing ─────────────────────────
if [ ! -f "$HOME/.x/xop/keyring" ]; then
echo "✗ ~/.x/xop/keyring not found — headless bypass will fail." >&2
echo " Fix (one-time, requires Touch ID): xop init OR xop key --save" >&2
exit 1
fi
# ── Locate xop binary (installed takes priority over local build) ────────
XOP_BIN="${XOP_BIN:-$(command -v xop 2>/dev/null || echo "./target/release/xop")}"
# ── Split args at the first `--`: xop flags go before, child args after ─
declare -a XOP_EXTRA=()
declare -a CHILD_ARGS=()
seen_sep=false
for arg in "$@"; do
if [ "$seen_sep" = false ] && [ "$arg" = "--" ]; then
seen_sep=true
continue
fi
if [ "$seen_sep" = true ]; then
CHILD_ARGS+=("$arg")
else
XOP_EXTRA+=("$arg")
fi
done
# ── 1. Headless bypass ────────────────────────────────────────────────────
# Only redirect if our stdin is currently a TTY. If the user is already
# piping data into us, the pipe end is already non-TTY — xop would see
# AllowKeyring anyway — so we leave it alone.
[ -t 0 ] && exec </dev/null
# ── 2. Env-var mapping (overridable for specific SDKs) ────────────────────
KEY_ENV="${LLM_ENV_KEY:-LLM_API_KEY}"
URL_ENV="${LLM_ENV_URL:-LLM_BASE_URL}"
MODEL_ENV="${LLM_ENV_MODEL:-LLM_MODEL}"
# ── 3. Network policy ─────────────────────────────────────────────────────
# Default: allow all (LLM dev convenience). Narrow with
# LLM_ALLOW_NET='host:port'
# to enforce a tighter egress window.
ALLOW_NET="${LLM_ALLOW_NET:-*}"
# ── 4. Hand off to xop run with all three secrets injected ────────────────
# `${ARR[@]+"${ARR[@]}"}` is the set -u-safe way to expand possibly-empty arrays.
exec "$XOP_BIN" run ${XOP_EXTRA[@]+"${XOP_EXTRA[@]}"} \
--secret "${KEY_ENV}=llm_key" \
--secret "${MODEL_ENV}=llm_model" \
--secret "${URL_ENV}=llm_url" \
--allow-net="${ALLOW_NET}" \
-- ${CHILD_ARGS[@]+"${CHILD_ARGS[@]}"}