Skip to content

Commit

Permalink
Fix DAC connections to localhost in managed SNI (#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Engel authored Dec 16, 2022
1 parent b3cfdbb commit ce70de9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +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))
{
ServerName = _connectionProtocol == Protocol.Admin ?
Environment.MachineName : DefaultHostName;
// For DAC use "localhost" instead of the server name.
ServerName = DefaultHostName;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
<Copy SourceFiles="config.default.json" DestinationFiles="config.json" Condition="!Exists('config.json')" />
</Target>
<ItemGroup>
<Content Include="config.json">
<None Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>config.json</Link>
</Content>
</None>
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
</ItemGroup>
</Project>

0 comments on commit ce70de9

Please sign in to comment.