練習 - 在雲端原生應用程式中修訂機密資料
您需要在訂單流程中增加一些紀錄。 您將使用 .NET 的修訂功能,以確保敏感數據不會外泄至記錄中。
在本練習中,您將會:
- 將
Microsoft.Extensions.Compliance.RedactionNuGet 套件新增至每個專案。 - 將編輯服務新增至依賴注入容器。
- 在紀錄架構中啟用修訂。
- 在訂單流程中呼叫紀錄架構。
- 新增 EUII 資料的自訂修訂實作。
- 選擇要針對每種分類數據類型使用的修訂實作。
新增修訂服務
您仍然應該開啟 Codespace 或 Visual Studio Code 視窗。 如果沒有的話,請立即開啟。
在 [終端] 視窗中輸入此命令:
cd /workspaces/mslearn-dotnet-cloudnative/dotnet-compliance/eShopLite/Store/將
Microsoft.Extensions.Compliance.RedactionNuGet 套件新增至專案:dotnet add package Microsoft.Extensions.Compliance.Redaction在 [檔案總管] 視窗中,展開 dotnet-compliance/eShopLite/Store 資料夾,然後選取 Program.cs 檔案。
在編輯器中,新增下列相依性:
using Microsoft.Extensions.Compliance.Classification; using Microsoft.Extensions.Compliance.Redaction;向下捲動至第 19 行,在
Add redaction註解下,將修訂服務新增至相依性插入容器:builder.Services.AddRedaction();
在紀錄架構中啟用修訂
在編輯器中,在
AddRedaction()行下方新增此程式碼:builder.Services.AddLogging(logging => { logging.EnableRedaction(); logging.AddJsonConsole(); //Enable structure logs on the console to view the redacted data. });上述程式碼會在紀錄架構中啟用修訂。
在訂單流程中呼叫紀錄架構
在 瀏覽器 視窗中,展開 dotnet-compliance/eShopLite/Store/Services 資料夾,然後選取 ProductService.cs 檔案。
在編輯器中,於檔案底部新增下列程序代碼:
public static partial class Log { [LoggerMessage(1, LogLevel.Information, "Placed Order: {order}")] public static partial void LogOrders(this ILogger logger, [LogProperties] Order order); }在編輯器中,於
CreateOrder工作裡呼叫LogOrders方法:public async Task<bool> CreateOrder(Order order) { try { _logger.LogOrders(order);上述程式代碼會呼叫
LogOrders方法,並將目前的順序資訊傳遞給它。
測試新的修訂記錄
使用上述所有程式代碼,應用程式可以使用預設的修訂實作來修訂 Order 資訊。 您現在將測試此作業。
在底部的 [終端機 ] 窗格中,移至 dotnet-compliance/eShopLite 資料夾。
cd ..更新應用程式容器。
dotnet publish /p:PublishProfile=DefaultContainer移至 dotnet-compliance 資料夾,並使用 Docker 啟動應用程式:
cd .. docker compose up選取 [埠] 索引標籤,然後選取 [在瀏覽器中開啟] 地球圖示以開啟 前端 (32000) 埠。
選取 [產品] 連結。 將一些產品新增至購物籃。
選取 [ 購買購物籃] 按鈕。
在 [終端機 ] 視窗中,按 Ctrl+F,在搜尋欄位中輸入 「EventId」:1,。
frontend-1 | {"EventId":1,"LogLevel":"Information","Category":"Store.Services.ProductService","Message":"Placed Order: DataEntities.Order","State":{"Message":"Microsoft.Extensions.Logging.ExtendedLogger\u002BModernTagJoiner","{OriginalFormat}":"Placed Order: {order}","order.Total":209.94,"order.Products":"[\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022]","order":"DataEntities.Order","order.CustomerAddress":"","order.CustomerName":"","order.Id":""}}您應該會看到這個 JSON 格式的日誌條目。 請注意, 順序。總 值位於記錄中,但 CustomerName 和 CustomerAddress 值是空字串。
根據預設,如果您未指定修訂實作,修訂引擎會使用 實
ErasingRedactor作來確保不會將敏感數據洩漏到記錄中。在 [終端機 ] 視窗中,按 Ctrl+C 以停止應用程式。
新增自訂編輯功能
您現在會增強修訂實作,以針對不同類型的數據使用不同的修訂演算法。 首先,您將新增自訂修訂實作,將值替換為 *****。
在 EXPLORER 窗格中,展開 dotnet-compliance/eShopLite/DataEntities 資料夾,然後選取 Compliance.cs 檔案。
在編輯器中,於檔案底部新增下列程序代碼:
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; } }上述程式代碼使
EShopCustomRedactor修訂方法可供修訂引擎使用。
選擇要使用的編輯實作
在 [檔案總管] 視窗中,展開 dotnet-compliance/eShopLite/Store 資料夾,然後選取 Program.cs 檔案。
替換
builder.Services.AddRedaction();程式碼,以提供修訂引擎的設定:builder.Services.AddRedaction(configure => { configure.SetRedactor<ErasingRedactor>(new DataClassificationSet(DataClassifications.EUPDataClassification)); configure.SetRedactor<EShopCustomRedactor>(new DataClassificationSet(DataClassifications.EUIIDataClassification)); });上述程式碼將編輯引擎設定為專門使用
ErasingRedactor實作來處理 EUP 數據,以及使用新的自定義EShopCustomRedactor實作來處理 EUII 數據。
測試新修訂實作
在 [終端機] 視窗中,建置並執行應用程式:
docker-compose up --build選取 [埠] 索引標籤,然後選取 [在瀏覽器中開啟] 地球圖示以開啟 前端 (32000) 埠。
選取 [產品] 連結。 將一些產品新增至購物籃。
選取 [ 購買購物籃] 按鈕。
在 [終端機 ] 視窗中,按 Ctrl+F,在搜尋欄位中輸入 「EventId」:1,。
frontend-1 | {"EventId":1,"LogLevel":"Information","Category":"Store.Services.ProductService","Message":"Placed Order: DataEntities.Order","State":{"Message":"Microsoft.Extensions.Logging.ExtendedLogger\u002BModernTagJoiner","{OriginalFormat}":"Placed Order: {order}","order.Total":269.88,"order.Products":"[\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022,\u0022DataEntities.Product\u0022]","order":"DataEntities.Order","order.CustomerAddress":"*****","order.CustomerName":"*****","order.Id":""}}您應該會看到這個 JSON 格式的日誌條目。 請注意, 順序。標識符 值仍然是空字串,但 CustomerName 和 CustomerAddress 值現在已是
*****.在 [終端機 ] 視窗中,按 Ctrl+C 以停止應用程式。