| | | 1 | | namespace AsiBackbone.Core.Evaluation; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides host-configurable options for the default AsiBackbone policy evaluator. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// Options remain mutable while a host is configuring them. The default evaluator validates and freezes the supplied |
| | | 8 | | /// instance during construction so later caller mutation cannot change evaluator behavior. Configure a separate options |
| | | 9 | | /// instance for each evaluator posture that must differ. |
| | | 10 | | /// </remarks> |
| | | 11 | | public sealed class AsiBackbonePolicyEvaluatorOptions |
| | | 12 | | { |
| | | 13 | | private const string DefaultNoConstraintsReasonMessage = |
| | | 14 | | "No policy constraints were registered or supplied for evaluation."; |
| | | 15 | | |
| | | 16 | | private const string DefaultConstraintExceptionReasonMessage = |
| | | 17 | | "A policy constraint failed during evaluation. The operation was denied by the evaluator failure policy."; |
| | | 18 | | |
| | | 19 | | private const string DefaultThreatContributorExceptionReasonMessage = |
| | | 20 | | "A threat model contributor failed during evaluation. The operation was denied by the evaluator failure policy." |
| | | 21 | | private bool isFrozen; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the default machine-readable reason code used when strict empty-policy evaluation denies execution. |
| | | 25 | | /// </summary> |
| | | 26 | | public const string DefaultNoConstraintsReasonCode = "asibackbone.policy.no_constraints"; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the default machine-readable reason code used when constraint exception evaluation denies execution. |
| | | 30 | | /// </summary> |
| | | 31 | | public const string DefaultConstraintExceptionReasonCode = "asibackbone.policy.constraint_exception"; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the default machine-readable reason code used when threat contributor exception evaluation denies execution |
| | | 35 | | /// </summary> |
| | | 36 | | public const string DefaultThreatContributorExceptionReasonCode = "asibackbone.threat.contributor_exception"; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets a value indicating whether evaluation should deny when no constraints are registered or supplied. |
| | | 40 | | /// </summary> |
| | | 41 | | public bool DenyWhenNoConstraints |
| | | 42 | | { |
| | 47 | 43 | | get; |
| | | 44 | | set |
| | | 45 | | { |
| | 39 | 46 | | ThrowIfFrozen(); |
| | 37 | 47 | | field = value; |
| | 37 | 48 | | } |
| | 309 | 49 | | } = true; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets a value indicating whether evaluation should stop after the first denied constraint result. |
| | | 53 | | /// </summary> |
| | | 54 | | public bool ShortCircuitOnFirstDenial |
| | | 55 | | { |
| | 720 | 56 | | get; |
| | | 57 | | set |
| | | 58 | | { |
| | 22 | 59 | | ThrowIfFrozen(); |
| | 22 | 60 | | field = value; |
| | 22 | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets or sets a value indicating whether constraint exceptions should be converted into denied governance decisio |
| | | 66 | | /// </summary> |
| | | 67 | | public bool TreatConstraintExceptionAsDenial |
| | | 68 | | { |
| | 40 | 69 | | get; |
| | | 70 | | set |
| | | 71 | | { |
| | 47 | 72 | | ThrowIfFrozen(); |
| | 47 | 73 | | field = value; |
| | 47 | 74 | | } |
| | 309 | 75 | | } = true; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets or sets a value indicating whether threat contributor exceptions should be converted into denied governance |
| | | 79 | | /// </summary> |
| | | 80 | | public bool TreatThreatContributorExceptionAsDenial |
| | | 81 | | { |
| | 32 | 82 | | get; |
| | | 83 | | set |
| | | 84 | | { |
| | 49 | 85 | | ThrowIfFrozen(); |
| | 49 | 86 | | field = value; |
| | 49 | 87 | | } |
| | 309 | 88 | | } = true; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets or sets a value indicating whether threat assessment outcomes should be protected from being downgraded to |
| | | 92 | | /// </summary> |
| | | 93 | | public bool PreventThreatAssessmentAllowDowngrade |
| | | 94 | | { |
| | 52 | 95 | | get; |
| | | 96 | | set |
| | | 97 | | { |
| | 51 | 98 | | ThrowIfFrozen(); |
| | 51 | 99 | | field = value; |
| | 51 | 100 | | } |
| | 309 | 101 | | } = true; |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets or sets the machine-readable reason code used when <see cref="DenyWhenNoConstraints" /> denies an empty pol |
| | | 105 | | /// </summary> |
| | | 106 | | public string NoConstraintsReasonCode |
| | | 107 | | { |
| | 326 | 108 | | get; |
| | | 109 | | set |
| | | 110 | | { |
| | 10 | 111 | | ThrowIfFrozen(); |
| | 10 | 112 | | field = value; |
| | 10 | 113 | | } |
| | 309 | 114 | | } = DefaultNoConstraintsReasonCode; |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Gets or sets the reason message used when <see cref="DenyWhenNoConstraints" /> denies an empty policy. |
| | | 118 | | /// </summary> |
| | | 119 | | public string NoConstraintsReasonMessage |
| | | 120 | | { |
| | 318 | 121 | | get; |
| | | 122 | | set |
| | | 123 | | { |
| | 10 | 124 | | ThrowIfFrozen(); |
| | 10 | 125 | | field = value; |
| | 10 | 126 | | } |
| | 309 | 127 | | } = DefaultNoConstraintsReasonMessage; |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets or sets the machine-readable reason code used when <see cref="TreatConstraintExceptionAsDenial" /> denies a |
| | | 131 | | /// </summary> |
| | | 132 | | public string ConstraintExceptionReasonCode |
| | | 133 | | { |
| | 310 | 134 | | get; |
| | | 135 | | set |
| | | 136 | | { |
| | 10 | 137 | | ThrowIfFrozen(); |
| | 10 | 138 | | field = value; |
| | 10 | 139 | | } |
| | 309 | 140 | | } = DefaultConstraintExceptionReasonCode; |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Gets or sets the reason message used when <see cref="TreatConstraintExceptionAsDenial" /> denies a constraint ex |
| | | 144 | | /// </summary> |
| | | 145 | | public string ConstraintExceptionReasonMessage |
| | | 146 | | { |
| | 302 | 147 | | get; |
| | | 148 | | set |
| | | 149 | | { |
| | 10 | 150 | | ThrowIfFrozen(); |
| | 10 | 151 | | field = value; |
| | 10 | 152 | | } |
| | 309 | 153 | | } = DefaultConstraintExceptionReasonMessage; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Gets or sets the machine-readable reason code used when <see cref="TreatThreatContributorExceptionAsDenial" /> d |
| | | 157 | | /// </summary> |
| | | 158 | | public string ThreatContributorExceptionReasonCode |
| | | 159 | | { |
| | 289 | 160 | | get; |
| | | 161 | | set |
| | | 162 | | { |
| | 2 | 163 | | ThrowIfFrozen(); |
| | 2 | 164 | | field = value; |
| | 2 | 165 | | } |
| | 309 | 166 | | } = DefaultThreatContributorExceptionReasonCode; |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Gets or sets the reason message used when <see cref="TreatThreatContributorExceptionAsDenial" /> denies a contri |
| | | 170 | | /// </summary> |
| | | 171 | | public string ThreatContributorExceptionReasonMessage |
| | | 172 | | { |
| | 287 | 173 | | get; |
| | | 174 | | set |
| | | 175 | | { |
| | 2 | 176 | | ThrowIfFrozen(); |
| | 2 | 177 | | field = value; |
| | 2 | 178 | | } |
| | 309 | 179 | | } = DefaultThreatContributorExceptionReasonMessage; |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Validates evaluator options and freezes the instance for evaluator use. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <remarks> |
| | | 185 | | /// The method is idempotent. After successful validation, attempts to change any option throw an |
| | | 186 | | /// <see cref="InvalidOperationException" /> so a constructed evaluator cannot observe configuration drift. |
| | | 187 | | /// </remarks> |
| | | 188 | | public void Validate() |
| | | 189 | | { |
| | 311 | 190 | | if (string.IsNullOrWhiteSpace(NoConstraintsReasonCode)) |
| | | 191 | | { |
| | 8 | 192 | | throw new InvalidOperationException($"{nameof(NoConstraintsReasonCode)} must not be empty."); |
| | | 193 | | } |
| | | 194 | | |
| | 303 | 195 | | if (string.IsNullOrWhiteSpace(NoConstraintsReasonMessage)) |
| | | 196 | | { |
| | 8 | 197 | | throw new InvalidOperationException($"{nameof(NoConstraintsReasonMessage)} must not be empty."); |
| | | 198 | | } |
| | | 199 | | |
| | 295 | 200 | | if (string.IsNullOrWhiteSpace(ConstraintExceptionReasonCode)) |
| | | 201 | | { |
| | 8 | 202 | | throw new InvalidOperationException($"{nameof(ConstraintExceptionReasonCode)} must not be empty."); |
| | | 203 | | } |
| | | 204 | | |
| | 287 | 205 | | if (string.IsNullOrWhiteSpace(ConstraintExceptionReasonMessage)) |
| | | 206 | | { |
| | 8 | 207 | | throw new InvalidOperationException($"{nameof(ConstraintExceptionReasonMessage)} must not be empty."); |
| | | 208 | | } |
| | | 209 | | |
| | 279 | 210 | | if (string.IsNullOrWhiteSpace(ThreatContributorExceptionReasonCode)) |
| | | 211 | | { |
| | 2 | 212 | | throw new InvalidOperationException($"{nameof(ThreatContributorExceptionReasonCode)} must not be empty."); |
| | | 213 | | } |
| | | 214 | | |
| | 277 | 215 | | if (string.IsNullOrWhiteSpace(ThreatContributorExceptionReasonMessage)) |
| | | 216 | | { |
| | 2 | 217 | | throw new InvalidOperationException($"{nameof(ThreatContributorExceptionReasonMessage)} must not be empty.") |
| | | 218 | | } |
| | | 219 | | |
| | 275 | 220 | | isFrozen = true; |
| | 275 | 221 | | } |
| | | 222 | | |
| | | 223 | | private void ThrowIfFrozen() |
| | | 224 | | { |
| | 252 | 225 | | if (isFrozen) |
| | | 226 | | { |
| | 2 | 227 | | throw new InvalidOperationException( |
| | 2 | 228 | | "Evaluator options cannot be changed after they have been validated for evaluator construction."); |
| | | 229 | | } |
| | 250 | 230 | | } |
| | | 231 | | } |