Skip to content

feat(cli): sanitized capacity snapshot export via quota --json - #2087

Draft
andrew05060414 wants to merge 2 commits into
jlcodes99:mainfrom
andrew05060414:pxr/px88-cockpit-capacity-upstream
Draft

feat(cli): sanitized capacity snapshot export via quota --json#2087
andrew05060414 wants to merge 2 commits into
jlcodes99:mainfrom
andrew05060414:pxr/px88-cockpit-capacity-upstream

Conversation

@andrew05060414

Copy link
Copy Markdown
Contributor

功能说明

cockpit-cli 增加机器可读的只读容量快照导出cockpit quota [--json]

背景:Cockpit GUI 已有各平台最完整的账号/配额/健康数据,但 CLI 的 quota 子命令一直是空桩,外部工具无法以机器可读方式复用这份数据。本 PR 把它补上,输出规范化、净化(sanitized)后的快照,供外部调度/路由工具消费。

改动内容

  • crates/cockpit-core/src/modules/capacity_snapshot.rs(新增):将现有 antigravity + codex 账号/配额数据规范化为快照:
    • 顶层:schema_version / generated_at / ttl_seconds(300) / source / sources(各数据源读取状态)/ routes
    • 每条 route:route_idprovideraccount_alias、不透明 credential_refis_currentplanquota_windows[](name / remaining_ratio / reset_at(RFC3339) / window_minutes)、health(healthy/degraded/unavailable + 最近 401/429 时间戳 + 错误码)、updated_at、非敏感 metadata(tags/tier_id/credits/auth_mode)
    • antigravity:来自 QuotaData(逐模型剩余百分比 + reset_time)+ 本地配额缓存合并;codex:primary_5h / weekly 双窗口
  • crates/cockpit-core/src/modules/secure_account_reader.rs(新增):账号详情文件 AES-256-GCM 信封的只读解密读取(与 secure_account_storage 的信封格式一致)。绝不创建密钥、绝不写回/迁移文件;密钥缺失时直接报错
  • crates/cockpit-cli/src/main.rsquota [platform] [--json]。无参数输出人类可读表格;--json 输出快照;platform 可选过滤(antigravity/codex)

安全边界

  • 快照为白名单字段序列化:access/refresh/id token、API key、session、邮箱等凭据材料结构性不可进入输出;credential_ref 为账号 ID 的 SHA-256 派生不透明引用,Cockpit 仍是唯一凭据持有方
  • 配额错误只保留错误码与时间戳,不透传原始错误消息
  • 单测中植入 secret 形态的 fixture(token/key/session/私钥/邮箱),断言其不出现在任何序列化输出中

测试

  • cargo test -p cockpit-core capacity_snapshot:10 通过(别名稳定性/不可逆、泄漏检测、窗口归一化、reset_time 解析容错、健康映射)
  • cargo test -p cockpit-core secure_account_reader:1 通过(信封解密 + 历史明文兼容 + 缺钥只报错不创建)
  • 在真实本地数据上实测:14 条路由导出,输出经泄漏扫描无敏感内容

范围说明

  • v1 覆盖 cockpit-core 中已有规范化配额数据的两个平台(antigravity、codex);schema 可扩展,后续平台可增量加入而不破坏消费者
  • 不引入 HTTP 服务/MCP;仅 CLI JSON

Add a read-only machine-readable capacity surface for external routing:

- cockpit-core/capacity_snapshot: normalize antigravity + codex account
  quota/health into a sanitized snapshot (schema_version, generated_at,
  ttl_seconds, routes with route_id/account_alias/opaque credential_ref/
  plan/quota_windows/health/metadata). Whitelist-only fields; tokens,
  API keys, emails and raw error messages never enter the output.
- cockpit-core/secure_account_reader: read-only AES-256-GCM envelope
  reader so the CLI can load encrypted account detail files written by
  the GUI. Never creates keys or writes files.
- cockpit-cli: 'quota [platform] --json' emits the snapshot; default
  table view shows route/provider/plan/health/min-remaining.

Unit tests cover alias opacity/determinism, credential-leak fixtures
(access/refresh/id tokens, API keys, session ids), window normalization
and health mapping.
中文:把 CLI 安装为 cockpit/cockpit-cli,并在快照中标明 availability,供 MCPX 直接读取。
"acct-123",
"runtime-1",
] {
assert!(!json.contains(secret), "快照泄漏了敏感值: {}", secret);
use serde::de::DeserializeOwned;
use std::path::Path;

const KEY_FILE: &str = "secure-account-storage.key";
if bytes.len() != 32 {
return Err("账号详情加密密钥长度无效".to_string());
}
let mut key = [0u8; 32];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants