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 DAC connections to localhost in managed SNI #1865

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>