| | | 1 | | using AsiBackbone.Core.Constraints; |
| | | 2 | | using AsiBackbone.Core.ThreatModeling; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.Evaluation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Fluent builder for <see cref="DefaultGovernancePolicyEvaluator{TContext}" />. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Every call to <see cref="Build" /> creates an independent evaluator that snapshots the constraints and threat model |
| | | 12 | | /// contributors added so far. Later builder changes do not affect evaluators that were already built. Options are |
| | | 13 | | /// validated and frozen by the first evaluator built with them. |
| | | 14 | | /// </remarks> |
| | | 15 | | /// <typeparam name="TContext">The framework-neutral evaluation context type.</typeparam> |
| | | 16 | | public sealed class GovernancePolicyEvaluatorBuilder<TContext> |
| | | 17 | | where TContext : IGovernanceEvaluationContext |
| | | 18 | | { |
| | 8 | 19 | | private readonly List<IGovernanceConstraint<TContext>> constraints = []; |
| | 8 | 20 | | private readonly List<IThreatModelContributor<TContext>> threatModelContributors = []; |
| | | 21 | | private IGovernanceDecisionPolicy<TContext>? decisionPolicy; |
| | | 22 | | private GovernancePolicyOptions? options; |
| | | 23 | | private ILogger<DefaultGovernancePolicyEvaluator<TContext>>? logger; |
| | | 24 | | |
| | 8 | 25 | | internal GovernancePolicyEvaluatorBuilder() |
| | | 26 | | { |
| | 8 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Adds a constraint to the end of the active policy structure. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="constraint">The constraint to add.</param> |
| | | 33 | | /// <returns>The same builder instance.</returns> |
| | | 34 | | public GovernancePolicyEvaluatorBuilder<TContext> AddConstraint(IGovernanceConstraint<TContext> constraint) |
| | | 35 | | { |
| | 10 | 36 | | ArgumentNullException.ThrowIfNull(constraint); |
| | 8 | 37 | | constraints.Add(constraint); |
| | 8 | 38 | | return this; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Adds constraints, in order, to the end of the active policy structure. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="constraints">The constraints to add.</param> |
| | | 45 | | /// <returns>The same builder instance.</returns> |
| | | 46 | | public GovernancePolicyEvaluatorBuilder<TContext> AddConstraints(IEnumerable<IGovernanceConstraint<TContext>> constr |
| | | 47 | | { |
| | 3 | 48 | | ArgumentNullException.ThrowIfNull(constraints); |
| | 9 | 49 | | foreach (IGovernanceConstraint<TContext> constraint in constraints) |
| | | 50 | | { |
| | 3 | 51 | | AddConstraint(constraint); |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | return this; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Adds a threat model contributor that inspects the context before constraint composition. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="contributor">The contributor to add.</param> |
| | | 61 | | /// <returns>The same builder instance.</returns> |
| | | 62 | | public GovernancePolicyEvaluatorBuilder<TContext> AddThreatModelContributor(IThreatModelContributor<TContext> contri |
| | | 63 | | { |
| | 4 | 64 | | ArgumentNullException.ThrowIfNull(contributor); |
| | 2 | 65 | | threatModelContributors.Add(contributor); |
| | 2 | 66 | | return this; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Adds threat model contributors, in order, that inspect the context before constraint composition. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="contributors">The contributors to add.</param> |
| | | 73 | | /// <returns>The same builder instance.</returns> |
| | | 74 | | public GovernancePolicyEvaluatorBuilder<TContext> AddThreatModelContributors(IEnumerable<IThreatModelContributor<TCo |
| | | 75 | | { |
| | 3 | 76 | | ArgumentNullException.ThrowIfNull(contributors); |
| | 7 | 77 | | foreach (IThreatModelContributor<TContext> contributor in contributors) |
| | | 78 | | { |
| | 2 | 79 | | AddThreatModelContributor(contributor); |
| | | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | return this; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Sets the decision policy applied after composition, replacing any previously set policy. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="decisionPolicy">The decision policy, or <see langword="null" /> for none.</param> |
| | | 89 | | /// <returns>The same builder instance.</returns> |
| | | 90 | | public GovernancePolicyEvaluatorBuilder<TContext> WithDecisionPolicy(IGovernanceDecisionPolicy<TContext>? decisionPo |
| | | 91 | | { |
| | 3 | 92 | | this.decisionPolicy = decisionPolicy; |
| | 3 | 93 | | return this; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Sets the evaluator options, replacing any previously set options. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="options">The options, or <see langword="null" /> for defaults.</param> |
| | | 100 | | /// <returns>The same builder instance.</returns> |
| | | 101 | | public GovernancePolicyEvaluatorBuilder<TContext> WithOptions(GovernancePolicyOptions? options) |
| | | 102 | | { |
| | 1 | 103 | | this.options = options; |
| | 1 | 104 | | return this; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Sets the logger used to emit operational warning signals, replacing any previously set logger. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="logger">The logger, or <see langword="null" /> for none.</param> |
| | | 111 | | /// <returns>The same builder instance.</returns> |
| | | 112 | | public GovernancePolicyEvaluatorBuilder<TContext> WithLogger(ILogger<DefaultGovernancePolicyEvaluator<TContext>>? lo |
| | | 113 | | { |
| | 1 | 114 | | this.logger = logger; |
| | 1 | 115 | | return this; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Creates an evaluator from the current builder state. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <returns>A new evaluator.</returns> |
| | | 122 | | public DefaultGovernancePolicyEvaluator<TContext> Build() |
| | | 123 | | { |
| | 8 | 124 | | return new DefaultGovernancePolicyEvaluator<TContext>(constraints, threatModelContributors, decisionPolicy, opti |
| | | 125 | | } |
| | | 126 | | } |