diff --git a/src/DurableTask.AzureStorage/Storage/TableUserDelegationKey.cs b/src/DurableTask.AzureStorage/Storage/TableUserDelegationKey.cs new file mode 100644 index 000000000..d74439437 --- /dev/null +++ b/src/DurableTask.AzureStorage/Storage/TableUserDelegationKey.cs @@ -0,0 +1,184 @@ +// ---------------------------------------------------------------------------------- +// 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.Storage +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.IO; + using System.Linq; + using System.Security.Cryptography; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Xml; + using System.Xml.Linq; + using Azure; + using Azure.Core; + using Azure.Core.Pipeline; + using Azure.Data.Tables; + + // Azure.Data.Tables does not yet expose Get User Delegation Key or its SAS signer. + // Wire format: https://learn.microsoft.com/rest/api/storageservices/create-user-delegation-sas + sealed class TableUserDelegationKey + { + internal const string ServiceVersion = "2025-07-05"; + const string SignedKeyObjectIdParameter = "skoid"; + const string SignedKeyTenantIdParameter = "sktid"; + const string SignedKeyStartParameter = "skt"; + const string SignedKeyExpiryParameter = "ske"; + const string SignedKeyServiceParameter = "sks"; + const string SignedKeyVersionParameter = "skv"; + const string TableService = "t"; + readonly Dictionary fields; + readonly byte[] key; + + TableUserDelegationKey(XElement xml) + { + string Read(string name) => (string?)xml.Element(name) ?? throw new InvalidOperationException("Missing user delegation key field: " + name); + + this.fields = new Dictionary + { + [SignedKeyObjectIdParameter] = Read("SignedOid"), + [SignedKeyTenantIdParameter] = Read("SignedTid"), + [SignedKeyStartParameter] = Read("SignedStart"), + [SignedKeyExpiryParameter] = Read("SignedExpiry"), + [SignedKeyServiceParameter] = Read("SignedService"), + [SignedKeyVersionParameter] = Read("SignedVersion"), + }; + if (this.fields[SignedKeyServiceParameter] != TableService) + { + throw new InvalidOperationException("Expected a Table user delegation key."); + } + this.key = Convert.FromBase64String(Read("Value")); + this.ExpiresOn = DateTimeOffset.Parse(this.fields[SignedKeyExpiryParameter], CultureInfo.InvariantCulture); + } + + public DateTimeOffset ExpiresOn { get; } + + // Request a Table delegation key using the provider's Entra credential. + public static async Task GetAsync(Uri endpoint, TokenCredential credential, TableClientOptions options, DateTimeOffset serverTime, CancellationToken cancellationToken) + { + TableAudience audience = options.Audience ?? TableAudience.AzurePublicCloud; + string resource = audience == TableAudience.AzurePublicCloud ? "https://storage.azure.com" + : audience == TableAudience.AzureGovernment ? "https://storage.azure.us" + : audience == TableAudience.AzureChina ? "https://storage.azure.cn" + : audience.ToString().TrimEnd('/'); + string scope = resource.EndsWith("/.default", StringComparison.OrdinalIgnoreCase) + ? resource : resource + "/.default"; + // Workload options may contain the SAS fence policy. Key acquisition has its own authenticated + // pipeline and cannot depend on an existing, unexpired workload SAS. + var keyOptions = new TableClientOptions { Transport = options.Transport }; + keyOptions.Retry.Mode = options.Retry.Mode; + keyOptions.Retry.Delay = options.Retry.Delay; + keyOptions.Retry.MaxDelay = options.Retry.MaxDelay; + keyOptions.Retry.MaxRetries = options.Retry.MaxRetries; + keyOptions.Retry.NetworkTimeout = options.Retry.NetworkTimeout; + HttpPipeline pipeline = HttpPipelineBuilder.Build(keyOptions, new BearerTokenAuthenticationPolicy(credential, scope)); + using HttpMessage message = pipeline.CreateMessage(); + message.Request.Method = RequestMethod.Post; + message.Request.Uri.Reset(new Uri(endpoint, "?restype=service&comp=userdelegationkey")); + message.Request.Headers.Add("x-ms-version", ServiceVersion); + message.Request.Headers.Add("Content-Type", "application/xml"); + var xml = new XElement("KeyInfo", + new XElement("Start", FormatTime(serverTime.AddMinutes(-15))), + new XElement("Expiry", FormatTime(serverTime.AddHours(24)))); + message.Request.Content = RequestContent.Create(Encoding.UTF8.GetBytes(xml.ToString(SaveOptions.DisableFormatting))); + await pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); + if (message.Response.Status != 200) + { + // Parse Storage XML errors; keep the SDK's parser for other responses. + RequestFailedDetailsParser? parser = message.Response.Content.ToString().TrimStart().StartsWith("<", StringComparison.Ordinal) + ? XmlErrorDetailsParser.Instance : null; + throw new RequestFailedException(message.Response, innerException: null, detailsParser: parser); + } + using var reader = new StreamReader(message.Response.ContentStream!); + return new TableUserDelegationKey(XElement.Parse(await reader.ReadToEndAsync().ConfigureAwait(false))); + } + + // Build a signed SAS query string granting read, add, update, and delete access to one table. + public string Sign(string accountName, string tableName, DateTimeOffset expiresOn) + { + string start = this.fields[SignedKeyStartParameter]; + string expiry = FormatTime(expiresOn); + string permissions = "raud"; + // Empty delegated tenant/user, IP, and partition/row ranges are required positions in the signature. + string toSign = string.Join( + "\n", + permissions, + start, + expiry, + "/table/" + accountName + "/" + tableName.ToLowerInvariant(), + this.fields[SignedKeyObjectIdParameter], + this.fields[SignedKeyTenantIdParameter], + this.fields[SignedKeyStartParameter], + this.fields[SignedKeyExpiryParameter], + this.fields[SignedKeyServiceParameter], + this.fields[SignedKeyVersionParameter], + "", + "", + "", + "https", + ServiceVersion, + "", + "", + "", + ""); + using var hmac = new HMACSHA256(this.key); + var query = new Dictionary(this.fields) + { + ["sp"] = permissions, + ["st"] = start, + ["se"] = expiry, + ["spr"] = "https", + ["sv"] = ServiceVersion, + ["tn"] = tableName, + ["sig"] = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(toSign))), + }; + return string.Join("&", query.Select(pair => pair.Key + "=" + Uri.EscapeDataString(pair.Value))); + } + + sealed class XmlErrorDetailsParser : RequestFailedDetailsParser + { + internal static readonly XmlErrorDetailsParser Instance = new XmlErrorDetailsParser(); + + public override bool TryParse(Response response, out ResponseError? error, out IDictionary? data) + { + error = null; + data = null; + try + { + XElement body = XElement.Parse(response.Content.ToString()); + string? code = (string?)body.Element("Code"); + if (body.Name != "Error" || string.IsNullOrEmpty(code)) + { + return false; + } + error = new ResponseError(code, (string?)body.Element("Message") ?? "Service request failed."); + return true; + } + catch (XmlException) + { + // Keep the original HTTP failure when the body is malformed. + return false; + } + } + } + + static string FormatTime(DateTimeOffset value) + { + return value.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture); + } + } +} diff --git a/test/DurableTask.AzureStorage.Tests/TableUserDelegationKeyTests.cs b/test/DurableTask.AzureStorage.Tests/TableUserDelegationKeyTests.cs new file mode 100644 index 000000000..bb03ee421 --- /dev/null +++ b/test/DurableTask.AzureStorage.Tests/TableUserDelegationKeyTests.cs @@ -0,0 +1,336 @@ +// ---------------------------------------------------------------------------------- +// 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.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Xml.Linq; + using Azure; + using Azure.Core; + using Azure.Core.Pipeline; + using Azure.Data.Tables; + using DurableTask.AzureStorage.Storage; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class TableUserDelegationKeyTests + { + const string KeyValue = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8="; + const string KeyStart = "2030-01-01T00:00:00Z"; + const string KeyExpiry = "2030-01-02T00:15:00Z"; + const string ObjectId = "11111111-1111-1111-1111-111111111111"; + const string TenantId = "22222222-2222-2222-2222-222222222222"; + static readonly Uri Endpoint = new Uri("https://account.table.core.windows.net/"); + static readonly DateTimeOffset ServerTime = new DateTimeOffset(2030, 1, 1, 8, 15, 0, TimeSpan.FromHours(8)); + + [TestMethod] + public async Task Get_UsesStorageTimeAndAnIndependentAuthenticatedPipeline() + { + // 1. Inspect the authenticated request without contacting Storage. + int requests = 0; + using HttpClient http = new HttpClient(new TestHandler(async (request, token) => + { + requests++; + Assert.AreEqual(HttpMethod.Post, request.Method); + Assert.AreEqual(new Uri(Endpoint, "?restype=service&comp=userdelegationkey"), request.RequestUri); + Assert.AreEqual("Bearer", request.Headers.Authorization!.Scheme); + Assert.AreEqual("unit-test-token", request.Headers.Authorization.Parameter); + Assert.AreEqual("2025-07-05", request.Headers.GetValues("x-ms-version").Single()); + Assert.AreEqual("application/xml", request.Content!.Headers.ContentType!.MediaType); + XElement body = XElement.Parse(await request.Content.ReadAsStringAsync()); + Assert.AreEqual("KeyInfo", body.Name.LocalName); + Assert.AreEqual(KeyStart, (string?)body.Element("Start")); + Assert.AreEqual(KeyExpiry, (string?)body.Element("Expiry")); + return KeyResponse(); + })); + TableClientOptions options = Options(http); + options.AddPolicy(new RejectWorkloadRequestsPolicy(), HttpPipelinePosition.PerCall); + TestCredential credential = new TestCredential(); + + // 2. Key acquisition must not run policies that require a workload SAS. + TableUserDelegationKey key = await TableUserDelegationKey.GetAsync(Endpoint, credential, options, ServerTime, default); + Assert.AreEqual(new DateTimeOffset(2030, 1, 2, 0, 15, 0, TimeSpan.Zero), key.ExpiresOn); + Assert.AreEqual(1, requests); + CollectionAssert.AreEqual(new[] { "https://storage.azure.com/.default" }, credential.Scopes); + } + + [DataTestMethod] + [DataRow(null, "https://storage.azure.com/.default")] + [DataRow("https://storage.azure.com/", "https://storage.azure.com/.default")] + [DataRow("https://storage.azure.us/", "https://storage.azure.us/.default")] + [DataRow("https://storage.azure.cn/", "https://storage.azure.cn/.default")] + [DataRow("https://custom.storage.example/", "https://custom.storage.example/.default")] + [DataRow("https://custom.storage.example/.default", "https://custom.storage.example/.default")] + public async Task Get_UsesTheConfiguredAudience(string? audience, string expectedScope) + { + // 1. Supply a cloud or custom token audience. + using HttpClient http = new HttpClient(new TestHandler((request, token) => Task.FromResult(KeyResponse()))); + TableClientOptions options = Options(http); + if (audience != null) + { + options.Audience = new TableAudience(audience); + } + TestCredential credential = new TestCredential(); + + // 2. Request the token for that audience exactly once. + await TableUserDelegationKey.GetAsync(Endpoint, credential, options, ServerTime, default); + CollectionAssert.AreEqual(new[] { expectedScope }, credential.Scopes); + Assert.AreEqual(1, credential.Requests); + } + + [DataTestMethod] + [DataRow(503, 0, 1, false)] + [DataRow(503, 1, 2, true)] + [DataRow(403, 1, 1, false)] + public async Task Get_HonorsRetryOptionsAndPropagatesFailures(int status, int maxRetries, int expectedRequests, bool succeeds) + { + // 1. Fail the first request and allow one later request to succeed. + int requests = 0; + using HttpClient http = new HttpClient(new TestHandler((request, token) => + { + requests++; + HttpResponseMessage response = requests == 1 + ? ErrorResponse(status, "TestFailureStorage refused the test request.") + : KeyResponse(); + return Task.FromResult(response); + })); + TableClientOptions options = Options(http); + options.Retry.Mode = RetryMode.Fixed; + options.Retry.Delay = TimeSpan.Zero; + options.Retry.MaxDelay = TimeSpan.Zero; + options.Retry.MaxRetries = maxRetries; + + // 2. Retry only when both the response and the configured policy allow it. + if (succeeds) + { + await TableUserDelegationKey.GetAsync(Endpoint, new TestCredential(), options, ServerTime, default); + } + else + { + RequestFailedException failure = await Assert.ThrowsExceptionAsync(() => + TableUserDelegationKey.GetAsync(Endpoint, new TestCredential(), options, ServerTime, default)); + Assert.AreEqual(status, failure.Status); + Assert.AreEqual("TestFailure", failure.ErrorCode); + StringAssert.Contains(failure.Message, "Storage refused the test request."); + StringAssert.Contains(failure.Message, "Status: " + status); + StringAssert.Contains(failure.Message, "TestFailure"); + Response response = failure.GetRawResponse()!; + Assert.IsNotNull(response); + Assert.IsTrue(response.Headers.TryGetValue("x-ms-request-id", out string? requestId)); + Assert.AreEqual("test-request-id", requestId); + } + Assert.AreEqual(expectedRequests, requests); + } + + [DataTestMethod] + [DataRow("TestFailure", null)] + [DataRow("No code was returned.", null)] + [DataRow("Service unavailable", null)] + [DataRow("", null)] + [DataRow("{\"error\":{\"code\":\"JsonFailure\",\"message\":\"Storage refused the JSON request.\"}}", "JsonFailure")] + public async Task Get_PreservesFailuresForOtherResponseBodies(string body, string? expectedCode) + { + // 1. Return a body that is not a complete XML error. + using HttpClient http = new HttpClient(new TestHandler((request, token) => + Task.FromResult(ErrorResponse(403, body)))); + + // 2. Keep the HTTP failure and any error code the SDK can parse. + RequestFailedException failure = await Assert.ThrowsExceptionAsync(() => + TableUserDelegationKey.GetAsync(Endpoint, new TestCredential(), Options(http), ServerTime, default)); + Assert.AreEqual(403, failure.Status); + Assert.AreEqual(expectedCode, failure.ErrorCode); + if (body.Length != 0) + { + StringAssert.Contains(failure.Message, body); + } + Response response = failure.GetRawResponse()!; + Assert.IsNotNull(response); + Assert.AreEqual(403, response.Status); + Assert.IsTrue(response.Headers.TryGetValue("x-ms-request-id", out string? requestId)); + Assert.AreEqual("test-request-id", requestId); + } + + [TestMethod] + [Timeout(10000)] + public async Task Get_CancelsAnOutstandingRequest() + { + // 1. Hold the response until the caller cancels. + TaskCompletionSource started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + using HttpClient http = new HttpClient(new TestHandler(async (request, token) => + { + started.SetResult(true); + await Task.Delay(Timeout.Infinite, token); + return KeyResponse(); + })); + using CancellationTokenSource cancellation = new CancellationTokenSource(); + Task pending = TableUserDelegationKey.GetAsync( + Endpoint, new TestCredential(), Options(http), ServerTime, cancellation.Token); + await started.Task; + + // 2. Cancellation must stop the request instead of returning a key. + cancellation.Cancel(); + try + { + await pending; + Assert.Fail("Key acquisition should have been canceled."); + } + catch (OperationCanceledException) + { + Assert.IsTrue(cancellation.IsCancellationRequested); + } + } + + [DataTestMethod] + [DataRow("SignedOid")] + [DataRow("SignedTid")] + [DataRow("SignedStart")] + [DataRow("SignedExpiry")] + [DataRow("SignedService")] + [DataRow("SignedVersion")] + [DataRow("Value")] + public async Task Get_RejectsMissingKeyFields(string missing) + { + // 1. Omit a required field from an otherwise valid response. + XElement body = KeyBody(); + body.Element(missing)!.Remove(); + using HttpClient http = new HttpClient(new TestHandler((request, token) => Task.FromResult(KeyResponse(body)))); + + // 2. Reject the incomplete key before it can sign a SAS. + InvalidOperationException failure = await Assert.ThrowsExceptionAsync(() => + TableUserDelegationKey.GetAsync(Endpoint, new TestCredential(), Options(http), ServerTime, default)); + StringAssert.Contains(failure.Message, missing); + } + + [DataTestMethod] + [DataRow("b")] + [DataRow("q")] + public async Task Get_RejectsKeysForOtherServices(string service) + { + // 1. Return a valid key for a different Storage service. + XElement body = KeyBody(); + body.Element("SignedService")!.Value = service; + using HttpClient http = new HttpClient(new TestHandler((request, token) => Task.FromResult(KeyResponse(body)))); + + // 2. Never use a Blob or Queue key to sign Table requests. + InvalidOperationException failure = await Assert.ThrowsExceptionAsync(() => + TableUserDelegationKey.GetAsync(Endpoint, new TestCredential(), Options(http), ServerTime, default)); + StringAssert.Contains(failure.Message, "Table user delegation key"); + } + + [DataTestMethod] + [DataRow("account", "HistoryTable", 15, "20YN1Rzzdm1GF9MrfJaD0Zn2RoIvejbHZbM/L/7tWD8=")] + [DataRow("account", "historytable", 15, "20YN1Rzzdm1GF9MrfJaD0Zn2RoIvejbHZbM/L/7tWD8=")] + [DataRow("otheraccount", "HistoryTable", 15, "kR3sfLgvjLyPkp5NOSENr3DbZl9+rj4eYHjRyyROK/Q=")] + [DataRow("account", "HistoryTable", 30, "OKzkOhSjSgdspP7c/WzkSbHWlpUh+Kj+a5A+ZQ0Ods8=")] + public async Task Sign_MatchesTheTableServiceSignature(string account, string table, int seconds, string expectedSignature) + { + // 1. Load a fixed test key and choose a short SAS expiry. + using HttpClient http = new HttpClient(new TestHandler((request, token) => Task.FromResult(KeyResponse()))); + TableUserDelegationKey key = await TableUserDelegationKey.GetAsync( + Endpoint, new TestCredential(), Options(http), ServerTime, default); + DateTimeOffset expiresOn = ServerTime.AddSeconds(seconds); + + // 2. Check the REST signature, including empty fields and lowercase table name. + Dictionary query = key.Sign(account, table, expiresOn).Split('&') + .Select(part => part.Split(new[] { '=' }, 2)) + .ToDictionary(parts => parts[0], parts => Uri.UnescapeDataString(parts[1])); + Assert.AreEqual(expectedSignature, query["sig"]); + Assert.AreEqual(table, query["tn"]); + Assert.AreEqual("raud", query["sp"]); + Assert.AreEqual("https", query["spr"]); + Assert.AreEqual("2025-07-05", query["sv"]); + Assert.AreEqual(KeyStart, query["st"]); + Assert.AreEqual(expiresOn.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture), query["se"]); + Assert.AreEqual(ObjectId, query["skoid"]); + Assert.AreEqual(TenantId, query["sktid"]); + Assert.AreEqual(KeyStart, query["skt"]); + Assert.AreEqual(KeyExpiry, query["ske"]); + Assert.AreEqual("t", query["sks"]); + Assert.AreEqual("2025-07-05", query["skv"]); + Assert.AreEqual(13, query.Count); + } + + static TableClientOptions Options(HttpClient http) + { + TableClientOptions options = new TableClientOptions { Transport = new HttpClientTransport(http) }; + options.Retry.MaxRetries = 0; + return options; + } + + static XElement KeyBody() => new XElement("UserDelegationKey", + new XElement("SignedOid", ObjectId), + new XElement("SignedTid", TenantId), + new XElement("SignedStart", KeyStart), + new XElement("SignedExpiry", KeyExpiry), + new XElement("SignedService", "t"), + new XElement("SignedVersion", "2025-07-05"), + new XElement("Value", KeyValue)); + + static HttpResponseMessage KeyResponse(XElement? body = null) => new HttpResponseMessage(HttpStatusCode.OK) + { + Content = new StringContent((body ?? KeyBody()).ToString(SaveOptions.DisableFormatting), Encoding.UTF8, "application/xml"), + }; + + static HttpResponseMessage ErrorResponse(int status, string body) + { + // The HTTP pipeline owns and disposes the returned response. + HttpResponseMessage response = new HttpResponseMessage((HttpStatusCode)status) + { + Content = new StringContent(body, Encoding.UTF8, "application/xml"), + }; + response.Headers.Add("x-ms-request-id", "test-request-id"); + return response; + } + + sealed class TestCredential : TokenCredential + { + public string[] Scopes { get; private set; } = Array.Empty(); + public int Requests { get; private set; } + + public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + this.Scopes = requestContext.Scopes; + this.Requests++; + return new AccessToken("unit-test-token", DateTimeOffset.UtcNow.AddHours(1)); + } + + public override ValueTask GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) => + new ValueTask(this.GetToken(requestContext, cancellationToken)); + } + + sealed class RejectWorkloadRequestsPolicy : HttpPipelineSynchronousPolicy + { + public override void OnSendingRequest(HttpMessage message) => + throw new InvalidOperationException("Key acquisition must not use workload policies."); + } + + sealed class TestHandler : HttpMessageHandler + { + readonly Func> respond; + + public TestHandler(Func> respond) => this.respond = respond; + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) => + this.respond(request, cancellationToken); + } + } +}