refactor(weknoracloud): update credential handling and configuration
- Replaced references to Docreader credentials with WeKnoraCloud credentials across multiple services and handlers. - Introduced a new CredentialsConfig structure to manage third-party provider credentials, specifically for WeKnoraCloud. - Updated database schema to include a credentials column for storing WeKnoraCloud AppID and AppSecret. - Enhanced methods for retrieving and utilizing WeKnoraCloud credentials, ensuring proper encryption and decryption during storage and retrieval. This update improves the management of WeKnoraCloud credentials, streamlining the integration and enhancing security measures.
This commit is contained in:
@@ -8057,12 +8057,12 @@ func (s *knowledgeService) resolveDocReader(ctx context.Context, engine, fileTyp
|
||||
case docparser.SimpleEngineName:
|
||||
return &docparser.SimpleFormatReader{}
|
||||
case docparser.WeKnoraCloudEngineName:
|
||||
creds := s.tenantService.GetDocreaderCredentials(ctx)
|
||||
creds := s.tenantService.GetWeKnoraCloudCredentials(ctx)
|
||||
if creds == nil {
|
||||
logger.Warnf(ctx, "[resolveDocReader] WeKnoraCloud: no tenant docreader credentials (fileType=%s)", fileType)
|
||||
logger.Warnf(ctx, "[resolveDocReader] WeKnoraCloud: no tenant credentials (fileType=%s)", fileType)
|
||||
return nil
|
||||
}
|
||||
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(creds.AppID, creds.APIKey)
|
||||
reader, err := docparser.NewWeKnoraCloudSignedDocumentReader(creds.AppID, creds.AppSecret)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "[resolveDocReader] WeKnoraCloud reader init failed: %v", err)
|
||||
return nil
|
||||
|
||||
@@ -71,7 +71,7 @@ func (s *modelService) resolveWeKnoraCloudCredentials(ctx context.Context, param
|
||||
if s.tenantService == nil {
|
||||
return
|
||||
}
|
||||
creds := s.tenantService.GetDocreaderCredentials(ctx)
|
||||
creds := s.tenantService.GetWeKnoraCloudCredentials(ctx)
|
||||
if creds == nil {
|
||||
return
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func (s *modelService) resolveWeKnoraCloudCredentials(ctx context.Context, param
|
||||
appID = creds.AppID
|
||||
}
|
||||
if appSecret == "" {
|
||||
appSecret = creds.APIKey
|
||||
appSecret = creds.AppSecret
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -343,42 +343,24 @@ func (s *tenantService) GetTenantByIDForUser(ctx context.Context, tenantID uint6
|
||||
return tenant, nil
|
||||
}
|
||||
|
||||
func (s *tenantService) GetDocreaderCredentials(ctx context.Context) *types.DocreaderCredentials {
|
||||
// Try to get tenant info directly first
|
||||
func (s *tenantService) GetWeKnoraCloudCredentials(ctx context.Context) *types.WeKnoraCloudCredentials {
|
||||
// Try to get tenant info from context first (already loaded by middleware).
|
||||
// CredentialsConfig.Scan handles decryption, so credentials are ready to use.
|
||||
if tenant, ok := types.TenantInfoFromContext(ctx); ok {
|
||||
if tenant.ParserEngineConfig != nil {
|
||||
appID := strings.TrimSpace(tenant.ParserEngineConfig.DocreaderAppID)
|
||||
if appID == "" || tenant.ParserEngineConfig.DocreaderAPIKey == "" {
|
||||
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: tenant.ParserEngineConfig.DocreaderAPIKey}
|
||||
if creds := tenant.Credentials.GetWeKnoraCloud(); creds != nil {
|
||||
return creds
|
||||
}
|
||||
}
|
||||
|
||||
// If no tenant info in context, try to get tenant ID and load tenant
|
||||
// Fallback: load tenant from repo by tenantID
|
||||
tenantID, ok := types.TenantIDFromContext(ctx)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load tenant from repo if we only have tenantID
|
||||
tenant, err := s.repo.GetTenantByID(ctx, tenantID)
|
||||
if err == nil && tenant != nil && tenant.ParserEngineConfig != nil {
|
||||
appID := strings.TrimSpace(tenant.ParserEngineConfig.DocreaderAppID)
|
||||
if appID != "" && tenant.ParserEngineConfig.DocreaderAPIKey != "" {
|
||||
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}
|
||||
}
|
||||
if err != nil || tenant == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
return tenant.Credentials.GetWeKnoraCloud()
|
||||
}
|
||||
|
||||
@@ -46,15 +46,8 @@ func (s *weKnoraCloudService) SaveCredentials(ctx context.Context, appID, appSec
|
||||
return fmt.Errorf("credential verification failed: %w", err)
|
||||
}
|
||||
|
||||
encryptedSecret := appSecret
|
||||
if key := utils.GetAESKey(); key != nil {
|
||||
if encrypted, err := utils.EncryptAESGCM(appSecret, key); err == nil {
|
||||
encryptedSecret = encrypted
|
||||
}
|
||||
}
|
||||
|
||||
tenantID := types.MustTenantIDFromContext(ctx)
|
||||
return s.updateTenantCredentials(ctx, tenantID, appID, encryptedSecret)
|
||||
return s.updateTenantCredentials(ctx, tenantID, appID, appSecret)
|
||||
}
|
||||
|
||||
// verifyCredentials 向 WeKnoraCloud /api/v1/health 发送带签名头的 GET。
|
||||
@@ -105,30 +98,26 @@ func (s *weKnoraCloudService) CheckStatus(ctx context.Context) (*types.WeKnoraCl
|
||||
return &types.WeKnoraCloudStatusResult{HasModels: false, NeedsReinit: false}, nil
|
||||
}
|
||||
|
||||
// Check if tenant has WeKnoraCloud credentials in parser config
|
||||
if tenant.ParserEngineConfig == nil || tenant.ParserEngineConfig.DocreaderAppID == "" || tenant.ParserEngineConfig.DocreaderAPIKey == "" {
|
||||
return &types.WeKnoraCloudStatusResult{
|
||||
HasModels: false,
|
||||
NeedsReinit: false,
|
||||
}, nil
|
||||
creds := tenant.Credentials.GetWeKnoraCloud()
|
||||
if creds == nil {
|
||||
return &types.WeKnoraCloudStatusResult{HasModels: false, NeedsReinit: false}, nil
|
||||
}
|
||||
|
||||
// Try to decrypt the API key
|
||||
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 和 APPSECRET",
|
||||
}, nil
|
||||
}
|
||||
// CredentialsConfig.Scan already attempts decryption.
|
||||
// If the AES key has rotated, Scan silently keeps the enc:v1:... blob.
|
||||
if strings.HasPrefix(creds.AppSecret, utils.EncPrefix) {
|
||||
return &types.WeKnoraCloudStatusResult{
|
||||
HasModels: true,
|
||||
NeedsReinit: true,
|
||||
Reason: "WeKnoraCloud 凭证解密失败(服务重启后加密密钥已变更),请重新填写 APPID 和 APPSECRET",
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &types.WeKnoraCloudStatusResult{HasModels: true, NeedsReinit: false}, nil
|
||||
}
|
||||
|
||||
// updateTenantCredentials 更新租户的 WeKnoraCloud 凭证和 DocReader 地址
|
||||
func (s *weKnoraCloudService) updateTenantCredentials(ctx context.Context, tenantID uint64, appID, encryptedSecret string) error {
|
||||
// updateTenantCredentials 更新租户的 WeKnoraCloud 凭证
|
||||
func (s *weKnoraCloudService) updateTenantCredentials(ctx context.Context, tenantID uint64, appID, appSecret string) error {
|
||||
if s.tenantRepo == nil {
|
||||
return fmt.Errorf("tenant repository is required")
|
||||
}
|
||||
@@ -137,10 +126,12 @@ func (s *weKnoraCloudService) updateTenantCredentials(ctx context.Context, tenan
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tenant.ParserEngineConfig == nil {
|
||||
tenant.ParserEngineConfig = &types.ParserEngineConfig{}
|
||||
if tenant.Credentials == nil {
|
||||
tenant.Credentials = &types.CredentialsConfig{}
|
||||
}
|
||||
tenant.Credentials.WeKnoraCloud = &types.WeKnoraCloudCredentials{
|
||||
AppID: appID,
|
||||
AppSecret: appSecret,
|
||||
}
|
||||
tenant.ParserEngineConfig.DocreaderAppID = appID
|
||||
tenant.ParserEngineConfig.DocreaderAPIKey = encryptedSecret
|
||||
return s.tenantRepo.UpdateTenant(ctx, tenant)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user