From 3e6c06ae77068c7a3ed57321f30ebcb73b345b89 Mon Sep 17 00:00:00 2001 From: David Engel Date: Thu, 8 Dec 2022 14:59:59 -0800 Subject: [PATCH 1/3] Fix DAC connections to localhost in managed SNI --- .../netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index ca5587c098..8b831043ff 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -581,8 +581,9 @@ private void InferLocalServerName() // If Server name is empty or localhost, then use "localhost" if (string.IsNullOrEmpty(ServerName) || IsLocalHost(ServerName)) { + // For DAC use "localhost" instead of the server name. ServerName = _connectionProtocol == Protocol.Admin ? - Environment.MachineName : DefaultHostName; + DefaultHostName : Environment.MachineName; } } @@ -761,6 +762,7 @@ private bool InferNamedPipesInformation() } private static bool IsLocalHost(string serverName) - => ".".Equals(serverName) || "(local)".Equals(serverName) || "localhost".Equals(serverName); + => ".".Equals(serverName) || "(local)".Equals(serverName) || "localhost".Equals(serverName) || + Environment.MachineName.Equals(serverName, StringComparison.CurrentCultureIgnoreCase); } } From 2ef4fd07d8be791648aaede6f236821eaa340554 Mon Sep 17 00:00:00 2001 From: David Engel Date: Tue, 13 Dec 2022 13:29:53 -0800 Subject: [PATCH 2/3] Only use localhost for DAC connections in managed SNI --- .../src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs index 8b831043ff..ac4d3599dd 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIProxy.cs @@ -579,11 +579,12 @@ internal static DataSource ParseServerName(string dataSource) private void InferLocalServerName() { // If Server name is empty or localhost, then use "localhost" - if (string.IsNullOrEmpty(ServerName) || IsLocalHost(ServerName)) + if (string.IsNullOrEmpty(ServerName) || IsLocalHost(ServerName) || + (Environment.MachineName.Equals(ServerName, StringComparison.CurrentCultureIgnoreCase) && + _connectionProtocol == Protocol.Admin)) { // For DAC use "localhost" instead of the server name. - ServerName = _connectionProtocol == Protocol.Admin ? - DefaultHostName : Environment.MachineName; + ServerName = DefaultHostName; } } @@ -762,7 +763,6 @@ private bool InferNamedPipesInformation() } private static bool IsLocalHost(string serverName) - => ".".Equals(serverName) || "(local)".Equals(serverName) || "localhost".Equals(serverName) || - Environment.MachineName.Equals(serverName, StringComparison.CurrentCultureIgnoreCase); + => ".".Equals(serverName) || "(local)".Equals(serverName) || "localhost".Equals(serverName); } } From a1651a5661a45a0392ed1064d99a51f618991bd1 Mon Sep 17 00:00:00 2001 From: David Engel Date: Thu, 15 Dec 2022 14:17:31 -0800 Subject: [PATCH 3/3] Added DAC test --- .../SQL/ConnectivityTests/ConnectivityTest.cs | 36 +++++++++++++++++++ ...rosoft.Data.SqlClient.TestUtilities.csproj | 5 ++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs index 08113cbd3a..489a414ce9 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectivityTests/ConnectivityTest.cs @@ -457,5 +457,41 @@ public static void ConnectionAliasTest() key.DeleteValue(b.DataSource); } } + + private static bool CanUseDacConnection() + { + if (!DataTestUtility.IsTCPConnStringSetup()) + { + return false; + } + + SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString); + if (!DataTestUtility.ParseDataSource(b.DataSource, out string hostname, out int port, out string instanceName)) + { + return false; + } + + if ("localhost".Equals(hostname.ToLower()) && (port.Equals(-1) || port.Equals(1433)) && + string.IsNullOrEmpty(instanceName) && b.UserID != null && b.UserID.ToLower().Equals("sa")) + { + return true; + } + + return false; + } + + [ConditionalFact(nameof(CanUseDacConnection))] + public static void DacConnectionTest() + { + if (!CanUseDacConnection()) + { + throw new Exception("Unable to use a DAC connection in this environment. Localhost + sa credentials required."); + } + + SqlConnectionStringBuilder b = new(DataTestUtility.TCPConnectionString); + b.DataSource = "admin:localhost"; + using SqlConnection sqlConnection = new(b.ConnectionString); + sqlConnection.Open(); + } } } diff --git a/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj b/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj index 087d348c82..3bd48830cc 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj +++ b/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Microsoft.Data.SqlClient.TestUtilities.csproj @@ -8,10 +8,9 @@ - + PreserveNewest - config.json - + \ No newline at end of file