| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use crate::common::*; |
| 17 |
|
| 18 |
const KEYS_PATH: &str = "/api/v1/sync/keys"; |
| 19 |
const ROTATE_PATH: &str = "/api/v1/sync/keys/rotate"; |
| 20 |
const ENTRIES_PATH: &str = "/api/v1/sync/keys/rotate/entries"; |
| 21 |
const BATCH_PATH: &str = "/api/v1/sync/keys/rotate/batch"; |
| 22 |
const COMPLETE_PATH: &str = "/api/v1/sync/keys/rotate/complete"; |
| 23 |
const PULL_PATH: &str = "/api/v1/sync/pull"; |
| 24 |
|
| 25 |
const ROTATE_PW: &str = "rotate-password"; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
fn get_keys_body(old_key: &[u8; 32]) -> serde_json::Value { |
| 30 |
let envelope = synckit_client::crypto::wrap_master_key(old_key, ROTATE_PW).unwrap(); |
| 31 |
json!({ "encrypted_key": envelope, "key_version": 1, "key_id": 1 }) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
fn rotation_entry(old_key: &[u8; 32], table: &str, row_id: &str) -> serde_json::Value { |
| 37 |
let ctx = synckit_client::crypto::AeadContext::entry(table, row_id); |
| 38 |
let sealed = |
| 39 |
synckit_client::crypto::encrypt_json_aad(&json!({"title": "secret"}), old_key, &ctx) |
| 40 |
.unwrap(); |
| 41 |
json!({ "seq": 1, "table": table, "row_id": row_id, "data": sealed }) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
fn begin_body(target_seq: usize) -> serde_json::Value { |
| 46 |
json!({ "rotation_id": Uuid::new_v4(), "target_seq": target_seq, "new_key_id": 2 }) |
| 47 |
} |
| 48 |
|
| 49 |
#[tokio::test] |
| 50 |
async fn rotate_key_drives_full_orchestration() { |
| 51 |
let kit = MockKit::start().await; |
| 52 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 53 |
|
| 54 |
kit.get(KEYS_PATH).json(get_keys_body(&old_key)).await; |
| 55 |
kit.post(ROTATE_PATH).json(begin_body(1)).await; |
| 56 |
|
| 57 |
kit.post(ENTRIES_PATH) |
| 58 |
.json(json!({ |
| 59 |
"entries": [rotation_entry(&old_key, "tasks", "r1")], |
| 60 |
"has_more": false |
| 61 |
})) |
| 62 |
.await; |
| 63 |
kit.post(BATCH_PATH) |
| 64 |
.json(json!({ "updated_count": 1 })) |
| 65 |
.await; |
| 66 |
kit.post(COMPLETE_PATH).empty().await; |
| 67 |
|
| 68 |
kit.authed() |
| 69 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 70 |
.await |
| 71 |
.expect("full rotation should complete"); |
| 72 |
|
| 73 |
|
| 74 |
assert_eq!(kit.hits(KEYS_PATH).await, 1, "fetched key state once"); |
| 75 |
assert_eq!(kit.hits(ROTATE_PATH).await, 1, "began rotation once"); |
| 76 |
assert!( |
| 77 |
kit.hits(ENTRIES_PATH).await >= 1, |
| 78 |
"pulled entries to re-encrypt" |
| 79 |
); |
| 80 |
assert_eq!( |
| 81 |
kit.hits(BATCH_PATH).await, |
| 82 |
1, |
| 83 |
"pushed one re-encrypted batch" |
| 84 |
); |
| 85 |
assert_eq!( |
| 86 |
kit.hits(COMPLETE_PATH).await, |
| 87 |
1, |
| 88 |
"completed once (no stragglers)" |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
#[tokio::test] |
| 93 |
async fn rotate_key_retries_reencrypt_on_straggler_conflict() { |
| 94 |
let kit = MockKit::start().await; |
| 95 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 96 |
|
| 97 |
kit.get(KEYS_PATH).json(get_keys_body(&old_key)).await; |
| 98 |
kit.post(ROTATE_PATH).json(begin_body(1)).await; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
kit.post(ENTRIES_PATH) |
| 103 |
.once() |
| 104 |
.json(json!({ |
| 105 |
"entries": [rotation_entry(&old_key, "tasks", "r1")], |
| 106 |
"has_more": false |
| 107 |
})) |
| 108 |
.await; |
| 109 |
kit.post(ENTRIES_PATH) |
| 110 |
.json(json!({ "entries": [], "has_more": false })) |
| 111 |
.await; |
| 112 |
kit.post(BATCH_PATH) |
| 113 |
.json(json!({ "updated_count": 1 })) |
| 114 |
.await; |
| 115 |
|
| 116 |
kit.post(COMPLETE_PATH) |
| 117 |
.code(409) |
| 118 |
.once() |
| 119 |
.json(json!({ "message": "stragglers" })) |
| 120 |
.await; |
| 121 |
kit.post(COMPLETE_PATH).empty().await; |
| 122 |
|
| 123 |
kit.authed() |
| 124 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 125 |
.await |
| 126 |
.expect("rotation should converge after the straggler retry"); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
assert_eq!( |
| 131 |
kit.hits(COMPLETE_PATH).await, |
| 132 |
2, |
| 133 |
"completed twice: 409 then 200" |
| 134 |
); |
| 135 |
assert!( |
| 136 |
kit.hits(ENTRIES_PATH).await >= 2, |
| 137 |
"straggler round re-pulled entries" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
fn relation_rows() -> Vec<(&'static str, &'static str, serde_json::Value)> { |
| 147 |
vec![ |
| 148 |
( |
| 149 |
"tasks", |
| 150 |
"row-1", |
| 151 |
json!({"title": "write the relation", "n": 1}), |
| 152 |
), |
| 153 |
( |
| 154 |
"tasks", |
| 155 |
"row-2", |
| 156 |
json!({"title": "keep the plaintext", "n": 2}), |
| 157 |
), |
| 158 |
( |
| 159 |
"notes", |
| 160 |
"row-3", |
| 161 |
json!({"body": "unicode: \u{1f6ab} \u{4f60}\u{597d}", "n": 3}), |
| 162 |
), |
| 163 |
] |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
fn sealed_envelope( |
| 169 |
key: &[u8; 32], |
| 170 |
device_id: DeviceId, |
| 171 |
table: &str, |
| 172 |
row_id: &str, |
| 173 |
payload: &serde_json::Value, |
| 174 |
) -> serde_json::Value { |
| 175 |
let ctx = synckit_client::crypto::AeadContext::entry(table, row_id); |
| 176 |
let envelope = json!({ |
| 177 |
"__skver": 2, |
| 178 |
"__skhlc": Hlc::zero(device_id), |
| 179 |
"data": payload, |
| 180 |
}); |
| 181 |
synckit_client::crypto::encrypt_json_aad(&envelope, key, &ctx).unwrap() |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
fn pull_wire( |
| 186 |
device_id: DeviceId, |
| 187 |
seq: i64, |
| 188 |
table: &str, |
| 189 |
row_id: &str, |
| 190 |
data: &serde_json::Value, |
| 191 |
key_id: i32, |
| 192 |
) -> serde_json::Value { |
| 193 |
json!({ |
| 194 |
"seq": seq, |
| 195 |
"device_id": device_id, |
| 196 |
"table": table, |
| 197 |
"op": "INSERT", |
| 198 |
"row_id": row_id, |
| 199 |
"timestamp": "2025-06-01T12:00:00Z", |
| 200 |
"key_id": key_id, |
| 201 |
"data": data, |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[tokio::test] |
| 221 |
async fn a_pull_spanning_a_rotation_yields_what_a_pull_before_it_yielded() { |
| 222 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 223 |
let new_key = synckit_client::crypto::generate_master_key(); |
| 224 |
let device_id = DeviceId::new(Uuid::new_v4()); |
| 225 |
let rows = relation_rows(); |
| 226 |
|
| 227 |
let sealed_under_old: Vec<serde_json::Value> = rows |
| 228 |
.iter() |
| 229 |
.map(|(table, row_id, payload)| { |
| 230 |
sealed_envelope(&old_key, device_id, table, row_id, payload) |
| 231 |
}) |
| 232 |
.collect(); |
| 233 |
|
| 234 |
|
| 235 |
let before = { |
| 236 |
let kit = MockKit::start().await; |
| 237 |
let client = kit.authed(); |
| 238 |
client.set_master_key_raw(old_key); |
| 239 |
let wire: Vec<serde_json::Value> = rows |
| 240 |
.iter() |
| 241 |
.zip(&sealed_under_old) |
| 242 |
.enumerate() |
| 243 |
.map(|(i, ((table, row_id, _), data))| { |
| 244 |
pull_wire(device_id, i as i64 + 1, table, row_id, data, 1) |
| 245 |
}) |
| 246 |
.collect(); |
| 247 |
kit.post(PULL_PATH) |
| 248 |
.json(json!({ |
| 249 |
"changes": wire, |
| 250 |
"cursor": rows.len(), |
| 251 |
"has_more": false, |
| 252 |
})) |
| 253 |
.await; |
| 254 |
|
| 255 |
let (changes, _, _) = client.pull(device_id, 0).await.unwrap(); |
| 256 |
changes |
| 257 |
}; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
let reencrypted = { |
| 262 |
let kit = MockKit::start().await; |
| 263 |
kit.get(KEYS_PATH) |
| 264 |
.json(json!({ |
| 265 |
"encrypted_key": synckit_client::crypto::wrap_master_key(&old_key, ROTATE_PW).unwrap(), |
| 266 |
"key_version": 1, |
| 267 |
"key_id": 1, |
| 268 |
"pending_key": { |
| 269 |
"encrypted_key": synckit_client::crypto::wrap_master_key(&new_key, ROTATE_PW).unwrap(), |
| 270 |
"key_id": 2, |
| 271 |
}, |
| 272 |
})) |
| 273 |
.await; |
| 274 |
kit.post(ROTATE_PATH).json(begin_body(rows.len())).await; |
| 275 |
let entries: Vec<serde_json::Value> = rows |
| 276 |
.iter() |
| 277 |
.zip(&sealed_under_old) |
| 278 |
.enumerate() |
| 279 |
.map(|(i, ((table, row_id, _), data))| { |
| 280 |
json!({ "seq": i as i64 + 1, "table": table, "row_id": row_id, "data": data }) |
| 281 |
}) |
| 282 |
.collect(); |
| 283 |
kit.post(ENTRIES_PATH) |
| 284 |
.once() |
| 285 |
.json(json!({ "entries": entries, "has_more": false })) |
| 286 |
.await; |
| 287 |
kit.post(ENTRIES_PATH) |
| 288 |
.json(json!({ "entries": [], "has_more": false })) |
| 289 |
.await; |
| 290 |
kit.post(BATCH_PATH) |
| 291 |
.json(json!({ "updated_count": rows.len() })) |
| 292 |
.await; |
| 293 |
kit.post(COMPLETE_PATH).empty().await; |
| 294 |
|
| 295 |
let client = kit.authed(); |
| 296 |
client.set_master_key_raw(old_key); |
| 297 |
client |
| 298 |
.rotate_key(device_id, ROTATE_PW) |
| 299 |
.await |
| 300 |
.expect("the rotation should complete"); |
| 301 |
|
| 302 |
|
| 303 |
let body = kit.body(BATCH_PATH).await; |
| 304 |
let entries = body["entries"] |
| 305 |
.as_array() |
| 306 |
.expect("batch body should carry entries") |
| 307 |
.clone(); |
| 308 |
assert_eq!( |
| 309 |
entries.len(), |
| 310 |
rows.len(), |
| 311 |
"the re-encrypted batch dropped a row before the pull below could see it" |
| 312 |
); |
| 313 |
entries |
| 314 |
}; |
| 315 |
|
| 316 |
|
| 317 |
let after = { |
| 318 |
let kit = MockKit::start().await; |
| 319 |
let client = kit.authed(); |
| 320 |
client.set_master_key_raw(new_key); |
| 321 |
let wire: Vec<serde_json::Value> = reencrypted |
| 322 |
.iter() |
| 323 |
.map(|entry| { |
| 324 |
let seq = entry["seq"].as_i64().expect("batch entry keeps its seq"); |
| 325 |
let (table, row_id, _) = &rows[seq as usize - 1]; |
| 326 |
pull_wire(device_id, seq, table, row_id, &entry["data"], 2) |
| 327 |
}) |
| 328 |
.collect(); |
| 329 |
kit.post(PULL_PATH) |
| 330 |
.json(json!({ |
| 331 |
"changes": wire, |
| 332 |
"cursor": rows.len(), |
| 333 |
"has_more": false, |
| 334 |
})) |
| 335 |
.await; |
| 336 |
|
| 337 |
let (changes, _, _) = client.pull(device_id, 0).await.unwrap(); |
| 338 |
changes |
| 339 |
}; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
assert_eq!( |
| 344 |
before.len(), |
| 345 |
rows.len(), |
| 346 |
"the pre-rotation pull delivered nothing, so the comparison below is vacuous" |
| 347 |
); |
| 348 |
assert!( |
| 349 |
before.iter().all(|c| c.data.is_some()), |
| 350 |
"the pre-rotation pull returned a row with no payload" |
| 351 |
); |
| 352 |
assert_eq!( |
| 353 |
before.len(), |
| 354 |
after.len(), |
| 355 |
"the rotation changed how many changes a pull returns: {} vs {}", |
| 356 |
before.len(), |
| 357 |
after.len() |
| 358 |
); |
| 359 |
for (a, b) in before.iter().zip(after.iter()) { |
| 360 |
assert_eq!( |
| 361 |
a.row_id, b.row_id, |
| 362 |
"the rotation reordered or dropped a row" |
| 363 |
); |
| 364 |
assert_eq!(a.table, b.table); |
| 365 |
assert_eq!(a.data, b.data, "the rotation changed a decrypted payload"); |
| 366 |
assert_eq!(a.hlc, b.hlc, "the rotation changed a row's clock"); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
fn paged_rows() -> [Vec<(&'static str, &'static str, serde_json::Value)>; 2] { |
| 377 |
[ |
| 378 |
vec![ |
| 379 |
("tasks", "row-1", json!({"title": "first page, first row"})), |
| 380 |
("tasks", "row-2", json!({"title": "first page, second row"})), |
| 381 |
], |
| 382 |
vec![ |
| 383 |
("notes", "row-3", json!({"body": "second page, first row"})), |
| 384 |
("notes", "row-4", json!({"body": "second page, second row"})), |
| 385 |
], |
| 386 |
] |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
fn entries_page( |
| 392 |
old_key: &[u8; 32], |
| 393 |
rows: &[(&'static str, &'static str, serde_json::Value)], |
| 394 |
first_seq: i64, |
| 395 |
has_more: bool, |
| 396 |
) -> serde_json::Value { |
| 397 |
let entries: Vec<serde_json::Value> = rows |
| 398 |
.iter() |
| 399 |
.enumerate() |
| 400 |
.map(|(i, (table, row_id, payload))| { |
| 401 |
let ctx = synckit_client::crypto::AeadContext::entry(table, row_id); |
| 402 |
let sealed = synckit_client::crypto::encrypt_json_aad(payload, old_key, &ctx).unwrap(); |
| 403 |
json!({ "seq": first_seq + i as i64, "table": table, "row_id": row_id, "data": sealed }) |
| 404 |
}) |
| 405 |
.collect(); |
| 406 |
json!({ "entries": entries, "has_more": has_more }) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
#[tokio::test] |
| 422 |
async fn a_second_entries_page_is_pulled_re_encrypted_and_pushed() { |
| 423 |
let kit = MockKit::start().await; |
| 424 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 425 |
let new_key = synckit_client::crypto::generate_master_key(); |
| 426 |
assert_ne!( |
| 427 |
old_key, new_key, |
| 428 |
"the two keys must differ or the old-key assertions below prove nothing" |
| 429 |
); |
| 430 |
let [page_one, page_two] = paged_rows(); |
| 431 |
|
| 432 |
kit.get(KEYS_PATH) |
| 433 |
.json(json!({ |
| 434 |
"encrypted_key": synckit_client::crypto::wrap_master_key(&old_key, ROTATE_PW).unwrap(), |
| 435 |
"key_version": 1, |
| 436 |
"key_id": 1, |
| 437 |
"pending_key": { |
| 438 |
"encrypted_key": synckit_client::crypto::wrap_master_key(&new_key, ROTATE_PW).unwrap(), |
| 439 |
"key_id": 2, |
| 440 |
}, |
| 441 |
})) |
| 442 |
.await; |
| 443 |
kit.post(ROTATE_PATH).json(begin_body(4)).await; |
| 444 |
|
| 445 |
|
| 446 |
kit.post(ENTRIES_PATH) |
| 447 |
.once() |
| 448 |
.json(entries_page(&old_key, &page_one, 1, true)) |
| 449 |
.await; |
| 450 |
kit.post(ENTRIES_PATH) |
| 451 |
.once() |
| 452 |
.json(entries_page(&old_key, &page_two, 3, false)) |
| 453 |
.await; |
| 454 |
|
| 455 |
kit.post(ENTRIES_PATH) |
| 456 |
.json(json!({ "entries": [], "has_more": false })) |
| 457 |
.await; |
| 458 |
kit.post(BATCH_PATH) |
| 459 |
.json(json!({ "updated_count": 2 })) |
| 460 |
.await; |
| 461 |
kit.post(COMPLETE_PATH).empty().await; |
| 462 |
|
| 463 |
kit.authed() |
| 464 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 465 |
.await |
| 466 |
.expect("a two-page rotation should complete"); |
| 467 |
|
| 468 |
assert_eq!( |
| 469 |
kit.hits(ENTRIES_PATH).await, |
| 470 |
2, |
| 471 |
"one pull per page: has_more on page one must run a second round, and page two must end it" |
| 472 |
); |
| 473 |
assert_eq!( |
| 474 |
kit.hits(BATCH_PATH).await, |
| 475 |
2, |
| 476 |
"each page is pushed back as its own batch" |
| 477 |
); |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
let pushed: Vec<serde_json::Value> = kit |
| 482 |
.bodies("POST", BATCH_PATH) |
| 483 |
.await |
| 484 |
.iter() |
| 485 |
.flat_map(|body| { |
| 486 |
body["entries"] |
| 487 |
.as_array() |
| 488 |
.expect("a batch body carries entries") |
| 489 |
.clone() |
| 490 |
}) |
| 491 |
.collect(); |
| 492 |
let all_rows: Vec<_> = page_one.iter().chain(page_two.iter()).collect(); |
| 493 |
assert_eq!( |
| 494 |
pushed.len(), |
| 495 |
all_rows.len(), |
| 496 |
"every row from both pages must come back re-encrypted" |
| 497 |
); |
| 498 |
|
| 499 |
for (i, (entry, (table, row_id, payload))) in pushed.iter().zip(&all_rows).enumerate() { |
| 500 |
let seq = i as i64 + 1; |
| 501 |
assert_eq!( |
| 502 |
entry["seq"].as_i64(), |
| 503 |
Some(seq), |
| 504 |
"row {row_id} came back under the wrong seq" |
| 505 |
); |
| 506 |
let ctx = synckit_client::crypto::AeadContext::entry(table, row_id); |
| 507 |
let opened = synckit_client::crypto::decrypt_json_aad(&entry["data"], &new_key, &ctx) |
| 508 |
.unwrap_or_else(|e| panic!("row {row_id} does not open under the new key: {e}")); |
| 509 |
assert_eq!( |
| 510 |
opened, *payload, |
| 511 |
"row {row_id} came back holding a different payload" |
| 512 |
); |
| 513 |
|
| 514 |
|
| 515 |
assert!( |
| 516 |
synckit_client::crypto::decrypt_json_aad(&entry["data"], &old_key, &ctx).is_err(), |
| 517 |
"row {row_id} still opens under the OLD key, so it was never re-encrypted" |
| 518 |
); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
const MAX_ROTATION_ROUNDS: usize = 100_000; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
struct Counted { |
| 537 |
hits: std::sync::Arc<std::sync::atomic::AtomicUsize>, |
| 538 |
code: u16, |
| 539 |
body: serde_json::Value, |
| 540 |
} |
| 541 |
|
| 542 |
impl wiremock::Respond for Counted { |
| 543 |
fn respond(&self, _request: &wiremock::Request) -> ResponseTemplate { |
| 544 |
self.hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 545 |
ResponseTemplate::new(self.code).set_body_json(self.body.clone()) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
#[tokio::test] |
| 560 |
async fn a_server_that_reports_stragglers_forever_stops_at_the_round_cap() { |
| 561 |
use std::sync::Arc; |
| 562 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 563 |
|
| 564 |
ensure_crypto_provider(); |
| 565 |
ensure_mock_keystore(); |
| 566 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 567 |
|
| 568 |
|
| 569 |
let server = wiremock::MockServer::builder() |
| 570 |
.disable_request_recording() |
| 571 |
.start() |
| 572 |
.await; |
| 573 |
|
| 574 |
let completes = Arc::new(AtomicUsize::new(0)); |
| 575 |
let entries_pulls = Arc::new(AtomicUsize::new(0)); |
| 576 |
|
| 577 |
wiremock::Mock::given(wiremock::matchers::method("GET")) |
| 578 |
.and(wiremock::matchers::path(KEYS_PATH)) |
| 579 |
.respond_with(ResponseTemplate::new(200).set_body_json(get_keys_body(&old_key))) |
| 580 |
.mount(&server) |
| 581 |
.await; |
| 582 |
wiremock::Mock::given(wiremock::matchers::method("POST")) |
| 583 |
.and(wiremock::matchers::path(ROTATE_PATH)) |
| 584 |
.respond_with(ResponseTemplate::new(200).set_body_json(begin_body(0))) |
| 585 |
.mount(&server) |
| 586 |
.await; |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
wiremock::Mock::given(wiremock::matchers::method("POST")) |
| 591 |
.and(wiremock::matchers::path(ENTRIES_PATH)) |
| 592 |
.respond_with(Counted { |
| 593 |
hits: Arc::clone(&entries_pulls), |
| 594 |
code: 200, |
| 595 |
body: json!({ "entries": [], "has_more": false }), |
| 596 |
}) |
| 597 |
.mount(&server) |
| 598 |
.await; |
| 599 |
|
| 600 |
wiremock::Mock::given(wiremock::matchers::method("POST")) |
| 601 |
.and(wiremock::matchers::path(COMPLETE_PATH)) |
| 602 |
.respond_with(Counted { |
| 603 |
hits: Arc::clone(&completes), |
| 604 |
code: 409, |
| 605 |
body: json!({ "message": "stragglers" }), |
| 606 |
}) |
| 607 |
.mount(&server) |
| 608 |
.await; |
| 609 |
|
| 610 |
let client = SyncKitClient::new(SyncKitConfig { |
| 611 |
server_url: server.uri(), |
| 612 |
api_key: "test-api-key".to_string(), |
| 613 |
}); |
| 614 |
let (user_id, app_id) = test_ids(); |
| 615 |
client.restore_session(&fresh_token(), user_id, app_id); |
| 616 |
|
| 617 |
let err = client |
| 618 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 619 |
.await |
| 620 |
.expect_err("a server that never converges must not be waited on forever"); |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
match &err { |
| 625 |
SyncKitError::Internal(msg) => assert!( |
| 626 |
msg.contains("server kept reporting stragglers past the round cap"), |
| 627 |
"wrong give-up path: {msg}" |
| 628 |
), |
| 629 |
other => panic!("expected the round-cap Internal error, got {other:?}"), |
| 630 |
} |
| 631 |
|
| 632 |
assert_eq!( |
| 633 |
completes.load(Ordering::Relaxed), |
| 634 |
MAX_ROTATION_ROUNDS, |
| 635 |
"the cap must fire on the round that reaches it: one completion attempt per round, no more and no fewer" |
| 636 |
); |
| 637 |
|
| 638 |
|
| 639 |
assert_eq!( |
| 640 |
entries_pulls.load(Ordering::Relaxed), |
| 641 |
MAX_ROTATION_ROUNDS, |
| 642 |
"one initial re-encrypt pass plus one per straggler round short of the cap" |
| 643 |
); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
#[cfg(feature = "keychain")] |
| 663 |
fn keychain_entry(app_id: AppId, user_id: UserId) -> keyring_core::Entry { |
| 664 |
keyring_core::Entry::new(&format!("synckit:{app_id}"), &user_id.to_string()) |
| 665 |
.expect("the mock store builds an entry") |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
#[cfg(feature = "keychain")] |
| 674 |
fn client_with_own_keychain(kit: &MockKit, n: u128) -> (SyncKitClient, AppId, UserId) { |
| 675 |
let app_id = AppId::new(Uuid::from_u128(n)); |
| 676 |
let user_id = UserId::new(Uuid::from_u128(n + 1000)); |
| 677 |
let client = kit.client(); |
| 678 |
client.restore_session(&fresh_token(), user_id, app_id); |
| 679 |
(client, app_id, user_id) |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
#[cfg(feature = "keychain")] |
| 685 |
async fn mount_resumed_rotation(kit: &MockKit, old_key: &[u8; 32], new_key: &[u8; 32]) { |
| 686 |
kit.get(KEYS_PATH) |
| 687 |
.json(json!({ |
| 688 |
"encrypted_key": synckit_client::crypto::wrap_master_key(old_key, ROTATE_PW).unwrap(), |
| 689 |
"key_version": 1, |
| 690 |
"key_id": 1, |
| 691 |
"pending_key": { |
| 692 |
"encrypted_key": synckit_client::crypto::wrap_master_key(new_key, ROTATE_PW).unwrap(), |
| 693 |
"key_id": 2, |
| 694 |
}, |
| 695 |
})) |
| 696 |
.await; |
| 697 |
kit.post(ROTATE_PATH).json(begin_body(0)).await; |
| 698 |
kit.post(ENTRIES_PATH) |
| 699 |
.json(json!({ "entries": [], "has_more": false })) |
| 700 |
.await; |
| 701 |
kit.post(COMPLETE_PATH).empty().await; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
#[cfg(feature = "keychain")] |
| 716 |
#[tokio::test] |
| 717 |
async fn a_rotation_that_cannot_cache_the_new_key_drops_the_stale_one() { |
| 718 |
let kit = MockKit::start().await; |
| 719 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 720 |
let new_key = synckit_client::crypto::generate_master_key(); |
| 721 |
mount_resumed_rotation(&kit, &old_key, &new_key).await; |
| 722 |
|
| 723 |
let (client, app_id, user_id) = client_with_own_keychain(&kit, 9_001); |
| 724 |
synckit_client::keystore::store_key(app_id, user_id, &old_key) |
| 725 |
.expect("seed the keychain with the pre-rotation key"); |
| 726 |
|
| 727 |
|
| 728 |
let entry = keychain_entry(app_id, user_id); |
| 729 |
let cred: &keyring_core::mock::Cred = entry |
| 730 |
.as_any() |
| 731 |
.downcast_ref() |
| 732 |
.expect("the mock store yields mock credentials"); |
| 733 |
cred.set_error(keyring_core::Error::NoStorageAccess(Box::new( |
| 734 |
std::io::Error::other("keychain is locked"), |
| 735 |
))); |
| 736 |
|
| 737 |
client |
| 738 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 739 |
.await |
| 740 |
.expect("a failed cache write must not fail the rotation: the key itself is fine"); |
| 741 |
|
| 742 |
match keychain_entry(app_id, user_id).get_password() { |
| 743 |
Err(keyring_core::Error::NoEntry) => {} |
| 744 |
Ok(held) => { |
| 745 |
let stale = base64::engine::general_purpose::STANDARD.encode(old_key); |
| 746 |
assert_ne!( |
| 747 |
held, stale, |
| 748 |
"the keychain still holds the PRE-ROTATION key: a cold launch would load it and decrypt nothing" |
| 749 |
); |
| 750 |
panic!("the stale entry was not dropped; it holds an unexpected value instead"); |
| 751 |
} |
| 752 |
Err(e) => panic!("unexpected keychain error: {e}"), |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
#[cfg(feature = "keychain")] |
| 764 |
#[tokio::test] |
| 765 |
async fn a_rotation_that_caches_the_new_key_keeps_it() { |
| 766 |
let kit = MockKit::start().await; |
| 767 |
let old_key = synckit_client::crypto::generate_master_key(); |
| 768 |
let new_key = synckit_client::crypto::generate_master_key(); |
| 769 |
assert_ne!( |
| 770 |
old_key, new_key, |
| 771 |
"the two keys must differ or the assertion below proves nothing" |
| 772 |
); |
| 773 |
mount_resumed_rotation(&kit, &old_key, &new_key).await; |
| 774 |
|
| 775 |
let (client, app_id, user_id) = client_with_own_keychain(&kit, 9_002); |
| 776 |
synckit_client::keystore::store_key(app_id, user_id, &old_key) |
| 777 |
.expect("seed the keychain with the pre-rotation key"); |
| 778 |
|
| 779 |
client |
| 780 |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW) |
| 781 |
.await |
| 782 |
.expect("the rotation should complete"); |
| 783 |
|
| 784 |
let held = keychain_entry(app_id, user_id) |
| 785 |
.get_password() |
| 786 |
.expect("the new key must still be cached after a successful rotation"); |
| 787 |
assert_eq!( |
| 788 |
held, |
| 789 |
base64::engine::general_purpose::STANDARD.encode(new_key), |
| 790 |
"the keychain does not hold the post-rotation key" |
| 791 |
); |
| 792 |
} |
| 793 |
|