Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix | Dropping reference to Microsoft.Win32.Registry in netcore. #1974

Merged
merged 5 commits into from Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -939,9 +939,6 @@
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectFactory.Managed.cs" />
<Compile Include="Microsoft\Data\SqlClient\TdsParser.Unix.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true' and '$(IsUAPAssembly)' != 'true'">
<Reference Include="Microsoft.Win32.Registry" />
</ItemGroup>
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(IsUAPAssembly)' == 'true'">
<Reference Include="System.Collections.NonGeneric" />
<Reference Include="System.Memory" />
Expand Down
Expand Up @@ -60,6 +60,7 @@ public static class DataTestUtility
public static readonly bool IsDNSCachingSupportedCR = false; // this is for the control ring
public static readonly bool IsDNSCachingSupportedTR = false; // this is for the tenant ring
public static readonly string UserManagedIdentityClientId = null;
public static readonly string AliasName = null;


public static readonly string EnclaveAzureDatabaseConnString = null;
Expand Down Expand Up @@ -117,6 +118,7 @@ static DataTestUtility()
KerberosDomainUser = c.KerberosDomainUser;
ManagedIdentitySupported = c.ManagedIdentitySupported;
IsManagedInstance = c.IsManagedInstance;
AliasName = c.AliasName;

System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;

Expand Down Expand Up @@ -300,6 +302,10 @@ public static bool AreConnStringsSetup()
return !string.IsNullOrEmpty(NPConnectionString) && !string.IsNullOrEmpty(TCPConnectionString);
}

public static bool IsSQLAliasSetup()
{
return !string.IsNullOrEmpty(AliasName);
}
public static bool IsTCPConnStringSetup()
{
return !string.IsNullOrEmpty(TCPConnectionString);
Expand Down
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Data;
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
Expand Down Expand Up @@ -369,7 +370,7 @@ public static void ConnectionOpenDisableRetry()
{
InitialCatalog = "DoesNotExist0982532435423",
Pooling = false,
ConnectTimeout=15
ConnectTimeout = 15
};
using SqlConnection sqlConnection = new(connectionStringBuilder.ConnectionString);
Stopwatch timer = new();
Expand All @@ -387,74 +388,24 @@ public static void ConnectionOpenDisableRetry()
Assert.True(duration.Seconds > 5, $"Connection Open() with retries took less time than expected. Expect > 5 sec with transient fault handling. Took {duration.Seconds} sec."); // sqlConnection.Open();
}

private const string ConnectToPath = "SOFTWARE\\Microsoft\\MSSQLServer\\Client\\ConnectTo";
private static bool CanCreateAliases()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
!DataTestUtility.IsTCPConnStringSetup())
{
return false;
}

using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
return false;
}
}

using RegistryKey key = Registry.LocalMachine.OpenSubKey(ConnectToPath, true);
if (key == null)
{
// Registry not writable
return false;
}

SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString);
if (!DataTestUtility.ParseDataSource(b.DataSource, out string hostname, out int port, out string instanceName) ||
!string.IsNullOrEmpty(instanceName))
{
return false;
}

return true;
}

[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(nameof(CanCreateAliases))]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsSQLAliasSetup))]
public static void ConnectionAliasTest()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new Exception("Alias test only valid on Windows");
}

if (!CanCreateAliases())
{
throw new Exception("Unable to create aliases in this environment. Windows + Admin + non-instance data source required.");
}

SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString);
if (!DataTestUtility.ParseDataSource(b.DataSource, out string hostname, out int port, out string instanceName) ||
!string.IsNullOrEmpty(instanceName))
SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString)
{
// Only works with connection strings that parse successfully and don't include an instance name
throw new Exception("Unable to create aliases in this configuration. Parsable data source without instance required.");
}

b.DataSource = "TESTALIAS-" + Guid.NewGuid().ToString().Replace("-", "");
using RegistryKey key = Registry.LocalMachine.OpenSubKey(ConnectToPath, true);
key.SetValue(b.DataSource, "DBMSSOCN," + hostname + "," + (port == -1 ? 1433 : port));
DataSource = DataTestUtility.AliasName
};
using SqlConnection sqlConnection = new(builder.ConnectionString);
Assert.Equal(DataTestUtility.AliasName, builder.DataSource);
try
{
using SqlConnection sqlConnection = new(b.ConnectionString);
sqlConnection.Open();
Assert.Equal(ConnectionState.Open, sqlConnection.State);
}
finally
catch (SqlException ex)
{
key.DeleteValue(b.DataSource);
Assert.Fail(ex.Message);
}
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ public class Config
public string KerberosDomainPassword = null;
public string KerberosDomainUser = null;
public bool IsManagedInstance = false;

public string AliasName = null;
public static Config Load(string configPath = @"config.json")
{
try
Expand Down
Expand Up @@ -30,5 +30,6 @@
"EnclaveAzureDatabaseConnString": "",
"ManagedIdentitySupported": true,
"UserManagedIdentityClientId": "",
"PowerShellPath": ""
"PowerShellPath": "",
"AliasName": ""
}