51 lines
1.2 KiB
C#
51 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace MxGateway.Worker.Bootstrap;
|
|
|
|
public static class WorkerLogRedactor
|
|
{
|
|
public const string RedactedValue = "[redacted]";
|
|
|
|
private static readonly string[] SensitiveFieldNameParts =
|
|
[
|
|
"nonce",
|
|
"secret",
|
|
"password",
|
|
"token",
|
|
"credential",
|
|
"apikey",
|
|
"api_key",
|
|
];
|
|
|
|
public static Dictionary<string, object?> RedactFields(IReadOnlyDictionary<string, object?> fields)
|
|
{
|
|
Dictionary<string, object?> redactedFields = [];
|
|
|
|
foreach (KeyValuePair<string, object?> field in fields)
|
|
{
|
|
redactedFields[field.Key] = RedactValue(field.Key, field.Value);
|
|
}
|
|
|
|
return redactedFields;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|