main/clientsupplier/client_supplier_example.rs
1// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use super::regional_role_client_supplier::RegionalRoleClientSupplier;
5use crate::test_utils;
6use aws_db_esdk::dynamodb::types::DynamoDbTableEncryptionConfig;
7use aws_db_esdk::intercept::DbEsdkInterceptor;
8use aws_db_esdk::material_providers::client as mpl_client;
9use aws_db_esdk::material_providers::types::material_providers_config::MaterialProvidersConfig;
10use aws_db_esdk::material_providers::types::DiscoveryFilter;
11use aws_db_esdk::CryptoAction;
12use aws_db_esdk::DynamoDbTablesEncryptionConfig;
13use aws_sdk_dynamodb::types::AttributeValue;
14use std::collections::HashMap;
15
16/*
17 This example sets up an MRK multi-keyring and an MRK discovery
18 multi-keyring using a custom client supplier.
19 A custom client supplier grants users access to more granular
20 configuration aspects of their authentication details and KMS
21 client. In this example, we create a simple custom client supplier
22 that authenticates with a different IAM role based on the
23 region of the KMS key.
24
25 This example creates a MRK multi-keyring configured with a custom
26 client supplier using a single MRK and puts an encrypted item to the
27 table. Then, it creates a MRK discovery multi-keyring to decrypt the item
28 and retrieves the item from the table.
29
30 Running this example requires access to the DDB Table whose name
31 is provided in CLI arguments.
32 This table must be configured with the following
33 primary key configuration:
34 - Partition key is named "partition_key" with type (S)
35 - Sort key is named "sort_key" with type (S)
36*/
37pub async fn put_item_get_item() -> Result<(), crate::BoxError> {
38 let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME;
39 // Note that we pass in an MRK in us-east-1...
40 let key_arn = test_utils::TEST_MRK_REPLICA_KEY_ID_US_EAST_1.to_string();
41 let account_ids = vec![test_utils::TEST_AWS_ACCOUNT_ID.to_string()];
42 // ...and access its replica in eu-west-1
43 let regions = vec!["eu-west-1".to_string()];
44
45 // 1. Create a single MRK multi-keyring.
46 // This can be either a single-region KMS key or an MRK.
47 // For this example to succeed, the key's region must either
48 // 1) be in the regions list, or
49 // 2) the key must be an MRK with a replica defined
50 // in a region in the regions list, and the client
51 // must have the correct permissions to access the replica.
52 let mpl_config = MaterialProvidersConfig::builder().build()?;
53 let mpl = mpl_client::Client::from_conf(mpl_config)?;
54
55 // Create the multi-keyring using our custom client supplier
56 // defined in the RegionalRoleClientSupplier class in this directory.
57 // Note: RegionalRoleClientSupplier will internally use the key_arn's region
58 // to retrieve the correct IAM role.
59
60 let mrk_keyring_with_client_supplier = mpl
61 .create_aws_kms_mrk_multi_keyring()
62 .client_supplier(RegionalRoleClientSupplier {})
63 .generator(key_arn)
64 .send()
65 .await?;
66
67 // 2. Configure which attributes are encrypted and/or signed when writing new items.
68 // For each attribute that may exist on the items we plan to write to our DynamoDbTable,
69 // we must explicitly configure how they should be treated during item encryption:
70 // - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
71 // - SIGN_ONLY: The attribute is not encrypted, but is still included in the signature
72 // - DO_NOTHING: The attribute is not encrypted and not included in the signature
73 let attribute_actions_on_encrypt = HashMap::from([
74 ("partition_key".to_string(), CryptoAction::SignOnly), // Our partition attribute must be SIGN_ONLY
75 ("sort_key".to_string(), CryptoAction::SignOnly), // Our sort attribute must be SIGN_ONLY
76 ("sensitive_data".to_string(), CryptoAction::EncryptAndSign),
77 ]);
78
79 // 3. Configure which attributes we expect to be included in the signature
80 // when reading items. There are two options for configuring this:
81 //
82 // - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
83 // When defining your DynamoDb schema and deciding on attribute names,
84 // choose a distinguishing prefix (such as ":") for all attributes that
85 // you do not want to include in the signature.
86 // This has two main benefits:
87 // - It is easier to reason about the security and authenticity of data within your item
88 // when all unauthenticated data is easily distinguishable by their attribute name.
89 // - If you need to add new unauthenticated attributes in the future,
90 // you can easily make the corresponding update to your `attributeActionsOnEncrypt`
91 // and immediately start writing to that new attribute, without
92 // any other configuration update needed.
93 // Once you configure this field, it is not safe to update it.
94 //
95 // - Configure `allowedUnsignedAttributes`: You may also explicitly list
96 // a set of attributes that should be considered unauthenticated when encountered
97 // on read. Be careful if you use this configuration. Do not remove an attribute
98 // name from this configuration, even if you are no longer writing with that attribute,
99 // as old items may still include this attribute, and our configuration needs to know
100 // to continue to exclude this attribute from the signature scope.
101 // If you add new attribute names to this field, you must first deploy the update to this
102 // field to all readers in your host fleet before deploying the update to start writing
103 // with that new attribute.
104 //
105 // For this example, we currently authenticate all attributes. To make it easier to
106 // add unauthenticated attributes in the future, we define a prefix ":" for such attributes.
107 const UNSIGNED_ATTR_PREFIX: &str = ":";
108
109 // 4. Create the DynamoDb Encryption configuration for the table we will be writing to.
110 let table_config = DynamoDbTableEncryptionConfig::builder()
111 .logical_table_name(ddb_table_name)
112 .partition_key_name("partition_key")
113 .sort_key_name("sort_key")
114 .attribute_actions_on_encrypt(attribute_actions_on_encrypt.clone())
115 .keyring(mrk_keyring_with_client_supplier)
116 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
117 .build()?;
118
119 let table_configs = DynamoDbTablesEncryptionConfig::builder()
120 .table_encryption_configs(HashMap::from([(ddb_table_name.to_string(), table_config)]))
121 .build()?;
122
123 // 5. Create a new AWS SDK DynamoDb client using the DynamoDb Config above
124 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
125 let dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
126 .interceptor(DbEsdkInterceptor::new(table_configs)?)
127 .build();
128 let ddb = aws_sdk_dynamodb::Client::from_conf(dynamo_config);
129
130 // 6. Put an item into our table using the above client.
131 // Before the item gets sent to DynamoDb, it will be encrypted
132 // client-side using the MRK multi-keyring.
133 // The data key protecting this item will be encrypted
134 // with all the KMS Keys in this keyring, so that it can be
135 // decrypted with any one of those KMS Keys.
136 let item = HashMap::from([
137 (
138 "partition_key".to_string(),
139 AttributeValue::S("clientSupplierItem".to_string()),
140 ),
141 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
142 (
143 "sensitive_data".to_string(),
144 AttributeValue::S("encrypt and sign me!".to_string()),
145 ),
146 ]);
147
148 ddb.put_item()
149 .table_name(ddb_table_name)
150 .set_item(Some(item.clone()))
151 .send()
152 .await?;
153
154 // 7. Get the item back from our table using the same keyring.
155 // The client will decrypt the item client-side using the MRK
156 // and return the original item.
157 let key_to_get = HashMap::from([
158 (
159 "partition_key".to_string(),
160 AttributeValue::S("clientSupplierItem".to_string()),
161 ),
162 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
163 ]);
164
165 let resp = ddb
166 .get_item()
167 .table_name(ddb_table_name)
168 .set_key(Some(key_to_get.clone()))
169 .consistent_read(true)
170 .send()
171 .await?;
172
173 assert_eq!(
174 resp.item.unwrap()["sensitive_data"],
175 AttributeValue::S("encrypt and sign me!".to_string())
176 );
177
178 // 8. Create a MRK discovery multi-keyring with a custom client supplier.
179 // A discovery MRK multi-keyring will be composed of
180 // multiple discovery MRK keyrings, one for each region.
181 // Each component keyring has its own KMS client in a particular region.
182 // When we provide a client supplier to the multi-keyring, all component
183 // keyrings will use that client supplier configuration.
184 // In our tests, we make `key_arn` an MRK with a replica, and
185 // provide only the replica region in our discovery filter.
186 let discovery_filter = DiscoveryFilter::builder()
187 .partition("aws")
188 .account_ids(account_ids)
189 .build()?;
190
191 let mrk_discovery_client_supplier_keyring = mpl
192 .create_aws_kms_mrk_discovery_multi_keyring()
193 .client_supplier(RegionalRoleClientSupplier {})
194 .discovery_filter(discovery_filter)
195 .regions(regions)
196 .send()
197 .await?;
198
199 // 9. Create a new config and client using the discovery keyring.
200 // This is the same setup as above, except we provide the discovery keyring to the config.
201 let only_replica_table_config = DynamoDbTableEncryptionConfig::builder()
202 .logical_table_name(ddb_table_name)
203 .partition_key_name("partition_key")
204 .sort_key_name("sort_key")
205 .attribute_actions_on_encrypt(attribute_actions_on_encrypt)
206 .keyring(mrk_discovery_client_supplier_keyring)
207 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
208 .build()?;
209
210 let only_replica_table_configs = DynamoDbTablesEncryptionConfig::builder()
211 .table_encryption_configs(HashMap::from([(
212 ddb_table_name.to_string(),
213 only_replica_table_config,
214 )]))
215 .build()?;
216
217 let only_replica_dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
218 .interceptor(DbEsdkInterceptor::new(only_replica_table_configs)?)
219 .build();
220 let only_replica_ddb = aws_sdk_dynamodb::Client::from_conf(only_replica_dynamo_config);
221
222 // 10. Get the item back from our table using the discovery keyring client.
223 // The client will decrypt the item client-side using the keyring,
224 // and return the original item.
225 // The discovery keyring will only use KMS keys in the provided regions and
226 // AWS accounts. Since we have provided it with a custom client supplier
227 // which uses different IAM roles based on the key region,
228 // the discovery keyring will use a particular IAM role to decrypt
229 // based on the region of the KMS key it uses to decrypt.
230
231 let resp = only_replica_ddb
232 .get_item()
233 .table_name(ddb_table_name)
234 .set_key(Some(key_to_get))
235 .consistent_read(true)
236 .send()
237 .await?;
238
239 assert_eq!(
240 resp.item.unwrap()["sensitive_data"],
241 AttributeValue::S("encrypt and sign me!".to_string())
242 );
243
244 println!("client_supplier_example successful.");
245 Ok(())
246}