Skip to content

Commit 7933a2c

Browse files
committed
m
1 parent 7ab9b08 commit 7933a2c

File tree

10 files changed

+191
-51
lines changed

10 files changed

+191
-51
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
use std::collections::HashMap;
95
use crate::test_utils;
106
use aws_sdk_dynamodb::types::AttributeValue;
117

12-
use db_esdk::deps::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
13-
use db_esdk::deps::aws_cryptography_materialProviders::client;
14-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
8+
use db_esdk::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
9+
use db_esdk::aws_cryptography_materialProviders::client;
10+
use db_esdk::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
1511

16-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::DynamoDbTableEncryptionConfig;
17-
use db_esdk::deps::aws_cryptography_materialProviders::types::DbeAlgorithmSuiteId;
12+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::DynamoDbTableEncryptionConfig;
13+
use db_esdk::aws_cryptography_materialProviders::types::DbeAlgorithmSuiteId;
1814
use db_esdk::intercept::DbEsdkInterceptor;
1915
use db_esdk::types::dynamo_db_tables_encryption_config::DynamoDbTablesEncryptionConfig;
2016

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::test_utils;
5+
use db_esdk::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
6+
use db_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
7+
use db_esdk::aws_cryptography_keyStore::client as keystore_client;
8+
9+
/*
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::test_utils;
5+
use db_esdk::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
6+
use db_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
7+
use db_esdk::aws_cryptography_keyStore::client as keystore_client;
8+
9+
/*
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+
This example demonstrates configuring a KeyStore and then
15+
using a helper method to create the DDB table that will be
16+
used to persist branch keys and beacons keys for this KeyStore.
17+
18+
This table creation should occur within your control plane. This
19+
only needs to occur once. While not demonstrated in this example,
20+
you should additionally use the `VersionKey` API on the KeyStore
21+
to periodically rotate your branch key material.
22+
*/
23+
24+
pub async fn keystore_create_table() -> String
25+
{
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+
// `ddbTableName` is the name you want for the DDB table that
32+
// will back your keystore.
33+
// `kmsKeyArn` is the KMS Key that will protect your branch keys and beacon keys
34+
// when they are stored in your DDB table.
35+
let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
36+
let key_store_config = KeyStoreConfig::builder()
37+
.kms_client(aws_sdk_kms::Client::new(&sdk_config))
38+
.ddb_client(aws_sdk_dynamodb::Client::new(&sdk_config))
39+
.ddb_table_name(key_store_table_name)
40+
.logical_key_store_name(logical_key_store_name)
41+
.kms_configuration(KmsConfiguration::KmsKeyArn(kms_key_arn.to_string()))
42+
.build()
43+
.unwrap();
44+
45+
let keystore = keystore_client::Client::from_conf(key_store_config).unwrap();
46+
47+
// 2. Create the DynamoDb table that will store the branch keys and beacon keys.
48+
// This checks if the correct table already exists at `ddbTableName`
49+
// by using the DescribeTable API. If no table exists,
50+
// it will create one. If a table exists, it will verify
51+
// the table's configuration and will error if the configuration is incorrect.
52+
keystore.create_keystore.send().await.unwrap();
53+
54+
// It may take a couple minutes for the table to become ACTIVE,
55+
// at which point it is ready to store branch and beacon keys.
56+
// See the create_keystore_key example for how to populate
57+
// this table.
58+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use crate::test_utils;
5+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::client as dbesdk_client;
6+
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+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::GetEncryptedDataKeyDescriptionUnion;
10+
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()
40+
.input(input_union)
41+
.send().await.unwrap();
42+
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());
46+
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()));
51+
52+
println!("get_encrypted_data_key_description successful.");
53+
}

DynamoDbEncryption/runtimes/rust/src/bin/example/itemencryptor/item_encrypt_decrypt.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
use std::collections::HashMap;
95
use crate::test_utils;
106
use aws_sdk_dynamodb::types::AttributeValue;
117

12-
use db_esdk::deps::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
13-
use db_esdk::deps::aws_cryptography_materialProviders::client as mpl_client;
14-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
8+
use db_esdk::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
9+
use db_esdk::aws_cryptography_materialProviders::client as mpl_client;
10+
use db_esdk::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
1511

16-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb_itemEncryptor::types::dynamo_db_item_encryptor_config::DynamoDbItemEncryptorConfig;
17-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb_itemEncryptor::client as enc_client;
18-
use db_esdk::deps::aws_cryptography_materialProviders::types::DbeAlgorithmSuiteId;
12+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb_itemEncryptor::types::dynamo_db_item_encryptor_config::DynamoDbItemEncryptorConfig;
13+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb_itemEncryptor::client as enc_client;
14+
use db_esdk::aws_cryptography_materialProviders::types::DbeAlgorithmSuiteId;
1915

