次の方法で共有


.NET でのデータの編集

Redaction は、ログ、エラー メッセージ、またはその他の出力の機密情報をサニタイズまたはマスクするのに役立ちます。 これにより、プライバシー規則に準拠し、機密データが保護されます。 これは、個人データ、財務情報、またはその他の機密データ ポイントを処理するアプリで役立ちます。

redaction パッケージをインストールする

まず、📦 Microsoft.Extensions.Compliance.Redaction NuGet パッケージをインストールします。

dotnet add package Microsoft.Extensions.Compliance.Redaction

または、.NET 10+ SDK を使用している場合:

dotnet package add Microsoft.Extensions.Compliance.Redaction

利用可能な編集者

編集者は、機密データを編集する行為を担当します。 機密情報の編集、置換、またはマスクを行います。 ライブラリによって提供される次の使用可能な編集機能について考えてみましょう。

  • ErasingRedactor は、入力を空の文字列に置き換えます。
  • HmacRedactor では、HMACSHA256 を使用して、編集中のデータをエンコードします。

使用例

組み込みの編集機能を使用するには、必要なサービスを登録する必要があります。 次の一覧で説明するように、使用可能な AddRedaction メソッドのいずれかを使用してサービスを登録します。

リダクターを設定する

IRedactorProviderを使用して実行時に編集者を取得します。 独自のプロバイダーを実装し、AddRedaction 呼び出し内に登録することも、既定のプロバイダーを使用することもできます。 次の IRedactionBuilder メソッドを使用して、編集プログラムを構成します。

// This will use the default redactor, which is the ErasingRedactor
var serviceCollection = new ServiceCollection();
serviceCollection.AddRedaction();

// Using the default redactor provider:
serviceCollection.AddRedaction(redactionBuilder =>
{
    // Assign a redactor to use for a set of data classifications.
    redactionBuilder.SetRedactor<StarRedactor>(
        MyTaxonomyClassifications.Private,
        MyTaxonomyClassifications.Personal);
    // Assign a fallback redactor to use when processing classified data for which no specific redactor has been registered.
    // The `ErasingRedactor` is the default fallback redactor. If no redactor is configured for a data classification then the data will be erased.
    redactionBuilder.SetFallbackRedactor<MyFallbackRedactor>();
});

// Using a custom redactor provider:
builder.Services.AddSingleton<IRedactorProvider, StarRedactorProvider>();

コードで次のデータ分類を行います。

public static class MyTaxonomyClassifications
{
    public static string Name => "MyTaxonomy";

    public static DataClassification Private => new(Name, nameof(Private));
    public static DataClassification Public => new(Name, nameof(Public));
    public static DataClassification Personal => new(Name, nameof(Personal));
}

HMAC リダクターを構成する

次の IRedactionBuilder メソッドを使用して HMAC リダクターを構成します。

var serviceCollection = new ServiceCollection();
serviceCollection.AddRedaction(builder =>
{
    builder.SetHmacRedactor(
        options =>
        {
            options.KeyId = 1234567890;
            options.Key = Convert.ToBase64String("1234567890abcdefghijklmnopqrstuvwxyz");
        },

        // Any data tagged with Personal or Private attributes will be redacted by the Hmac redactor.
        MyTaxonomyClassifications.Personal, MyTaxonomyClassifications.Private,

        // "DataClassificationSet" lets you compose multiple data classifications:
        // For example, here the Hmac redactor will be used for data tagged
        // with BOTH Personal and Private (but not one without the other).
        new DataClassificationSet(MyTaxonomyClassifications.Personal,
                                  MyTaxonomyClassifications.Private));
});

または、次のように構成します。

var serviceCollection = new ServiceCollection();
serviceCollection.AddRedaction(builder =>
{
    builder.SetHmacRedactor(
        Configuration.GetSection("HmacRedactorOptions"), MyTaxonomyClassifications.Personal);
});

