Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(data-masking): add docs for data masking utility #3186

Merged
merged 178 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
178 commits
Select commit Hold shift + click to select a range
236e223
Skeleton draft of data_masking docs
seshubaws Oct 10, 2023
025fc77
Added example getting started code to data masking docs
seshubaws Oct 11, 2023
0c0c4ac
Added SAM template example and fixed KMS permission info
seshubaws Oct 11, 2023
0112f69
Added clearer file names
seshubaws Oct 11, 2023
a45a4e3
Add testing your code example
seshubaws Oct 12, 2023
7b1645d
Added diagram and fixed highlighting in code examples
seshubaws Oct 12, 2023
3c83aa0
Added SAM template section to md file
seshubaws Oct 12, 2023
36faafb
Merge branch 'develop' into develop
leandrodamascena Oct 18, 2023
a2fbff8
Merge branch 'develop' into develop
leandrodamascena Oct 26, 2023
d26f78d
Merge branch 'develop' into develop
leandrodamascena Nov 6, 2023
3988f10
Separated examples into more tabs, fixed comments
seshubaws Nov 8, 2023
1609105
Fix mypy errors
seshubaws Nov 8, 2023
65a9e9f
Fix mypy errors
seshubaws Nov 14, 2023
9fc33b4
Fix mypy
seshubaws Nov 14, 2023
e631581
Remove itsdangerous
seshubaws Nov 14, 2023
2a959fb
fix mypy
seshubaws Nov 14, 2023
b206873
delete superflous init file
seshubaws Nov 14, 2023
bb1c2a9
Fix mypy
seshubaws Nov 14, 2023
ff8d490
Merge branch 'develop' into develop
seshubaws Nov 14, 2023
e87b05a
Merge branch 'develop' into develop
seshubaws Nov 21, 2023
9f0acb2
Reorganized data masking docs
seshubaws Nov 22, 2023
ae7deb6
nit fixes
seshubaws Nov 22, 2023
b8bd70f
Added itsdangerous as dev dep
seshubaws Nov 22, 2023
2f7c6b7
grammar fixes
seshubaws Dec 5, 2023
3199576
merging from develop
leandrodamascena Dec 8, 2023
b9c2c48
docs: refactor diag to make operations explicit
heitorlessa Dec 8, 2023
b35effd
docs: line editing for intro line
heitorlessa Dec 8, 2023
202d906
docs: line editing for one key feature
heitorlessa Dec 8, 2023
6687ebc
docs: Masking to Possible Operations to remove ambiguity
heitorlessa Dec 8, 2023
5942a70
Removing custom provider
leandrodamascena Dec 8, 2023
619292d
Adding banner
leandrodamascena Dec 8, 2023
c33f18e
docs: remove BYO from key features, highlight best practices
heitorlessa Dec 8, 2023
9ab5a40
Modifying SAM
leandrodamascena Dec 8, 2023
8965413
docs: line editing terminology
heitorlessa Dec 8, 2023
0762ba2
Revise docs
seshubaws Dec 9, 2023
3b49271
Modifying SAM
leandrodamascena Dec 11, 2023
bc97ca8
Merging from develop
leandrodamascena Dec 11, 2023
847c326
Removing itsdangerous dependency - we are not using
leandrodamascena Dec 11, 2023
ed768ca
Fixing mypy errors
leandrodamascena Dec 11, 2023
243e8cb
Adding more information
leandrodamascena Dec 11, 2023
b3f0884
Merge branch 'develop' into develop
leandrodamascena Dec 11, 2023
b6d0470
Added more info about fields param
seshubaws Dec 11, 2023
ef232de
Merge branch 'develop' into develop
leandrodamascena Dec 12, 2023
7111fba
Making error message actionable
leandrodamascena Dec 12, 2023
08fcadc
Making error message actionable
leandrodamascena Dec 12, 2023
3dc9d57
Making error message actionable
leandrodamascena Dec 12, 2023
410ed3b
docs: add first sequence diagram for operations
heitorlessa Dec 12, 2023
42a682b
docs: add encrypt operations sequence diagram
heitorlessa Dec 12, 2023
abe27f1
docs: remove dot notation from mask operation
heitorlessa Dec 12, 2023
a265b64
Adding typing
leandrodamascena Dec 12, 2023
e16833f
Fixes for SAM template comments
seshubaws Dec 13, 2023
ecf505e
Merge branch 'develop' into develop
seshubaws Dec 13, 2023
0cb967a
Add return types for mask()
seshubaws Dec 13, 2023
af86cd0
Merge branch 'develop' into develop
seshubaws Dec 13, 2023
e3c62fd
Addressing Seshu's feedback
leandrodamascena Dec 14, 2023
ebcc343
Merging from develop
leandrodamascena Dec 14, 2023
f41026b
Improving examples
leandrodamascena Dec 14, 2023
9413a26
Improving examples
leandrodamascena Dec 14, 2023
cfae267
docs: improve encrypt ops sequence diagram
heitorlessa Dec 14, 2023
74bbd60
docs: early caching msg before diagram
heitorlessa Dec 14, 2023
db318cd
docs: add caching in encryption sdk ops
heitorlessa Dec 14, 2023
fe184c4
Added max_bytes_encrypted to CMM
seshubaws Dec 14, 2023
65bf540
Fix mypy errors
seshubaws Dec 14, 2023
aacf0db
Adding logging + data type + variable names
leandrodamascena Dec 14, 2023
6970cbf
Added docstrings to baseprovider
seshubaws Dec 15, 2023
20f1315
Explain fields syntax more
seshubaws Dec 15, 2023
1879abc
Merge branch 'develop' into develop
heitorlessa Dec 15, 2023
5832e49
Merge branch 'develop' into develop
seshubaws Dec 15, 2023
007231a
Clarify fields param
seshubaws Dec 15, 2023
b395f12
docs: add decrypt operation diag
heitorlessa Dec 18, 2023
13a1f5d
docs: add encryption ctx, envelope encryption terminologies
heitorlessa Dec 18, 2023
0d8c530
docs: line editing terminology
heitorlessa Dec 18, 2023
352ed1e
docs: correct getting started, install sections
heitorlessa Dec 18, 2023
1dc130c
docs: add note on min memory and separation of concerns upfront
heitorlessa Dec 18, 2023
fd7fd71
docs: use newer Powertools log level env var
heitorlessa Dec 18, 2023
9b1bf22
docs: add missing logger ctx, remove metrics
heitorlessa Dec 18, 2023
7540dcc
docs: line editing comments
heitorlessa Dec 18, 2023
be60985
docs: add code annotation, further cleanup
heitorlessa Dec 18, 2023
963f8cc
docs: remove unused section
heitorlessa Dec 18, 2023
40644f1
docs: move navigation order, fix encryption typo
heitorlessa Dec 18, 2023
1b1f19b
Merge branch 'develop' into develop
heitorlessa Dec 18, 2023
9a33393
docs: line editing masking data
heitorlessa Dec 18, 2023
7733633
docs: fix file names in data masking
heitorlessa Dec 18, 2023
3d7b237
docs: add symmetric word in required resources
heitorlessa Dec 18, 2023
b816197
docs: line editing encrypting data
heitorlessa Dec 18, 2023
2bae940
docs: line editing decrypting data; code snippet fixes
heitorlessa Dec 18, 2023
8104cb3
docs: move load tests and traces upfront
heitorlessa Dec 18, 2023
a714623
docs: use pascal case for encryption sdk provider
heitorlessa Dec 18, 2023
fa24044
docs: remove redundant info from encryption sdk
heitorlessa Dec 18, 2023
0afe8fa
docs: add encryption message, link provider section
heitorlessa Dec 18, 2023
9b14252
docs: add initial encryption context section
heitorlessa Dec 18, 2023
b5a0aef
docs: add initial encryption context section
heitorlessa Dec 18, 2023
07cf100
docs: remove incorrect info on enc ctx being required, thus allowing …
heitorlessa Dec 18, 2023
24bcef7
docs: rename to AAD and add examples
heitorlessa Dec 18, 2023
ca7897f
docs: rename to AAD and add examples
heitorlessa Dec 18, 2023
fec33a6
Adding support to list/set/tuple fields + renaming the class of the p…
leandrodamascena Dec 18, 2023
74fccd7
Small fix
leandrodamascena Dec 18, 2023
a741e7e
Merge remote-tracking branch 'upstream/develop' into seshubaws/develop
leandrodamascena Dec 18, 2023
189bcba
Fixing typing
leandrodamascena Dec 18, 2023
483c1b4
Fixing typing
leandrodamascena Dec 18, 2023
79db91e
Small fixes
seshubaws Dec 18, 2023
e9d93bf
docs: rename to enc ctx and correct wording for upcoming API change t…
heitorlessa Dec 19, 2023
a0921a3
docs: use Dict as return as that's the expected return type
heitorlessa Dec 19, 2023
d9d9e31
docs: fix highlighting
heitorlessa Dec 19, 2023
93a20f8
docs: add choosing parts of your data section
heitorlessa Dec 19, 2023
e7ae1f1
docs: improve JSON section
heitorlessa Dec 19, 2023
ce37852
docs: break down field syntax into two sections
heitorlessa Dec 19, 2023
06fbb09
Merge branch 'develop' into develop
heitorlessa Dec 19, 2023
ea77812
refactor: allow customers to bring custom serializer/deserializer
heitorlessa Dec 19, 2023
f97ffb5
refactor: DataMasking depend on provider serializers
heitorlessa Dec 19, 2023
8b3c6d2
docs: add custom serializer example
heitorlessa Dec 19, 2023
f1c8dde
refactor: allow arbitrary encryption context
heitorlessa Dec 19, 2023
34f8e1b
docs: rename encryption context example
heitorlessa Dec 19, 2023
8519896
fix: encryption context propagation
heitorlessa Dec 19, 2023
7be9566
refactor: validate encryption context values
heitorlessa Dec 19, 2023
1ab12a2
docs: remove todo
heitorlessa Dec 19, 2023
e6889ba
pull out comparing enc_ctx into new method
seshubaws Dec 20, 2023
840b85b
Adding support to jsonpath-ng + refactoring tests
leandrodamascena Dec 20, 2023
2cad772
refactor: delegate encoding/decoding to shared fn as much as possible…
heitorlessa Dec 20, 2023
a770fa1
refactor: correct typing for encrypt
heitorlessa Dec 20, 2023
a918ced
refactor: correct typing for mask
heitorlessa Dec 20, 2023
f937d80
docs: correct code snippets typing
heitorlessa Dec 20, 2023
e9735d5
Adding flag to support raise on non existing field
leandrodamascena Dec 20, 2023
f763a11
docs: make it explicit the behaviour diff in encrypt & mask
heitorlessa Dec 20, 2023
634cfc2
Adding examples on how to access data
leandrodamascena Dec 20, 2023
4eef61e
Modifying perf test
leandrodamascena Dec 20, 2023
c284d4a
refactor: split lambda in partial + lambda to ease maintenance
heitorlessa Dec 20, 2023
b1da92e
refactor: decryption context for exact match
heitorlessa Dec 20, 2023
65b3d6a
chore: test encryption ctx validation
heitorlessa Dec 20, 2023
e0f4a3a
chore: fix linting on loop variable
heitorlessa Dec 20, 2023
fbed1a1
Adding complex examples + using ext ngjson
leandrodamascena Dec 20, 2023
d89cbcc
Adding text + increasing perf threshold
leandrodamascena Dec 20, 2023
5d1687e
Making utility public
leandrodamascena Dec 20, 2023
49b180a
Merge branch 'develop' into develop
leandrodamascena Dec 20, 2023
b0213a1
Adding chaging algorithm section
leandrodamascena Dec 20, 2023
64d6352
Merge branch 'develop' into develop
seshubaws Dec 20, 2023
95bc7a1
docs: address initial feedback
heitorlessa Dec 20, 2023
ace1c28
docs: re-incorporate initial feedback
heitorlessa Dec 20, 2023
429eb8a
Adding test
leandrodamascena Dec 20, 2023
85766bf
Fixed docstrings, added a test
seshubaws Jan 23, 2024
58c66f9
Merge develop from develop
seshubaws Jan 23, 2024
2328c50
Merge branch 'develop' into develop
leandrodamascena Jan 26, 2024
95098e5
Removed fields param from enc+dec methods
seshubaws Jan 29, 2024
af6932a
trying to fix pytest
seshubaws Jan 29, 2024
fbdcfd1
Merge branch 'develop' into develop
seshubaws Jan 29, 2024
9116ba6
trying to fix pytest
seshubaws Jan 29, 2024
5251e0c
Fix linting
seshubaws Jan 29, 2024
284d340
debug linting
seshubaws Jan 29, 2024
d01b657
debug linting
seshubaws Jan 29, 2024
00f6165
Changing mask to erase in docs, comments, etc
seshubaws Jan 29, 2024
5949cea
Fix for jsonpath upgrade
seshubaws Jan 29, 2024
cfa20c8
Fixing mypy typing
seshubaws Jan 29, 2024
7864bed
Merge branch 'develop' into develop
seshubaws Jan 29, 2024
eb66f19
test e2e
seshubaws Jan 30, 2024
0f01d50
Renaming files
leandrodamascena Jan 30, 2024
0d96349
Fixing examples
leandrodamascena Jan 30, 2024
990e8ad
Fixing examples
leandrodamascena Jan 30, 2024
cbc2b14
Merge branch 'develop' into develop
leandrodamascena Jan 30, 2024
a6bb1e3
Used AWS guidelines safe names in examples
seshubaws Jan 30, 2024
b7a3345
Merge branch 'develop' into develop
seshubaws Jan 30, 2024
62e1aba
Docs cleanup
seshubaws Jan 30, 2024
5ca7f99
Revise docs
seshubaws Jan 30, 2024
160b85f
Merge branch 'develop' into develop
leandrodamascena Jan 31, 2024
f116d3c
Update docs/utilities/data_masking.md
seshubaws Jan 31, 2024
839755a
Update docs/utilities/data_masking.md
seshubaws Jan 31, 2024
2a38326
Update docs/utilities/data_masking.md
seshubaws Jan 31, 2024
a6a975c
Added data masking to features lists, removed unnecessary tabs
seshubaws Jan 31, 2024
4156b3d
Made passing sdk args section more general
seshubaws Jan 31, 2024
f0cc727
Added using multiple keys section
seshubaws Jan 31, 2024
9d1d1e9
Fix lint
seshubaws Jan 31, 2024
581145d
Polishing few things
leandrodamascena Feb 1, 2024
b374d9f
Merge branch 'develop' into develop
leandrodamascena Feb 1, 2024
8f40a15
Addressing Heitor's feedback
leandrodamascena Feb 1, 2024
09f89ea
Adding workflow
leandrodamascena Feb 1, 2024
f19651d
Addressing GH hidden conversations
leandrodamascena Feb 1, 2024
f660683
Documentation
Feb 1, 2024
cfeb833
Final changes
leandrodamascena Feb 1, 2024
007fba7
Lock file
leandrodamascena Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions docs/utilities/data_masking.md
seshubaws marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
title: Data Masking
description: Utility
---

