refactor: replace CryptoService with utils AES helpers and simplify encryption

- Remove infrastructure/crypto package (CryptoService, config, PBKDF2-based encryption)
- Replace with lightweight utils.GetAESKey/EncryptAESGCM/DecryptAESGCM helpers
- Remove cryptoSvc dependency from modelService, tenantService, weKnoraCloudService
- Remove crypto state persistence logic from container initialization
- Add TruncatePromptTokens field to WeKnoraCloud embed request
- Update frontend hint text for WeKnoraCloud credential errors

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
begoniezhao
2026-04-13 20:43:59 +08:00
parent 2c0c1b8bab
commit d4edd374df
8 changed files with 41 additions and 515 deletions
-89
View File
@@ -6,7 +6,6 @@ package container
import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
@@ -64,7 +63,6 @@ import (
"github.com/Tencent/WeKnora/internal/im/telegram"
"github.com/Tencent/WeKnora/internal/im/wechat"
"github.com/Tencent/WeKnora/internal/im/wecom"
"github.com/Tencent/WeKnora/internal/infrastructure/crypto"
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
infra_web_search "github.com/Tencent/WeKnora/internal/infrastructure/web_search"
"github.com/Tencent/WeKnora/internal/logger"
@@ -100,7 +98,6 @@ func BuildContainer(container *dig.Container) *dig.Container {
// Core infrastructure configuration
logger.Debugf(ctx, "[Container] Registering core infrastructure...")
must(container.Provide(config.LoadConfig))
must(container.Provide(initCryptoService))
must(container.Provide(initTracer))
must(container.Provide(initDatabase))
must(container.Provide(initFileService))
@@ -311,92 +308,6 @@ func initTracer() (*tracing.Tracer, error) {
return tracing.InitTracer()
}
// cryptoStateFile 是持久化 crypto 状态的文件路径,存储在 data-files volume 中
// 保证重启后能恢复相同的 masterKey 和 salt,避免已加密数据无法解密
const cryptoStateFile = "/data/files/.crypto_state.json"
// cryptoState 持久化存储的 crypto 配置
type cryptoState struct {
MasterKey string `json:"master_key"`
Salt string `json:"salt"` // base64 编码
}
func initCryptoService() (*crypto.CryptoService, error) {
cfg := crypto.LoadConfigFromEnv()
// 如果环境变量同时提供了 MasterKey 和 Salt,直接使用(优先级最高)
if cfg.MasterKey != "" && cfg.Salt != "" {
return crypto.NewCryptoServiceFromConfig(cfg)
}
// 尝试从持久化文件恢复状态
if state, err := loadCryptoState(cryptoStateFile); err == nil {
// 文件读取成功,环境变量中未覆盖的字段从文件补充
if cfg.MasterKey == "" {
cfg.MasterKey = state.MasterKey
}
if cfg.Salt == "" {
cfg.Salt = state.Salt
}
return crypto.NewCryptoServiceFromConfig(cfg)
}
// 文件不存在或读取失败,使用/生成配置后写入文件
if cfg.MasterKey == "" {
cfg.MasterKey = "weknora-default-key"
}
// 调用 NewCryptoServiceFromConfig 会在 cfg.Salt 为空时自动生成随机 salt
svc, err := crypto.NewCryptoServiceFromConfig(cfg)
if err != nil {
return nil, err
}
// 将实际使用的 masterKey 和 salt 持久化,供重启恢复
saltB64 := base64.StdEncoding.EncodeToString(svc.GetSalt())
state := &cryptoState{
MasterKey: cfg.MasterKey,
Salt: saltB64,
}
if saveErr := saveCryptoState(cryptoStateFile, state); saveErr != nil {
// 持久化失败仅记录警告,不阻止服务启动
logger.Warnf(context.Background(), "[CryptoService] 无法持久化 crypto 状态到 %s: %v (重启后已加密数据将无法解密)", cryptoStateFile, saveErr)
} else {
logger.Infof(context.Background(), "[CryptoService] crypto 状态已持久化到 %s", cryptoStateFile)
}
return svc, nil
}
// loadCryptoState 从文件加载 crypto 状态
func loadCryptoState(path string) (*cryptoState, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var state cryptoState
if err := json.Unmarshal(data, &state); err != nil {
return nil, fmt.Errorf("parse crypto state: %w", err)
}
if state.MasterKey == "" || state.Salt == "" {
return nil, fmt.Errorf("invalid crypto state: missing fields")
}
return &state, nil
}
// saveCryptoState 将 crypto 状态写入文件
func saveCryptoState(path string, state *cryptoState) error {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
data, err := json.Marshal(state)
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
// 权限 0o600:仅 owner 可读写
return os.WriteFile(path, data, 0o600)
}
func initRedisClient() (*redis.Client, error) {
redisAddr := os.Getenv("REDIS_ADDR")
if redisAddr == "" {