platform-assistant/scripts/rotate-mcp-key.sh
Sisyphus afac18e843 feat(F002): W26 + W29.x + W30 + W31 平台助手全部改动首次 commit
- W26: NestJS API + Next.js Web + Prisma + SQLite scaffold
- W26: Lead 模型 + Crawler + 询价发布助手 (线索池 + 一键发布)
- W29.x: MCP 集成 (Integration 模型 + connectMcp + publish_rfq 5 工具端到端跑通)
- W30: 3 字段补充 (notes / quotationType / quotationDemand)
- W31 P1: 自动填 enterpriseId / userId (Prisma companyUserId 字段 + get_app_identity tool + lead.service.ts publishArgs)
- W31: detail 详情页 + admin UI 集成管理 (OpenAppListView + AuditLogListView)

关联: ~/Documents/02-平台开发/产品开发/F002-W31-交接文档.md
GIT-AUDIT-REPORT-2026-06-24.md
2026-06-24 11:07:11 +08:00

98 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# 临时辅助脚本:密钥轮换后用一次即删
# 使用方法:
# 1. 把下面的 NEW_APP_ID / NEW_APP_SECRET 填入新值
# 2. bash scripts/rotate-mcp-key.sh
# 3. 用完后删除此文件
set -e
# === 配置 ===
API_BASE="${API_BASE:-http://localhost:4000}"
USERNAME="${USERNAME:-admin}"
PASSWORD="${PASSWORD:-admin123}"
INTEGRATION_ID="b201ec9b-f769-4ff2-92a2-4dfa52bf4e34"
INTEGRATION_NAME="巨变平台"
ENDPOINT="http://100.111.84.16:3002/mcp"
# === 待填入(拿到密钥后替换)===
NEW_APP_ID="${NEW_APP_ID:-}"
NEW_APP_SECRET="${NEW_APP_SECRET:-}"
if [ -z "$NEW_APP_ID" ] || [ -z "$NEW_APP_SECRET" ]; then
echo "❌ 请先设置 NEW_APP_ID 和 NEW_APP_SECRET 环境变量"
echo " 例:"
echo " NEW_APP_ID=app_xxx NEW_APP_SECRET=yyy bash scripts/rotate-mcp-key.sh"
exit 1
fi
echo "🔑 1. 登录拿 JWT..."
TOKEN=$(curl -s -X POST "$API_BASE/api/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" \
| python3 -c "import sys,json; print(json.load(sys.stdin)['access_token'])")
if [ -z "$TOKEN" ]; then
echo "❌ 登录失败"
exit 1
fi
echo "✅ 登录成功"
echo ""
echo "🔄 2. PUT 更新集成密钥..."
RESP=$(curl -s -w "\n%{http_code}" -X PUT "$API_BASE/api/integrations/$INTEGRATION_ID" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"$INTEGRATION_NAME\",
\"type\": \"mcp\",
\"endpoint\": \"$ENDPOINT\",
\"appId\": \"$NEW_APP_ID\",
\"apiKey\": \"$NEW_APP_SECRET\",
\"customerCode\": \"\"
}")
HTTP_CODE=$(echo "$RESP" | tail -1)
BODY=$(echo "$RESP" | sed '$d')
if [ "$HTTP_CODE" != "200" ]; then
echo "❌ 更新失败 (HTTP $HTTP_CODE): $BODY"
exit 1
fi
echo "✅ 集成密钥已更新"
echo ""
echo "🔌 3. 测试连接..."
CONNECT_RESP=$(curl -s -w "\n%{http_code}" -X POST "$API_BASE/api/integrations/$INTEGRATION_ID/connect" \
-H "Authorization: Bearer $TOKEN")
CONNECT_CODE=$(echo "$CONNECT_RESP" | tail -1)
CONNECT_BODY=$(echo "$CONNECT_RESP" | sed '$d')
if [ "$CONNECT_CODE" != "201" ] && [ "$CONNECT_CODE" != "200" ]; then
echo "❌ 连接失败 (HTTP $CONNECT_CODE): $CONNECT_BODY"
exit 1
fi
echo "✅ 连接成功!"
echo "$CONNECT_BODY" | python3 -m json.tool 2>/dev/null || echo "$CONNECT_BODY"
echo ""
echo "📋 4. 查询集成当前状态..."
curl -s -H "Authorization: Bearer $TOKEN" "$API_BASE/api/integrations" \
| python3 -c "
import sys, json
data = json.load(sys.stdin)
for item in data:
if item['id'] == '$INTEGRATION_ID':
print(f' 状态: {item[\"status\"]}')
print(f' 错误: {item.get(\"error\", \"-\") or \"-\"}')
print(f' 最后连接: {item.get(\"lastConnected\", \"-\")}')
print(f' appId: {item.get(\"appId\", \"-\")}')
print(f' apiKey: {item.get(\"apiKey\", \"-\")}')
"
echo ""
echo "🎉 密钥轮换完成!"