using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using StellaOps.Feedser.Source.Common.Fetch; using StellaOps.Feedser.Source.Common.Http; using StellaOps.Feedser.Source.Vndr.Cisco.Configuration; using StellaOps.Feedser.Source.Vndr.Cisco.Internal; namespace StellaOps.Feedser.Source.Vndr.Cisco; public static class CiscoServiceCollectionExtensions { public static IServiceCollection AddCiscoConnector(this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Configure(configure) .PostConfigure(static opts => opts.Validate()) .ValidateOnStart(); services.TryAddSingleton(_ => TimeProvider.System); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); services.AddHttpClient(CiscoOptions.AuthHttpClientName) .ConfigureHttpClient((sp, client) => { var options = sp.GetRequiredService>().Value; client.Timeout = options.RequestTimeout; client.DefaultRequestHeaders.UserAgent.ParseAdd("StellaOps.Feedser.Cisco/1.0"); client.DefaultRequestHeaders.Accept.ParseAdd("application/json"); if (options.TokenEndpoint is not null) { client.BaseAddress = new Uri(options.TokenEndpoint.GetLeftPart(UriPartial.Authority)); } }); services.AddSourceHttpClient(CiscoOptions.HttpClientName, static (sp, clientOptions) => { var options = sp.GetRequiredService>().Value; clientOptions.Timeout = options.RequestTimeout; clientOptions.UserAgent = "StellaOps.Feedser.Cisco/1.0"; clientOptions.AllowedHosts.Clear(); clientOptions.AllowedHosts.Add(options.BaseUri.Host); clientOptions.AllowedHosts.Add("sec.cloudapps.cisco.com"); clientOptions.AllowedHosts.Add("www.cisco.com"); clientOptions.MaxAttempts = 5; clientOptions.BaseDelay = TimeSpan.FromSeconds(2); }).AddHttpMessageHandler(); services.AddSingleton(sp => { var fetchService = sp.GetRequiredService(); var optionsMonitor = sp.GetRequiredService>(); var logger = sp.GetRequiredService>(); return new CiscoOpenVulnClient(fetchService, optionsMonitor, logger, VndrCiscoConnectorPlugin.SourceName); }); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); return services; } }