This commit is contained in:
robbwilcox
2024-10-14 15:57:55 -05:00
parent cd9682379d
commit a099b77bf4
4 changed files with 65 additions and 64 deletions

View File

@@ -81,6 +81,7 @@ public class RoleAssesorBackgroundService : IHostedService, IDisposable
public void Dispose() public void Dispose()
{ {
GC.SuppressFinalize(this);
_timer?.Dispose(); _timer?.Dispose();
} }
} }

View File

@@ -2,55 +2,29 @@
using Microsoft.Extensions.Azure; using Microsoft.Extensions.Azure;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
using ProperDI.Azure.Endpoints.ResourceGroup; using ProperDI.Azure.Endpoints.ResourceGroup;
public class Program HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHttpClient();
builder.Services.AddTransient<IActivityLogReader, ActivityLogReader>();
builder.Services.AddAzureClients(clientBuilder =>
{ {
public static async Task Main(string[] args) // var creds = new ClientSecretCredential(
{ // "",
var host = CreateHostBuilder(args).Build(); // "",
await host.RunAsync(); // "");
} clientBuilder.UseCredential(new DefaultAzureCredential());
clientBuilder.AddArmClient(builder.Configuration.GetSection("Am95DevSubscriptionId"));
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
// Optionally, you can add JSON configuration if needed
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
})
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient();
services.AddTransient<IActivityLogReader, ActivityLogReader>();
services.AddAzureClients(clientBuilder =>
{
var configuration = hostContext.Configuration;
// Read Azure credentials from environment variables
var tenantId = configuration["AzureTenantId"];
var clientId = configuration["AzureClientId"];
var clientSecret = configuration["AzureClientSecret"];
var subscriptionId = configuration["AzureSubscriptionId"];
System.Console.WriteLine($"Tenant id: {tenantId}\nClientID: {clientId}\nClientSecret: {clientSecret}");
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
clientBuilder.UseCredential(credential);
clientBuilder.AddArmClient(subscriptionId);
clientBuilder.ConfigureDefaults(client => clientBuilder.ConfigureDefaults(client =>
{ {
client.Retry.MaxRetries = 3; client.Retry.MaxRetries = 3;
}); });
clientBuilder.AddLogsQueryClient(); });
});
services.Configure<RoleAssesorBackgroundServiceOptions>( builder.Services.Configure<RoleAssesorBackgroundServiceOptions>(builder.Configuration.GetSection("RoleAssessorBackgroundService"));
hostContext.Configuration.GetSection("RoleAssessorBackgroundService")); builder.Services.AddHostedService<RoleAssesorBackgroundService>();
services.AddHostedService<RoleAssesorBackgroundService>();
}); var host = builder.Build();
}
await host.RunAsync();

25
Sentinel.sln Normal file
View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sentinel", "Sentinel.csproj", "{FA937A3C-5191-4DAC-AC62-EBF7EE90BD1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FA937A3C-5191-4DAC-AC62-EBF7EE90BD1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA937A3C-5191-4DAC-AC62-EBF7EE90BD1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA937A3C-5191-4DAC-AC62-EBF7EE90BD1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA937A3C-5191-4DAC-AC62-EBF7EE90BD1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {79EE9B52-D46E-4FBB-8310-32FD5835CF1D}
EndGlobalSection
EndGlobal

View File

@@ -12,6 +12,7 @@
"RoleAssessorBackgroundService": { "RoleAssessorBackgroundService": {
"RunFrequencyInMinutes": 12 "RunFrequencyInMinutes": 12
}, },
"Am95DevSubscriptionId": "040698c2-a013-45be-b1fa-e975f46b9d63",
"DefaultSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee", "DefaultSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee",
"ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_ENVIRONMENT": "Development",
"AzureSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee", "AzureSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee",