Skip to content

Commit 25e1219

Browse files
authored
chore(python): tests for OpaqueWithText (#1656)
1 parent 34f9689 commit 25e1219

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Test keystore exception handling examples."""
4+
import pytest
5+
import uuid
6+
import boto3
7+
8+
from aws_cryptographic_material_providers.keystore.client import KeyStore
9+
from aws_cryptographic_material_providers.keystore.config import KeyStoreConfig
10+
from aws_cryptographic_material_providers.keystore.models import (
11+
CreateKeyInput,
12+
KMSConfigurationKmsKeyArn,
13+
)
14+
from aws_cryptographic_material_providers.smithygenerated.aws_cryptography_keystore.errors import (
15+
ComAmazonawsKms,
16+
ComAmazonawsDynamodb
17+
)
18+
19+
pytestmark = [pytest.mark.examples]
20+
21+
BRANCH_KEY_ID = "test_transaction_canceled_exception_branch_id"
22+
# Constants for test configuration
23+
TEST_TABLE_NAME = "KeyStoreDdbTable"
24+
TEST_KMS_KEY_ARN = "arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126"
25+
# KMS key ARN without kms:GenerateDataKeyWithoutPlaintext permission
26+
KMS_KEY_ARN_WITHOUT_PERMISSIONS = "arn:aws:kms:us-west-2:370957321024:key/da179005-1c04-4b91-a103-ee43b9a707e6"
27+
28+
29+
def test_kms_permission_exception():
30+
"""This test verifies that the smithy-python properly handles `OpaqueWithText` exception from KMS."""
31+
try:
32+
keystore: KeyStore = KeyStore(
33+
KeyStoreConfig(
34+
ddb_table_name=TEST_TABLE_NAME,
35+
kms_configuration=KMSConfigurationKmsKeyArn(KMS_KEY_ARN_WITHOUT_PERMISSIONS),
36+
logical_key_store_name=TEST_TABLE_NAME,
37+
kms_client=boto3.client("kms"),
38+
ddb_client=boto3.client("dynamodb"),
39+
)
40+
)
41+
42+
# Attempt to create a key - this should fail due to KMS permissions
43+
branch_key_id = keystore.create_key(CreateKeyInput()).branch_key_identifier
44+
pytest.fail("Expected ComAmazonawsKms exception, but no exception was raised")
45+
except ComAmazonawsKms as ex:
46+
assert "ClientError" in str(ex)
47+
48+
49+
def test_transaction_canceled_exception():
50+
"""This test verifies that attempting to create an already configured branch key ID, throws a TransactionCanceledException."""
51+
# Create AWS clients
52+
ddb_client = boto3.client('dynamodb')
53+
kms_client = boto3.client('kms')
54+
55+
# Branch Key ID is pre-configured in the table for this test
56+
branch_key_id = "test_transaction_canceled_exception_branch_id"
57+
58+
# Initialize KeyStore
59+
keystore = KeyStore(
60+
KeyStoreConfig(
61+
ddb_table_name=TEST_TABLE_NAME,
62+
kms_configuration=KMSConfigurationKmsKeyArn(TEST_KMS_KEY_ARN),
63+
logical_key_store_name=TEST_TABLE_NAME,
64+
kms_client=boto3.client("kms"),
65+
ddb_client=boto3.client("dynamodb"),
66+
)
67+
)
68+
69+
# Attempt to create the branch key again with same branch key id, which should fail with TransactionCanceledException
70+
try:
71+
create_input = CreateKeyInput(branch_key_identifier=branch_key_id, encryption_context={'Robbie': 'is a Dog'})
72+
keystore.create_key(create_input)
73+
pytest.fail("Expected TransactionCanceledException but no exception was raised")
74+
except ComAmazonawsDynamodb as e:
75+
assert "Transaction cancelled" in str(e), f"Expected TransactionCanceledException but got: {e}"
76+
except Exception as e:
77+
pytest.fail(f"Unexpected error: {e}")

0 commit comments

Comments
 (0)