main/keyring/
kms_rsa_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::DbeAlgorithmSuiteId;
10use aws_db_esdk::CryptoAction;
11use aws_db_esdk::DynamoDbTablesEncryptionConfig;
12use aws_sdk_dynamodb::types::AttributeValue;
13use std::collections::HashMap;
14use std::fs::File;
15use std::io::Read;
16use std::io::Write;
17use std::path::Path;
18
19/*
20 This example sets up DynamoDb Encryption for the AWS SDK client
21 using the KMS RSA Keyring. This keyring uses a KMS RSA key pair to
22 encrypt and decrypt records. The client uses the downloaded public key
23 to encrypt items it adds to the table.
24 The keyring uses the private key to decrypt existing table items it retrieves,
25 by calling KMS' decrypt API.
26
27 Running this example requires access to the DDB Table whose name
28 is provided in CLI arguments.
29 This table must be configured with the following
30 primary key configuration:
31   - Partition key is named "partition_key" with type (S)
32   - Sort key is named "sort_key" with type (S)
33 This example also requires access to a KMS RSA key.
34 Our tests provide a KMS RSA ARN that anyone can use, but you
35 can also provide your own KMS RSA key.
36 To use your own KMS RSA key, you must have either:
37  - Its public key downloaded in a UTF-8 encoded PEM file
38  - kms:GetPublicKey permissions on that key
39 If you do not have the public key downloaded, running this example
40 through its main method will download the public key for you
41 by calling kms:GetPublicKey.
42 You must also have kms:Decrypt permissions on the KMS RSA key.
43*/
44
45const DEFAULT_EXAMPLE_RSA_PUBLIC_KEY_FILENAME: &str = "KmsRsaKeyringExamplePublicKey.pem";
46
47pub async fn put_item_get_item() -> Result<(), crate::BoxError> {
48    let ddb_table_name = test_utils::TEST_DDB_TABLE_NAME;
49    let rsa_key_arn = test_utils::TEST_KMS_RSA_KEY_ID;
50
51    // You may provide your own RSA public key at EXAMPLE_RSA_PUBLIC_KEY_FILENAME.
52    // This must be the public key for the RSA key represented at rsaKeyArn.
53    // If this file is not present, this will write a UTF-8 encoded PEM file for you.
54    if should_get_new_public_key(DEFAULT_EXAMPLE_RSA_PUBLIC_KEY_FILENAME) {
55        write_public_key_pem_for_rsa_key(
56            test_utils::TEST_KMS_RSA_KEY_ID,
57            DEFAULT_EXAMPLE_RSA_PUBLIC_KEY_FILENAME,
58        )
59        .await?;
60    }
61
62    // 1. Load UTF-8 encoded public key PEM file.
63    //    You may have an RSA public key file already defined.
64    //    If not, the main method in this class will call
65    //    the KMS RSA key, retrieve its public key, and store it
66    //    in a PEM file for example use.
67    let mut file = File::open(Path::new(DEFAULT_EXAMPLE_RSA_PUBLIC_KEY_FILENAME))?;
68    let mut public_key_utf8_bytes = Vec::new();
69    file.read_to_end(&mut public_key_utf8_bytes)?;
70
71    // 2. Create a KMS RSA keyring.
72    //    This keyring takes in:
73    //     - kmsClient
74    //     - kmsKeyId: Must be an ARN representing a KMS RSA key
75    //     - publicKey: A ByteBuffer of a UTF-8 encoded PEM file representing the public
76    //                  key for the key passed into kmsKeyId
77    //     - encryptionAlgorithm: Must be either RSAES_OAEP_SHA_256 or RSAES_OAEP_SHA_1
78    let mpl_config = MaterialProvidersConfig::builder().build()?;
79    let mpl = mpl_client::Client::from_conf(mpl_config)?;
80    let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
81    let kms_rsa_keyring = mpl
82        .create_aws_kms_rsa_keyring()
83        .kms_key_id(rsa_key_arn)
84        .public_key(public_key_utf8_bytes)
85        .encryption_algorithm(aws_sdk_kms::types::EncryptionAlgorithmSpec::RsaesOaepSha256)
86        .kms_client(aws_sdk_kms::Client::new(&sdk_config))
87        .send()
88        .await?;
89
90    // 3. Configure which attributes are encrypted and/or signed when writing new items.
91    //    For each attribute that may exist on the items we plan to write to our DynamoDbTable,
92    //    we must explicitly configure how they should be treated during item encryption:
93    //      - ENCRYPT_AND_SIGN: The attribute is encrypted and included in the signature
94    //      - SIGN_ONLY: The attribute not encrypted, but is still included in the signature
95    //      - DO_NOTHING: The attribute is not encrypted and not included in the signature
96    let attribute_actions_on_encrypt = HashMap::from([
97        ("partition_key".to_string(), CryptoAction::SignOnly), // Our partition attribute must be SIGN_ONLY
98        ("sort_key".to_string(), CryptoAction::SignOnly), // Our sort attribute must be SIGN_ONLY
99        ("sensitive_data".to_string(), CryptoAction::EncryptAndSign),
100    ]);
101
102    // 4. Configure which attributes we expect to be included in the signature
103    //    when reading items. There are two options for configuring this:
104    //
105    //    - (Recommended) Configure `allowedUnsignedAttributesPrefix`:
106    //      When defining your DynamoDb schema and deciding on attribute names,
107    //      choose a distinguishing prefix (such as ":") for all attributes that
108    //      you do not want to include in the signature.
109    //      This has two main benefits:
110    //      - It is easier to reason about the security and authenticity of data within your item
111    //        when all unauthenticated data is easily distinguishable by their attribute name.
112    //      - If you need to add new unauthenticated attributes in the future,
113    //        you can easily make the corresponding update to your `attributeActions`
114    //        and immediately start writing to that new attribute, without
115    //        any other configuration update needed.
116    //      Once you configure this field, it is not safe to update it.
117    //
118    //    - Configure `allowedUnsignedAttributes`: You may also explicitly list
119    //      a set of attributes that should be considered unauthenticated when encountered
120    //      on read. Be careful if you use this configuration. Do not remove an attribute
121    //      name from this configuration, even if you are no longer writing with that attribute,
122    //      as old items may still include this attribute, and our configuration needs to know
123    //      to continue to exclude this attribute from the signature scope.
124    //      If you add new attribute names to this field, you must first deploy the update to this
125    //      field to all readers in your host fleet before deploying the update to start writing
126    //      with that new attribute.
127    //
128    //   For this example, we currently authenticate all attributes. To make it easier to
129    //   add unauthenticated attributes in the future, we define a prefix ":" for such attributes.
130    const UNSIGNED_ATTR_PREFIX: &str = ":";
131
132    // 5. Create the DynamoDb Encryption configuration for the table we will be writing to.
133    //    Note: To use the KMS RSA keyring, your table config must specify an algorithmSuite
134    //    that does not use asymmetric signing.
135    let table_config = DynamoDbTableEncryptionConfig::builder()
136        .logical_table_name(ddb_table_name)
137        .partition_key_name("partition_key")
138        .sort_key_name("sort_key")
139        .attribute_actions_on_encrypt(attribute_actions_on_encrypt)
140        .keyring(kms_rsa_keyring)
141        .allowed_unsigned_attribute_prefix(UNSIGNED_ATTR_PREFIX)
142        // Specify algorithmSuite without asymmetric signing here
143        // As of v3.0.0, the only supported algorithmSuite without asymmetric signing is
144        // ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY_SYMSIG_HMAC_SHA384.
145        .algorithm_suite_id(DbeAlgorithmSuiteId::AlgAes256GcmHkdfSha512CommitKeySymsigHmacSha384)
146        .build()?;
147
148    let table_configs = DynamoDbTablesEncryptionConfig::builder()
149        .table_encryption_configs(HashMap::from([(ddb_table_name.to_string(), table_config)]))
150        .build()?;
151
152    // 6. Create a new AWS SDK DynamoDb client using the DynamoDb Encryption Interceptor above
153    let dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config)
154        .interceptor(DbEsdkInterceptor::new(table_configs)?)
155        .build();
156    let ddb = aws_sdk_dynamodb::Client::from_conf(dynamo_config);
157
158    // 7. Put an item into our table using the above client.
159    //    Before the item gets sent to DynamoDb, it will be encrypted
160    //    client-side, according to our configuration.
161    let item = HashMap::from([
162        (
163            "partition_key".to_string(),
164            AttributeValue::S("awsKmsRsaKeyringItem".to_string()),
165        ),
166        ("sort_key".to_string(), AttributeValue::N("0".to_string())),
167        (
168            "sensitive_data".to_string(),
169            AttributeValue::S("encrypt and sign me!".to_string()),
170        ),
171    ]);
172
173    ddb.put_item()
174        .table_name(ddb_table_name)
175        .set_item(Some(item.clone()))
176        .send()
177        .await?;
178
179    // 8. Get the item back from our table using the client.
180    //    The client will decrypt the item client-side using the RSA keyring
181    //    and return the original item.
182    let key_to_get = HashMap::from([
183        (
184            "partition_key".to_string(),
185            AttributeValue::S("awsKmsRsaKeyringItem".to_string()),
186        ),
187        ("sort_key".to_string(), AttributeValue::N("0".to_string())),
188    ]);
189
190    let resp = ddb
191        .get_item()
192        .table_name(ddb_table_name)
193        .set_key(Some(key_to_get))
194        .consistent_read(true)
195        .send()
196        .await?;
197
198    assert_eq!(resp.item, Some(item));
199    println!("kms_rsa_keyring successful.");
200    Ok(())
201}
202
203fn should_get_new_public_key(rsa_public_key_filename: &str) -> bool {
204    // Check if a public key file already exists
205    !Path::new(rsa_public_key_filename).exists()
206}
207
208async fn write_public_key_pem_for_rsa_key(
209    rsa_key_arn: &str,
210    rsa_public_key_filename: &str,
211) -> Result<(), crate::BoxError> {
212    // Safety check: Validate file is not present
213    if Path::new(rsa_public_key_filename).exists() {
214        return Err(crate::BoxError(
215            "write_public_key_pem_for_rsa_key will not overwrite existing PEM files.".to_string(),
216        ));
217    }
218
219    // This code will call KMS to get the public key for the KMS RSA key.
220    // You must have kms:GetPublicKey permissions on the key for this to succeed.
221    // The public key will be written to the file EXAMPLE_RSA_PUBLIC_KEY_FILENAME.
222    let sdk_config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
223    let getter_for_public_key = aws_sdk_kms::Client::new(&sdk_config);
224
225    let response = getter_for_public_key
226        .get_public_key()
227        .key_id(rsa_key_arn)
228        .send()
229        .await?;
230
231    let public_key_bytes = response.public_key.unwrap().into_inner();
232
233    let public_key = pem::Pem::new("PUBLIC KEY", public_key_bytes);
234    let public_key = pem::encode(&public_key);
235
236    std::fs::OpenOptions::new()
237        .write(true)
238        .create(true)
239        .truncate(true)
240        .open(Path::new(rsa_public_key_filename))?
241        .write_all(public_key.as_bytes())?;
242
243    Ok(())
244}