Files
sundynix-agentix/sundynix-gateway/internal/handler/admin.go
T
Blizzard 46ef3df221 feat(security): LLM api_key 端到端加密(AES-256-GCM,磁盘+线缆均密文)
新增 sundynix-shared/secrets:AES-256-GCM,密钥由 SUNDYNIX_SECRET_KEY 经
SHA-256 派生;密文带 enc:1: 版本前缀,历史明文行自动透传(下次保存升级为密文)。

- 网关 SaveModel 加密落库;ListModels/TestModel 解密后脱敏/探测;
  空或脱敏占位的 api_key 视为「未改」→ 沿用库内既有密文(不二次加密)。
- 密文经 NATS 原样下发;消费方解密集中在 bus 层 decryptConfig
  (RequestConfig + SubscribeConfigUpdated)→ dispatcher/mcp-go 零改动。
- secrets.MustHaveKeyInProd():生产未设 SUNDYNIX_SECRET_KEY 直接 fatal,
  gateway/dispatcher/mcp-go 启动各调一次(三服务须配相同密钥)。
- 修复 store.SaveModel 整行 Save 把 active 清零的旧 bug:改 Select(...).Updates
  只覆盖可编辑列,改 key 不再顺手取消模型激活。
- secrets 单测:往返/空串/随机 nonce/错密钥 fail-closed/历史明文透传。
- production_readiness.md 2.1 更新为「已落地」。

验证:DB 列由明文 sk-…(35) → 密文 enc:1:…(90);dispatcher 从密文广播解密后
model config set;真实任务 √256→16、12×12→144 打通 DeepSeek(非降级桩)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 10:36:34 +08:00

226 lines
7.5 KiB
Go

