|
1 |
| -// Licensed to the .NET Foundation under one or more agreements. |
2 |
| -// The .NET Foundation licenses this file to you under the MIT license. |
3 |
| -// See the LICENSE file in the project root for more information. |
4 |
| - |
5 |
| -using System; |
6 |
| -using System.Collections.Generic; |
7 |
| -using System.Linq; |
8 |
| -using Azure.Identity; |
9 |
| -using Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider; |
10 |
| -using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup; |
11 |
| -using Xunit; |
12 |
| - |
13 |
| -namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted |
14 |
| -{ |
15 |
| - public class AKVTest : IClassFixture<SQLSetupStrategyAzureKeyVault> |
16 |
| - { |
17 |
| - private readonly SQLSetupStrategyAzureKeyVault _fixture; |
18 |
| - private readonly string _akvTableName; |
19 |
| - |
20 |
| - public AKVTest(SQLSetupStrategyAzureKeyVault fixture) |
21 |
| - { |
22 |
| - _fixture = fixture; |
23 |
| - _akvTableName = fixture.AKVTestTable.Name; |
24 |
| - |
25 |
| - // Disable the cache to avoid false failures. |
26 |
| - SqlConnection.ColumnEncryptionQueryMetadataCacheEnabled = false; |
27 |
| - } |
28 |
| - |
29 |
| - [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))] |
30 |
| - public void TestEncryptDecryptWithAKV() |
31 |
| - { |
32 |
| - SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS) |
33 |
| - { |
34 |
| - ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled, |
35 |
| - AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified, |
36 |
| - EnclaveAttestationUrl = "" |
37 |
| - }; |
38 |
| - using SqlConnection sqlConnection = new (builder.ConnectionString); |
39 |
| - |
40 |
| - sqlConnection.Open(); |
41 |
| - Customer customer = new(45, "Microsoft", "Corporation"); |
42 |
| - |
43 |
| - // Start a transaction and either commit or rollback based on the test variation. |
44 |
| - using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction()) |
45 |
| - { |
46 |
| - DatabaseHelper.InsertCustomerData(sqlConnection, sqlTransaction, _akvTableName, customer); |
47 |
| - sqlTransaction.Commit(); |
48 |
| - } |
49 |
| - |
50 |
| - // Test INPUT parameter on an encrypted parameter |
51 |
| - using SqlCommand sqlCommand = new ($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName", |
52 |
| - sqlConnection); |
53 |
| - SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft"); |
54 |
| - customerFirstParam.Direction = System.Data.ParameterDirection.Input; |
55 |
| - customerFirstParam.ForceColumnEncryption = true; |
56 |
| - |
57 |
| - using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); |
58 |
| - DatabaseHelper.ValidateResultSet(sqlDataReader); |
59 |
| - } |
60 |
| - |
61 |
| - [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAKVSetupAvailable))] |
62 |
| - [PlatformSpecific(TestPlatforms.Windows)] |
63 |
| - public void TestRoundTripWithAKVAndCertStoreProvider() |
64 |
| - { |
65 |
| - using SQLSetupStrategyCertStoreProvider certStoreFixture = new (); |
66 |
| - byte[] plainTextColumnEncryptionKey = ColumnEncryptionKey.GenerateRandomBytes(ColumnEncryptionKey.KeySizeInBytes); |
67 |
| - byte[] encryptedColumnEncryptionKeyUsingAKV = _fixture.AkvStoreProvider.EncryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", plainTextColumnEncryptionKey); |
68 |
| - byte[] columnEncryptionKeyReturnedAKV2Cert = certStoreFixture.CertStoreProvider.DecryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingAKV); |
69 |
| - Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedAKV2Cert), @"Roundtrip failed"); |
70 |
| - |
71 |
| - // Try the opposite. |
72 |
| - byte[] encryptedColumnEncryptionKeyUsingCert = certStoreFixture.CertStoreProvider.EncryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", plainTextColumnEncryptionKey); |
73 |
| - byte[] columnEncryptionKeyReturnedCert2AKV = _fixture.AkvStoreProvider.DecryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCert); |
74 |
| - Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCert2AKV), @"Roundtrip failed"); |
75 |
| - } |
76 |
| - |
77 |
| - [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))] |
78 |
| - public void TestLocalCekCacheIsScopedToProvider() |
79 |
| - { |
80 |
| - SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS) |
81 |
| - { |
82 |
| - ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled, |
83 |
| - AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified, |
84 |
| - EnclaveAttestationUrl = "" |
85 |
| - }; |
86 |
| - |
87 |
| - using SqlConnection sqlConnection = new(builder.ConnectionString); |
88 |
| - |
89 |
| - sqlConnection.Open(); |
90 |
| - |
91 |
| - // Test INPUT parameter on an encrypted parameter |
92 |
| - using SqlCommand sqlCommand = new($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName", |
93 |
| - sqlConnection); |
94 |
| - SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft"); |
95 |
| - customerFirstParam.Direction = System.Data.ParameterDirection.Input; |
96 |
| - customerFirstParam.ForceColumnEncryption = true; |
97 |
| - |
98 |
| - SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); |
99 |
| - sqlDataReader.Close(); |
100 |
| - |
101 |
| - SqlColumnEncryptionAzureKeyVaultProvider sqlColumnEncryptionAzureKeyVaultProvider = |
102 |
| - new(new SqlClientCustomTokenCredential()); |
103 |
| - |
104 |
| - Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customProvider = new() |
105 |
| - { |
106 |
| - { SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, sqlColumnEncryptionAzureKeyVaultProvider } |
107 |
| - }; |
108 |
| - |
109 |
| - // execute a query using provider from command-level cache. this will cache the cek in the local cek cache |
110 |
| - sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider); |
111 |
| - SqlDataReader sqlDataReader2 = sqlCommand.ExecuteReader(); |
112 |
| - sqlDataReader2.Close(); |
113 |
| - |
114 |
| - // global cek cache and local cek cache are populated above |
115 |
| - // when using a new per-command provider, it will only use its local cek cache |
116 |
| - // the following query should fail due to an empty cek cache and invalid credentials |
117 |
| - customProvider[SqlColumnEncryptionAzureKeyVaultProvider.ProviderName] = |
118 |
| - new SqlColumnEncryptionAzureKeyVaultProvider(new ClientSecretCredential("tenant", "client", "secret")); |
119 |
| - sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider); |
120 |
| - Exception ex = Assert.Throws<SqlException>(() => sqlCommand.ExecuteReader()); |
121 |
| - Assert.StartsWith("The current credential is not configured to acquire tokens for tenant", ex.InnerException.Message); |
122 |
| - } |
123 |
| - } |
124 |
| -} |
| 1 | +//// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +//// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +//// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +//using System; |
| 6 | +//using System.Collections.Generic; |
| 7 | +//using System.Linq; |
| 8 | +//using Azure.Identity; |
| 9 | +//using Microsoft.Data.SqlClient.AlwaysEncrypted.AzureKeyVaultProvider; |
| 10 | +//using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup; |
| 11 | +//using Xunit; |
| 12 | + |
| 13 | +//namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted |
| 14 | +//{ |
| 15 | +// public class AKVTest : IClassFixture<SQLSetupStrategyAzureKeyVault> |
| 16 | +// { |
| 17 | +// private readonly SQLSetupStrategyAzureKeyVault _fixture; |
| 18 | +// private readonly string _akvTableName; |
| 19 | + |
| 20 | +// public AKVTest(SQLSetupStrategyAzureKeyVault fixture) |
| 21 | +// { |
| 22 | +// _fixture = fixture; |
| 23 | +// _akvTableName = fixture.AKVTestTable.Name; |
| 24 | + |
| 25 | +// // Disable the cache to avoid false failures. |
| 26 | +// SqlConnection.ColumnEncryptionQueryMetadataCacheEnabled = false; |
| 27 | +// } |
| 28 | + |
| 29 | +// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))] |
| 30 | +// public void TestEncryptDecryptWithAKV() |
| 31 | +// { |
| 32 | +// SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS) |
| 33 | +// { |
| 34 | +// ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled, |
| 35 | +// AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified, |
| 36 | +// EnclaveAttestationUrl = "" |
| 37 | +// }; |
| 38 | +// using SqlConnection sqlConnection = new (builder.ConnectionString); |
| 39 | + |
| 40 | +// sqlConnection.Open(); |
| 41 | +// Customer customer = new(45, "Microsoft", "Corporation"); |
| 42 | + |
| 43 | +// // Start a transaction and either commit or rollback based on the test variation. |
| 44 | +// using (SqlTransaction sqlTransaction = sqlConnection.BeginTransaction()) |
| 45 | +// { |
| 46 | +// DatabaseHelper.InsertCustomerData(sqlConnection, sqlTransaction, _akvTableName, customer); |
| 47 | +// sqlTransaction.Commit(); |
| 48 | +// } |
| 49 | + |
| 50 | +// // Test INPUT parameter on an encrypted parameter |
| 51 | +// using SqlCommand sqlCommand = new ($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName", |
| 52 | +// sqlConnection); |
| 53 | +// SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft"); |
| 54 | +// customerFirstParam.Direction = System.Data.ParameterDirection.Input; |
| 55 | +// customerFirstParam.ForceColumnEncryption = true; |
| 56 | + |
| 57 | +// using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); |
| 58 | +// DatabaseHelper.ValidateResultSet(sqlDataReader); |
| 59 | +// } |
| 60 | + |
| 61 | +// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsAKVSetupAvailable))] |
| 62 | +// [PlatformSpecific(TestPlatforms.Windows)] |
| 63 | +// public void TestRoundTripWithAKVAndCertStoreProvider() |
| 64 | +// { |
| 65 | +// using SQLSetupStrategyCertStoreProvider certStoreFixture = new (); |
| 66 | +// byte[] plainTextColumnEncryptionKey = ColumnEncryptionKey.GenerateRandomBytes(ColumnEncryptionKey.KeySizeInBytes); |
| 67 | +// byte[] encryptedColumnEncryptionKeyUsingAKV = _fixture.AkvStoreProvider.EncryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", plainTextColumnEncryptionKey); |
| 68 | +// byte[] columnEncryptionKeyReturnedAKV2Cert = certStoreFixture.CertStoreProvider.DecryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingAKV); |
| 69 | +// Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedAKV2Cert), @"Roundtrip failed"); |
| 70 | + |
| 71 | +// // Try the opposite. |
| 72 | +// byte[] encryptedColumnEncryptionKeyUsingCert = certStoreFixture.CertStoreProvider.EncryptColumnEncryptionKey(certStoreFixture.CspColumnMasterKey.KeyPath, @"RSA_OAEP", plainTextColumnEncryptionKey); |
| 73 | +// byte[] columnEncryptionKeyReturnedCert2AKV = _fixture.AkvStoreProvider.DecryptColumnEncryptionKey(DataTestUtility.AKVUrl, @"RSA_OAEP", encryptedColumnEncryptionKeyUsingCert); |
| 74 | +// Assert.True(plainTextColumnEncryptionKey.SequenceEqual(columnEncryptionKeyReturnedCert2AKV), @"Roundtrip failed"); |
| 75 | +// } |
| 76 | + |
| 77 | +// [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringSetupForAE), nameof(DataTestUtility.IsAKVSetupAvailable))] |
| 78 | +// public void TestLocalCekCacheIsScopedToProvider() |
| 79 | +// { |
| 80 | +// SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionStringHGSVBS) |
| 81 | +// { |
| 82 | +// ColumnEncryptionSetting = SqlConnectionColumnEncryptionSetting.Enabled, |
| 83 | +// AttestationProtocol = SqlConnectionAttestationProtocol.NotSpecified, |
| 84 | +// EnclaveAttestationUrl = "" |
| 85 | +// }; |
| 86 | + |
| 87 | +// using SqlConnection sqlConnection = new(builder.ConnectionString); |
| 88 | + |
| 89 | +// sqlConnection.Open(); |
| 90 | + |
| 91 | +// // Test INPUT parameter on an encrypted parameter |
| 92 | +// using SqlCommand sqlCommand = new($"SELECT CustomerId, FirstName, LastName FROM [{_akvTableName}] WHERE FirstName = @firstName", |
| 93 | +// sqlConnection); |
| 94 | +// SqlParameter customerFirstParam = sqlCommand.Parameters.AddWithValue(@"firstName", @"Microsoft"); |
| 95 | +// customerFirstParam.Direction = System.Data.ParameterDirection.Input; |
| 96 | +// customerFirstParam.ForceColumnEncryption = true; |
| 97 | + |
| 98 | +// SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); |
| 99 | +// sqlDataReader.Close(); |
| 100 | + |
| 101 | +// SqlColumnEncryptionAzureKeyVaultProvider sqlColumnEncryptionAzureKeyVaultProvider = |
| 102 | +// new(new SqlClientCustomTokenCredential()); |
| 103 | + |
| 104 | +// Dictionary<string, SqlColumnEncryptionKeyStoreProvider> customProvider = new() |
| 105 | +// { |
| 106 | +// { SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, sqlColumnEncryptionAzureKeyVaultProvider } |
| 107 | +// }; |
| 108 | + |
| 109 | +// // execute a query using provider from command-level cache. this will cache the cek in the local cek cache |
| 110 | +// sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider); |
| 111 | +// SqlDataReader sqlDataReader2 = sqlCommand.ExecuteReader(); |
| 112 | +// sqlDataReader2.Close(); |
| 113 | + |
| 114 | +// // global cek cache and local cek cache are populated above |
| 115 | +// // when using a new per-command provider, it will only use its local cek cache |
| 116 | +// // the following query should fail due to an empty cek cache and invalid credentials |
| 117 | +// customProvider[SqlColumnEncryptionAzureKeyVaultProvider.ProviderName] = |
| 118 | +// new SqlColumnEncryptionAzureKeyVaultProvider(new ClientSecretCredential("tenant", "client", "secret")); |
| 119 | +// sqlCommand.RegisterColumnEncryptionKeyStoreProvidersOnCommand(customProvider); |
| 120 | +// Exception ex = Assert.Throws<SqlException>(() => sqlCommand.ExecuteReader()); |
| 121 | +// Assert.StartsWith("The current credential is not configured to acquire tokens for tenant", ex.InnerException.Message); |
| 122 | +// } |
| 123 | +// } |
| 124 | +//} |
0 commit comments