2025-09-17 08:02:08 +00:00
package handler
import (
2025-12-30 03:37:30 +00:00
"context"
2026-02-28 05:22:07 +00:00
"fmt"
"net"
2025-11-16 15:46:33 +00:00
"os"
2026-02-28 05:22:07 +00:00
"regexp"
2025-11-16 15:46:33 +00:00
"strings"
2026-04-13 09:23:16 +00:00
"github.com/Tencent/WeKnora/internal/application/service"
2026-02-28 05:22:07 +00:00
"github.com/Tencent/WeKnora/internal/application/service/file"
2025-11-16 15:46:33 +00:00
"github.com/Tencent/WeKnora/internal/config"
2026-03-02 03:08:45 +00:00
"github.com/Tencent/WeKnora/internal/database"
2026-02-28 05:22:07 +00:00
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
2025-09-17 08:02:08 +00:00
"github.com/Tencent/WeKnora/internal/logger"
2026-01-12 09:34:01 +00:00
"github.com/Tencent/WeKnora/internal/types"
2026-02-28 05:22:07 +00:00
"github.com/Tencent/WeKnora/internal/types/interfaces"
2026-03-17 07:33:19 +00:00
secutils "github.com/Tencent/WeKnora/internal/utils"
2025-09-17 08:02:08 +00:00
"github.com/gin-gonic/gin"
2025-12-30 03:37:30 +00:00
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
2026-04-11 14:06:39 +00:00
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
2025-09-17 08:02:08 +00:00
)
// SystemHandler handles system-related requests
2025-11-16 15:46:33 +00:00
type SystemHandler struct {
2026-02-28 05:22:07 +00:00
cfg * config . Config
neo4jDriver neo4j . Driver
documentReader interfaces . DocumentReader
2026-04-13 09:23:16 +00:00
tenantSvc interfaces . TenantService
2025-11-16 15:46:33 +00:00
}
2025-09-17 08:02:08 +00:00
// NewSystemHandler creates a new system handler
2026-04-13 09:23:16 +00:00
func NewSystemHandler ( cfg * config . Config ,
neo4jDriver neo4j . Driver ,
documentReader interfaces . DocumentReader ,
tenantSvc interfaces . TenantService ,
) * SystemHandler {
2025-11-16 15:46:33 +00:00
return & SystemHandler {
2026-02-28 05:22:07 +00:00
cfg : cfg ,
neo4jDriver : neo4jDriver ,
documentReader : documentReader ,
2026-04-13 09:23:16 +00:00
tenantSvc : tenantSvc ,
2025-11-16 15:46:33 +00:00
}
2025-09-17 08:02:08 +00:00
}
// GetSystemInfoResponse defines the response structure for system info
type GetSystemInfoResponse struct {
2025-11-16 15:46:33 +00:00
Version string ` json:"version" `
2026-02-28 05:22:07 +00:00
Edition string ` json:"edition" `
2025-11-16 15:46:33 +00:00
CommitID string ` json:"commit_id,omitempty" `
BuildTime string ` json:"build_time,omitempty" `
GoVersion string ` json:"go_version,omitempty" `
KeywordIndexEngine string ` json:"keyword_index_engine,omitempty" `
VectorStoreEngine string ` json:"vector_store_engine,omitempty" `
GraphDatabaseEngine string ` json:"graph_database_engine,omitempty" `
2025-11-17 03:38:02 +00:00
MinioEnabled bool ` json:"minio_enabled,omitempty" `
2026-03-02 03:08:45 +00:00
DBVersion string ` json:"db_version,omitempty" `
2025-09-17 08:02:08 +00:00
}
// 编译时注入的版本信息
var (
Version = "unknown"
2026-02-28 05:22:07 +00:00
Edition = "standard"
2025-09-17 08:02:08 +00:00
CommitID = "unknown"
BuildTime = "unknown"
GoVersion = "unknown"
)
2025-12-16 08:40:13 +00:00
// GetSystemInfo godoc
// @Summary 获取系统信息
// @Description 获取系统版本、构建信息和引擎配置
// @Tags 系统
// @Accept json
// @Produce json
// @Success 200 {object} GetSystemInfoResponse "系统信息"
// @Router /system/info [get]
2025-09-17 08:02:08 +00:00
func ( h * SystemHandler ) GetSystemInfo ( c * gin . Context ) {
ctx := logger . CloneContext ( c . Request . Context ( ) )
2025-11-16 15:46:33 +00:00
// Get keyword index engine from RETRIEVE_DRIVER
keywordIndexEngine := h . getKeywordIndexEngine ( )
// Get vector store engine from config or RETRIEVE_DRIVER
vectorStoreEngine := h . getVectorStoreEngine ( )
// Get graph database engine from NEO4J_ENABLE
graphDatabaseEngine := h . getGraphDatabaseEngine ( )
2025-11-17 03:38:02 +00:00
// Get MinIO enabled status
2026-02-28 05:22:07 +00:00
minioEnabled := h . isMinioConfigured ( c )
2025-11-17 03:38:02 +00:00
2026-03-02 03:08:45 +00:00
var dbVersion string
if ver , dirty , ok := database . CachedMigrationVersion ( ) ; ok {
dbVersion = fmt . Sprintf ( "%d" , ver )
if dirty {
dbVersion += " (dirty)"
}
}
2025-09-17 08:02:08 +00:00
response := GetSystemInfoResponse {
2025-11-16 15:46:33 +00:00
Version : Version ,
2026-02-28 05:22:07 +00:00
Edition : Edition ,
2025-11-16 15:46:33 +00:00
CommitID : CommitID ,
BuildTime : BuildTime ,
GoVersion : GoVersion ,
KeywordIndexEngine : keywordIndexEngine ,
VectorStoreEngine : vectorStoreEngine ,
GraphDatabaseEngine : graphDatabaseEngine ,
2025-11-17 03:38:02 +00:00
MinioEnabled : minioEnabled ,
2026-03-02 03:08:45 +00:00
DBVersion : dbVersion ,
2025-09-17 08:02:08 +00:00
}
logger . Info ( ctx , "System info retrieved successfully" )
c . JSON ( 200 , gin . H {
"code" : 0 ,
"msg" : "success" ,
"data" : response ,
} )
}
2025-11-16 15:46:33 +00:00
2026-02-28 05:22:07 +00:00
func ( h * SystemHandler ) getDocReaderConnInfo ( ) ( addr , transport string ) {
addr = strings . TrimSpace ( os . Getenv ( "DOCREADER_ADDR" ) )
transport = strings . TrimSpace ( os . Getenv ( "DOCREADER_TRANSPORT" ) )
if transport == "" {
transport = "grpc"
}
transport = strings . ToLower ( transport )
return addr , transport
}
// ListParserEngines returns available document parser engines.
// Merges Go-native static engines with engines discovered from the remote
// docreader service, so newly added Python engines are auto-discovered.
// @Summary 列出可用的文档解析引擎
// @Tags 系统
// @Produce json
// @Success 200 {object} map[string]interface{} "解析引擎列表"
// @Router /system/parser-engines [get]
func ( h * SystemHandler ) ListParserEngines ( c * gin . Context ) {
var overrides map [ string ] string
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
2026-04-15 02:55:39 +00:00
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil {
if tenant . ParserEngineConfig != nil {
overrides = tenant . ParserEngineConfig . ToOverridesMap ( )
}
if creds := tenant . Credentials . GetWeKnoraCloud ( ) ; creds != nil {
if overrides == nil {
overrides = make ( map [ string ] string )
}
overrides [ "weknoracloud_app_id" ] = creds . AppID
}
2026-02-28 05:22:07 +00:00
}
}
2026-04-13 09:23:16 +00:00
reader , docreaderAddr , docreaderTransport := h . resolveDocReader ( c . Request . Context ( ) , overrides )
connected := reader != nil && reader . IsConnected ( )
remoteEngines := h . fetchRemoteEngines ( c . Request . Context ( ) , reader , overrides )
2026-02-28 05:22:07 +00:00
engines := docparser . ListAllEngines ( connected , overrides , remoteEngines )
c . JSON ( 200 , gin . H { "code" : 0 , "msg" : "success" , "data" : engines , "docreader_addr" : docreaderAddr , "docreader_transport" : docreaderTransport , "connected" : connected } )
}
// ReconnectDocReader reconnects the document converter to a new (or same) DocReader address.
// @Summary 重连文档解析服务
// @Tags 系统
// @Accept json
// @Produce json
// @Param request body object{addr string} true "DocReader 地址"
// @Success 200
// @Router /system/docreader/reconnect [post]
func ( h * SystemHandler ) ReconnectDocReader ( c * gin . Context ) {
var req struct {
Addr string ` json:"addr" binding:"required" `
}
if err := c . ShouldBindJSON ( & req ) ; err != nil {
c . JSON ( 400 , gin . H { "code" : 1 , "msg" : "请提供 addr 参数" } )
return
}
addr := strings . TrimSpace ( req . Addr )
if addr == "" {
c . JSON ( 400 , gin . H { "code" : 1 , "msg" : "addr 不能为空" } )
return
}
2026-03-17 07:33:19 +00:00
// SSRF validation for docreader address
if err := secutils . ValidateURLForSSRF ( addr ) ; err != nil {
logger . Warnf ( c . Request . Context ( ) , "SSRF validation failed for docreader addr: %v" , err )
c . JSON ( 400 , gin . H { "code" : 1 , "msg" : fmt . Sprintf ( "地址未通过安全校验: %v" , err ) } )
return
}
2026-02-28 05:22:07 +00:00
if h . documentReader == nil {
c . JSON ( 500 , gin . H { "code" : 1 , "msg" : "document converter not initialized" } )
return
}
if err := h . documentReader . Reconnect ( addr ) ; err != nil {
logger . Errorf ( c . Request . Context ( ) , "Failed to reconnect docreader to %s: %v" , addr , err )
c . JSON ( 200 , gin . H { "code" : 1 , "msg" : fmt . Sprintf ( "连接失败: %v" , err ) } )
return
}
var overrides map [ string ] string
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
2026-04-15 02:55:39 +00:00
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil {
if tenant . ParserEngineConfig != nil {
overrides = tenant . ParserEngineConfig . ToOverridesMap ( )
}
if creds := tenant . Credentials . GetWeKnoraCloud ( ) ; creds != nil {
if overrides == nil {
overrides = make ( map [ string ] string )
}
overrides [ "weknoracloud_app_id" ] = creds . AppID
}
2026-02-28 05:22:07 +00:00
}
}
2026-04-13 09:23:16 +00:00
remoteEngines := h . fetchRemoteEngines ( c . Request . Context ( ) , h . documentReader , overrides )
2026-02-28 05:22:07 +00:00
engines := docparser . ListAllEngines ( true , overrides , remoteEngines )
_ , docreaderTransport := h . getDocReaderConnInfo ( )
c . JSON ( 200 , gin . H { "code" : 0 , "msg" : "连接成功" , "data" : engines , "docreader_addr" : addr , "docreader_transport" : docreaderTransport , "connected" : true } )
}
// CheckParserEngines runs availability check with the given config overrides (e.g. current form values).
// Used to test engine availability without saving; body shape matches ParserEngineConfig.
// @Summary 使用当前参数检测解析引擎可用性
// @Tags 系统
// @Accept json
// @Produce json
// @Param body body object true "解析引擎配置(与保存接口同结构)"
// @Success 200
// @Router /system/parser-engines/check [post]
func ( h * SystemHandler ) CheckParserEngines ( c * gin . Context ) {
var body types . ParserEngineConfig
if err := c . ShouldBindJSON ( & body ) ; err != nil {
c . JSON ( 400 , gin . H { "code" : 1 , "msg" : "请求体格式错误" } )
return
}
overrides := body . ToOverridesMap ( )
2026-04-15 02:55:39 +00:00
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil {
if creds := tenant . Credentials . GetWeKnoraCloud ( ) ; creds != nil {
if overrides == nil {
overrides = make ( map [ string ] string )
}
overrides [ "weknoracloud_app_id" ] = creds . AppID
}
}
}
2026-04-13 09:23:16 +00:00
reader , docreaderAddr , docreaderTransport := h . resolveDocReader ( c . Request . Context ( ) , overrides )
connected := reader != nil && reader . IsConnected ( )
remoteEngines := h . fetchRemoteEngines ( c . Request . Context ( ) , reader , overrides )
2026-02-28 05:22:07 +00:00
engines := docparser . ListAllEngines ( connected , overrides , remoteEngines )
c . JSON ( 200 , gin . H { "code" : 0 , "msg" : "success" , "data" : engines , "docreader_addr" : docreaderAddr , "docreader_transport" : docreaderTransport , "connected" : connected } )
}
2026-04-13 09:23:16 +00:00
func ( h * SystemHandler ) resolveDocReader ( ctx context . Context , overrides map [ string ] string ) ( interfaces . DocumentReader , string , string ) {
if len ( overrides ) > 0 {
if addr := strings . TrimSpace ( overrides [ "docreader_addr" ] ) ; addr != "" && service . IsWeKnoraCloudDocReaderAddr ( addr ) {
reader := h . ResolveDocumentReader ( ctx , addr )
return reader , addr , transportFromDocReaderAddr ( addr )
}
}
addr , transport := h . getDocReaderConnInfo ( )
return h . documentReader , addr , transport
}
func transportFromDocReaderAddr ( addr string ) string {
if strings . HasPrefix ( strings . ToLower ( strings . TrimSpace ( addr ) ) , "https://" ) {
return "https"
}
return "http"
}
2026-02-28 05:22:07 +00:00
// fetchRemoteEngines queries the remote docreader for its engine list.
// Returns nil on any error (e.g. not connected), letting the caller
// fall back to Go's static registry only.
2026-04-13 09:23:16 +00:00
func ( h * SystemHandler ) fetchRemoteEngines ( ctx context . Context , reader interfaces . DocumentReader , overrides map [ string ] string ) [ ] types . ParserEngineInfo {
if reader == nil || ! reader . IsConnected ( ) {
2026-02-28 05:22:07 +00:00
return nil
}
2026-04-13 09:23:16 +00:00
engines , err := reader . ListEngines ( ctx , overrides )
2026-02-28 05:22:07 +00:00
if err != nil {
logger . Warnf ( ctx , "Failed to fetch remote engines from docreader: %v" , err )
return nil
}
return engines
}
2025-11-16 15:46:33 +00:00
// getKeywordIndexEngine returns the keyword index engine name
func ( h * SystemHandler ) getKeywordIndexEngine ( ) string {
retrieveDriver := os . Getenv ( "RETRIEVE_DRIVER" )
if retrieveDriver == "" {
return "未配置"
}
drivers := strings . Split ( retrieveDriver , "," )
// Filter out engines that support keyword retrieval
keywordEngines := [ ] string { }
for _ , driver := range drivers {
driver = strings . TrimSpace ( driver )
2026-01-12 09:34:01 +00:00
if h . supportsRetrieverType ( driver , types . KeywordsRetrieverType ) {
2025-11-16 15:46:33 +00:00
keywordEngines = append ( keywordEngines , driver )
}
}
if len ( keywordEngines ) == 0 {
return "未配置"
}
return strings . Join ( keywordEngines , ", " )
}
// getVectorStoreEngine returns the vector store engine name
func ( h * SystemHandler ) getVectorStoreEngine ( ) string {
// First check config.yaml
if h . cfg != nil && h . cfg . VectorDatabase != nil && h . cfg . VectorDatabase . Driver != "" {
return h . cfg . VectorDatabase . Driver
}
// Fallback to RETRIEVE_DRIVER for vector support
retrieveDriver := os . Getenv ( "RETRIEVE_DRIVER" )
if retrieveDriver == "" {
return "未配置"
}
drivers := strings . Split ( retrieveDriver , "," )
// Filter out engines that support vector retrieval
vectorEngines := [ ] string { }
for _ , driver := range drivers {
driver = strings . TrimSpace ( driver )
2026-01-12 09:34:01 +00:00
if h . supportsRetrieverType ( driver , types . VectorRetrieverType ) {
2025-11-16 15:46:33 +00:00
vectorEngines = append ( vectorEngines , driver )
}
}
if len ( vectorEngines ) == 0 {
return "未配置"
}
return strings . Join ( vectorEngines , ", " )
}
// getGraphDatabaseEngine returns the graph database engine name
func ( h * SystemHandler ) getGraphDatabaseEngine ( ) string {
2025-11-20 08:35:07 +00:00
if h . neo4jDriver == nil {
2026-03-06 15:31:14 +00:00
return "Not Enabled"
2025-11-16 15:46:33 +00:00
}
2025-11-20 08:35:07 +00:00
return "Neo4j"
2025-11-16 15:46:33 +00:00
}
2025-11-17 03:38:02 +00:00
2026-01-12 09:34:01 +00:00
// supportsRetrieverType checks if a driver supports a specific retriever type
// by looking up the retrieverEngineMapping from types package
func ( h * SystemHandler ) supportsRetrieverType ( driver string , retrieverType types . RetrieverType ) bool {
// Get the mapping of all supported drivers and their capabilities
mapping := types . GetRetrieverEngineMapping ( )
// Check if the driver exists in the mapping
engines , exists := mapping [ driver ]
if ! exists {
return false
}
// Check if any of the engine configurations support the requested retriever type
for _ , engine := range engines {
if engine . RetrieverType == retrieverType {
return true
}
}
return false
}
2026-02-28 05:22:07 +00:00
// getMinioConfig resolves MinIO connection parameters from tenant config (if mode=remote) or env vars (mode=docker/default).
func ( h * SystemHandler ) getMinioConfig ( c * gin . Context ) ( endpoint , accessKeyID , secretAccessKey string ) {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . MinIO != nil {
m := tenant . StorageEngineConfig . MinIO
if m . Mode == "remote" {
return m . Endpoint , m . AccessKeyID , m . SecretAccessKey
}
}
}
endpoint = os . Getenv ( "MINIO_ENDPOINT" )
accessKeyID = os . Getenv ( "MINIO_ACCESS_KEY_ID" )
secretAccessKey = os . Getenv ( "MINIO_SECRET_ACCESS_KEY" )
return
}
2025-11-17 03:38:02 +00:00
2026-02-28 05:22:07 +00:00
// isMinioConfigured checks whether MinIO connection info is available (from tenant config or env).
func ( h * SystemHandler ) isMinioConfigured ( c * gin . Context ) bool {
endpoint , accessKeyID , secretAccessKey := h . getMinioConfig ( c )
2025-11-17 03:38:02 +00:00
return endpoint != "" && accessKeyID != "" && secretAccessKey != ""
}
2025-12-30 03:37:30 +00:00
2026-02-28 05:22:07 +00:00
// isMinioEnvAvailable checks whether MinIO env vars (MINIO_ENDPOINT etc.) are set.
func ( h * SystemHandler ) isMinioEnvAvailable ( ) bool {
return os . Getenv ( "MINIO_ENDPOINT" ) != "" &&
os . Getenv ( "MINIO_ACCESS_KEY_ID" ) != "" &&
os . Getenv ( "MINIO_SECRET_ACCESS_KEY" ) != ""
}
// isCOSConfigured checks whether COS connection info is available from tenant config.
func ( h * SystemHandler ) isCOSConfigured ( c * gin . Context ) bool {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . COS != nil {
cosConf := tenant . StorageEngineConfig . COS
return cosConf . SecretID != "" && cosConf . SecretKey != "" && cosConf . Region != "" && cosConf . BucketName != ""
}
}
return false
}
2026-02-28 07:57:09 +00:00
// isTOSConfigured checks whether TOS connection info is available from tenant config or env.
func ( h * SystemHandler ) isTOSConfigured ( c * gin . Context ) bool {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . TOS != nil {
tosConf := tenant . StorageEngineConfig . TOS
return tosConf . Endpoint != "" && tosConf . Region != "" && tosConf . AccessKey != "" && tosConf . SecretKey != "" && tosConf . BucketName != ""
}
}
return h . isTOSEnvAvailable ( )
}
2026-04-13 04:04:47 +00:00
// isOSSConfigured checks whether OSS connection info is available from tenant config.
func ( h * SystemHandler ) isOSSConfigured ( c * gin . Context ) bool {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . OSS != nil {
ossConf := tenant . StorageEngineConfig . OSS
return ossConf . Endpoint != "" && ossConf . Region != "" && ossConf . AccessKey != "" && ossConf . SecretKey != "" && ossConf . BucketName != ""
}
}
return false
}
2026-05-01 11:20:25 +00:00
// isKS3Configured checks whether KS3 connection info is available from tenant config.
func ( h * SystemHandler ) isKS3Configured ( c * gin . Context ) bool {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . KS3 != nil {
ks3Conf := tenant . StorageEngineConfig . KS3
return ks3Conf . Endpoint != "" && ks3Conf . Region != "" && ks3Conf . AccessKey != "" && ks3Conf . SecretKey != "" && ks3Conf . BucketName != ""
}
}
return false
}
2026-02-28 07:57:09 +00:00
// isTOSEnvAvailable checks whether TOS env vars are set.
func ( h * SystemHandler ) isTOSEnvAvailable ( ) bool {
return os . Getenv ( "TOS_ENDPOINT" ) != "" &&
os . Getenv ( "TOS_REGION" ) != "" &&
os . Getenv ( "TOS_ACCESS_KEY" ) != "" &&
os . Getenv ( "TOS_SECRET_KEY" ) != "" &&
os . Getenv ( "TOS_BUCKET_NAME" ) != ""
}
2026-02-28 05:22:07 +00:00
// StorageEngineStatusItem describes one storage engine's availability and description.
type StorageEngineStatusItem struct {
fix(storage): merge tos/s3/oss/ks3 tenant configs in buildStorageConfig (#1117)
knowledgeService.buildStorageConfig had hardcoded switches in two places that
only handled local/minio/cos. Knowledge bases configured for tos, s3, oss, or
ks3 reached the docreader with a DocParserStorageConfig containing only the
Provider tag — bucket, endpoint, and credentials were silently dropped, and
extraction stalled or fetched from the wrong location.
- Tenant-merge switch: add tos/s3/oss/ks3 cases mapping
StorageEngineConfig.{TOS,S3,OSS,KS3} fields onto DocParserStorageConfig.
Same shape as the existing minio/cos branches; AccessKey -> AccessKeyID,
SecretKey -> SecretAccessKey, no AppID.
- Legacy compat switch: list tos/s3/oss/ks3 alongside local with hasKBFull=false
so fall-through to the tenant-merge path is intentional rather than an
unrecognised provider sliding past the switch. The legacy `cos_config` column
predates these providers and they should never resolve via that path.
- Doc drift: types/custom_agent.go ImageStorageProvider comment and
handler/system.go StorageEngineStatusItem.Name comment now list all 7
providers (matched against types.StorageEngineConfig and ParseProviderScheme).
Tests (knowledge_storage_config_test.go, +268 LOC, 17 cases):
- TestBuildStorageConfig_TenantMergeAllProviders pins per-provider field
population for all 7 providers; the tos/s3/oss/ks3 sub-tests are the
regression guard for #1117.
- TestBuildStorageConfig_LegacyPathOnlyForCOSAndMinIO pins that legacy
StorageConfig data is only consumed for cos/minio and skipped for the
others (intentional fall-through).
- TestBuildStorageConfig_NoTenantFallsThroughToEmpty pins the no-tenant case.
Out of scope (called out in the linked issue):
- The systemic refactor (single source-of-truth provider registry) — left as
a follow-up; this PR keeps changes local to the buggy code path.
- initialization.go validateStorageEngineConfig multimodal validation gap —
the InitializationRequest struct does not carry tos/s3/oss/ks3 fields, so
fixing it requires API surface additions; can ship separately.
Closes #1117 (the buildStorageConfig switches; remaining items deferred).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 08:47:59 +00:00
Name string ` json:"name" ` // "local", "minio", "cos", "tos", "s3", "oss", "ks3"
2026-04-29 10:58:09 +00:00
Allowed bool ` json:"allowed" `
2026-02-28 05:22:07 +00:00
Available bool ` json:"available" ` // whether the engine can be used
Description string ` json:"description" ` // short description for UI
}
// GetStorageEngineStatusResponse is the response for GET /system/storage-engine-status.
type GetStorageEngineStatusResponse struct {
Engines [ ] StorageEngineStatusItem ` json:"engines" `
2026-04-29 10:58:09 +00:00
AllowedProviders [ ] string ` json:"allowed_providers" `
2026-02-28 05:22:07 +00:00
MinioEnvAvailable bool ` json:"minio_env_available" `
}
// GetStorageEngineStatus godoc
// @Summary 获取存储引擎状态
// @Description 返回 Local、MinIO、COS 各存储引擎的可用状态及说明,供全局设置与知识库选择使用
// @Tags 系统
// @Produce json
// @Success 200 {object} GetStorageEngineStatusResponse
// @Router /system/storage-engine-status [get]
func ( h * SystemHandler ) GetStorageEngineStatus ( c * gin . Context ) {
minioConfigured := h . isMinioConfigured ( c )
minioEnvAvailable := h . isMinioEnvAvailable ( )
cosConfigured := h . isCOSConfigured ( c )
2026-02-28 07:57:09 +00:00
tosConfigured := h . isTOSConfigured ( c )
2026-04-29 10:58:09 +00:00
s3Configured := h . isS3Configured ( c )
2026-04-13 04:04:47 +00:00
ossConfigured := h . isOSSConfigured ( c )
2026-05-01 11:20:25 +00:00
ks3Configured := h . isKS3Configured ( c )
2026-04-29 10:58:09 +00:00
allowed := getAllowedStorageProviders ( )
allowedProviders := make ( [ ] string , 0 , len ( supportedStorageProviders ) )
for _ , provider := range getSupportedStorageProviders ( ) {
if allowed [ provider ] {
allowedProviders = append ( allowedProviders , provider )
}
}
2026-02-28 05:22:07 +00:00
engines := [ ] StorageEngineStatusItem {
2026-04-29 10:58:09 +00:00
{ Name : "local" , Allowed : allowed [ "local" ] , Available : true , Description : "本地文件系统存储,仅适合单机部署" } ,
{ Name : "minio" , Allowed : allowed [ "minio" ] , Available : minioConfigured || minioEnvAvailable , Description : "S3 兼容的自托管对象存储,适合内网和私有云部署" } ,
{ Name : "cos" , Allowed : allowed [ "cos" ] , Available : cosConfigured , Description : "腾讯云对象存储服务,适合公有云部署,支持 CDN 加速" } ,
{ Name : "tos" , Allowed : allowed [ "tos" ] , Available : tosConfigured , Description : "火山引擎对象存储服务,适合公有云部署" } ,
{ Name : "s3" , Allowed : allowed [ "s3" ] , Available : s3Configured , Description : "AWS S3 与兼容对象存储服务,适合公有云与混合云部署" } ,
{ Name : "oss" , Allowed : allowed [ "oss" ] , Available : ossConfigured , Description : "阿里云对象存储服务,适合公有云部署,支持 S3 兼容协议" } ,
2026-05-01 11:20:25 +00:00
{ Name : "ks3" , Allowed : allowed [ "ks3" ] , Available : ks3Configured , Description : "金山云对象存储服务,适合公有云部署" } ,
2026-02-28 05:22:07 +00:00
}
c . JSON ( 200 , gin . H {
"code" : 0 ,
"msg" : "success" ,
2026-04-29 10:58:09 +00:00
"data" : GetStorageEngineStatusResponse { Engines : engines , AllowedProviders : allowedProviders , MinioEnvAvailable : minioEnvAvailable } ,
2026-02-28 05:22:07 +00:00
} )
}
// --- Storage engine helpers ---
// cosFieldPattern validates COS region and bucket name format to prevent URL injection.
var cosFieldPattern = regexp . MustCompile ( ` ^[a-zA-Z0-9][a-zA-Z0-9._-] { 0,62}$ ` )
2026-04-13 06:14:20 +00:00
// ossFieldPattern validates OSS region and bucket name format to prevent URL injection.
var ossFieldPattern = regexp . MustCompile ( ` ^[a-zA-Z0-9][a-zA-Z0-9._-] { 0,62}$ ` )
2026-02-28 05:22:07 +00:00
// sanitizeStorageCheckError converts a raw storage connectivity error into a safe
// user-facing message that does not leak internal network details (hostnames, IPs, ports).
func sanitizeStorageCheckError ( err error ) string {
msg := err . Error ( )
switch {
2026-03-16 10:23:44 +00:00
case strings . Contains ( msg , "Endpoint url cannot have fully qualified paths" ) :
return "Endpoint 地址格式错误:请去除 http:// 或 https:// 前缀,只填写域名或 IP 地址和端口( 例如: minio.example.com:9000) "
2026-02-28 05:22:07 +00:00
case strings . Contains ( msg , "no such host" ) :
return "DNS 解析失败,请检查地址是否正确"
case strings . Contains ( msg , "connection refused" ) :
return "连接被拒绝,请确认服务已启动且端口正确"
case strings . Contains ( msg , "no route to host" ) :
return "无法路由到目标地址,请检查网络配置"
case strings . Contains ( msg , "i/o timeout" ) || strings . Contains ( msg , "deadline exceeded" ) || strings . Contains ( msg , "context deadline" ) :
return "连接超时,请检查网络或服务状态"
case strings . Contains ( msg , "403" ) || strings . Contains ( msg , "AccessDenied" ) || strings . Contains ( msg , "access denied" ) :
return "认证失败,请检查访问凭证是否正确"
case strings . Contains ( msg , "certificate" ) || strings . Contains ( msg , "tls" ) || strings . Contains ( msg , "x509" ) :
return "TLS/SSL 证书错误,请检查 SSL 配置"
case strings . Contains ( msg , "404" ) || strings . Contains ( msg , "NoSuchBucket" ) :
return "Bucket 不存在,请检查名称和 Region"
default :
return "连接失败,请检查配置参数是否正确"
}
}
// isBlockedStorageEndpoint checks whether a storage endpoint resolves to a dangerous
2026-03-31 10:52:25 +00:00
// address (cloud metadata, loopback, link-local). Unlike the stricter isSSRFSafeURL,
2026-02-28 05:22:07 +00:00
// this allows private IPs since MinIO is commonly deployed on internal networks.
2026-03-17 07:33:19 +00:00
// It also respects the SSRF_WHITELIST environment variable for whitelisted hosts.
2026-02-28 05:22:07 +00:00
func isBlockedStorageEndpoint ( endpoint string ) ( bool , string ) {
host , _ , err := net . SplitHostPort ( endpoint )
if err != nil {
host = endpoint
}
2026-03-17 07:33:19 +00:00
// Check SSRF whitelist first – whitelisted hosts bypass the block check.
if secutils . IsSSRFWhitelisted ( host ) {
return false , ""
}
2026-02-28 05:22:07 +00:00
hostLower := strings . ToLower ( host )
blockedHosts := [ ] string {
"metadata.google.internal" ,
"metadata.tencentyun.com" ,
"metadata.aws.internal" ,
}
for _ , bh := range blockedHosts {
if hostLower == bh {
return true , "该地址不允许访问"
}
}
checkIP := func ( ip net . IP ) ( bool , string ) {
if ip . IsLoopback ( ) {
return true , "不允许访问本地回环地址"
}
if ip . IsLinkLocalUnicast ( ) || ip . IsLinkLocalMulticast ( ) {
return true , "不允许访问链路本地地址"
}
if ip . IsUnspecified ( ) {
return true , "无效的地址"
}
return false , ""
}
if ip := net . ParseIP ( host ) ; ip != nil {
return checkIP ( ip )
}
ips , err := net . LookupIP ( host )
if err != nil {
return false , ""
}
for _ , ip := range ips {
if blocked , reason := checkIP ( ip ) ; blocked {
return blocked , reason
}
}
return false , ""
}
// --- Storage engine connectivity check ---
// StorageCheckRequest is the body for POST /system/storage-engine-check.
type StorageCheckRequest struct {
2026-05-01 11:20:25 +00:00
Provider string ` json:"provider" ` // "minio", "cos", "tos", "s3", "oss", "ks3"
2026-02-28 05:22:07 +00:00
MinIO * types . MinIOEngineConfig ` json:"minio,omitempty" `
COS * types . COSEngineConfig ` json:"cos,omitempty" `
2026-02-28 07:57:09 +00:00
TOS * types . TOSEngineConfig ` json:"tos,omitempty" `
2026-03-07 06:01:22 +00:00
S3 * types . S3EngineConfig ` json:"s3,omitempty" `
2026-04-13 04:04:47 +00:00
OSS * types . OSSEngineConfig ` json:"oss,omitempty" `
2026-05-01 11:20:25 +00:00
KS3 * types . KS3EngineConfig ` json:"ks3,omitempty" `
2026-02-28 05:22:07 +00:00
}
// StorageCheckResponse is the response for a single-engine connectivity check.
type StorageCheckResponse struct {
2026-03-04 12:46:40 +00:00
OK bool ` json:"ok" `
Message string ` json:"message" `
BucketCreated bool ` json:"bucket_created,omitempty" `
2026-02-28 05:22:07 +00:00
}
// CheckStorageEngine tests connectivity for a single storage engine using the provided config.
// @Summary 测试存储引擎连通性
// @Description 使用当前填写的参数测试 MinIO/COS 连通性,不保存配置
// @Tags 系统
// @Accept json
// @Produce json
// @Param body body StorageCheckRequest true "存储引擎配置"
// @Success 200 {object} StorageCheckResponse
// @Router /system/storage-engine-check [post]
func ( h * SystemHandler ) CheckStorageEngine ( c * gin . Context ) {
ctx := logger . CloneContext ( c . Request . Context ( ) )
var req StorageCheckRequest
if err := c . ShouldBindJSON ( & req ) ; err != nil {
c . JSON ( 400 , gin . H { "code" : 1 , "msg" : "请求体格式错误" } )
return
}
2026-04-29 10:58:09 +00:00
if ! isStorageProviderAllowed ( req . Provider ) {
c . JSON ( 403 , gin . H { "code" : 1 , "msg" : "该存储引擎已被禁用" } )
return
}
2026-02-28 05:22:07 +00:00
switch req . Provider {
case "minio" :
h . checkMinio ( c , ctx , req . MinIO )
case "cos" :
h . checkCOS ( c , ctx , req . COS )
2026-02-28 07:57:09 +00:00
case "tos" :
h . checkTOS ( c , ctx , req . TOS )
2026-03-07 06:01:22 +00:00
case "s3" :
h . checkS3 ( c , ctx , req . S3 )
2026-04-13 04:04:47 +00:00
case "oss" :
h . checkOSS ( c , ctx , req . OSS )
2026-05-01 11:20:25 +00:00
case "ks3" :
h . checkKS3 ( c , ctx , req . KS3 )
2026-02-28 05:22:07 +00:00
default :
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : "本地存储无需检测" } } )
}
}
2026-04-29 10:58:09 +00:00
func ( h * SystemHandler ) isS3Configured ( c * gin . Context ) bool {
if v , exists := c . Get ( types . TenantInfoContextKey . String ( ) ) ; exists {
if tenant , ok := v . ( * types . Tenant ) ; ok && tenant != nil && tenant . StorageEngineConfig != nil && tenant . StorageEngineConfig . S3 != nil {
s3Conf := tenant . StorageEngineConfig . S3
return s3Conf . Endpoint != "" && s3Conf . Region != "" && s3Conf . AccessKey != "" && s3Conf . SecretKey != "" && s3Conf . BucketName != ""
}
}
return false
}
2026-02-28 05:22:07 +00:00
func ( h * SystemHandler ) checkMinio ( c * gin . Context , ctx context . Context , cfg * types . MinIOEngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 MinIO 配置" } } )
return
}
2026-04-15 08:24:37 +00:00
if cfg . BucketName != "" && ! cosFieldPattern . MatchString ( cfg . BucketName ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Bucket 名称格式不正确,仅允许字母、数字、点、连字符" } } )
return
}
2026-02-28 05:22:07 +00:00
endpoint , accessKeyID , secretAccessKey := cfg . Endpoint , cfg . AccessKeyID , cfg . SecretAccessKey
if cfg . Mode != "remote" {
endpoint = os . Getenv ( "MINIO_ENDPOINT" )
accessKeyID = os . Getenv ( "MINIO_ACCESS_KEY_ID" )
secretAccessKey = os . Getenv ( "MINIO_SECRET_ACCESS_KEY" )
}
if endpoint == "" || accessKeyID == "" || secretAccessKey == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Endpoint、Access Key、Secret Key 不能为空" } } )
return
}
if cfg . Mode == "remote" {
if blocked , reason := isBlockedStorageEndpoint ( endpoint ) ; blocked {
2026-04-08 14:20:32 +00:00
logger . Warnf ( ctx , "Storage check: MinIO endpoint blocked by SSRF protection, endpoint: %s" , endpoint )
2026-02-28 05:22:07 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : reason } } )
return
}
}
err := file . CheckMinioConnectivity ( ctx , endpoint , accessKeyID , secretAccessKey , cfg . BucketName , cfg . UseSSL )
if err != nil {
errMsg := err . Error ( )
2026-04-15 08:24:37 +00:00
// If bucket does not exist, auto-create it
2026-03-04 12:46:40 +00:00
if strings . Contains ( errMsg , "does not exist" ) && cfg . BucketName != "" {
logger . Info ( ctx , "Storage check: bucket does not exist, attempting auto-creation" , "bucket" , cfg . BucketName )
minioClient , clientErr := minio . New ( endpoint , & minio . Options {
Creds : credentials . NewStaticV4 ( accessKeyID , secretAccessKey , "" ) ,
Secure : cfg . UseSSL ,
} )
if clientErr != nil {
2026-04-15 08:24:37 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Failed to create MinIO client: %s" , sanitizeStorageCheckError ( clientErr ) ) } } )
2026-03-04 12:46:40 +00:00
return
}
if mkErr := minioClient . MakeBucket ( ctx , cfg . BucketName , minio . MakeBucketOptions { } ) ; mkErr != nil {
logger . Error ( ctx , "Storage check: failed to create bucket" , "bucket" , cfg . BucketName , "error" , mkErr )
2026-04-15 08:24:37 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Failed to auto-create Bucket '%s': %s" , cfg . BucketName , sanitizeStorageCheckError ( mkErr ) ) } } )
2026-03-04 12:46:40 +00:00
return
}
2026-04-15 08:24:37 +00:00
logger . Info ( ctx , "Storage check: bucket created" , "bucket" , cfg . BucketName )
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , BucketCreated : true , Message : fmt . Sprintf ( "Bucket '%s' does not exist, and has been automatically created" , cfg . BucketName ) } } )
2026-02-28 05:22:07 +00:00
return
}
2026-03-04 12:46:40 +00:00
logger . Error ( ctx , "Storage check: MinIO connectivity failed" , "error" , err )
2026-02-28 05:22:07 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : sanitizeStorageCheckError ( err ) } } )
return
}
msg := "连接成功"
if cfg . BucketName != "" {
msg = fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName )
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : msg } } )
}
func ( h * SystemHandler ) checkCOS ( c * gin . Context , ctx context . Context , cfg * types . COSEngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 COS 配置" } } )
return
}
if cfg . SecretID == "" || cfg . SecretKey == "" || cfg . Region == "" || cfg . BucketName == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Secret ID、Secret Key、Region、Bucket 名称不能为空" } } )
return
}
if ! cosFieldPattern . MatchString ( cfg . Region ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Region 格式不正确,仅允许字母、数字、点、连字符" } } )
return
}
if ! cosFieldPattern . MatchString ( cfg . BucketName ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Bucket 名称格式不正确,仅允许字母、数字、点、连字符" } } )
return
}
err := file . CheckCosConnectivity ( ctx , cfg . BucketName , cfg . Region , cfg . SecretID , cfg . SecretKey )
if err != nil {
logger . Errorf ( ctx , "Storage check: COS connectivity failed, bucket: %s, error: %v" , cfg . BucketName , err )
errMsg := err . Error ( )
if strings . Contains ( errMsg , "403" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "认证失败,请检查 Secret ID / Secret Key 是否正确" } } )
return
}
if strings . Contains ( errMsg , "404" ) || strings . Contains ( errMsg , "NoSuchBucket" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Bucket「%s」不存在, 请检查名称和 Region" , cfg . BucketName ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : sanitizeStorageCheckError ( err ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName ) } } )
}
2026-02-28 07:57:09 +00:00
func ( h * SystemHandler ) checkTOS ( c * gin . Context , ctx context . Context , cfg * types . TOSEngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 TOS 配置" } } )
return
}
if cfg . Endpoint == "" || cfg . Region == "" || cfg . AccessKey == "" || cfg . SecretKey == "" || cfg . BucketName == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Endpoint、Region、Access Key、Secret Key、Bucket 名称不能为空" } } )
return
}
if blocked , reason := isBlockedStorageEndpoint ( cfg . Endpoint ) ; blocked {
logger . Warnf ( ctx , "Storage check: TOS endpoint blocked by SSRF protection, endpoint: %s" , cfg . Endpoint )
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : reason } } )
return
}
err := file . CheckTosConnectivity ( ctx , cfg . Endpoint , cfg . Region , cfg . AccessKey , cfg . SecretKey , cfg . BucketName )
if err != nil {
logger . Errorf ( ctx , "Storage check: TOS connectivity failed, bucket: %s, error: %v" , cfg . BucketName , err )
errMsg := err . Error ( )
if strings . Contains ( errMsg , "403" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "认证失败,请检查 Access Key / Secret Key 是否正确" } } )
return
}
if strings . Contains ( errMsg , "404" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Bucket「%s」不存在, 请检查名称和 Region" , cfg . BucketName ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : sanitizeStorageCheckError ( err ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName ) } } )
}
2026-03-07 06:01:22 +00:00
func ( h * SystemHandler ) checkS3 ( c * gin . Context , ctx context . Context , cfg * types . S3EngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 S3 配置" } } )
return
}
if cfg . Endpoint == "" || cfg . Region == "" || cfg . AccessKey == "" || cfg . SecretKey == "" || cfg . BucketName == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Endpoint、Region、Access Key、Secret Key、Bucket 名称不能为空" } } )
return
}
if blocked , reason := isBlockedStorageEndpoint ( cfg . Endpoint ) ; blocked {
logger . Warnf ( ctx , "Storage check: S3 endpoint blocked by SSRF protection, endpoint: %s" , cfg . Endpoint )
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : reason } } )
return
}
err := file . CheckS3Connectivity ( ctx , cfg . Endpoint , cfg . AccessKey , cfg . SecretKey , cfg . BucketName , cfg . Region )
if err != nil {
logger . Errorf ( ctx , "Storage check: S3 connectivity failed, bucket: %s, error: %v" , cfg . BucketName , err )
errMsg := err . Error ( )
if strings . Contains ( errMsg , "403" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "认证失败,请检查 Access Key / Secret Key 是否正确" } } )
return
}
if strings . Contains ( errMsg , "404" ) || strings . Contains ( errMsg , "NotFound" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Bucket「%s」不存在, 请检查名称和 Region" , cfg . BucketName ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : sanitizeStorageCheckError ( err ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName ) } } )
}
2026-04-13 04:04:47 +00:00
func ( h * SystemHandler ) checkOSS ( c * gin . Context , ctx context . Context , cfg * types . OSSEngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 OSS 配置" } } )
return
}
endpoint , accessKey , secretKey := cfg . Endpoint , cfg . AccessKey , cfg . SecretKey
if endpoint == "" || accessKey == "" || secretKey == "" || cfg . BucketName == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Endpoint、Access Key、Secret Key、Bucket Name 不能为空" } } )
return
}
2026-04-13 06:14:20 +00:00
// Strip URL scheme before SSRF check — OSS endpoint may include http:// or https://
ssrfEndpoint := strings . TrimPrefix ( strings . TrimPrefix ( endpoint , "https://" ) , "http://" )
if blocked , reason := isBlockedStorageEndpoint ( ssrfEndpoint ) ; blocked {
2026-04-16 10:39:30 +00:00
logger . Warnf ( ctx , "Storage check: OSS endpoint blocked by SSRF protection, endpoint: %s" , endpoint )
2026-04-13 04:04:47 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : reason } } )
return
}
2026-04-13 06:14:20 +00:00
if ! ossFieldPattern . MatchString ( cfg . Region ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Region 格式不正确,仅允许字母、数字、点、连字符" } } )
return
}
if ! ossFieldPattern . MatchString ( cfg . BucketName ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Bucket 名称格式不正确,仅允许字母、数字、点、连字符" } } )
return
}
2026-04-13 04:04:47 +00:00
err := file . CheckOssConnectivity ( ctx , endpoint , cfg . Region , accessKey , secretKey , cfg . BucketName )
if err != nil {
errMsg := err . Error ( )
if strings . Contains ( errMsg , "403" ) || strings . Contains ( errMsg , "AccessDenied" ) {
2026-04-16 10:39:30 +00:00
logger . Errorf ( ctx , "Storage check: OSS auth failed, endpoint: %s, bucket: %s" , endpoint , cfg . BucketName )
2026-04-13 04:04:47 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "认证失败,请检查 Access Key / Secret Key 是否正确" } } )
return
}
if strings . Contains ( errMsg , "404" ) || strings . Contains ( errMsg , "NoSuchBucket" ) {
2026-04-16 10:39:30 +00:00
logger . Errorf ( ctx , "Storage check: OSS bucket not found, bucket: %s" , cfg . BucketName )
2026-04-13 04:04:47 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Bucket「%s」不存在" , cfg . BucketName ) } } )
return
}
2026-04-16 10:39:30 +00:00
logger . Errorf ( ctx , "Storage check: OSS connectivity failed, endpoint: %s, bucket: %s, error: %v" , endpoint , cfg . BucketName , err )
2026-04-13 04:04:47 +00:00
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "OSS 连通性检测失败: %s" , sanitizeStorageCheckError ( err ) ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName ) } } )
2026-04-14 02:03:45 +00:00
}
2026-05-01 11:20:25 +00:00
func ( h * SystemHandler ) checkKS3 ( c * gin . Context , ctx context . Context , cfg * types . KS3EngineConfig ) {
if cfg == nil {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "未提供 KS3 配置" } } )
return
}
endpoint , region , accessKey , secretKey := cfg . Endpoint , cfg . Region , cfg . AccessKey , cfg . SecretKey
if endpoint == "" || region == "" || accessKey == "" || secretKey == "" || cfg . BucketName == "" {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "Endpoint、Region、Access Key、Secret Key、Bucket 名称不能为空" } } )
return
}
if blocked , reason := isBlockedStorageEndpoint ( endpoint ) ; blocked {
logger . Warnf ( ctx , "Storage check: KS3 endpoint blocked by SSRF protection, endpoint: %s" , endpoint )
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : reason } } )
return
}
err := file . CheckKS3Connectivity ( ctx , endpoint , region , accessKey , secretKey , cfg . BucketName )
if err != nil {
logger . Errorf ( ctx , "Storage check: KS3 connectivity failed, bucket: %s, error: %v" , cfg . BucketName , err )
errMsg := err . Error ( )
if strings . Contains ( errMsg , "403" ) || strings . Contains ( errMsg , "AccessDenied" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : "认证失败,请检查 Access Key / Secret Key 是否正确" } } )
return
}
if strings . Contains ( errMsg , "404" ) || strings . Contains ( errMsg , "NoSuchBucket" ) {
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : fmt . Sprintf ( "Bucket「%s」不存在, 请检查名称和 Region" , cfg . BucketName ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : false , Message : sanitizeStorageCheckError ( err ) } } )
return
}
c . JSON ( 200 , gin . H { "code" : 0 , "data" : StorageCheckResponse { OK : true , Message : fmt . Sprintf ( "连接成功, Bucket「%s」已确认存在" , cfg . BucketName ) } } )
}
2026-04-13 09:23:16 +00:00
func ( h * SystemHandler ) ResolveDocumentReader ( ctx context . Context , addr string ) interfaces . DocumentReader {
if addr == "" {
return h . documentReader
}
if service . IsWeKnoraCloudDocReaderAddr ( addr ) {
2026-04-15 02:55:39 +00:00
creds := h . tenantSvc . GetWeKnoraCloudCredentials ( ctx )
2026-04-13 09:23:16 +00:00
if creds == nil {
return nil
}
2026-04-15 02:55:39 +00:00
reader , err := docparser . NewWeKnoraCloudSignedDocumentReader ( creds . AppID , creds . AppSecret )
2026-04-13 09:23:16 +00:00
if err != nil {
return nil
}
return reader
}
reader , err := docparser . NewHTTPDocumentReader ( addr )
if err != nil || reader == nil {
return reader
}
return reader
2026-04-13 04:04:47 +00:00
}