Skip to content

chore(examples): Added raw RSA/AES keyring multithreaded examples #694

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 27 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
86f4332
mplv2
lucasmcdonald3 May 20, 2024
4ce31f6
Merge branch 'mpl-reviewed' into lucmcdon/mpl-v2
lucasmcdonald3 May 20, 2024
cb80152
cleanup
lucasmcdonald3 May 20, 2024
efe2d1d
isort
lucasmcdonald3 May 20, 2024
dd285f6
win
lucasmcdonald3 May 22, 2024
bf1a15f
debug win import
lucasmcdonald3 May 22, 2024
2144b14
debug win import
lucasmcdonald3 May 22, 2024
a83ab6d
actual v2
lucasmcdonald3 May 22, 2024
3b5157d
add
lucasmcdonald3 Jun 27, 2024
1591bdf
refactoring and fixes
RitvikKapila Jun 28, 2024
e75fe70
chore: performance tests for ESDK-python (#680)
RitvikKapila Jun 5, 2024
5ea31f5
chore(performance_tests): added hierarchy keyring and caching cmm tes…
RitvikKapila Jun 7, 2024
f45e641
chore(migration examples): added KMS, raw AES and raw RSA keyring/MKP…
RitvikKapila Jun 13, 2024
dca6fdd
chore(custom_cmm_example.py): added test for custom_cmm_example.py (#…
RitvikKapila Jun 26, 2024
23084a0
chore(MPL): Update README and primary pydocs (#658)
lucasmcdonald3 Jul 30, 2024
a8fb0ed
chore: Change MPL branch, remove PYTHONPATH workarounds (#683)
lucasmcdonald3 Aug 5, 2024
42c2aea
Merge branch 'mpl-reviewed' into threaded-tests
lucasmcdonald3 Aug 6, 2024
233faa7
Update src/aws_encryption_sdk/streaming_client.py
lucasmcdonald3 Aug 6, 2024
48a67f5
cleanup
lucasmcdonald3 Aug 6, 2024
4228c92
cleanup
lucasmcdonald3 Aug 7, 2024
fa2d084
cleanup
lucasmcdonald3 Aug 7, 2024
512eb7c
cleanup
lucasmcdonald3 Aug 7, 2024
0c0b7d9
cleanup
lucasmcdonald3 Aug 7, 2024
1d063de
cleanup
lucasmcdonald3 Aug 7, 2024
56937f0
fix
lucasmcdonald3 Aug 7, 2024
7cd9713
clean
lucasmcdonald3 Aug 7, 2024
0650844
fix
lucasmcdonald3 Aug 7, 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
6 changes: 1 addition & 5 deletions .github/workflows/ci_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ jobs:
matrix:
os:
- ubuntu-latest
# Windows fails due to "No module named 'Wrappers'"
# This SHOULD be fixed once Dafny generates fully-qualified import statements
# (i.e. doo files, per-package module names)
# Disable for now
# - windows-latest
- windows-latest
- macos-12
python:
- 3.8
Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_discovery_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
For more information on how to use KMS Discovery keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html#kms-keyring-discovery
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -49,11 +48,6 @@
from aws_encryption_sdk import CommitmentPolicy
from aws_encryption_sdk.exceptions import AWSEncryptionSDKClientError

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_discovery_multi_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
For more information on how to use KMS Discovery keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html#kms-keyring-discovery
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -45,11 +44,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
For more information on how to use KMS keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -29,11 +28,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_mrk_discovery_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
For more information on how to use KMS Discovery keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html#kms-keyring-discovery
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -50,11 +49,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_mrk_discovery_multi_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
For more information on how to use KMS Discovery keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html#kms-keyring-discovery
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -52,11 +51,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_mrk_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
For more info on KMS MRK (multi-region keys), see the KMS documentation:
https://docs.aws.amazon.com/kms/latest/developerguide/multi-region-keys-overview.html
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -33,11 +32,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_mrk_multi_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
For more info on KMS MRK (multi-region keys), see the KMS documentation:
https://docs.aws.amazon.com/kms/latest/developerguide/multi-region-keys-overview.html
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -39,11 +38,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_multi_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
For more information on how to use Multi keyrings, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-multi-keyring.html
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -48,11 +47,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/aws_kms_rsa_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# For more information on how to use KMS keyrings, see
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -27,11 +26,6 @@
from aws_encryption_sdk import CommitmentPolicy
from aws_encryption_sdk.identifiers import AlgorithmSuite

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
6 changes: 0 additions & 6 deletions examples/src/file_streaming_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"""
import filecmp
import secrets
import sys

from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
Expand All @@ -36,11 +35,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)


def encrypt_and_decrypt_with_keyring(
plaintext_filename: str,
Expand Down
6 changes: 0 additions & 6 deletions examples/src/hierarchical_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
This example also requires using a KMS Key. You need the following access on this key: -
GenerateDataKeyWithoutPlaintext - Decrypt
"""
import sys

import boto3
# Ignore missing MPL for pylint, but the MPL is required for this example
Expand All @@ -55,11 +54,6 @@

from .branch_key_id_supplier_example import ExampleBranchKeyIdSupplier

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
module_root_dir = '/'.join(__file__.split("/")[:-1])

sys.path.append(module_root_dir)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
1 change: 0 additions & 1 deletion examples/src/legacy/module_.py

This file was deleted.

6 changes: 0 additions & 6 deletions examples/src/migration_set_commitment_policy_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
For more information on setting your commitment policy, see
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#commitment-policy
"""
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -32,11 +31,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
1 change: 0 additions & 1 deletion examples/src/module_.py

This file was deleted.

6 changes: 0 additions & 6 deletions examples/src/multi_keyring_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-multi-keyring.html
"""
import secrets
import sys

import boto3
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
Expand All @@ -54,11 +53,6 @@
import aws_encryption_sdk
from aws_encryption_sdk import CommitmentPolicy

# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
MODULE_ROOT_DIR = '/'.join(__file__.split("/")[:-1])

sys.path.append(MODULE_ROOT_DIR)

EXAMPLE_DATA: bytes = b"Hello World"


Expand Down
63 changes: 63 additions & 0 deletions examples/src/multithreading/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""init file for multi-threading examples."""
import time

from aws_cryptographic_materialproviders.mpl.references import IKeyring
from typing import Dict # noqa pylint: disable=wrong-import-order

import aws_encryption_sdk


def encrypt_and_decrypt_with_keyring(
plaintext_data: bytes,
keyring: IKeyring,
client: aws_encryption_sdk.EncryptionSDKClient
):
"""Demonstrate how to encrypt and decrypt plaintext data using a keyring.

Usage: encrypt_and_decrypt_with_keyring(plaintext_data, keyring, client)
:param plaintext_data: plaintext data you want to encrypt
:type: bytes
:param keyring: Keyring to use for encryption.
:type keyring: IKeyring
:param client: The Encryption SDK client to use for encryption.
:type client: aws_encryption_sdk.EncryptionSDKClient
:return: encrypted and decrypted (cycled) plaintext data
:rtype: bytes
"""
encryption_context: Dict[str, str] = {
"encryption": "context",
"is not": "secret",
"but adds": "useful metadata",
"that can help you": "be confident that",
"the data you are handling": "is what you think it is",
}

ciphertext_data, _ = client.encrypt(
source=plaintext_data,
keyring=keyring,
encryption_context=encryption_context
)

decrypted_plaintext_data, _ = client.decrypt(
source=ciphertext_data,
keyring=keyring
)

return decrypted_plaintext_data


def run_encrypt_and_decrypt_with_keyring_for_duration_seconds(
plaintext_data: bytes,
keyring: IKeyring,
client: aws_encryption_sdk.EncryptionSDKClient,
duration: int = 2
):
"""Helper function to repeatedly run an encrypt and decrypt cycle for 'duration' seconds."""
time_end = time.time() + duration

while time.time() < time_end:
decrypted_plaintext_data = encrypt_and_decrypt_with_keyring(plaintext_data, keyring, client)
assert decrypted_plaintext_data == plaintext_data, \
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
38 changes: 38 additions & 0 deletions examples/src/multithreading/raw_aes_keyring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""This file contains methods to use for testing multi-threading for Raw AES keyring."""

import secrets

from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
from aws_cryptographic_materialproviders.mpl.models import AesWrappingAlg, CreateRawAesKeyringInput
from aws_cryptographic_materialproviders.mpl.references import IKeyring


def create_keyring():
"""Demonstrate how to create a Raw AES keyring.

Usage: create_keyring()
"""
key_name_space = "Some managed raw keys"
key_name = "My 256-bit AES wrapping key"

static_key = secrets.token_bytes(32)

mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
config=MaterialProvidersConfig()
)

keyring_input: CreateRawAesKeyringInput = CreateRawAesKeyringInput(
key_namespace=key_name_space,
key_name=key_name,
wrapping_key=static_key,
wrapping_alg=AesWrappingAlg.ALG_AES256_GCM_IV12_TAG16
)

keyring: IKeyring = mat_prov.create_raw_aes_keyring(
input=keyring_input
)

return keyring
Loading
Loading