<!-- markdownlint-disable MD051 -->

The data masking utility provides a simple solution to conceal incoming data so that sensitive information is not passed downstream or logged.

## Key features

* Mask data irreversibly without having to install any encryption library.
* Out of the box integration with the [AWS Encryption SDK](https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/introduction.html){target="_blank" rel="nofollow"} to easily encrypt and decrypt data.
* Install any encryption provider and connect it with our new Data Masker class to easily mask, encrypt, and decrypt data.

## Terminology

Mask: This refers to concealing or partially replacing sensitive information with a non-sensitive placeholder or mask. The key characteristic of this operation is that it is irreversible, meaning the original sensitive data cannot be retrieved from the masked data. Masking is commonly applied when displaying data to users or for anonymizing data in non-reversible scenarios. For example, display the last four digits of a credit card number as "**** **** **** 1234".

Encrypt: This is the process of transforming plaintext data into a ciphertext format using an encryption algorithm and a cryptographic key. Encryption is a reversible process, meaning the original data can be retrieved (decrypted) using the appropriate decryption key. You can use this, for instance, to encrypt any PII (personally identifiable information) of your customers and make sure only the people with the right permissions are allowed to decrypt and view the plaintext PII data, in accordance with GDPR.

Decrypt: This is the process of reversing the encryption process, converting ciphertext back into its original plaintext using a decryption algorithm and the correct decryption key that only authorized personnel should have access to.

