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
+6 -8
View File
@@ -4,7 +4,6 @@ import (
"context"
"errors"
"github.com/Tencent/WeKnora/internal/infrastructure/crypto"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/models/asr"
"github.com/Tencent/WeKnora/internal/models/chat"
@@ -14,6 +13,7 @@ import (
"github.com/Tencent/WeKnora/internal/models/vlm"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
)
// ErrModelNotFound is returned when a model cannot be found in the repository
@@ -24,20 +24,17 @@ type modelService struct {
repo interfaces.ModelRepository
ollamaService *ollama.OllamaService
pooler embedding.EmbedderPooler
cryptoSvc *crypto.CryptoService
}
// NewModelService creates a new model service instance
func NewModelService(repo interfaces.ModelRepository,
ollamaService *ollama.OllamaService,
pooler embedding.EmbedderPooler,
cryptoSvc *crypto.CryptoService,
) interfaces.ModelService {
return &modelService{
repo: repo,
ollamaService: ollamaService,
pooler: pooler,
cryptoSvc: cryptoSvc,
}
}
@@ -46,11 +43,12 @@ func (s *modelService) decryptAppSecret(encrypted string) string {
if encrypted == "" {
return encrypted
}
plain, err := s.cryptoSvc.DecryptString(encrypted)
if err != nil {
return ""
if key := utils.GetAESKey(); key != nil {
if encrypted, err := utils.DecryptAESGCM(encrypted, key); err == nil {
return encrypted
}
}
return plain
return encrypted
}
// CreateModel creates a new model in the repository
+13 -12
View File
@@ -13,7 +13,6 @@ import (
"strings"
"time"
"github.com/Tencent/WeKnora/internal/infrastructure/crypto"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -34,13 +33,12 @@ type ListTenantsParams struct {
// tenantService implements the TenantService interface
type tenantService struct {
repo interfaces.TenantRepository // Repository for tenant data operations
cryptoSvc *crypto.CryptoService
repo interfaces.TenantRepository // Repository for tenant data operations
}
// NewTenantService creates a new tenant service instance
func NewTenantService(repo interfaces.TenantRepository, cryptoSvc *crypto.CryptoService) interfaces.TenantService {
return &tenantService{repo: repo, cryptoSvc: cryptoSvc}
func NewTenantService(repo interfaces.TenantRepository) interfaces.TenantService {
return &tenantService{repo: repo}
}
// CreateTenant creates a new tenant
@@ -353,11 +351,12 @@ func (s *tenantService) GetDocreaderCredentials(ctx context.Context) *types.Docr
if appID == "" || tenant.ParserEngineConfig.DocreaderAPIKey == "" {
return nil
}
apiKey, err := s.cryptoSvc.DecryptString(tenant.ParserEngineConfig.DocreaderAPIKey)
if err != nil || apiKey == "" {
return nil
if key := utils.GetAESKey(); key != nil {
if encrypted, err := utils.DecryptAESGCM(tenant.ParserEngineConfig.DocreaderAPIKey, key); err == nil {
return &types.DocreaderCredentials{AppID: appID, APIKey: encrypted}
}
}
return &types.DocreaderCredentials{AppID: appID, APIKey: apiKey}
return &types.DocreaderCredentials{AppID: appID, APIKey: tenant.ParserEngineConfig.DocreaderAPIKey}
}
}
@@ -372,10 +371,12 @@ func (s *tenantService) GetDocreaderCredentials(ctx context.Context) *types.Docr
if err == nil && tenant != nil && tenant.ParserEngineConfig != nil {
appID := strings.TrimSpace(tenant.ParserEngineConfig.DocreaderAppID)
if appID != "" && tenant.ParserEngineConfig.DocreaderAPIKey != "" {
apiKey, err := s.cryptoSvc.DecryptString(tenant.ParserEngineConfig.DocreaderAPIKey)
if err == nil && apiKey != "" {
return &types.DocreaderCredentials{AppID: appID, APIKey: apiKey}
if key := utils.GetAESKey(); key != nil {
if encrypted, err := utils.DecryptAESGCM(tenant.ParserEngineConfig.DocreaderAPIKey, key); err == nil {
return &types.DocreaderCredentials{AppID: appID, APIKey: encrypted}
}
}
return &types.DocreaderCredentials{AppID: appID, APIKey: tenant.ParserEngineConfig.DocreaderAPIKey}
}
}
+17 -15
View File
@@ -7,29 +7,28 @@ import (
"strings"
"time"
"github.com/Tencent/WeKnora/internal/infrastructure/crypto"
"github.com/Tencent/WeKnora/internal/models/provider"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/internal/utils"
"github.com/google/uuid"
)
const WeKnoraCloudAPI = "https://weknora.woa.com/platform/openapi"
type weKnoraCloudService struct {
repo interfaces.ModelRepository
tenantRepo interfaces.TenantRepository
cryptoSvc *crypto.CryptoService
}
// NewWeKnoraCloudService 构造 WeKnoraCloudService
func NewWeKnoraCloudService(
repo interfaces.ModelRepository,
tenantRepo interfaces.TenantRepository,
cryptoSvc *crypto.CryptoService,
) interfaces.WeKnoraCloudService {
return &weKnoraCloudService{
repo: repo,
tenantRepo: tenantRepo,
cryptoSvc: cryptoSvc,
}
}
@@ -99,9 +98,11 @@ func (s *weKnoraCloudService) prepareInitialize(ctx context.Context, appID, appS
}
// Step 2: Encrypt appSecret
encryptedSecret, err := s.cryptoSvc.EncryptString(appSecret)
if err != nil {
return nil, fmt.Errorf("encrypt AppSecret failed: %w", err)
encryptedSecret := appSecret
if key := utils.GetAESKey(); key != nil {
if encrypted, err := utils.EncryptAESGCM(appSecret, key); err == nil {
encryptedSecret = encrypted
}
}
// Step 3: Get tenantID and take snapshot
@@ -296,18 +297,19 @@ func (s *weKnoraCloudService) CheckStatus(ctx context.Context) (*types.WeKnoraCl
return &types.WeKnoraCloudStatusResult{
HasModels: true,
NeedsReinit: true,
Reason: "WeKnoraCloud 凭证为空,请重新填写 APPID 和 API Key",
Reason: fmt.Sprintf("WeKnoraCloud 凭证为空,请重新填写 APPID 和 API Key, 请前往:%s", WeKnoraCloudAPI),
}, nil
}
// Try to decrypt the API key
_, decErr := s.cryptoSvc.DecryptString(tenant.ParserEngineConfig.DocreaderAPIKey)
if decErr != nil {
return &types.WeKnoraCloudStatusResult{
HasModels: true,
NeedsReinit: true,
Reason: "WeKnoraCloud 凭证解密失败(服务重启后加密密钥已变更),请重新填写 APPID 和 API Key",
}, nil
if key := utils.GetAESKey(); key != nil {
if _, err := utils.DecryptAESGCM(tenant.ParserEngineConfig.DocreaderAPIKey, key); err != nil {
return &types.WeKnoraCloudStatusResult{
HasModels: true,
NeedsReinit: true,
Reason: "WeKnoraCloud 凭证解密失败(服务重启后加密密钥已变更),请重新填写 APPID 和 API Key",
}, nil
}
}
return &types.WeKnoraCloudStatusResult{HasModels: true, NeedsReinit: false}, nil