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:
wizardchen
2026-04-15 10:55:39 +08:00
committed by lyingbug
parent 3b23dccd5c
commit 3f8201cd49
13 changed files with 150 additions and 91 deletions
+2 -2
View File
@@ -28,8 +28,8 @@ type TenantService interface {
SearchTenants(ctx context.Context, keyword string, tenantID uint64, page, pageSize int) ([]*types.Tenant, int64, error)
// GetTenantByIDForUser gets a tenant by ID with permission check
GetTenantByIDForUser(ctx context.Context, tenantID uint64, userID string) (*types.Tenant, error)
// GetTenantByAPIKey gets a tenant by API key
GetDocreaderCredentials(ctx context.Context) *types.DocreaderCredentials
// GetWeKnoraCloudCredentials returns the decrypted WeKnoraCloud credentials for the current tenant.
GetWeKnoraCloudCredentials(ctx context.Context) *types.WeKnoraCloudCredentials
}
// TenantRepository defines the tenant repository interface
+65 -7
View File
@@ -101,6 +101,8 @@ type Tenant struct {
ConversationConfig *ConversationConfig `yaml:"conversation_config" json:"conversation_config" gorm:"type:jsonb"`
// Parser engine config overrides (MinerU endpoint, API key, etc.). Used when parsing documents; overrides env.
ParserEngineConfig *ParserEngineConfig `yaml:"parser_engine_config" json:"parser_engine_config" gorm:"type:jsonb"`
// Credentials config: third-party provider credentials (e.g. WeKnoraCloud AppID/AppSecret)
Credentials *CredentialsConfig `yaml:"credentials" json:"credentials" gorm:"type:jsonb"`
// Storage engine config: parameters for Local, MinIO, COS. Used for document/file storage and docreader.
StorageEngineConfig *StorageEngineConfig `yaml:"storage_engine_config" json:"storage_engine_config" gorm:"type:jsonb"`
// Chat history config: knowledge base configuration for indexing and searching chat messages via vector search
@@ -230,13 +232,72 @@ func (c *ConversationConfig) Scan(value interface{}) error {
return json.Unmarshal(b, c)
}
// CredentialsConfig holds third-party provider credentials at the tenant level.
// Stored as a single JSONB column; each provider is a nested object so new
// providers can be added without schema changes.
type CredentialsConfig struct {
WeKnoraCloud *WeKnoraCloudCredentials `json:"weknoracloud,omitempty"`
}
// WeKnoraCloudCredentials stores WeKnoraCloud AppID and AppSecret.
// AppSecret is AES-256 encrypted before persisting to database.
type WeKnoraCloudCredentials struct {
AppID string `json:"app_id"`
AppSecret string `json:"app_secret"`
}
// GetWeKnoraCloud returns the WeKnoraCloud credentials, or nil if not configured.
func (c *CredentialsConfig) GetWeKnoraCloud() *WeKnoraCloudCredentials {
if c == nil || c.WeKnoraCloud == nil {
return nil
}
if c.WeKnoraCloud.AppID == "" || c.WeKnoraCloud.AppSecret == "" {
return nil
}
return c.WeKnoraCloud
}
// Value implements the driver.Valuer interface for CredentialsConfig
func (c *CredentialsConfig) Value() (driver.Value, error) {
if c == nil {
return nil, nil
}
cp := *c
if cp.WeKnoraCloud != nil && cp.WeKnoraCloud.AppSecret != "" {
if key := utils.GetAESKey(); key != nil {
if encrypted, err := utils.EncryptAESGCM(cp.WeKnoraCloud.AppSecret, key); err == nil {
cp.WeKnoraCloud = &WeKnoraCloudCredentials{AppID: cp.WeKnoraCloud.AppID, AppSecret: encrypted}
}
}
}
return json.Marshal(cp)
}
// Scan implements the sql.Scanner interface for CredentialsConfig
func (c *CredentialsConfig) Scan(value interface{}) error {
if value == nil {
return nil
}
b, ok := value.([]byte)
if !ok {
return nil
}
if err := json.Unmarshal(b, c); err != nil {
return err
}
if c.WeKnoraCloud != nil && c.WeKnoraCloud.AppSecret != "" {
if key := utils.GetAESKey(); key != nil {
if decrypted, err := utils.DecryptAESGCM(c.WeKnoraCloud.AppSecret, key); err == nil {
c.WeKnoraCloud.AppSecret = decrypted
}
}
}
return nil
}
// ParserEngineConfig holds tenant-level overrides for document parser engines (e.g. MinerU endpoint, API key).
// These values take precedence over environment variables when parsing documents.
type ParserEngineConfig struct {
// docreader 凭证
DocreaderAppID string `json:"docreader_app_id,omitempty"`
DocreaderAPIKey string `json:"docreader_api_key,omitempty"`
MinerUEndpoint string `json:"mineru_endpoint"` // MinerU 自建服务端点
MinerUAPIKey string `json:"mineru_api_key"` // MinerU 云 API Key
@@ -262,9 +323,6 @@ func (c *ParserEngineConfig) ToOverridesMap() map[string]string {
return nil
}
m := make(map[string]string)
if c.DocreaderAppID != "" {
m["docreader_app_id"] = c.DocreaderAppID
}
if c.MinerUEndpoint != "" {
m["mineru_endpoint"] = c.MinerUEndpoint
}
-5
View File
@@ -6,8 +6,3 @@ type WeKnoraCloudStatusResult struct {
NeedsReinit bool `json:"needs_reinit"` // 是否需要重新初始化(凭证损坏)
Reason string `json:"reason,omitempty"` // 需要重新初始化的原因
}
type DocreaderCredentials struct {
AppID string
APIKey string
}