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

Remove Diagnostic Source + Configuration Manager from .NET Standard DLL as it's not supported #535

Merged
merged 11 commits into from
Apr 30, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientEventSource.cs">
<Link>Microsoft\Data\SqlClient\SqlClientEventSource.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientLogger.cs" >
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientLogger.cs">
<Link>Microsoft\Data\SqlClient\SqlClientLogger.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\Sql\SqlNotificationRequest.cs">
Expand Down Expand Up @@ -66,7 +66,7 @@
<Compile Include="..\..\src\Microsoft\Data\SqlClient\ColumnEncryptionKeyInfo.cs">
<Link>Microsoft\Data\SqlClient\ColumnEncryptionKeyInfo.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\OnChangedEventHandler.cs" >
<Compile Include="..\..\src\Microsoft\Data\SqlClient\OnChangedEventHandler.cs">
<Link>Microsoft\Data\SqlClient\OnChangedEventHandler.cs</Link>
</Compile>
<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlAeadAes256CbcHmac256Algorithm.cs">
Expand Down Expand Up @@ -143,13 +143,16 @@
<Compile Include="Microsoft.Data.SqlClient.TypeForwards.cs" />
</ItemGroup>
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(TargetGroup)' == 'netstandard'">
<Compile Include="Microsoft\Data\SqlClient\SqlDiagnosticListener.NetStandard.cs" />
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.NetStandard.cs" />
</ItemGroup>
<ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(TargetGroup)' == 'netcoreapp'">
<Compile Include="Microsoft\Data\Common\DbConnectionStringCommon.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPool.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlAuthenticationProviderManager.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionString.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionStringBuilder.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlDiagnosticListener.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\PoolBlockingPeriod.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.NetCoreApp.cs" />
<Compile Include="Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.NetCoreApp.cs" />
Expand Down Expand Up @@ -657,12 +660,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetsWindows)' == 'true' and '$(IsUAPAssembly)' != 'true'" Include="Microsoft.Win32.Registry" Version="$(MicrosoftWin32RegistryVersion)" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="$(SystemConfigurationConfigurationManagerVersion)" />
<PackageReference Condition="'$(TargetGroup)' == 'netcoreapp'" Include="System.Configuration.ConfigurationManager" Version="$(SystemConfigurationConfigurationManagerVersion)" />
<PackageReference Include="System.Security.Permissions" Version="$(SystemSecurityPermissionsVersion)" />
<PackageReference Include="System.Security.Principal.Windows" Version="$(SystemSecurityPrincipalWindowsVersion)" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="$(SystemTextEncodingCodePagesVersion)" />
<PackageReference Include="runtime.native.System.Data.SqlClient.sni" Version="$(RuntimeNativeSystemDataSqlClientSNIVersion)" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourceVersion)" />
<PackageReference Condition="$(TargetGroup) == 'netcoreapp' " Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourceVersion)" />
<PackageReference Include="System.Threading.Tasks" Version="$(SystemThreadingTasksVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="Microsoft.Identity.Client" Version="$(MicrosoftIdentityClientVersion)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;

namespace Microsoft.Data.SqlClient
{
internal partial class SqlAuthenticationProviderManager
{
private readonly SqlAuthenticationInitializer _initializer;

/// <summary>
/// Constructor.
/// </summary>
public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSection configSection = null)
{
var methodName = "Ctor";
_typeName = GetType().Name;
_providers = new ConcurrentDictionary<SqlAuthenticationMethod, SqlAuthenticationProvider>();
var authenticationsWithAppSpecifiedProvider = new HashSet<SqlAuthenticationMethod>();
_authenticationsWithAppSpecifiedProvider = authenticationsWithAppSpecifiedProvider;

if (configSection == null)
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found.");
return;
}

// Create user-defined auth initializer, if any.
if (!string.IsNullOrEmpty(configSection.InitializerType))
{
try
{
var initializerType = Type.GetType(configSection.InitializerType, true);
_initializer = (SqlAuthenticationInitializer)Activator.CreateInstance(initializerType);
_initializer.Initialize();
}
catch (Exception e)
{
throw SQL.CannotCreateSqlAuthInitializer(configSection.InitializerType, e);
}
_sqlAuthLogger.LogInfo(_typeName, methodName, "Created user-defined SqlAuthenticationInitializer.");
}
else
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined SqlAuthenticationInitializer found.");
}

