diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCache.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCache.cs index e59e284270..aec7f24bf5 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCache.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCache.cs @@ -3,9 +3,11 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Threading; +using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; #nullable enable @@ -22,6 +24,23 @@ internal sealed class SymmetricKeyCache private readonly MemoryCache _cache; + // Makes cache check-and-publish atomic so sync/async races share the first published key. + // Also protects pending-gate membership and user counts. Unlike s_cacheLock, this short + // lock never covers provider I/O, so a blocked provider cannot delay async publication. + private readonly object _publicationLock = new(); + + // Shares one load gate among async misses for the same key, avoiding duplicate decryptions + // while allowing different keys' async I/O to overlap. A single global async gate would + // serialize unrelated keys; user counts keep each entry alive until all callers leave. + private readonly Dictionary _pendingKeyLoads = new(); + + private sealed class KeyLoadGate + { + internal readonly SemaphoreSlim Semaphore = new(1, 1); + // Count owners and waiters so removal cannot create a second gate for an active key. + internal int Users; + } + private SymmetricKeyCache() { _cache = new MemoryCache(new MemoryCacheOptions()); @@ -36,17 +55,7 @@ public SymmetricKey GetKey(SqlEncryptionKeyInfo keyInfo, SqlConnection connectio { string serverName = connection.DataSource; Debug.Assert(serverName is not null, @"serverName should not be null."); - int capacity = serverName!.Length + SqlSecurityUtility.GetBase64LengthFromByteLength(keyInfo.encryptedKey.Length) + keyInfo.keyStoreName.Length + 2 /* separators */; - StringBuilder cacheLookupKeyBuilder = new(serverName, capacity); - - cacheLookupKeyBuilder.Append(':'); - cacheLookupKeyBuilder.Append(Convert.ToBase64String(keyInfo.encryptedKey)); - cacheLookupKeyBuilder.Append(':'); - cacheLookupKeyBuilder.Append(keyInfo.keyStoreName); - - string cacheLookupKey = cacheLookupKeyBuilder.ToString(); - - Debug.Assert(cacheLookupKey.Length <= capacity, "We needed to allocate a larger array"); + string cacheLookupKey = CreateCacheLookupKey(keyInfo, serverName!); // Lookup the key in cache if (!_cache.TryGetValue(cacheLookupKey, out SymmetricKey? encryptionKey) @@ -101,7 +110,7 @@ public SymmetricKey GetKey(SqlEncryptionKeyInfo keyInfo, SqlConnection connectio { // In case multiple threads reach here at the same time, the first one wins. // The allocated memory will be reclaimed by Garbage Collector. - _cache.Set(cacheLookupKey, encryptionKey, absoluteExpirationRelativeToNow: SqlConnection.ColumnEncryptionKeyCacheTtl); + encryptionKey = PublishKey(cacheLookupKey, encryptionKey, SqlConnection.ColumnEncryptionKeyCacheTtl); } } } @@ -114,5 +123,183 @@ public SymmetricKey GetKey(SqlEncryptionKeyInfo keyInfo, SqlConnection connectio return encryptionKey; } + + /// + /// Asynchronously retrieves a plaintext key, preserving cancellation and provider error wrapping. + /// + /// + /// Provider invocation uses the process-wide gate to serialize synchronous fallbacks. + /// The returned task is awaited outside that gate so asynchronous I/O does not hold it. + /// When caching is enabled, async misses for the same key wait for its pending load + /// and recheck the cache. Uncached loads do not wait for other asynchronous loads. + /// Different keys load independently; synchronous races retain first-published key identity. + /// + internal async Task GetKeyAsync( + SqlEncryptionKeyInfo keyInfo, + SqlConnection connection, + SqlCommand? command, + CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + string serverName = connection.DataSource; + Debug.Assert(serverName is not null, @"serverName should not be null."); + string cacheLookupKey = CreateCacheLookupKey(keyInfo, serverName!); + + if (_cache.TryGetValue(cacheLookupKey, out SymmetricKey? cachedKey) && cachedKey is not null) + { + return cachedKey; + } + + if (SqlConnection.ColumnEncryptionKeyCacheTtl == TimeSpan.Zero) + { + return await LoadKeyAsync( + keyInfo, connection, command, cancellationToken, serverName!, cacheLookupKey) + .ConfigureAwait(false); + } + + KeyLoadGate gate; + lock (_publicationLock) + { + if (!_pendingKeyLoads.TryGetValue(cacheLookupKey, out gate!)) + { + gate = new KeyLoadGate(); + _pendingKeyLoads.Add(cacheLookupKey, gate); + } + gate.Users++; + } + + bool acquired = false; + try + { + await gate.Semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); + acquired = true; + cancellationToken.ThrowIfCancellationRequested(); + return await LoadKeyAsync(keyInfo, connection, command, cancellationToken, serverName!, cacheLookupKey) + .ConfigureAwait(false); + } + finally + { + if (acquired) + { + gate.Semaphore.Release(); + } + + lock (_publicationLock) + { + if (--gate.Users == 0) + { + _pendingKeyLoads.Remove(cacheLookupKey); + gate.Semaphore.Dispose(); + } + } + } + } + + private async Task LoadKeyAsync( + SqlEncryptionKeyInfo keyInfo, + SqlConnection connection, + SqlCommand? command, + CancellationToken cancellationToken, + string serverName, + string cacheLookupKey) + { + if (_cache.TryGetValue(cacheLookupKey, out SymmetricKey? cachedKey) && cachedKey is not null) + { + return cachedKey; + } + + Debug.Assert(SqlConnection.ColumnEncryptionTrustedMasterKeyPaths is not null, + @"SqlConnection.ColumnEncryptionTrustedMasterKeyPaths should not be null"); + SqlSecurityUtility.ThrowIfKeyPathIsNotTrustedForServer(serverName, keyInfo.keyPath); + + if (!SqlSecurityUtility.TryGetColumnEncryptionKeyStoreProvider( + keyInfo.keyStoreName, out SqlColumnEncryptionKeyStoreProvider provider, connection, command)) + { + throw SQL.UnrecognizedKeyStoreProviderName(keyInfo.keyStoreName, + SqlConnection.GetColumnEncryptionSystemKeyStoreProvidersNames(), + SqlSecurityUtility.GetListOfProviderNamesThatWereSearched(connection, command)); + } + + cancellationToken.ThrowIfCancellationRequested(); + + byte[] plaintextKey; + try + { + Task pendingDecryption; + await s_cacheLock.WaitAsync(cancellationToken).ConfigureAwait(false); + try + { + cancellationToken.ThrowIfCancellationRequested(); + // A synchronous caller may have published this key while we waited. + if (_cache.TryGetValue(cacheLookupKey, out cachedKey) && cachedKey is not null) + { + return cachedKey; + } + + // The base async implementation invokes the synchronous provider before returning. + provider.ColumnEncryptionKeyCacheTtl = TimeSpan.Zero; + pendingDecryption = provider.DecryptColumnEncryptionKeyAsync( + keyInfo.keyPath, keyInfo.algorithmName, keyInfo.encryptedKey, cancellationToken); + } + finally + { + s_cacheLock.Release(); + } + plaintextKey = await pendingDecryption.ConfigureAwait(false); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception e) + { + string keyHex = SqlSecurityUtility.GetBytesAsString(keyInfo.encryptedKey, fLast: true, countOfBytes: 10); + throw SQL.KeyDecryptionFailed(keyInfo.keyStoreName, keyHex, e); + } + + SymmetricKey decryptedKey = new(plaintextKey); + TimeSpan cacheTtl = SqlConnection.ColumnEncryptionKeyCacheTtl; + if (cacheTtl == TimeSpan.Zero) + { + return decryptedKey; + } + + // Publish completed decryption even if cancellation arrived after the provider returned. + return PublishKey(cacheLookupKey, decryptedKey, cacheTtl); + } + + /// + /// Publishes one shared key instance under a short lock that never covers provider I/O. + /// + private SymmetricKey PublishKey(string cacheLookupKey, SymmetricKey decryptedKey, TimeSpan cacheTtl) + { + lock (_publicationLock) + { + if (_cache.TryGetValue(cacheLookupKey, out SymmetricKey? cachedKey) && cachedKey is not null) + { + return cachedKey; + } + + _cache.Set(cacheLookupKey, decryptedKey, absoluteExpirationRelativeToNow: cacheTtl); + return decryptedKey; + } + } + + /// + /// Builds the shared sync/async cache lookup key from the server and encrypted key material. + /// + private static string CreateCacheLookupKey(SqlEncryptionKeyInfo keyInfo, string serverName) + { + int capacity = serverName.Length + SqlSecurityUtility.GetBase64LengthFromByteLength(keyInfo.encryptedKey.Length) + keyInfo.keyStoreName.Length + 2 /* separators */; + StringBuilder cacheLookupKeyBuilder = new(serverName, capacity); + cacheLookupKeyBuilder.Append(':'); + cacheLookupKeyBuilder.Append(Convert.ToBase64String(keyInfo.encryptedKey)); + cacheLookupKeyBuilder.Append(':'); + cacheLookupKeyBuilder.Append(keyInfo.keyStoreName); + + string cacheLookupKey = cacheLookupKeyBuilder.ToString(); + Debug.Assert(cacheLookupKey.Length <= capacity, "We needed to allocate a larger array"); + return cacheLookupKey; + } } } diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCacheTests.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCacheTests.cs new file mode 100644 index 0000000000..969865f666 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/AlwaysEncrypted/SymmetricKeyCacheTests.cs @@ -0,0 +1,803 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Data.SqlClient.AlwaysEncrypted; +using Microsoft.Data.SqlClient.UnitTests.Utilities; +using Xunit; + +namespace Microsoft.Data.SqlClient.UnitTests.AlwaysEncrypted +{ + /// + /// Verifies asynchronous CEK loading, cancellation, and shared cache publication. + /// + [Collection(SymmetricKeyCacheTestCollection.Name)] + public class SymmetricKeyCacheTests + { + private const string ProviderName = "TEST_ASYNC_PROVIDER"; + private const string KeyPath = "test-key-path"; + private const string EncryptionAlgorithm = "RSA_OAEP"; + + /// Creates a deterministic 256-bit root key for encryption tests. + /// Starting value for the key bytes. + /// Key material of the length required by AEAD_AES_256_CBC_HMAC_SHA256. + private static byte[] NewPlaintextKey(byte seed) + { + byte[] key = new byte[32]; + for (int i = 0; i < key.Length; i++) + { + key[i] = (byte)(seed + i); + } + return key; + } + + /// + /// Produces key material whose cache lookup key cannot collide with any other test. The CEK + /// cache is a process-wide singleton shared by every test in this assembly. + /// + /// Name of the provider that can decrypt this key. + /// Master key path supplied to that provider. + /// Key information with unique encrypted bytes for cache isolation. + private static SqlEncryptionKeyInfo NewKeyInfo(string providerName = ProviderName, string keyPath = KeyPath) + => new SqlEncryptionKeyInfo + { + encryptedKey = Guid.NewGuid().ToByteArray(), + databaseId = 1, + cekId = 1, + cekVersion = 1, + cekMdVersion = 1, + keyPath = keyPath, + keyStoreName = providerName, + algorithmName = EncryptionAlgorithm, + }; + + /// Registers test providers on an unopened connection without changing global registrations. + /// Provider names and instances used by this connection. + /// An unopened connection that the caller must dispose. + private static SqlConnection NewConnection(params (string Name, SqlColumnEncryptionKeyStoreProvider Provider)[] providers) + { + SqlConnection connection = new SqlConnection("Data Source=async-ae-unit-test;Column Encryption Setting=Enabled"); + Dictionary map = new(); + foreach ((string name, SqlColumnEncryptionKeyStoreProvider provider) in providers) + { + map[name] = provider; + } + connection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(map); + return connection; + } + + #region SymmetricKeyCache.GetKeyAsync + + /// + /// A key cached synchronously is reused asynchronously without another provider call. + /// + [Fact] + public async Task GetKeyAsync_SynchronouslyCachedKey_ReusesEntry() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + + TestKeyStoreProvider provider = new TestKeyStoreProvider { PlaintextKey = NewPlaintextKey(seed: 11) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + + SqlEncryptionKeyInfo syncFirst = NewKeyInfo(); + SymmetricKey fromSync = cache.GetKey(syncFirst, connection, command: null); + + // Act + SymmetricKey fromAsync = await cache.GetKeyAsync(syncFirst, connection, command: null, CancellationToken.None); + + // Assert + Assert.Same(fromSync, fromAsync); + Assert.Equal(1, provider.DecryptCallCount); + Assert.Equal(0, provider.DecryptAsyncCallCount); + } + + /// + /// A key cached asynchronously is reused synchronously without another provider call. + /// + [Fact] + public async Task GetKey_AsynchronouslyCachedKey_ReusesEntry() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TestKeyStoreProvider provider = new() { PlaintextKey = NewPlaintextKey(seed: 11) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo asyncFirst = NewKeyInfo(); + SymmetricKey asyncKey = await cache.GetKeyAsync(asyncFirst, connection, command: null, CancellationToken.None); + + // Act + SymmetricKey syncKey = cache.GetKey(asyncFirst, connection, command: null); + + // Assert + Assert.Same(asyncKey, syncKey); + Assert.Equal(0, provider.DecryptCallCount); + Assert.Equal(1, provider.DecryptAsyncCallCount); + } + + /// + /// A synchronously populated cache must still honor cancellation on async lookup. + /// + [Fact] + public async Task GetKeyAsync_SynchronouslyCachedKey_ObservesCancellation() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TestKeyStoreProvider provider = new TestKeyStoreProvider { PlaintextKey = NewPlaintextKey(seed: 17) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + SymmetricKey cachedKey = cache.GetKey(keyInfo, connection, command: null); + using CancellationTokenSource cts = new CancellationTokenSource(); + cts.Cancel(); + + // Act + Task cancelled = cache.GetKeyAsync(keyInfo, connection, command: null, cts.Token); + // Assert + await Assert.ThrowsAnyAsync(() => cancelled); + Assert.True(cancelled.IsCanceled); + Assert.Same(cachedKey, cache.GetKey(keyInfo, connection, command: null)); + Assert.Equal(1, provider.DecryptCallCount); + Assert.Equal(0, provider.DecryptAsyncCallCount); + } + + /// + /// An asynchronously populated cache must still honor cancellation on later lookup. + /// + [Fact] + public async Task GetKeyAsync_AsynchronouslyCachedKey_ObservesCancellation() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TestKeyStoreProvider provider = new TestKeyStoreProvider { PlaintextKey = NewPlaintextKey(seed: 17) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + SymmetricKey cachedKey = await cache.GetKeyAsync(keyInfo, connection, command: null, CancellationToken.None); + using CancellationTokenSource cts = new CancellationTokenSource(); + cts.Cancel(); + + // Act + Task cancelled = cache.GetKeyAsync(keyInfo, connection, command: null, cts.Token); + // Assert + await Assert.ThrowsAnyAsync(() => cancelled); + Assert.True(cancelled.IsCanceled); + Assert.Same(cachedKey, cache.GetKey(keyInfo, connection, command: null)); + Assert.Equal(0, provider.DecryptCallCount); + Assert.Equal(1, provider.DecryptAsyncCallCount); + } + + /// + /// Concurrent cold-cache callers share one provider fetch and the published key instance. + /// + [Fact] + public async Task GetKeyAsync_WithConcurrentCacheMisses_FetchesOnceAndPublishesOneKey() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + + TaskCompletionSource release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + + TestKeyStoreProvider provider = new TestKeyStoreProvider + { + DecryptAsyncCallback = async _ => + { + await release.Task; + return NewPlaintextKey(seed: 21); + } + }; + + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + // Act + Task first = cache.GetKeyAsync(keyInfo, connection, command: null, CancellationToken.None); + Task[] followers = Enumerable.Range(0, 8) + .Select(_ => cache.GetKeyAsync(keyInfo, connection, command: null, CancellationToken.None)).ToArray(); + int callsBeforeRelease = provider.DecryptAsyncCallCount; + release.TrySetResult(true); + + SymmetricKey firstKey = await first; + SymmetricKey[] followerKeys = await Task.WhenAll(followers); + + // Assert + Assert.Equal(1, callsBeforeRelease); + Assert.Equal(1, provider.DecryptAsyncCallCount); + Assert.All(followerKeys, key => Assert.Same(firstKey, key)); + + // Subsequent callers, sync or async, see the published instance without another fetch. + Assert.Same(firstKey, cache.GetKey(keyInfo, connection, command: null)); + Assert.Same(firstKey, await cache.GetKeyAsync(keyInfo, connection, command: null, CancellationToken.None)); + Assert.Equal(1, provider.DecryptAsyncCallCount); + Assert.Equal(0, provider.DecryptCallCount); + } + + /// + /// Cancelling a waiting caller must not cancel the active fetch or prevent another key from loading. + /// + [Fact] + public async Task GetKeyAsync_CancelledWaiter_DoesNotBlockOtherCallersOrKeys() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource result = new(TaskCreationOptions.RunContinuationsAsynchronously); + TestKeyStoreProvider provider = new() { DecryptAsyncCallback = _ => result.Task }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + // Act + Task first = cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + using CancellationTokenSource cts = new(); + Task cancelled = cache.GetKeyAsync(keyInfo, connection, null, cts.Token); + Task follower = cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + cts.Cancel(); + + try + { + // Assert + Assert.Same(cancelled, await Task.WhenAny(cancelled, Task.Delay(TimeSpan.FromSeconds(5)))); + OperationCanceledException error = await Assert.ThrowsAnyAsync(() => cancelled); + Assert.Equal(cts.Token, error.CancellationToken); + Assert.False(first.IsCompleted); + provider.DecryptAsyncCallback = null; + Task otherKey = cache.GetKeyAsync(NewKeyInfo(), connection, null, CancellationToken.None); + Assert.Same(otherKey, await Task.WhenAny(otherKey, Task.Delay(TimeSpan.FromSeconds(5)))); + await otherKey; + Assert.Equal(2, provider.DecryptAsyncCallCount); + } + finally + { + // Cleanup + result.TrySetResult(NewPlaintextKey(seed: 22)); + await first; + await follower; + // Drain the task even when an assertion fails before the cancellation check. + try { await cancelled; } + catch (OperationCanceledException) { } + } + + Assert.Same(await first, await follower); + Assert.Equal(2, provider.DecryptAsyncCallCount); + } + + /// + /// A cancelled owner releases the per-key gate so a queued caller can retry independently. + /// + [Fact] + public async Task GetKeyAsync_CancelledOwner_AllowsWaitingCallerToRetry() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource result = new(TaskCreationOptions.RunContinuationsAsynchronously); + TestKeyStoreProvider provider = new() { DecryptAsyncCallback = _ => result.Task }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + using CancellationTokenSource cts = new(); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + // Act + Task first = cache.GetKeyAsync(keyInfo, connection, null, cts.Token); + Task follower = cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + int callsBeforeRelease = provider.DecryptAsyncCallCount; + provider.DecryptAsyncCallback = null; + cts.Cancel(); + result.TrySetCanceled(); + + // Assert + await Assert.ThrowsAnyAsync(() => first); + Assert.True(first.IsCanceled); + + Assert.Same(follower, await Task.WhenAny(follower, Task.Delay(TimeSpan.FromSeconds(5)))); + SymmetricKey key = await follower; + Assert.Equal(1, callsBeforeRelease); + Assert.Equal(2, provider.DecryptAsyncCallCount); + Assert.Same(key, await cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None)); + } + + /// + /// A failed owner releases the per-key gate so a queued caller can retry independently. + /// + [Fact] + public async Task GetKeyAsync_FailedOwner_AllowsWaitingCallerToRetry() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource result = new(TaskCreationOptions.RunContinuationsAsynchronously); + TestKeyStoreProvider provider = new() { DecryptAsyncCallback = _ => result.Task }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + using CancellationTokenSource cts = new(); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + // Act + Task first = cache.GetKeyAsync(keyInfo, connection, null, cts.Token); + Task follower = cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + int callsBeforeRelease = provider.DecryptAsyncCallCount; + provider.DecryptAsyncCallback = null; + result.TrySetException(new InvalidOperationException("Provider failure")); + + // Assert + await Assert.ThrowsAsync(() => first); + + Assert.Same(follower, await Task.WhenAny(follower, Task.Delay(TimeSpan.FromSeconds(5)))); + SymmetricKey key = await follower; + Assert.Equal(1, callsBeforeRelease); + Assert.Equal(2, provider.DecryptAsyncCallCount); + Assert.Same(key, await cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None)); + } + + /// + /// Async fallback waits behind synchronous provider invocation and can cancel before entry. + /// + [Fact] + public async Task GetKeyAsync_SynchronousOwner_WaitsForProviderGateAndHonorsCancellation() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource entered = new(TaskCreationOptions.RunContinuationsAsynchronously); + using ManualResetEventSlim release = new(); + int calls = 0; + SynchronousKeyStoreProvider provider = new() + { + DecryptCallback = () => + { + if (Interlocked.Increment(ref calls) == 1) + { + entered.TrySetResult(true); + release.Wait(); + } + return NewPlaintextKey(seed: 45); + } + }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo ownerInfo = NewKeyInfo(); + // Act + Task owner = Task.Factory.StartNew( + () => Task.FromResult(cache.GetKey(ownerInfo, connection, null)), + CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap(); + using CancellationTokenSource cts = new(); + Task? waiter = null; + SqlEncryptionKeyInfo waiterInfo = NewKeyInfo(); + + try + { + // Assert + Assert.Same(entered.Task, await Task.WhenAny( + entered.Task, Task.Delay(TimeSpan.FromSeconds(5)))); + waiter = cache.GetKeyAsync(waiterInfo, connection, null, cts.Token); + Assert.False(waiter.IsCompleted); + Assert.Equal(1, Volatile.Read(ref calls)); + cts.Cancel(); + Assert.Same(waiter, await Task.WhenAny( + waiter, Task.Delay(TimeSpan.FromSeconds(5)))); + OperationCanceledException error = + await Assert.ThrowsAnyAsync(() => waiter); + Assert.Equal(cts.Token, error.CancellationToken); + Assert.True(waiter.IsCanceled); + Assert.Equal(1, Volatile.Read(ref calls)); + } + finally + { + // Cleanup + release.Set(); + await owner; + if (waiter is not null) + { + try { await waiter; } + catch (OperationCanceledException) { } + } + } + + await cache.GetKeyAsync(waiterInfo, connection, null, CancellationToken.None); + Assert.Equal(2, Volatile.Read(ref calls)); + } + + /// + /// A key published while an async caller waits for the provider gate avoids another decrypt. + /// + [Fact] + public async Task GetKeyAsync_SynchronousOwnerPublishesWhileWaiting_ReusesPublishedKey() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource entered = new(TaskCreationOptions.RunContinuationsAsynchronously); + using ManualResetEventSlim release = new(); + int calls = 0; + byte[] expectedKey = NewPlaintextKey(seed: 46); + SynchronousKeyStoreProvider provider = new() + { + DecryptCallback = () => + { + if (Interlocked.Increment(ref calls) != 1) + { + throw new InvalidOperationException("The key was already decrypted."); + } + entered.TrySetResult(true); + release.Wait(); + return expectedKey; + } + }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + Task? waiter = null; + + // Act + Task owner = Task.Factory.StartNew( + () => cache.GetKey(keyInfo, connection, null), + CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + try + { + Assert.Same(entered.Task, await Task.WhenAny( + entered.Task, Task.Delay(TimeSpan.FromSeconds(5)))); + waiter = cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + bool waitedForOwner = !waiter.IsCompleted; + release.Set(); + SymmetricKey ownerKey = await owner; + SymmetricKey waiterKey = await waiter; + + // Assert + Assert.True(waitedForOwner); + Assert.Same(ownerKey, waiterKey); + Assert.Equal(expectedKey, waiterKey.RootKey); + Assert.Equal(1, Volatile.Read(ref calls)); + } + finally + { + // Cleanup + release.Set(); + await owner; + if (waiter is not null) + { + await waiter; + } + } + } + + /// + /// Async fallback calls remain serialized, and waiting callers can cancel before entry. + /// + [Fact] + public async Task GetKeyAsync_BaseAsyncFallbackOwner_WaitsForProviderGateAndHonorsCancellation() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource entered = new(TaskCreationOptions.RunContinuationsAsynchronously); + using ManualResetEventSlim release = new(); + int calls = 0; + SynchronousKeyStoreProvider provider = new() + { + DecryptCallback = () => + { + if (Interlocked.Increment(ref calls) == 1) + { + entered.TrySetResult(true); + release.Wait(); + } + return NewPlaintextKey(seed: 45); + } + }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo ownerInfo = NewKeyInfo(); + // Act + Task owner = Task.Factory.StartNew( + () => cache.GetKeyAsync(ownerInfo, connection, null, CancellationToken.None), + CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap(); + using CancellationTokenSource cts = new(); + Task? waiter = null; + SqlEncryptionKeyInfo waiterInfo = NewKeyInfo(); + + try + { + // Assert + Assert.Same(entered.Task, await Task.WhenAny( + entered.Task, Task.Delay(TimeSpan.FromSeconds(5)))); + waiter = cache.GetKeyAsync(waiterInfo, connection, null, cts.Token); + Assert.False(waiter.IsCompleted); + Assert.Equal(1, Volatile.Read(ref calls)); + cts.Cancel(); + Assert.Same(waiter, await Task.WhenAny( + waiter, Task.Delay(TimeSpan.FromSeconds(5)))); + OperationCanceledException error = + await Assert.ThrowsAnyAsync(() => waiter); + Assert.Equal(cts.Token, error.CancellationToken); + Assert.True(waiter.IsCanceled); + Assert.Equal(1, Volatile.Read(ref calls)); + } + finally + { + // Cleanup + release.Set(); + await owner; + if (waiter is not null) + { + try { await waiter; } + catch (OperationCanceledException) { } + } + } + + await cache.GetKeyAsync(waiterInfo, connection, null, CancellationToken.None); + Assert.Equal(2, Volatile.Read(ref calls)); + } + + /// + /// Completed async decryption must not wait for unrelated synchronous provider I/O to publish its key. + /// + [Fact] + public async Task GetKeyAsync_UnrelatedSynchronousProviderBlocked_PublishesWithoutWaiting() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource asyncResult = new(TaskCreationOptions.RunContinuationsAsynchronously); + TaskCompletionSource syncEntered = new(TaskCreationOptions.RunContinuationsAsynchronously); + using ManualResetEventSlim releaseSync = new(); + byte[] expectedKey = NewPlaintextKey(seed: 43); + TestKeyStoreProvider provider = new() + { + DecryptAsyncCallback = _ => asyncResult.Task, + DecryptCallback = () => + { + syncEntered.TrySetResult(true); + releaseSync.Wait(); + return NewPlaintextKey(seed: 44); + } + }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo asyncKeyInfo = NewKeyInfo(); + SqlEncryptionKeyInfo syncKeyInfo = NewKeyInfo(); + // Act + Task asyncKey = cache.GetKeyAsync(asyncKeyInfo, connection, command: null, CancellationToken.None); + // Keep blocking provider I/O off the pool needed by the async continuations. + Task syncKey = Task.Factory.StartNew( + () => cache.GetKey(syncKeyInfo, connection, command: null), + CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + try + { + // Assert + Assert.Same(syncEntered.Task, await Task.WhenAny(syncEntered.Task, Task.Delay(TimeSpan.FromSeconds(5)))); + asyncResult.TrySetResult(expectedKey); + Assert.Same(asyncKey, await Task.WhenAny(asyncKey, Task.Delay(TimeSpan.FromSeconds(5)))); + Assert.Equal(expectedKey, (await asyncKey).RootKey); + Assert.False(syncKey.IsCompleted); + Assert.Same(await asyncKey, cache.GetKey(asyncKeyInfo, connection, command: null)); + } + finally + { + // Cleanup + asyncResult.TrySetResult(expectedKey); + releaseSync.Set(); + await syncKey; + await asyncKey; + } + } + + /// + /// Async publication wins a same-key synchronous race without waiting for provider I/O. + /// + [Fact] + public async Task GetKeyAsync_SameKeySynchronousProviderBlocked_PublishesFirstKeyInstance() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TaskCompletionSource asyncResult = new(TaskCreationOptions.RunContinuationsAsynchronously); + TaskCompletionSource syncEntered = new(TaskCreationOptions.RunContinuationsAsynchronously); + using ManualResetEventSlim releaseSync = new(); + byte[] expectedKey = NewPlaintextKey(seed: 43); + TestKeyStoreProvider provider = new() + { + DecryptAsyncCallback = _ => asyncResult.Task, + DecryptCallback = () => + { + syncEntered.TrySetResult(true); + releaseSync.Wait(); + return NewPlaintextKey(seed: 44); + } + }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo asyncKeyInfo = NewKeyInfo(); + SqlEncryptionKeyInfo syncKeyInfo = asyncKeyInfo; + // Act + Task asyncKey = cache.GetKeyAsync(asyncKeyInfo, connection, command: null, CancellationToken.None); + // Keep blocking provider I/O off the pool needed by the async continuations. + Task syncKey = Task.Factory.StartNew( + () => cache.GetKey(syncKeyInfo, connection, command: null), + CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + + try + { + // Assert + Assert.Same(syncEntered.Task, await Task.WhenAny(syncEntered.Task, Task.Delay(TimeSpan.FromSeconds(5)))); + asyncResult.TrySetResult(expectedKey); + Assert.Same(asyncKey, await Task.WhenAny(asyncKey, Task.Delay(TimeSpan.FromSeconds(5)))); + Assert.Equal(expectedKey, (await asyncKey).RootKey); + Assert.False(syncKey.IsCompleted); + Assert.Same(await asyncKey, cache.GetKey(asyncKeyInfo, connection, command: null)); + } + finally + { + // Cleanup + asyncResult.TrySetResult(expectedKey); + releaseSync.Set(); + await syncKey; + await asyncKey; + } + + Assert.Same(await asyncKey, await syncKey); + } + + /// + /// A decryption that has already succeeded must not be thrown away just because the caller + /// cancelled while the result was being published. The remote work is done at that point, so + /// discarding it would only force the next caller to repeat it. + /// + [Fact] + public async Task GetKeyAsync_WhenCancelledAfterDecryptSucceeds_StillPublishesTheKey() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + + using CancellationTokenSource cts = new CancellationTokenSource(); + TestKeyStoreProvider provider = new TestKeyStoreProvider + { + DecryptAsyncCallback = _ => + { + // Cancel between a successful provider call and the cache insertion. + cts.Cancel(); + return Task.FromResult(NewPlaintextKey(seed: 57)); + } + }; + + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + // Act + SymmetricKey key = await cache.GetKeyAsync(keyInfo, connection, command: null, cts.Token); + + // Assert + Assert.NotNull(key); + Assert.Equal(1, provider.DecryptAsyncCallCount); + + // The key was published despite the cancellation, so no second decryption is needed. + Assert.Same(key, cache.GetKey(keyInfo, connection, command: null)); + Assert.Equal(1, provider.DecryptAsyncCallCount); + Assert.Equal(0, provider.DecryptCallCount); + } + + /// + /// Pre-cancelled requests do not invoke the provider or populate the cache. + /// + [Fact] + public async Task GetKeyAsync_PreCancelledToken_DoesNotInvokeProvider() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + + TestKeyStoreProvider provider = new TestKeyStoreProvider { PlaintextKey = NewPlaintextKey(seed: 31) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + + using CancellationTokenSource preCancelled = new CancellationTokenSource(); + preCancelled.Cancel(); + + // Act + Task preCancelledTask = cache.GetKeyAsync(keyInfo, connection, command: null, preCancelled.Token); + + // Assert + await Assert.ThrowsAnyAsync(() => preCancelledTask); + Assert.True(preCancelledTask.IsCanceled); + Assert.Equal(0, provider.DecryptAsyncCallCount); + SymmetricKey key = await cache.GetKeyAsync(keyInfo, connection, null, CancellationToken.None); + Assert.Equal(provider.PlaintextKey, key.RootKey); + Assert.Equal(1, provider.DecryptAsyncCallCount); + } + + /// + /// In-flight cancellation remains unwrapped and does not populate the cache. + /// + [Fact] + public async Task GetKeyAsync_InFlightCancellation_ProducesCancelledTaskAndDoesNotCache() + { + // Arrange + SymmetricKeyCache cache = SymmetricKeyCache.Instance; + TestKeyStoreProvider provider = new() { PlaintextKey = NewPlaintextKey(seed: 31) }; + using SqlConnection connection = NewConnection((ProviderName, provider)); + SqlEncryptionKeyInfo keyInfo = NewKeyInfo(); + using CancellationTokenSource cts = new CancellationTokenSource(); + TaskCompletionSource providerEntered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + provider.DecryptAsyncCallback = async token => + { + providerEntered.TrySetResult(true); + await Task.Delay(Timeout.Infinite, token); + return NewPlaintextKey(seed: 32); + }; + + // Act + Task inFlight = cache.GetKeyAsync(keyInfo, connection, command: null, cts.Token); + await providerEntered.Task; + cts.Cancel(); + + // Assert + // Cancellation is not wrapped in SQL.KeyDecryptionFailed. + await Assert.ThrowsAnyAsync(() => inFlight); + Assert.True(inFlight.IsCanceled); + + // Nothing was cached, so a subsequent successful call still reaches the provider. + provider.DecryptAsyncCallback = null; + SymmetricKey key = await cache.GetKeyAsync(keyInfo, connection, command: null, CancellationToken.None); + Assert.Equal(provider.PlaintextKey, key.RootKey); + } + + #endregion + + /// + /// Uncached requests for the same key overlap provider I/O and do not publish their results. + /// + [Fact] + public async Task GetKeyAsync_ZeroTtl_LoadsSameKeyConcurrentlyWithoutCaching() + { + // Arrange + TimeSpan originalTtl = SqlConnection.ColumnEncryptionKeyCacheTtl; + PendingDecryptionKeyStoreProvider provider = new(); + using SqlConnection connection = new("Data Source=async-ae-unit-test"); + connection.RegisterColumnEncryptionKeyStoreProvidersOnConnection( + new Dictionary + { + ["PENDING_DECRYPTION_PROVIDER"] = provider + }); + SqlEncryptionKeyInfo keyInfo = new() + { + encryptedKey = Guid.NewGuid().ToByteArray(), + keyPath = "test-key-path", + keyStoreName = "PENDING_DECRYPTION_PROVIDER", + algorithmName = "RSA_OAEP" + }; + byte[] plaintextKey = new byte[32]; + Task? first = null; + Task? second = null; + + try + { + SqlConnection.ColumnEncryptionKeyCacheTtl = TimeSpan.Zero; + + // Act + first = SymmetricKeyCache.Instance.GetKeyAsync( + keyInfo, connection, null, CancellationToken.None); + second = SymmetricKeyCache.Instance.GetKeyAsync( + keyInfo, connection, null, CancellationToken.None); + int callsBeforeCompletion = provider.CallCount; + provider.Completion.TrySetResult(plaintextKey); + SymmetricKey[] keys = await Task.WhenAll(first, second); + SymmetricKey subsequent = await SymmetricKeyCache.Instance.GetKeyAsync( + keyInfo, connection, null, CancellationToken.None); + + // Assert + Assert.Equal(2, callsBeforeCompletion); + Assert.Equal(3, provider.CallCount); + Assert.NotSame(keys[0], keys[1]); + Assert.NotSame(keys[0], subsequent); + Assert.All(keys, key => Assert.Equal(plaintextKey, key.RootKey)); + Assert.Equal(plaintextKey, subsequent.RootKey); + } + finally + { + // Cleanup + provider.Completion.TrySetResult(plaintextKey); + try + { + if (first is not null) + { + await first; + } + if (second is not null) + { + await second; + } + } + finally + { + SqlConnection.ColumnEncryptionKeyCacheTtl = originalTtl; + } + } + } + } +} diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/TestIsolationCollections.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/TestIsolationCollections.cs index 8af887ec3d..f9e5151c4b 100644 --- a/src/Microsoft.Data.SqlClient/tests/UnitTests/TestIsolationCollections.cs +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/TestIsolationCollections.cs @@ -25,3 +25,12 @@ public sealed class SimulatedServerTestCollection { public const string Name = "SimulatedServerTests"; } + +/// +/// Isolates tests that temporarily change the process-wide column encryption key cache lifetime. +/// +[CollectionDefinition(Name, DisableParallelization = true)] +public sealed class SymmetricKeyCacheTestCollection +{ + public const string Name = "SymmetricKeyCacheTests"; +} diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/PendingDecryptionKeyStoreProvider.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/PendingDecryptionKeyStoreProvider.cs new file mode 100644 index 0000000000..03c55fda07 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/PendingDecryptionKeyStoreProvider.cs @@ -0,0 +1,47 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Data.SqlClient.UnitTests.Utilities +{ + /// + /// Holds asynchronous decryptions pending so tests can observe overlapping provider calls. + /// + internal sealed class PendingDecryptionKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider + { + private int _callCount; + + internal int CallCount => Volatile.Read(ref _callCount); + + internal TaskCompletionSource Completion { get; } = + new(TaskCreationOptions.RunContinuationsAsynchronously); + + public override Task DecryptColumnEncryptionKeyAsync( + string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey, + CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref _callCount); + return Completion.Task; + } + + public override byte[] DecryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) + => throw new NotSupportedException(); + + public override byte[] EncryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] columnEncryptionKey) + => throw new NotSupportedException(); + + public override byte[] SignColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations) + => throw new NotSupportedException(); + + public override bool VerifyColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations, byte[] signature) + => throw new NotSupportedException(); + } +} diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/SymmetricKeyCacheTestProviders.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/SymmetricKeyCacheTestProviders.cs new file mode 100644 index 0000000000..b08860fb10 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/Utilities/SymmetricKeyCacheTestProviders.cs @@ -0,0 +1,82 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Data.SqlClient.UnitTests.Utilities +{ + /// + /// Exercises the base async implementation without overriding its synchronous fallback. + /// + internal sealed class SynchronousKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider + { + internal Func DecryptCallback { get; set; } = + () => throw new NotSupportedException(); + + public override byte[] DecryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) + => DecryptCallback(); + + public override byte[] EncryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] columnEncryptionKey) + => throw new NotSupportedException(); + + public override byte[] SignColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations) + => throw new NotSupportedException(); + + public override bool VerifyColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations, byte[] signature) + => throw new NotSupportedException(); + } + + /// + /// Makes synchronous and asynchronous decryption independently observable in cache tests. + /// + internal sealed class TestKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider + { + private int _decryptCallCount; + private int _decryptAsyncCallCount; + + internal byte[] PlaintextKey { get; set; } = new byte[32]; + + internal Func>? DecryptAsyncCallback { get; set; } + + internal Func? DecryptCallback { get; set; } + + internal int DecryptCallCount => _decryptCallCount; + + internal int DecryptAsyncCallCount => _decryptAsyncCallCount; + + public override byte[] DecryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) + { + Interlocked.Increment(ref _decryptCallCount); + return DecryptCallback is not null ? DecryptCallback() : PlaintextKey; + } + + public override Task DecryptColumnEncryptionKeyAsync( + string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey, + CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref _decryptAsyncCallCount); + Func>? callback = DecryptAsyncCallback; + return callback is not null ? callback(cancellationToken) : Task.FromResult(PlaintextKey); + } + + public override byte[] EncryptColumnEncryptionKey( + string masterKeyPath, string encryptionAlgorithm, byte[] columnEncryptionKey) + => throw new NotSupportedException(); + + public override byte[] SignColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations) + => throw new NotSupportedException(); + + public override bool VerifyColumnMasterKeyMetadata( + string masterKeyPath, bool allowEnclaveComputations, byte[] signature) + => throw new NotSupportedException(); + } +}