Skip to content

Commit 7eef910

Browse files
committed
Code review changes
* Reorder properties and constructors * Move AEConnectionStringProviderWithCspParameters to its own file * Tweak to the AKV token acquisition
1 parent a935c62 commit 7eef910

File tree

10 files changed

+69
-64
lines changed

10 files changed

+69
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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;
7+
using System.Collections.Generic;
8+
using System.Security.Cryptography;
9+
10+
namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
11+
{
12+
public class AEConnectionStringProviderWithCspParameters : IEnumerable<object[]>
13+
{
14+
public IEnumerator<object[]> GetEnumerator()
15+
{
16+
const string ProvidersRegistryKeyPath = @"SOFTWARE\Microsoft\Cryptography\Defaults\Provider";
17+
using Microsoft.Win32.RegistryKey defaultCryptoProvidersRegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(ProvidersRegistryKeyPath);
18+
19+
foreach (string subKeyName in defaultCryptoProvidersRegistryKey.GetSubKeyNames())
20+
{
21+
CspParameters providerCspParameters;
22+
23+
// NOTE: RSACryptoServiceProvider.SignData() fails for other providers when testing locally
24+
if (!subKeyName.Contains(@"RSA and AES"))
25+
{
26+
continue;
27+
}
28+
29+
using (Microsoft.Win32.RegistryKey providerKey = defaultCryptoProvidersRegistryKey.OpenSubKey(subKeyName))
30+
{
31+
// Get Provider Name and its type
32+
string providerName = providerKey.Name.Substring(providerKey.Name.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
33+
int providerType = (int)providerKey.GetValue(@"Type");
34+
35+
providerCspParameters = new CspParameters(providerType, providerName);
36+
}
37+
38+
foreach (string connStrAE in DataTestUtility.AEConnStrings)
39+
{
40+
yield return new object[] { connStrAE, providerCspParameters };
41+
}
42+
}
43+
}
44+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
45+
}
46+
}

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/AzureKeyVaultKeyFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests.AlwaysEncrypted
88
{
99
public sealed class AzureKeyVaultKeyFixture : AzureKeyVaultKeyFixtureBase
1010
{
11-
public string GeneratedKeyUri { get; }
12-
1311
public AzureKeyVaultKeyFixture()
1412
: base(DataTestUtility.AKVBaseUri, DataTestUtility.GetTokenCredential())
1513
{
1614
GeneratedKeyUri = CreateKey(nameof(GeneratedKeyUri), 2048).ToString();
1715
}
16+
17+
public string GeneratedKeyUri { get; }
1818
}
1919
}

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/DatabaseHelper.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -342,39 +342,4 @@ public IEnumerator<object[]> GetEnumerator()
342342
}
343343
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
344344
}
345-
346-
public class AEConnectionStringProviderWithCspParameters : IEnumerable<object[]>
347-
{
348-
public IEnumerator<object[]> GetEnumerator()
349-
{
350-
const string ProvidersRegistryKeyPath = @"SOFTWARE\Microsoft\Cryptography\Defaults\Provider";
351-
using Microsoft.Win32.RegistryKey defaultCryptoProvidersRegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(ProvidersRegistryKeyPath);
352-
353-
foreach (string subKeyName in defaultCryptoProvidersRegistryKey.GetSubKeyNames())
354-
{
355-
CspParameters providerCspParameters;
356-
357-
// NOTE: RSACryptoServiceProvider.SignData() fails for other providers when testing locally
358-
if (!subKeyName.Contains(@"RSA and AES"))
359-
{
360-
continue;
361-
}
362-
363-
using (Microsoft.Win32.RegistryKey providerKey = defaultCryptoProvidersRegistryKey.OpenSubKey(subKeyName))
364-
{
365-
// Get Provider Name and its type
366-
string providerName = providerKey.Name.Substring(providerKey.Name.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
367-
int providerType = (int)providerKey.GetValue(@"Type");
368-
369-
providerCspParameters = new CspParameters(providerType, providerName);
370-
}
371-
372-
foreach (string connStrAE in DataTestUtility.AEConnStrings)
373-
{
374-
yield return new object[] { connStrAE, providerCspParameters };
375-
}
376-
}
377-
}
378-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
379-
}
380345
}

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/SQLSetupStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class SQLSetupStrategy : ColumnMasterKeyCertificateFixture
6161
public SQLSetupStrategy()
6262
: base(true)
6363
{
64-
ColumnMasterKeyPath = string.Concat(StoreLocation.CurrentUser.ToString(), "/", StoreName.My.ToString(), "/", ColumnMasterKeyCertificate.Thumbprint);
64+
ColumnMasterKeyPath = string.Format("{0}/{1}/{2}", StoreLocation.CurrentUser, StoreName.My, ColumnMasterKeyCertificate.Thumbprint);
6565
}
6666

