diff --git a/src/TriggerService/AzureStorageUtility.cs b/src/TriggerService/AzureStorageUtility.cs index eb556b9f..fa3d5b6d 100644 --- a/src/TriggerService/AzureStorageUtility.cs +++ b/src/TriggerService/AzureStorageUtility.cs @@ -6,12 +6,12 @@ using System.Linq; using System.Net.Http; using System.Threading.Tasks; -using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.Storage; using Azure.Storage; using Azure.Storage.Blobs; using CommonUtilities.AzureCloud; +using Microsoft.Extensions.Configuration; namespace TriggerService @@ -21,9 +21,10 @@ public interface IAzureStorageUtility Task<(List, IAzureStorage)> GetStorageAccountsUsingMsiAsync(string accountName); } - public class AzureStorageUtility(AzureCloudConfig azureCloudConfig) : IAzureStorageUtility + public class AzureStorageUtility(AzureCloudConfig azureCloudConfig, IConfiguration configuration) : IAzureStorageUtility { private readonly AzureCloudConfig azureCloudConfig = azureCloudConfig; + private readonly IConfiguration configuration = configuration; public async Task<(List, IAzureStorage)> GetStorageAccountsUsingMsiAsync(string accountName) { @@ -76,7 +77,7 @@ private IAsyncEnumerable GetAccessibleStorageAccountsAsync() .SelectMany(a => a); } - private async Task GetStorageAccountKeyAsync(StorageAccountInfo storageAccountInfo) + private static async Task GetStorageAccountKeyAsync(StorageAccountInfo storageAccountInfo) { return (await storageAccountInfo.StorageAccount.GetKeysAsync().FirstOrDefaultAsync()).Value; } @@ -86,7 +87,7 @@ private async Task GetStorageAccountKeyAsync(StorageAccountInfo storageA /// /// An authenticated Azure Client instance private ArmClient GetAzureManagementClient() - => new(new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri(azureCloudConfig.Authentication.LoginEndpointUrl) }), + => new(new CommonUtilities.AzureServicesConnectionStringCredential(new(configuration, azureCloudConfig)), default, new() { Environment = azureCloudConfig.ArmEnvironment }); } diff --git a/src/TriggerService/TriggerService.cs b/src/TriggerService/TriggerService.cs index abbe6a53..47f98ca7 100644 --- a/src/TriggerService/TriggerService.cs +++ b/src/TriggerService/TriggerService.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; -using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.ApplicationInsights; using Azure.ResourceManager.Resources; @@ -25,36 +24,36 @@ internal class TriggerService public TriggerService() => Common.NewtonsoftJsonSafeInit.SetDefaultSettings(); - internal static string applicationInsightsConnectionString = ""; - public static async Task Main() { Console.WriteLine($"TriggerService Build: {Assembly.GetExecutingAssembly().GetCustomAttribute().InformationalVersion}"); AzureCloudConfig azureCloudConfig = null; + var applicationInsightsConnectionString = string.Empty; + IConfiguration configuration = null; await Host.CreateDefaultBuilder() .ConfigureAppConfiguration((hostBuilderContext, configurationBuilder) => { configurationBuilder.AddJsonFile("appsettings.json"); configurationBuilder.AddEnvironmentVariables(); - var config = configurationBuilder.Build(); - azureCloudConfig = GetAzureCloudConfig(config); + configuration = configurationBuilder.Build(); + azureCloudConfig = GetAzureCloudConfig(configuration); var triggerServiceOptions = new TriggerServiceOptions(); - config.Bind(TriggerServiceOptions.TriggerServiceOptionsSectionName, triggerServiceOptions); + configuration.Bind(TriggerServiceOptions.TriggerServiceOptionsSectionName, triggerServiceOptions); const string legacyApplicationInsightsConnectionStringKey = "APPLICATIONINSIGHTS_CONNECTION_STRING"; - if (!string.IsNullOrWhiteSpace(config[legacyApplicationInsightsConnectionStringKey])) + if (!string.IsNullOrWhiteSpace(configuration[legacyApplicationInsightsConnectionStringKey])) { // Legacy CoA setting Console.WriteLine($"Using {legacyApplicationInsightsConnectionStringKey}"); - applicationInsightsConnectionString = config[legacyApplicationInsightsConnectionStringKey]; + applicationInsightsConnectionString = configuration[legacyApplicationInsightsConnectionStringKey]; } else if (!string.IsNullOrWhiteSpace(triggerServiceOptions.ApplicationInsightsAccountName)) { Console.WriteLine($"Getting Azure subscriptions and Application Insights Connection string"); // name was specified, get the subscription, then the connection string from the account - applicationInsightsConnectionString = GetApplicationInsightsConnectionString(azureCloudConfig, triggerServiceOptions); + applicationInsightsConnectionString = GetApplicationInsightsConnectionStringAsync(configuration, azureCloudConfig, triggerServiceOptions).GetAwaiter().GetResult(); Console.WriteLine($"Successfully retrieved applicationInsightsConnectionString: {!string.IsNullOrWhiteSpace(applicationInsightsConnectionString)}"); } @@ -69,7 +68,14 @@ await Host.CreateDefaultBuilder() loggingBuilder.AddApplicationInsights( configuration => { - configuration.ConnectionString = applicationInsightsConnectionString; + if (string.IsNullOrWhiteSpace(applicationInsightsConnectionString)) + { + configuration.DisableTelemetry = true; + } + else + { + configuration.ConnectionString = applicationInsightsConnectionString; + } }, options => { }); @@ -82,7 +88,7 @@ await Host.CreateDefaultBuilder() serviceCollection.AddSingleton(azureCloudConfig); serviceCollection.AddSingleton(); serviceCollection.AddSingleton, TesTaskPostgreSqlRepository>(); - serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(sp => ActivatorUtilities.CreateInstance(sp, configuration)); serviceCollection.AddHostedService(); }) .Build() @@ -97,26 +103,26 @@ static AzureCloudConfig GetAzureCloudConfig(IConfiguration configuration) } } - private static string GetApplicationInsightsConnectionString(AzureCloudConfig azureCloudConfig, TriggerServiceOptions triggerServiceOptions) + private static async Task GetApplicationInsightsConnectionStringAsync(IConfiguration config, AzureCloudConfig azureCloudConfig, TriggerServiceOptions triggerServiceOptions) { try { string applicationInsightsConnectionString; - var tokenCredential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { AuthorityHost = new Uri(azureCloudConfig.Authentication.LoginEndpointUrl) }); + var tokenCredential = new CommonUtilities.AzureServicesConnectionStringCredential(new(config, azureCloudConfig)); ArmClient armClient = new(tokenCredential, null, new() { Environment = azureCloudConfig.ArmEnvironment }); - var subscriptionId = armClient.GetSubscriptions().GetAllAsync().Select(s => s.Id.SubscriptionId).FirstAsync().Result; + var subscriptionId = await armClient.GetSubscriptions().GetAllAsync().Select(s => s.Id.SubscriptionId).FirstAsync(); Console.WriteLine($"Running in subscriptionId: {subscriptionId}"); - applicationInsightsConnectionString = armClient.GetSubscriptionResource(SubscriptionResource.CreateResourceIdentifier(subscriptionId)) - .GetApplicationInsightsComponentsAsync() - .SelectAwait(async c => (await c.GetAsync()).Value) - .FirstAsync(c => c.Data.ApplicationId.Equals(triggerServiceOptions.ApplicationInsightsAccountName, StringComparison.OrdinalIgnoreCase)) - .Result.Data.ConnectionString; + applicationInsightsConnectionString = (await armClient.GetSubscriptionResource(SubscriptionResource.CreateResourceIdentifier(subscriptionId)) + .GetApplicationInsightsComponentsAsync() + .SelectAwait(async c => (await c.GetAsync()).Value) + .FirstAsync(c => c.Data.ApplicationId.Equals(triggerServiceOptions.ApplicationInsightsAccountName, StringComparison.OrdinalIgnoreCase))) + .Data.ConnectionString; return applicationInsightsConnectionString; } catch (Exception exc) { - Console.WriteLine($"Exception in {nameof(GetApplicationInsightsConnectionString)}: {exc.Message} {exc}"); + Console.WriteLine($"Exception in {nameof(GetApplicationInsightsConnectionStringAsync)}: {exc.Message} {exc}"); throw; } } diff --git a/src/deploy-cromwell-on-azure/deploy-cromwell-on-azure.csproj b/src/deploy-cromwell-on-azure/deploy-cromwell-on-azure.csproj index bed23c90..d60111f6 100644 --- a/src/deploy-cromwell-on-azure/deploy-cromwell-on-azure.csproj +++ b/src/deploy-cromwell-on-azure/deploy-cromwell-on-azure.csproj @@ -48,6 +48,8 @@ + +