Skip to content

Commit aa83cf3

Browse files
authored
[DurableTask.ServiceBus] List only containers starting with prefix (#1240)
1 parent 8480d0a commit aa83cf3

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/DurableTask.ServiceBus/Tracking/BlobStorageClient.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ private async Task<BlobClient> GetCloudBlobClientAsync(string containerNameSuffi
136136
public async Task<IEnumerable<BlobContainerItem>> ListContainersAsync()
137137
{
138138
List<BlobContainerItem> results = new List<BlobContainerItem>();
139-
var response = this.blobServiceClient.GetBlobContainersAsync();
139+
// Use the service client's prefix parameter to only enumerate containers that start with the
140+
// configured containerNamePrefix. Also apply a defensive in-memory filter in case of any
141+
// unexpected differences in casing or service behavior.
142+
var response = this.blobServiceClient.GetBlobContainersAsync(prefix: this.containerNamePrefix);
140143
await foreach (var container in response)
141144
{
142-
results.Add(container);
145+
if (container.Name != null && container.Name.StartsWith(this.containerNamePrefix, StringComparison.OrdinalIgnoreCase))
146+
{
147+
results.Add(container);
148+
}
143149
}
144150
return results;
145151
}

test/DurableTask.ServiceBus.Tests/BlobStorageClientTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,33 @@ public async Task TestDeleteContainers()
102102
containers = (await this.blobStorageClient.ListContainersAsync()).ToList();
103103
Assert.AreEqual(0, containers.Count);
104104
}
105+
106+
[TestMethod]
107+
public async Task TestListContainersOnlyWithPrefix()
108+
{
109+
// Create a container that does NOT use the current client's prefix
110+
string storageConnectionString = TestHelpers.GetTestSetting("StorageConnectionString");
111+
var serviceClient = new BlobServiceClient(storageConnectionString);
112+
113+
string otherHub = "otherhubprefix" + Guid.NewGuid().ToString("N").Substring(0, 8);
114+
string otherPrefix = BlobStorageClientHelper.BuildContainerNamePrefix(otherHub);
115+
string otherContainerName = BlobStorageClientHelper.BuildContainerName(otherPrefix, "message-20100101");
116+
117+
var otherContainer = serviceClient.GetBlobContainerClient(otherContainerName);
118+
await otherContainer.CreateIfNotExistsAsync();
119+
120+
try
121+
{
122+
// Ensure the externally-created container is not returned by the client's listing
123+
List<BlobContainerItem> containers = (await this.blobStorageClient.ListContainersAsync()).ToList();
124+
Assert.IsFalse(containers.Any(c => string.Equals(c.Name, otherContainerName, StringComparison.OrdinalIgnoreCase)),
125+
"ListContainersAsync returned a container that does not match the client's prefix.");
126+
}
127+
finally
128+
{
129+
// Clean up the external container explicitly
130+
await otherContainer.DeleteIfExistsAsync();
131+
}
132+
}
105133
}
106134
}

0 commit comments

Comments
 (0)