|
| 1 | +# ControlR Data Redaction Library |
| 2 | + |
| 3 | +A lightweight library for redacting sensitive data in logs and outputs using Microsoft's data compliance framework. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This library provides tools to automatically redact sensitive information during logging operations, helping you maintain security and comply with data protection regulations. It uses Microsoft's `Microsoft.Extensions.Compliance.Redaction` package to seamlessly integrate with standard .NET logging infrastructure. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +dotnet add package ControlR.Libraries.DataRedaction |
| 13 | +``` |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +### 1. Register the Redactor |
| 18 | + |
| 19 | +Add the redactor to your service collection: |
| 20 | + |
| 21 | +```csharp |
| 22 | +using ControlR.Libraries.DataRedaction; |
| 23 | + |
| 24 | +var builder = WebApplication.CreateBuilder(args); |
| 25 | + |
| 26 | +// Add redaction services |
| 27 | +builder.Services.AddStarRedactor(); |
| 28 | + |
| 29 | +var app = builder.Build(); |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Mark Sensitive Properties |
| 33 | + |
| 34 | +Use the `ProtectedDataClassification` attribute to mark properties that should be redacted: |
| 35 | + |
| 36 | +```csharp |
| 37 | +public class User |
| 38 | +{ |
| 39 | + public string Name { get; set; } |
| 40 | + |
| 41 | + [ProtectedDataClassification] |
| 42 | + public string Password { get; set; } |
| 43 | + |
| 44 | + [ProtectedDataClassification] |
| 45 | + public string ApiKey { get; set; } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Log Structured Data |
| 50 | + |
| 51 | +When you log objects with protected properties, they'll automatically be redacted: |
| 52 | + |
| 53 | +```csharp |
| 54 | +var user = new User |
| 55 | +{ |
| 56 | + Name = "John Doe", |
| 57 | + Password = "secret123", |
| 58 | + ApiKey = "sk_live_abc123" |
| 59 | +}; |
| 60 | + |
| 61 | +logger.LogInformation("User details: {@User}", user); |
| 62 | +// Output: User details: { Name: "John Doe", Password: "****", ApiKey: "****" } |
| 63 | +``` |
| 64 | + |
| 65 | +## Features |
| 66 | + |
| 67 | +- **Automatic Redaction**: Sensitive data is redacted automatically during logging |
| 68 | +- **Attribute-Based**: Simple `[ProtectedDataClassification]` attribute to mark sensitive properties |
| 69 | +- **Star Redactor**: Replaces sensitive values with `****` |
| 70 | +- **Standard Integration**: Works with Microsoft's logging and compliance infrastructure |
| 71 | +- **Zero Configuration**: Works out of the box with sensible defaults |
| 72 | + |
| 73 | +## API Reference |
| 74 | + |
| 75 | +### `AddStarRedactor()` |
| 76 | + |
| 77 | +Registers the `StarRedactor` and enables redaction in the logging pipeline. |
| 78 | + |
| 79 | +```csharp |
| 80 | +services.AddStarRedactor(); |
| 81 | +``` |
| 82 | + |
| 83 | +### `ProtectedDataClassificationAttribute` |
| 84 | + |
| 85 | +Marks a property as containing protected data that should be redacted. |
| 86 | + |
| 87 | +```csharp |
| 88 | +[ProtectedDataClassification] |
| 89 | +public string SensitiveData { get; set; } |
| 90 | +``` |
| 91 | + |
| 92 | +### `StarRedactor` |
| 93 | + |
| 94 | +The default redactor that replaces sensitive values with `****`. |
| 95 | + |
| 96 | +### `DefaultDataClassifications` |
| 97 | + |
| 98 | +Provides standard data classification types: |
| 99 | +- `Protected`: Data that should be redacted |
| 100 | +- `Public`: Data that can be logged without redaction |
| 101 | + |
| 102 | +## Advanced Usage |
| 103 | + |
| 104 | +### Custom Data Classifications |
| 105 | + |
| 106 | +You can use the built-in classifications for more granular control: |
| 107 | + |
| 108 | +```csharp |
| 109 | +using Microsoft.Extensions.Compliance.Classification; |
| 110 | + |
| 111 | +public class CustomModel |
| 112 | +{ |
| 113 | + [DataClassification(DefaultDataClassifications.Public)] |
| 114 | + public string PublicInfo { get; set; } |
| 115 | + |
| 116 | + [DataClassification(DefaultDataClassifications.Protected)] |
| 117 | + public string PrivateInfo { get; set; } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## Requirements |
| 122 | + |
| 123 | +- .NET 10.0 or later |
| 124 | +- Microsoft.Extensions.Compliance.Redaction |
| 125 | +- Microsoft.Extensions.Logging |
| 126 | + |
| 127 | +## Learn More |
| 128 | + |
| 129 | +For more information about data redaction in .NET, see the official documentation: |
| 130 | +- [Data Redaction in .NET](https://learn.microsoft.com/en-us/dotnet/core/extensions/data-redaction) |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +MIT |
0 commit comments