Skip to content

Commit f0cc727

Browse files
committed
Added using multiple keys section
1 parent 4156b3d commit f0cc727

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

docs/utilities/data_masking.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,19 @@ For compatibility or performance, you can optionally pass your own JSON serializ
417417

418418
You can modify the following values when initializing the `AWSEncryptionSDKProvider` to best accommodate your security and performance thresholds.
419419

420-
=== "aws_encryption_provider_example.py"
421-
422-
```python hl_lines="14-19"
423-
--8<-- "examples/data_masking/src/aws_encryption_provider_example.py"
424-
```
425-
426420
| Parameter | Default | Description |
427421
| -------------------------- | --------------------- | --------------------------------------------------------------------------------------------- |
428422
| **local_cache_capacity** | `100` | The maximum number of entries that can be retained in the local cryptographic materials cache |
429423
| **max_cache_age_seconds** | `300` | The maximum time (in seconds) that a cache entry may be kept in the cache |
430424
| **max_messages_encrypted** | `4294967296` | The maximum number of messages that may be encrypted under a cache entry |
431425
| **max_bytes_encrypted** | `9223372036854775807` | The maximum number of bytes that may be encrypted under a cache entry |
432426

427+
=== "aws_encryption_provider_example.py"
428+
429+
```python hl_lines="14-19"
430+
--8<-- "examples/data_masking/src/aws_encryption_provider_example.py"
431+
```
432+
433433
**Passing additional SDK arguments**
434434

435435
You can pass additional arguments to the `AWSEncryptionSDKProvider` via the `provider_options` parameter. To learn more about the different arguments you can give to the SDK, see the [EncryptionSDKClient's documentation](https://aws-encryption-sdk-python.readthedocs.io/en/latest/generated/aws_encryption_sdk.html#aws_encryption_sdk.EncryptionSDKClient.encrypt){target="_blank"}.
@@ -438,10 +438,20 @@ For example, the AWS Encryption SDK defaults to using the `AES_256_GCM_HKDF_SHA5
438438

439439
=== "changing_default_algorithm.py"
440440

441-
```python hl_lines="5 26"
441+
```python hl_lines="5 26 30"
442442
--8<-- "examples/data_masking/src/changing_default_algorithm.py"
443443
```
444444

445+
**Using multiple keys**
446+
447+
The `AWSEncryptionSDKProvider` allows you to instantiate it with several KMS keys by passing them all in a `list` to the `keys` parameter. This could be beneficial if you own keys in different regions, enabling you to perform cross-regional encryption and decryption.
448+
449+
=== "using_multiple_keys.py"
450+
451+
```python hl_lines="15"
452+
--8<-- "examples/data_masking/src/using_multiple_keys.py"
453+
```
454+
445455
### Data masking request flow
446456

447457
The following sequence diagrams explain how `DataMasking` behaves under different scenarios.

examples/data_masking/src/changing_default_algorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def lambda_handler(event: dict, context: LambdaContext) -> str:
2525

2626
provider_options = {"algorithm": Algorithm.AES_256_GCM_HKDF_SHA512_COMMIT_KEY}
2727

28-
decrypted = data_masker.encrypt(
28+
encrypted = data_masker.encrypt(
2929
data,
3030
provider_options=provider_options,
3131
)
3232

33-
return decrypted
33+
return encrypted
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from _future_ import annotations
2+
3+
import os
4+
5+
from aws_lambda_powertools import Logger
6+
from aws_lambda_powertools.utilities.data_masking import DataMasking
7+
from aws_lambda_powertools.utilities.data_masking.provider.kms.aws_encryption_sdk import (
8+
AWSEncryptionSDKProvider,
9+
)
10+
from aws_lambda_powertools.utilities.typing import LambdaContext
11+
12+
KMS_KEY_ARN_1 = os.getenv("KMS_KEY_ARN_1", "")
13+
KMS_KEY_ARN_2 = os.getenv("KMS_KEY_ARN_2", "")
14+
15+
encryption_provider = AWSEncryptionSDKProvider(keys=[KMS_KEY_ARN_1, KMS_KEY_ARN_2])
16+
data_masker = DataMasking(provider=encryption_provider)
17+
18+
logger = Logger()
19+
20+
21+
@logger.inject_lambda_context
22+
def lambda_handler(event: dict, context: LambdaContext) -> dict:
23+
data: dict = event.get("body", {})
24+
25+
logger.info("Encrypting the whole object")
26+
27+
encrypted = data_masker.encrypt(data)
28+
29+
return {"body": encrypted}

0 commit comments

Comments
 (0)