#!/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 "🎉 密钥轮换完成!"