Repository navigation
feat(data-masking): add Data Masking utility (Erase, Encrypt/Decrypt, Logging integration) - #1277
nicolastarzia wants to merge 5 commits into
Conversation
Introduces the AWS.Lambda.Powertools.DataMasking package with the Erase operation for irreversibly masking sensitive fields in a payload. - DataMasking.Erase overloads: string and JsonNode (AOT-safe), object (annotated RequiresUnreferencedCode/RequiresDynamicCode), and object + JsonTypeInfo (AOT-safe via source generator) - MaskingOptions: default *****, fixed value, length-preserving, and regex + replacement rules - Field selection via simple dotted paths over System.Text.Json.Nodes (JMESPath expression support deferred to a later phase) - Unit tests (13) and an examples/DataMasking sample function Relates to aws-powertools#1257. Phase 1 (Erase) only; encryption/decryption via a pluggable provider (AWS Encryption SDK + KMS) planned for later phases.
Adds reversible encryption to the Data Masking utility via a pluggable provider, with a default AWS KMS envelope-encryption implementation. - IDataMaskingProvider abstraction (EncryptAsync/DecryptAsync with encryption context and cancellation token) - AwsEncryptionSdkProvider: default provider using the AWS Encryption SDK for .NET; single-key (CreateAwsKmsKeyring) or multi-key (multi-keyring); supports a custom KMS client. Annotated RequiresUnreferencedCode/ RequiresDynamicCode since the SDK's Dafny runtime is not AOT-safe. - DataMasking: optional ctor(IDataMaskingProvider) plus EncryptAsync/ DecryptAsync for whole-payload and field-level encryption over JsonNode - JsonNodeMasker.TransformFieldsAsync for async field transforms - Central Package Management: AWS.Cryptography.EncryptionSDK 5.0.0 and AWSSDK.KeyManagementService - Tests: FakeDataMaskingProvider (reversible, context-binding) and 8 Encrypt/Decrypt tests (round-trip, field-level, context validation, missing-provider guards). 21 tests total pass on net10.0. Relates to aws-powertools#1257. Phase 2 (Encrypt/Decrypt) building on Phase 1 (Erase). The AwsEncryptionSdkProvider is not covered by unit tests as it requires live AWS KMS credentials.
Adds an ergonomic way to redact sensitive fields before they reach logs, without coupling the Data Masking and Logging packages. - DataMasking.EraseToNode(string json, string[] fields, MaskingOptions?) returns a JsonNode ready for structured logging, e.g. Logger.LogInformation(masker.EraseToNode(json, fields)) — no extra string round-trip. Fully trimming/AOT safe. - Example updated to log the masked payload via EraseToNode - Package and example READMEs updated with the Logging integration Relates to aws-powertools#1257. Phase 3 (Logging integration + docs) completing the Erase / Encrypt / Decrypt trio. 25 unit tests pass on net10.0.
- Add a timeout to the example Regex to avoid potential ReDoS (fixes the Security rating on new code) - Reduce code duplication in DataMasking by extracting MaskRoot/Serialize/ TransformFieldsToStringAsync helpers (fixes duplication > 3% on new code) - Declare an explicit CloudWatch Log Group in the example SAM template (with 7-day retention), matching the BatchProcessing example All 25 unit tests still pass on net10.0.
Extract a shared TryResolveLeaf (and ReadRawValue) helper so the sync mask and async transform paths no longer duplicate the dotted-path walk, bringing new-code duplication under the SonarCloud threshold. Behavior unchanged; 25 tests still pass.
|
|
tests failing |
|
Hey @hjgraca , First of all, sorry my late response and thanks for flagging. I've looked the failed step and it's related to this package: Related to this package, did not involves code this PR touches: the PR only adds the new DataMasking package, ConcurrencyTests doesn't reference it, and its transitive package graph is identical to develop's. Locally all tests passed in all runs, on my branch and on develop. |



Issue number: #1257
Closes #1257
Summary
Changes
Adds a new
AWS.Lambda.Powertools.DataMaskingutility that protects sensitive data (PII, credentials, financial records) before it is logged, stored, or forwarded — helping meet GDPR/HIPAA/PCI-DSS requirements. Ported from the Powertools for Python/TypeScript design.Delivered in three phases:
DataMasking.Erase(...)masks fields in place while keeping the surrounding payload readable. Overloads forstringandJsonNode(AOT-safe),object(annotated), andobject + JsonTypeInfo(AOT-safe via source generator).MaskingOptionssupports a fixed mask (default*****), length-preserving mask, and regex + replacement.IDataMaskingProvider; defaultAwsEncryptionSdkProviderperforms AWS KMS envelope encryption via the AWS Encryption SDK for .NET, with encryption-context binding/validation and single- or multi-key keyrings.EncryptAsync/DecryptAsyncsupport whole-payload and field-level operations.EraseToNode(...)returns aJsonNodeready for structured logging, so users canLogger.LogInformation(masker.EraseToNode(json, fields))without coupling the Data Masking and Logging packages.Field selection uses simple dotted paths (for example
address.street). Also includes a runnable example underexamples/DataMaskingand unit tests.User experience
Before: .NET customers hand-roll masking/encryption logic, which is inconsistent across teams and easy to get wrong (especially field-level encryption that preserves structure, and KMS envelope encryption with data-key handling).
After: a single, consistent utility:
Checklist
Testing
net10.0(13 Erase + 8 Encrypt/Decrypt + 4 EraseToNode). Library builds clean multi-targetingnet8.0;net10.0.AwsEncryptionSdkProvideritself is not unit-tested as it requires live AWS KMS credentials; the DataMasking Encrypt/Decrypt orchestration is tested against a reversible in-memory fake provider.Open design questions for maintainers
Since this is a new public utility, a few choices would benefit from your input:
JsonNode. Full JMESPath support (the repo already shipsAWS.Lambda.Powertools.JMESPath, which currently only extracts, not sets) is deferred — extend JMESPath or keep an internal setter?IDataMaskingProviderexposesEncryptAsync/DecryptAsyncfor future-proofing (the default provider wraps sync calls). Keep async or expose sync?AwsEncryptionSdkProviderdepends on the SDK's Dafny runtime (reflection) and is annotated[RequiresDynamicCode]/[RequiresUnreferencedCode];Erase/EraseToNoderemain AOT-safe.Is this a breaking change?
No. This adds a new package; no existing library code or public API is modified.
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.