|
12 | 12 | using Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch; |
13 | 13 | using AzureSdkQaBot.Model; |
14 | 14 | using Octokit; |
| 15 | +using Azure.Identity; |
| 16 | +using Azure.Security.KeyVault.Certificates; |
15 | 17 |
|
16 | 18 | var builder = WebApplication.CreateBuilder(args); |
17 | 19 |
|
|
21 | 23 |
|
22 | 24 | // Prepare Configuration for ConfigurationBotFrameworkAuthentication |
23 | 25 | var config = builder.Configuration.Get<ConfigOptions>()!; |
24 | | -builder.Configuration["MicrosoftAppType"] = "MultiTenant"; |
25 | | -builder.Configuration["MicrosoftAppId"] = config.BOT_ID; |
26 | | -builder.Configuration["MicrosoftAppPassword"] = config.BOT_PASSWORD; |
| 26 | + |
| 27 | + |
| 28 | +// Access key vault |
| 29 | +if (string.IsNullOrEmpty(config.KeyVaultUrl)) |
| 30 | +{ |
| 31 | + throw new Exception("KeyVaultUrl is not set in the configuration."); |
| 32 | +} |
| 33 | + |
| 34 | +System.Security.Cryptography.X509Certificates.X509Certificate2? certificate = null; |
| 35 | +try |
| 36 | +{ |
| 37 | + CertificateClient client = new(vaultUri: new Uri(config.KeyVaultUrl), credential: new DefaultAzureCredential()); |
| 38 | + if (client == null) |
| 39 | + { |
| 40 | + throw new Exception($"Failed to create KeyVault client for {config.KeyVaultUrl}"); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + //Get certificate in X509Certificate format |
| 45 | + if (string.IsNullOrEmpty(config.CertificateName)) |
| 46 | + { |
| 47 | + throw new Exception("CertificateName is not set in the configuration."); |
| 48 | + } |
| 49 | + string certificateName = config.CertificateName; |
| 50 | + certificate = client.DownloadCertificate(certificateName).Value; |
| 51 | + |
| 52 | + if (certificate == null) |
| 53 | + { |
| 54 | + throw new Exception($"Certificate {certificateName} not found in KeyVault {config.KeyVaultUrl}"); |
| 55 | + } |
| 56 | +} |
| 57 | +catch (Exception ex) |
| 58 | +{ |
| 59 | + throw new Exception($"Failed to get certificate {config.CertificateName} from KeyVault {config.KeyVaultUrl}", ex); |
| 60 | +} |
| 61 | + |
| 62 | +// Create the ClientCredentialsFactory to user certificate authentication |
| 63 | +builder.Services.AddSingleton<ServiceClientCredentialsFactory>((e) => new CertificateServiceClientCredentialsFactory(certificate, config.BOT_ID, "72f988bf-86f1-41af-91ab-2d7cd011db47")); |
27 | 64 |
|
28 | 65 | // Create the Bot Framework Authentication to be used with the Bot Adapter. |
29 | 66 | builder.Services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>(); |
|
86 | 123 | IPlanner<AppState> planner = new AzureOpenAIPlanner<AppState>(sp.GetService<AzureOpenAIPlannerOptions>(), loggerFactory.CreateLogger<AzureOpenAIPlanner<AppState>>()); |
87 | 124 | //IModerator<AppState> moderator = new AzureContentSafetyModerator<AppState>(sp.GetService<AzureContentSafetyModeratorOptions>(), loggerFactory.CreateLogger<AzureContentSafetyModerator<AppState>>()); |
88 | 125 |
|
89 | | - ApplicationOptions<AppState, AppStateManager> applicationOptions = new ApplicationOptions<AppState, AppStateManager>() |
| 126 | + ApplicationOptions<AppState, AppStateManager> applicationOptions = new() |
90 | 127 | { |
91 | 128 | AI = new AIOptions<AppState>(planner, promptManager) |
92 | 129 | { |
|
0 commit comments