Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Security.Cryptography;
using Azure.Core;
using Azure.Security.KeyVault.Keys;

Expand All @@ -18,20 +19,41 @@ namespace Microsoft.Data.SqlClient.Tests.Common.Fixtures;
public abstract class AzureKeyVaultKeyFixtureBase : IDisposable
{
private readonly KeyClient _keyClient;
private readonly Random _randomGenerator;
private readonly RandomNumberGenerator _randomGenerator;

private readonly List<KeyVaultKey> _createdKeys = new List<KeyVaultKey>();

protected AzureKeyVaultKeyFixtureBase(Uri keyVaultUri, TokenCredential keyVaultToken)
{
_keyClient = new KeyClient(keyVaultUri, keyVaultToken);
_randomGenerator = new Random();
_randomGenerator = RandomNumberGenerator.Create();
}

protected Uri CreateKey(string name, int keySize)
{
CreateRsaKeyOptions createOptions = new CreateRsaKeyOptions(GenerateUniqueName(name)) { KeySize = keySize };
KeyVaultKey created = _keyClient.CreateRsaKey(createOptions);
const int MaxConflictResolutions = 5;
KeyVaultKey created;
int i = 0;

while (true)
{
CreateRsaKeyOptions createOptions = new CreateRsaKeyOptions(GenerateUniqueName(name)) { KeySize = keySize };

try
{
created = _keyClient.CreateRsaKey(createOptions);
break;
}
// It's possible for a key to already exist with the same name, even in a deleted state. If so, CreateRsaKey
// will throw an exception with HTTP status code 409 (Conflict.)
// We can't assume we possess permissions to purge or to recover the key, so regenerate the name and try again.
// Only make MaxConflictResolutions attempts, to avoid possible infinite loops.
catch (Azure.RequestFailedException conflictException)
when (conflictException.Status == 409 && i < MaxConflictResolutions)
{
i++;
}
}

_createdKeys.Add(created);
return created.Id;
Expand All @@ -41,7 +63,7 @@ private string GenerateUniqueName(string name)
{
byte[] rndBytes = new byte[16];

_randomGenerator.NextBytes(rndBytes);
_randomGenerator.GetBytes(rndBytes);
return name + "-" + BitConverter.ToString(rndBytes);
}

Expand All @@ -64,5 +86,7 @@ protected virtual void Dispose(bool disposing)
continue;
}
}

_randomGenerator.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public void Dispose()

#region Switch Value Getters and Setters

// These properties get or set the like-named underlying switch field value.
// These properties get the like-named underlying switch *property* value and set the underlying
// switch *field* value. This allows tests to verify the default switch values.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getter and setter of these properties isn't identical - we read from them to get the values of the AppContext switches (including any default values/dependencies between properties.) I'm not completely happy with this, but the alternative would be for call sites to instantiate LocalAppContextSwitchesHelper, only to leave it untouched and read property values directly from LocalAppContextSwitches. Doing so doesn't strike me as an obvious way to use the type.

//
// They all throw if the value cannot be retrieved or set.

Expand All @@ -218,7 +219,7 @@ public void Dispose()
/// </summary>
public bool? DisableTnirByDefault
{
get => GetSwitchValue("s_disableTnirByDefault");
get => GetSwitchPropertyValue(nameof(DisableTnirByDefault));
set => SetSwitchValue("s_disableTnirByDefault", value);
}
#endif
Expand All @@ -228,7 +229,7 @@ public bool? DisableTnirByDefault
/// </summary>
public bool? EnableMultiSubnetFailoverByDefault
{
get => GetSwitchValue("s_enableMultiSubnetFailoverByDefault");
get => GetSwitchPropertyValue(nameof(EnableMultiSubnetFailoverByDefault));
set => SetSwitchValue("s_enableMultiSubnetFailoverByDefault", value);
}

