83 lines
5.6 KiB
C#
83 lines
5.6 KiB
C#
using System.Security.Authentication;
|
|
using Grpc.Core;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace GrpcDataProbe;
|
|
|
|
/// <summary>Fixed fixture stages; no caller-controlled text is emitted as a stage.</summary>
|
|
public enum ProbeFailurePhase
|
|
{
|
|
ValidateCommand, GetCapabilities, Metadata, Browse, Read, Write, Subscribe, NodeProbe,
|
|
WireSetup, WirePositiveCapabilities, WireProtocolMajor, WirePreviousKey, WireBadKey, WireWrongCa,
|
|
WireWrongSan, WireWrongListener, WireH2c, WireRetiredCa, WireOperation, Other
|
|
}
|
|
|
|
/// <summary>Fixed local rejection code and an optional gRPC status enum name.</summary>
|
|
public sealed record ProbeFailureFrame(string Code, string? RpcStatus = null);
|
|
|
|
/// <summary>Bounded nonsecret exception-chain evidence for fixture assertions.</summary>
|
|
public sealed record ProbeFailureEvidence(string Phase, string Category, bool Truncated, IReadOnlyList<ProbeFailureFrame> Chain);
|
|
|
|
/// <summary>Classifies only known local messages and typed transport failure categories.</summary>
|
|
public static class ProbeFailureClassifier
|
|
{
|
|
/// <summary>Recognizes only an explicit missing-Read denial from the exact pinned SOURCE primary.</summary>
|
|
public static bool IsPinnedReadPermissionDenied(DataTarget target, DataCapabilities? capabilities, ProbeFailureEvidence evidence) =>
|
|
target.Mode == DataTargetMode.Node && target.Consistency == DataConsistency.Primary
|
|
&& target.ExpectedClusterId == "SOURCE" && target.ExpectedNodeId is "source-1:4053" or "source-2:4053"
|
|
&& capabilities is { ProtocolMajor: 1, ProtocolMinor: >= 3, Readiness: DataReadiness.Ready,
|
|
ResponseServingIdentity: true, SubscriptionAgeReadiness: true, ClusterId: "SOURCE" }
|
|
&& capabilities.Serving.NodeId == target.ExpectedNodeId && capabilities.Serving.Role == "Primary"
|
|
&& !string.IsNullOrWhiteSpace(capabilities.Serving.Incarnation) && capabilities.Serving.CatalogEpoch > 0
|
|
&& capabilities.Serving.AuthorityEpoch > 0 && capabilities.Serving.ServingGeneration > 0
|
|
&& (capabilities.Operations & DataOperations.Read) == 0
|
|
&& (capabilities.Operations & ~(DataOperations.Browse | DataOperations.Metadata | DataOperations.Read | DataOperations.Write | DataOperations.Subscribe)) == 0
|
|
&& evidence is { Phase: "Read", Category: "RpcFailure", Truncated: false }
|
|
&& evidence.Chain.Count == 2
|
|
&& evidence.Chain[0] is { Code: "NoReadyEndpoint", RpcStatus: null }
|
|
&& evidence.Chain[1] is { Code: "RpcFailure", RpcStatus: "PermissionDenied" };
|
|
|
|
/// <summary>Recognizes a typed TLS authentication rejection at the retired-CA fixture phase.</summary>
|
|
public static bool IsRetiredCaTlsRejection(ProbeFailureEvidence evidence) =>
|
|
evidence is { Phase: "WireRetiredCa", Category: "TlsFailure", Truncated: false }
|
|
&& evidence.Chain.Count == 3
|
|
&& evidence.Chain[0] is { Code: "RpcFailure", RpcStatus: "Internal" or "Unavailable" }
|
|
&& evidence.Chain[1] is { Code: "TransportFailure", RpcStatus: null }
|
|
&& evidence.Chain[2] is { Code: "TlsFailure", RpcStatus: null };
|
|
|
|
/// <summary>Captures bounded fixed-code evidence without exception text, URIs or stacks.</summary>
|
|
public static ProbeFailureEvidence Capture(Exception error, ProbeFailurePhase phase)
|
|
{
|
|
var frames = new List<ProbeFailureFrame>(8);
|
|
Exception? current = error;
|
|
while (current is not null && frames.Count < 8)
|
|
{
|
|
frames.Add(current switch
|
|
{
|
|
InvalidOperationException { Message: "No ready authenticated data endpoint." } => new("NoReadyEndpoint"),
|
|
InvalidOperationException { Message: "Endpoint is not an authenticated ready target." } => new("TargetIneligible"),
|
|
InvalidOperationException { Message: "Data endpoint discovery is backing off." } => new("DiscoveryBackoff"),
|
|
InvalidOperationException { Message: "No ready subscription endpoint." } => new("NoReadySubscriptionEndpoint"),
|
|
InvalidOperationException { Message: "Subscriptions unavailable or peer stream capacity exhausted." } => new("SubscriptionUnavailable"),
|
|
InvalidOperationException { Message: "Unary capacity exhausted." } => new("UnaryCapacity"),
|
|
UnauthorizedAccessException { Message: "Connection credential or trust unavailable." } => new("SecurityMaterialUnavailable"),
|
|
TimeoutException { Message: "Original data request budget expired." } => new("RequestBudgetExpired"),
|
|
RpcException rpc => new("RpcFailure", Enum.IsDefined(rpc.StatusCode) ? rpc.StatusCode.ToString() : "Unknown"),
|
|
AuthenticationException => new("TlsFailure"),
|
|
HttpRequestException => new("TransportFailure"),
|
|
OperationCanceledException => new("Cancelled"),
|
|
_ => new("Other")
|
|
});
|
|
current = current.InnerException;
|
|
}
|
|
var truncated = current is not null;
|
|
var ineligible = !truncated && frames.Any(frame => frame.Code == "TargetIneligible")
|
|
&& frames.All(frame => frame.Code is "NoReadyEndpoint" or "TargetIneligible");
|
|
var category = ineligible ? "TargetIneligible" : frames.Any(frame => frame.Code == "SecurityMaterialUnavailable")
|
|
? "SecurityMaterialUnavailable" : frames.Any(frame => frame.Code == "TlsFailure") ? "TlsFailure"
|
|
: frames.Any(frame => frame.Code == "TransportFailure") ? "TransportFailure"
|
|
: frames.Any(frame => frame.Code == "RpcFailure") ? "RpcFailure" : "Other";
|
|
return new(Enum.IsDefined(phase) ? phase.ToString() : "Other", category, truncated, frames);
|
|
}
|
|
}
|