|
| 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 | +} |
0 commit comments