|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use aws_db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::operation::get_branch_key_id_from_ddb_key::GetBranchKeyIdFromDdbKeyInput; |
| 5 | +use aws_db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::operation::get_branch_key_id_from_ddb_key::GetBranchKeyIdFromDdbKeyOutput; |
| 6 | +use aws_db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::error::Error; |
| 7 | +use aws_db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::DynamoDbKeyBranchKeyIdSupplier; |
| 8 | + |
| 9 | +// Used in the 'HierarchicalKeyringExample'. |
| 10 | +// In that example, we have a table where we distinguish multiple tenants |
| 11 | +// by a tenant ID that is stored in our partition attribute. |
| 12 | +// The expectation is that this does not produce a confused deputy |
| 13 | +// because the tenants are separated by partition. |
| 14 | +// In order to create a Hierarchical Keyring that is capable of encrypting or |
| 15 | +// decrypting data for either tenant, we implement this interface |
| 16 | +// to map the correct branch key ID to the correct tenant ID. |
| 17 | +pub struct ExampleBranchKeyIdSupplier { |
| 18 | + branch_key_id_for_tenant1 : String, |
| 19 | + branch_key_id_for_tenant2 : String |
| 20 | +} |
| 21 | + |
| 22 | +impl ExampleBranchKeyIdSupplier { |
| 23 | + pub fn new(tenant1_id : &str, tenant2_id : &str) -> Self |
| 24 | + { |
| 25 | + Self { |
| 26 | + branch_key_id_for_tenant1 : tenant1_id.to_string(), |
| 27 | + branch_key_id_for_tenant2 : tenant2_id.to_string(), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl DynamoDbKeyBranchKeyIdSupplier for ExampleBranchKeyIdSupplier { |
| 33 | + fn get_branch_key_id_from_ddb_key( |
| 34 | + &mut self, |
| 35 | + input: GetBranchKeyIdFromDdbKeyInput, |
| 36 | + ) -> Result< |
| 37 | + GetBranchKeyIdFromDdbKeyOutput, |
| 38 | + Error, |
| 39 | + > { |
| 40 | + let key = input.ddb_key.unwrap(); |
| 41 | + |
| 42 | + if !key.contains_key("partition_key") |
| 43 | + { |
| 44 | + return Err(Error::DynamoDbEncryptionException{message : "Item invalid, does not contain expected partition key attribute.".to_string()}); |
| 45 | + } |
| 46 | + let tenant_key_id = key["partition_key"].as_s().unwrap(); |
| 47 | + |
| 48 | + if tenant_key_id == "tenant1Id" |
| 49 | + { |
| 50 | + Ok(GetBranchKeyIdFromDdbKeyOutput::builder().branch_key_id(self.branch_key_id_for_tenant1.clone()).build().unwrap()) |
| 51 | + } |
| 52 | + else if tenant_key_id == "tenant2Id" |
| 53 | + { |
| 54 | + Ok(GetBranchKeyIdFromDdbKeyOutput::builder().branch_key_id(self.branch_key_id_for_tenant2.clone()).build().unwrap()) |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + Err(Error::DynamoDbEncryptionException{message : "Item does not contain valid tenant ID.".to_string()}) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments