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 | Disable encryption when connecting to SQL Local DB #1312

Merged
merged 1 commit into from
Oct 6, 2021
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 @@ -394,6 +394,13 @@ internal void Connect(
authType == SqlAuthenticationMethod.NotSpecified ? SqlAuthenticationMethod.SqlPassword.ToString() : authType.ToString());
}

// Encryption is not supported on SQL Local DB - disable it for current session.
if (connHandler.ConnectionOptions.LocalDBInstance != null && encrypt)
{
encrypt = false;
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Encryption will be disabled as target server is a SQL Local DB instance.");
}

_sniSpnBuffer = null;

// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,15 @@ internal void Connect(ServerInfo serverInfo,

//Create LocalDB instance if necessary
if (connHandler.ConnectionOptions.LocalDBInstance != null)
{
LocalDBAPI.CreateLocalDBInstance(connHandler.ConnectionOptions.LocalDBInstance);
if (encrypt)
{
// Encryption is not supported on SQL Local DB - disable it for current session.
encrypt = false;
SqlClientEventSource.Log.TryTraceEvent("<sc.TdsParser.Connect|SEC> Encryption will be disabled as target server is a SQL Local DB instance.");
}
}

// AD Integrated behaves like Windows integrated when connecting to a non-fedAuth server
if (integratedSecurity || authType == SqlAuthenticationMethod.ActiveDirectoryIntegrated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static void SqlLocalDbConnectionTest()
ConnectionTest(s_localDbConnectionString);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
[ConditionalFact(nameof(IsLocalDBEnvironmentSet))]
public static void LocalDBEncryptionNotSupportedTest()
{
// Encryption is not supported by SQL Local DB.
// But connection should succeed as encryption is disabled by driver.
ConnectionWithEncryptionTest(s_localDbConnectionString);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
[ConditionalFact(nameof(IsLocalDBEnvironmentSet))]
public static void LocalDBMarsTest()
Expand All @@ -40,6 +49,18 @@ public static void InvalidLocalDBTest()
#endregion

#region SharedLocalDb tests
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
[ConditionalFact(nameof(IsLocalDbSharedInstanceSet))]
public static void SharedLocalDbEncryptionTest()
{
foreach (string connectionString in s_sharedLocalDbInstances)
{
// Encryption is not supported by SQL Local DB.
// But connection should succeed as encryption is disabled by driver.
ConnectionWithEncryptionTest(connectionString);
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.Uap)] // No Registry support on UAP
[ConditionalFact(nameof(IsLocalDbSharedInstanceSet))]
public static void SharedLocalDbMarsTest()
Expand Down Expand Up @@ -67,18 +88,28 @@ private static void ConnectionWithMarsTest(string connectionString)
{
IntegratedSecurity = true,
MultipleActiveResultSets = true,
ConnectTimeout = 2
};
OpenConnection(builder.ConnectionString);
}

private static void ConnectionWithEncryptionTest(string connectionString)
{
SqlConnectionStringBuilder builder = new(connectionString)
{
IntegratedSecurity = true,
ConnectTimeout = 2,
Encrypt = false
Encrypt = true
};
OpenConnection(builder.ConnectionString);
}

private static void ConnectionTest(string connectionString)
{
SqlConnectionStringBuilder builder = new(connectionString)
{
IntegratedSecurity = true,
ConnectTimeout = 2,
Encrypt = false
ConnectTimeout = 2
};
OpenConnection(builder.ConnectionString);
}
Expand All @@ -87,6 +118,7 @@ private static void OpenConnection(string connString)
{
using SqlConnection connection = new(connString);
connection.Open();
Assert.Equal(System.Data.ConnectionState.Open, connection.State);
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
using SqlCommand command = new SqlCommand("SELECT @@SERVERNAME", connection);
var result = command.ExecuteScalar();
Assert.NotNull(result);
Expand Down