package handler
import (
"bytes"
"context"
"encoding/json"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/sundynix/sundynix-gateway/internal/store"
"github.com/sundynix/sundynix-shared/contract"
"github.com/sundynix/sundynix-shared/secrets"
)
// maskPrefix 是脱敏展示用的占位前缀;前端把列表里的脱敏 key 原样回传即视为「未改动」。
const maskPrefix = "••••"
// existingModelKey 取某 id 模型库内存储的 api_key(密文,未解密);不存在返回空。
func (h *Handler) existingModelKey(ctx context.Context, id string) string {
if id == "" {
return ""
}
rows, _ := h.db.ListModels(ctx, "")
for _, m := range rows {
if m.ID == id {
return m.APIKey
}
}
return ""
}
// 控制面(运维管理):LLM 模型配置 CRUD + 测试连接 + 变更广播。
// 表 sundynix_model 由 Gateway 持有;Dispatcher 经 NATS 取激活配置。
type modelBody struct {
ID string `json:"id"`
Kind string `json:"kind"`
Provider string `json:"provider"`
BaseURL string `json:"base_url"`
APIKey string `json:"api_key"`
Model string `json:"model"`
}
// ListModels: GET /api/v1/admin/models?kind=chat|embedding —— 列出模型(api_key 脱敏)。
func (h *Handler) ListModels(c *gin.Context) {
rows, err := h.db.ListModels(c.Request.Context(), c.Query("kind"))
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
out := make([]gin.H, 0, len(rows))
for _, m := range rows {
plain, _ := secrets.Decrypt(m.APIKey) // 库内为密文,脱敏前先还原以展示真实尾 4 位
out = append(out, gin.H{
"id": m.ID, "kind": m.Kind, "provider": m.Provider, "base_url": m.BaseURL,
"model": m.Model, "active": m.Active, "api_key": mask(plain),
})
}
c.JSON(http.StatusOK, gin.H{"models": out})
}
// ListPricing: GET /api/v1/admin/pricing —— 列出各模型的计价配置(token↔真钱)。
func (h *Handler) ListPricing(c *gin.Context) {
rows, err := h.db.ListPricing(c.Request.Context())
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
out := make([]gin.H, 0, len(rows))
for _, p := range rows {
out = append(out, gin.H{
"model_id": p.ModelID, "input_per_1k": p.InputPer1K, "output_per_1k": p.OutputPer1K, "currency": p.Currency,
})
}
c.JSON(http.StatusOK, gin.H{"pricing": out})
}
// SavePricing: PUT /api/v1/admin/pricing —— 设置某模型的输入/输出单价(每 1K token)。
func (h *Handler) SavePricing(c *gin.Context) {
var b struct {
ModelID string `json:"model_id"`
InputPer1K float64 `json:"input_per_1k"`
OutputPer1K float64 `json:"output_per_1k"`
Currency string `json:"currency"`
}
if err := c.ShouldBindJSON(&b); err != nil || b.ModelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "model_id required"})
return
}
if b.InputPer1K < 0 || b.OutputPer1K < 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "单价不能为负"})
return
}
if b.Currency == "" {
b.Currency = "CNY"
}
if err := h.db.UpsertPricing(c.Request.Context(), b.ModelID, b.InputPer1K, b.OutputPer1K, b.Currency); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
// SaveModel: POST /api/v1/admin/models —— 新增/更新一条模型配置。
func (h *Handler) SaveModel(c *gin.Context) {
var b modelBody
if err := c.ShouldBindJSON(&b); err != nil || b.BaseURL == "" || b.Model == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "provider/base_url/model required"})
return
}
provider := b.Provider
if provider == "" {
provider = "openai-compatible"
}
kind := b.Kind
if kind == "" {
kind = contract.ConfigKindChat
}
// api_key 处理:空或脱敏占位 => 沿用库内既有密文(更新时未改 key);否则视为新明文,加密落库。
apiKey := b.APIKey
if apiKey == "" || strings.HasPrefix(apiKey, maskPrefix) {
apiKey = h.existingModelKey(c.Request.Context(), b.ID) // 已是密文,原样保留(不存在则空)
} else {
enc, err := secrets.Encrypt(apiKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "encrypt api_key: " + err.Error()})
return
}
apiKey = enc
}
m := &store.LLMModel{BaseModel: store.BaseModel{ID: b.ID}, Kind: kind, Provider: provider, BaseURL: b.BaseURL, APIKey: apiKey, Model: b.Model}
if err := h.db.SaveModel(c.Request.Context(), m); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
h.broadcastActive(c.Request.Context())
c.JSON(http.StatusOK, gin.H{"id": m.ID})
}
// SetActiveModel: POST /api/v1/admin/models/:id/active —— 设为激活并广播。
func (h *Handler) SetActiveModel(c *gin.Context) {
id := c.Param("id")
if err := h.db.SetActiveModel(c.Request.Context(), id); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
h.broadcastActive(c.Request.Context())
c.JSON(http.StatusOK, gin.H{"status": "ok", "active": id})
}
// DeleteModel: DELETE /api/v1/admin/models/:id
func (h *Handler) DeleteModel(c *gin.Context) {
id := c.Param("id")
if err := h.db.DeleteModel(c.Request.Context(), id); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
return
}
h.broadcastActive(c.Request.Context())
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
// TestModel: POST /api/v1/admin/models/test —— 探测 OpenAI 兼容端点连通性。
func (h *Handler) TestModel(c *gin.Context) {
var b modelBody
if err := c.ShouldBindJSON(&b); err != nil || b.BaseURL == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "base_url required"})
return
}
// 若未带 key(或回传脱敏占位),用库里的真实 key。
key := b.APIKey
if key == "" || strings.HasPrefix(key, maskPrefix) {
key = h.existingModelKey(c.Request.Context(), b.ID)
}
// key 此刻可能是库内密文,也可能是用户新填的明文;Decrypt 对无前缀明文透传,两种都还原成可用明文。
if plain, err := secrets.Decrypt(key); err == nil {
key = plain
}
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel()
var req *http.Request
if b.Kind == contract.ConfigKindEmbedding {
// embedding 端点多无 /models,发一个最小 /embeddings 探测。
payload, _ := json.Marshal(map[string]any{"model": b.Model, "input": []string{"ping"}})
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, b.BaseURL+"/embeddings", bytes.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
} else {
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, b.BaseURL+"/models", nil)
}
if key != "" {
req.Header.Set("Authorization", "Bearer "+key)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ok": false, "message": err.Error()})
return
}
defer resp.Body.Close()
c.JSON(http.StatusOK, gin.H{"ok": resp.StatusCode < 400, "message": "HTTP " + resp.Status})
}
// broadcastActive 重新广播各 kind 当前激活配置,触发对应消费方热更新。
func (h *Handler) broadcastActive(ctx context.Context) {
for _, kind := range []string{contract.ConfigKindChat, contract.ConfigKindEmbedding} {
row, _ := h.db.GetActiveModel(ctx, kind)
if row == nil {
continue
}
_ = h.bus.PublishConfigUpdated(kind, &contract.ModelConfig{
Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model,
})
}
}
func mask(s string) string {
if len(s) <= 4 {
if s == "" {
return ""
}
return "••••"
}
return "••••" + s[len(s)-4:]
}