2016
/*
2117
This example sets up a DynamoDb Item Encryptor and uses
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
pub mod item_encrypt_decrypt;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ pub mod test_utils;
1010
pub mod itemencryptor;
1111
pub mod searchableencryption;
1212
pub mod create_keystore_key;
13+
pub mod get_encrypted_data_key_description;
1314

1415
#[tokio::main]
1516
pub async fn main() {
1617
basic_get_put_example::put_item_get_item().await;
1718
itemencryptor::item_encrypt_decrypt::encrypt_decrypt().await;
19+
get_encrypted_data_key_description::get_encrypted_data_key_description().await;
1820

1921
let key_id = create_keystore_key::keystore_create_key().await;
2022
// let key_id2 = create_keystore_key::keystore_create_key().await;
@@ -23,10 +25,9 @@ pub async fn main() {
2325
std::thread::sleep(std::time::Duration::from_secs(5));
2426

2527
searchableencryption::basic_searchable_encryption::put_and_query_with_beacon(&key_id).await;
26-
/*
28+
// FIXME : ScanError will have to wait until we have a reasonable error message strategy
2729

28-
await ScanErrorExample.ScanError();
29-
await GetEncryptedDataKeyDescriptionExample.GetEncryptedDataKeyDescription();
30+
/*
3031
await MultiPutGetExample.MultiPutGet();
3132
await ClientSupplierExample.ClientSupplierPutItemGetItem();
3233
await MultiMrkKeyringExample.MultiMrkKeyringGetItemPutItem();
@@ -42,6 +43,6 @@ pub async fn main() {
4243
await VirtualBeaconSearchableEncryptionExample.PutItemQueryItemWithVirtualBeacon(keyId);
4344
await BeaconStylesSearchableEncryptionExample.PutItemQueryItemWithBeaconStyles(keyId);
4445
await ComplexSearchableEncryptionExample.RunExample(keyId);
45-
Console.Write("All examples completed successfully.\n");
4646
*/
47+
println!("All examples completed successfully.\n");
4748
}

DynamoDbEncryption/runtimes/rust/src/bin/example/searchableencryption/basic_searchable_encryption.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
use std::collections::HashMap;
95
use crate::test_utils;
106
use aws_sdk_dynamodb::types::AttributeValue;
117

12-
use db_esdk::deps::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
13-
use db_esdk::deps::aws_cryptography_materialProviders::client;
14-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
8+
use db_esdk::aws_cryptography_materialProviders::types::material_providers_config::MaterialProvidersConfig;
9+
use db_esdk::aws_cryptography_materialProviders::client;
10+
use db_esdk::aws_cryptography_dbEncryptionSdk_structuredEncryption::types::CryptoAction;
1511

16-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::DynamoDbTableEncryptionConfig;
17-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::StandardBeacon;
12+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::DynamoDbTableEncryptionConfig;
13+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::StandardBeacon;
1814
use db_esdk::intercept::DbEsdkInterceptor;
1915
use db_esdk::types::dynamo_db_tables_encryption_config::DynamoDbTablesEncryptionConfig;
20-
use db_esdk::deps::aws_cryptography_keyStore::client as keystore_client;
21-
use db_esdk::deps::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
22-
use db_esdk::deps::aws_cryptography_keyStore::types::KmsConfiguration;
23-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::BeaconVersion;
24-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::SingleKeyStore;
25-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::BeaconKeySource;
26-
use db_esdk::deps::aws_cryptography_dbEncryptionSdk_dynamoDb::types::SearchConfig;
16+
use db_esdk::aws_cryptography_keyStore::client as keystore_client;
17+
use db_esdk::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
18+
use db_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
19+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::BeaconVersion;
20+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::SingleKeyStore;
21+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::BeaconKeySource;
22+
use db_esdk::aws_cryptography_dbEncryptionSdk_dynamoDb::types::SearchConfig;
2723

2824
/*
2925
This example demonstrates how to set up a beacon on an encrypted attribute,
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
pub mod basic_searchable_encryption;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#![deny(warnings, unconditional_panic)]
5-
#![deny(nonstandard_style)]
6-
#![deny(clippy::all)]
7-
84
pub const TEST_KEYSTORE_NAME : &str = "KeyStoreDdbTable";
95
pub const TEST_LOGICAL_KEYSTORE_NAME : &str = "KeyStoreDdbTable";
106

0 commit comments

Comments
 (0)