-
Notifications
You must be signed in to change notification settings - Fork 429
docs(data-masking): add docs for data masking utility #3186
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
178 commits
Select commit
Hold shift + click to select a range
236e223
Skeleton draft of data_masking docs
seshubaws 025fc77
Added example getting started code to data masking docs
seshubaws 0c0c4ac
Added SAM template example and fixed KMS permission info
seshubaws 0112f69
Added clearer file names
seshubaws a45a4e3
Add testing your code example
seshubaws 7b1645d
Added diagram and fixed highlighting in code examples
seshubaws 3c83aa0
Added SAM template section to md file
seshubaws 36faafb
Merge branch 'develop' into develop
leandrodamascena a2fbff8
Merge branch 'develop' into develop
leandrodamascena d26f78d
Merge branch 'develop' into develop
leandrodamascena 3988f10
Separated examples into more tabs, fixed comments
seshubaws 1609105
Fix mypy errors
seshubaws 65a9e9f
Fix mypy errors
seshubaws 9fc33b4
Fix mypy
seshubaws e631581
Remove itsdangerous
seshubaws 2a959fb
fix mypy
seshubaws b206873
delete superflous init file
seshubaws bb1c2a9
Fix mypy
seshubaws ff8d490
Merge branch 'develop' into develop
seshubaws e87b05a
Merge branch 'develop' into develop
seshubaws 9f0acb2
Reorganized data masking docs
seshubaws ae7deb6
nit fixes
seshubaws b8bd70f
Added itsdangerous as dev dep
seshubaws 2f7c6b7
grammar fixes
seshubaws 3199576
merging from develop
leandrodamascena b9c2c48
docs: refactor diag to make operations explicit
heitorlessa b35effd
docs: line editing for intro line
heitorlessa 202d906
docs: line editing for one key feature
heitorlessa 6687ebc
docs: Masking to Possible Operations to remove ambiguity
heitorlessa 5942a70
Removing custom provider
leandrodamascena 619292d
Adding banner
leandrodamascena c33f18e
docs: remove BYO from key features, highlight best practices
heitorlessa 9ab5a40
Modifying SAM
leandrodamascena 8965413
docs: line editing terminology
heitorlessa 0762ba2
Revise docs
seshubaws 3b49271
Modifying SAM
leandrodamascena bc97ca8
Merging from develop
leandrodamascena 847c326
Removing itsdangerous dependency - we are not using
leandrodamascena ed768ca
Fixing mypy errors
leandrodamascena 243e8cb
Adding more information
leandrodamascena b3f0884
Merge branch 'develop' into develop
leandrodamascena b6d0470
Added more info about fields param
seshubaws ef232de
Merge branch 'develop' into develop
leandrodamascena 7111fba
Making error message actionable
leandrodamascena 08fcadc
Making error message actionable
leandrodamascena 3dc9d57
Making error message actionable
leandrodamascena 410ed3b
docs: add first sequence diagram for operations
heitorlessa 42a682b
docs: add encrypt operations sequence diagram
heitorlessa abe27f1
docs: remove dot notation from mask operation
heitorlessa a265b64
Adding typing
leandrodamascena e16833f
Fixes for SAM template comments
seshubaws ecf505e
Merge branch 'develop' into develop
seshubaws 0cb967a
Add return types for mask()
seshubaws af86cd0
Merge branch 'develop' into develop
seshubaws e3c62fd
Addressing Seshu's feedback
leandrodamascena ebcc343
Merging from develop
leandrodamascena f41026b
Improving examples
leandrodamascena 9413a26
Improving examples
leandrodamascena cfae267
docs: improve encrypt ops sequence diagram
heitorlessa 74bbd60
docs: early caching msg before diagram
heitorlessa db318cd
docs: add caching in encryption sdk ops
heitorlessa fe184c4
Added max_bytes_encrypted to CMM
seshubaws 65bf540
Fix mypy errors
seshubaws aacf0db
Adding logging + data type + variable names
leandrodamascena 6970cbf
Added docstrings to baseprovider
seshubaws 20f1315
Explain fields syntax more
seshubaws 1879abc
Merge branch 'develop' into develop
heitorlessa 5832e49
Merge branch 'develop' into develop
seshubaws 007231a
Clarify fields param
seshubaws b395f12
docs: add decrypt operation diag
heitorlessa 13a1f5d
docs: add encryption ctx, envelope encryption terminologies
heitorlessa 0d8c530
docs: line editing terminology
heitorlessa 352ed1e
docs: correct getting started, install sections
heitorlessa 1dc130c
docs: add note on min memory and separation of concerns upfront
heitorlessa fd7fd71
docs: use newer Powertools log level env var
heitorlessa 9b1bf22
docs: add missing logger ctx, remove metrics
heitorlessa 7540dcc
docs: line editing comments
heitorlessa be60985
docs: add code annotation, further cleanup
heitorlessa 963f8cc
docs: remove unused section
heitorlessa 40644f1
docs: move navigation order, fix encryption typo
heitorlessa 1b1f19b
Merge branch 'develop' into develop
heitorlessa 9a33393
docs: line editing masking data
heitorlessa 7733633
docs: fix file names in data masking
heitorlessa 3d7b237
docs: add symmetric word in required resources
heitorlessa b816197
docs: line editing encrypting data
heitorlessa 2bae940
docs: line editing decrypting data; code snippet fixes
heitorlessa 8104cb3
docs: move load tests and traces upfront
heitorlessa a714623
docs: use pascal case for encryption sdk provider
heitorlessa fa24044
docs: remove redundant info from encryption sdk
heitorlessa 0afe8fa
docs: add encryption message, link provider section
heitorlessa 9b14252
docs: add initial encryption context section
heitorlessa b5a0aef
docs: add initial encryption context section
heitorlessa 07cf100
docs: remove incorrect info on enc ctx being required, thus allowing …
heitorlessa 24bcef7
docs: rename to AAD and add examples
heitorlessa ca7897f
docs: rename to AAD and add examples
heitorlessa fec33a6
Adding support to list/set/tuple fields + renaming the class of the p…
leandrodamascena 74fccd7
Small fix
leandrodamascena a741e7e
Merge remote-tracking branch 'upstream/develop' into seshubaws/develop
leandrodamascena 189bcba
Fixing typing
leandrodamascena 483c1b4
Fixing typing
leandrodamascena 79db91e
Small fixes
seshubaws e9d93bf
docs: rename to enc ctx and correct wording for upcoming API change t…
heitorlessa a0921a3
docs: use Dict as return as that's the expected return type
heitorlessa d9d9e31
docs: fix highlighting
heitorlessa 93a20f8
docs: add choosing parts of your data section
heitorlessa e7ae1f1
docs: improve JSON section
heitorlessa ce37852
docs: break down field syntax into two sections
heitorlessa 06fbb09
Merge branch 'develop' into develop
heitorlessa ea77812
refactor: allow customers to bring custom serializer/deserializer
heitorlessa f97ffb5
refactor: DataMasking depend on provider serializers
heitorlessa 8b3c6d2
docs: add custom serializer example
heitorlessa f1c8dde
refactor: allow arbitrary encryption context
heitorlessa 34f8e1b
docs: rename encryption context example
heitorlessa 8519896
fix: encryption context propagation
heitorlessa 7be9566
refactor: validate encryption context values
heitorlessa 1ab12a2
docs: remove todo
heitorlessa e6889ba
pull out comparing enc_ctx into new method
seshubaws 840b85b
Adding support to jsonpath-ng + refactoring tests
leandrodamascena 2cad772
refactor: delegate encoding/decoding to shared fn as much as possible…
heitorlessa a770fa1
refactor: correct typing for encrypt
heitorlessa a918ced
refactor: correct typing for mask
heitorlessa f937d80
docs: correct code snippets typing
heitorlessa e9735d5
Adding flag to support raise on non existing field
leandrodamascena f763a11
docs: make it explicit the behaviour diff in encrypt & mask
heitorlessa 634cfc2
Adding examples on how to access data
leandrodamascena 4eef61e
Modifying perf test
leandrodamascena c284d4a
refactor: split lambda in partial + lambda to ease maintenance
heitorlessa b1da92e
refactor: decryption context for exact match
heitorlessa 65b3d6a
chore: test encryption ctx validation
heitorlessa e0f4a3a
chore: fix linting on loop variable
heitorlessa fbed1a1
Adding complex examples + using ext ngjson
leandrodamascena d89cbcc
Adding text + increasing perf threshold
leandrodamascena 5d1687e
Making utility public
leandrodamascena 49b180a
Merge branch 'develop' into develop
leandrodamascena b0213a1
Adding chaging algorithm section
leandrodamascena 64d6352
Merge branch 'develop' into develop
seshubaws 95bc7a1
docs: address initial feedback
heitorlessa ace1c28
docs: re-incorporate initial feedback
heitorlessa 429eb8a
Adding test
leandrodamascena 85766bf
Fixed docstrings, added a test
seshubaws 58c66f9
Merge develop from develop
seshubaws 2328c50
Merge branch 'develop' into develop
leandrodamascena 95098e5
Removed fields param from enc+dec methods
seshubaws af6932a
trying to fix pytest
seshubaws fbdcfd1
Merge branch 'develop' into develop
seshubaws 9116ba6
trying to fix pytest
seshubaws 5251e0c
Fix linting
seshubaws 284d340
debug linting
seshubaws d01b657
debug linting
seshubaws 00f6165
Changing mask to erase in docs, comments, etc
seshubaws 5949cea
Fix for jsonpath upgrade
seshubaws cfa20c8
Fixing mypy typing
seshubaws 7864bed
Merge branch 'develop' into develop
seshubaws eb66f19
test e2e
seshubaws 0f01d50
Renaming files
leandrodamascena 0d96349
Fixing examples
leandrodamascena 990e8ad
Fixing examples
leandrodamascena cbc2b14
Merge branch 'develop' into develop
leandrodamascena a6bb1e3
Used AWS guidelines safe names in examples
seshubaws b7a3345
Merge branch 'develop' into develop
seshubaws 62e1aba
Docs cleanup
seshubaws 5ca7f99
Revise docs
seshubaws 160b85f
Merge branch 'develop' into develop
leandrodamascena f116d3c
Update docs/utilities/data_masking.md
seshubaws 839755a
Update docs/utilities/data_masking.md
seshubaws 2a38326
Update docs/utilities/data_masking.md
seshubaws a6a975c
Added data masking to features lists, removed unnecessary tabs
seshubaws 4156b3d
Made passing sdk args section more general
seshubaws f0cc727
Added using multiple keys section
seshubaws 9d1d1e9
Fix lint
seshubaws 581145d
Polishing few things
leandrodamascena b374d9f
Merge branch 'develop' into develop
leandrodamascena 8f40a15
Addressing Heitor's feedback
leandrodamascena 09f89ea
Adding workflow
leandrodamascena f19651d
Addressing GH hidden conversations
leandrodamascena f660683
Documentation
cfeb833
Final changes
leandrodamascena 007fba7
Lock file
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
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="3 10" | ||
--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 10" | ||
--8<-- "examples/data_masking/src/getting_started_encrypt_data.py" | ||
``` | ||
|
||
## 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 both the `encrypt()` and `decrypt()` methods in order to encrypt and decrypt data using your custom encryption provider. 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. | ||
|
||
All masking logic is handled by the `mask()` and methods from the `BaseProvider` class. | ||
|
||
Here is an examples 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="5 13 20 24" | ||
--8<-- "examples/data_masking/src/working_with_own_provider.py" | ||
``` | ||
|
||
=== "custom_provider.py" | ||
```python hl_lines="6 9 17 24" | ||
--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.encrypt` 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" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
examples/data_masking/src/data_masking_function_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "[email protected]", | ||
"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} |
34 changes: 34 additions & 0 deletions
34
examples/data_masking/src/data_masking_function_example_output.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"Decrypted_json": { | ||
"id": 1, | ||
"name": "John Doe", | ||
"age": 30, | ||
"email": "[email protected]", | ||
"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 " | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
examples/data_masking/src/getting_started_encrypt_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os | ||
|
||
from aws_lambda_powertools.utilities.data_masking import DataMasking | ||
from aws_lambda_powertools.utilities.data_masking.provider.kms.aws_encryption_sdk import AwsEncryptionSdkProvider | ||
|
||
KMS_KEY_ARN = os.environ["KMS_KEY_ARN"] | ||
|
||
def lambda_handler(event, context): | ||
|
||
data = { | ||
"id": 1, | ||
"name": "John Doe", | ||
"age": 30, | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "123 Main St", | ||
"city": "Anytown", | ||
"state": "CA", | ||
"zip": "12345", | ||
}, | ||
} | ||
|
||
encryption_provider = AwsEncryptionSdkProvider(keys=[KMS_KEY_ARN]) | ||
data_masker = DataMasking(provider=encryption_provider) | ||
|
||
encrypted = data_masker.encrypt(data=data, fields=["email", "address.street"]) | ||
# encrypted = { | ||
seshubaws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "InRoaXMgaXMgYSBzdHJpbmciHsLZGx2na-XzP_TB5Bf2LNU1bLc", | ||
# "address": { | ||
# "street": "XMgYSB_KDddaDJYMb-JpbmGnagTklwQ-msdaDLP", | ||
# "city": "Anytown", | ||
# "state": "CA", | ||
# "zip": "12345" | ||
# }, | ||
# } | ||
|
||
decrypted = data_masker.decrypt(data=encrypted, fields=["email", "address.street"]) | ||
# decrypted = { | ||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "[email protected]", | ||
# "address": { | ||
# "street": "123 Main St", | ||
# "city": "Anytown", | ||
# "state": "CA", | ||
# "zip": "12345" | ||
# }, | ||
# } | ||
|
||
encrypted = data_masker.encrypt(data=data, fields=["email", "address"]) | ||
# encrypted = { | ||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "InRoaXMgaXMgYSBzdHJpbmciHsLZGx2na-XzP_TB5Bf2LNU1bLc", | ||
# "address": "XMgYSB_KDddaDJYMb-JpbmGnagTklwQ-msdaDLP" | ||
# } | ||
|
||
decrypted = data_masker.decrypt(data=encrypted, fields=["email", "address"]) | ||
# decrypted = { | ||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "[email protected]", | ||
# "address": { | ||
# "street": "123 Main St", | ||
# "city": "Anytown", | ||
# "state": "CA", | ||
# "zip": "12345" | ||
# }, | ||
# } | ||
|
||
encrypted = data_masker.encrypt(data=data) | ||
# encrypted = "InRoaXMgaXMgYSBzdHJpbmciHsLZGx2na-XzP_TB5Bf2LNU1bLc" | ||
|
||
decrypted = data_masker.decrypt(data=encrypted) | ||
# decrypted = { | ||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "[email protected]", | ||
# "address": { | ||
# "street": "123 Main St", | ||
# "city": "Anytown", | ||
# "state": "CA", | ||
# "zip": "12345" | ||
# }, | ||
# } | ||
|
||
masked = data_masker.mask(data=data, fields=["email", "address.street"]) | ||
# masked = { | ||
# "id": 1, | ||
# "name": "John Doe", | ||
# "age": 30, | ||
# "email": "*****", | ||
# "address": { | ||
# "street": "*****", | ||
# "city": "Anytown", | ||
# "state": "CA", | ||
# "zip": "12345" | ||
# }, | ||
# } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.