cipherstash_dynamodb/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#![doc(
    html_favicon_url = "https://cipherstash.com/favicon.ico",
    html_logo_url = "https://raw.githubusercontent.com/cipherstash/meta/main/cipherstash-logo.svg"
)]
//! # CipherStash for DynamoDB
//!
//! Based on the CipherStash SDK and ZeroKMS key service, CipherStash for DynamoDB provides a simple interface for
//! storing and retrieving encrypted data in DynamoDB.
//!
//! ## Playground
//!
//! To easily try out CipherStash for DynamoDB, visit the [cipherstash-playground](https://github.com/cipherstash/cipherstash-playground) repo.
//!
//! ## Code status
//!
//! [![Test suite](https://github.com/cipherstash/cipherstash-dynamodb/actions/workflows/test.yml/badge.svg)](https://github.com/cipherstash/cipherstash-dynamodb/actions/workflows/test.yml) [![Published documentation](https://github.com/cipherstash/cipherstash-dynamodb/actions/workflows/deploy-public-docs.yml/badge.svg)](https://github.com/cipherstash/cipherstash-dynamodb/actions/workflows/deploy-public-docs.yml)
//!
//! Code documentation is available [here](https://cipherstash.com/rustdoc/cipherstash_dynamodb/index.html).
//!
//! ## Prerequisites
//!
//! You will need to have completed the following steps before using CipherStash for DynamoDB:
//!
//! 1. [Create a CipherStash account](#step-1---create-a-cipherstash-account)
//! 2. [Install the CLI](#step-2---install-the-cli)
//! 3. [Login and create a Dataset](#step-3---create-a-dataset)
//! 4. [Init ZeroKMS](#step-4---init-zerokms)
//!
//! ### Step 1 - Create a CipherStash account
//!
//! To use CipherStash for DynamoDB, you must first [create a CipherStash account](https://cipherstash.com/signup).
//!
//! ### Step 2 - Install the CLI
//!
//! The `stash` CLI tool is required to create and manage datasets and keys used for encryption and decryption.
//! Install the CLI by following the instructions in the [CLI reference doc](https://cipherstash.com/docs/reference/cli).
//!
//! ### Step 3 - Create a dataset and client key
//!
//! To use CipherStash for DynamoDB, you must create a dataset and a client key.
//!
//! 1. [Create a dataset](https://cipherstash.com/docs/how-to/creating-datasets)
//! 2. [Create a client key](https://cipherstash.com/docs/how-to/creating-clients)
//!
//! ### Step 4 - Init ZeroKMS
//!
//! ZeroKMS uses a root key to encrypt and decrypt data.
//! This key is initialized on upload of a Dataset configuration.
//! This step is an artifact of the SQL implementation of CipherStash.
//! For now, it is sufficient to upload an empty configuration.
//!
//! There is an empty `dataset.yml` in the root of the repository, ready to be uploaded.
//! Upload it to ZeroKMS using the following command:
//!
//! ```bash
//! stash datasets config upload --file dataset.yml --client-id $CS_CLIENT_ID --client-key $CS_CLIENT_KEY
//! ```
//!
//! ## Usage
//!
//! To use CipherStash for DynamoDB, you must first create a table in DynamoDB.
//! The table must have a at least partition key, sort key, and term field - all of type String.
//!
//! CipherStash for DynamoDB also expects a Global Secondary Index called "TermIndex" to exist if you want to
//! search and query against records. This index should project all fields and have a key schema
//! that is a hash on the term attribute.
//!
//! You can use the the `aws` CLI to create a table with an appropriate schema as follows:
//!
//! ```bash
//! aws dynamodb create-table \
//!     --table-name users \
//!     --attribute-definitions \
//!         AttributeName=pk,AttributeType=S \
//!         AttributeName=sk,AttributeType=S \
//!         AttributeName=term,AttributeType=B \
//!     --key-schema \
//!         AttributeName=pk,KeyType=HASH \
//!         AttributeName=sk,KeyType=RANGE \
//!     --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
//!     --global-secondary-indexes "IndexName=TermIndex,KeySchema=[{AttributeName=term,KeyType=HASH}],Projection={ProjectionType=ALL},ProvisionedThroughput={ReadCapacityUnits=5,WriteCapacityUnits=5}"
//! ```
//!
//! See below for more information on schema design for CipherStash for DynamoDB tables.
//!
//! ### Annotating a cipherstash-dynamodb Type
//!
//! To use CipherStash for DynamoDB, you must first annotate a struct with the `Encryptable`, `Searchable` and
//! `Decryptable` derive macros.
//!
//! ```rust
//! use cipherstash_dynamodb::{Searchable, Decryptable, Encryptable, Identifiable};
//!
//! #[derive(Debug, Searchable, Decryptable, Encryptable, Identifiable)]
//! struct User {
//!     name: String,
//!     #[partition_key]
//!     email: String,
//! }
//! ```
//!
//! These derive macros will generate implementations for the following traits of the same name:
//!
//! * `Decryptable` - a trait that allows you to decrypt a record from DynamoDB
//! * `Encryptable` - a trait that allows you to encrypt a record for storage in DynamoDB
//! * `Searchable`  - a trait that allows you to search for records in DynamoDB
//!
//! The above example is the minimum required to use CipherStash for DynamoDB however you can expand capabilities via several macros.
//!
//! ### Controlling Encryption
//!
//! By default, all fields on an annotated struct are stored encrypted in the table.
//!
//! To store a field as a plaintext, you can use the `plaintext` attribute:
//!
//! ```rust
//! use cipherstash_dynamodb::{Searchable, Decryptable, Encryptable, Identifiable};
//!
//! #[derive(Debug, Searchable, Decryptable, Encryptable, Identifiable)]
//! struct User {
//!     #[partition_key]
//!     email: String,
//!     name: String,
//!
//!     #[cipherstash(plaintext)]
//!     not_sensitive: String,
//! }
//! ```
//!
//! If you don't want a field stored in the the database at all, you can annotate the field with `#[cipherstash(skip)]`.
//!
//!```rust
//! use cipherstash_dynamodb::{Searchable, Encryptable, Decryptable, Identifiable};
//!
//! #[derive(Debug, Searchable, Encryptable, Decryptable, Identifiable)]
//! struct User {
//!     #[partition_key]
//!     email: String,
//!     name: String,
//!
//!     #[cipherstash(skip)]
//!     not_required: String,
//! }
//! ```
//!
//! If you implement the `Decryptable` trait these skipped fields need to implement `Default`.
//!
//! ### Sort keys
//!
//! cipherstash-dynamodb requires every record to have a sort key. By default this will be derived based on the name of the struct.
//! However, if you want to specify your own, you can use the `sort_key_prefix` attribute:
//!
//!```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! #[cipherstash(sort_key_prefix = "user")]
//! struct User {
//!     #[partition_key]
//!     email: String,
//!     name: String,
//!
//!     #[cipherstash(skip)]
//!     not_required: String,
//! }
//! ```
//!
//! #### Dynamic Sort keys
//!
//! CipherStash for DynamoDB also supports specifying the sort key dynamically based on a field on the struct.
//! You can choose the field using the `#[sort_key]` attribute.
//!
//! ```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! struct User {
//!     #[partition_key]
//!     email: String,
//!     #[sort_key]
//!     name: String,
//!
//!     #[cipherstash(skip)]
//!     not_required: String,
//! }
//! ```
//!
//! Sort keys will contain that value and will be prefixed by the sort key prefix.
//!
//! #### Explicit `pk` and `sk` fields
//!
//! It's common in DynamoDB to use fields on your records called `pk` and `sk` for your partition
//! and sort keys. To support this behaviour these are treated as special keywords in cipherstash-dynamodb.
//! If your field contains a `pk` or an `sk` field they must be annotated with the `#[partition_key]` and `#[sort_key]` attributes respectively.
//!
//! ```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! struct User {
//!     #[partition_key]
//!     pk: String,
//!     #[sort_key]
//!     sk: String,
//!
//!     #[cipherstash(skip)]
//!     not_required: String,
//! }
//! ```
//!
//! ## Indexing
//!
//! cipherstash-dynamodb supports indexing of encrypted fields for searching.
//! Exact, prefix and compound match types are currently supported.
//! To index a field, use the `query` attribute:
//!
//! ```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! struct User {
//!     #[cipherstash(query = "exact")]
//!     #[partition_key]
//!     email: String,
//!     
//!    #[cipherstash(query = "prefix")]
//!     name: String,
//! }
//! ```
//!
//! You can also specify a compound index by using the `compound` attribute.
//! Indexes with the same name will be combined into the one index.
//!
//! Compound index names must be a combination of field names separated by a #.
//! Fields mentioned in the compound index name that aren't correctly annotated will result in a
//! compilation error.
//!
//! ```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! struct User {
//!     #[cipherstash(query = "exact", compound = "email#name")]
//!     #[partition_key]
//!     email: String,
//!     
//!    #[cipherstash(query = "prefix", compound = "email#name")]
//!     name: String,
//! }
//! ```
//!
//! It's also possible to add more than one query attribute to support querying records in multiple
//! different ways.
//!
//!
//! ```rust
//! use cipherstash_dynamodb::{Encryptable, Identifiable};
//!
//! #[derive(Debug, Encryptable, Identifiable)]
//! struct User {
//!     #[cipherstash(query = "exact")]
//!     #[cipherstash(query = "exact", compound = "email#name")]
//!     #[partition_key]
//!     email: String,
//!     
//!    #[cipherstash(query = "prefix")]
//!    #[cipherstash(query = "exact")]
//!    #[cipherstash(query = "prefix", compound = "email#name")]
//!     name: String,
//! }
//! ```
//! It's important to note that the more annotations that are added to a field the more index terms that will be generated.
//! Adding too many attributes could result in a proliferation of terms and data.
//!
//! The previous example for example would have the following terms generated:
//!
//! - One term for the exact index on email
//! - One term for the exact index on name
//! - Up to 25 terms for the prefix index on name
//! - Up to 25 terms for the compound index of email and name
//!
//! This would mean a total of 53 records would be inserted.
//!
//! ## Storing and Retrieving Records
//!
//! Interacting with a table in DynamoDB is done via the [EncryptedTable] struct.
//!
//! ```no_run
//! use cipherstash_dynamodb::{EncryptedTable, Key};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let config = aws_config::from_env()
//!         .endpoint_url("http://localhost:8000")
//!         .load()
//!         .await;
//!
//!     let client = aws_sdk_dynamodb::Client::new(&config);
//!     let table = EncryptedTable::init(client, "users").await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! All operations on the table are `async` and so you will need a runtime to execute them.
//! In the above example, we connect to a DynamoDB running in a local container and initialize an `EncryptedTable` struct
//! for the "users" table.
//!
//! ### Putting Records
//!
//! To store a record in the table, use the [`EncryptedTable::put`] method:
//!
//! ```no_run
//! # use cipherstash_dynamodb::*;
//! #
//! # #[derive(Debug, Identifiable, Encryptable, Searchable, Decryptable)]
//! # struct User {
//! #    #[partition_key]
//! #    email: String,
//! #    name: String,
//! # }
//! # impl User {
//! #   fn new(email: impl Into<String>, name: impl Into<String>) -> Self {
//! #       Self { email: email.into(), name: name.into() }
//! #   }
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! let user = User::new("dan@coderdan", "Dan Draper");
//! table.put(user).await?;
//! # Ok(())
//! # }
//! ```
//!
//! To get a record, use the [`EncryptedTable::get`] method:
//!
//! ```no_run
//! # use cipherstash_dynamodb::*;
//! #
//! # #[derive(Debug, Identifiable, Decryptable, Encryptable)]
//! # struct User {
//! #    #[partition_key]
//! #    email: String,
//! #    name: String,
//! # }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! let user: Option<User> = table.get("dan@coderdan.co").await?;
//! # Ok(())
//! # }
//! ```
//!
//! The `get` method will return `None` if the record does not exist.
//! It uses type information to decrypt the record and return it as a struct.
//!
//! ### Deleting Records
//!
//! To delete a record, use the [`EncryptedTable::delete`] method:
//!
//! ```no_run
//! # use cipherstash_dynamodb::*;
//! #
//! # #[derive(Debug, Identifiable, Decryptable, Searchable, Encryptable)]
//! # struct User {
//! #    #[partition_key]
//! #    email: String,
//! #    name: String,
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! table.delete::<User>("jane@smith.org").await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Querying Records
//!
//! To query records, use the [`EncryptedTable::query`] method which returns a builder:
//!
//! ```no_run
//! # use cipherstash_dynamodb::{Searchable, Decryptable, Encryptable, EncryptedTable, Identifiable};
//! #
//! # #[derive(Debug, Decryptable, Searchable, Encryptable, Identifiable)]
//! # struct User {
//! #    #[partition_key]
//! #    email: String,
//! #    name: String,
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! let results: Vec<User> = table
//!     .query()
//!     .starts_with("name", "Dan")
//!     .send()
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! If you have a compound index defined, CipherStash for DynamoDB will automatically use it when querying.
//!
//! ```no_run
//! # use cipherstash_dynamodb::{Encryptable, Searchable, Decryptable, EncryptedTable, Key, Identifiable};
//! #
//! # #[derive(Debug, Encryptable, Searchable, Decryptable, Identifiable)]
//! # struct User {
//! #    #[partition_key]
//! #    #[cipherstash(query = "exact")]
//! #    email: String,
//! #    #[cipherstash(query = "prefix")]
//! #    name: String,
//! # }
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! let results: Vec<User> = table
//!     .query()
//!     .eq("email", "dan@coderdan")
//!     .starts_with("name", "Dan")
//!     .send()
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! Note: if you don't have the correct indexes defined this query builder will return a runtime
//! error.
//!
//! ## Table Verticalization
//!
//! CipherStash for DynamoDB uses a technique called "verticalization" which is a popular approach to storing data in DynamoDB.
//! In practice, this means you can store multiple types in the same table.
//!
//! For example, you might want to store related records to `User` such as `License`.
//!
//! ```rust
//! use cipherstash_dynamodb::{ Searchable, Encryptable, Decryptable, Identifiable };
//!
//! #[derive(Debug, Searchable, Encryptable, Decryptable, Identifiable)]
//! struct License {
//!     #[cipherstash(query = "exact")]
//!     #[partition_key]
//!     user_email: String,
//!
//!     #[cipherstash(plaintext)]
//!     license_type: String,
//!
//!     #[cipherstash(query = "exact")]
//!     license_number: String,
//! }
//! ```
//!
//! ### Data Views
//!
//! In some cases, these types might simply be a different representation of the same data based on query requirements.
//! For example, you might want to query users by name using a prefix (say for using a "type ahead") but only return the name.
//!
//! ```rust
//! # use cipherstash_dynamodb::{Searchable, Encryptable, Decryptable, Identifiable};
//!
//! #[derive(Debug, Searchable, Encryptable, Decryptable, Identifiable)]
//! pub struct UserView {
//!     #[cipherstash(skip)]
//!     #[partition_key]
//!     email: String,
//!     
//!     #[cipherstash(query = "prefix")]
//!     name: String,
//! }
//! ```
//!
//! To use the view, you can first `put` and then `query` the value.
//!
//! ```no_run
//! # use cipherstash_dynamodb::*;
//! # #[derive(Debug, Identifiable, Searchable, Encryptable, Decryptable)]
//! # pub struct UserView {
//! #     #[cipherstash(skip)]
//! #     #[partition_key]
//! #     email: String,
//! #     
//! #     #[cipherstash(query = "prefix")]
//! #     name: String,
//! # }
//! # impl UserView {
//! #     fn new(email: impl Into<String>, name: impl Into<String>) -> Self {
//! #         Self { email: email.into(), name: name.into() }
//! #     }
//! # }
//! #
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! #    let config = aws_config::from_env()
//! #        .endpoint_url("http://localhost:8000")
//! #        .load()
//! #        .await;
//! #   let client = aws_sdk_dynamodb::Client::new(&config);
//! #   let table = EncryptedTable::init(client, "users").await?;
//! let user = UserView::new("dan@coderdan", "Dan Draper");
//! table.put(user).await?;
//! let results: Vec<UserView> = table
//!     .query()
//!     .starts_with("name", "Dan")
//!     .send()
//!     .await?;
//! # Ok(())
//! # }
//! ```
//!
//! So long as the indexes are equivalent, you can mix and match types.
//!
//! ## Internals
//!
//! ### Table Schema
//!
//! Tables created by CipherStash for DynamoDB have the following schema:
//!
//! ```txt
//! PK        |  SK           |  term                  |   name       |  email   ....
//! ---------------------------------------------------------------------------
//! HMAC(123) |  user         |                        |   Enc(name)  |  Enc(email)
//! HMAC(123) |  user#email   | STE("foo@example.net") |
//! HMAC(123) |  user#name#1  | STE("Mik")             |
//! HMAC(123) |  user#name#2  | STE("Mike")            |
//! HMAC(123) |  user#name#3  | STE("Mike ")           |
//! HMAC(123) |  user#name#4  | STE("Mike R")          |
//! ```
//!
//! `PK` and `SK` are the partition and sort keys respectively.
//! `term` is a global secondary index that is used for searching.
//! And all other attributes are dependent on the type.
//! They may be encrypted or otherwise.
//!
//! ### Source Encryption
//!
//! CipherStash for DynamoDB uses the CipherStash SDK to encrypt and decrypt data.
//! Values are encypted using a unique key for each record using AES-GCM-SIV with 256-bit keys.
//! Key generation is performed using the ZeroKMS key service and bulk operations are supported making even large queries quite fast.
//!
//! ZeroKMS's root keys are encrypted using AWS KMS and stored in DynamoDB (separate database to the data).
//!
//! When self-hosting ZeroKMS, we recommend running it in different account to your main application workloads.
//!
//! ## Issues and TODO
//!
//! - [ ] Sort keys are not currently hashed (but this may change in the future)
//!
pub mod crypto;
pub mod encrypted_table;
pub mod traits;
pub use encrypted_table::{EncryptedTable, QueryBuilder};
pub use traits::{
    Decryptable, Encryptable, Identifiable, IndexType, Pk, PkSk, PrimaryKey, Searchable,
    SingleIndex,
};

pub mod errors;
pub use errors::Error;

#[doc(hidden)]
pub use cipherstash_dynamodb_derive::{Decryptable, Encryptable, Identifiable, Searchable};

// Re-exports
pub use cipherstash_client::encryption;

mod async_map_somes;

pub type Key = [u8; 32];