Expand All @@ -238,7 +239,7 @@ public bool? EnableMultiSubnetFailoverByDefault
/// </summary>
public bool? GlobalizationInvariantMode
{
get => GetSwitchValue("s_globalizationInvariantMode");
get => GetSwitchPropertyValue(nameof(GlobalizationInvariantMode));
set => SetSwitchValue("s_globalizationInvariantMode", value);
}
#endif
Expand All @@ -248,7 +249,7 @@ public bool? GlobalizationInvariantMode
/// </summary>
public bool? IgnoreServerProvidedFailoverPartner
{
get => GetSwitchValue("s_ignoreServerProvidedFailoverPartner");
get => GetSwitchPropertyValue(nameof(IgnoreServerProvidedFailoverPartner));
set => SetSwitchValue("s_ignoreServerProvidedFailoverPartner", value);
}

Expand All @@ -257,7 +258,7 @@ public bool? IgnoreServerProvidedFailoverPartner
/// </summary>
public bool? UseLegacyFailoverAlternationOnLoginSqlErrors
{
get => GetSwitchValue("s_useLegacyFailoverAlternationOnLoginSqlErrors");
get => GetSwitchPropertyValue(nameof(UseLegacyFailoverAlternationOnLoginSqlErrors));
set => SetSwitchValue("s_useLegacyFailoverAlternationOnLoginSqlErrors", value);
}

Expand All @@ -266,7 +267,7 @@ public bool? UseLegacyFailoverAlternationOnLoginSqlErrors
/// </summary>
public bool? LegacyRowVersionNullBehavior
{
get => GetSwitchValue("s_legacyRowVersionNullBehavior");
get => GetSwitchPropertyValue(nameof(LegacyRowVersionNullBehavior));
set => SetSwitchValue("s_legacyRowVersionNullBehavior", value);
}

Expand All @@ -275,7 +276,7 @@ public bool? LegacyRowVersionNullBehavior
/// </summary>
public bool? LegacyVarTimeZeroScaleBehaviour
{
get => GetSwitchValue("s_legacyVarTimeZeroScaleBehaviour");
get => GetSwitchPropertyValue(nameof(LegacyVarTimeZeroScaleBehaviour));
set => SetSwitchValue("s_legacyVarTimeZeroScaleBehaviour", value);
}

Expand All @@ -284,7 +285,7 @@ public bool? LegacyVarTimeZeroScaleBehaviour
/// </summary>
public bool? MakeReadAsyncBlocking
{
get => GetSwitchValue("s_makeReadAsyncBlocking");
get => GetSwitchPropertyValue(nameof(MakeReadAsyncBlocking));
set => SetSwitchValue("s_makeReadAsyncBlocking", value);
}

Expand All @@ -293,7 +294,7 @@ public bool? MakeReadAsyncBlocking
/// </summary>
public bool? SuppressInsecureTlsWarning
{
get => GetSwitchValue("s_suppressInsecureTlsWarning");
get => GetSwitchPropertyValue(nameof(SuppressInsecureTlsWarning));
set => SetSwitchValue("s_suppressInsecureTlsWarning", value);
}

Expand All @@ -302,7 +303,7 @@ public bool? SuppressInsecureTlsWarning
/// </summary>
public bool? TruncateScaledDecimal
{
get => GetSwitchValue("s_truncateScaledDecimal");
get => GetSwitchPropertyValue(nameof(TruncateScaledDecimal));
set => SetSwitchValue("s_truncateScaledDecimal", value);
}

Expand All @@ -311,7 +312,7 @@ public bool? TruncateScaledDecimal
/// </summary>
public bool? UseCompatibilityAsyncBehaviour
{
get => GetSwitchValue("s_useCompatibilityAsyncBehaviour");
get => GetSwitchPropertyValue(nameof(UseCompatibilityAsyncBehaviour));
set => SetSwitchValue("s_useCompatibilityAsyncBehaviour", value);
}

Expand All @@ -320,7 +321,7 @@ public bool? UseCompatibilityAsyncBehaviour
/// </summary>
public bool? UseCompatibilityProcessSni
{
get => GetSwitchValue("s_useCompatibilityProcessSni");
get => GetSwitchPropertyValue(nameof(UseCompatibilityProcessSni));
set => SetSwitchValue("s_useCompatibilityProcessSni", value);
}

Expand All @@ -329,7 +330,7 @@ public bool? UseCompatibilityProcessSni
/// </summary>
public bool? UseConnectionPoolV2
{
get => GetSwitchValue("s_useConnectionPoolV2");
get => GetSwitchPropertyValue(nameof(UseConnectionPoolV2));
set => SetSwitchValue("s_useConnectionPoolV2", value);
}

