From 95d0131017bae18eaba13cb020e75e0e1bea0553 Mon Sep 17 00:00:00 2001 From: Samir Solanki Date: Thu, 18 Sep 2025 06:05:35 +0000 Subject: [PATCH] List only containers starting with prefix --- .../Tracking/BlobStorageClient.cs | 10 +++++-- .../BlobStorageClientTest.cs | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/DurableTask.ServiceBus/Tracking/BlobStorageClient.cs b/src/DurableTask.ServiceBus/Tracking/BlobStorageClient.cs index 8a07aa75d..a1d32d363 100644 --- a/src/DurableTask.ServiceBus/Tracking/BlobStorageClient.cs +++ b/src/DurableTask.ServiceBus/Tracking/BlobStorageClient.cs @@ -136,10 +136,16 @@ private async Task GetCloudBlobClientAsync(string containerNameSuffi public async Task> ListContainersAsync() { List results = new List(); - var response = this.blobServiceClient.GetBlobContainersAsync(); + // Use the service client's prefix parameter to only enumerate containers that start with the + // configured containerNamePrefix. Also apply a defensive in-memory filter in case of any + // unexpected differences in casing or service behavior. + var response = this.blobServiceClient.GetBlobContainersAsync(prefix: this.containerNamePrefix); await foreach (var container in response) { - results.Add(container); + if (container.Name != null && container.Name.StartsWith(this.containerNamePrefix, StringComparison.OrdinalIgnoreCase)) + { + results.Add(container); + } } return results; } diff --git a/test/DurableTask.ServiceBus.Tests/BlobStorageClientTest.cs b/test/DurableTask.ServiceBus.Tests/BlobStorageClientTest.cs index 09afd551c..ebc3c2350 100644 --- a/test/DurableTask.ServiceBus.Tests/BlobStorageClientTest.cs +++ b/test/DurableTask.ServiceBus.Tests/BlobStorageClientTest.cs @@ -102,5 +102,33 @@ public async Task TestDeleteContainers() containers = (await this.blobStorageClient.ListContainersAsync()).ToList(); Assert.AreEqual(0, containers.Count); } + + [TestMethod] + public async Task TestListContainersOnlyWithPrefix() + { + // Create a container that does NOT use the current client's prefix + string storageConnectionString = TestHelpers.GetTestSetting("StorageConnectionString"); + var serviceClient = new BlobServiceClient(storageConnectionString); + + string otherHub = "otherhubprefix" + Guid.NewGuid().ToString("N").Substring(0, 8); + string otherPrefix = BlobStorageClientHelper.BuildContainerNamePrefix(otherHub); + string otherContainerName = BlobStorageClientHelper.BuildContainerName(otherPrefix, "message-20100101"); + + var otherContainer = serviceClient.GetBlobContainerClient(otherContainerName); + await otherContainer.CreateIfNotExistsAsync(); + + try + { + // Ensure the externally-created container is not returned by the client's listing + List containers = (await this.blobStorageClient.ListContainersAsync()).ToList(); + Assert.IsFalse(containers.Any(c => string.Equals(c.Name, otherContainerName, StringComparison.OrdinalIgnoreCase)), + "ListContainersAsync returned a container that does not match the client's prefix."); + } + finally + { + // Clean up the external container explicitly + await otherContainer.DeleteIfExistsAsync(); + } + } } }