Skip to content

Commit

Permalink
Merge branch 'main' into bmurri/simplify-task-management
Browse files Browse the repository at this point in the history
  • Loading branch information
BMurri committed Nov 26, 2024
2 parents aa5372b + 7b3ffd7 commit 10fff51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/TriggerService/AzureStorageUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,9 +21,10 @@ public interface IAzureStorageUtility
Task<(List<IAzureStorage>, 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>, IAzureStorage)> GetStorageAccountsUsingMsiAsync(string accountName)
{
Expand Down Expand Up @@ -76,7 +77,7 @@ private IAsyncEnumerable<StorageAccountInfo> GetAccessibleStorageAccountsAsync()
.SelectMany(a => a);
}

private async Task<string> GetStorageAccountKeyAsync(StorageAccountInfo storageAccountInfo)
private static async Task<string> GetStorageAccountKeyAsync(StorageAccountInfo storageAccountInfo)
{
return (await storageAccountInfo.StorageAccount.GetKeysAsync().FirstOrDefaultAsync()).Value;
}
Expand All @@ -86,7 +87,7 @@ private async Task<string> GetStorageAccountKeyAsync(StorageAccountInfo storageA
/// </summary>
/// <returns>An authenticated Azure Client instance</returns>
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 });
}
Expand Down
46 changes: 26 additions & 20 deletions src/TriggerService/TriggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AssemblyInformationalVersionAttribute>().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)}");
}
Expand All @@ -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 => { });

Expand All @@ -82,7 +88,7 @@ await Host.CreateDefaultBuilder()
serviceCollection.AddSingleton(azureCloudConfig);
serviceCollection.AddSingleton<ICromwellApiClient, CromwellApiClient.CromwellApiClient>();
serviceCollection.AddSingleton<IRepository<TesTask>, TesTaskPostgreSqlRepository>();
serviceCollection.AddSingleton<IAzureStorageUtility, AzureStorageUtility>();
serviceCollection.AddSingleton<IAzureStorageUtility, AzureStorageUtility>(sp => ActivatorUtilities.CreateInstance<AzureStorageUtility>(sp, configuration));
serviceCollection.AddHostedService<TriggerHostedService>();
})
.Build()
Expand All @@ -97,26 +103,26 @@ static AzureCloudConfig GetAzureCloudConfig(IConfiguration configuration)
}
}

private static string GetApplicationInsightsConnectionString(AzureCloudConfig azureCloudConfig, TriggerServiceOptions triggerServiceOptions)
private static async Task<string> 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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/deploy-cromwell-on-azure/deploy-cromwell-on-azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.1.2" />
<!--Mitigate reported security issues-->
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<!--Mitigate reported security issues-->
<PackageReference Include="System.Private.Uri" Version="4.3.2" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 10fff51

Please sign in to comment.