feat(storage): improve storage connectivity error handling and multi-instance support

Enhance error messaging for storage connectivity issues by sanitizing raw errors to prevent leaking sensitive information. Introduce a new utility function for consistent error handling across storage backend operations. Additionally, implement logic to support multi-instance storage backends, allowing for more flexible workspace configurations and improved reporting of active storage providers.
This commit is contained in:
wizardchen
2026-07-15 14:36:26 +08:00
committed by lyingbug
parent 6349fff002
commit a8dea5f9a3
8 changed files with 208 additions and 42 deletions
+64 -37
View File
@@ -58,6 +58,11 @@ type SystemHandler struct {
// runtime task console. It updates business state and tracing before queue
// records are removed, unlike a raw Redis deletion.
knowledgeSvc runtimeKnowledgeCanceller
// storageBackendRepo lets GetStorageEngineStatus report multi-instance
// storage backends (Settings → Storage) as "available", not just the legacy
// singleton tenant.StorageEngineConfig. Optional — nil in partially-wired
// unit tests, in which case only the legacy config is consulted.
storageBackendRepo interfaces.StorageBackendRepository
}
// NewSystemHandler creates a new system handler
@@ -70,17 +75,19 @@ func NewSystemHandler(cfg *config.Config,
auditSvc interfaces.AuditLogService,
taskInspector interfaces.TaskInspector,
knowledgeSvc interfaces.KnowledgeService,
storageBackendRepo interfaces.StorageBackendRepository,
) *SystemHandler {
return &SystemHandler{
cfg: cfg,
neo4jDriver: neo4jDriver,
documentReader: documentReader,
tenantSvc: tenantSvc,
userSvc: userSvc,
systemSettingSvc: systemSettingSvc,
auditSvc: auditSvc,
taskInspector: taskInspector,
knowledgeSvc: knowledgeSvc,
cfg: cfg,
neo4jDriver: neo4jDriver,
documentReader: documentReader,
tenantSvc: tenantSvc,
userSvc: userSvc,
systemSettingSvc: systemSettingSvc,
auditSvc: auditSvc,
taskInspector: taskInspector,
knowledgeSvc: knowledgeSvc,
storageBackendRepo: storageBackendRepo,
}
}
@@ -502,6 +509,40 @@ func (h *SystemHandler) getMinioConfig(c *gin.Context) (endpoint, accessKeyID, s
return
}
// activeBackendProviders returns the set of storage providers that have at
// least one active multi-instance backend registered for the caller's
// workspace. Used to keep GetStorageEngineStatus in sync with the new Storage
// settings UI, which writes to storage_backends rather than the legacy
// tenant.StorageEngineConfig singleton. Best-effort and nil-safe: a missing
// repo, missing tenant, or query error yields an empty set so callers fall
// back to the legacy config checks.
func (h *SystemHandler) activeBackendProviders(c *gin.Context) map[string]bool {
result := map[string]bool{}
if h.storageBackendRepo == nil {
return result
}
v, exists := c.Get(types.TenantInfoContextKey.String())
if !exists {
return result
}
tenant, ok := v.(*types.Tenant)
if !ok || tenant == nil {
return result
}
backends, err := h.storageBackendRepo.List(c.Request.Context(), tenant.ID)
if err != nil {
logger.Warnf(c.Request.Context(), "[storage] list backends for status failed: tenant=%d err=%v", tenant.ID, err)
return result
}
for _, backend := range backends {
if backend == nil || backend.Status != types.StorageBackendStatusActive {
continue
}
result[strings.ToLower(strings.TrimSpace(backend.Provider))] = true
}
return result
}
// 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)
@@ -611,14 +652,19 @@ type GetStorageEngineStatusResponse struct {
// @Success 200 {object} GetStorageEngineStatusResponse
// @Router /system/storage-engine-status [get]
func (h *SystemHandler) GetStorageEngineStatus(c *gin.Context) {
minioConfigured := h.isMinioConfigured(c)
// Providers that already have an active multi-instance backend registered
// (Settings → Storage). These are authoritative and independent of the
// legacy singleton tenant.StorageEngineConfig, so a workspace that only
// configured storage through the new UI is still reported as available.
activeBackend := h.activeBackendProviders(c)
minioConfigured := h.isMinioConfigured(c) || activeBackend["minio"]
minioEnvAvailable := h.isMinioEnvAvailable()
cosConfigured := h.isCOSConfigured(c)
tosConfigured := h.isTOSConfigured(c)
s3Configured := h.isS3Configured(c)
ossConfigured := h.isOSSConfigured(c)
ks3Configured := h.isKS3Configured(c)
obsConfigured := h.isOBSConfigured(c)
cosConfigured := h.isCOSConfigured(c) || activeBackend["cos"]
tosConfigured := h.isTOSConfigured(c) || activeBackend["tos"]
s3Configured := h.isS3Configured(c) || activeBackend["s3"]
ossConfigured := h.isOSSConfigured(c) || activeBackend["oss"]
ks3Configured := h.isKS3Configured(c) || activeBackend["ks3"]
obsConfigured := h.isOBSConfigured(c) || activeBackend["obs"]
allowed := getAllowedStorageProviders()
allowedProviders := make([]string, 0, len(getSupportedStorageProviders()))
for _, provider := range getSupportedStorageProviders() {
@@ -652,28 +698,9 @@ var ossFieldPattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`)
// sanitizeStorageCheckError converts a raw storage connectivity error into a safe
// user-facing message that does not leak internal network details (hostnames, IPs, ports).
// The concrete mapping lives in internal/utils so the service layer can share it.
func sanitizeStorageCheckError(err error) string {
msg := err.Error()
switch {
case strings.Contains(msg, "Endpoint url cannot have fully qualified paths"):
return "Endpoint 地址格式错误:请去除 http:// 或 https:// 前缀,只填写域名或 IP 地址和端口(例如:minio.example.com:9000)"
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 "连接失败,请检查配置参数是否正确"
}
return secutils.SanitizeStorageConnectivityError(err)
}
// storageEndpointHost extracts the hostname from a storage endpoint string.