| | | 1 | | using AsiBackbone.Core.Results; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Metadata; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the provider-neutral classification result for one governance metadata entry. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class GovernanceMetadataClassificationResult |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the default replacement value used by <see cref="Redact(string, string, string)" />. |
| | | 12 | | /// </summary> |
| | | 13 | | public const string DefaultRedactedValue = "[REDACTED]"; |
| | | 14 | | |
| | 24 | 15 | | private GovernanceMetadataClassificationResult( |
| | 24 | 16 | | GovernanceMetadataSanitizationAction action, |
| | 24 | 17 | | OperationReason? reason, |
| | 24 | 18 | | string? replacementValue) |
| | | 19 | | { |
| | 24 | 20 | | Action = action; |
| | 24 | 21 | | Reason = reason; |
| | 24 | 22 | | ReplacementValue = replacementValue; |
| | 24 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the action requested by the classifier. |
| | | 27 | | /// </summary> |
| | 34 | 28 | | public GovernanceMetadataSanitizationAction Action { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the stable machine-readable reason associated with a non-allow action. |
| | | 32 | | /// </summary> |
| | 48 | 33 | | public OperationReason? Reason { get; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the safe replacement value used when <see cref="Action" /> is <see cref="GovernanceMetadataSanitizationActi |
| | | 37 | | /// </summary> |
| | 16 | 38 | | public string? ReplacementValue { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Creates an allow result. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <returns>An allow classification result.</returns> |
| | | 44 | | public static GovernanceMetadataClassificationResult Allow() |
| | | 45 | | { |
| | 2 | 46 | | return new GovernanceMetadataClassificationResult( |
| | 2 | 47 | | GovernanceMetadataSanitizationAction.Allow, |
| | 2 | 48 | | reason: null, |
| | 2 | 49 | | replacementValue: null); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Creates a warning result that permits the original value to continue. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="reasonCode">The stable machine-readable warning code.</param> |
| | | 56 | | /// <param name="reasonMessage">The curated warning message.</param> |
| | | 57 | | /// <returns>A warning classification result.</returns> |
| | | 58 | | public static GovernanceMetadataClassificationResult Warn( |
| | | 59 | | string reasonCode, |
| | | 60 | | string reasonMessage) |
| | | 61 | | { |
| | 6 | 62 | | return CreateWithReason( |
| | 6 | 63 | | GovernanceMetadataSanitizationAction.Warn, |
| | 6 | 64 | | reasonCode, |
| | 6 | 65 | | reasonMessage, |
| | 6 | 66 | | replacementValue: null); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Creates a redaction result with a safe replacement value. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="reasonCode">The stable machine-readable redaction code.</param> |
| | | 73 | | /// <param name="reasonMessage">The curated redaction message.</param> |
| | | 74 | | /// <param name="replacementValue">The safe value that replaces the classified value.</param> |
| | | 75 | | /// <returns>A redaction classification result.</returns> |
| | | 76 | | public static GovernanceMetadataClassificationResult Redact( |
| | | 77 | | string reasonCode, |
| | | 78 | | string reasonMessage, |
| | | 79 | | string replacementValue = DefaultRedactedValue) |
| | | 80 | | { |
| | 12 | 81 | | ArgumentNullException.ThrowIfNull(replacementValue); |
| | | 82 | | |
| | 10 | 83 | | return CreateWithReason( |
| | 10 | 84 | | GovernanceMetadataSanitizationAction.Redact, |
| | 10 | 85 | | reasonCode, |
| | 10 | 86 | | reasonMessage, |
| | 10 | 87 | | replacementValue); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Creates a drop result that removes the metadata entry. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="reasonCode">The stable machine-readable drop code.</param> |
| | | 94 | | /// <param name="reasonMessage">The curated drop message.</param> |
| | | 95 | | /// <returns>A drop classification result.</returns> |
| | | 96 | | public static GovernanceMetadataClassificationResult Drop( |
| | | 97 | | string reasonCode, |
| | | 98 | | string reasonMessage) |
| | | 99 | | { |
| | 6 | 100 | | return CreateWithReason( |
| | 6 | 101 | | GovernanceMetadataSanitizationAction.Drop, |
| | 6 | 102 | | reasonCode, |
| | 6 | 103 | | reasonMessage, |
| | 6 | 104 | | replacementValue: null); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Creates a deny result that prevents the metadata collection from continuing. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="reasonCode">The stable machine-readable denial code.</param> |
| | | 111 | | /// <param name="reasonMessage">The curated denial message.</param> |
| | | 112 | | /// <returns>A denial classification result.</returns> |
| | | 113 | | public static GovernanceMetadataClassificationResult Deny( |
| | | 114 | | string reasonCode, |
| | | 115 | | string reasonMessage) |
| | | 116 | | { |
| | 10 | 117 | | return CreateWithReason( |
| | 10 | 118 | | GovernanceMetadataSanitizationAction.Deny, |
| | 10 | 119 | | reasonCode, |
| | 10 | 120 | | reasonMessage, |
| | 10 | 121 | | replacementValue: null); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private static GovernanceMetadataClassificationResult CreateWithReason( |
| | | 125 | | GovernanceMetadataSanitizationAction action, |
| | | 126 | | string reasonCode, |
| | | 127 | | string reasonMessage, |
| | | 128 | | string? replacementValue) |
| | | 129 | | { |
| | 32 | 130 | | return new GovernanceMetadataClassificationResult( |
| | 32 | 131 | | action, |
| | 32 | 132 | | OperationReason.Create(reasonCode, reasonMessage), |
| | 32 | 133 | | replacementValue); |
| | | 134 | | } |
| | | 135 | | } |