|
1 | | -use cipherstash_dynamodb::{Decryptable, Encryptable, EncryptedTable, Identifiable, Searchable}; |
| 1 | +use cipherstash_client::ZeroKMSConfig; |
| 2 | +use cipherstash_dynamodb::{encrypted_table::Dynamo, Decryptable, Encryptable, EncryptedTable, Identifiable, Searchable}; |
2 | 3 | use miette::IntoDiagnostic; |
| 4 | +use uuid::Uuid; |
3 | 5 | mod common; |
4 | 6 |
|
5 | 7 | #[derive(Debug, Clone, PartialEq, Identifiable, Encryptable, Decryptable, Searchable)] |
@@ -123,26 +125,10 @@ struct Crazy { |
123 | 125 | pt_k_none: Option<Vec<Vec<u8>>>, |
124 | 126 | } |
125 | 127 |
|
126 | | -#[tokio::test] |
127 | | -async fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> { |
128 | | - let config = aws_config::from_env() |
129 | | - .endpoint_url("http://localhost:8000") |
130 | | - .load() |
131 | | - .await; |
132 | | - |
133 | | - let client = aws_sdk_dynamodb::Client::new(&config); |
134 | | - |
135 | | - let table_name = "crazy-record"; |
136 | | - |
137 | | - common::create_table(&client, table_name).await; |
138 | | - |
139 | | - let table = EncryptedTable::init(client, table_name) |
140 | | - .await |
141 | | - .expect("Failed to init table"); |
142 | | - |
143 | | - let r = Crazy { |
144 | | - email: "[email protected]".into(), |
145 | | - name: "Dan".into(), |
| 128 | +fn build_test_record(email: &str, name: &str) -> Crazy { |
| 129 | + Crazy { |
| 130 | + email: email.into(), |
| 131 | + name: name.into(), |
146 | 132 |
|
147 | 133 | ct_a: 123, |
148 | 134 | ct_b: 321, |
@@ -205,17 +191,91 @@ async fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> { |
205 | 191 | pt_i_none: None, |
206 | 192 | pt_j_none: None, |
207 | 193 | pt_k_none: None, |
208 | | - }; |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +async fn init_table() -> EncryptedTable<Dynamo> { |
| 198 | + let config = aws_config::from_env() |
| 199 | + .endpoint_url("http://localhost:8000") |
| 200 | + .load() |
| 201 | + .await; |
| 202 | + |
| 203 | + let client = aws_sdk_dynamodb::Client::new(&config); |
| 204 | + |
| 205 | + let table_name = "crazy-record"; |
| 206 | + |
| 207 | + common::create_table(&client, table_name).await; |
| 208 | + |
| 209 | + EncryptedTable::init(client, table_name) |
| 210 | + .await |
| 211 | + .expect("Failed to init table") |
| 212 | +} |
209 | 213 |
|
210 | | - table.put(r.clone()).await.into_diagnostic()?; |
| 214 | +#[tokio::test] |
| 215 | +async fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> { |
| 216 | + let table = init_table().await; |
| 217 | + let record = build_test_record("[email protected]", "Dan"); |
| 218 | + table.put(record.clone()).await.into_diagnostic()?; |
211 | 219 |
|
212 | 220 | let s: Crazy = table |
213 | 221 | .get(("[email protected]", "Dan")) |
214 | 222 | .await |
215 | 223 | .into_diagnostic()? |
216 | 224 | .unwrap(); |
217 | 225 |
|
218 | | - assert_eq!(s, r); |
| 226 | + assert_eq!(s, record); |
| 227 | + |
| 228 | + Ok(()) |
| 229 | +} |
| 230 | + |
| 231 | +#[tokio::test] |
| 232 | +async fn test_invalid_dataset() -> Result<(), Box<dyn std::error::Error>> { |
| 233 | + let table = init_table().await; |
| 234 | + let record = build_test_record("[email protected]", "Dan"); |
| 235 | + |
| 236 | + // A random UUID doesn't exist |
| 237 | + assert_err!(table.put_via(record.clone(), Uuid::new_v4()).await); |
| 238 | + |
| 239 | + Ok(()) |
| 240 | +} |
| 241 | + |
| 242 | +#[tokio::test] |
| 243 | +async fn test_invalid_specific_dataset() -> miette::Result<()> { |
| 244 | + // TODO: Load client ID from env |
| 245 | + let client_id = Uuid::parse_str("b91e5b26-f21f-4694-8bce-c61c10e42301").into_diagnostic()?; |
| 246 | + let client = ZeroKMSConfig::builder() |
| 247 | + .with_env() |
| 248 | + .build() |
| 249 | + .into_diagnostic()? |
| 250 | + .create_client(); |
| 251 | + |
| 252 | + let dataset = client |
| 253 | + .create_dataset("test-dataset", "Test dataset") |
| 254 | + .await |
| 255 | + .into_diagnostic()?; |
| 256 | + |
| 257 | + // Grant ourselves access to the dataset |
| 258 | + client.grant_dataset(client_id, dataset.id) |
| 259 | + .await |
| 260 | + .into_diagnostic()?; |
| 261 | + |
| 262 | + let table = init_table().await; |
| 263 | + let record = build_test_record("[email protected]", "Person"); |
| 264 | + |
| 265 | + table.put_via(record.clone(), dataset.id).await?; |
| 266 | + |
| 267 | + let s: Crazy = table |
| 268 | + .get_via(("[email protected]", "Person"), dataset .id) |
| 269 | + .await? |
| 270 | + .unwrap(); |
| 271 | + |
| 272 | + assert_eq!(s, record); |
| 273 | + |
| 274 | + // Test that we can't get the record via the default dataset |
| 275 | + assert_none!(table |
| 276 | + .get ::< Crazy> (("[email protected]", "Person")) |
| 277 | + .await?); |
| 278 | + |
219 | 279 |
|
220 | 280 | Ok(()) |
221 | 281 | } |
0 commit comments