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