Skip to content

Commit 8784720

Browse files
committed
m
1 parent a7c313f commit 8784720

File tree

14 files changed

+1180
-981
lines changed

14 files changed

+1180
-981
lines changed

DynamoDbEncryption/runtimes/rust/src/bin/example/basic_get_put_example.rs

Lines changed: 155 additions & 135 deletions
Large diffs are not rendered by default.

DynamoDbEncryption/runtimes/rust/src/bin/example/create_keystore_key.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,50 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::test_utils;
5+
use db_esdk::aws_cryptography_keyStore::client as keystore_client;
56
use db_esdk::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
67
use db_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
7-
use db_esdk::aws_cryptography_keyStore::client as keystore_client;
88

99
/*
10-
The Hierarchical Keyring Example and Searchable Encryption Examples
11-
rely on the existence of a DDB-backed key store with pre-existing
12-
branch key material or beacon key material.
13-
14-
See the "Create KeyStore Table Example" for how to first set up
15-
the DDB Table that will back this KeyStore.
16-
17-
This example demonstrates configuring a KeyStore and then
18-
using a helper method to create a branch key and beacon key
19-
that share the same Id, then return that Id.
20-
We will always create a new beacon key alongside a new branch key,
21-
even if you are not using searchable encryption.
22-
23-
This key creation should occur within your control plane.
24-
*/
25-
pub async fn keystore_create_key() -> String
26-
{
27-
let key_store_table_name = test_utils::TEST_KEYSTORE_NAME;
28-
let logical_key_store_name = test_utils::TEST_LOGICAL_KEYSTORE_NAME;
29-
let kms_key_arn = test_utils::TEST_KEYSTORE_KMS_KEY_ID;
30-
31-
// 1. Configure your KeyStore resource.
32-
// This SHOULD be the same configuration that was used to create the DDB table
33-
// in the "Create KeyStore Table Example".
34-
let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
35-
let key_store_config = KeyStoreConfig::builder()
36-
.kms_client(aws_sdk_kms::Client::new(&sdk_config))
37-
.ddb_client(aws_sdk_dynamodb::Client::new(&sdk_config))
38-
.ddb_table_name(key_store_table_name)
39-
.logical_key_store_name(logical_key_store_name)
40-
.kms_configuration(KmsConfiguration::KmsKeyArn(kms_key_arn.to_string()))
41-
.build()
42-
.unwrap();
43-
44-
let keystore = keystore_client::Client::from_conf(key_store_config).unwrap();
45-
46-
// 2. Create a new branch key and beacon key in our KeyStore.
47-
// Both the branch key and the beacon key will share an Id.
48-
// This creation is eventually consistent.
49-
50-
let new_key = keystore.create_key().send().await.unwrap();
51-
return new_key.branch_key_identifier.unwrap();
52-
}
10+
The Hierarchical Keyring Example and Searchable Encryption Examples
11+
rely on the existence of a DDB-backed key store with pre-existing
12+
branch key material or beacon key material.
13+
14+
See the "Create KeyStore Table Example" for how to first set up
15+
the DDB Table that will back this KeyStore.
16+
17+
This example demonstrates configuring a KeyStore and then
18+
using a helper method to create a branch key and beacon key
19+
that share the same Id, then return that Id.
20+
We will always create a new beacon key alongside a new branch key,
21+
even if you are not using searchable encryption.
22+
23+
This key creation should occur within your control plane.
24+
*/
25+
pub async fn keystore_create_key() -> String {
26+
let key_store_table_name = test_utils::TEST_KEYSTORE_NAME;
27+
let logical_key_store_name = test_utils::TEST_LOGICAL_KEYSTORE_NAME;
28+
let kms_key_arn = test_utils::TEST_KEYSTORE_KMS_KEY_ID;
29+
30+
// 1. Configure your KeyStore resource.
31+
// This SHOULD be the same configuration that was used to create the DDB table
32+
// in the "Create KeyStore Table Example".
33+
let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
34+
let key_store_config = KeyStoreConfig::builder()
35+
.kms_client(aws_sdk_kms::Client::new(&sdk_config))
36+
.ddb_client(aws_sdk_dynamodb::Client::new(&sdk_config))
37+
.ddb_table_name(key_store_table_name)
38+
.logical_key_store_name(logical_key_store_name)
39+
.kms_configuration(KmsConfiguration::KmsKeyArn(kms_key_arn.to_string()))
40+
.build()
41+
.unwrap();
42+
43+
let keystore = keystore_client::Client::from_conf(key_store_config).unwrap();
44+
45+
// 2. Create a new branch key and beacon key in our KeyStore.
46+
// Both the branch key and the beacon key will share an Id.
47+
// This creation is eventually consistent.
48+
49+
let new_key = keystore.create_key().send().await.unwrap();
50+
new_key.branch_key_identifier.unwrap()
51+
}

DynamoDbEncryption/runtimes/rust/src/bin/example/get_encrypted_data_key_description.rs

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,64 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::test_utils;
5+
use aws_sdk_dynamodb::types::AttributeValue;
56
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::client as dbesdk_client;
67
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;
98
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();
1036

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()
4044
.input(input_union)
41-
.send().await.unwrap();
45+
.send()
46+
.await
47+
.unwrap();
4248

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());
4652

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+
);
5163

52-
println!("get_encrypted_data_key_description successful.");
53-
}
64+
println!("get_encrypted_data_key_description successful.");
65+
}

0 commit comments

Comments
 (0)