// add user-defined providers, if any.
if (configSection.Providers != null && configSection.Providers.Count > 0)
{
foreach (ProviderSettings providerSettings in configSection.Providers)
{
SqlAuthenticationMethod authentication = AuthenticationEnumFromString(providerSettings.Name);
SqlAuthenticationProvider provider;
try
{
var providerType = Type.GetType(providerSettings.Type, true);
provider = (SqlAuthenticationProvider)Activator.CreateInstance(providerType);
}
catch (Exception e)
{
throw SQL.CannotCreateAuthProvider(authentication.ToString(), providerSettings.Type, e);
}
if (!provider.IsSupported(authentication))
{
throw SQL.UnsupportedAuthenticationByProvider(authentication.ToString(), providerSettings.Type);
}

_providers[authentication] = provider;
authenticationsWithAppSpecifiedProvider.Add(authentication);
_sqlAuthLogger.LogInfo(_typeName, methodName, string.Format("Added user-defined auth provider: {0} for authentication {1}.", providerSettings?.Type, authentication));
}
}
else
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined auth providers.");
}
}

private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication)
{
switch (authentication.ToLowerInvariant())
{
case ActiveDirectoryPassword:
return SqlAuthenticationMethod.ActiveDirectoryPassword;
default:
throw SQL.UnsupportedAuthentication(authentication);
}
}

