| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Reflection; |
| | 8 | | using System.Security.Cryptography.X509Certificates; |
| | 9 | | using System.Text; |
| | 10 | | using Azure.Core; |
| | 11 | | using Azure.Identity; |
| | 12 | | using Microsoft.Extensions.Configuration; |
| | 13 | |
|
| | 14 | | namespace Microsoft.Extensions.Azure |
| | 15 | | { |
| | 16 | | internal static class ClientFactory |
| | 17 | | { |
| | 18 | | public static object CreateClient(Type clientType, Type optionsType, object options, IConfiguration configuratio |
| | 19 | | { |
| 22 | 20 | | List<object> arguments = new List<object>(); |
| 246 | 21 | | foreach (var constructor in clientType.GetConstructors().OrderByDescending(c => c.GetParameters().Length)) |
| | 22 | | { |
| 62 | 23 | | if (!IsApplicableConstructor(constructor, optionsType)) |
| | 24 | | { |
| | 25 | | continue; |
| | 26 | | } |
| | 27 | |
|
| 60 | 28 | | arguments.Clear(); |
| | 29 | |
|
| 60 | 30 | | bool match = true; |
| 236 | 31 | | foreach (var parameter in constructor.GetParameters()) |
| | 32 | | { |
| 88 | 33 | | if (IsCredentialParameter(parameter)) |
| | 34 | | { |
| 4 | 35 | | if (credential == null) |
| | 36 | | { |
| 2 | 37 | | match = false; |
| 2 | 38 | | break; |
| | 39 | | } |
| | 40 | |
|
| 2 | 41 | | arguments.Add(credential); |
| 2 | 42 | | continue; |
| | 43 | | } |
| | 44 | |
|
| 84 | 45 | | if (IsOptionsParameter(parameter, optionsType)) |
| | 46 | | { |
| | 47 | | break; |
| | 48 | | } |
| | 49 | |
|
| 66 | 50 | | if (!TryConvertArgument(configuration, parameter.Name, parameter.ParameterType, out object argument) |
| | 51 | | { |
| 38 | 52 | | match = false; |
| 38 | 53 | | break; |
| | 54 | | } |
| | 55 | |
|
| 26 | 56 | | arguments.Add(argument); |
| | 57 | | } |
| | 58 | |
|
| 58 | 59 | | if (!match) |
| | 60 | | { |
| | 61 | | continue; |
| | 62 | | } |
| | 63 | |
|
| 18 | 64 | | arguments.Add(options); |
| | 65 | |
|
| 18 | 66 | | return constructor.Invoke(arguments.ToArray()); |
| | 67 | | } |
| | 68 | |
|
| 2 | 69 | | throw new InvalidOperationException(BuildErrorMessage(clientType, optionsType)); |
| 18 | 70 | | } |
| | 71 | |
|
| | 72 | | internal static TokenCredential CreateCredential(IConfiguration configuration, TokenCredentialOptions identityCl |
| | 73 | | { |
| 18 | 74 | | var clientId = configuration["clientId"]; |
| 18 | 75 | | var tenantId = configuration["tenantId"]; |
| 18 | 76 | | var clientSecret = configuration["clientSecret"]; |
| 18 | 77 | | var certificate = configuration["clientCertificate"]; |
| 18 | 78 | | var certificateStoreName = configuration["clientCertificateStoreName"]; |
| 18 | 79 | | var certificateStoreLocation = configuration["clientCertificateStoreLocation"]; |
| | 80 | |
|
| 18 | 81 | | if (!string.IsNullOrWhiteSpace(tenantId) && |
| 18 | 82 | | !string.IsNullOrWhiteSpace(clientId) && |
| 18 | 83 | | !string.IsNullOrWhiteSpace(clientSecret)) |
| | 84 | | { |
| 6 | 85 | | return new ClientSecretCredential(tenantId, clientId, clientSecret, identityClientOptions); |
| | 86 | | } |
| | 87 | |
|
| 12 | 88 | | if (!string.IsNullOrWhiteSpace(tenantId) && |
| 12 | 89 | | !string.IsNullOrWhiteSpace(clientId) && |
| 12 | 90 | | !string.IsNullOrWhiteSpace(certificate)) |
| | 91 | | { |
| 6 | 92 | | StoreLocation storeLocation = StoreLocation.CurrentUser; |
| | 93 | |
|
| 6 | 94 | | if (!string.IsNullOrWhiteSpace(certificateStoreLocation)) |
| | 95 | | { |
| 4 | 96 | | storeLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), certificateStoreLocation, true); |
| | 97 | | } |
| | 98 | |
|
| 6 | 99 | | if (string.IsNullOrWhiteSpace(certificateStoreName)) |
| | 100 | | { |
| 2 | 101 | | certificateStoreName = "MY"; // MY is the default used in X509Store |
| | 102 | | } |
| | 103 | |
|
| 6 | 104 | | using var store = new X509Store(certificateStoreName, storeLocation); |
| 6 | 105 | | store.Open(OpenFlags.ReadOnly); |
| 6 | 106 | | X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindByThumbprint, certificate, f |
| | 107 | |
|
| 6 | 108 | | if (certs.Count == 0) |
| | 109 | | { |
| 0 | 110 | | throw new InvalidOperationException($"Unable to find a certificate with thumbprint '{certificate}'") |
| | 111 | | } |
| | 112 | |
|
| 6 | 113 | | var credential = new ClientCertificateCredential(tenantId, clientId, certs[0], identityClientOptions); |
| 6 | 114 | | store.Close(); |
| | 115 | |
|
| 6 | 116 | | return credential; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | // TODO: More logging |
| 6 | 120 | | return null; |
| 6 | 121 | | } |
| | 122 | |
|
| | 123 | | private static bool IsCredentialParameter(ParameterInfo parameter) |
| | 124 | | { |
| 88 | 125 | | return parameter.ParameterType == typeof(TokenCredential); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | private static bool IsOptionsParameter(ParameterInfo parameter, Type optionsType) |
| | 129 | | { |
| 174 | 130 | | return parameter.ParameterType.IsAssignableFrom(optionsType) && |
| 174 | 131 | | parameter.Position == ((ConstructorInfo)parameter.Member).GetParameters().Length - 1; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private static string BuildErrorMessage(Type clientType, Type optionsType) |
| | 135 | | { |
| 2 | 136 | | var builder = new StringBuilder(); |
| 2 | 137 | | builder.AppendLine("Unable to find matching constructor. Define one of the follow sets of configuration para |
| | 138 | |
|
| 2 | 139 | | int counter = 1; |
| | 140 | |
|
| 24 | 141 | | foreach (var constructor in clientType.GetConstructors()) |
| | 142 | | { |
| 10 | 143 | | if (!IsApplicableConstructor(constructor, optionsType)) |
| | 144 | | { |
| | 145 | | continue; |
| | 146 | | } |
| | 147 | |
|
| 8 | 148 | | builder.Append(counter).Append(". "); |
| | 149 | |
|
| 8 | 150 | | bool first = true; |
| | 151 | |
|
| 44 | 152 | | foreach (var parameter in constructor.GetParameters()) |
| | 153 | | { |
| 18 | 154 | | if (IsOptionsParameter(parameter, optionsType)) |
| | 155 | | { |
| | 156 | | break; |
| | 157 | | } |
| | 158 | |
|
| 10 | 159 | | if (first) |
| | 160 | | { |
| 8 | 161 | | first = false; |
| | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 2 | 165 | | builder.Append(", "); |
| | 166 | | } |
| | 167 | |
|
| 10 | 168 | | builder.Append(parameter.Name); |
| | 169 | | } |
| | 170 | |
|
| 8 | 171 | | builder.AppendLine(); |
| 8 | 172 | | counter++; |
| | 173 | | } |
| | 174 | |
|
| 2 | 175 | | return builder.ToString(); |
| | 176 | | } |
| | 177 | |
|
| | 178 | | private static bool IsApplicableConstructor(ConstructorInfo constructorInfo, Type optionsType) |
| | 179 | | { |
| 72 | 180 | | var parameters = constructorInfo.GetParameters(); |
| | 181 | |
|
| 72 | 182 | | return constructorInfo.IsPublic && |
| 72 | 183 | | parameters.Length > 0 && |
| 72 | 184 | | IsOptionsParameter(parameters[parameters.Length - 1], optionsType); |
| | 185 | | } |
| | 186 | |
|
| | 187 | | private static bool TryConvertArgument(IConfiguration configuration, string parameterName, Type parameterType, o |
| | 188 | | { |
| 88 | 189 | | if (parameterType == typeof(string)) |
| | 190 | | { |
| 40 | 191 | | return TryConvertFromString(configuration, parameterName, s => s, out value); |
| | 192 | | } |
| | 193 | |
|
| 56 | 194 | | if (parameterType == typeof(Uri)) |
| | 195 | | { |
| 62 | 196 | | return TryConvertFromString(configuration, parameterName, s => new Uri(s), out value); |
| | 197 | | } |
| | 198 | |
|
| 14 | 199 | | if (configuration[parameterName] != null) |
| | 200 | | { |
| 0 | 201 | | throw new InvalidOperationException($"Unable to convert value '{configuration[parameterName]}' to parame |
| | 202 | | } |
| | 203 | |
|
| 14 | 204 | | return TryCreateObject(parameterType, configuration.GetSection(parameterName), out value); |
| | 205 | | } |
| | 206 | |
|
| | 207 | | private static bool TryConvertFromString(IConfiguration configuration, string parameterName, Func<string, object |
| | 208 | | { |
| 74 | 209 | | string stringValue = configuration[parameterName]; |
| 74 | 210 | | if (stringValue == null) |
| | 211 | | { |
| 46 | 212 | | value = null; |
| 46 | 213 | | return false; |
| | 214 | | } |
| | 215 | |
|
| 28 | 216 | | value = func(stringValue); |
| 28 | 217 | | return true; |
| | 218 | | } |
| | 219 | |
|
| | 220 | | internal static bool TryCreateObject(Type type, IConfigurationSection configuration, out object value) |
| | 221 | | { |
| 14 | 222 | | if (!configuration.GetChildren().Any()) |
| | 223 | | { |
| 6 | 224 | | value = null; |
| 6 | 225 | | return false; |
| | 226 | | } |
| | 227 | |
|
| 8 | 228 | | List<object> arguments = new List<object>(); |
| 74 | 229 | | foreach (var constructor in type.GetConstructors().OrderByDescending(c => c.GetParameters().Length)) |
| | 230 | | { |
| 20 | 231 | | arguments.Clear(); |
| | 232 | |
|
| 20 | 233 | | bool match = true; |
| 70 | 234 | | foreach (var parameter in constructor.GetParameters()) |
| | 235 | | { |
| 22 | 236 | | if (!TryConvertArgument(configuration, parameter.Name, parameter.ParameterType, out object argument) |
| | 237 | | { |
| 14 | 238 | | match = false; |
| 14 | 239 | | break; |
| | 240 | | } |
| | 241 | |
|
| 8 | 242 | | arguments.Add(argument); |
| | 243 | | } |
| | 244 | |
|
| 20 | 245 | | if (!match) |
| | 246 | | { |
| | 247 | | continue; |
| | 248 | | } |
| | 249 | |
|
| 6 | 250 | | value = constructor.Invoke(arguments.ToArray()); |
| 6 | 251 | | return true; |
| | 252 | | } |
| | 253 | |
|
| 2 | 254 | | throw new InvalidOperationException($"Unable to convert section '{configuration.Path}' to parameter type '{t |
| 6 | 255 | | } |
| | 256 | |
|
| | 257 | | } |
| | 258 | | } |