-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathColumnDecryptErrorTests.cs
More file actions
170 lines (138 loc) · 7.77 KB
/
ColumnDecryptErrorTests.cs
File metadata and controls
170 lines (138 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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;
using System.Collections.Generic;
using Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted.Setup;
using Xunit;
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
{
[Trait("Set", "AE")]
public sealed class ColumnDecryptErrorTests : IClassFixture<SQLSetupStrategyAzureKeyVault>, IDisposable
{
private SQLSetupStrategyAzureKeyVault fixture;
private readonly string tableName;
public ColumnDecryptErrorTests(SQLSetupStrategyAzureKeyVault context)
{
fixture = context;
tableName = fixture.ColumnDecryptErrorTestTable.Name;
}
/*
* This test ensures that column decryption errors and connection pooling play nicely together.
* When a decryption error is encountered, we expect the connection to be drained of data and
* properly reset before being returned to the pool. If this doesn't happen, then random bytes
* may be left in the connection's state. These can interfere with the next operation that utilizes
* the connection.
*
* We test that state is properly reset by triggering the same error condition twice. Routing column key discovery
* away from AKV toward a dummy key store achieves this. Each connection pulls from a pool of max
* size one to ensure we are using the same internal connection/socket both times. We expect to
* receive the "Failed to decrypt column" exception twice. If the state were not cleaned properly,
* the second error would be different because the TDS stream would be unintelligible.
*
* Finally, we assert that restoring the connection to AKV allows a successful query.
*/
[ConditionalTheory(typeof(DataTestUtility), nameof(DataTestUtility.IsTargetReadyForAeWithKeyStore), nameof(DataTestUtility.IsAKVSetupAvailable))]
[ClassData(typeof(TestQueries))]
public void TestCleanConnectionAfterDecryptFail(string connString, string selectQuery, int totalColumnsInSelect, string[] types)
{
// Arrange
Assert.False(string.IsNullOrWhiteSpace(selectQuery), "FAILED: select query should not be null or empty.");
Assert.True(totalColumnsInSelect <= 3, "FAILED: totalColumnsInSelect should <= 3.");
using (SqlConnection sqlConnection = new SqlConnection(connString))
{
sqlConnection.Open();
Table.DeleteData(tableName, sqlConnection);
Customer customer = new Customer(
45,
"Microsoft",
"Corporation");
DatabaseHelper.InsertCustomerData(sqlConnection, null, tableName, customer);
}
// Act - Trigger a column decrypt error on the connection
Dictionary<String, SqlColumnEncryptionKeyStoreProvider> keyStoreProviders = new()
{
{ "AZURE_KEY_VAULT", new DummyKeyStoreProvider() }
};
String poolEnabledConnString = new SqlConnectionStringBuilder(connString) { Pooling = true, MaxPoolSize = 1 }.ToString();
using (SqlConnection sqlConnection = new SqlConnection(poolEnabledConnString))
{
sqlConnection.Open();
sqlConnection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(keyStoreProviders);
using SqlCommand sqlCommand = new SqlCommand(string.Format(selectQuery, tableName),
sqlConnection, null, SqlCommandColumnEncryptionSetting.Enabled);
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
Assert.True(sqlDataReader.HasRows, "FAILED: Select statement did not return any rows.");
while (sqlDataReader.Read())
{
var error = Assert.Throws<SqlException>(() => DatabaseHelper.CompareResults(sqlDataReader, types, totalColumnsInSelect));
Assert.Contains("Failed to decrypt column", error.Message);
}
}
// Assert
using (SqlConnection sqlConnection = new SqlConnection(poolEnabledConnString))
{
sqlConnection.Open();
sqlConnection.RegisterColumnEncryptionKeyStoreProvidersOnConnection(keyStoreProviders);
using SqlCommand sqlCommand = new SqlCommand(string.Format(selectQuery, tableName),
sqlConnection, null, SqlCommandColumnEncryptionSetting.Enabled);
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
Assert.True(sqlDataReader.HasRows, "FAILED: Select statement did not return any rows.");
while (sqlDataReader.Read())
{
var error = Assert.Throws<SqlException>(() => DatabaseHelper.CompareResults(sqlDataReader, types, totalColumnsInSelect));
Assert.Contains("Failed to decrypt column", error.Message);
}
}
using (SqlConnection sqlConnection = new SqlConnection(poolEnabledConnString))
{
sqlConnection.Open();
using SqlCommand sqlCommand = new SqlCommand(string.Format(selectQuery, tableName),
sqlConnection, null, SqlCommandColumnEncryptionSetting.Enabled);
using SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
Assert.True(sqlDataReader.HasRows, "FAILED: Select statement did not return any rows.");
while (sqlDataReader.Read())
{
DatabaseHelper.CompareResults(sqlDataReader, types, totalColumnsInSelect);
}
}
}
public void Dispose()
{
foreach (string connStrAE in DataTestUtility.AEConnStringsSetup)
{
using (SqlConnection sqlConnection = new SqlConnection(connStrAE))
{
sqlConnection.Open();
Table.DeleteData(fixture.ColumnDecryptErrorTestTable.Name, sqlConnection);
}
}
}
private sealed class DummyKeyStoreProvider : SqlColumnEncryptionKeyStoreProvider
{
public override byte[] DecryptColumnEncryptionKey(string masterKeyPath, string encryptionAlgorithm, byte[] encryptedColumnEncryptionKey)
{
// Must be 32 to match the key length expected for the 'AEAD_AES_256_CBC_HMAC_SHA256' algorithm
return new byte[32];
}
public override byte[] EncryptColumnEncryptionKey(string masterKeyPath, string encryptionAlgorithm, byte[] columnEncryptionKey)
{
return new byte[32];
}
}
}
public class TestQueries : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
foreach (string connStrAE in DataTestUtility.AEConnStrings)
{
yield return new object[] { connStrAE, @"select CustomerId, FirstName, LastName from [{0}] ", 3, new string[] { @"int", @"string", @"string" } };
yield return new object[] { connStrAE, @"select CustomerId, FirstName from [{0}] ", 2, new string[] { @"int", @"string" } };
yield return new object[] { connStrAE, @"select LastName from [{0}] ", 1, new string[] { @"string" } };
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}