powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Feature request: Add support to mask/encrypt/decrypt Pydantic models, Dataclasses, and standard Python classes in the DataMasking utility

Open leandrodamascena opened this issue 2 years ago • 6 comments

Use case

Currently, the DataMasking utility only supports operations with traversable types in Python, for example: Lists, Dict, str, and others. It is a limitation for customers who want to integrate DataMasking utility with their existing Pydantic models, data classes, or standard Python classes.

Solution/User Experience

Add support for mask, encrypt and decrypt Pydantic models, Dataclasses, and standard Python classes.

Alternative solutions

No response

Acknowledgment

leandrodamascena avatar Dec 08 '23 14:12 leandrodamascena

We've added this to our backlog, and we intend to work on this early next year.

rubenfonseca avatar Dec 18 '23 12:12 rubenfonseca

Hey @leandrodamascena, whenever you have time this week, could you please leave a comment with some more details about what needs to be done / implemented as part of this issue?

This will help potential contributors to orient themselves.

dreamorosi avatar Feb 10 '25 11:02 dreamorosi

Hey everyone! Our current implementation supports dict and lists as they can be directly converted. However, we recognize that customers may want to use more complex data structures such as Pydantic Models, DataClasses, or custom Python classes.

To accommodate these cases, we need to implement a pre-processing step to prepare the data before submitting it for erase, encrypt, or decrypt. This solution addresses the input data challenge, but it's important to note that we cannot guarantee the data will remain a valid instance of the original Pydantic model after processing, for example.

The encryption/erase process may alter the data type in ways that break the model's validation rules. For example:

from __future__ import annotations

from aws_lambda_powertools.utilities.data_masking import DataMasking

from pydantic import BaseModel

class MyModel(BaseModel):
    name: str
    age: int


data_masker = DataMasking()

data = MyModel(name="powertools", age=5)

erased = data_masker.erase(data, fields=["age"])  

print(erased)
# output: {'name': 'powertools', 'age': '*****'}

Not that age is now a string and no longer an int, breaking Pydantic's validation model.

For now, we can implement this support only when we input the data, not in the output. To do this, I suggest to create a new function called prepare_data in this file and in this function it check the data type and convert it to dict, this could be something like this:

def prepare_data(data: Any) -> Any:
    # Convert from dataclasses
    if hasattr(data, "__dataclass_fields__"):
        import dataclasses

        return dataclasses.asdict(data)

    # Convert from Pydantic model
    if callable(getattr(data, "model_dump", None)):
        return data.model_dump()
    
    # Convert from event source data class
    if callable(getattr(data, "dict", None)):
        return data.dict()

    return data

After that, call this method in the first line of erase, encrypt and decrypt methods.

Also, need to add more tests here https://github.com/aws-powertools/powertools-lambda-python/tree/develop/tests/functional/data_masking to make sure it working as expected.

leandrodamascena avatar Feb 14 '25 12:02 leandrodamascena

@leandrodamascena, I see this a pretty old issue, I can pick this up

VatsalGoel3 avatar Apr 04 '25 18:04 VatsalGoel3

Sure, go ahead @VatsalGoel3.

leandrodamascena avatar Apr 04 '25 19:04 leandrodamascena

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Apr 11 '25 14:04 github-actions[bot]