6767
protected SQLSetupStrategy(string customKeyPath)

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestFixtures/SQLSetupStrategyCspProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public class SQLSetupStrategyCspProvider : SQLSetupStrategy
1515

1616
private readonly List<CspParameters> _cspKeyParameters = new List<CspParameters>();
1717

18-
public SqlColumnEncryptionCspProvider CspProvider { get; }
19-
2018
public SQLSetupStrategyCspProvider(CspParameters cspParameters)
2119
: base(cspParameters.ProviderName + "/" + cspParameters.KeyContainerName)
2220
{
@@ -32,6 +30,8 @@ public SQLSetupStrategyCspProvider(CspParameters cspParameters)
3230
SetupDatabase();
3331
}
3432

33+
public SqlColumnEncryptionCspProvider CspProvider { get; }
34+
3535
internal override void SetupDatabase()
3636
{
3737
ColumnMasterKey columnMasterKey = new CspProviderColumnMasterKey(GenerateUniqueName("CspExt"), SqlColumnEncryptionCspProvider.ProviderName, ColumnMasterKeyPath);

src/Microsoft.Data.SqlClient/tests/ManualTests/AlwaysEncrypted/TestTrustedMasterKeyPaths.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,14 @@ public void TestTrustedColumnEncryptionMasterKeyPathsWithMultipleServers(string
147147
}
148148

149149
// Add entries for one server
150-
List<string> server1TrustedKeyPaths = new List<string>();
151-
152-
// Add some random key paths
153-
foreach (char c in new char[] { 'A', 'B' })
150+
List<string> server1TrustedKeyPaths = new List<string>()
154151
{
155-
string invalidKeyPath = string.Format(@"{0}/my/{1}", StoreLocation.CurrentUser.ToString(), dummyThumbprint);
156-
server1TrustedKeyPaths.Add(invalidKeyPath);
157-
}
158-
159-
// Add the key path used by the test
160-
server1TrustedKeyPaths.Add(columnMasterKeyPath);
152+
// Add some random key paths
153+
string.Format(@"{0}/my/{1}", StoreLocation.CurrentUser.ToString(), dummyThumbprint),
154+
string.Format(@"{0}/my/{1}", StoreLocation.CurrentUser.ToString(), dummyThumbprint),
155+
// Add the key path used by the test
156+
columnMasterKeyPath
157+
};
161158

162159
// Add it to the dictionary
163160
SqlConnection.ColumnEncryptionTrustedMasterKeyPaths.Add(connBuilder.DataSource, server1TrustedKeyPaths);

src/Microsoft.Data.SqlClient/tests/ManualTests/DataCommon/SqlClientCustomTokenCredential.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ public class SqlClientCustomTokenCredential : TokenCredential
1818
private const string DEFAULT_PREFIX = "/.default";
1919
private const string AKVKeyName = "TestSqlClientAzureKeyVaultProvider";
2020

21-
private static string AKVUrl = (new Uri(DataTestUtility.AKVBaseUri, $"/keys/{AKVKeyName}")).AbsoluteUri;
22-
2321
string _authority = "";
2422
string _resource = "";
25-
string _akvUrl = "";
2623

2724
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) =>
2825
AcquireTokenAsync().GetAwaiter().GetResult();
@@ -34,10 +31,11 @@ private async Task<AccessToken> AcquireTokenAsync()
3431
{
3532
// Added to reduce HttpClient calls.
3633
// For multi-user support, a better design can be implemented as needed.
37-
if (_akvUrl != AKVUrl)
34+
if (string.IsNullOrEmpty(_authority) || string.IsNullOrEmpty(_resource))
3835
{
3936
using (HttpClient httpClient = new HttpClient())
4037
{
38+
string AKVUrl = (new Uri(DataTestUtility.AKVBaseUri, $"/keys/{AKVKeyName}")).AbsoluteUri;
4139
HttpResponseMessage response = await httpClient.GetAsync(AKVUrl);
4240
string challenge = response?.Headers.WwwAuthenticate.FirstOrDefault()?.ToString();
4341
string trimmedChallenge = ValidateChallenge(challenge);
@@ -69,8 +67,6 @@ private async Task<AccessToken> AcquireTokenAsync()
6967
}
7068
}
7169
}
72-
// Since this is a test, we only create single-instance temp cache
73-
_akvUrl = AKVUrl;
7470
}
7571

7672
AccessToken accessToken = await AzureActiveDirectoryAuthenticationCallback(_authority, _resource);

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="AlwaysEncrypted\SqlBulkCopyTruncation.cs" />
5050
<Compile Include="AlwaysEncrypted\SqlNullValues.cs" />
5151
<Compile Include="AlwaysEncrypted\TestFixtures\DatabaseHelper.cs" />
52+
<Compile Include="AlwaysEncrypted\TestFixtures\AEConnectionStringProviderWithCspParameters.cs" />
5253
<Compile Include="AlwaysEncrypted\TestFixtures\AzureKeyVaultKeyFixture.cs" />
5354
<Compile Include="AlwaysEncrypted\TestFixtures\SQLSetupStrategy.cs" />
5455
<Compile Include="AlwaysEncrypted\TestFixtures\SQLSetupStrategyCertStoreProvider.cs" />

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/ColumnMasterKeyCertificateFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Microsoft.Data.SqlClient.TestUtilities.Fixtures
99
{
1010
public class ColumnMasterKeyCertificateFixture : CertificateFixtureBase
1111
{
12-
public X509Certificate2 ColumnMasterKeyCertificate { get; }
13-
1412
public ColumnMasterKeyCertificateFixture()
1513
: this(true)
1614
{
1715
}
1816

17+
public X509Certificate2 ColumnMasterKeyCertificate { get; }
18+
1919
protected ColumnMasterKeyCertificateFixture(bool createCertificate)
2020
{
2121
if (createCertificate)

src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CspCertificateFixture.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ namespace Microsoft.Data.SqlClient.TestUtilities.Fixtures
1010
{
1111
public class CspCertificateFixture : CertificateFixtureBase
1212
{
13-
public X509Certificate2 CspCertificate { get; }
14-
15-
public string CspCertificatePath { get; }
16-
17-
public string CspKeyPath { get; }
18-
1913
public CspCertificateFixture()
2014
{
2115
CspCertificate = CreateCertificate(nameof(CspCertificate), Array.Empty<string>(), Array.Empty<string>(), true);
@@ -26,6 +20,12 @@ public CspCertificateFixture()
2620
CspKeyPath = GetCspPathFromCertificate();
2721
}
2822

23+
public X509Certificate2 CspCertificate { get; }
24+
25+
public string CspCertificatePath { get; }
26+
27+
public string CspKeyPath { get; }
28+
2929
private string GetCspPathFromCertificate()
3030
{
3131
RSA privateKey = CspCertificate.GetRSAPrivateKey();

0 commit comments

Comments
 (0)