Add Policy DSL Validator, Schema Exporter, and Simulation Smoke tools
- Implemented PolicyDslValidator with command-line options for strict mode and JSON output. - Created PolicySchemaExporter to generate JSON schemas for policy-related models. - Developed PolicySimulationSmoke tool to validate policy simulations against expected outcomes. - Added project files and necessary dependencies for each tool. - Ensured proper error handling and usage instructions across tools.
This commit is contained in:
		
							
								
								
									
										104
									
								
								src/StellaOps.Scheduler.Worker/ImpactTargetingService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								src/StellaOps.Scheduler.Worker/ImpactTargetingService.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,104 @@
 | 
			
		||||
using System.Collections.Immutable;
 | 
			
		||||
using StellaOps.Scheduler.ImpactIndex;
 | 
			
		||||
using StellaOps.Scheduler.Models;
 | 
			
		||||
 | 
			
		||||
namespace StellaOps.Scheduler.Worker;
 | 
			
		||||
 | 
			
		||||
public interface IImpactTargetingService
 | 
			
		||||
{
 | 
			
		||||
    ValueTask<ImpactSet> ResolveByPurlsAsync(
 | 
			
		||||
        IEnumerable<string> productKeys,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        CancellationToken cancellationToken = default);
 | 
			
		||||
 | 
			
		||||
    ValueTask<ImpactSet> ResolveByVulnerabilitiesAsync(
 | 
			
		||||
        IEnumerable<string> vulnerabilityIds,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        CancellationToken cancellationToken = default);
 | 
			
		||||
 | 
			
		||||
    ValueTask<ImpactSet> ResolveAllAsync(
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        CancellationToken cancellationToken = default);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public sealed class ImpactTargetingService : IImpactTargetingService
 | 
			
		||||
{
 | 
			
		||||
    private readonly IImpactIndex _impactIndex;
 | 
			
		||||
    private readonly TimeProvider _timeProvider;
 | 
			
		||||
 | 
			
		||||
    public ImpactTargetingService(IImpactIndex impactIndex, TimeProvider? timeProvider = null)
 | 
			
		||||
    {
 | 
			
		||||
        _impactIndex = impactIndex ?? throw new ArgumentNullException(nameof(impactIndex));
 | 
			
		||||
        _timeProvider = timeProvider ?? TimeProvider.System;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async ValueTask<ImpactSet> ResolveByPurlsAsync(
 | 
			
		||||
        IEnumerable<string> productKeys,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        CancellationToken cancellationToken = default)
 | 
			
		||||
    {
 | 
			
		||||
        ArgumentNullException.ThrowIfNull(productKeys);
 | 
			
		||||
        ArgumentNullException.ThrowIfNull(selector);
 | 
			
		||||
 | 
			
		||||
        var distinct = productKeys
 | 
			
		||||
            .Where(static key => !string.IsNullOrWhiteSpace(key))
 | 
			
		||||
            .Select(static key => key.Trim())
 | 
			
		||||
            .Distinct(StringComparer.OrdinalIgnoreCase)
 | 
			
		||||
            .ToArray();
 | 
			
		||||
 | 
			
		||||
        if (distinct.Length == 0)
 | 
			
		||||
        {
 | 
			
		||||
            return CreateEmptyImpactSet(selector, usageOnly);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return await _impactIndex.ResolveByPurlsAsync(distinct, usageOnly, selector, cancellationToken).ConfigureAwait(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async ValueTask<ImpactSet> ResolveByVulnerabilitiesAsync(
 | 
			
		||||
        IEnumerable<string> vulnerabilityIds,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        CancellationToken cancellationToken = default)
 | 
			
		||||
    {
 | 
			
		||||
        ArgumentNullException.ThrowIfNull(vulnerabilityIds);
 | 
			
		||||
        ArgumentNullException.ThrowIfNull(selector);
 | 
			
		||||
 | 
			
		||||
        var distinct = vulnerabilityIds
 | 
			
		||||
            .Where(static id => !string.IsNullOrWhiteSpace(id))
 | 
			
		||||
            .Select(static id => id.Trim())
 | 
			
		||||
            .Distinct(StringComparer.OrdinalIgnoreCase)
 | 
			
		||||
            .ToArray();
 | 
			
		||||
 | 
			
		||||
        if (distinct.Length == 0)
 | 
			
		||||
        {
 | 
			
		||||
            return CreateEmptyImpactSet(selector, usageOnly);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return await _impactIndex.ResolveByVulnerabilitiesAsync(distinct, usageOnly, selector, cancellationToken).ConfigureAwait(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ValueTask<ImpactSet> ResolveAllAsync(
 | 
			
		||||
        Selector selector,
 | 
			
		||||
        bool usageOnly,
 | 
			
		||||
        CancellationToken cancellationToken = default)
 | 
			
		||||
    {
 | 
			
		||||
        ArgumentNullException.ThrowIfNull(selector);
 | 
			
		||||
        return _impactIndex.ResolveAllAsync(selector, usageOnly, cancellationToken);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ImpactSet CreateEmptyImpactSet(Selector selector, bool usageOnly)
 | 
			
		||||
    {
 | 
			
		||||
        return new ImpactSet(
 | 
			
		||||
            selector,
 | 
			
		||||
            ImmutableArray<ImpactImage>.Empty,
 | 
			
		||||
            usageOnly,
 | 
			
		||||
            _timeProvider.GetUtcNow(),
 | 
			
		||||
            total: 0,
 | 
			
		||||
            snapshotId: null,
 | 
			
		||||
            schemaVersion: SchedulerSchemaVersions.ImpactSet);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user