microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Core/Hosting/BotConfig.cs
207lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.Configuration; |
| 5 | using Microsoft.Extensions.DependencyInjection; |
| 6 | using Microsoft.Extensions.Logging; |
| 7 | using Microsoft.Extensions.Logging.Abstractions; |
| 8 | |
| 9 | namespace Microsoft.Teams.Bot.Core.Hosting; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Configuration model for bot authentication credentials. |
| 13 | /// </summary> |
| 14 | /// <remarks> |
| 15 | /// This class consolidates bot authentication settings from various configuration sources including |
| 16 | /// Bot Framework SDK configuration, Core configuration, and MSAL configuration sections. |
| 17 | /// It supports multiple authentication modes: client secrets, system-assigned managed identities, |
| 18 | /// user-assigned managed identities, and federated identity credentials (FIC). |
| 19 | /// </remarks> |
| 20 | internal sealed class BotConfig |
| 21 | { |
| 22 | /// <summary> |
| 23 | /// Identifier used to specify system-assigned managed identity authentication. |
| 24 | /// When FicClientId equals this value, the system will use the system-assigned managed identity. |
| 25 | /// </summary> |
| 26 | public const string SystemManagedIdentityIdentifier = "system"; |
| 27 | |
| 28 | private const string BotScope = "https://api.botframework.com/.default"; |
| 29 | |
| 30 | private const string DefaultSectionName = "AzureAd"; |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Gets or sets the Azure AD tenant ID. |
| 34 | /// </summary> |
| 35 | public string TenantId { get; set; } = string.Empty; |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Gets or sets the application (client) ID from Azure AD app registration. |
| 39 | /// </summary> |
| 40 | public string ClientId { get; set; } = string.Empty; |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Gets or sets the client secret for client credentials authentication. |
| 44 | /// Optional if using managed identity or federated identity credentials. |
| 45 | /// </summary> |
| 46 | public string? ClientSecret { get; set; } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Gets or sets the client ID for federated identity credentials or user-assigned managed identity. |
| 50 | /// Use <see cref="SystemManagedIdentityIdentifier"/> to specify system-assigned managed identity. |
| 51 | /// </summary> |
| 52 | public string? FicClientId { get; set; } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Gets or sets the configuration section name used to resolve this BotConfig. |
| 56 | /// </summary> |
| 57 | public string SectionName { get; set; } = DefaultSectionName; |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Gets or sets the scope for token acquisition. |
| 61 | /// Defaults to "https://api.botframework.com/.default" if not specified. |
| 62 | /// </summary> |
| 63 | public string Scope { get; set; } = BotScope; |
| 64 | |
| 65 | internal IConfigurationSection? MsalConfigurationSection { get; set; } |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Creates a BotConfig from Bot Framework SDK configuration format. |
| 69 | /// </summary> |
| 70 | /// <param name="configuration">Configuration containing MicrosoftAppId, MicrosoftAppPassword, and MicrosoftAppTenantId settings.</param> |
| 71 | /// <returns>A new BotConfig instance with settings from Bot Framework configuration.</returns> |
| 72 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 73 | public static BotConfig FromBFConfig(IConfiguration configuration) |
| 74 | { |
| 75 | ArgumentNullException.ThrowIfNull(configuration); |
| 76 | return new() |
| 77 | { |
| 78 | TenantId = configuration["MicrosoftAppTenantId"] ?? string.Empty, |
| 79 | ClientId = configuration["MicrosoftAppId"] ?? string.Empty, |
| 80 | ClientSecret = configuration["MicrosoftAppPassword"], |
| 81 | Scope = configuration["Scope"] ?? BotScope |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | /// <summary> |
| 86 | /// Creates a BotConfig from Teams Bot Core environment variable format. |
| 87 | /// </summary> |
| 88 | /// <param name="configuration">Configuration containing TENANT_ID, CLIENT_ID, CLIENT_SECRET, and MANAGED_IDENTITY_CLIENT_ID settings.</param> |
| 89 | /// <returns>A new BotConfig instance with settings from Core configuration.</returns> |
| 90 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 91 | /// <remarks> |
| 92 | /// This format is typically used with environment variables in containerized deployments. |
| 93 | /// The MANAGED_IDENTITY_CLIENT_ID can be set to "system" for system-assigned managed identity. |
| 94 | /// </remarks> |
| 95 | public static BotConfig FromCoreConfig(IConfiguration configuration) |
| 96 | { |
| 97 | ArgumentNullException.ThrowIfNull(configuration); |
| 98 | return new() |
| 99 | { |
| 100 | TenantId = configuration["TENANT_ID"] ?? string.Empty, |
| 101 | ClientId = configuration["CLIENT_ID"] ?? string.Empty, |
| 102 | ClientSecret = configuration["CLIENT_SECRET"], |
| 103 | FicClientId = configuration["MANAGED_IDENTITY_CLIENT_ID"], |
| 104 | Scope = configuration["Scope"] ?? BotScope, |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | /// <summary> |
| 109 | /// Creates a BotConfig from MSAL configuration section format. |
| 110 | /// </summary> |
| 111 | /// <param name="configuration">Configuration containing an MSAL configuration section.</param> |
| 112 | /// <param name="sectionName">The name of the configuration section containing MSAL settings. Defaults to "AzureAd".</param> |
| 113 | /// <returns>A new BotConfig instance with settings from the MSAL configuration section.</returns> |
| 114 | /// <exception cref="ArgumentNullException">Thrown when <paramref name="configuration"/> is null.</exception> |
| 115 | /// <remarks> |
| 116 | /// This format is compatible with Microsoft.Identity.Web configuration sections in appsettings.json. |
| 117 | /// The section should contain TenantId, ClientId, and optionally ClientSecret properties. |
| 118 | /// </remarks> |
| 119 | public static BotConfig FromMsalConfig(IConfiguration configuration, string sectionName = "AzureAd") |
| 120 | { |
| 121 | ArgumentNullException.ThrowIfNull(configuration); |
| 122 | IConfigurationSection section = configuration.GetSection(sectionName); |
| 123 | return new() |
| 124 | { |
| 125 | TenantId = section["TenantId"] ?? string.Empty, |
| 126 | ClientId = section["ClientId"] ?? string.Empty, |
| 127 | ClientSecret = section["ClientSecret"], |
| 128 | Scope = section["Scope"] ?? BotScope, |
| 129 | MsalConfigurationSection = section, |
| 130 | SectionName = sectionName |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | /// <summary> |
| 135 | /// Resolves a BotConfig from a service collection by extracting configuration and logger, |
| 136 | /// then trying all configuration formats in priority order. |
| 137 | /// </summary> |
| 138 | /// <param name="services">The service collection containing IConfiguration and ILoggerFactory registrations.</param> |
| 139 | /// <param name="sectionName">The MSAL configuration section name. Defaults to "AzureAd".</param> |
| 140 | /// <returns>The first BotConfig with a non-empty ClientId, or a BotConfig with empty ClientId if none is found.</returns> |
| 141 | public static BotConfig Resolve(IServiceCollection services, string sectionName = "AzureAd") |
| 142 | { |
| 143 | ArgumentNullException.ThrowIfNull(services); |
| 144 | |
| 145 | // Extract IConfiguration from service collection |
| 146 | ServiceDescriptor? configDescriptor = services.FirstOrDefault(d => d.ServiceType == typeof(IConfiguration)); |
| 147 | IConfiguration configuration = configDescriptor?.ImplementationInstance as IConfiguration |
| 148 | ?? services.BuildServiceProvider().GetRequiredService<IConfiguration>(); |
| 149 | |
| 150 | // Get logger using the helper method from AddBotApplicationExtensions |
| 151 | ILogger logger = AddBotApplicationExtensions.GetLoggerFromServices(services, typeof(BotConfig)); |
| 152 | |
| 153 | return Resolve(configuration, sectionName, logger); |
| 154 | } |
| 155 | |
| 156 | /// <summary> |
| 157 | /// Resolves a BotConfig by trying all configuration formats in priority order: |
| 158 | /// MSAL section, Core environment variables, then Bot Framework SDK keys. |
| 159 | /// </summary> |
| 160 | /// <param name="configuration">The application configuration.</param> |
| 161 | /// <param name="sectionName">The MSAL configuration section name. Defaults to "AzureAd".</param> |
| 162 | /// <param name="logger">Optional logger to log which configuration source was used.</param> |
| 163 | /// <returns>The first BotConfig with a non-empty ClientId, or a BotConfig with empty ClientId if none is found.</returns> |
| 164 | public static BotConfig Resolve(IConfiguration configuration, string sectionName = "AzureAd", ILogger? logger = null) |
| 165 | { |
| 166 | ArgumentNullException.ThrowIfNull(configuration); |
| 167 | logger ??= NullLogger.Instance; |
| 168 | |
| 169 | BotConfig config = FromMsalConfig(configuration, sectionName); |
| 170 | if (!string.IsNullOrEmpty(config.ClientId)) |
| 171 | { |
| 172 | _logUsingSectionConfig(logger, sectionName, null); |
| 173 | config.SectionName = sectionName; |
| 174 | return config; |
| 175 | } |
| 176 | |
| 177 | config = FromCoreConfig(configuration); |
| 178 | if (!string.IsNullOrEmpty(config.ClientId)) |
| 179 | { |
| 180 | _logUsingCoreConfig(logger, null); |
| 181 | config.SectionName = sectionName; |
| 182 | return config; |
| 183 | } |
| 184 | |
| 185 | config = FromBFConfig(configuration); |
| 186 | if (!string.IsNullOrEmpty(config.ClientId)) |
| 187 | { |
| 188 | _logUsingBFConfig(logger, null); |
| 189 | config.SectionName = sectionName; |
| 190 | return config; |
| 191 | } |
| 192 | |
| 193 | // No configuration found - log warning and return empty config |
| 194 | _logNoConfigFound(logger, null); |
| 195 | return new BotConfig { SectionName = sectionName }; |
| 196 | } |
| 197 | |
| 198 | private static readonly Action<ILogger, Exception?> _logUsingBFConfig = |
| 199 | LoggerMessage.Define(LogLevel.Debug, new(1), "Resolved bot configuration from Bot Framework configuration keys"); |
| 200 | private static readonly Action<ILogger, Exception?> _logUsingCoreConfig = |
| 201 | LoggerMessage.Define(LogLevel.Debug, new(2), "Resolved bot configuration from Core environment variables"); |
| 202 | private static readonly Action<ILogger, string, Exception?> _logUsingSectionConfig = |
| 203 | LoggerMessage.Define<string>(LogLevel.Debug, new(3), "Resolved bot configuration from '{SectionName}' configuration section"); |
| 204 | private static readonly Action<ILogger, Exception?> _logNoConfigFound = |
| 205 | LoggerMessage.Define(LogLevel.Warning, new(4), "No bot configuration found in configuration."); |
| 206 | |
| 207 | } |