Skip to content

Commit 3082ae5

Browse files
Add support for aliases on netcore+Windows (#1588)
* Add support for aliases on netcore+Windows * Add test * Revert spacing * Better partition Windows/Unix code * Add missing files * Fix attempt * Re-work new test * Applying review suggestions * Deduplicate code Co-authored-by: DavoudEshtehari <[email protected]>
1 parent c45c507 commit 3082ae5

File tree

11 files changed

+330
-125
lines changed

11 files changed

+330
-125
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+10-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
<Compile Include="..\..\src\Microsoft\Data\SqlClient\RowsCopiedEventHandler.cs">
173173
<Link>Microsoft\Data\SqlClient\RowsCopiedEventHandler.cs</Link>
174174
</Compile>
175-
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlSequentialTextReader.cs">
175+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlSequentialTextReader.cs">
176176
<Link>Microsoft\Data\SqlClient\SqlSequentialTextReader.cs</Link>
177177
</Compile>
178178
<Compile Include="..\..\src\Microsoft\Data\SqlClient\Server\ExtendedClrTypeCode.cs">
@@ -481,6 +481,9 @@
481481
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
482482
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
483483
</Compile>
484+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
485+
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
486+
</Compile>
484487
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
485488
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
486489
</Compile>
@@ -656,10 +659,12 @@
656659
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
657660
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
658661
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObjectManaged.cs" />
659-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStaticMethods.cs" />
660662
</ItemGroup>
661663
<!-- Windows only -->
662664
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
665+
<Compile Include="..\..\src\Microsoft\Data\Common\AdapterUtil.Windows.cs">
666+
<Link>Microsoft\Data\Common\AdapterUtil.Windows.cs</Link>
667+
</Compile>
663668
<Compile Include="..\..\src\Microsoft\Data\Sql\SqlDataSourceEnumeratorNativeHelper.cs">
664669
<Link>Microsoft\Data\Sql\SqlDataSourceEnumeratorNativeHelper.cs</Link>
665670
</Compile>
@@ -672,6 +677,9 @@
672677
</ItemGroup>
673678
<!-- Unix only -->
674679
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
680+
<Compile Include="..\..\src\Microsoft\Data\Common\AdapterUtil.Unix.cs">
681+
<Link>Microsoft\Data\Common\AdapterUtil.Unix.cs</Link>
682+
</Compile>
675683
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCertificateStoreProvider.Unix.cs" />
676684
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCngProvider.Unix.cs" />
677685
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.Unix.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

+30-5
Original file line numberDiff line numberDiff line change
@@ -1856,11 +1856,36 @@ private void ResolveExtendedServerName(ServerInfo serverInfo, bool aliasLookup,
18561856
string host = serverInfo.UserServerName;
18571857
string protocol = serverInfo.UserProtocol;
18581858

1859-
//TODO: fix local host enforcement with datadirectory and failover
1860-
if (options.EnforceLocalHost)
1861-
{
1862-
// verify LocalHost for |DataDirectory| usage
1863-
SqlConnectionString.VerifyLocalHostAndFixup(ref host, true, true /*fix-up to "."*/);
1859+
if (aliasLookup)
1860+
{ // We skip this for UserInstances...
1861+
// Perform registry lookup to see if host is an alias. It will appropriately set host and protocol, if an Alias.
1862+
// Check if it was already resolved, during CR reconnection _currentSessionData values will be copied from
1863+
// _reconnectSessonData of the previous connection
1864+
if (_currentSessionData != null && !string.IsNullOrEmpty(host))
1865+
{
1866+
Tuple<string, string> hostPortPair;
1867+
if (_currentSessionData._resolvedAliases.TryGetValue(host, out hostPortPair))
1868+
{
1869+
host = hostPortPair.Item1;
1870+
protocol = hostPortPair.Item2;
1871+
}
1872+
else
1873+
{
1874+
TdsParserStaticMethods.AliasRegistryLookup(ref host, ref protocol);
1875+
_currentSessionData._resolvedAliases.Add(serverInfo.UserServerName, new Tuple<string, string>(host, protocol));
1876+
}
1877+
}
1878+
else
1879+
{
1880+
TdsParserStaticMethods.AliasRegistryLookup(ref host, ref protocol);
1881+
}
1882+
1883+
//TODO: fix local host enforcement with datadirectory and failover
1884+
if (options.EnforceLocalHost)
1885+
{
1886+
// verify LocalHost for |DataDirectory| usage
1887+
SqlConnectionString.VerifyLocalHostAndFixup(ref host, true, true /*fix-up to "."*/);
1888+
}
18641889
}
18651890

18661891
serverInfo.SetDerivedNames(protocol, host);

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595
<Compile Include="..\..\src\Microsoft\Data\Common\AdapterUtil.cs">
9696
<Link>Microsoft\Data\Common\AdapterUtil.cs</Link>
9797
</Compile>
98+
<Compile Include="..\..\src\Microsoft\Data\Common\AdapterUtil.Windows.cs">
99+
<Link>Microsoft\Data\Common\AdapterUtil.Windows.cs</Link>
100+
</Compile>
98101
<Compile Include="..\..\src\Microsoft\Data\Common\DbConnectionStringCommon.cs">
99102
<Link>Microsoft\Data\Common\DbConnectionStringCommon.cs</Link>
100103
</Compile>
@@ -545,6 +548,9 @@
545548
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParameterSetter.cs">
546549
<Link>Microsoft\Data\SqlClient\TdsParameterSetter.cs</Link>
547550
</Compile>
551+
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsParserStaticMethods.cs">
552+
<Link>Microsoft\Data\SqlClient\TdsParserStaticMethods.cs</Link>
553+
</Compile>
548554
<Compile Include="..\..\src\Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs">
549555
<Link>Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs</Link>
550556
</Compile>
@@ -639,7 +645,6 @@
639645
<Compile Include="Microsoft\Data\SqlClient\TdsParserSafeHandles.cs" />
640646
<Compile Include="Microsoft\Data\SqlClient\TdsParserSessionPool.cs" />
641647
<Compile Include="Microsoft\Data\SqlClient\TdsParserStateObject.cs" />
642-
<Compile Include="Microsoft\Data\SqlClient\TdsParserStaticMethods.cs" />
643648
<Compile Include="Microsoft\Data\SqlTypes\SqlFileStream.cs" />
644649
<Compile Include="Microsoft\Data\SqlTypes\SqlStreamChars.cs" />
645650
<Compile Include="Microsoft\Data\SqlTypes\UnsafeNativeMethods.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
namespace Microsoft.Data.Common
6+
{
7+
/// <summary>
8+
/// The class ADP defines the exceptions that are specific to the Adapters.
9+
/// The class contains functions that take the proper informational variables and then construct
10+
/// the appropriate exception with an error string obtained from the resource framework.
11+
/// The exception is then returned to the caller, so that the caller may then throw from its
12+
/// location so that the catcher of the exception will have the appropriate call stack.
13+
/// This class is used so that there will be compile time checking of error messages.
14+
/// The resource Framework.txt will ensure proper string text based on the appropriate locale.
15+
/// </summary>
16+
internal static partial class ADP
17+
{
18+
internal static object LocalMachineRegistryValue(string subkey, string queryvalue)
19+
{
20+
// No registry in non-Windows environments
21+
return null;
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.Runtime.InteropServices;
6+
using System.Runtime.Versioning;
7+
using System.Security;
8+
using System.Security.Permissions;
9+
using Microsoft.Win32;
10+
11+
namespace Microsoft.Data.Common
12+
{
13+
/// <summary>
14+
/// The class ADP defines the exceptions that are specific to the Adapters.
15+
/// The class contains functions that take the proper informational variables and then construct
16+
/// the appropriate exception with an error string obtained from the resource framework.
17+
/// The exception is then returned to the caller, so that the caller may then throw from its
18+
/// location so that the catcher of the exception will have the appropriate call stack.
19+
/// This class is used so that there will be compile time checking of error messages.
20+
/// The resource Framework.txt will ensure proper string text based on the appropriate locale.
21+
/// </summary>
22+
internal static partial class ADP
23+
{
24+
[ResourceExposure(ResourceScope.Machine)]
25+
[ResourceConsumption(ResourceScope.Machine)]
26+
internal static object LocalMachineRegistryValue(string subkey, string queryvalue)
27+
{ // MDAC 77697
28+
(new RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\\" + subkey)).Assert(); // MDAC 62028
29+
try
30+
{
31+
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subkey, false))
32+
{
33+
return key?.GetValue(queryvalue);
34+
}
35+
}
36+
catch (SecurityException e)
37+
{
38+
// Even though we assert permission - it's possible there are
39+
// ACL's on registry that cause SecurityException to be thrown.
40+
ADP.TraceExceptionWithoutRethrow(e);
41+
return null;
42+
}
43+
finally
44+
{
45+
RegistryPermission.RevertAssert();
46+
}
47+
}
48+
}
49+
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs

+1-26
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Microsoft.Data.Common
4343
/// This class is used so that there will be compile time checking of error messages.
4444
/// The resource Framework.txt will ensure proper string text based on the appropriate locale.
4545
/// </summary>
46-
internal static class ADP
46+
internal static partial class ADP
4747
{
4848
// NOTE: Initializing a Task in SQL CLR requires the "UNSAFE" permission set (http://msdn.microsoft.com/en-us/library/ms172338.aspx)
4949
// Therefore we are lazily initializing these Tasks to avoid forcing customers to use the "UNSAFE" set when they are actually using no Async features
@@ -1478,31 +1478,6 @@ internal static string GetComputerNameDnsFullyQualified()
14781478
return value;
14791479
}
14801480

1481-
[ResourceExposure(ResourceScope.Machine)]
1482-
[ResourceConsumption(ResourceScope.Machine)]
1483-
internal static object LocalMachineRegistryValue(string subkey, string queryvalue)
1484-
{ // MDAC 77697
1485-
(new RegistryPermission(RegistryPermissionAccess.Read, "HKEY_LOCAL_MACHINE\\" + subkey)).Assert(); // MDAC 62028
1486-
try
1487-
{
1488-
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(subkey, false))
1489-
{
1490-
return key?.GetValue(queryvalue);
1491-
}
1492-
}
1493-
catch (SecurityException e)
1494-
{
1495-
// Even though we assert permission - it's possible there are
1496-
// ACL's on registry that cause SecurityException to be thrown.
1497-
ADP.TraceExceptionWithoutRethrow(e);
1498-
return null;
1499-
}
1500-
finally
1501-
{
1502-
RegistryPermission.RevertAssert();
1503-
}
1504-
}
1505-
15061481
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
15071482
internal static IntPtr IntPtrOffset(IntPtr pbase, int offset)
15081483
{

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionString.cs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1102,20 +1102,6 @@ internal PoolBlockingPeriod ConvertValueToPoolBlockingPeriod()
11021102
}
11031103
}
11041104

1105-
#if NETFRAMEWORK
1106-
protected internal override PermissionSet CreatePermissionSet()
1107-
{
1108-
PermissionSet permissionSet = new(PermissionState.None);
1109-
permissionSet.AddPermission(new SqlClientPermission(this));
1110-
return permissionSet;
1111-
}
1112-
1113-
internal bool ConvertValueToEncrypt()
1114-
{
1115-
bool defaultEncryptValue = !Parsetable.ContainsKey(KEY.Authentication) ? DEFAULT.Encrypt : true;
1116-
return ConvertValueToBoolean(KEY.Encrypt, defaultEncryptValue);
1117-
}
1118-
11191105
static internal Hashtable NetlibMapping()
11201106
{
11211107
const int NetLibCount = 8;
@@ -1166,6 +1152,21 @@ internal static class NETLIB
11661152
}
11671153

11681154
private static Hashtable s_netlibMapping;
1155+
1156+
#if NETFRAMEWORK
1157+
protected internal override PermissionSet CreatePermissionSet()
1158+
{
1159+
PermissionSet permissionSet = new(PermissionState.None);
1160+
permissionSet.AddPermission(new SqlClientPermission(this));
1161+
return permissionSet;
1162+
}
1163+
1164+
internal bool ConvertValueToEncrypt()
1165+
{
1166+
bool defaultEncryptValue = !Parsetable.ContainsKey(KEY.Authentication) ? DEFAULT.Encrypt : true;
1167+
return ConvertValueToBoolean(KEY.Encrypt, defaultEncryptValue);
1168+
}
1169+
11691170
private readonly bool _connectionReset;
11701171
private readonly bool _contextConnection;
11711172
private readonly bool _transparentNetworkIPResolution;

0 commit comments

Comments
 (0)