Expand All @@ -338,7 +339,7 @@ public bool? UseConnectionPoolV2
/// </summary>
public bool? UseOverallConnectTimeoutForPoolWait
{
get => GetSwitchValue("s_useOverallConnectTimeoutForPoolWait");
get => GetSwitchPropertyValue(nameof(UseOverallConnectTimeoutForPoolWait));
set => SetSwitchValue("s_useOverallConnectTimeoutForPoolWait", value);
}

Expand All @@ -348,7 +349,7 @@ public bool? UseOverallConnectTimeoutForPoolWait
/// </summary>
public bool? UseManagedNetworking
{
get => GetSwitchValue("s_useManagedNetworking");
get => GetSwitchPropertyValue(nameof(UseManagedNetworking));
set => SetSwitchValue("s_useManagedNetworking", value);
}
#endif
Expand All @@ -358,7 +359,7 @@ public bool? UseManagedNetworking
/// </summary>
public bool? UseMinimumLoginTimeout
{
get => GetSwitchValue("s_useMinimumLoginTimeout");
get => GetSwitchPropertyValue(nameof(UseMinimumLoginTimeout));
set => SetSwitchValue("s_useMinimumLoginTimeout", value);
}

Expand All @@ -371,19 +372,7 @@ public bool? UseMinimumLoginTimeout
/// </summary>
private static bool? GetSwitchValue(string fieldName)
{
var assembly = Assembly.GetAssembly(typeof(SqlConnection));
if (assembly is null)
{
throw new InvalidOperationException(
"Could not get assembly for Microsoft.Data.SqlClient");
}

var type = assembly.GetType("Microsoft.Data.SqlClient.LocalAppContextSwitches");
if (type is null)
{
throw new InvalidOperationException(
"Could not get type LocalAppContextSwitches");
}
var type = GetLocalAppContextSwitchesType();

var field = type.GetField(
fieldName,
Expand Down Expand Up @@ -418,19 +407,7 @@ public bool? UseMinimumLoginTimeout
/// </summary>
private static void SetSwitchValue(string fieldName, bool? value)
{
var assembly = Assembly.GetAssembly(typeof(SqlConnection));
if (assembly is null)
{
throw new InvalidOperationException(
"Could not get assembly for Microsoft.Data.SqlClient");
}

var type = assembly.GetType("Microsoft.Data.SqlClient.LocalAppContextSwitches");
if (type is null)
{
throw new InvalidOperationException(
"Could not get type LocalAppContextSwitches");
}
var type = GetLocalAppContextSwitchesType();

var field = type.GetField(
fieldName,
Expand All @@ -455,5 +432,49 @@ private static void SetSwitchValue(string fieldName, bool? value)
field.SetValue(null, Enum.ToObject(field.FieldType, byteValue));
}

/// <summary>
/// Use reflection to get a switch property value from LocalAppContextSwitches.
/// </summary>
/// <remarks>
/// Each property in LocalAppContextSwitchHelper corresponds to a like-named property in
/// LocalAppContextSwitches, which may return a different value when the AppContext switch
/// has not been set.
/// </remarks>
private static bool GetSwitchPropertyValue(string propertyName)
{
var type = GetLocalAppContextSwitchesType();
var property = type.GetProperty(
propertyName,
BindingFlags.Static | BindingFlags.Public);

if (property == null)
{
throw new InvalidOperationException(
$"Property '{propertyName}' not found in LocalAppContextSwitches");
}

object? value = property.GetValue(null);

return value is bool boolValue
? boolValue
: throw new InvalidOperationException($"Property '{propertyName}' is not of type bool.");
}

private static Type GetLocalAppContextSwitchesType()
{
var assembly = Assembly.GetAssembly(typeof(SqlConnection));
if (assembly is null)
{
throw new InvalidOperationException("Could not get assembly for Microsoft.Data.SqlClient");
}

var type = assembly.GetType("Microsoft.Data.SqlClient.LocalAppContextSwitches");
if (type is null)
{
throw new InvalidOperationException("Could not get type LocalAppContextSwitches");
}
return type;
}

#endregion
}
Loading
Loading