main/keyring/multi_mrk_keyring.rs
1// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::test_utils;
5use aws_db_esdk::dynamodb::types::DynamoDbTableEncryptionConfig;
6use aws_db_esdk::intercept::DbEsdkInterceptor;
7use aws_db_esdk::material_providers::client as mpl_client;
8use aws_db_esdk::material_providers::types::material_providers_config::MaterialProvidersConfig;
9use aws_db_esdk::CryptoAction;
10use aws_db_esdk::DynamoDbTablesEncryptionConfig;
11use aws_sdk_dynamodb::types::AttributeValue;
12use std::collections::HashMap;
13
14/*
15 This example sets up DynamoDb Encryption for the AWS SDK client
16 using the MRK multi-keyring. This keyring takes in multiple AWS KMS
17 MRKs (multi-region keys) or regular AWS KMS keys (single-region keys)
18 and uses them to encrypt and decrypt data. Data encrypted using an MRK
19 multi-keyring can be decrypted using any of its component keys. If a component
20 key is an MRK with a replica in a second region, the replica key can also be
21 used to decrypt data.
22
23 For more information on MRKs, see
24 https://docs.aws.amazon.com/kms/latest/developerguide/multi-region-keys-overview.html
25
26 For more information on multi-keyrings, see
27 https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-multi-keyring.html
28
29 This example creates a new MRK multi-keyring consisting of one MRK
30 (labeled as the "generator keyring") and one single-region key (labeled
31 as the only "child keyring"). The MRK also has a replica in a second region.
32
33 This example encrypts a test item using the MRK multi-keyring and puts the
34 encrypted item to the provided DynamoDb table. Then, it gets the item
35 from the table and decrypts it using three different configs:
36 1. The MRK multi-keyring, where the MRK key is used to decrypt
37 2. Another MRK multi-keyring, where the replica MRK key is used to decrypt
38 3. Another MRK multi-keyring, where the single-region key that was present
39 in the original MRK multi-keyring is used to decrypt
40
41 Running this example requires access to the DDB Table whose name
42 is provided in CLI arguments.
43 This table must be configured with the following
44 primary key configuration:
45 - Partition key is named "partition_key" with type (S)
46 - Sort key is named "sort_key" with type (S)
47
48 This example demonstrates multi-region use cases. As a result,
49 it requires that you have a default region set in your AWS client.
50 You can set a default region through the AWS CLI with
51 `aws configure set region [region-name]`
52 e.g.
53 `aws configure set region us-west-2`
54 For more information on using AWS CLI to set config, see
55 https://awscli.amazonaws.com/v2/documentation/api/latest/reference/configure/set.html
56*/
57pub async fn put_item_get_item() -> Result<(), crate::BoxError> {
58 let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME;
59 let mrk_key_arn = test_utils::TEST_MRK_KEY_ID;
60 let key_arn = test_utils::TEST_KMS_KEY_ID;
61 let mrk_replica_key_arn = test_utils::TEST_MRK_REPLICA_KEY_ID_US_EAST_1;
62
63 // 1. Create a single MRK multi-keyring using the MRK arn and the single-region key arn.
64 let mpl_config = MaterialProvidersConfig::builder().build()?;
65 let mpl = mpl_client::Client::from_conf(mpl_config)?;
66 // Create the multi-keyring, using the MRK as the generator key,
67 // and the single-region key as a child key.
68 // Note that the generator key will generate and encrypt a plaintext data key
69 // and all child keys will only encrypt that same plaintext data key.
70 // As such, you must have permission to call KMS:GenerateDataKey on your generator key
71 // and permission to call KMS:Encrypt on all child keys.
72 // For more information, see the AWS docs on multi-keyrings above.
73 let aws_kms_mrk_multi_keyring = mpl
74 .create_aws_kms_mrk_multi_keyring()
75 .generator(mrk_key_arn)
76 .kms_key_ids(vec![key_arn.to_string()])
77 .send()
78 .await?;
79
80 // 2. Configure which attributes are encrypted and/or signed when writing new items.
81 // For each attribute that may exist on the items we plan to write to our DynamoDbTable,
82 // we must explicitly configure how they should be treated during item encryption:
83 // - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
84 // - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
85 // - DO_NOTHING: The attribute is not encrypted and not included in the signature
86 let attribute_actions_on_encrypt = HashMap::from([
87 ("partition_key".to_string(), CryptoAction::SignOnly), // Our partition attribute must be SIGN_ONLY
88 ("sort_key".to_string(), CryptoAction::SignOnly), // Our sort attribute must be SIGN_ONLY
89 ("sensitive_data".to_string(), CryptoAction::EncryptAndSign),
90 ]);
91
92 // 3. Configure which attributes we expect to be included in the signature
93 // when reading items. There are two options for configuring this:
94 //
95 // - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
96 // When defining your DynamoDb schema and deciding on attribute names,
97 // choose a distinguishing prefix (such as ":") for all attributes that
98 // you do not want to include in the signature.
99 // This has two main benefits:
100 // - It is easier to reason about the security and authenticity of data within your item
101 // when all unauthenticated data is easily distinguishable by their attribute name.
102 // - If you need to add new unauthenticated attributes in the future,
103 // you can easily make the corresponding update to your `attributeActionsOnEncrypt`
104 // and immediately start writing to that new attribute, without
105 // any other configuration update needed.
106 // Once you configure this field, it is not safe to update it.
107 //
108 // - Configure `allowedUnsignedAttributes`: You may also explicitly list
109 // a set of attributes that should be considered unauthenticated when encountered
110 // on read. Be careful if you use this configuration. Do not remove an attribute
111 // name from this configuration, even if you are no longer writing with that attribute,
112 // as old items may still include this attribute, and our configuration needs to know
113 // to continue to exclude this attribute from the signature scope.
114 // If you add new attribute names to this field, you must first deploy the update to this
115 // field to all readers in your host fleet before deploying the update to start writing
116 // with that new attribute.
117 //
118 // For this example, we currently authenticate all attributes. To make it easier to
119 // add unauthenticated attributes in the future, we define a prefix ":" for such attributes.
120 const UNSIGNED_ATTR_PREFIX: &str = ":";
121
122 // 4. Create the DynamoDb Encryption configuration for the table we will be writing to.
123 let table_config = DynamoDbTableEncryptionConfig::builder()
124 .logical_table_name(ddb_table_name)
125 .partition_key_name("partition_key")
126 .sort_key_name("sort_key")
127 .attribute_actions_on_encrypt(attribute_actions_on_encrypt.clone())
128 .keyring(aws_kms_mrk_multi_keyring)
129 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
130 .build()?;
131
132 let table_configs = DynamoDbTablesEncryptionConfig::builder()
133 .table_encryption_configs(HashMap::from([(ddb_table_name.to_string(), table_config)]))
134 .build()?;
135
136 // 5. Create the DynamoDb Encryption Interceptor
137 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
138 let dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
139 .interceptor(DbEsdkInterceptor::new(table_configs)?)
140 .build();
141 let ddb = aws_sdk_dynamodb::Client::from_conf(dynamo_config);
142
143 // 6. Put an item into our table using the above client.
144 // Before the item gets sent to DynamoDb, it will be encrypted
145 // client-side using the MRK multi-keyring.
146 // The data key protecting this item will be encrypted
147 // with all the KMS Keys in this keyring, so that it can be
148 // decrypted with any one of those KMS Keys.
149 let item = HashMap::from([
150 (
151 "partition_key".to_string(),
152 AttributeValue::S("awsKmsMrkMultiKeyringItem".to_string()),
153 ),
154 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
155 (
156 "sensitive_data".to_string(),
157 AttributeValue::S("encrypt and sign me!".to_string()),
158 ),
159 ]);
160
161 ddb.put_item()
162 .table_name(ddb_table_name)
163 .set_item(Some(item.clone()))
164 .send()
165 .await?;
166
167 // 7. Get the item back from our table using the client.
168 // The client will decrypt the item client-side using the MRK
169 // and return back the original item.
170 // Since the generator key is the first available key in the keyring,
171 // that is the KMS Key that will be used to decrypt this item.
172 let key_to_get = HashMap::from([
173 (
174 "partition_key".to_string(),
175 AttributeValue::S("awsKmsMrkMultiKeyringItem".to_string()),
176 ),
177 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
178 ]);
179
180 let resp = ddb
181 .get_item()
182 .table_name(ddb_table_name)
183 .set_key(Some(key_to_get.clone()))
184 .consistent_read(true)
185 .send()
186 .await?;
187
188 assert_eq!(resp.item, Some(item.clone()));
189
190 // 8. Create a MRK keyring using the replica MRK arn.
191 // We will use this to demonstrate that the replica MRK
192 // can decrypt data created with the original MRK,
193 // even when the replica MRK was not present in the
194 // encrypting multi-keyring.
195 let only_replica_key_mrk_multi_keyring = mpl
196 .create_aws_kms_mrk_multi_keyring()
197 .kms_key_ids(vec![mrk_replica_key_arn.to_string()])
198 .send()
199 .await?;
200
201 // 9. Create a new config and client using the MRK keyring.
202 // This is the same setup as above, except we provide the MRK keyring to the config.
203 let only_replica_table_config = DynamoDbTableEncryptionConfig::builder()
204 .logical_table_name(ddb_table_name)
205 .partition_key_name("partition_key")
206 .sort_key_name("sort_key")
207 .attribute_actions_on_encrypt(attribute_actions_on_encrypt.clone())
208 // Only replica keyring added here
209 .keyring(only_replica_key_mrk_multi_keyring)
210 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
211 .build()?;
212
213 let only_replica_table_configs = DynamoDbTablesEncryptionConfig::builder()
214 .table_encryption_configs(HashMap::from([(
215 ddb_table_name.to_string(),
216 only_replica_table_config,
217 )]))
218 .build()?;
219
220 let only_replica_dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
221 .interceptor(DbEsdkInterceptor::new(only_replica_table_configs)?)
222 .build();
223 let only_replica_ddb = aws_sdk_dynamodb::Client::from_conf(only_replica_dynamo_config);
224
225 // 10. Get the item back from our table using the client configured with the replica.
226 // The client will decrypt the item client-side using the replica MRK
227 // and return back the original item.
228
229 let only_replica_resp = only_replica_ddb
230 .get_item()
231 .table_name(ddb_table_name)
232 .set_key(Some(key_to_get.clone()))
233 .consistent_read(true)
234 .send()
235 .await?;
236
237 assert_eq!(only_replica_resp.item, Some(item.clone()));
238
239 // 11. Create an AWS KMS keyring using the single-region key ARN.
240 // We will use this to demonstrate that the single-region key
241 // can decrypt data created with the MRK multi-keyring,
242 // since it is present in the keyring used to encrypt.
243 let only_srk_key_mrk_multi_keyring = mpl
244 .create_aws_kms_mrk_multi_keyring()
245 .kms_key_ids(vec![key_arn.to_string()])
246 .send()
247 .await?;
248
249 // 12. Create a new config and client using the AWS KMS keyring.
250 // This is the same setup as above, except we provide the AWS KMS keyring to the config.
251 let only_srk_table_config = DynamoDbTableEncryptionConfig::builder()
252 .logical_table_name(ddb_table_name)
253 .partition_key_name("partition_key")
254 .sort_key_name("sort_key")
255 .attribute_actions_on_encrypt(attribute_actions_on_encrypt)
256 // Only srk keyring added here
257 .keyring(only_srk_key_mrk_multi_keyring)
258 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
259 .build()?;
260
261 let only_srk_table_configs = DynamoDbTablesEncryptionConfig::builder()
262 .table_encryption_configs(HashMap::from([(
263 ddb_table_name.to_string(),
264 only_srk_table_config,
265 )]))
266 .build()?;
267
268 let only_srk_dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
269 .interceptor(DbEsdkInterceptor::new(only_srk_table_configs)?)
270 .build();
271 let only_srk_ddb = aws_sdk_dynamodb::Client::from_conf(only_srk_dynamo_config);
272
273 // 13. Get the item back from our table using the client configured with the AWS KMS keyring.
274 // The client will decrypt the item client-side using the single-region key
275 // and return back the original item.
276
277 let only_srk_resp = only_srk_ddb
278 .get_item()
279 .table_name(ddb_table_name)
280 .set_key(Some(key_to_get))
281 .consistent_read(true)
282 .send()
283 .await?;
284
285 assert_eq!(only_srk_resp.item, Some(item));
286
287 println!("multi_mrk_keyring successful.");
288 Ok(())
289}