main/keyring/raw_aes_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::material_providers::types::AesWrappingAlg;
10use aws_db_esdk::CryptoAction;
11use aws_db_esdk::DynamoDbTablesEncryptionConfig;
12use aws_sdk_dynamodb::types::AttributeValue;
13use std::collections::HashMap;
14
15/*
16 This example sets up DynamoDb Encryption for the AWS SDK client
17 using the raw AES Keyring. This keyring takes in an AES key
18 and uses that key to protect the data keys that encrypt and
19 decrypt DynamoDb table items.
20
21 This example takes in an `aesKeyBytes` parameter. This parameter
22 should be a ByteBuffer representing a 256-bit AES key. If this example
23 is run through the class' main method, it will create a new key.
24 In practice, users of this library should not randomly generate a key,
25 and should instead retrieve an existing key from a secure key
26 management system (e.g. an HSM).
27
28 This example encrypts a test item using the provided AES key and puts the
29 encrypted item to the provided DynamoDb table. Then, it gets the
30 item from the table and decrypts it.
31
32 Running this example requires access to the DDB Table whose name
33 is provided in CLI arguments.
34 This table must be configured with the following
35 primary key configuration:
36 - Partition key is named "partition_key" with type (S)
37 - Sort key is named "sort_key" with type (S)
38*/
39
40pub async fn put_item_get_item() -> Result<(), crate::BoxError> {
41 let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME;
42 let aes_key_bytes = generate_aes_key_bytes();
43
44 // 1. Create the keyring.
45 // The DynamoDb encryption client uses this to encrypt and decrypt items.
46 let mpl_config = MaterialProvidersConfig::builder().build()?;
47 let mpl = mpl_client::Client::from_conf(mpl_config)?;
48 let raw_aes_keyring = mpl
49 .create_raw_aes_keyring()
50 .key_name("my-aes-key-name")
51 .key_namespace("my-key-namespace")
52 .wrapping_key(aes_key_bytes)
53 .wrapping_alg(AesWrappingAlg::AlgAes256GcmIv12Tag16)
54 .send()
55 .await?;
56
57 // 2. Configure which attributes are encrypted and/or signed when writing new items.
58 // For each attribute that may exist on the items we plan to write to our DynamoDbTable,
59 // we must explicitly configure how they should be treated during item encryption:
60 // - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
61 // - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
62 // - DO_NOTHING: The attribute is not encrypted and not included in the signature
63 let attribute_actions_on_encrypt = HashMap::from([
64 ("partition_key".to_string(), CryptoAction::SignOnly), // Our partition attribute must be SIGN_ONLY
65 ("sort_key".to_string(), CryptoAction::SignOnly), // Our sort attribute must be SIGN_ONLY
66 ("sensitive_data".to_string(), CryptoAction::EncryptAndSign),
67 ]);
68
69 // 3. Configure which attributes we expect to be included in the signature
70 // when reading items. There are two options for configuring this:
71 //
72 // - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
73 // When defining your DynamoDb schema and deciding on attribute names,
74 // choose a distinguishing prefix (such as ":") for all attributes that
75 // you do not want to include in the signature.
76 // This has two main benefits:
77 // - It is easier to reason about the security and authenticity of data within your item
78 // when all unauthenticated data is easily distinguishable by their attribute name.
79 // - If you need to add new unauthenticated attributes in the future,
80 // you can easily make the corresponding update to your `attributeActionsOnEncrypt`
81 // and immediately start writing to that new attribute, without
82 // any other configuration update needed.
83 // Once you configure this field, it is not safe to update it.
84 //
85 // - Configure `allowedUnsignedAttributes`: You may also explicitly list
86 // a set of attributes that should be considered unauthenticated when encountered
87 // on read. Be careful if you use this configuration. Do not remove an attribute
88 // name from this configuration, even if you are no longer writing with that attribute,
89 // as old items may still include this attribute, and our configuration needs to know
90 // to continue to exclude this attribute from the signature scope.
91 // If you add new attribute names to this field, you must first deploy the update to this
92 // field to all readers in your host fleet before deploying the update to start writing
93 // with that new attribute.
94 //
95 // For this example, we currently authenticate all attributes. To make it easier to
96 // add unauthenticated attributes in the future, we define a prefix ":" for such attributes.
97 const UNSIGNED_ATTR_PREFIX: &str = ":";
98
99 // 4. Create the DynamoDb Encryption configuration for the table we will be writing to.
100 let table_config = DynamoDbTableEncryptionConfig::builder()
101 .logical_table_name(ddb_table_name)
102 .partition_key_name("partition_key")
103 .sort_key_name("sort_key")
104 .attribute_actions_on_encrypt(attribute_actions_on_encrypt)
105 .keyring(raw_aes_keyring)
106 .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
107 .build()?;
108
109 let table_configs = DynamoDbTablesEncryptionConfig::builder()
110 .table_encryption_configs(HashMap::from([(ddb_table_name.to_string(), table_config)]))
111 .build()?;
112
113 // 5. Create a new AWS SDK DynamoDb client using the Config above
114 let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
115 let dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
116 .interceptor(DbEsdkInterceptor::new(table_configs)?)
117 .build();
118 let ddb = aws_sdk_dynamodb::Client::from_conf(dynamo_config);
119
120 // 6. Put an item into our table using the above client.
121 // Before the item gets sent to DynamoDb, it will be encrypted
122 // client-side, according to our configuration.
123 let item = HashMap::from([
124 (
125 "partition_key".to_string(),
126 AttributeValue::S("rawAesKeyringItem".to_string()),
127 ),
128 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
129 (
130 "sensitive_data".to_string(),
131 AttributeValue::S("encrypt and sign me!".to_string()),
132 ),
133 ]);
134
135 ddb.put_item()
136 .table_name(ddb_table_name)
137 .set_item(Some(item.clone()))
138 .send()
139 .await?;
140
141 // 7. Get the item back from our table using the same client.
142 // The client will decrypt the item client-side, and return
143 // back the original item.
144 let key_to_get = HashMap::from([
145 (
146 "partition_key".to_string(),
147 AttributeValue::S("rawAesKeyringItem".to_string()),
148 ),
149 ("sort_key".to_string(), AttributeValue::N("0".to_string())),
150 ]);
151
152 let resp = ddb
153 .get_item()
154 .table_name(ddb_table_name)
155 .set_key(Some(key_to_get))
156 // In this example we configure a strongly consistent read
157 // because we perform a read immediately after a write (for demonstrative purposes).
158 // By default, reads are only eventually consistent.
159 // Read our docs to determine which read consistency to use for your application:
160 // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html
161 .consistent_read(true)
162 .send()
163 .await?;
164
165 assert_eq!(resp.item, Some(item));
166
167 println!("raw_aes_keyring successful.");
168 Ok(())
169}
170
171fn generate_aes_key_bytes() -> Vec<u8> {
172 // This example returns a static key.
173 // In practice, you should not generate this key in your code, and should instead
174 // retrieve this key from a secure key management system (e.g. HSM).
175 // This key is created here for example purposes only and should not be used for any other purpose.
176 vec![
177 1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
178 25, 26, 27, 28, 29, 30, 31, 32,
179 ]
180}