|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | use crate::test_utils;
|
| 5 | +use aws_sdk_dynamodb::types::AttributeValue; |
5 | 6 | use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::client as dbesdk_client;
|
6 | 7 | use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::dynamo_db_encryption_config::DynamoDbEncryptionConfig;
|
7 |
| -use std::collections::HashMap; |
8 |
| -use aws_sdk_dynamodb::types::AttributeValue; |
9 | 8 | use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::GetEncryptedDataKeyDescriptionUnion;
|
| 9 | +use std::collections::HashMap; |
| 10 | + |
| 11 | +pub async fn get_encrypted_data_key_description() { |
| 12 | + let kms_key_id = test_utils::TEST_KMS_KEY_ID; |
| 13 | + let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME; |
| 14 | + let config = DynamoDbEncryptionConfig::builder().build().unwrap(); |
| 15 | + let ddb_enc = dbesdk_client::Client::from_conf(config).unwrap(); |
| 16 | + |
| 17 | + // 1. Define keys that will be used to retrieve item from the DynamoDB table. |
| 18 | + let key_to_get = HashMap::from([ |
| 19 | + ( |
| 20 | + "partition_key".to_string(), |
| 21 | + AttributeValue::S("BasicPutGetExample".to_string()), |
| 22 | + ), |
| 23 | + ("sort_key".to_string(), AttributeValue::N("0".to_string())), |
| 24 | + ]); |
| 25 | + |
| 26 | + // 2. Create a Amazon DynamoDB Client and retrieve item from DynamoDB table |
| 27 | + let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await; |
| 28 | + let ddb = aws_sdk_dynamodb::Client::new(&sdk_config); |
| 29 | + let get_item_response = ddb |
| 30 | + .get_item() |
| 31 | + .set_key(Some(key_to_get)) |
| 32 | + .table_name(ddb_table_name) |
| 33 | + .send() |
| 34 | + .await |
| 35 | + .unwrap(); |
10 | 36 |
|
11 |
| -pub async fn get_encrypted_data_key_description() |
12 |
| - { |
13 |
| - let kms_key_id = test_utils::TEST_KMS_KEY_ID; |
14 |
| - let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME; |
15 |
| - let config = DynamoDbEncryptionConfig::builder().build().unwrap(); |
16 |
| - let ddb_enc = dbesdk_client::Client::from_conf(config).unwrap(); |
17 |
| - |
18 |
| - // 1. Define keys that will be used to retrieve item from the DynamoDB table. |
19 |
| - let key_to_get = HashMap::from([ |
20 |
| - ("partition_key".to_string(), AttributeValue::S("BasicPutGetExample".to_string())), |
21 |
| - ("sort_key".to_string(), AttributeValue::N("0".to_string())), |
22 |
| - ]); |
23 |
| - |
24 |
| - |
25 |
| - // 2. Create a Amazon DynamoDB Client and retrieve item from DynamoDB table |
26 |
| - let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await; |
27 |
| - let ddb = aws_sdk_dynamodb::Client::new(&sdk_config); |
28 |
| - let get_item_response = ddb.get_item() |
29 |
| - .set_key(Some(key_to_get)) |
30 |
| - .table_name(ddb_table_name) |
31 |
| - .send().await.unwrap(); |
32 |
| - |
33 |
| - |
34 |
| - // 3. Extract the item from the dynamoDB table and prepare input for the GetEncryptedDataKeyDescription method. |
35 |
| - // Here, we are sending dynamodb item but you can also input the header itself by extracting the header from |
36 |
| - // "aws_dbe_head" attribute in the dynamoDB item. The part of the code where we send input as the header is commented. |
37 |
| - let returned_item = get_item_response.item.unwrap(); |
38 |
| - let input_union = GetEncryptedDataKeyDescriptionUnion::Item(returned_item); |
39 |
| - let output = ddb_enc.get_encrypted_data_key_description() |
| 37 | + // 3. Extract the item from the dynamoDB table and prepare input for the GetEncryptedDataKeyDescription method. |
| 38 | + // Here, we are sending dynamodb item but you can also input the header itself by extracting the header from |
| 39 | + // "aws_dbe_head" attribute in the dynamoDB item. The part of the code where we send input as the header is commented. |
| 40 | + let returned_item = get_item_response.item.unwrap(); |
| 41 | + let input_union = GetEncryptedDataKeyDescriptionUnion::Item(returned_item); |
| 42 | + let output = ddb_enc |
| 43 | + .get_encrypted_data_key_description() |
40 | 44 | .input(input_union)
|
41 |
| - .send().await.unwrap(); |
| 45 | + .send() |
| 46 | + .await |
| 47 | + .unwrap(); |
42 | 48 |
|
43 |
| - // The code below shows how we can send header as the input to the DynamoDB. This code is written to demo the |
44 |
| - // alternative approach. So, it is commented. |
45 |
| - // let input_union = GetEncryptedDataKeyDescriptionUnion::Header(returned_item["aws_dbe_head"].as_b().unwrap().clone()); |
| 49 | + // The code below shows how we can send header as the input to the DynamoDB. This code is written to demo the |
| 50 | + // alternative approach. So, it is commented. |
| 51 | + // let input_union = GetEncryptedDataKeyDescriptionUnion::Header(returned_item["aws_dbe_head"].as_b().unwrap().clone()); |
46 | 52 |
|
47 |
| - // 4. Get encrypted DataKey Descriptions from GetEncryptedDataKeyDescription method output and assert if its true. |
48 |
| - let encrypted_data_key_descriptions = output.encrypted_data_key_description_output.unwrap(); |
49 |
| - assert_eq!(encrypted_data_key_descriptions[0].key_provider_id, Some("aws-kms".to_string())); |
50 |
| - assert_eq!(encrypted_data_key_descriptions[0].key_provider_info, Some(kms_key_id.to_string())); |
| 53 | + // 4. Get encrypted DataKey Descriptions from GetEncryptedDataKeyDescription method output and assert if its true. |
| 54 | + let encrypted_data_key_descriptions = output.encrypted_data_key_description_output.unwrap(); |
| 55 | + assert_eq!( |
| 56 | + encrypted_data_key_descriptions[0].key_provider_id, |
| 57 | + Some("aws-kms".to_string()) |
| 58 | + ); |
| 59 | + assert_eq!( |
| 60 | + encrypted_data_key_descriptions[0].key_provider_info, |
| 61 | + Some(kms_key_id.to_string()) |
| 62 | + ); |
51 | 63 |
|
52 |
| - println!("get_encrypted_data_key_description successful."); |
53 |
| - } |
| 64 | + println!("get_encrypted_data_key_description successful."); |
| 65 | +} |
0 commit comments