JSON 構成ファイルに次のセクションを含めます。

{
    "HmacRedactorOptions": {
        "KeyId": 1234567890,
        "Key": "1234567890abcdefghijklmnopqrstuvwxyz"
    }
}
  • HmacRedactorOptions では、HmacRedactorOptions.Key プロパティと HmacRedactorOptions.KeyId プロパティを設定する必要があります。
  • Key は、base 64 形式で、44 文字以上にする必要があります。 サービスの主要なデプロイごとに個別のキーを使用します。 重要な素材を秘密にし、定期的にローテーションします。
  • KeyId は、データのハッシュに使用されるキーを識別するために、編集された各値に追加されます。
  • キー ID が異なると、値は無関係であり、関連付けに使用できないことを意味します。

HmacRedactor はまだ試験段階であるため、上記のメソッドでは、まだ安定していないことを示す EXTEXP0002 警告が発生します。 これを使用するには、プロジェクト ファイルに <NoWarn>$(NoWarn);EXTEXP0002</NoWarn> を追加するか、#pragma warning disable EXTEXP0002の呼び出しに関する SetHmacRedactor を追加します。

カスタム リダクターを構成する

カスタムリダクターを作成するには、Redactorから継承するサブクラスを定義します。

public sealed class StarRedactor : Redactor

public class StarRedactor : 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;
    }
}

カスタム編集者プロバイダーを作成する

IRedactorProvider インターフェイスは、データ分類に基づく編集機能のインスタンスを提供します。 カスタム リダクター プロバイダーを作成するには、次の例に示すように IRedactorProvider から継承します。

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

public sealed class StarRedactorProvider : IRedactorProvider
{
    private static readonly StarRedactor _starRedactor = new();

    public static StarRedactorProvider Instance { get; } = new();

    public Redactor GetRedactor(DataClassificationSet classifications) => _starRedactor;
}

機密情報のログ記録

ログ記録は、偶発的なデータ漏えいの一般的な原因です。 個人データ、資格情報、財務情報などの機密情報は、プレーンテキストでログに書き込むべきではありません。 これを防ぐには、機密データのログ記録時に常に編集を使用してください。

機密データをログに記録する手順

  1. テレメトリ拡張機能パッケージをインストールする: Microsoft.Extensions.Telemetry をインストールして、拡張ロガーを使用して編集機能を有効にすることができます。
  2. 編集の設定: AddRedaction(IServiceCollection) メソッドを呼び出して、ログ パイプラインと編集機能を統合し、機密フィールドをログに書き込む前に自動的にサニタイズまたはマスクします。
  3. 機密フィールドを識別する: アプリケーションのどのデータが機密性が高く、保護が必要かを把握し、適切なデータ分類でマークします。
  4. ログ出力を確認する: 機密データが公開されないように、ログを定期的に監査します。

例: ログ内のデータの編集

Microsoft.Extensions.Logging を使用する場合、次のように、編集とログ記録を組み合わせることができます。

using Microsoft.Extensions.Telemetry;
using Microsoft.Extensions.Compliance.Redaction;

var services = new ServiceCollection();
services.AddLogging(builder =>
{
    // Enable redaction.
    builder.EnableRedaction();
});

services.AddRedaction(builder =>
{
    // configure redactors for your data classifications
    builder.SetRedactor<StarRedactor>(MyTaxonomyClassifications.Private);
});
// Use annotations to mark sensitive data.
// For example, apply the Private classification to SSN data.
[LoggerMessage(0, LogLevel.Information, "User SSN: {SSN}")]
public static partial void LogPrivateInformation(
    this ILogger logger,
    [MyTaxonomyClassifications.Private] string SSN);

public void TestLogging()
{
    LogPrivateInformation("MySSN");
}

出力は次のようになります。

User SSN: *****

これにより、ログに記録される前に機密データが編集され、データ リークのリスクが軽減されます。