| 2859 |
2859 |
|
.unwrap();
|
| 2860 |
2860 |
|
assert_eq!(cursor, 42);
|
| 2861 |
2861 |
|
}
|
|
2862 |
+ |
|
|
2863 |
+ |
// ── SSE subscribe / reconnect state machine ──
|
|
2864 |
+ |
//
|
|
2865 |
+ |
// These drive the real `subscribe()` -> `SyncNotifyStream::next_change()` path
|
|
2866 |
+ |
// against wiremock so the reconnect state machine (subscribe.rs) is exercised
|
|
2867 |
+ |
// end to end: a live notification, a transparent reconnect across a stream
|
|
2868 |
+ |
// drop, the fatal auth-rejection exit, and the give-up-after-N-failures cap.
|
|
2869 |
+ |
|
|
2870 |
+ |
const SUBSCRIBE_PATH: &str = "/api/v1/sync/subscribe";
|
|
2871 |
+ |
|
|
2872 |
+ |
/// One complete SSE "changed" block, body-terminated so the client sees a full
|
|
2873 |
+ |
/// event and then end-of-stream.
|
|
2874 |
+ |
fn sse_changed_block() -> &'static str {
|
|
2875 |
+ |
"event: changed\n\n"
|
|
2876 |
+ |
}
|
|
2877 |
+ |
|
|
2878 |
+ |
#[tokio::test]
|
|
2879 |
+ |
async fn subscribe_yields_changed_event() {
|
|
2880 |
+ |
let server = MockServer::start().await;
|
|
2881 |
+ |
Mock::given(method("GET"))
|
|
2882 |
+ |
.and(path(SUBSCRIBE_PATH))
|
|
2883 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_string(sse_changed_block()))
|
|
2884 |
+ |
.mount(&server)
|
|
2885 |
+ |
.await;
|
|
2886 |
+ |
|
|
2887 |
+ |
let client = authed_client(&server);
|
|
2888 |
+ |
let mut stream = client.subscribe().await.expect("subscribe should succeed");
|
|
2889 |
+ |
|
|
2890 |
+ |
// The first block is delivered inside the initial response body, so this
|
|
2891 |
+ |
// returns without any reconnect.
|
|
2892 |
+ |
assert_eq!(stream.next_change().await, Some(()));
|
|
2893 |
+ |
}
|
|
2894 |
+ |
|
|
2895 |
+ |
#[tokio::test]
|
|
2896 |
+ |
async fn subscribe_reconnects_transparently_after_stream_drop() {
|
|
2897 |
+ |
let server = MockServer::start().await;
|
|
2898 |
+ |
// Every connection serves one block then ends (Content-Length terminates the
|
|
2899 |
+ |
// body). Consuming the first event, then reading past it, drops the stream
|
|
2900 |
+ |
// and forces a reconnect that must transparently yield the next event.
|
|
2901 |
+ |
Mock::given(method("GET"))
|
|
2902 |
+ |
.and(path(SUBSCRIBE_PATH))
|
|
2903 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_string(sse_changed_block()))
|
|
2904 |
+ |
.mount(&server)
|
|
2905 |
+ |
.await;
|
|
2906 |
+ |
|
|
2907 |
+ |
let client = authed_client(&server);
|
|
2908 |
+ |
let mut stream = client.subscribe().await.expect("subscribe should succeed");
|
|
2909 |
+ |
|
|
2910 |
+ |
// #1 comes from the initial connection; #2 can only arrive after the stream
|
|
2911 |
+ |
// drops (body EOF) and reconnect() re-establishes it.
|
|
2912 |
+ |
assert_eq!(stream.next_change().await, Some(()));
|
|
2913 |
+ |
assert_eq!(stream.next_change().await, Some(()));
|
|
2914 |
+ |
|
|
2915 |
+ |
// subscribe() opened one connection; the second event required at least one
|
|
2916 |
+ |
// reconnect, so the server saw two or more subscribe requests.
|
|
2917 |
+ |
let hits = server
|
|
2918 |
+ |
.received_requests()
|
|
2919 |
+ |
.await
|
|
2920 |
+ |
.unwrap()
|
|
2921 |
+ |
.into_iter()
|
|
2922 |
+ |
.filter(|r| r.url.path() == SUBSCRIBE_PATH)
|
|
2923 |
+ |
.count();
|
|
2924 |
+ |
assert!(hits >= 2, "expected a reconnect (>=2 subscribe requests), got {hits}");
|
|
2925 |
+ |
}
|
|
2926 |
+ |
|
|
2927 |
+ |
#[tokio::test]
|
|
2928 |
+ |
async fn subscribe_stream_closes_on_auth_rejection() {
|
|
2929 |
+ |
let server = MockServer::start().await;
|
|
2930 |
+ |
// First connection opens cleanly but carries no event and ends immediately,
|
|
2931 |
+ |
// forcing a reconnect. The reconnect is rejected for auth -> fatal, so the
|
|
2932 |
+ |
// stream ends with `None` rather than retrying.
|
|
2933 |
+ |
Mock::given(method("GET"))
|
|
2934 |
+ |
.and(path(SUBSCRIBE_PATH))
|
|
2935 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_string(""))
|
|
2936 |
+ |
.up_to_n_times(1)
|
|
2937 |
+ |
.mount(&server)
|
|
2938 |
+ |
.await;
|
|
2939 |
+ |
Mock::given(method("GET"))
|
|
2940 |
+ |
.and(path(SUBSCRIBE_PATH))
|
|
2941 |
+ |
.respond_with(ResponseTemplate::new(401).set_body_string("unauthorized"))
|
|
2942 |
+ |
.mount(&server)
|
|
2943 |
+ |
.await;
|
|
2944 |
+ |
|
|
2945 |
+ |
let client = authed_client(&server);
|
|
2946 |
+ |
let mut stream = client.subscribe().await.expect("initial subscribe is 200");
|
|
2947 |
+ |
|
|
2948 |
+ |
// Empty body -> EOF -> reconnect -> 401 -> fatal -> None.
|
|
2949 |
+ |
assert_eq!(stream.next_change().await, None);
|
|
2950 |
+ |
}
|
|
2951 |
+ |
|
|
2952 |
+ |
// Note: the "give up after MAX_RECONNECT_ATTEMPTS consecutive failures" path is
|
|
2953 |
+ |
// deliberately not covered end-to-end here. Exercising it against a live mock
|
|
2954 |
+ |
// server would incur the real exponential backoff (growing to a 60s cap, minutes
|
|
2955 |
+ |
// of wall-clock), and `tokio::time::pause()` cannot collapse it: the wiremock
|
|
2956 |
+ |
// server runs on its own runtime, so a paused clock auto-advances past the
|
|
2957 |
+ |
// cross-thread request to the nearest timer and the reconnect never completes.
|
|
2958 |
+ |
// The two pieces of that path are covered separately: the backoff schedule by
|
|
2959 |
+ |
// `reconnect_delay_grows_then_caps` (subscribe.rs), and the fatal-exit mechanics
|
|
2960 |
+ |
// (returning `None` and clearing state) by the auth-rejection test above.
|
|
2961 |
+ |
|
|
2962 |
+ |
// ── End-to-end key-rotation orchestration ──
|
|
2963 |
+ |
//
|
|
2964 |
+ |
// These drive `rotate_key()` through the full server protocol against wiremock:
|
|
2965 |
+ |
// fetch key -> begin -> re-encrypt loop -> complete, plus the straggler retry on
|
|
2966 |
+ |
// a 409. They are gated `#[cfg(not(feature = "keychain"))]` because `rotate_key`
|
|
2967 |
+ |
// finishes by caching the new key with `keystore::store_key`, which hits the OS
|
|
2968 |
+ |
// secret-service under the `keychain` feature and is unavailable on a headless
|
|
2969 |
+ |
// test host. With keychain off, `store_key` is the no-op stub, so the test
|
|
2970 |
+ |
// exercises the orchestration hermetically. Run with:
|
|
2971 |
+ |
// cargo test --no-default-features --features store,testing
|
|
2972 |
+ |
#[cfg(not(feature = "keychain"))]
|
|
2973 |
+ |
mod rotation_e2e {
|
|
2974 |
+ |
use super::*;
|
|
2975 |
+ |
|
|
2976 |
+ |
const KEYS_PATH: &str = "/api/v1/sync/keys";
|
|
2977 |
+ |
const ROTATE_PATH: &str = "/api/v1/sync/keys/rotate";
|
|
2978 |
+ |
const ENTRIES_PATH: &str = "/api/v1/sync/keys/rotate/entries";
|
|
2979 |
+ |
const BATCH_PATH: &str = "/api/v1/sync/keys/rotate/batch";
|
|
2980 |
+ |
const COMPLETE_PATH: &str = "/api/v1/sync/keys/rotate/complete";
|
|
2981 |
+ |
|
|
2982 |
+ |
const ROTATE_PW: &str = "rotate-password";
|
|
2983 |
+ |
|
|
2984 |
+ |
/// A `GET /keys` body wrapping `old_key` under [`ROTATE_PW`], with no rotation
|
|
2985 |
+ |
/// in progress, so `rotate_key` verifies the password and mints a fresh key.
|
|
2986 |
+ |
fn get_keys_body(old_key: &[u8; 32]) -> serde_json::Value {
|
|
2987 |
+ |
let envelope = synckit_client::crypto::wrap_master_key(old_key, ROTATE_PW).unwrap();
|
|
2988 |
+ |
json!({ "encrypted_key": envelope, "key_version": 1, "key_id": 1 })
|
|
2989 |
+ |
}
|
|
2990 |
+ |
|
|
2991 |
+ |
/// One rotation entry: `plaintext` sealed under `old_key` with the same
|
|
2992 |
+ |
/// `(table, row_id)` AAD the client rebinds during re-encryption.
|
|
2993 |
+ |
fn rotation_entry(old_key: &[u8; 32], table: &str, row_id: &str) -> serde_json::Value {
|
|
2994 |
+ |
let ctx = synckit_client::crypto::AeadContext::entry(table, row_id);
|
|
2995 |
+ |
let sealed =
|
|
2996 |
+ |
synckit_client::crypto::encrypt_json_aad(&json!({"title": "secret"}), old_key, &ctx)
|
|
2997 |
+ |
.unwrap();
|
|
2998 |
+ |
json!({ "seq": 1, "table": table, "row_id": row_id, "data": sealed })
|
|
2999 |
+ |
}
|
|
3000 |
+ |
|
|
3001 |
+ |
fn hits(reqs: &[wiremock::Request], p: &str) -> usize {
|
|
3002 |
+ |
reqs.iter().filter(|r| r.url.path() == p).count()
|
|
3003 |
+ |
}
|
|
3004 |
+ |
|
|
3005 |
+ |
#[tokio::test]
|
|
3006 |
+ |
async fn rotate_key_drives_full_orchestration() {
|
|
3007 |
+ |
let server = MockServer::start().await;
|
|
3008 |
+ |
let old_key = synckit_client::crypto::generate_master_key();
|
|
3009 |
+ |
|
|
3010 |
+ |
Mock::given(method("GET"))
|
|
3011 |
+ |
.and(path(KEYS_PATH))
|
|
3012 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(get_keys_body(&old_key)))
|
|
3013 |
+ |
.mount(&server)
|
|
3014 |
+ |
.await;
|
|
3015 |
+ |
Mock::given(method("POST"))
|
|
3016 |
+ |
.and(path(ROTATE_PATH))
|
|
3017 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(
|
|
3018 |
+ |
json!({ "rotation_id": Uuid::new_v4(), "target_seq": 1, "new_key_id": 2 }),
|
|
3019 |
+ |
))
|
|
3020 |
+ |
.mount(&server)
|
|
3021 |
+ |
.await;
|
|
3022 |
+ |
// One batch of work, then drained (has_more = false ends the re-encrypt loop).
|
|
3023 |
+ |
Mock::given(method("POST"))
|
|
3024 |
+ |
.and(path(ENTRIES_PATH))
|
|
3025 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
|
3026 |
+ |
"entries": [rotation_entry(&old_key, "tasks", "r1")],
|
|
3027 |
+ |
"has_more": false
|
|
3028 |
+ |
})))
|
|
3029 |
+ |
.mount(&server)
|
|
3030 |
+ |
.await;
|
|
3031 |
+ |
Mock::given(method("POST"))
|
|
3032 |
+ |
.and(path(BATCH_PATH))
|
|
3033 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "updated_count": 1 })))
|
|
3034 |
+ |
.mount(&server)
|
|
3035 |
+ |
.await;
|
|
3036 |
+ |
Mock::given(method("POST"))
|
|
3037 |
+ |
.and(path(COMPLETE_PATH))
|
|
3038 |
+ |
.respond_with(ResponseTemplate::new(200))
|
|
3039 |
+ |
.mount(&server)
|
|
3040 |
+ |
.await;
|
|
3041 |
+ |
|
|
3042 |
+ |
let client = authed_client(&server);
|
|
3043 |
+ |
client
|
|
3044 |
+ |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW)
|
|
3045 |
+ |
.await
|
|
3046 |
+ |
.expect("full rotation should complete");
|
|
3047 |
+ |
|
|
3048 |
+ |
// Every stage of the protocol was driven, in the right shape.
|
|
3049 |
+ |
let reqs = server.received_requests().await.unwrap();
|
|
3050 |
+ |
assert_eq!(hits(&reqs, KEYS_PATH), 1, "fetched key state once");
|
|
3051 |
+ |
assert_eq!(hits(&reqs, ROTATE_PATH), 1, "began rotation once");
|
|
3052 |
+ |
assert!(hits(&reqs, ENTRIES_PATH) >= 1, "pulled entries to re-encrypt");
|
|
3053 |
+ |
assert_eq!(hits(&reqs, BATCH_PATH), 1, "pushed one re-encrypted batch");
|
|
3054 |
+ |
assert_eq!(hits(&reqs, COMPLETE_PATH), 1, "completed once (no stragglers)");
|
|
3055 |
+ |
}
|
|
3056 |
+ |
|
|
3057 |
+ |
#[tokio::test]
|
|
3058 |
+ |
async fn rotate_key_retries_reencrypt_on_straggler_conflict() {
|
|
3059 |
+ |
let server = MockServer::start().await;
|
|
3060 |
+ |
let old_key = synckit_client::crypto::generate_master_key();
|
|
3061 |
+ |
|
|
3062 |
+ |
Mock::given(method("GET"))
|
|
3063 |
+ |
.and(path(KEYS_PATH))
|
|
3064 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(get_keys_body(&old_key)))
|
|
3065 |
+ |
.mount(&server)
|
|
3066 |
+ |
.await;
|
|
3067 |
+ |
Mock::given(method("POST"))
|
|
3068 |
+ |
.and(path(ROTATE_PATH))
|
|
3069 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(
|
|
3070 |
+ |
json!({ "rotation_id": Uuid::new_v4(), "target_seq": 1, "new_key_id": 2 }),
|
|
3071 |
+ |
))
|
|
3072 |
+ |
.mount(&server)
|
|
3073 |
+ |
.await;
|
|
3074 |
+ |
// First entries pull returns work; every later pull is drained. Mounted in
|
|
3075 |
+ |
// this order so the up_to_n_times(1) mock wins the first call, then the
|
|
3076 |
+ |
// empty-set fallback serves the straggler round's re-pull.
|
|
3077 |
+ |
Mock::given(method("POST"))
|
|
3078 |
+ |
.and(path(ENTRIES_PATH))
|
|
3079 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
|
3080 |
+ |
"entries": [rotation_entry(&old_key, "tasks", "r1")],
|
|
3081 |
+ |
"has_more": false
|
|
3082 |
+ |
})))
|
|
3083 |
+ |
.up_to_n_times(1)
|
|
3084 |
+ |
.mount(&server)
|
|
3085 |
+ |
.await;
|
|
3086 |
+ |
Mock::given(method("POST"))
|
|
3087 |
+ |
.and(path(ENTRIES_PATH))
|
|
3088 |
+ |
.respond_with(
|
|
3089 |
+ |
ResponseTemplate::new(200).set_body_json(json!({ "entries": [], "has_more": false })),
|
|
3090 |
+ |
)
|
|
3091 |
+ |
.mount(&server)
|
|
3092 |
+ |
.await;
|
|
3093 |
+ |
Mock::given(method("POST"))
|
|
3094 |
+ |
.and(path(BATCH_PATH))
|
|
3095 |
+ |
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "updated_count": 1 })))
|
|
3096 |
+ |
.mount(&server)
|
|
3097 |
+ |
.await;
|
|
3098 |
+ |
// First completion reports a straggler (409); the retry then succeeds.
|
|
3099 |
+ |
Mock::given(method("POST"))
|
|
3100 |
+ |
.and(path(COMPLETE_PATH))
|
|
3101 |
+ |
.respond_with(ResponseTemplate::new(409).set_body_json(json!({ "message": "stragglers" })))
|
|
3102 |
+ |
.up_to_n_times(1)
|
|
3103 |
+ |
.mount(&server)
|
|
3104 |
+ |
.await;
|
|
3105 |
+ |
Mock::given(method("POST"))
|
|
3106 |
+ |
.and(path(COMPLETE_PATH))
|
|
3107 |
+ |
.respond_with(ResponseTemplate::new(200))
|
|
3108 |
+ |
.mount(&server)
|
|
3109 |
+ |
.await;
|
|
3110 |
+ |
|
|
3111 |
+ |
let client = authed_client(&server);
|
|
3112 |
+ |
client
|
|
3113 |
+ |
.rotate_key(DeviceId::new(Uuid::new_v4()), ROTATE_PW)
|
|
3114 |
+ |
.await
|
|
3115 |
+ |
.expect("rotation should converge after the straggler retry");
|
|
3116 |
+ |
|
|
3117 |
+ |
// The 409 forced a second completion attempt, and the straggler round
|
|
3118 |
+ |
// re-ran the re-encrypt loop (a second entries pull).
|
|
3119 |
+ |
let reqs = server.received_requests().await.unwrap();
|
|
3120 |
+ |
assert_eq!(hits(&reqs, COMPLETE_PATH), 2, "completed twice: 409 then 200");
|
|
3121 |
+ |
assert!(
|
|
3122 |
+ |
hits(&reqs, ENTRIES_PATH) >= 2,
|
|
3123 |
+ |
"straggler round re-pulled entries"
|
|
3124 |
+ |
);
|
|
3125 |
+ |
}
|
|
3126 |
+ |
}
|