namespace ZB.MOM.WW.OtOpcUa.Driver.MTConnect;
///
/// Review 2026-10-21 R06:T-8 — the display form of an Agent URI for every log line, exception message,
/// DriverHealth.LastError and HostName this driver produces. An AgentUri may carry
/// user:pass@; the Test-Connect probe already redacted it (MTConnectDriverProbe.RedactedDisplay)
/// while the driver itself logged and surfaced the raw string — and every request URI built from it.
///
///
/// A URI with no user-info (and, for the configured string, no query or fragment) is returned unchanged,
/// so the common case — and every host key derived from it — is byte-identical to what it was. One that does
/// carry user-info loses it (and, for the configured string, its query / fragment, which a reverse proxy may use
/// for a token). A configured string that is not an absolute URI but contains an @ is replaced wholesale:
/// there is no reliable way to find the credential in it.
///
internal static class MTConnectUriRedaction
{
/// Placeholder for a configured value that cannot be parsed and might carry a credential.
internal const string Unparseable = "(agentUri withheld: not an absolute URI)";
/// The display form of a configured AgentUri.
/// The configured value.
/// The value with any user-info (and query / fragment) removed.
public static string Redact(string? agentUri)
{
if (string.IsNullOrWhiteSpace(agentUri))
{
return agentUri ?? string.Empty;
}
if (!Uri.TryCreate(agentUri.Trim(), UriKind.Absolute, out var uri))
{
return agentUri.Contains('@', StringComparison.Ordinal) ? Unparseable : agentUri;
}
if (uri.UserInfo.Length == 0 && uri.Query.Length == 0 && uri.Fragment.Length == 0)
{
return agentUri;
}
var port = uri.IsDefaultPort ? string.Empty : $":{uri.Port}";
var path = uri.AbsolutePath == "/" ? string.Empty : uri.AbsolutePath;
return $"{uri.Scheme}://{uri.Host}{port}{path}";
}
/// The display form of a request URI built from the AgentUri (its query is the request's own).
/// The request URI.
/// The URI as text, without user-info.
public static string Redact(Uri uri)
{
ArgumentNullException.ThrowIfNull(uri);
if (!uri.IsAbsoluteUri || uri.UserInfo.Length == 0)
{
return uri.ToString();
}
return new UriBuilder(uri) { UserName = string.Empty, Password = string.Empty }.Uri.ToString();
}
}