forked from dotnet/SqlClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlAuthenticationProviderManager.NetCoreApp.cs
194 lines (176 loc) · 8.78 KB
/
SqlAuthenticationProviderManager.NetCoreApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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;
static SqlAuthenticationProviderManager()
{
SqlAuthenticationProviderConfigurationSection configurationSection = null;
try
{
// New configuration section "SqlClientAuthenticationProviders" for Microsoft.Data.SqlClient accepted to avoid conflicts with older one.
configurationSection = FetchConfigurationSection<SqlClientAuthenticationProviderConfigurationSection>(SqlClientAuthenticationProviderConfigurationSection.Name);
if (null == configurationSection)
{
// If configuration section is not yet found, try with old Configuration Section name for backwards compatibility
configurationSection = FetchConfigurationSection<SqlAuthenticationProviderConfigurationSection>(SqlAuthenticationProviderConfigurationSection.Name);
}
}
catch (ConfigurationErrorsException e)
{
// Don't throw an error for invalid config files
SqlClientEventSource.Log.TryTraceEvent("static SqlAuthenticationProviderManager: Unable to load custom SqlAuthenticationProviders or SqlClientAuthenticationProviders. ConfigurationManager failed to load due to configuration errors: {0}", e);
}
Instance = new SqlAuthenticationProviderManager(configurationSection);
SetDefaultAuthProviders(Instance);
}
/// <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, "Neither SqlClientAuthenticationProviders nor SqlAuthenticationProviders configuration section found.");
return;
}
if (!string.IsNullOrEmpty(configSection.ApplicationClientId))
{
_applicationClientId = configSection.ApplicationClientId;
_sqlAuthLogger.LogInfo(_typeName, methodName, "Received user-defined Application Client Id");
}
else
{
_sqlAuthLogger.LogInfo(_typeName, methodName, "No user-defined Application Client Id found.");
}
// 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 T FetchConfigurationSection<T>(string name)
{
Type t = typeof(T);
object section = ConfigurationManager.GetSection(name);
if (null != section)
{
if (section is ConfigurationSection configSection && configSection.GetType() == t)
{
return (T)section;
}
else
{
SqlClientEventSource.Log.TraceEvent("Found a custom {0} configuration but it is not of type {1}.", name, t.FullName);
}
}
return default;
}
private static SqlAuthenticationMethod AuthenticationEnumFromString(string authentication)
{
switch (authentication.ToLowerInvariant())
{
case ActiveDirectoryIntegrated:
return SqlAuthenticationMethod.ActiveDirectoryIntegrated;
case ActiveDirectoryPassword:
return SqlAuthenticationMethod.ActiveDirectoryPassword;
case ActiveDirectoryInteractive:
return SqlAuthenticationMethod.ActiveDirectoryInteractive;
case ActiveDirectoryServicePrincipal:
return SqlAuthenticationMethod.ActiveDirectoryServicePrincipal;
case ActiveDirectoryDeviceCodeFlow:
return SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow;
case ActiveDirectoryManagedIdentity:
return SqlAuthenticationMethod.ActiveDirectoryManagedIdentity;
case ActiveDirectoryMSI:
return SqlAuthenticationMethod.ActiveDirectoryMSI;
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)this["providers"];
/// <summary>
/// User-defined initializer.
/// </summary>
[ConfigurationProperty("initializerType")]
public string InitializerType => this["initializerType"] as string;
/// <summary>
/// Application Client Id
/// </summary>
[ConfigurationProperty("applicationClientId", IsRequired = false)]
public string ApplicationClientId => this["applicationClientId"] as string;
}
/// <summary>
/// The configuration section definition for reading app.config.
/// </summary>
internal class SqlClientAuthenticationProviderConfigurationSection : SqlAuthenticationProviderConfigurationSection
{
public new const string Name = "SqlClientAuthenticationProviders";
}
}
}