fix(client-dotnet): keepalive, error mapping, lifecycle, CLI batch/redaction (CDN-001..011, #257)
- keepalive: 30 s ping / 10 s timeout / Always, configurable via KeepAlivePingDelay/Timeout (CDN-004, D7) - GrpcStatusMapper shared by both transports: server Cancelled with a live token is no longer OperationCanceledException; NotFound/FailedPrecondition on session RPCs -> MxGatewaySessionException; MxGatewayException.GrpcStatusCode preserves the status (CDN-001, CDN-002) - Register/AddItem/AddItem2 raise MxGatewayMalformedReplyException on a payload-less reply (CDN-008) - MxGatewayEventStream.DisposeAsync waits for the in-flight read (CDN-003); session DisposeAsync is bounded to 5 s and swallows cancellation (CDN-005); owned handler + pinned CA disposed (CDN-006) - ResourceExhausted no longer retried (CDN-007) - CLI: batch reuses clients, stream/bench commands exempt from the 30 s timeout, OCE reported as an error with exit 1, --value redacted only for write-secured* and only as a whole token, bench backs off on failures (CDN-009, CDN-010, CDN-011)
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Builds the <see cref="SocketsHttpHandler"/> shared by <see cref="MxGatewayClient"/> and
|
||||
/// <see cref="GalaxyRepositoryClient"/>: connect timeout, HTTP/2 keepalive, and TLS pinning.
|
||||
/// </summary>
|
||||
internal static class GatewayHttpHandlerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates the handler for an owned channel.
|
||||
/// </summary>
|
||||
/// <param name="options">The client options supplying keepalive, connect timeout, and TLS settings.</param>
|
||||
/// <param name="trustedRoot">
|
||||
/// The pinned CA certificate captured by the validation callback, or <see langword="null"/> when none is pinned.
|
||||
/// The caller disposes it after the handler is disposed.
|
||||
/// </param>
|
||||
/// <returns>The configured handler.</returns>
|
||||
public static SocketsHttpHandler Create(MxGatewayClientOptions options, out X509Certificate2? trustedRoot)
|
||||
{
|
||||
trustedRoot = null;
|
||||
|
||||
// HTTP/2 keepalive (D7): a server-streaming call only reads, so on a half-open connection
|
||||
// nothing would ever fail the pending read. Pings are sent while no call is active too
|
||||
// (Always), because an idle event stream is exactly the case to detect.
|
||||
SocketsHttpHandler handler = new()
|
||||
{
|
||||
ConnectTimeout = options.ConnectTimeout,
|
||||
KeepAlivePingDelay = options.KeepAlivePingDelay,
|
||||
KeepAlivePingTimeout = options.KeepAlivePingTimeout,
|
||||
KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always,
|
||||
};
|
||||
|
||||
if (!options.UseTls)
|
||||
{
|
||||
return handler;
|
||||
}
|
||||
|
||||
handler.SslOptions = new SslClientAuthenticationOptions();
|
||||
if (!string.IsNullOrWhiteSpace(options.ServerNameOverride))
|
||||
{
|
||||
handler.SslOptions.TargetHost = options.ServerNameOverride;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.CaCertificatePath))
|
||||
{
|
||||
X509Certificate2 root = X509CertificateLoader.LoadCertificateFromFile(options.CaCertificatePath);
|
||||
trustedRoot = root;
|
||||
handler.SslOptions.RemoteCertificateValidationCallback = (_, certificate, chain, errors) =>
|
||||
{
|
||||
if ((errors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (certificate is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using X509Chain customChain = new();
|
||||
customChain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
|
||||
customChain.ChainPolicy.CustomTrustStore.Add(root);
|
||||
customChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
|
||||
customChain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
|
||||
X509Certificate2 certificateToValidate = certificate as X509Certificate2
|
||||
?? X509CertificateLoader.LoadCertificate(certificate.Export(X509ContentType.Cert));
|
||||
return customChain.Build(certificateToValidate);
|
||||
};
|
||||
}
|
||||
else if (!options.RequireCertificateValidation)
|
||||
{
|
||||
handler.SslOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true;
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user