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:
@@ -131,12 +131,34 @@ func ValidateKBScopedStoragePath(filePath string, tenantID uint64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// storageBackendScheme wraps a provider:// path with the concrete instance id:
|
||||
// storage://<backendID>/<provider>://... It is duplicated here (rather than
|
||||
// reusing types.ParseStorageBackendPath) because internal/types already imports
|
||||
// internal/utils, so a reverse import would create a cycle.
|
||||
const storageBackendScheme = "storage://"
|
||||
|
||||
// unwrapStorageBackendPath strips a leading storage://<backendID>/ wrapper and
|
||||
// returns the inner provider:// path. Non-wrapped paths are returned unchanged.
|
||||
// This keeps tenant/exports parsing anchored on the provider path instead of
|
||||
// relying on the backend id happening not to look like a tenant segment.
|
||||
func unwrapStorageBackendPath(filePath string) string {
|
||||
if !strings.HasPrefix(filePath, storageBackendScheme) {
|
||||
return filePath
|
||||
}
|
||||
rest := strings.TrimPrefix(filePath, storageBackendScheme)
|
||||
parts := strings.SplitN(rest, "/", 2)
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||
return filePath
|
||||
}
|
||||
return parts[1]
|
||||
}
|
||||
|
||||
// storagePathHasExportsScope reports whether tenantID appears next to an
|
||||
// exports segment in either canonical layout:
|
||||
// - {tenant}/exports/... (local, minio, s3, most cloud backends)
|
||||
// - exports/{tenant}/... (OSS temp-bucket layout)
|
||||
func storagePathHasExportsScope(filePath string, tenantID uint64) bool {
|
||||
_, rest, ok := strings.Cut(filePath, "://")
|
||||
_, rest, ok := strings.Cut(unwrapStorageBackendPath(filePath), "://")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
@@ -165,6 +187,9 @@ func storagePathHasExportsScope(filePath string, tenantID uint64) bool {
|
||||
// Callers that have an authoritative resource-owner tenant ID available
|
||||
// should pass it directly to SignFileURL instead of relying on this parser.
|
||||
func ParseTenantIDFromStoragePath(filePath string) uint64 {
|
||||
// Unwrap storage://<backendID>/ so the tenant scan is anchored on the inner
|
||||
// provider path, not the (opaque) backend id.
|
||||
filePath = unwrapStorageBackendPath(filePath)
|
||||
// Strip scheme: "local://1/abc/img.png" → "1/abc/img.png"
|
||||
_, rest, ok := strings.Cut(filePath, "://")
|
||||
if !ok {
|
||||
|
||||
@@ -99,9 +99,14 @@ func TestValidateKBScopedStoragePath(t *testing.T) {
|
||||
assert.NoError(t, ValidateKBScopedStoragePath("minio://bucket/10008/exports/uuid.png", tenantID))
|
||||
assert.NoError(t, ValidateKBScopedStoragePath("oss://bucket/exports/10008/uuid.png", tenantID))
|
||||
|
||||
// storage://<backendID>/ wrapped paths must resolve to the same tenant/exports scope.
|
||||
assert.NoError(t, ValidateKBScopedStoragePath("storage://backend-a/local://10008/exports/img.jpg", tenantID))
|
||||
assert.NoError(t, ValidateKBScopedStoragePath("storage://backend-a/cos://bucket/ap-test/10008/exports/a.png", tenantID))
|
||||
|
||||
assert.Error(t, ValidateKBScopedStoragePath("local://10008/knowledge-id/123.pdf", tenantID))
|
||||
assert.Error(t, ValidateKBScopedStoragePath("local://9999/exports/img.jpg", tenantID))
|
||||
assert.Error(t, ValidateKBScopedStoragePath("local://10008/other/img.jpg", tenantID))
|
||||
assert.Error(t, ValidateKBScopedStoragePath("storage://backend-a/local://9999/exports/img.jpg", tenantID))
|
||||
}
|
||||
|
||||
func TestParseTenantIDFromStoragePath(t *testing.T) {
|
||||
@@ -119,6 +124,8 @@ func TestParseTenantIDFromStoragePath(t *testing.T) {
|
||||
{"https://example.com/img.png", 0},
|
||||
{"invalid", 0},
|
||||
{"local://exports/file.csv", 0},
|
||||
{"storage://backend-a/local://1/abc/img.png", 1},
|
||||
{"storage://backend-a/cos://bucket/region/prefix/42/abc/img.png", 42},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
// SanitizeStorageConnectivityError converts a raw storage connectivity error
|
||||
// into a safe, user-facing message. It deliberately avoids echoing the raw
|
||||
// driver/network error so responses never leak internal hostnames, IPs, ports
|
||||
// or TLS/certificate details. Callers that need the full error must log it
|
||||
// server-side instead of returning it to the client.
|
||||
func SanitizeStorageConnectivityError(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
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 "连接失败,请检查配置参数是否正确"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user