Skip to content

Commit d03a340

Browse files
authored
chore(examples): add version branch key id example (#710)
1 parent 689624d commit d03a340

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

AwsEncryptionSDK/runtimes/rust/examples/keyring/aws_kms_hierarchical/create_branch_key_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use aws_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
1111
branch key material.
1212
1313
This example demonstrates configuring a KeyStore and then
14-
using a helper method to create a branch key.
14+
uses a helper method to create a branch key.
1515
*/
1616
pub async fn create_branch_key_id(
1717
key_store_table_name: &str,

AwsEncryptionSDK/runtimes/rust/examples/keyring/aws_kms_hierarchical/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
pub mod aws_kms_hierarchical_keyring_example;
55
pub mod create_branch_key_id;
6+
pub mod version_branch_key_id_example;
67
pub mod example_branch_key_id_supplier;
78
pub mod shared_cache_across_hierarchical_keyrings_example;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use aws_esdk::aws_cryptography_keyStore::client as keystore_client;
5+
use aws_esdk::aws_cryptography_keyStore::types::key_store_config::KeyStoreConfig;
6+
use aws_esdk::aws_cryptography_keyStore::types::KmsConfiguration;
7+
8+
/*
9+
This example demonstrates configuring a KeyStore and then
10+
uses a helper method to version a branch key.
11+
*/
12+
pub async fn version_branch_key_id(
13+
key_store_table_name: &str,
14+
logical_key_store_name: &str,
15+
kms_key_arn: &str,
16+
branch_key_id: &str
17+
) -> Result<(), crate::BoxError> {
18+
// Create a Key Store
19+
// The KMS Configuration you use in the KeyStore MUST have the right access to the resources in the KeyStore.
20+
let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
21+
let key_store_config = KeyStoreConfig::builder()
22+
.kms_client(aws_sdk_kms::Client::new(&sdk_config))
23+
.ddb_client(aws_sdk_dynamodb::Client::new(&sdk_config))
24+
.ddb_table_name(key_store_table_name)
25+
.logical_key_store_name(logical_key_store_name)
26+
.kms_configuration(KmsConfiguration::KmsKeyArn(kms_key_arn.to_string()))
27+
.build()?;
28+
29+
let keystore = keystore_client::Client::from_conf(key_store_config)?;
30+
31+
// To version a branch key you MUST have access to kms:ReEncrypt* and kms:GenerateDataKeyWithoutPlaintext
32+
keystore.version_key()
33+
.branch_key_identifier(branch_key_id)
34+
.send()
35+
.await?;
36+
37+
println!("Version Branch Key Example Completed Successfully");
38+
39+
Ok(())
40+
}
41+
42+
// Function to test version_branch_key_id in main.rs in examples directory
43+
pub async fn create_and_version_branch_key_id() -> Result<(), crate::BoxError2> {
44+
use crate::example_utils::utils;
45+
use super::create_branch_key_id::create_branch_key_id;
46+
47+
let branch_key_id: String = create_branch_key_id(
48+
utils::TEST_KEY_STORE_NAME,
49+
utils::TEST_LOGICAL_KEY_STORE_NAME,
50+
utils::TEST_KEY_STORE_KMS_KEY_ID
51+
).await?;
52+
53+
version_branch_key_id(
54+
utils::TEST_KEY_STORE_NAME,
55+
utils::TEST_LOGICAL_KEY_STORE_NAME,
56+
utils::TEST_KEY_STORE_KMS_KEY_ID,
57+
&branch_key_id
58+
).await?;
59+
60+
Ok(())
61+
}
62+
63+
#[tokio::test(flavor = "multi_thread")]
64+
pub async fn test_version_branch_key_id() -> Result<(), crate::BoxError2> {
65+
// Test function for Version Branch Key example
66+
create_and_version_branch_key_id().await?;
67+
Ok(())
68+
}

AwsEncryptionSDK/runtimes/rust/examples/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ pub async fn main() -> Result<(), BoxError2> {
188188
utils::TEST_KEY_STORE_KMS_KEY_ID
189189
).await?;
190190

191+
keyring::aws_kms_hierarchical::version_branch_key_id_example::create_and_version_branch_key_id().await?;
192+
191193
keyring::aws_kms_hierarchical::shared_cache_across_hierarchical_keyrings_example::encrypt_and_decrypt_with_keyring(
192194
utils::TEST_EXAMPLE_DATA,
193195
utils::TEST_KEY_STORE_NAME,

0 commit comments

Comments
 (0)