| | | 1 | | using AsiBackbone.Core.Decisions; |
| | | 2 | | using Microsoft.AspNetCore.Hosting; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using Microsoft.Extensions.Hosting; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 8 | | |
| | | 9 | | internal static class AsiBackboneEndpointGovernanceDevelopmentDiagnostics |
| | | 10 | | { |
| | | 11 | | private const string RedactedValue = "[redacted]"; |
| | | 12 | | private const string DocumentationArticleName = "endpoint-governance-development-diagnostics.html"; |
| | | 13 | | |
| | | 14 | | public static bool IsEnabled( |
| | | 15 | | HttpContext httpContext, |
| | | 16 | | AsiBackboneEndpointGovernanceOptions options) |
| | | 17 | | { |
| | 12 | 18 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 12 | 19 | | ArgumentNullException.ThrowIfNull(options); |
| | | 20 | | |
| | 12 | 21 | | if (!options.EnableDevelopmentDiagnostics) |
| | | 22 | | { |
| | 8 | 23 | | return false; |
| | | 24 | | } |
| | | 25 | | |
| | 4 | 26 | | IWebHostEnvironment? environment = httpContext.RequestServices.GetService<IWebHostEnvironment>(); |
| | | 27 | | |
| | 4 | 28 | | return environment?.IsDevelopment() == true; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | public static IResult CreateProblem( |
| | | 32 | | HttpContext httpContext, |
| | | 33 | | AsiBackboneEndpointGovernanceOptions options, |
| | | 34 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 35 | | GovernanceDecision? decision, |
| | | 36 | | string decisionStage, |
| | | 37 | | string title, |
| | | 38 | | string detail, |
| | | 39 | | int statusCode, |
| | | 40 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 41 | | { |
| | 3 | 42 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 3 | 43 | | ArgumentNullException.ThrowIfNull(options); |
| | 3 | 44 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | 3 | 45 | | ArgumentException.ThrowIfNullOrWhiteSpace(decisionStage); |
| | 3 | 46 | | ArgumentException.ThrowIfNullOrWhiteSpace(title); |
| | 3 | 47 | | ArgumentException.ThrowIfNullOrWhiteSpace(detail); |
| | | 48 | | |
| | 3 | 49 | | Dictionary<string, string> diagnosticMetadata = metadata is null |
| | 3 | 50 | | ? new Dictionary<string, string>(descriptor.ToMetadata(options.MetadataMode), StringComparer.Ordinal) |
| | 3 | 51 | | : new Dictionary<string, string>(metadata, StringComparer.Ordinal); |
| | 3 | 52 | | Dictionary<string, object?> extensions = CreateExtensions( |
| | 3 | 53 | | options, |
| | 3 | 54 | | descriptor, |
| | 3 | 55 | | decision, |
| | 3 | 56 | | decisionStage, |
| | 3 | 57 | | diagnosticMetadata); |
| | | 58 | | |
| | 3 | 59 | | return Microsoft.AspNetCore.Http.Results.Problem( |
| | 3 | 60 | | title: title, |
| | 3 | 61 | | detail: detail, |
| | 3 | 62 | | statusCode: statusCode, |
| | 3 | 63 | | extensions: extensions); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | private static Dictionary<string, object?> CreateExtensions( |
| | | 67 | | AsiBackboneEndpointGovernanceOptions options, |
| | | 68 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 69 | | GovernanceDecision? decision, |
| | | 70 | | string decisionStage, |
| | | 71 | | IReadOnlyDictionary<string, string> metadata) |
| | | 72 | | { |
| | 3 | 73 | | var extensions = new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 3 | 74 | | { |
| | 3 | 75 | | ["decisionStage"] = decisionStage, |
| | 3 | 76 | | ["endpointOperationName"] = descriptor.OperationName, |
| | 3 | 77 | | ["endpointPolicyTypes"] = descriptor.PolicyTypes |
| | 1 | 78 | | .Select(static policyType => policyType.FullName ?? policyType.Name) |
| | 0 | 79 | | .OrderBy(static policyType => policyType, StringComparer.Ordinal) |
| | 3 | 80 | | .ToArray(), |
| | 3 | 81 | | ["capabilityScopes"] = descriptor.CapabilityScopes |
| | 0 | 82 | | .OrderBy(static scope => scope, StringComparer.Ordinal) |
| | 3 | 83 | | .ToArray(), |
| | 3 | 84 | | ["metadataMode"] = options.MetadataMode.ToString(), |
| | 3 | 85 | | ["metadataKeys"] = metadata.Keys |
| | 9 | 86 | | .OrderBy(static key => key, StringComparer.Ordinal) |
| | 3 | 87 | | .ToArray(), |
| | 3 | 88 | | ["metadata"] = RedactMetadata(options, metadata) |
| | 3 | 89 | | }; |
| | | 90 | | |
| | 3 | 91 | | if (decision is not null) |
| | | 92 | | { |
| | 3 | 93 | | extensions["outcome"] = decision.Outcome.ToString(); |
| | 3 | 94 | | extensions["reasonCodes"] = decision.ReasonCodes.ToArray(); |
| | 3 | 95 | | extensions["reasonMessages"] = decision.Reasons |
| | 3 | 96 | | .Select(static reason => reason.Message) |
| | 3 | 97 | | .ToArray(); |
| | | 98 | | |
| | 3 | 99 | | AddIfPresent(extensions, "correlationId", decision.CorrelationId); |
| | 3 | 100 | | AddIfPresent(extensions, "traceId", decision.TraceId); |
| | 3 | 101 | | AddIfPresent(extensions, "policyVersion", decision.PolicyVersion); |
| | 3 | 102 | | AddIfPresent(extensions, "policyHash", decision.PolicyHash); |
| | | 103 | | } |
| | | 104 | | |
| | 3 | 105 | | string? documentationUrl = CreateDocumentationUrl(options.DevelopmentDiagnosticsDocumentationBaseUrl); |
| | 3 | 106 | | AddIfPresent(extensions, "documentationUrl", documentationUrl); |
| | | 107 | | |
| | 3 | 108 | | return extensions; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | private static Dictionary<string, string> RedactMetadata( |
| | | 112 | | AsiBackboneEndpointGovernanceOptions options, |
| | | 113 | | IReadOnlyDictionary<string, string> metadata) |
| | | 114 | | { |
| | 3 | 115 | | Dictionary<string, string> redacted = new(StringComparer.Ordinal); |
| | | 116 | | |
| | 36 | 117 | | foreach (KeyValuePair<string, string> item in metadata.OrderBy(static pair => pair.Key, StringComparer.Ordinal)) |
| | | 118 | | { |
| | 10 | 119 | | redacted[item.Key] = ShouldRedactMetadataValue(options, item.Key) |
| | 10 | 120 | | ? RedactedValue |
| | 10 | 121 | | : item.Value; |
| | | 122 | | } |
| | | 123 | | |
| | 3 | 124 | | return redacted; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static bool ShouldRedactMetadataValue( |
| | | 128 | | AsiBackboneEndpointGovernanceOptions options, |
| | | 129 | | string key) |
| | | 130 | | { |
| | 18 | 131 | | if (!options.IncludeDevelopmentDiagnosticsMetadataValues) |
| | | 132 | | { |
| | 0 | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | 37 | 136 | | foreach (string sensitiveKey in options.DevelopmentDiagnosticsRedactedMetadataKeys) |
| | | 137 | | { |
| | 1 | 138 | | if (string.Equals(key, sensitiveKey, StringComparison.OrdinalIgnoreCase)) |
| | | 139 | | { |
| | 1 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | 17 | 144 | | return key.Contains("secret", StringComparison.OrdinalIgnoreCase) |
| | 17 | 145 | | || key.Contains("token", StringComparison.OrdinalIgnoreCase) |
| | 17 | 146 | | || key.Contains("password", StringComparison.OrdinalIgnoreCase) |
| | 17 | 147 | | || key.Contains("credential", StringComparison.OrdinalIgnoreCase) |
| | 17 | 148 | | || key.Contains("cookie", StringComparison.OrdinalIgnoreCase) |
| | 17 | 149 | | || key.Contains("authorization", StringComparison.OrdinalIgnoreCase) |
| | 17 | 150 | | || key.Contains("key", StringComparison.OrdinalIgnoreCase); |
| | 1 | 151 | | } |
| | | 152 | | |
| | | 153 | | private static string? CreateDocumentationUrl(string? baseUrl) |
| | | 154 | | { |
| | 3 | 155 | | if (string.IsNullOrWhiteSpace(baseUrl)) |
| | | 156 | | { |
| | 1 | 157 | | return null; |
| | | 158 | | } |
| | | 159 | | |
| | 2 | 160 | | string trimmedBaseUrl = baseUrl.Trim(); |
| | | 161 | | |
| | 2 | 162 | | return trimmedBaseUrl.EndsWith('/') |
| | 2 | 163 | | ? trimmedBaseUrl + DocumentationArticleName |
| | 2 | 164 | | : trimmedBaseUrl + "/" + DocumentationArticleName; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private static void AddIfPresent( |
| | | 168 | | Dictionary<string, object?> extensions, |
| | | 169 | | string key, |
| | | 170 | | string? value) |
| | | 171 | | { |
| | 15 | 172 | | if (!string.IsNullOrWhiteSpace(value)) |
| | | 173 | | { |
| | 10 | 174 | | extensions[key] = value; |
| | | 175 | | } |
| | 15 | 176 | | } |
| | | 177 | | } |