using System; using System.Collections.Generic; namespace ZB.MOM.WW.MxGateway.Worker.Bootstrap; /// /// Redacts sensitive fields from worker log messages. /// public static class WorkerLogRedactor { /// /// Replacement text for redacted values. /// public const string RedactedValue = "[redacted]"; private static readonly string[] SensitiveFieldNameParts = [ "nonce", "secret", "password", "token", "credential", "apikey", "api_key", ]; /// /// Redacts sensitive field values from a log field dictionary. /// /// Dictionary of field names and values. /// A new dictionary with sensitive field values replaced by . public static Dictionary RedactFields(IReadOnlyDictionary fields) { Dictionary redactedFields = []; foreach (KeyValuePair field in fields) { redactedFields[field.Key] = RedactValue(field.Key, field.Value); } return redactedFields; } /// /// Redacts a single value if its field name contains sensitive keywords. /// /// Name of the field to check. /// Value to redact if sensitive. /// The redacted placeholder when looks sensitive; otherwise the original value. public static object? RedactValue(string fieldName, object? value) { if (value is null) { return null; } foreach (string sensitiveFieldNamePart in SensitiveFieldNameParts) { if (fieldName.IndexOf(sensitiveFieldNamePart, StringComparison.OrdinalIgnoreCase) >= 0) { return RedactedValue; } } return value; } }