## Getting started
seshubaws marked this conversation as resolved.
Show resolved Hide resolved

### IAM Permissions

If using the AWS Encryption SDK, your Lambda function IAM Role must have `kms:Decrypt` and `kms:GenerateDataKey` IAM permissions.

If using any other encryption provider, make sure to have the permissions for your role that it requires.

If not using any encryption services and just masking data, your Lambda does not need any additional permissions to use this utility.

### Required resources

If using the AWS Encryption SDK, you must have an AWS KMS key with full read/write permissions. You can create one and learn more on the [AWS KMS console](https://us-east-1.console.aws.amazon.com/kms/home?region=us-east-1#/kms/home){target="_blank" rel="nofollow"}.

If using any other encryption provider, you must have the resources required for that provider.

## Using the utility

### Masking data

You can mask data without having to install any encryption library.

=== "getting_started_mask_data.py"
```python hl_lines="1 6 21 35 44"
--8<-- "examples/data_masking/src/getting_started_mask_data.py"
```

### Encryting and decrypting data

In order to encrypt data, you must use either our out-of-the-box integration with the AWS Encryption SDK, or install another encryption provider of your own. You can still use the masking feature while using any encryption provider.

=== "getting_started_encrypt_data.py"
```python hl_lines="3-4 6 23-26 40 54 63 77 80 94"
--8<-- "examples/data_masking/src/getting_started_encrypt_data.py"
```

### SAM template example
=== "template.yaml"
```yaml hl_lines="11-23 30 33-39 46"
--8<-- "examples/data_masking/sam/template.yaml"
```

=== "data_masking_function_example.py"
```python hl_lines="8 47-50"
--8<-- "examples/data_masking/src/data_masking_function_example.py"
```

=== "output.json"
```json
--8<-- "examples/data_masking/src/data_masking_function_example_output.json"
```

## Advanced

### Adjusting configurations for AWS Encryption SDK

You have the option to modify some of the configurations we have set as defaults when connecting to the AWS Encryption SDK. You can find and modify these values at `utilities/data_masking/constants.py`.

The `CACHE_CAPACITY` value is currently set at `100`. This value represents the maximum number of entries that can be retained in the local cryptographic materials cache. Please see the [AWS Encryption SDK documentation](https://aws-encryption-sdk-python.readthedocs.io/en/latest/generated/aws_encryption_sdk.caches.local.html){target="_blank" rel="nofollow"} for more information.

The `MAX_CACHE_AGE_SECONDS` value is currently set at `300`. It represents the maximum time (in seconds) that a cache entry may be kept in the cache.

The `MAX_MESSAGES_ENCRYPTED` value is currently set at `200`. It represents the maximum number of messages that may be encrypted under a cache entry. Please see the [AWS Encryption SDK documentation](https://aws-encryption-sdk-python.readthedocs.io/en/latest/generated/aws_encryption_sdk.materials_managers.caching.html#module-aws_encryption_sdk.materials_managers.caching){target="_blank" rel="nofollow"} for more information about this and `MAX_CACHE_AGE_SECONDS`.


### Create your own encryption provider

You can create your own custom encryption provider by inheriting the `BaseProvider` class, and implementing the `encrypt()` and `decrypt()` methods, and optionally the `mask()` method. You can also either use your own data serializer and deserializer by passing the `BaseProvider` class a `json_serializer` and `json_deserializer` argument, or you can use the default.

<!-- markdownlint-disable MD031 MD040 -->
<center>
```mermaid
classDiagram
direction LR
class BaseProvider {
<<interface>>
+encrypt(data: Any)
+decrypt(data: str)
+mask(data: Any)
}

class YourCustomEncryptionProvider {
+encrypt(data: Any)
+decrypt(data: str)
+mask(data: Any)
}

BaseProvider <|-- YourCustomEncryptionProvider : implement
```
<i>Visual representation to bring your own encryption provider</i>
</center>

* **`encrypt()`** – handles all logic for how to encrypt any data
* **`decrypt()`** – handles all logic for how to decrypt encrypted data
* **`mask()`** – handles all logic for how to irreversably mask data (optional)

You can then use this custom encryption provider class as the `provider` argument when creating a new `DataMasking` instance to use the encryption and decryption algorithms of the encryption library you have chosen.

Here is an example of implementing a custom encryption using an external encryption library like [ItsDangerous](https://itsdangerous.palletsprojects.com/en/2.1.x/){target="_blank" rel="nofollow"}, a widely popular encryption library.

=== "working_with_own_provider.py"
```python hl_lines="1-2 19-22 36 50 59 73 76 90"
--8<-- "examples/data_masking/src/working_with_own_provider.py"
```

=== "custom_provider.py"
```python hl_lines="1 3 6 8 11 16"
--8<-- "examples/data_masking/src/custom_provider.py"
```

## Testing your code

For unit testing your applications, you can mock the calls to the data masking utility to avoid calling AWS APIs. This can be achieved in a number of ways - in this example, we use the pytest monkeypatch fixture to patch the `data_masking.decrypt` method.

=== "test_single_mock.py"
```python hl_lines="4 8"
--8<-- "examples/data_masking/tests/test_single_mock.py"
```

=== "single_mock.py"
```python
--8<-- "examples/data_masking/tests/src/single_mock.py"
```

If we need to use this pattern across multiple tests, we can avoid repetition by refactoring to use our own pytest fixture:

=== "test_with_fixture.py"
```python hl_lines="5 10"
--8<-- "examples/data_masking/tests/test_with_fixture.py"
```
57 changes: 57 additions & 0 deletions examples/data_masking/sam/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Powertools for AWS Lambda (Python) data masking example

Globals: # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html
Function:
Timeout: 5
Runtime: python3.10
Tracing: Active
Resources:
MyKMSKey:
Type: AWS::KMS::Key
Properties:
Enabled: true
KeyPolicy:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: kms:*
Resource: "*"
Principal:
AWS: !Join [ "", [ "arn:aws:iam::", !Ref "AWS::AccountId", ":root" ] ]
DataMaskingFunctionExample:
Type: AWS::Serverless::Function # More info about Function Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html
Properties:
Handler: data_masking_function_example.lambda_handler
CodeUri: ../src
Description: Data Masking Function Example
MemorySize: 1024 # TODO: Recommended to use 1024 MB due to
seshubaws marked this conversation as resolved.
Show resolved Hide resolved
Architectures:
- x86_64
Policies:
Statement:
- Effect: Allow
Action:
- kms:Decrypt
- kms:GenerateDataKey
Resource: !GetAtt MyKMSKey.Arn
Tracing: Active
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: PowertoolsHelloWorld
POWERTOOLS_METRICS_NAMESPACE: Powertools
LOG_LEVEL: INFO
KMS_KEY_ARN: !GetAtt MyKMSKey.Arn
Tags:
LambdaPowertools: python

Outputs:
KMSKeyArn:
Description: ARN of the KMS Key
Value: !GetAtt MyKMSKey.Arn

DataMaskingFunctionExample:
Description: Data Masking Function Example
Value: !GetAtt DataMaskingFunctionExample.Arn
19 changes: 19 additions & 0 deletions examples/data_masking/src/custom_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from itsdangerous.url_safe import URLSafeSerializer

from aws_lambda_powertools.utilities.data_masking.provider import BaseProvider


class MyCustomEncryption(BaseProvider):
def __init__(self, secret):
super().__init__()
self.secret = URLSafeSerializer(secret)

def encrypt(self, data: str) -> str:
if data is None:
return data
return self.secret.dumps(data)

def decrypt(self, data: str) -> str:
if data is None:
return data
return self.secret.loads(data)
50 changes: 50 additions & 0 deletions examples/data_masking/src/data_masking_function_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.data_masking import DataMasking
from aws_lambda_powertools.utilities.data_masking.provider.kms.aws_encryption_sdk import AwsEncryptionSdkProvider
from aws_lambda_powertools.utilities.typing import LambdaContext

KMS_KEY_ARN = os.environ["KMS_KEY_ARN"]

json_blob = {
"id": 1,
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com",
"address": {"street": "123 Main St", "city": "Anytown", "state": "CA", "zip": "12345"},
"phone_numbers": ["+1-555-555-1234", "+1-555-555-5678"],
"interests": ["Hiking", "Traveling", "Photography", "Reading"],
"job_history": {
"company": {
"company_name": "Acme Inc.",
"company_address": "5678 Interview Dr.",
},
"position": "Software Engineer",
"start_date": "2015-01-01",
"end_date": "2017-12-31",
},
"about_me": """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt velit quis
sapien mollis, at egestas massa tincidunt. Suspendisse ultrices arcu a dolor dapibus,
ut pretium turpis volutpat. Vestibulum at sapien quis sapien dignissim volutpat ut a enim.
Praesent fringilla sem eu dui convallis luctus. Donec ullamcorper, sapien ut convallis congue,
risus mauris pretium tortor, nec dignissim arcu urna a nisl. Vivamus non fermentum ex. Proin
interdum nisi id sagittis egestas. Nam sit amet nisi nec quam pharetra sagittis. Aliquam erat
volutpat. Donec nec luctus sem, nec ornare lorem. Vivamus vitae orci quis enim faucibus placerat.
Nulla facilisi. Proin in turpis orci. Donec imperdiet velit ac tellus gravida, eget laoreet tellus
malesuada. Praesent venenatis tellus ac urna blandit, at varius felis posuere. Integer a commodo nunc.
""",
}

tracer = Tracer()
logger = Logger()


@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
logger.info("Hello world function - HTTP 200")
data_masker = DataMasking(provider=AwsEncryptionSdkProvider(keys=[KMS_KEY_ARN]))
encrypted = data_masker.encrypt(json_blob, fields=["address.street", "job_history.company.company_name"])
decrypted = data_masker.decrypt(encrypted, fields=["address.street", "job_history.company.company_name"])
return {"Decrypted_json": decrypted}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"Decrypted_json": {
"id": 1,
"name": "John Doe",
"age": 30,
"email": "johndoe@example.com",
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"zip": "12345"
},
"phone_numbers": [
"+1-555-555-1234",
"+1-555-555-5678"
],
"interests": [
"Hiking",
"Traveling",
"Photography",
"Reading"
],
"job_history": {
"company": {
"company_name": "Acme Inc.",
"company_address": "5678 Interview Dr."
},
"position": "Software Engineer",
"start_date": "2015-01-01",
"end_date": "2017-12-31"
},
"about_me": "\n Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla tincidunt velit quis\n sapien mollis, at egestas massa tincidunt. Suspendisse ultrices arcu a dolor dapibus,\n ut pretium turpis volutpat. Vestibulum at sapien quis sapien dignissim volutpat ut a enim.\n Praesent fringilla sem eu dui convallis luctus. Donec ullamcorper, sapien ut convallis congue,\n risus mauris pretium tortor, nec dignissim arcu urna a nisl. Vivamus non fermentum ex. Proin\n interdum nisi id sagittis egestas. Nam sit amet nisi nec quam pharetra sagittis. Aliquam erat\n volutpat. Donec nec luctus sem, nec ornare lorem. Vivamus vitae orci quis enim faucibus placerat.\n Nulla facilisi. Proin in turpis orci. Donec imperdiet velit ac tellus gravida, eget laoreet tellus\n malesuada. Praesent venenatis tellus ac urna blandit, at varius felis posuere. Integer a commodo nunc.\n "
}
}
Loading