fix(files): accept canonical storage:// paths in bare provider services (#3166)

Signed-off-by: dzc-bit <210145775+dzc-bit@users.noreply.github.com>
Co-authored-by: dzc-bit <210145775+dzc-bit@users.noreply.github.com>
main
dzc-bit 2026-09-25 18:48:04 +08:00 committed by GitHub
parent 6eb3cce164
commit e5a301ed3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 192 additions and 0 deletions

View File

@ -26,6 +26,18 @@ func NewBackendScopedFileService(backendID string, inner interfaces.FileService)
return &backendScopedFileService{backendID: backendID, inner: inner}
}
// storageBackendInnerPath strips the canonical storage://<backend-id>/ wrapper
// from path when present. Drivers wrapped in backendScopedFileService already
// unwrap through it; drivers reached bare (process-global env storage, legacy
// tenants) call this first so canonical paths produced by the resource catalog
// resolve to their provider path instead of failing scheme/bucket validation.
func storageBackendInnerPath(path string) string {
if _, inner, ok := types.ParseStorageBackendPath(path); ok {
return inner
}
return path
}
func (s *backendScopedFileService) unwrap(path string) (string, error) {
id, inner, ok := types.ParseStorageBackendPath(path)
if !ok {

View File

@ -0,0 +1,154 @@
package file
import (
"strings"
"testing"
)
// Canonical storage://<backend-id>/... paths are produced by the resource
// catalog and consumed by backendScopedFileService. Providers reached bare
// (process-global env storage, legacy tenants) must tolerate the same form,
// otherwise storageurl outbound rewrites fail with "invalid ... file path" and
// internal handles leak to IM channels (#3151).
func TestStorageBackendInnerPath(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{"canonical s3 path", "storage://be-1/s3://weknora/data/a.jpg", "s3://weknora/data/a.jpg"},
{"canonical minio path", "storage://be-1/minio://weknora/data/a.jpg", "minio://weknora/data/a.jpg"},
{"bare provider path unchanged", "s3://weknora/data/a.jpg", "s3://weknora/data/a.jpg"},
{"legacy URL unchanged", "https://bucket.cos.region.myqcloud.com/a.jpg", "https://bucket.cos.region.myqcloud.com/a.jpg"},
{"resource handle unchanged", "resource://abc123", "resource://abc123"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := storageBackendInnerPath(tc.in); got != tc.want {
t.Fatalf("storageBackendInnerPath(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestParseS3FilePathAcceptsCanonicalBackendPath(t *testing.T) {
svc := &s3FileService{bucketName: "weknora"}
got, err := svc.parseS3FilePath("storage://be-1/s3://weknora/data/10000/exports/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if got != "data/10000/exports/a.jpg" {
t.Fatalf("object key = %q, want %q", got, "data/10000/exports/a.jpg")
}
// Bare provider paths keep working.
if got, err := svc.parseS3FilePath("s3://weknora/data/a.jpg"); err != nil || got != "data/a.jpg" {
t.Fatalf("bare path: key=%q err=%v", got, err)
}
// A canonical path for another bucket must still be rejected.
if _, err := svc.parseS3FilePath("storage://be-1/s3://other-bucket/data/a.jpg"); err == nil {
t.Fatal("expected bucket mismatch error for foreign canonical path")
}
// resource:// handles are not provider paths and must stay rejected.
if _, err := svc.parseS3FilePath("resource://abc123"); err == nil {
t.Fatal("expected error for resource:// handle")
}
}
func TestParseMinioFilePathAcceptsCanonicalBackendPath(t *testing.T) {
svc := &minioFileService{bucketName: "weknora"}
got, err := svc.parseMinioFilePath("storage://be-1/minio://weknora/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if got != "data/a.jpg" {
t.Fatalf("object key = %q, want %q", got, "data/a.jpg")
}
if _, err := svc.parseMinioFilePath("minio://weknora/data/a.jpg"); err != nil {
t.Fatalf("bare path rejected: %v", err)
}
}
func TestParseOssFilePathAcceptsCanonicalBackendPath(t *testing.T) {
bucket, key, err := parseOssFilePath("storage://be-1/oss://weknora/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if bucket != "weknora" || key != "data/a.jpg" {
t.Fatalf("bucket=%q key=%q, want weknora/data/a.jpg", bucket, key)
}
if _, _, err := parseOssFilePath("oss://weknora/data/a.jpg"); err != nil {
t.Fatalf("bare path rejected: %v", err)
}
}
func TestParseKS3FilePathAcceptsCanonicalBackendPath(t *testing.T) {
bucket, key, err := parseKS3FilePath("storage://be-1/ks3://weknora/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if bucket != "weknora" || key != "data/a.jpg" {
t.Fatalf("bucket=%q key=%q, want weknora/data/a.jpg", bucket, key)
}
if _, _, err := parseKS3FilePath("ks3://weknora/data/a.jpg"); err != nil {
t.Fatalf("bare path rejected: %v", err)
}
}
func TestParseTOSFilePathAcceptsCanonicalBackendPath(t *testing.T) {
bucket, key, err := parseTOSFilePath("storage://be-1/tos://weknora/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if bucket != "weknora" || key != "data/a.jpg" {
t.Fatalf("bucket=%q key=%q, want weknora/data/a.jpg", bucket, key)
}
if _, _, err := parseTOSFilePath("tos://weknora/data/a.jpg"); err != nil {
t.Fatalf("bare path rejected: %v", err)
}
}
func TestParseObsFilePathAcceptsCanonicalBackendPath(t *testing.T) {
svc := &obsFileService{bucketName: "weknora"}
got, err := svc.parseObsFilePath("storage://be-1/obs://weknora/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if got != "data/a.jpg" {
t.Fatalf("object key = %q, want %q", got, "data/a.jpg")
}
// Without the fix the canonical form silently fell through to the raw
// passthrough branch and came back as the "object key" verbatim.
got, err = svc.parseObsFilePath("https://bucket.obs.region.mycloud.com/a.jpg")
if err != nil || !strings.HasPrefix(got, "https://") {
t.Fatalf("legacy URL passthrough broken: key=%q err=%v", got, err)
}
}
func TestParseCosObjectNameAcceptsCanonicalBackendPath(t *testing.T) {
svc := &cosFileService{}
got, err := svc.parseCosObjectName("storage://be-1/cos://bucket/ap-shanghai/data/a.jpg")
if err != nil {
t.Fatalf("canonical path rejected: %v", err)
}
if got != "data/a.jpg" {
t.Fatalf("object name = %q, want %q", got, "data/a.jpg")
}
// Cross-provider canonical paths must still be rejected, not silently
// passed to the legacy-URL branch.
if _, err := svc.parseCosObjectName("storage://be-1/s3://weknora/data/a.jpg"); err == nil {
t.Fatal("expected error for foreign provider path")
}
}

View File

@ -166,7 +166,11 @@ func (s *cosFileService) DeleteFile(ctx context.Context, filePath string) error
// parseCosObjectName extracts the object name from:
// - provider scheme: cos://{bucket}/{region}/{objectKey}
// - legacy URL: https://bucket.cos.region.myqcloud.com/{objectKey}
// Canonical storage://<backend-id>/cos://... paths are accepted too: the
// wrapper is stripped first, otherwise the whole canonical form silently falls
// through to the legacy-URL branch and is returned unchanged (#3151).
func (s *cosFileService) parseCosObjectName(filePath string) (string, error) {
filePath = storageBackendInnerPath(filePath)
for _, other := range []string{"local://", "minio://", "s3://", "tos://", "oss://", "ks3://", "obs://"} {
if strings.HasPrefix(filePath, other) {
return "", fmt.Errorf("cos file service cannot resolve %s path", strings.Split(other, "://")[0])

View File

@ -154,7 +154,11 @@ func joinKS3Key(parts ...string) string {
return strings.Join(filtered, "/")
}
// parseKS3FilePath extracts bucket and object key from: ks3://{bucket}/{objectKey}
// Canonical storage://<backend-id>/ks3://{bucket}/{objectKey} paths are
// accepted too (see storageBackendInnerPath, #3151).
func parseKS3FilePath(filePath string) (bucket, objectKey string, err error) {
filePath = storageBackendInnerPath(filePath)
if !strings.HasPrefix(filePath, ks3Scheme) {
return "", "", fmt.Errorf("invalid KS3 file path: %s", filePath)
}

View File

@ -99,9 +99,12 @@ func CheckMinioConnectivity(ctx context.Context, endpoint, accessKeyID, secretAc
}
// parseMinioFilePath extracts the object name from a provider scheme: minio://{bucket}/{objectKey}
// Canonical storage://<backend-id>/minio://{bucket}/{objectKey} paths are
// accepted too (see storageBackendInnerPath, #3151).
func (s *minioFileService) parseMinioFilePath(filePath string) (string, error) {
// Provider scheme format: minio://{bucket}/{objectKey}
const prefix = "minio://"
filePath = storageBackendInnerPath(filePath)
if !strings.HasPrefix(filePath, prefix) {
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
}

View File

@ -135,6 +135,10 @@ func (s *obsFileService) CheckConnectivity(ctx context.Context) error {
}
func (s *obsFileService) parseObsFilePath(filePath string) (string, error) {
// Canonical storage://<backend-id>/... paths must unwrap before prefix
// matching, otherwise the whole canonical form silently falls through to
// the raw passthrough below and is returned as the object key (#3151).
filePath = storageBackendInnerPath(filePath)
prefix := s.getPrifix()
if strings.HasPrefix(filePath, prefix) {

View File

@ -140,7 +140,10 @@ func CheckOssConnectivity(ctx context.Context, endpoint, region, accessKey, secr
}
// parseOssFilePath extracts bucket and object key from: oss://{bucket}/{objectKey}
// Canonical storage://<backend-id>/oss://{bucket}/{objectKey} paths are
// accepted too (see storageBackendInnerPath, #3151).
func parseOssFilePath(filePath string) (bucketName string, objectKey string, err error) {
filePath = storageBackendInnerPath(filePath)
if !strings.HasPrefix(filePath, ossScheme) {
return "", "", fmt.Errorf("invalid OSS file path: %s", filePath)
}

View File

@ -199,9 +199,13 @@ func CheckS3ConnectivityWithOptions(ctx context.Context, endpoint, accessKey, se
}
// parseS3FilePath extracts the object name from a provider scheme: s3://{bucket}/{objectKey}
// Canonical storage://<backend-id>/s3://{bucket}/{objectKey} paths are accepted
// too: the wrapper is stripped so services reached bare (global env storage,
// legacy tenants) can resolve catalog-produced paths (#3151).
func (s *s3FileService) parseS3FilePath(filePath string) (string, error) {
// Provider scheme format: s3://{bucket}/{objectKey}
const prefix = "s3://"
filePath = storageBackendInnerPath(filePath)
if !strings.HasPrefix(filePath, prefix) {
return "", fmt.Errorf("invalid S3 file path: %s", filePath)
}

View File

@ -155,7 +155,11 @@ func joinTOSObjectKey(parts ...string) string {
return strings.Join(filtered, "/")
}
// parseTOSFilePath extracts bucket and object key from: tos://{bucket}/{objectKey}
// Canonical storage://<backend-id>/tos://{bucket}/{objectKey} paths are
// accepted too (see storageBackendInnerPath, #3151).
func parseTOSFilePath(filePath string) (bucketName string, objectKey string, err error) {
filePath = storageBackendInnerPath(filePath)
if !strings.HasPrefix(filePath, tosScheme) {
return "", "", fmt.Errorf("invalid TOS file path: %s", filePath)
}