From 875bc150dab94e08c62ecfa51d3e81e69dc36d43 Mon Sep 17 00:00:00 2001 From: Flavian Missi Date: Thu, 8 Feb 2024 12:44:49 +0100 Subject: [PATCH] registry/storage/driver/azure: fix storage path to use leading slash this keeps backwards compatibility with pre-v3 registries. --- registry/storage/driver/azure/azure.go | 13 ++--- registry/storage/driver/azure/azure_test.go | 58 +++++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/registry/storage/driver/azure/azure.go b/registry/storage/driver/azure/azure.go index 601bf528323..a3bf3e5e0c5 100644 --- a/registry/storage/driver/azure/azure.go +++ b/registry/storage/driver/azure/azure.go @@ -410,14 +410,11 @@ func (d *driver) listBlobs(ctx context.Context, virtPath string) ([]string, erro // This is to cover for the cases when the rootDirectory of the driver is either "" or "/". // In those cases, there is no root prefix to replace and we must actually add a "/" to all // results in order to keep them as valid paths as recognized by storagedriver.PathRegexp - prefix := "" - if blobPrefix == "" { - prefix = "/" - } + prefix := "/" out := []string{} - listPrefix := d.blobName(virtPath) + pager := d.client.NewListBlobsFlatPager(&container.ListBlobsFlatOptions{ Prefix: &listPrefix, }) @@ -427,11 +424,13 @@ func (d *driver) listBlobs(ctx context.Context, virtPath string) ([]string, erro return nil, err } for _, blob := range resp.Segment.BlobItems { + if blob.Name == nil { return nil, fmt.Errorf("required blob property Name is missing while listing blobs under: %s", listPrefix) } name := *blob.Name out = append(out, strings.Replace(name, blobPrefix, prefix, 1)) + } } @@ -446,7 +445,9 @@ func (d *driver) blobName(path string) string { return path } - return strings.TrimLeft(strings.TrimRight(d.rootDirectory, "/")+path, "/") + trimmedRoot := strings.TrimRight(d.rootDirectory, "/") + trimmedPath := strings.TrimLeft(path, "/") + return trimmedRoot + "/" + trimmedPath } func is404(err error) bool { diff --git a/registry/storage/driver/azure/azure_test.go b/registry/storage/driver/azure/azure_test.go index 5fbd01ade4b..c9a4b32fd11 100644 --- a/registry/storage/driver/azure/azure_test.go +++ b/registry/storage/driver/azure/azure_test.go @@ -91,6 +91,64 @@ func randStringRunes(n int) string { return string(b) } +func writeBlob(ctx context.Context, driver storagedriver.StorageDriver, contents, path string, append_ bool) error { + writer, err := driver.Writer(ctx, path, append_) + if err != nil { + return fmt.Errorf("unexpected error from driver.Writer: %v", err) + } + _, err = writer.Write([]byte(contents)) + if err != nil { + return fmt.Errorf("writer.Write: unexpected error: %v", err) + } + + err = writer.Commit() + if err != nil { + return fmt.Errorf("writer.Commit: unexpected error: %v", err) + } + err = writer.Close() + if err != nil { + return fmt.Errorf("writer.Close: unexpected error: %v", err) + } + return nil +} + +func TestListBlobs(t *testing.T) { + driver, err := azureDriverConstructor() + if err != nil { + t.Fatalf("unexpected error creating azure driver: %v", err) + } + + contents := randStringRunes(4 * 1024 * 1024) + path := "/file" + ctx := context.Background() + + defer driver.Delete(ctx, path) + if err := writeBlob(ctx, driver, contents, path, false); err != nil { + t.Fatal(err) + } + + blobNames, err := driver.List(ctx, "/") + if err != nil { + t.Fatalf("driver.List: unexpected error: %v", err) + } + + if len(blobNames) == 0 { + t.Fatalf("blob %q was not listed! driver.List returned 0 blobs!", path) + } + + found := false + for _, blobName := range blobNames { + if blobName == path { + found = true + break + } + fmt.Println("TestListBlobs blobName", blobName, "did not match path", path) + } + if !found { + t.Fatalf("blob %q was not listed!", path) + } +} + func TestCommitAfterMove(t *testing.T) { driver, err := azureDriverConstructor() if err != nil {