在云原生应用程序中编修敏感数据

已完成

应用程序中的修订通常在日志消息和遥测上完成。 它还可用于其他方案,例如在指标中编辑维度,或在中间件中对标头数据进行编辑。

.NET 日志记录框架提供了在日志消息中对数据进行修订的简单方法。 该 Microsoft.Extensions.Compliance.Abstractions 包增强了日志记录功能,其中包含一个 Redactor 类用于对数据进行屏蔽。

什么是编修?

修订是从消息中删除敏感信息的过程。 例如,你可能希望从日志消息中编辑用户的名称。 或者,你可能想要从遥测事件中删除用户的 IP 地址。

最简单的修订是擦除值,并返回变量的空字符串。 此行为默认发生,因为ErasingRedactor是默认的回退编辑器。 Microsoft包括一个 HMACSHA256Redactor 类,可用于使用哈希函数对数据进行修订。 如果需要编辑数据,HMAC 编辑会非常有用,同时仍能够跨多个日志条目关联日志信息。 最后一个选项是提供自己的修订函数,如果想要使用自定义算法对数据进行编辑,这非常有用。

例如,你想要在日志中更清楚地表示一个值已被编辑,通过将其替换为*****

如何在云原生应用程序中编辑数据

组织云原生应用可以编写日志并在多个项目中创建遥测数据。 例如,它可以从数据库服务、Web 应用或任何其他 API 编写日志。 根据日志记录的类型,需要将编修服务添加到每种类型。

在应用中启用修订需要执行四个步骤:

  1. Microsoft.Extensions.Compliance.Redaction NuGet 包添加到每个项目。
  2. 将编辑服务添加到依赖注入容器中。
  3. 选择要用于每种类型的分类数据的修订实现。
  4. 在记录框架中启用编修。

将编修服务添加到依赖项注入容器

以下示例适用于 Blazor WebAssembly 应用。 此过程与其他类型的应用类似,但代码略有不同,具体取决于依赖项注入容器的配置方式。

program.cs 文件中,添加以下依赖项:

using Microsoft.Extensions.Compliance.Classification;
using Microsoft.Extensions.Compliance.Redaction;

通过上述包,可以使用以下代码将修订服务添加到依赖项注入容器:

builder.Services.AddRedaction();

选择用于每种类型的分类数据的编辑策略

该方法 AddRedactor 可以包含参数 RedactorOptions 。 通过此参数,可以指定要用于每个数据分类的修订实现。

例如,以下代码指定 HMACSHA256Redactor 应用于 EUII 数据。

builder.Services.AddRedaction(configure =>
{
    // Configure to use the HMAC redactor
    configure.SetHmacRedactor(configureHmac =>
    {
        // This key should be fetched from keyvault or some other secure store.
        configureHmac.Key = "thisisadummykeythatshouldbereplacedwithakeyfromakeystore";
        // Some discriminator to differentiate between different deployments of a service.
        configureHmac.KeyId = 1;

    }, new DataClassificationSet(DataClassifications.EUIIDataClassification));
});

注释

HMAC 消隐算法是实验性的,所以如果使用它,需要禁用编译器警告。 将上述代码用 #pragma warning disable EXTEXP0002#pragma warning restore EXTEXP0002 包围,便能使你编译项目。

可以将多个修订实现添加到 RedactorOptions 参数。 例如,以下代码为 EUPI 数据添加自定义编辑器。

builder.Services.AddRedaction(configure =>
{
    // Configure to use the HMAC redactor for EUII data
    configure.SetHmacRedactor(configureHmac =>
    {
        // This key should be fetched from keyvault or some other secure store.
        configureHmac.Key = "thisisadummykeythatshouldbereplacedwithakeyfromakeystore";
        // Some discriminator to differentiate between different deployments of a service.
        configureHmac.KeyId = 1;

    }, new DataClassificationSet(DataClassifications.EUIIDataClassification));

    // Configure a custom redactor for EUPI data
    configure.SetRedactor<EShopCustomRedactor>(new DataClassificationSet(DataClassifications.EUPIDataClassification));
});

在日志记录框架中启用删减

下一步是在记录框架中启用编修。 为此,请将 .EnableRedaction 属性设置为应用程序日志记录生成器。 对于示例应用,代码为:

builder.Services.AddLogging(logging => 
{
    logging.EnableRedaction();
    logging.AddJsonConsole(); //Enable structure logs on the console to view the redacted data.
});

使用上述代码,可以创建使用编修服务的新记录器。 在要将订单信息写入日志的位置,实现新的 LogOrders 记录器。

public static partial class Log
{
    [LoggerMessage(1, LogLevel.Information, "Write the Order data formatted as JSON: {order}")]
    public static partial void LogOrders(this ILogger logger, [LogProperties] Order order);
}

创建自定义编辑处理实现方案

Microsoft允许你创建自定义修订实现。 如果要使用自己的算法对数据进行编辑,你将使用自定义编辑。 让我们实现一个自定义编辑器,将敏感数据替换为*****

自定义编辑器需要实现Redactor类。 类需要实现两种方法:

public class EShopCustomRedactor : Redactor
{
    private const string Stars = "*****";

    public override int GetRedactedLength(ReadOnlySpan<char> input) => Stars.Length;

    public override int Redact(ReadOnlySpan<char> source, Span<char> destination)
    {
        Stars.CopyTo(destination);
        return Stars.Length;
    }
}

在我们的示例 eShopLite 体系结构中,可以将此类添加到数据分类代码下方 Compliance.cs 中的 DataEntities 项目中