Skip to content

Commit 9dbcb4d

Browse files
committed
fix kms keyring test
1 parent 706f915 commit 9dbcb4d

File tree

4 files changed

+153
-12
lines changed

4 files changed

+153
-12
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Performance tests for the hierarchy keyring."""
4+
5+
import aws_encryption_sdk
6+
import boto3
7+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
8+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
9+
from aws_cryptographic_materialproviders.mpl.models import CreateAwsKmsKeyringInput
10+
from aws_cryptographic_materialproviders.mpl.references import IKeyring
11+
12+
from aws_cryptographic_materialproviders.keystore import KeyStore
13+
from aws_cryptographic_materialproviders.keystore.config import KeyStoreConfig
14+
from aws_cryptographic_materialproviders.keystore.models import CreateKeyInput, KMSConfigurationKmsKeyArn
15+
from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders
16+
from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig
17+
from aws_cryptographic_materialproviders.mpl.models import (
18+
CacheTypeDefault,
19+
CreateAwsKmsHierarchicalKeyringInput,
20+
DefaultCache,
21+
)
22+
from aws_cryptographic_materialproviders.mpl.references import IBranchKeyIdSupplier, IKeyring
23+
from typing import Dict # noqa pylint: disable=wrong-import-order
24+
25+
import aws_encryption_sdk
26+
from aws_encryption_sdk import CommitmentPolicy
27+
from aws_encryption_sdk.exceptions import AWSEncryptionSDKClientError
28+
29+
from .branch_key_id_supplier_example import ExampleBranchKeyIdSupplier
30+
31+
32+
def create_keyring(
33+
key_store_table_name: str,
34+
logical_key_store_name: str,
35+
kms_key_id: str
36+
):
37+
"""Demonstrate how to create a hierarchy keyring.
38+
39+
Usage: create_keyring(key_store_table_name, logical_key_store_name, kms_key_id)
40+
:param key_store_table_name: Name of the KeyStore DynamoDB table.
41+
:type key_store_table_name: string
42+
:param logical_key_store_name: Logical name of the KeyStore.
43+
:type logical_key_store_name: string
44+
:param kms_key_id: KMS Key identifier for the KMS key you want to use.
45+
:type kms_key_id: string
46+
47+
For more information on KMS Key identifiers, see
48+
https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
49+
"""
50+
# Create boto3 clients for DynamoDB and KMS.
51+
ddb_client = boto3.client('dynamodb', region_name="us-west-2")
52+
kms_client = boto3.client('kms', region_name="us-west-2")
53+
54+
# Configure your KeyStore resource.
55+
# This SHOULD be the same configuration that you used
56+
# to initially create and populate your KeyStore.
57+
keystore: KeyStore = KeyStore(
58+
config=KeyStoreConfig(
59+
ddb_client=ddb_client,
60+
ddb_table_name=key_store_table_name,
61+
logical_key_store_name=logical_key_store_name,
62+
kms_client=kms_client,
63+
kms_configuration=KMSConfigurationKmsKeyArn(
64+
value=kms_key_id
65+
),
66+
)
67+
)
68+
69+
# Call CreateKey to create two new active branch keys
70+
branch_key_id_a: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier
71+
branch_key_id_b: str = keystore.create_key(input=CreateKeyInput()).branch_key_identifier
72+
73+
# Create a branch key supplier that maps the branch key id to a more readable format
74+
branch_key_id_supplier: IBranchKeyIdSupplier = ExampleBranchKeyIdSupplier(
75+
tenant_1_id=branch_key_id_a,
76+
tenant_2_id=branch_key_id_b,
77+
)
78+
79+
# Create the Hierarchical Keyring.
80+
mat_prov: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders(
81+
config=MaterialProvidersConfig()
82+
)
83+
84+
keyring_input: CreateAwsKmsHierarchicalKeyringInput = CreateAwsKmsHierarchicalKeyringInput(
85+
key_store=keystore,
86+
branch_key_id_supplier=branch_key_id_supplier,
87+
ttl_seconds=600,
88+
cache=CacheTypeDefault(
89+
value=DefaultCache(
90+
entry_capacity=100
91+
)
92+
),
93+
)
94+
95+
keyring: IKeyring = mat_prov.create_aws_kms_hierarchical_keyring(
96+
input=keyring_input
97+
)
98+
99+
return keyring
100+
101+
102+
def encrypt_using_keyring(
103+
plaintext_data: bytes,
104+
keyring: IKeyring
105+
):
106+
"""Demonstrate how to encrypt plaintext data using an AWS KMS keyring.
107+
108+
Usage: encrypt_using_keyring(plaintext_data, keyring)
109+
:param plaintext_data: plaintext data you want to encrypt
110+
:type: bytes
111+
:param keyring: Keyring to use for encryption.
112+
:type keyring: IKeyring
113+
"""
114+
client = aws_encryption_sdk.EncryptionSDKClient()
115+
116+
ciphertext_data, _ = client.encrypt(
117+
source=plaintext_data,
118+
keyring=keyring
119+
)
120+
121+
return ciphertext_data
122+
123+
124+
def decrypt_using_keyring(
125+
ciphertext_data: bytes,
126+
keyring: IKeyring
127+
):
128+
"""Demonstrate how to decrypt ciphertext data using an AWS KMS keyring.
129+
130+
Usage: decrypt_using_keyring(ciphertext_data, keyring)
131+
:param ciphertext_data: ciphertext data you want to decrypt
132+
:type: bytes
133+
:param keyring: Keyring to use for decryption.
134+
:type keyring: IKeyring
135+
"""
136+
client = aws_encryption_sdk.EncryptionSDKClient()
137+
138+
decrypted_plaintext_data, _ = client.decrypt(
139+
source=ciphertext_data,
140+
keyring=keyring
141+
)
142+
143+
return decrypted_plaintext_data

performance_tests/src/aws_encryption_sdk_performance_tests/utils/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ def get_rsa_key_from_file(filename):
9797
@staticmethod
9898
def write_time_list_to_csv(time_list, filename):
9999
"""Writes the time list to a CSV file."""
100-
with open('results/' + filename + '.csv', 'w', encoding='utf-8') as myfile:
100+
with open(filename + '.csv', 'w', encoding='utf-8') as myfile:
101101
for time in time_list:
102102
myfile.write(str(time) + '\n')

performance_tests/test/keyrings/test_aws_kms_keyring.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create_kms_keyring():
3030
@click.option('--n_iters',
3131
default=PerfTestUtils.DEFAULT_N_ITERS)
3232
@click.option('--output_file',
33-
default='kms_keyring_create')
33+
default='/'.join(__file__.split("/")[:-3]) + '/results/kms_keyring_create')
3434
def create(
3535
kms_key_id: str,
3636
n_iters: int,
@@ -46,9 +46,6 @@ def create(
4646
# calculate elapsed time in milliseconds
4747
elapsed_time = (time.time() - curr_time) * 1000
4848
time_list.append(elapsed_time)
49-
50-
print('time_list', time_list)
51-
print('output_file', output_file)
5249
PerfTestUtils.write_time_list_to_csv(time_list, output_file)
5350

5451

@@ -63,7 +60,7 @@ def create_kms_keyring_given_kms_client():
6360
@click.option('--n_iters',
6461
default=PerfTestUtils.DEFAULT_N_ITERS)
6562
@click.option('--output_file',
66-
default='kms_keyring_create_given_kms_client')
63+
default='/'.join(__file__.split("/")[:-3]) + '/results/kms_keyring_create_given_kms_client')
6764
def create_given_kms_client(
6865
kms_key_id: str,
6966
n_iters: int,
@@ -91,14 +88,15 @@ def encrypt_kms_keyring():
9188

9289
@encrypt_kms_keyring.command()
9390
@click.option('--plaintext_data_filename',
94-
default='test/resources/plaintext/plaintext-data-' + PerfTestUtils.DEFAULT_FILE_SIZE + '.dat',
91+
default='/'.join(__file__.split("/")[:-2]) + '/resources/plaintext/plaintext-data-'
92+
+ PerfTestUtils.DEFAULT_FILE_SIZE + '.dat',
9593
prompt='Filename containing plaintext data you want to encrypt')
9694
@click.option('--kms_key_id',
9795
default='arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f')
9896
@click.option('--n_iters',
9997
default=PerfTestUtils.DEFAULT_N_ITERS)
10098
@click.option('--output_file',
101-
default='kms_keyring_encrypt')
99+
default='/'.join(__file__.split("/")[:-3]) + '/results/kms_keyring_encrypt')
102100
def encrypt(
103101
plaintext_data_filename: str,
104102
kms_key_id: str,
@@ -130,14 +128,15 @@ def decrypt_kms_keyring():
130128

131129
@decrypt_kms_keyring.command()
132130
@click.option('--ciphertext_data_filename',
133-
default='test/resources/ciphertext/kms/ciphertext-data-' + PerfTestUtils.DEFAULT_FILE_SIZE + '.ct',
131+
default='/'.join(__file__.split("/")[:-2]) + '/resources/ciphertext/kms/ciphertext-data-'
132+
+ PerfTestUtils.DEFAULT_FILE_SIZE + '.ct',
134133
prompt='Filename containing ciphertext data you want to decrypt')
135134
@click.option('--kms_key_id',
136135
default='arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f')
137136
@click.option('--n_iters',
138137
default=PerfTestUtils.DEFAULT_N_ITERS)
139138
@click.option('--output_file',
140-
default='kms_keyring_decrypt')
139+
default='/'.join(__file__.split("/")[:-3]) + '/results/kms_keyring_decrypt')
141140
def decrypt(
142141
ciphertext_data_filename: str,
143142
kms_key_id: str,
@@ -177,7 +176,6 @@ def runner():
177176
def test_create(runner):
178177
"""Test the create_keyring function"""
179178
result = runner.invoke(create_kms_keyring.commands['create'], ['--n_iters', 1])
180-
print('time_list', result.output)
181179
assert result.exit_code == 0
182180

183181

performance_tests/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ envlist =
3131
# release :: Builds dist files and uploads to pypi pypirc profile.
3232

3333
[testenv:base-command]
34-
commands = pytest -s -v test/
34+
commands = pytest test/
3535
deps =
3636
click
3737

0 commit comments

Comments
 (0)