diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs index d3302e0f58..eec8ebe403 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Encryption.cs @@ -792,30 +792,125 @@ private void PrepareTransparentEncryptionFinallyBlock( /// Readonly dictionary with the map of parameter encryption rpc requests with the corresponding original rpc requests. /// Indicates if this is a retry from a failed call. private void ReadDescribeEncryptionParameterResults( - SqlDataReader ds, // @TODO: Rename something more obvious + SqlDataReader ds, ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap, bool isRetry) { - foreach (byte[] attestationInfo in ReadDescribeEncryptionParameterResultsCore(ds, describeParameterEncryptionRpcOriginalRpcMap)) + PendingColumnEncryptionKeyOperations pending = new PendingColumnEncryptionKeyOperations(); + int verifiedCount = 0; + foreach (byte[] attestationInfo in ReadDescribeEncryptionParameterResultsCore(ds, describeParameterEncryptionRpcOriginalRpcMap, pending)) { + // Reject untrusted keys before attestation creates and caches an enclave session. + verifiedCount = VerifyPendingColumnMasterKeySignatures(pending, verifiedCount); EnclaveDelegate.Instance.CreateEnclaveSession( _activeConnection.AttestationProtocol, _activeConnection.Parser.EnclaveType, GetEnclaveSessionParameters(), attestationInfo, enclaveAttestationParameters, customData, customDataLength, isRetry); enclaveAttestationParameters = null; } + + VerifyPendingColumnMasterKeySignatures(pending, verifiedCount); + + IReadOnlyList keyDecryptions = pending.KeyDecryptions; + for (int i = 0; i < keyDecryptions.Count; i++) + { + SqlSecurityUtility.DecryptSymmetricKey(keyDecryptions[i], _activeConnection, this); + } + + CacheQueryMetadataIfNeeded(); + } + + /// + /// Verifies the column master key signatures recorded after the first entries. + /// + /// The number of signatures verified so far. + private int VerifyPendingColumnMasterKeySignatures(PendingColumnEncryptionKeyOperations pending, int startIndex) + { + IReadOnlyList verifications = pending.SignatureVerifications; + for (int i = startIndex; i < verifications.Count; i++) + { + ColumnMasterKeySignatureVerification verification = verifications[i]; + SqlSecurityUtility.VerifyColumnMasterKeySignature( + verification.KeyStoreName, + verification.KeyPath, + verification.IsEnclaveEnabled, + verification.Signature, + _activeConnection, + this); + } + + return verifications.Count; } + /// + /// Asynchronously reads the output of sp_describe_parameter_encryption. + /// + /// + /// Async counterpart of . Result set parsing is + /// shared with the synchronous path; enclave session creation, column master key signature + /// verification, and column encryption key decryption use their asynchronous provider APIs. + /// + /// Resultset from calling to sp_describe_parameter_encryption + /// Readonly dictionary with the map of parameter encryption rpc requests with the corresponding original rpc requests. + /// Indicates if this is a retry from a failed call. + /// Token used to request cancellation of the operation private async Task ReadDescribeEncryptionParameterResultsAsync( SqlDataReader ds, ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap, bool isRetry, CancellationToken cancellationToken) { - foreach (byte[] attestationInfo in ReadDescribeEncryptionParameterResultsCore(ds, describeParameterEncryptionRpcOriginalRpcMap)) + PendingColumnEncryptionKeyOperations pending = new PendingColumnEncryptionKeyOperations(); + int verifiedCount = 0; + foreach (byte[] attestationInfo in ReadDescribeEncryptionParameterResultsCore(ds, describeParameterEncryptionRpcOriginalRpcMap, pending)) { + // Reject untrusted keys before attestation creates and caches an enclave session. + verifiedCount = await VerifyPendingColumnMasterKeySignaturesAsync(pending, verifiedCount, cancellationToken) + .ConfigureAwait(false); await CreateEnclaveSessionAsync(attestationInfo, isRetry, cancellationToken).ConfigureAwait(false); } + + await VerifyPendingColumnMasterKeySignaturesAsync(pending, verifiedCount, cancellationToken) + .ConfigureAwait(false); + + // Metadata entries frequently share a key. Resolve them sequentially so the first result + // populates the cache without creating additional waiters for the same key. + IReadOnlyList keyDecryptions = pending.KeyDecryptions; + for (int i = 0; i < keyDecryptions.Count; i++) + { + await SqlSecurityUtility.DecryptSymmetricKeyAsync(keyDecryptions[i], _activeConnection, this, cancellationToken) + .ConfigureAwait(false); + } + + CacheQueryMetadataIfNeeded(); + } + + /// + /// Asynchronously verifies the column master key signatures recorded after the first + /// entries. + /// + /// The number of signatures verified so far. + private async Task VerifyPendingColumnMasterKeySignaturesAsync( + PendingColumnEncryptionKeyOperations pending, + int startIndex, + CancellationToken cancellationToken) + { + IReadOnlyList verifications = pending.SignatureVerifications; + for (int i = startIndex; i < verifications.Count; i++) + { + ColumnMasterKeySignatureVerification verification = verifications[i]; + await SqlSecurityUtility.VerifyColumnMasterKeySignatureAsync( + verification.KeyStoreName, + verification.KeyPath, + verification.IsEnclaveEnabled, + verification.Signature, + _activeConnection, + this, + cancellationToken) + .ConfigureAwait(false); + } + + return verifications.Count; } private async Task CreateEnclaveSessionAsync(byte[] attestationInfo, bool isRetry, CancellationToken cancellationToken) @@ -827,10 +922,48 @@ await EnclaveDelegate.Instance.CreateEnclaveSessionAsync( enclaveAttestationParameters = null; } - // Yield at attestation boundaries so both execution modes share metadata parsing and cache updates. + /// + /// Adds the encryption metadata for the current query to the query metadata cache when applicable. + /// + private void CacheQueryMetadataIfNeeded() + { + // If we are not in Batch RPC mode, update the query cache with the encryption MD. + if (!_batchRPCMode && ShouldCacheEncryptionMetadata && _parameters?.Count > 0) + { + SqlQueryMetadataCache.GetInstance().AddQueryMetadata(this, ignoreQueriesWithReturnValueParams: true); + } + } + + /// + /// Parses the result sets returned by sp_describe_parameter_encryption. + /// + /// + /// + /// Column master key signature verification and column encryption key decryption are not performed + /// here; they are recorded in so that the caller can execute them either + /// synchronously or asynchronously. Deferring them also means no key store network call is made while + /// the describe-parameter-encryption reader is still positioned mid-stream. + /// + /// + /// Deferring these operations changes which exception surfaces when a query hits more than one + /// problem. Previously verification and decryption were interleaved with parsing, so a bad signature + /// on the first key was raised before the parameter metadata result set was parsed at all. Now parsing + /// completes first, except that recorded signatures are verified before each enclave session is created. + /// A malformed result set is therefore usually reported in preference to a key store failure. + /// + /// + /// The caller verifies signatures before creating enclave sessions, so an untrusted key is rejected + /// before attestation, as before. Key decryption now runs after enclave session creation, so a + /// decryption failure can leave a cached enclave session; the set of exceptions is unchanged. + /// + /// + /// Resultset from calling to sp_describe_parameter_encryption + /// Readonly dictionary with the map of parameter encryption rpc requests with the corresponding original rpc requests. + /// Collects the key store operations that the caller must complete private IEnumerable ReadDescribeEncryptionParameterResultsCore( - SqlDataReader ds, - ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap) + SqlDataReader ds, // @TODO: Rename something more obvious + ReadOnlyDictionary<_SqlRPC, _SqlRPC> describeParameterEncryptionRpcOriginalRpcMap, + PendingColumnEncryptionKeyOperations pending) { // @TODO: This should be SqlTceCipherInfoTable Dictionary columnEncryptionKeyTable = new Dictionary(); @@ -862,7 +995,7 @@ private IEnumerable ReadDescribeEncryptionParameterResultsCore( } // 1) Read the first result set that contains the column encryption key list - bool enclaveMetadataExists = ReadDescribeEncryptionParameterResultsKeys(ds, columnEncryptionKeyTable); + bool enclaveMetadataExists = ReadDescribeEncryptionParameterResultsKeys(ds, columnEncryptionKeyTable, pending); if (!enclaveMetadataExists && !ds.NextResult()) { throw SQL.UnexpectedDescribeParamFormatParameterMetadata(); @@ -896,7 +1029,7 @@ private IEnumerable ReadDescribeEncryptionParameterResultsCore( int receivedMetadataCount = 0; if (!enclaveMetadataExists || ds.NextResult()) { - receivedMetadataCount = ReadDescribeEncryptionParameterResultsMetadata(ds, rpc, columnEncryptionKeyTable); + receivedMetadataCount = ReadDescribeEncryptionParameterResultsMetadata(ds, rpc, columnEncryptionKeyTable, pending); } // When the RPC object gets reused, the parameter array has more parameters that the valid params for the command. @@ -947,12 +1080,64 @@ private IEnumerable ReadDescribeEncryptionParameterResultsCore( } } } + } - // If we are not in Batch RPC mode, update the query cache with the encryption MD. - if (!_batchRPCMode && ShouldCacheEncryptionMetadata && _parameters?.Count > 0) + /// + /// Key store operations discovered while parsing sp_describe_parameter_encryption results, deferred so + /// that they can be executed either synchronously or asynchronously. + /// + private sealed class PendingColumnEncryptionKeyOperations + { + private List _signatureVerifications; + private List _keyDecryptions; + + /// Column master key signatures that must be verified. + internal IReadOnlyList SignatureVerifications => + (IReadOnlyList)_signatureVerifications ?? + Array.Empty(); + + /// Column encryption keys that must be decrypted. + internal IReadOnlyList KeyDecryptions => + (IReadOnlyList)_keyDecryptions ?? Array.Empty(); + + /// + /// Records a column master key signature that must be verified before the query runs. + /// + internal void AddSignatureVerification(string keyStoreName, string keyPath, bool isEnclaveEnabled, byte[] signature) => + (_signatureVerifications ??= new List()) + .Add(new ColumnMasterKeySignatureVerification(keyStoreName, keyPath, isEnclaveEnabled, signature)); + + /// + /// Records a column encryption key that must be decrypted before the query runs. + /// + internal void AddKeyDecryption(SqlCipherMetadata cipherMetadata) => + (_keyDecryptions ??= new List()).Add(cipherMetadata); + } + + /// + /// Describes a pending column master key signature verification. + /// + private readonly struct ColumnMasterKeySignatureVerification + { + internal ColumnMasterKeySignatureVerification(string keyStoreName, string keyPath, bool isEnclaveEnabled, byte[] signature) { - SqlQueryMetadataCache.GetInstance().AddQueryMetadata(this, ignoreQueriesWithReturnValueParams: true); + KeyStoreName = keyStoreName; + KeyPath = keyPath; + IsEnclaveEnabled = isEnclaveEnabled; + Signature = signature; } + + internal string KeyStoreName { get; } + + internal string KeyPath { get; } + + /// + /// Whether the server reported that this key is required by the enclave. Carried explicitly + /// rather than assumed, because it selects which signature the key store validates. + /// + internal bool IsEnclaveEnabled { get; } + + internal byte[] Signature { get; } } private IEnumerable ReadDescribeEncryptionParameterResultsAttestation(SqlDataReader ds) @@ -993,7 +1178,8 @@ private IEnumerable ReadDescribeEncryptionParameterResultsAttestation(Sq private bool ReadDescribeEncryptionParameterResultsKeys( SqlDataReader ds, - Dictionary columnEncryptionKeyTable) + Dictionary columnEncryptionKeyTable, + PendingColumnEncryptionKeyOperations pending) { bool enclaveMetadataExists = true; while (ds.Read()) @@ -1093,13 +1279,9 @@ private bool ReadDescribeEncryptionParameterResultsKeys( length: keySignatureLength); } - SqlSecurityUtility.VerifyColumnMasterKeySignature( - providerName, - keyPath, - isEnclaveEnabled: isRequestedByEnclave, - keySignature, - _activeConnection, - this); + // Defer signature verification: it may reach a key store over the network and must not + // run while this reader is still positioned mid-result-set. + pending.AddSignatureVerification(providerName, keyPath, isRequestedByEnclave, keySignature); // Lookup the key, failing which throw an exception // @TODO: Seriously, we *just* did this, why are we looking it up again?? @@ -1134,7 +1316,8 @@ private bool ReadDescribeEncryptionParameterResultsKeys( private int ReadDescribeEncryptionParameterResultsMetadata( SqlDataReader ds, _SqlRPC rpc, - Dictionary columnEncryptionKeyTable) + Dictionary columnEncryptionKeyTable, + PendingColumnEncryptionKeyOperations pending) { Debug.Assert(rpc is not null, "Describe Parameter Encryption requested for non-TCE spec proc"); @@ -1189,8 +1372,10 @@ private int ReadDescribeEncryptionParameterResultsMetadata( encryptionType: columnEncryptionType, normalizationRuleVersion: columnNormalizationRuleVersion); - // Decrypt the symmetric key. This will also validate and throw if needed. - SqlSecurityUtility.DecryptSymmetricKey(sqlParameter.CipherMetadata, _activeConnection, this); + // Defer decryption of the symmetric key: it may reach a key store over the network + // and must not run while this reader is still positioned mid-result-set. Decryption + // also validates the metadata and will throw if it is invalid. + pending.AddKeyDecryption(sqlParameter.CipherMetadata); // This is effective only for _batchRPCMode even though we set it for // non-_batchRPCMode also, since for non-_batchRPCMode, param options diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs index 7369df8030..7cb9fd1a5f 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.NonQuery.cs @@ -336,6 +336,8 @@ private void BeginExecuteNonQueryInternalReadStage(TaskCompletionSource private void CleanupAfterExecuteNonQueryAsync(Task task, TaskCompletionSource source, Guid operationId) { + _asyncExecutionCancellationToken = default; + if (task.IsFaulted) { Exception e = task.Exception?.InnerException; @@ -709,14 +711,16 @@ private Task InternalExecuteNonQueryAsync(CancellationToken cancellationTok registration = cancellationToken.Register(callback: s_cancelIgnoreFailure, state: this); } - Task returnedTask = source.Task; + _asyncExecutionCancellationToken = cancellationToken; - ExecuteNonQueryAsyncCallContext context = new ExecuteNonQueryAsyncCallContext(); - context.Set(this, source, registration, operationId); - context.CancellationToken = cancellationToken; + Task returnedTask = source.Task; + ExecuteNonQueryAsyncCallContext context = null; try { returnedTask = RegisterForConnectionCloseNotification(returnedTask); + context = new ExecuteNonQueryAsyncCallContext(); + context.Set(this, source, registration, operationId); + context.CancellationToken = cancellationToken; // @TODO: Replace with native async implementation, make Begin/End implementation rely on async implementation Task.Factory.FromAsync( @@ -752,9 +756,17 @@ private Task InternalExecuteNonQueryAsync(CancellationToken cancellationTok } catch (Exception e) { + _asyncExecutionCancellationToken = default; s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e); - context.Dispose(); + if (context is not null) + { + context.Dispose(); + } + else + { + registration.Dispose(); + } source.SetException(e); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs index bbc678a713..98e26bf62a 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Reader.cs @@ -601,6 +601,8 @@ private void CleanupExecuteReaderAsync( TaskCompletionSource source, Guid operationId) { + _asyncExecutionCancellationToken = default; + if (task.IsFaulted) { Exception e = task.Exception.InnerException; @@ -1007,6 +1009,8 @@ private Task InternalExecuteReaderAsync( registration = cancellationToken.Register(s_cancelIgnoreFailure, state: this); } + _asyncExecutionCancellationToken = cancellationToken; + Task returnedTask = source.Task; ExecuteReaderAsyncCallContext context = null; try @@ -1056,13 +1060,20 @@ private Task InternalExecuteReaderAsync( } catch (Exception e) { + _asyncExecutionCancellationToken = default; if (!_parentOperationStarted) { s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e); } - registration.Dispose(); - context?.Dispose(); + if (context is not null) + { + context.Dispose(); + } + else + { + registration.Dispose(); + } source.SetException(e); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs index 2dac832e39..9715f0a144 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.Xml.cs @@ -371,6 +371,8 @@ private void CleanupAfterExecuteXmlReaderAsync( TaskCompletionSource source, Guid operationId) { + _asyncExecutionCancellationToken = default; + if (task.IsFaulted) { Exception e = task.Exception?.InnerException; @@ -511,20 +513,22 @@ private Task InternalExecuteXmlReaderAsync(CancellationToken cancella registration = cancellationToken.Register(callback: s_cancelIgnoreFailure, state: this); } + _asyncExecutionCancellationToken = cancellationToken; + // @TODO: This can be cleaned up to lines if InnerConnection is always SqlInternalConnection ExecuteXmlReaderAsyncCallContext context = null; - if (_activeConnection?.InnerConnection is SqlConnectionInternal sqlInternalConnection) - { - context = sqlInternalConnection.CachedContexts.TakeCommandExecuteXmlReaderAsyncContext(); - } - - context ??= new ExecuteXmlReaderAsyncCallContext(); - context.Set(this, source, registration, operationId); - context.CancellationToken = cancellationToken; - Task returnedTask = source.Task; try { + if (_activeConnection?.InnerConnection is SqlConnectionInternal sqlInternalConnection) + { + context = sqlInternalConnection.CachedContexts.TakeCommandExecuteXmlReaderAsyncContext(); + } + + context ??= new ExecuteXmlReaderAsyncCallContext(); + context.Set(this, source, registration, operationId); + context.CancellationToken = cancellationToken; + returnedTask = RegisterForConnectionCloseNotification(returnedTask); // @TODO: Replace with native async implementation, make Begin/End implementation rely on async implementation @@ -560,9 +564,17 @@ private Task InternalExecuteXmlReaderAsync(CancellationToken cancella } catch (Exception e) { + _asyncExecutionCancellationToken = default; s_diagnosticListener.WriteCommandError(operationId, this, _transaction, e); - context.Dispose(); + if (context is not null) + { + context.Dispose(); + } + else + { + registration.Dispose(); + } source.SetException(e); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs index 3ea3ce2039..c2aa34fc90 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs @@ -129,6 +129,28 @@ private static class ProcParamsColumns /// private AsyncState _cachedAsyncState = null; + /// + /// Cancellation token supplied by the caller of the asynchronous execution that is currently in + /// flight, or when the command is executing synchronously. + /// + /// + /// + /// The asynchronous execution entry points reach the Always Encrypted machinery through several + /// layers of synchronous, callback-driven code that carry no cancellation token. Rather than + /// threading a token through every one of those signatures, including the synchronous paths that + /// would only ever pass , the token is recorded here for the + /// duration of the operation. It is intended only for the asynchronous Always Encrypted key + /// store calls, which are the sole cancellable I/O in that machinery. + /// + /// + /// Read this field only on the thread that started the execution, and pass the captured value on as + /// an explicit parameter. The Always Encrypted work is handed to the thread pool and can still be + /// running when the execution's cleanup continuation resets this field, so a read taken after that + /// hand-off may silently observe . + /// + /// + private CancellationToken _asyncExecutionCancellationToken; + private int _currentlyExecutingBatch; /// diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/AsyncKeyStoreProviderTests.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/AsyncKeyStoreProviderTests.cs new file mode 100644 index 0000000000..2dc1f69878 --- /dev/null +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/AsyncKeyStoreProviderTests.cs @@ -0,0 +1,493 @@ +// 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.Threading; +using System.Threading.Tasks; +using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup; +using Xunit; + +namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted +{ + /// + /// Verifies that asynchronous command execution decrypts column encryption keys found during + /// parameter discovery through the asynchronous key store provider API, forwarding the + /// caller's cancellation token, while synchronous execution keeps using the synchronous API. + /// + [Trait("Set", "AE")] + public sealed class AsyncKeyStoreProviderTests + : IClassFixture + { + private readonly SQLSetupStrategy _fixture; + + /// Captures the fixture that owns the encrypted test table. + /// Always Encrypted setup shared by the tests in this class. + public AsyncKeyStoreProviderTests(SQLSetupStrategyCertStoreProvider fixture) + { + _fixture = fixture; + } + + /// Configured Always Encrypted connection strings. + public static TheoryData AEConnectionStrings + { + get + { + TheoryData data = new(); + foreach (string connectionString in DataTestUtility.AEConnStrings) + { + data.Add(connectionString); + } + return data; + } + } + + /// + /// Synchronous reader execution must keep using only the synchronous provider API, so sync + /// callers see no behavior change from asynchronous key loading. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public void ExecuteReader_EncryptedParameter_UsesSyncProviderApi(string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = new(connectionString); + connection.Open(); + connection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(Wrap(provider)); + using SqlCommand command = CreateEncryptedParameterCommand(connection); + + // Act + Assert.Throws(() => command.ExecuteReader()); + + // Assert + Assert.True(provider.SyncCalls > 0, "Expected the synchronous provider API."); + Assert.Equal(0, provider.AsyncCalls); + } + + /// + /// Asynchronous reader execution must decrypt keys through the asynchronous provider API. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteReaderAsync_EncryptedParameter_UsesAsyncProviderApi( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection); + + // Act + await Assert.ThrowsAsync(() => command.ExecuteReaderAsync()); + + // Assert + AssertOnlyAsyncApiUsed(provider); + } + + /// + /// Asynchronous non-query execution must decrypt keys through the asynchronous provider + /// API. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteNonQueryAsync_EncryptedParameter_UsesAsyncProviderApi( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection); + + // Act + await Assert.ThrowsAsync(() => command.ExecuteNonQueryAsync()); + + // Assert + AssertOnlyAsyncApiUsed(provider); + } + + /// + /// XML execution must use the asynchronous provider API just like reader and non-query + /// execution. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteXmlReaderAsync_EncryptedParameter_UsesAsyncProviderApi( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection, forXml: true); + + // Act + await Assert.ThrowsAsync(() => command.ExecuteXmlReaderAsync()); + + // Assert + AssertOnlyAsyncApiUsed(provider); + } + + /// + /// Reader execution must forward the caller's token through parameter encryption to the + /// provider, and the command must stay reusable after the cancellation. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteReaderAsync_ProviderCancels_ForwardsCallerToken( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection); + using CancellationTokenSource cts = CancelFromProvider(provider); + + // Act + await Assert.ThrowsAnyAsync( + () => command.ExecuteReaderAsync(cts.Token)); + + // Assert + await AssertTokenForwardedAsync(provider, cts.Token, command); + } + + /// + /// Non-query execution must preserve provider cancellation after releasing describe-phase + /// state, and the command must stay reusable after the cancellation. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteNonQueryAsync_ProviderCancels_ForwardsCallerToken( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection); + using CancellationTokenSource cts = CancelFromProvider(provider); + + // Act + await Assert.ThrowsAnyAsync( + () => command.ExecuteNonQueryAsync(cts.Token)); + + // Assert + await AssertTokenForwardedAsync(provider, cts.Token, command); + } + + /// + /// XML execution has separate token capture and must forward the exact caller token to the + /// provider, and the command must stay reusable after the cancellation. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteXmlReaderAsync_ProviderCancels_ForwardsCallerToken( + string connectionString) + { + // Arrange + RecordingKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateEncryptedParameterCommand(connection, forXml: true); + using CancellationTokenSource cts = CancelFromProvider(provider); + + // Act + await Assert.ThrowsAnyAsync( + () => command.ExecuteXmlReaderAsync(cts.Token)); + + // Assert + await AssertTokenForwardedAsync(provider, cts.Token, command); + } + + /// + /// Reader execution must stay pending while the provider's asynchronous key decryption is + /// pending, then complete with the query result once the provider returns a valid key. + /// This proves the decrypted key reaches the parameter metadata used for execution. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteReaderAsync_PendingProvider_CompletesAfterRelease( + string connectionString) + { + // Arrange + GatedKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateGatedCountCommand(connection); + + // Act + // Start execution and wait until the provider is inside its pending decrypt call. + Task execution = command.ExecuteReaderAsync(); + await WaitForProviderEntryAsync(provider); + bool pendingBeforeRelease = !execution.IsCompleted; + + // Let the provider return the key and finish the execution. + provider.Release(); + using SqlDataReader reader = await execution; + + // Assert + Assert.True(pendingBeforeRelease, "Execution completed before the key was loaded."); + Assert.True(await reader.ReadAsync()); + Assert.Equal(0, reader.GetInt32(0)); + } + + /// + /// Cancelling the caller's token while the provider's asynchronous key decryption is + /// pending must fail execution with cancellation and leave the command reusable. + /// + [ConditionalTheory( + typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore))] + [MemberData(nameof(AEConnectionStrings))] + public async Task ExecuteReaderAsync_CanceledWhileProviderPending_ThrowsAndStaysReusable( + string connectionString) + { + // Arrange + GatedKeyStoreProvider provider = new(); + using SqlConnection connection = + await OpenWithProviderAsync(connectionString, provider); + using SqlCommand command = CreateGatedCountCommand(connection); + using CancellationTokenSource cts = new(); + + // Act + // Start execution, then cancel once the provider is inside its pending decrypt call. + Task execution = command.ExecuteReaderAsync(cts.Token); + await WaitForProviderEntryAsync(provider); + cts.Cancel(); + + // Assert + await Assert.ThrowsAnyAsync(() => execution); + await AssertCommandReusableAsync(command); + } + + /// Opens a connection with the given provider registered on it. + /// Configured Always Encrypted test connection. + /// Provider whose decrypt calls the test observes. + /// An open connection owned by the caller. + private static async Task OpenWithProviderAsync( + string connectionString, SqlColumnEncryptionKeyStoreProvider provider) + { + SqlConnection connection = new(connectionString); + await connection.OpenAsync(); + connection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(Wrap(provider)); + return connection; + } + + /// + /// Makes the provider cancel the returned source from inside its async decrypt call, which + /// proves the caller's token was threaded all the way down into the key store operation. + /// + /// Provider that triggers the cancellation. + /// A token source owned by the caller. + private static CancellationTokenSource CancelFromProvider( + RecordingKeyStoreProvider provider) + { + CancellationTokenSource cts = new(); + provider.OnAsyncCall = () => cts.Cancel(); + return cts; + } + + /// Checks that only the asynchronous provider API was used. + /// Provider whose counters are checked. + private static void AssertOnlyAsyncApiUsed(RecordingKeyStoreProvider provider) + { + Assert.True(provider.AsyncCalls > 0, "Expected the asynchronous provider API."); + Assert.Equal(0, provider.SyncCalls); + } + + /// + /// Checks that the provider received the caller's token and that the command can run again, + /// which guards against cancellation leaving released describe-phase state locked. + /// + /// Provider whose counters and token are checked. + /// The token passed to the execution. + /// The command reused for a plain query after cancellation. + /// A task completing after the checks. + private static async Task AssertTokenForwardedAsync( + RecordingKeyStoreProvider provider, CancellationToken expectedToken, SqlCommand command) + { + AssertOnlyAsyncApiUsed(provider); + Assert.Equal(expectedToken, provider.LastCancellationToken); + await AssertCommandReusableAsync(command); + } + + /// + /// Checks that the command can run a plain query, which guards against cancellation + /// leaving released describe-phase state locked. + /// + /// The command reused for a plain query. + /// A task completing after the check. + private static async Task AssertCommandReusableAsync(SqlCommand command) + { + command.Parameters.Clear(); + command.CommandText = "SELECT 1"; + Assert.Equal(1, await command.ExecuteScalarAsync()); + } + + /// + /// Waits until the gated provider is inside its asynchronous decrypt call, failing instead + /// of hanging if execution never reaches the provider. + /// + /// Provider expected to be entered. + /// A task completing once the provider has been entered. + private static async Task WaitForProviderEntryAsync(GatedKeyStoreProvider provider) + { + Task completed = await Task.WhenAny( + provider.Entered, Task.Delay(TimeSpan.FromSeconds(60))); + Assert.True( + completed == provider.Entered, "The asynchronous provider API was not called."); + } + + /// Maps the fixture's dummy provider name to the given provider. + /// Provider whose decrypt calls the test observes. + /// A provider map for registration on a single connection. + private static Dictionary Wrap( + SqlColumnEncryptionKeyStoreProvider provider) => + new() { { DummyKeyStoreProvider.Name, provider } }; + + /// + /// Builds an unencrypted count over an encrypted predicate. The fixture never inserts rows + /// into this table, so the count is 0. A unique comment keeps the query out of the query + /// metadata cache, so every run takes the parameter discovery path. + /// + /// Open connection with the gated provider registered. + /// A disposable command with a deterministic encrypted predicate. + private SqlCommand CreateGatedCountCommand(SqlConnection connection) + { + SqlCommand command = new( + $"SELECT COUNT(*) FROM [{_fixture.CustomKeyStoreProviderTestTable.Name}] " + + $"WHERE CustomerID = @id /* {Guid.NewGuid()} */", + connection, + transaction: null, + SqlCommandColumnEncryptionSetting.Enabled); + command.Parameters.AddWithValue("id", 9); + return command; + } + + /// + /// Builds an encrypted predicate; XML queries project only a constant because SQL Server + /// does not support FOR XML over encrypted result columns. + /// + /// Open connection with the recording provider registered. + /// Whether to request XML instead of the encrypted columns. + /// A disposable command with a deterministic encrypted predicate. + private SqlCommand CreateEncryptedParameterCommand( + SqlConnection connection, bool forXml = false) + { + string projection = forXml ? "1 AS Value" : "*"; + string xmlClause = forXml ? " FOR XML PATH('row')" : string.Empty; + SqlCommand command = new( + $"SELECT {projection} FROM [{_fixture.CustomKeyStoreProviderTestTable.Name}] " + + $"WHERE CustomerID = @id{xmlClause}", + connection, + transaction: null, + SqlCommandColumnEncryptionSetting.Enabled); + command.Parameters.AddWithValue("id", 9); + return command; + } + + /// + /// Records which key store provider overload the driver invoked. Both overloads throw so + /// the tests never depend on real key material; assertions use the counters. The + /// deterministic equality predicate requests neither enclave computations nor CMK + /// signature verification. + /// + private sealed class RecordingKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider + { + private int _syncCalls; + private int _asyncCalls; + + public int SyncCalls => Volatile.Read(ref _syncCalls); + + public int AsyncCalls => Volatile.Read(ref _asyncCalls); + + public Action OnAsyncCall { get; set; } + + public CancellationToken LastCancellationToken { get; private set; } + + public override byte[] DecryptColumnEncryptionKey( + string masterKeyPath, + string encryptionAlgorithm, + byte[] encryptedColumnEncryptionKey) + { + Interlocked.Increment(ref _syncCalls); + throw new NotImplementedException(); + } + + public override Task DecryptColumnEncryptionKeyAsync( + string masterKeyPath, + string encryptionAlgorithm, + byte[] encryptedColumnEncryptionKey, + CancellationToken cancellationToken = default) + { + Interlocked.Increment(ref _asyncCalls); + LastCancellationToken = cancellationToken; + OnAsyncCall?.Invoke(); + cancellationToken.ThrowIfCancellationRequested(); + throw new NotImplementedException(); + } + + public override byte[] EncryptColumnEncryptionKey( + string masterKeyPath, + string encryptionAlgorithm, + byte[] columnEncryptionKey) => + throw new NotImplementedException(); + } + + /// + /// Holds asynchronous key decryption pending until released, then returns the real key. + /// The fixture encrypts the dummy provider's key with the certificate store provider, so + /// decryption delegates to it. + /// + private sealed class GatedKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider + { + private readonly SqlColumnEncryptionCertificateStoreProvider _certStoreProvider = new(); + private readonly TaskCompletionSource _entered = + new(TaskCreationOptions.RunContinuationsAsynchronously); + private readonly TaskCompletionSource _release = + new(TaskCreationOptions.RunContinuationsAsynchronously); + + public Task Entered => _entered.Task; + + public void Release() => _release.TrySetResult(true); + + public override byte[] DecryptColumnEncryptionKey( + string masterKeyPath, + string encryptionAlgorithm, + byte[] encryptedColumnEncryptionKey) => + _certStoreProvider.DecryptColumnEncryptionKey( + masterKeyPath, encryptionAlgorithm, encryptedColumnEncryptionKey); + + public override async Task DecryptColumnEncryptionKeyAsync( + string masterKeyPath, + string encryptionAlgorithm, + byte[] encryptedColumnEncryptionKey, + CancellationToken cancellationToken = default) + { + _entered.TrySetResult(true); + await Task.WhenAny( + _release.Task, Task.Delay(Timeout.Infinite, cancellationToken)); + cancellationToken.ThrowIfCancellationRequested(); + return DecryptColumnEncryptionKey( + masterKeyPath, encryptionAlgorithm, encryptedColumnEncryptionKey); + } + + public override byte[] EncryptColumnEncryptionKey( + string masterKeyPath, + string encryptionAlgorithm, + byte[] columnEncryptionKey) => + throw new NotImplementedException(); + } + } +} diff --git a/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandTests.cs b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandTests.cs index 9c0d2bb38e..0d308b009b 100644 --- a/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandTests.cs +++ b/src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandTests.cs @@ -2,14 +2,20 @@ // 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.Reflection; +using System.Threading; +using System.Threading.Tasks; using Microsoft.Data.SqlClient.Parser; using Xunit; namespace Microsoft.Data.SqlClient.UnitTests; -/// Verifies batch command lookup for SQL errors and info messages. +/// +/// Verifies batch command lookup for SQL errors and info messages, and asynchronous execution +/// cancellation state cleanup. +/// public class SqlCommandTests { /// Errors after the final DONEPROC have no remaining command to attribute. @@ -133,6 +139,114 @@ public void GetBatchCommand_IndexWithinBatch_ReturnsBatchCommand(int index) Assert.Same(expected, batchCommand); } + /// + /// Key store cancellation after the describe phase releases the parser state must surface as + /// cancellation, not as a null-lock failure, when the reader execution ends. + /// + [Fact] + public void EndExecuteReaderAsync_CanceledTask_ThrowsOperationCanceled() + { + // Act + Exception? inner = InvokeEndAsyncWithCanceledTask("EndExecuteReaderAsync"); + + // Assert + Assert.IsAssignableFrom(inner); + } + + /// + /// Key store cancellation after the describe phase releases the parser state must surface as + /// cancellation, not as a null-lock failure, when the non-query execution ends. + /// + [Fact] + public void EndExecuteNonQueryAsync_CanceledTask_ThrowsOperationCanceled() + { + // Act + Exception? inner = InvokeEndAsyncWithCanceledTask("EndExecuteNonQueryAsync"); + + // Assert + Assert.IsAssignableFrom(inner); + } + + /// + /// Key store cancellation after the describe phase releases the parser state must surface as + /// cancellation, not as a null-lock failure, when the XML reader execution ends. + /// + [Fact] + public void EndExecuteXmlReaderAsync_CanceledTask_ThrowsOperationCanceled() + { + // Act + Exception? inner = InvokeEndAsyncWithCanceledTask("EndExecuteXmlReaderAsync"); + + // Assert + Assert.IsAssignableFrom(inner); + } + + /// + /// A missing or closed connection faults reader setup without retaining the caller's token, + /// even though setup fails before the cleanup continuation is registered. + /// + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task ExecuteReaderAsync_FailedSetup_ClearsCancellationToken(bool hasConnection) + { + // Arrange + using SqlConnection connection = new(); + using SqlCommand command = new("SELECT 1", hasConnection ? connection : null); + using CancellationTokenSource source = new(); + + // Act + await Assert.ThrowsAsync( + () => command.ExecuteReaderAsync(source.Token)); + + // Assert + Assert.Equal(CancellationToken.None, GetAsyncExecutionCancellationToken(command)); + } + + /// + /// A missing or closed connection faults non-query setup without retaining the caller's token, + /// even though setup fails before the cleanup continuation is registered. + /// + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task ExecuteNonQueryAsync_FailedSetup_ClearsCancellationToken(bool hasConnection) + { + // Arrange + using SqlConnection connection = new(); + using SqlCommand command = new("SELECT 1", hasConnection ? connection : null); + using CancellationTokenSource source = new(); + + // Act + await Assert.ThrowsAsync( + () => command.ExecuteNonQueryAsync(source.Token)); + + // Assert + Assert.Equal(CancellationToken.None, GetAsyncExecutionCancellationToken(command)); + } + + /// + /// A missing or closed connection faults XML reader setup without retaining the caller's + /// token, even though setup fails before the cleanup continuation is registered. + /// + [Theory] + [InlineData(false)] + [InlineData(true)] + public async Task ExecuteXmlReaderAsync_FailedSetup_ClearsCancellationToken(bool hasConnection) + { + // Arrange + using SqlConnection connection = new(); + using SqlCommand command = new("SELECT 1", hasConnection ? connection : null); + using CancellationTokenSource source = new(); + + // Act + await Assert.ThrowsAsync( + () => command.ExecuteXmlReaderAsync(source.Token)); + + // Assert + Assert.Equal(CancellationToken.None, GetAsyncExecutionCancellationToken(command)); + } + /// Creates RPC entries without requiring a network connection. /// The number of entries to add. /// A command owned by the test. @@ -156,4 +270,35 @@ private static List<_SqlRPC> GetBatchEntries(SqlCommand command) => Assert.IsType>(typeof(SqlCommand) .GetField("_RPCList", BindingFlags.Instance | BindingFlags.NonPublic)! .GetValue(command)); + + /// + /// Invokes a private asynchronous End method with an already canceled task, as happens when a + /// key store operation is canceled after the parser state was released. + /// + /// The name of the private End method to invoke. + /// The exception thrown by the End method. + private static Exception? InvokeEndAsyncWithCanceledTask(string endMethod) + { + using SqlConnection connection = new( + "Data Source=async-ae-unit-test;Column Encryption Setting=Enabled"); + using SqlCommand command = new("SELECT 1", connection); + TaskCompletionSource completion = new(); + completion.SetCanceled(); + + MethodInfo method = typeof(SqlCommand).GetMethod( + endMethod, BindingFlags.Instance | BindingFlags.NonPublic) + ?? throw new InvalidOperationException("Async End method not found."); + TargetInvocationException exception = Assert.Throws( + () => method.Invoke(command, new object[] { completion.Task })); + return exception.InnerException; + } + + /// Reads the private token captured for the in-flight async execution. + /// The command to inspect. + /// The captured cancellation token. + private static CancellationToken GetAsyncExecutionCancellationToken(SqlCommand command) => + Assert.IsType(typeof(SqlCommand) + .GetField( + "_asyncExecutionCancellationToken", BindingFlags.Instance | BindingFlags.NonPublic)! + .GetValue(command)); }