关于 op-cli
这款技能介绍如何使用 1Password CLI(`op`)读取并传递密钥,避免将密钥值输出到对话中,也涵盖条目查找、含斜杠标题及安全验证方法。
主要功能
- 01将密钥直接管道传给目标工具
- 02安全查找条目和字段
- 03处理含斜杠的条目标题
- 04验证密钥而不打印内容
适用场景
- 01向 wrangler 传递密钥
- 02从 1Password 创建 Kubernetes 密钥
- 03检查密钥是否存在或匹配
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ alleneubank/ claude-code --skill op-cli安装后在 Claude Code 中输入/op-cli确认已加载。
详细介绍
1Password CLI(op)— 安全处理
核心规则:绝不打印机密
绝不要使用会将机密值打印到对话中的 op 命令。始终将输出直接传递给使用工具,或使用 wc -c/脱敏方式验证,避免暴露机密。
# WRONG — would print secret to stdout (do not run)
# op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal
# RIGHT — pipe directly to consumer
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal | \
wrangler secret put SECRET_NAME --env ENV
# RIGHT — verify a value exists without exposing it
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal 2>/dev/null | wc -c
标题中包含斜杠的条目
许多 1Password 条目使用路径样式的标题(例如 pool-party/testnet-pool-party-public/credentials)。op:// URI 格式无法处理这类标题,因为它使用 / 作为分隔符。
# BROKEN — too many '/' segments
op read "op://pool-party-testnet/pool-party/testnet-pool-party-public/credentials/PASSWORD"
# ERROR: too many '/': secret references should match op://<vault>/<item>[/<section>]/<field>
# WORKS — use item ID instead (avoid printing values)
op item get ITEM_ID --vault VAULT --fields label=FIELD --reveal 2>/dev/null | wc -c
发现工作流
不知道条目 ID 时:
# 1. List items in a vault to find the title and ID
op item list --vault VAULT_NAME
# 2. Use the ID (first column) for all subsequent reads
op item get ITEM_ID --vault VAULT_NAME --fields label=FIELD_NAME --reveal 2>/dev/null | wc -c
从同一条目读取多个字段
# Verify which fields exist (safe — shows labels not values)
op item get ITEM_ID --vault VAULT_NAME --format json 2>/dev/null | \
python3 -c "import json,sys; [print(f['label']) for s in json.load(sys.stdin).get('fields',[]) for f in [s] if f.get('label')]"
# Pipe each field to its destination
op item get ITEM_ID --vault VAULT --fields label=USERNAME --reveal | consumer_cmd ...
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal | consumer_cmd ...
常见管道模式
Cloudflare Workers(wrangler)
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal | \
npx wrangler secret put POOL_PARTY_PUBLIC_PASSWORD --env testnet
环境变量(子 Shell)
SECRET="$(op item get ITEM_ID --vault VAULT --fields label=TOKEN --reveal 2>/dev/null)"
# Use $SECRET in subsequent commands within the same shell — it won't appear in output
kubectl
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal | \
kubectl create secret generic my-secret --from-file=password=/dev/stdin
验证而不暴露机密
# Check a value is non-empty (char count)
op item get ITEM_ID --vault VAULT --fields label=PASSWORD --reveal 2>/dev/null | wc -c
# Compare two sources match (exit code only)
if cmp -s <(op item get ID1 --vault V --fields label=F --reveal 2>/dev/null) \
<(op item get ID2 --vault V --fields label=F --reveal 2>/dev/null); then
echo "match"
else
echo "differ"
fi
故障排除
| 错误 | 原因 | 修复方法 |
|---|---|---|
too many '/' | 条目标题包含斜杠,op:// 无法解析 | 使用条目 ID 调用 op item get |
could not find item | 保险库错误或标题不匹配 | 运行 op item list --vault VAULT 进行查找 |
| 输出为空 | 缺少 --reveal 标志 | 添加 --reveal 并将输出传递给使用工具(或使用 ` |
not signed in | 会话已过期 | 运行 eval "$(op signin)"(避免打印会话令牌) |
评论
支持 Markdown