|
1 |
| -# # Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
2 |
| -# # SPDX-License-Identifier: Apache-2.0 |
3 |
| -# """ |
4 |
| -# Example implementation of a branch key ID supplier. |
5 |
| -# |
6 |
| -# Used in the 'HierarchicalKeyringExample'. |
7 |
| -# In that example, we have a table where we distinguish multiple tenants |
8 |
| -# by a tenant ID that is stored in our partition attribute. |
9 |
| -# The expectation is that this does not produce a confused deputy |
10 |
| -# because the tenants are separated by partition. |
11 |
| -# In order to create a Hierarchical Keyring that is capable of encrypting or |
12 |
| -# decrypting data for either tenant, we implement this interface |
13 |
| -# to map the correct branch key ID to the correct tenant ID. |
14 |
| -# """ |
15 |
| -# from typing import Dict |
16 |
| -# |
17 |
| -# # TODO: Resolve dependency |
18 |
| -# from aws_dbesdk_dynamodb.structures import IDynamoDbKeyBranchKeyIdSupplier |
19 |
| -# from aws_dbesdk_dynamodb.model import ( |
20 |
| -# GetBranchKeyIdFromDdbKeyInput, |
21 |
| -# GetBranchKeyIdFromDdbKeyOutput, |
22 |
| -# AttributeValue |
23 |
| -# ) |
24 |
| -# # from aws_dynamodb_encryption_client.dynamodb.model import AttributeValue |
25 |
| -# |
26 |
| -# |
27 |
| -# class ExampleBranchKeyIdSupplier(IDynamoDbKeyBranchKeyIdSupplier): |
28 |
| -# """Example implementation of a branch key ID supplier.""" |
29 |
| -# |
30 |
| -# def __init__(self, tenant1_id: str, tenant2_id: str): |
31 |
| -# """Example constructor for a branch key ID supplier. |
32 |
| -# |
33 |
| -# :param tenant1_id: Branch key ID for tenant 1 |
34 |
| -# :param tenant2_id: Branch key ID for tenant 2 |
35 |
| -# """ |
36 |
| -# self.branch_key_id_for_tenant1 = tenant1_id |
37 |
| -# self.branch_key_id_for_tenant2 = tenant2_id |
38 |
| -# |
39 |
| -# def get_branch_key_id_from_ddb_key( |
40 |
| -# self, |
41 |
| -# input: GetBranchKeyIdFromDdbKeyInput |
42 |
| -# ) -> GetBranchKeyIdFromDdbKeyOutput: |
43 |
| -# """Returns branch key ID from the tenant ID in input's DDB key. |
44 |
| -# |
45 |
| -# :param input: Input containing DDB key |
46 |
| -# :return: Output containing branch key ID |
47 |
| -# :raises ValueError: If DDB key is invalid or contains invalid tenant ID |
48 |
| -# """ |
49 |
| -# key: Dict[str, AttributeValue] = input.ddb_key() |
50 |
| -# |
51 |
| -# if "partition_key" not in key: |
52 |
| -# raise ValueError( |
53 |
| -# "Item invalid, does not contain expected partition key attribute." |
54 |
| -# ) |
55 |
| -# |
56 |
| -# tenant_key_id = key["partition_key"].s() |
57 |
| -# |
58 |
| -# if tenant_key_id == "tenant1Id": |
59 |
| -# branch_key_id = self.branch_key_id_for_tenant1 |
60 |
| -# elif tenant_key_id == "tenant2Id": |
61 |
| -# branch_key_id = self.branch_key_id_for_tenant2 |
62 |
| -# else: |
63 |
| -# raise ValueError("Item does not contain valid tenant ID") |
64 |
| -# |
65 |
| -# return GetBranchKeyIdFromDdbKeyOutput( |
66 |
| -# branch_key_id=branch_key_id |
67 |
| -# ) |
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +Example implementation of a branch key ID supplier. |
| 5 | +
|
| 6 | +Used in the 'HierarchicalKeyringExample'. |
| 7 | +In that example, we have a table where we distinguish multiple tenants |
| 8 | +by a tenant ID that is stored in our partition attribute. |
| 9 | +The expectation is that this does not produce a confused deputy |
| 10 | +because the tenants are separated by partition. |
| 11 | +In order to create a Hierarchical Keyring that is capable of encrypting or |
| 12 | +decrypting data for either tenant, we implement this interface |
| 13 | +to map the correct branch key ID to the correct tenant ID. |
| 14 | +""" |
| 15 | +from typing import Dict, override |
| 16 | + |
| 17 | +from boto3.dynamodb.conditions import Attr |
| 18 | +from aws_dbesdk_dynamodb.smithygenerated.aws_cryptography_dbencryptionsdk_dynamodb.references import IDynamoDbKeyBranchKeyIdSupplier |
| 19 | +# TODO: Resolve dependency |
| 20 | +from aws_dbesdk_dynamodb.structures.dynamodb import ( |
| 21 | + GetBranchKeyIdFromDdbKeyInput, |
| 22 | + GetBranchKeyIdFromDdbKeyOutput |
| 23 | +) |
| 24 | + |
| 25 | +class ExampleBranchKeyIdSupplier(IDynamoDbKeyBranchKeyIdSupplier): |
| 26 | + """Example implementation of a branch key ID supplier.""" |
| 27 | + |
| 28 | + def __init__(self, tenant1_id: str, tenant2_id: str): |
| 29 | + """Example constructor for a branch key ID supplier. |
| 30 | +
|
| 31 | + :param tenant1_id: Branch key ID for tenant 1 |
| 32 | + :param tenant2_id: Branch key ID for tenant 2 |
| 33 | + """ |
| 34 | + self.branch_key_id_for_tenant1 = tenant1_id |
| 35 | + self.branch_key_id_for_tenant2 = tenant2_id |
| 36 | + |
| 37 | + def get_branch_key_id_from_ddb_key( |
| 38 | + self, |
| 39 | + input: GetBranchKeyIdFromDdbKeyInput |
| 40 | + ) -> GetBranchKeyIdFromDdbKeyOutput: |
| 41 | + """Returns branch key ID from the tenant ID in input's DDB key. |
| 42 | +
|
| 43 | + :param input: Input containing DDB key |
| 44 | + :return: Output containing branch key ID |
| 45 | + :raises ValueError: If DDB key is invalid or contains invalid tenant ID |
| 46 | + """ |
| 47 | + key: Dict[str, Attr] = input.ddb_key() |
| 48 | + |
| 49 | + if "partition_key" not in key: |
| 50 | + raise ValueError( |
| 51 | + "Item invalid, does not contain expected partition key attribute." |
| 52 | + ) |
| 53 | + |
| 54 | + tenant_key_id = key["partition_key"].s() |
| 55 | + |
| 56 | + if tenant_key_id == "tenant1Id": |
| 57 | + branch_key_id = self.branch_key_id_for_tenant1 |
| 58 | + elif tenant_key_id == "tenant2Id": |
| 59 | + branch_key_id = self.branch_key_id_for_tenant2 |
| 60 | + else: |
| 61 | + raise ValueError("Item does not contain valid tenant ID") |
| 62 | + |
| 63 | + return GetBranchKeyIdFromDdbKeyOutput( |
| 64 | + branch_key_id=branch_key_id |
| 65 | + ) |
0 commit comments