diff --git a/.gitignore b/.gitignore index bc6c934..94837e5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ !.vscode/launch.json !.vscode/extensions.json !.vscode/*.code-snippets +.idea/* # Local History for Visual Studio Code .history/ diff --git a/Azure.Endpoints/ResourceGroup/ActivityLogReader.cs b/Azure.Endpoints/ResourceGroup/ActivityLogReader.cs index bec75a9..4789e8c 100644 --- a/Azure.Endpoints/ResourceGroup/ActivityLogReader.cs +++ b/Azure.Endpoints/ResourceGroup/ActivityLogReader.cs @@ -1,3 +1,4 @@ +using Azure.ResourceManager; using Microsoft.Extensions.Logging; namespace ProperDI.Azure.Endpoints.ResourceGroup.LogLooker; @@ -7,24 +8,21 @@ public interface IActivityLogReader Task Scan(CancellationToken cancellationToken); } -public class ActivityLogReader : IActivityLogReader +public class ActivityLogReader( + ILogger logger, + ArmClient armClient) + : IActivityLogReader { - private readonly ILogger _logger; - private readonly HttpClient _httpClient; - - public ActivityLogReader(IHttpClientFactory httpClientFactory, ILogger logger) - { - _httpClient = httpClientFactory.CreateClient() ?? throw new ArgumentNullException(nameof(httpClientFactory)); - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } + private readonly ILogger _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + private readonly ArmClient _armClient = armClient ?? throw new ArgumentNullException(nameof(armClient)); public async Task Scan(CancellationToken cancellationToken) { try { - var response = await _httpClient.GetAsync("https://ident.me", cancellationToken); - var responseBody = await response.Content.ReadAsStringAsync(); - _logger.LogInformation("Response body: {content}", responseBody); + _logger.LogInformation("Scanning for resources"); + var resp = await _armClient.GetDefaultSubscriptionAsync(cancellationToken); + _logger.LogInformation("Found default subscription {sub}", resp.Data.DisplayName); } catch (TaskCanceledException) { diff --git a/Program.cs b/Program.cs index db42d63..2b1d5f8 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,8 @@ -using Microsoft.Extensions.DependencyInjection; +using Azure.Identity; +using Azure.ResourceManager; +using Azure.ResourceManager.Resources; +using Microsoft.Extensions.Azure; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using ProperDI.Azure.Endpoints.ResourceGroup.LogLooker; @@ -6,6 +10,19 @@ HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); builder.Services.AddHttpClient(); builder.Services.AddTransient(); +builder.Services.AddAzureClients(clientBuilder => +{ + // var creds = new ClientSecretCredential( + // "", + // "", + // ""); + clientBuilder.UseCredential(new DefaultAzureCredential()); + clientBuilder.AddArmClient("2690a5f1-155b-4fa8-896f-92c6bcb62bee"); + clientBuilder.ConfigureDefaults(client => + { + client.Retry.MaxRetries = 3; + }); +}); builder.Services.Configure(builder.Configuration.GetSection("RoleAssessorBackgroundService")); builder.Services.AddHostedService(); diff --git a/Sentinel.csproj b/Sentinel.csproj index abf59c4..825b962 100644 --- a/Sentinel.csproj +++ b/Sentinel.csproj @@ -8,6 +8,10 @@ + + + + diff --git a/appsettings.json b/appsettings.json index eecb6a9..dbba275 100644 --- a/appsettings.json +++ b/appsettings.json @@ -8,6 +8,7 @@ } }, "RoleAssessorBackgroundService": { - "RunFrequencyInMinutes": 1 - } + "RunFrequencyInMinutes": 12 + }, + "DefaultSubscriptionId": "2690a5f1-155b-4fa8-896f-92c6bcb62bee" } \ No newline at end of file