/// <summary>
/// The configuration section definition for reading app.config.
/// </summary>
internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSection
{
public const string Name = "SqlAuthenticationProviders";

/// <summary>
/// User-defined auth providers.
/// </summary>
[ConfigurationProperty("providers")]
public ProviderSettingsCollection Providers => (ProviderSettingsCollection)base["providers"];

/// <summary>
/// User-defined initializer.
/// </summary>
[ConfigurationProperty("initializerType")]
public string InitializerType => base["initializerType"] as string;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ namespace Microsoft.Data.SqlClient
/// <summary>
/// Authentication provider manager.
/// </summary>
internal class SqlAuthenticationProviderManager
internal partial class SqlAuthenticationProviderManager
{
private const string ActiveDirectoryPassword = "active directory password";
private const string ActiveDirectoryIntegrated = "active directory integrated";
private const string ActiveDirectoryInteractive = "active directory interactive";

private readonly string _typeName;
private readonly IReadOnlyCollection<SqlAuthenticationMethod> _authenticationsWithAppSpecifiedProvider;
private readonly ConcurrentDictionary<SqlAuthenticationMethod, SqlAuthenticationProvider> _providers;
private readonly SqlClientLogger _sqlAuthLogger = new SqlClientLogger();

public static readonly SqlAuthenticationProviderManager Instance;

static SqlAuthenticationProviderManager()
{
var activeDirectoryAuthNativeProvider = new ActiveDirectoryNativeAuthenticationProvider();
#if netcoreapp
SqlAuthenticationProviderConfigurationSection configurationSection;

try
{
configurationSection = (SqlAuthenticationProviderConfigurationSection)ConfigurationManager.GetSection(SqlAuthenticationProviderConfigurationSection.Name);
Expand All @@ -32,84 +41,23 @@ static SqlAuthenticationProviderManager()
{
throw SQL.CannotGetAuthProviderConfig(e);
}

Instance = new SqlAuthenticationProviderManager(configurationSection);
#else
Instance = new SqlAuthenticationProviderManager();
#endif
Instance.SetProvider(SqlAuthenticationMethod.ActiveDirectoryPassword, activeDirectoryAuthNativeProvider);
}
public static readonly SqlAuthenticationProviderManager Instance;

private readonly string _typeName;
private readonly SqlAuthenticationInitializer _initializer;
private readonly IReadOnlyCollection<SqlAuthenticationMethod> _authenticationsWithAppSpecifiedProvider;
private readonly ConcurrentDictionary<SqlAuthenticationMethod, SqlAuthenticationProvider> _providers;
private readonly SqlClientLogger _sqlAuthLogger = new SqlClientLogger();

/// <summary>
/// Constructor.
/// </summary>
public SqlAuthenticationProviderManager(SqlAuthenticationProviderConfigurationSection configSection)
public SqlAuthenticationProviderManager()
{
_typeName = GetType().Name;
var methodName = "Ctor";
_providers = new ConcurrentDictionary<SqlAuthenticationMethod, SqlAuthenticationProvider>();
var authenticationsWithAppSpecifiedProvider = new HashSet<SqlAuthenticationMethod>();
_authenticationsWithAppSpecifiedProvider = authenticationsWithAppSpecifiedProvider;

if (configSection == null)
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No SqlAuthProviders configuration section found.");
return;
}

// Create user-defined auth initializer, if any.
//
if (!string.IsNullOrEmpty(configSection.InitializerType))
{
try
{
var initializerType = Type.GetType(configSection.InitializerType, true);
_initializer = (SqlAuthenticationInitializer)Activator.CreateInstance(initializerType);
_initializer.Initialize();
}
catch (Exception e)
{
throw SQL.CannotCreateSqlAuthInitializer(configSection.InitializerType, e);
}
_sqlAuthLogger.LogInfo(_typeName, methodName, "Created user-defined SqlAuthenticationInitializer.");
}
else
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined SqlAuthenticationInitializer found.");
}

// add user-defined providers, if any.
//
if (configSection.Providers != null && configSection.Providers.Count > 0)
{
foreach (ProviderSettings providerSettings in configSection.Providers)
{
SqlAuthenticationMethod authentication = AuthenticationEnumFromString(providerSettings.Name);
SqlAuthenticationProvider provider;
try
{
var providerType = Type.GetType(providerSettings.Type, true);
provider = (SqlAuthenticationProvider)Activator.CreateInstance(providerType);
}
catch (Exception e)
{
throw SQL.CannotCreateAuthProvider(authentication.ToString(), providerSettings.Type, e);
}
if (!provider.IsSupported(authentication))
throw SQL.UnsupportedAuthenticationByProvider(authentication.ToString(), providerSettings.Type);

_providers[authentication] = provider;
authenticationsWithAppSpecifiedProvider.Add(authentication);
_sqlAuthLogger.LogInfo(_typeName, methodName, string.Format("Added user-defined auth provider: {0} for authentication {1}.", providerSettings?.Type, authentication));
}
}
else
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined auth providers.");
}
_authenticationsWithAppSpecifiedProvider = new HashSet<SqlAuthenticationMethod>();
_sqlAuthLogger.LogInfo(_typeName, "Ctor", "No SqlAuthProviders configuration section found.");
}

/// <summary>
Expand Down Expand Up @@ -156,17 +104,6 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent
return true;
}

private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication)
{
switch (authentication.ToLowerInvariant())
{
case ActiveDirectoryPassword:
return SqlAuthenticationMethod.ActiveDirectoryPassword;
default:
throw SQL.UnsupportedAuthentication(authentication);
}
}

private static string GetProviderType(SqlAuthenticationProvider provider)
{
if (provider == null)
Expand All @@ -175,26 +112,6 @@ private static string GetProviderType(SqlAuthenticationProvider provider)
}
}

/// <summary>
/// The configuration section definition for reading app.config.
/// </summary>
internal class SqlAuthenticationProviderConfigurationSection : ConfigurationSection
{
public const string Name = "SqlAuthenticationProviders";

/// <summary>
/// User-defined auth providers.
/// </summary>
[ConfigurationProperty("providers")]
public ProviderSettingsCollection Providers => (ProviderSettingsCollection)base["providers"];

/// <summary>
/// User-defined initializer.
/// </summary>
[ConfigurationProperty("initializerType")]
public string InitializerType => base["initializerType"] as string;
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationInitializer.xml' path='docs/members[@name="SqlAuthenticationInitializer"]/SqlAuthenticationInitializer/*'/>
public abstract class SqlAuthenticationInitializer
{
Expand Down
Loading