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(
// "",
// "",
// "");
clientBuilder.UseCredential(new DefaultAzureCredential());
clientBuilder.AddArmClient(builder.Configuration.GetSection("Am95DevSubscriptionId"));
clientBuilder.ConfigureDefaults(client =>
{ {
var host = CreateHostBuilder(args).Build(); client.Retry.MaxRetries = 3;
await host.RunAsync(); });
} });
public static IHostBuilder CreateHostBuilder(string[] args) => builder.Services.Configure<RoleAssesorBackgroundServiceOptions>(builder.Configuration.GetSection("RoleAssessorBackgroundService"));
Host.CreateDefaultBuilder(args) builder.Services.AddHostedService<RoleAssesorBackgroundService>();
.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 host = builder.Build();
{
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); await host.RunAsync();
clientBuilder.AddArmClient(subscriptionId);
clientBuilder.ConfigureDefaults(client =>
{
client.Retry.MaxRetries = 3;
});
clientBuilder.AddLogsQueryClient();
});
services.Configure<RoleAssesorBackgroundServiceOptions>(
hostContext.Configuration.GetSection("RoleAssessorBackgroundService"));
services.AddHostedService<RoleAssesorBackgroundService>();
});
}

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

@@ -1,21 +1,22 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information", "Microsoft.Hosting.Lifetime": "Information",
"System.Net": "Warning", "System.Net": "Warning",
"Azure.Core": "Warning", "Azure.Core": "Warning",
"Azure.Identity": "Warning" "Azure.Identity": "Warning"
} }
}, },
"RoleAssessorBackgroundService": { "RoleAssessorBackgroundService": {
"RunFrequencyInMinutes": 12 "RunFrequencyInMinutes": 12
}, },
"DefaultSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee", "Am95DevSubscriptionId": "040698c2-a013-45be-b1fa-e975f46b9d63",
"ASPNETCORE_ENVIRONMENT": "Development", "DefaultSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee",
"AzureSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee", "ASPNETCORE_ENVIRONMENT": "Development",
"AzureTenantId": "11ffe5d3-5e72-4414-8dfd-1eb209497679", "AzureSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee",
"AzureClientId": "d0870778-24dd-4c87-a449-2c36674035d1", "AzureTenantId": "11ffe5d3-5e72-4414-8dfd-1eb209497679",
"AzureClientSecret": "EWX8Q~jpfKab8hB1TzujorzKIeJEV5NKEfCuOaFC" "AzureClientId": "d0870778-24dd-4c87-a449-2c36674035d1",
"AzureClientSecret": "EWX8Q~jpfKab8hB1TzujorzKIeJEV5NKEfCuOaFC"
} }