diff --git a/src/DurableTask.AzureStorage/DefaultStorageServiceClientProvider.cs b/src/DurableTask.AzureStorage/DefaultStorageServiceClientProvider.cs index 90a27295..10ac513b 100644 --- a/src/DurableTask.AzureStorage/DefaultStorageServiceClientProvider.cs +++ b/src/DurableTask.AzureStorage/DefaultStorageServiceClientProvider.cs @@ -16,17 +16,20 @@ namespace DurableTask.AzureStorage using System; using Azure.Core; - sealed class DefaultStorageServiceClientProvider : IStorageServiceClientProvider where TClientOptions : ClientOptions + sealed class DefaultStorageServiceClientProvider : IStorageServiceClientProvider, IStorageTokenCredentialProvider where TClientOptions : ClientOptions { readonly Func factory; readonly TClientOptions options; - public DefaultStorageServiceClientProvider(Func factory, TClientOptions options) + public DefaultStorageServiceClientProvider(Func factory, TClientOptions options, TokenCredential? tokenCredential = null) { + this.TokenCredential = tokenCredential; this.factory = factory ?? throw new ArgumentNullException(nameof(factory)); this.options = options ?? throw new ArgumentNullException(nameof(options)); } + public TokenCredential? TokenCredential { get; } + public TClient CreateClient(TClientOptions options) { return this.factory(options); diff --git a/src/DurableTask.AzureStorage/IStorageTokenCredentialProvider.cs b/src/DurableTask.AzureStorage/IStorageTokenCredentialProvider.cs new file mode 100644 index 00000000..5b4107c3 --- /dev/null +++ b/src/DurableTask.AzureStorage/IStorageTokenCredentialProvider.cs @@ -0,0 +1,30 @@ +// ---------------------------------------------------------------------------------- +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- +#nullable enable +namespace DurableTask.AzureStorage +{ + using Azure.Core; + + /// + /// Optional capability of a storage client provider used to obtain Table user delegation keys. + /// + /// + /// Entra-authenticated Table providers must expose the same credential they use to create the service client. + /// Other providers do not need to implement this interface unless they support Entra authentication. + /// + public interface IStorageTokenCredentialProvider + { + /// Gets the Entra credential used by the provider, or null for non-Entra authentication. + TokenCredential? TokenCredential { get; } + } +} diff --git a/src/DurableTask.AzureStorage/StorageServiceClientProvider.cs b/src/DurableTask.AzureStorage/StorageServiceClientProvider.cs index aa6b08fb..9c20fe70 100644 --- a/src/DurableTask.AzureStorage/StorageServiceClientProvider.cs +++ b/src/DurableTask.AzureStorage/StorageServiceClientProvider.cs @@ -93,7 +93,7 @@ public static IStorageServiceClientProvider( o => new BlobServiceClient(serviceUri, tokenCredential, o), - options ?? new BlobClientOptions()); + options ?? new BlobClientOptions(), tokenCredential); } #endregion @@ -165,7 +165,7 @@ public static IStorageServiceClientProvider( o => new QueueServiceClient(serviceUri, tokenCredential, o), - options ?? new QueueClientOptions()); + options ?? new QueueClientOptions(), tokenCredential); } #endregion @@ -237,7 +237,7 @@ public static IStorageServiceClientProvider( o => new TableServiceClient(serviceUri, tokenCredential, o), - options ?? new TableClientOptions()); + options ?? new TableClientOptions(), tokenCredential); } #endregion diff --git a/Test/DurableTask.AzureStorage.Tests/AzureStorageMigrationTests.cs b/test/DurableTask.AzureStorage.Tests/AzureStorageMigrationTests.cs similarity index 100% rename from Test/DurableTask.AzureStorage.Tests/AzureStorageMigrationTests.cs rename to test/DurableTask.AzureStorage.Tests/AzureStorageMigrationTests.cs diff --git a/test/DurableTask.AzureStorage.Tests/StorageServiceClientProviderTests.cs b/test/DurableTask.AzureStorage.Tests/StorageServiceClientProviderTests.cs new file mode 100644 index 00000000..6d138478 --- /dev/null +++ b/test/DurableTask.AzureStorage.Tests/StorageServiceClientProviderTests.cs @@ -0,0 +1,132 @@ +// ---------------------------------------------------------------------------------- +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- +#nullable enable +namespace DurableTask.AzureStorage.Tests +{ + using System; + using System.Threading; + using System.Threading.Tasks; + using Azure.Core; + using Azure.Data.Tables; + using Azure.Storage.Blobs; + using Azure.Storage.Queues; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class StorageServiceClientProviderTests + { + [DataTestMethod] + [DataRow("blob", false)] + [DataRow("blob", true)] + [DataRow("queue", false)] + [DataRow("queue", true)] + [DataRow("table", false)] + [DataRow("table", true)] + public void TokenProvider_ExposesTheSuppliedCredential(string service, bool useAccountName) + { + // 1. Create a provider with an explicit credential and options. + TestCredential credential = new TestCredential(); + Uri endpoint = new Uri($"https://account.{service}.core.windows.net"); + switch (service) + { + case "blob": + BlobClientOptions blobOptions = new BlobClientOptions(); + IStorageServiceClientProvider blobs = useAccountName + ? StorageServiceClientProvider.ForBlob("account", credential, blobOptions) + : StorageServiceClientProvider.ForBlob(endpoint, credential, blobOptions); + AssertProvider(blobs, blobOptions, credential); + break; + case "queue": + QueueClientOptions queueOptions = new QueueClientOptions(); + IStorageServiceClientProvider queues = useAccountName + ? StorageServiceClientProvider.ForQueue("account", credential, queueOptions) + : StorageServiceClientProvider.ForQueue(endpoint, credential, queueOptions); + AssertProvider(queues, queueOptions, credential); + break; + case "table": + TableClientOptions tableOptions = new TableClientOptions(); + IStorageServiceClientProvider tables = useAccountName + ? StorageServiceClientProvider.ForTable("account", credential, tableOptions) + : StorageServiceClientProvider.ForTable(endpoint, credential, tableOptions); + AssertProvider(tables, tableOptions, credential); + break; + default: + throw new ArgumentOutOfRangeException(nameof(service)); + } + } + + [DataTestMethod] + [DataRow("blob")] + [DataRow("queue")] + [DataRow("table")] + public void ConnectionStringProvider_HasNoTokenCredential(string service) + { + // 1. Keep connection-string clients on their existing authentication path. + const string connectionString = "UseDevelopmentStorage=true"; + switch (service) + { + case "blob": + BlobClientOptions blobOptions = new BlobClientOptions(); + AssertProvider(StorageServiceClientProvider.ForBlob(connectionString, blobOptions), blobOptions, null); + break; + case "queue": + QueueClientOptions queueOptions = new QueueClientOptions(); + AssertProvider(StorageServiceClientProvider.ForQueue(connectionString, queueOptions), queueOptions, null); + break; + case "table": + TableClientOptions tableOptions = new TableClientOptions(); + AssertProvider(StorageServiceClientProvider.ForTable(connectionString, tableOptions), tableOptions, null); + break; + default: + throw new ArgumentOutOfRangeException(nameof(service)); + } + } + + [TestMethod] + public void CustomProvider_DoesNotRequireTheOptionalInterface() + { + // 1. Implement only the existing client-provider contract. + IStorageServiceClientProvider provider = new CustomProvider(); + + // 2. Client creation still works without exposing a credential. + Assert.IsFalse(provider is IStorageTokenCredentialProvider); + Assert.IsNotNull(provider.CreateClient(provider.CreateOptions())); + } + + static void AssertProvider( + IStorageServiceClientProvider provider, TOptions options, TokenCredential? credential) + where TOptions : ClientOptions + { + // 2. Keep the same credential and options, and continue creating clients. + Assert.IsInstanceOfType(provider); + Assert.AreSame(credential, ((IStorageTokenCredentialProvider)provider).TokenCredential); + Assert.AreSame(options, provider.CreateOptions()); + Assert.IsNotNull(provider.CreateClient(options)); + } + + sealed class TestCredential : TokenCredential + { + public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) => + throw new InvalidOperationException("Creating a provider must not request a token."); + + public override ValueTask GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) => + throw new InvalidOperationException("Creating a provider must not request a token."); + } + + sealed class CustomProvider : IStorageServiceClientProvider + { + public object CreateClient(TableClientOptions options) => new object(); + public TableClientOptions CreateOptions() => new TableClientOptions(); + } + } +}