main/keyring/
aws_kms_keyring_example.rs1use aws_esdk::client as esdk_client;
25use aws_esdk::material_providers::client as mpl_client;
26use aws_esdk::material_providers::types::material_providers_config::MaterialProvidersConfig;
27use aws_esdk::types::aws_encryption_sdk_config::AwsEncryptionSdkConfig;
28use std::collections::HashMap;
29
30pub async fn encrypt_and_decrypt_with_keyring(
31 example_data: &str,
32 kms_key_id: &str,
33) -> Result<(), crate::BoxError> {
34 let esdk_config = AwsEncryptionSdkConfig::builder().build()?;
40 let esdk_client = esdk_client::Client::from_conf(esdk_config)?;
41
42 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
44 let kms_client = aws_sdk_kms::Client::new(&sdk_config);
45
46 let encryption_context = HashMap::from([
51 ("encryption".to_string(), "context".to_string()),
52 ("is not".to_string(), "secret".to_string()),
53 ("but adds".to_string(), "useful metadata".to_string()),
54 (
55 "that can help you".to_string(),
56 "be confident that".to_string(),
57 ),
58 (
59 "the data you are handling".to_string(),
60 "is what you think it is".to_string(),
61 ),
62 ]);
63
64 let mpl_config = MaterialProvidersConfig::builder().build()?;
66 let mpl = mpl_client::Client::from_conf(mpl_config)?;
67
68 let kms_keyring = mpl
69 .create_aws_kms_keyring()
70 .kms_key_id(kms_key_id)
71 .kms_client(kms_client)
72 .send()
73 .await?;
74
75 let plaintext = example_data.as_bytes();
77
78 let encryption_response = esdk_client
79 .encrypt()
80 .plaintext(plaintext)
81 .keyring(kms_keyring.clone())
82 .encryption_context(encryption_context.clone())
83 .send()
84 .await?;
85
86 let ciphertext = encryption_response
87 .ciphertext
88 .expect("Unable to unwrap ciphertext from encryption response");
89
90 assert_ne!(
93 ciphertext,
94 aws_smithy_types::Blob::new(plaintext),
95 "Ciphertext and plaintext data are the same. Invalid encryption"
96 );
97
98 let decryption_response = esdk_client
100 .decrypt()
101 .ciphertext(ciphertext)
102 .keyring(kms_keyring)
103 .encryption_context(encryption_context)
105 .send()
106 .await?;
107
108 let decrypted_plaintext = decryption_response
109 .plaintext
110 .expect("Unable to unwrap plaintext from decryption response");
111
112 assert_eq!(
115 decrypted_plaintext,
116 aws_smithy_types::Blob::new(plaintext),
117 "Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
118 );
119
120 println!("KMS Keyring Example Completed Successfully");
121
122 Ok(())
123}
124
125#[tokio::test(flavor = "multi_thread")]
126pub async fn test_encrypt_and_decrypt_with_keyring() -> Result<(), crate::BoxError2> {
127 use crate::example_utils::utils;
129
130 encrypt_and_decrypt_with_keyring(utils::TEST_EXAMPLE_DATA, utils::TEST_DEFAULT_KMS_KEY_ID)
131 .await?;
132
133 Ok(())
134}
135
136#[tokio::test(flavor = "multi_thread")]
137pub async fn test_encrypt_and_decrypt_with_keyring_async() -> Result<(), crate::BoxError2> {
138 use crate::example_utils::utils;
140
141 let handle = tokio::spawn(async move {
142 encrypt_and_decrypt_with_keyring(utils::TEST_EXAMPLE_DATA, utils::TEST_DEFAULT_KMS_KEY_ID)
143 .await
144 });
145
146 assert!(handle.await.unwrap().is_ok());
147 Ok(())
148}