170 lines
5.8 KiB
C#
170 lines
5.8 KiB
C#
// -----------------------------------------------------------------------------
|
|
// VexGateServiceCollectionExtensions.cs
|
|
// Sprint: SPRINT_20260106_003_002_SCANNER_vex_gate_service
|
|
// Task: T028 - Add gate policy to tenant configuration
|
|
// Description: Service collection extensions for registering VEX gate services.
|
|
// -----------------------------------------------------------------------------
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace StellaOps.Scanner.Gate;
|
|
|
|
/// <summary>
|
|
/// Extension methods for registering VEX gate services.
|
|
/// </summary>
|
|
public static class VexGateServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds VEX gate services with configuration from the specified section.
|
|
/// </summary>
|
|
/// <param name="services">The service collection.</param>
|
|
/// <param name="configuration">The configuration root.</param>
|
|
/// <returns>The service collection for chaining.</returns>
|
|
public static IServiceCollection AddVexGate(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
// Bind and validate options
|
|
services.AddOptions<VexGateOptions>()
|
|
.Bind(configuration.GetSection(VexGateOptions.SectionName))
|
|
.ValidateDataAnnotations()
|
|
.ValidateOnStart();
|
|
|
|
// Register policy from options
|
|
services.AddSingleton<VexGatePolicy>(sp =>
|
|
{
|
|
var options = sp.GetRequiredService<IOptions<VexGateOptions>>();
|
|
if (!options.Value.Enabled)
|
|
{
|
|
// Return a permissive policy when disabled
|
|
return new VexGatePolicy
|
|
{
|
|
DefaultDecision = VexGateDecision.Pass,
|
|
Rules = [],
|
|
};
|
|
}
|
|
|
|
return options.Value.ToPolicy();
|
|
});
|
|
|
|
// Register core services
|
|
services.AddSingleton<IVexGatePolicy, VexGatePolicyEvaluator>();
|
|
|
|
// Register caching with configured limits
|
|
services.AddSingleton<IMemoryCache>(sp =>
|
|
{
|
|
var options = sp.GetRequiredService<IOptions<VexGateOptions>>();
|
|
return new MemoryCache(new MemoryCacheOptions
|
|
{
|
|
SizeLimit = options.Value.Cache.MaxEntries,
|
|
});
|
|
});
|
|
|
|
// Register VEX gate service
|
|
services.AddSingleton<IVexGateService, VexGateService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds VEX gate services with explicit options.
|
|
/// </summary>
|
|
/// <param name="services">The service collection.</param>
|
|
/// <param name="configureOptions">The options configuration action.</param>
|
|
/// <returns>The service collection for chaining.</returns>
|
|
public static IServiceCollection AddVexGate(
|
|
this IServiceCollection services,
|
|
Action<VexGateOptions> configureOptions)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
ArgumentNullException.ThrowIfNull(configureOptions);
|
|
|
|
// Configure and validate options
|
|
services.AddOptions<VexGateOptions>()
|
|
.Configure(configureOptions)
|
|
.ValidateDataAnnotations()
|
|
.ValidateOnStart();
|
|
|
|
// Register policy from options
|
|
services.AddSingleton<VexGatePolicy>(sp =>
|
|
{
|
|
var options = sp.GetRequiredService<IOptions<VexGateOptions>>();
|
|
if (!options.Value.Enabled)
|
|
{
|
|
return new VexGatePolicy
|
|
{
|
|
DefaultDecision = VexGateDecision.Pass,
|
|
Rules = [],
|
|
};
|
|
}
|
|
|
|
return options.Value.ToPolicy();
|
|
});
|
|
|
|
// Register core services
|
|
services.AddSingleton<IVexGatePolicy, VexGatePolicyEvaluator>();
|
|
|
|
// Register caching with configured limits
|
|
services.AddSingleton<IMemoryCache>(sp =>
|
|
{
|
|
var options = sp.GetRequiredService<IOptions<VexGateOptions>>();
|
|
return new MemoryCache(new MemoryCacheOptions
|
|
{
|
|
SizeLimit = options.Value.Cache.MaxEntries,
|
|
});
|
|
});
|
|
|
|
// Register VEX gate service
|
|
services.AddSingleton<IVexGateService, VexGateService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds VEX gate services with default policy.
|
|
/// </summary>
|
|
/// <param name="services">The service collection.</param>
|
|
/// <returns>The service collection for chaining.</returns>
|
|
public static IServiceCollection AddVexGateWithDefaultPolicy(this IServiceCollection services)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(services);
|
|
|
|
// Configure with default options
|
|
services.AddOptions<VexGateOptions>()
|
|
.Configure(options =>
|
|
{
|
|
options.Enabled = true;
|
|
var defaultPolicy = VexGatePolicy.Default;
|
|
options.DefaultDecision = defaultPolicy.DefaultDecision.ToString();
|
|
options.Rules = defaultPolicy.Rules
|
|
.Select(VexGateRuleOptions.FromRule)
|
|
.ToList();
|
|
})
|
|
.ValidateDataAnnotations()
|
|
.ValidateOnStart();
|
|
|
|
// Register default policy
|
|
services.AddSingleton<VexGatePolicy>(_ => VexGatePolicy.Default);
|
|
|
|
// Register core services
|
|
services.AddSingleton<IVexGatePolicy, VexGatePolicyEvaluator>();
|
|
|
|
// Register caching with default limits
|
|
services.AddSingleton<IMemoryCache>(_ => new MemoryCache(new MemoryCacheOptions
|
|
{
|
|
SizeLimit = 10000,
|
|
}));
|
|
|
|
// Register VEX gate service
|
|
services.AddSingleton<IVexGateService, VexGateService>();
|
|
|
|
return services;
|
|
}
|
|
}
|