distributed 4.4.2

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
Documentation
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
//! Causal wait-path host used by GraphQL. Local in-process or HTTP loopback.

use async_trait::async_trait;
use reqwest::redirect::Policy;
use serde_json::Value;
use std::sync::Arc;
use std::time::Duration;

use crate::graphql::identity::VerifiedPrincipal;
use crate::graphql::protocol::ProtocolResponseAccumulator;
use crate::microsvc::cell_host::{
    InternalHttpSecret, CELL_INTERNAL_SECRET_HEADER, CELL_PRINCIPAL_PARTITION_HEADER,
    CELL_SERVICE_ID_HEADER,
};
use crate::microsvc::{
    CausalCommandPublicStatus, CausalDispatchError, CausalDispatchResult, Service, Session,
    ROLE_KEY, USER_ID_KEY,
};

/// Wait-path command host. GraphQL mutations call this instead of `Service`.
#[async_trait]
pub trait CommandHost: Send + Sync {
    async fn invoke(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalDispatchResult, CausalDispatchError>;

    async fn status(
        &self,
        command_id: &str,
        session: &Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalCommandPublicStatus, CausalDispatchError>;
}

pub type SharedCommandHost = Arc<dyn CommandHost>;

pub(crate) fn validate_principal_session(
    session: &Session,
    principal: &VerifiedPrincipal,
) -> Result<(), CausalDispatchError> {
    let subject = session
        .user_id()
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .ok_or_else(|| CausalDispatchError::Rejected {
            code: "UNAUTHORIZED",
            status: 401,
            message: "durable commands require an authenticated session subject".into(),
        })?;
    if !principal.subject_matches(subject) {
        return Err(CausalDispatchError::Rejected {
            code: "UNAUTHORIZED",
            status: 401,
            message: "session subject does not match the verified principal".into(),
        });
    }
    Ok(())
}

pub(crate) fn validate_principal_session_if_present(
    session: &Session,
    principal: &VerifiedPrincipal,
) -> Result<(), CausalDispatchError> {
    match session
        .user_id()
        .map(str::trim)
        .filter(|value| !value.is_empty())
    {
        Some(subject) if principal.subject_matches(subject) => Ok(()),
        Some(_) => Err(CausalDispatchError::Rejected {
            code: "UNAUTHORIZED",
            status: 401,
            message: "session subject does not match the verified principal".into(),
        }),
        None => Ok(()),
    }
}

/// In-process host wrapping a writer [`Service`].
pub struct LocalCommandHost {
    service: Arc<Service>,
}

impl LocalCommandHost {
    pub fn new(service: Arc<Service>) -> Self {
        Self { service }
    }

    pub fn service(&self) -> &Arc<Service> {
        &self.service
    }
}

#[async_trait]
impl CommandHost for LocalCommandHost {
    async fn invoke(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalDispatchResult, CausalDispatchError> {
        validate_principal_session(&session, &principal)?;
        match protocol {
            Some(protocol) => {
                self.service
                    .dispatch_causal_with_receipt_and_protocol(
                        command, command_id, input, session, principal, protocol,
                    )
                    .await
            }
            None => {
                self.service
                    .dispatch_causal_with_receipt(command, command_id, input, session, principal)
                    .await
            }
        }
    }

    async fn status(
        &self,
        command_id: &str,
        session: &Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalCommandPublicStatus, CausalDispatchError> {
        validate_principal_session_if_present(session, &principal)?;
        match protocol {
            Some(protocol) => {
                self.service
                    .causal_command_status_with_protocol(command_id, session, principal, protocol)
                    .await
            }
            None => {
                self.service
                    .causal_command_status(command_id, session, principal)
                    .await
            }
        }
    }
}

/// HTTP wait-path client (`POST {base}/{command}` with `{ commandId, input }`).
#[derive(Clone)]
pub struct HttpCommandHost {
    base: reqwest::Url,
    client: reqwest::Client,
    internal_secret: Option<InternalHttpSecret>,
}

impl HttpCommandHost {
    const MAX_JSON_BYTES: usize = 2 * 1024 * 1024;

    pub fn new(base: impl AsRef<str>) -> Result<Self, CausalDispatchError> {
        Self::build(base.as_ref(), None)
    }

    pub fn new_internal(
        base: impl AsRef<str>,
        secret: InternalHttpSecret,
    ) -> Result<Self, CausalDispatchError> {
        Self::build(base.as_ref(), Some(secret))
    }

    fn build(
        base: &str,
        internal_secret: Option<InternalHttpSecret>,
    ) -> Result<Self, CausalDispatchError> {
        let base = reqwest::Url::parse(base.trim_end_matches('/')).map_err(|error| {
            CausalDispatchError::Internal(format!("invalid wait-path base URL: {error}"))
        })?;
        if !matches!(base.scheme(), "http" | "https")
            || !base.username().is_empty()
            || base.password().is_some()
            || base.query().is_some()
            || base.fragment().is_some()
            || base.host_str().is_none()
        {
            return Err(CausalDispatchError::Internal(
                "wait-path base URL must be http(s) with a host and without credentials, query, or fragment".into(),
            ));
        }
        let client = reqwest::Client::builder()
            .connect_timeout(Duration::from_secs(2))
            .timeout(Duration::from_secs(10))
            .redirect(Policy::none())
            .build()
            .map_err(|error| {
                CausalDispatchError::Internal(format!("wait-path HTTP client: {error}"))
            })?;
        Ok(Self {
            base,
            client,
            internal_secret,
        })
    }

    /// Same connection pool, different wait-path base (`{celld}/{shard}`).
    /// `Client::new()` loads TLS roots; do not construct one per command.
    pub fn retarget_segments(&self, segments: &[&str]) -> Result<Self, CausalDispatchError> {
        let mut base = self.base.clone();
        {
            let mut path = base.path_segments_mut().map_err(|_| {
                CausalDispatchError::Internal(
                    "wait-path base URL cannot contain path segments".into(),
                )
            })?;
            path.pop_if_empty();
            for segment in segments {
                if segment.is_empty() {
                    return Err(CausalDispatchError::Internal(
                        "wait-path URL segment must not be empty".into(),
                    ));
                }
                path.push(segment);
            }
        }
        Ok(Self {
            base,
            client: self.client.clone(),
            internal_secret: self.internal_secret.clone(),
        })
    }

    pub fn base(&self) -> &str {
        self.base.as_str()
    }

    fn request_url(&self, segment: &str) -> Result<reqwest::Url, CausalDispatchError> {
        if segment.is_empty() {
            return Err(CausalDispatchError::Internal(
                "wait-path URL segment must not be empty".into(),
            ));
        }
        let mut url = self.base.clone();
        url.path_segments_mut()
            .map_err(|_| {
                CausalDispatchError::Internal("wait-path URL cannot contain path segments".into())
            })?
            .pop_if_empty()
            .push(segment);
        Ok(url)
    }

    fn request_json(
        &self,
        segment: &str,
        body: &Value,
    ) -> Result<reqwest::RequestBuilder, CausalDispatchError> {
        let encoded = serde_json::to_vec(body).map_err(|error| {
            CausalDispatchError::Internal(format!("wait-path request JSON: {error}"))
        })?;
        if encoded.len() > Self::MAX_JSON_BYTES {
            return Err(CausalDispatchError::BadRequest(
                "wait-path request exceeds 2 MiB".into(),
            ));
        }
        let mut request = self
            .client
            .post(self.request_url(segment)?)
            .header(reqwest::header::CONTENT_TYPE, "application/json")
            .body(encoded);
        if let Some(secret) = &self.internal_secret {
            request = request.header(CELL_INTERNAL_SECRET_HEADER, secret.header_value());
        }
        Ok(request)
    }

    async fn response_json(
        mut response: reqwest::Response,
    ) -> Result<(u16, Value), CausalDispatchError> {
        let status = response.status().as_u16();
        if response
            .content_length()
            .is_some_and(|length| length > Self::MAX_JSON_BYTES as u64)
        {
            return Err(CausalDispatchError::Internal(
                "wait-path response exceeds 2 MiB".into(),
            ));
        }
        let mut bytes = Vec::new();
        while let Some(chunk) = response.chunk().await.map_err(|error| {
            CausalDispatchError::Internal(format!("wait-path HTTP body: {error}"))
        })? {
            if bytes.len().saturating_add(chunk.len()) > Self::MAX_JSON_BYTES {
                return Err(CausalDispatchError::Internal(
                    "wait-path response exceeds 2 MiB".into(),
                ));
            }
            bytes.extend_from_slice(&chunk);
        }
        let body = serde_json::from_slice(&bytes).map_err(|error| {
            CausalDispatchError::Internal(format!("wait-path HTTP body is not valid JSON: {error}"))
        })?;
        Ok((status, body))
    }

    /// POST `{base}/{path}` with a JSON body (cell `outbox.complete`, alarms).
    pub async fn post_json(
        &self,
        path: &str,
        body: Value,
    ) -> Result<(u16, Value), CausalDispatchError> {
        let response = self
            .request_json(path, &body)?
            .send()
            .await
            .map_err(|err| CausalDispatchError::Internal(format!("cell HTTP failed: {err}")))?;
        Self::response_json(response).await
    }

    /// Authenticated GET of one encoded path segment with the same transport bounds.
    pub async fn get_json(&self, path_segment: &str) -> Result<(u16, Value), CausalDispatchError> {
        let mut request = self.client.get(self.request_url(path_segment)?);
        if let Some(secret) = &self.internal_secret {
            request = request.header(CELL_INTERNAL_SECRET_HEADER, secret.header_value());
        }
        let response = request
            .send()
            .await
            .map_err(|error| CausalDispatchError::Internal(format!("cell HTTP failed: {error}")))?;
        Self::response_json(response).await
    }

    /// POST `{base}/{command}` and return status + JSON, including 4xx with
    /// cell `outbox` for drain retries.
    pub async fn post_wait_path(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: &Session,
    ) -> Result<(u16, Value), CausalDispatchError> {
        self.post_wait_path_inner(command, command_id, input, session, None)
            .await
    }

    /// POST a cell wait-path command with identity derived by the verified
    /// GraphQL host. These headers are part of the trusted internal boundary,
    /// not values copied from public request headers or command input.
    pub async fn post_cell_wait_path(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: &Session,
        service_id: &str,
        principal_partition: &str,
    ) -> Result<(u16, Value), CausalDispatchError> {
        self.post_wait_path_inner(
            command,
            command_id,
            input,
            session,
            Some((service_id, principal_partition)),
        )
        .await
    }

    async fn post_wait_path_inner(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: &Session,
        cell_identity: Option<(&str, &str)>,
    ) -> Result<(u16, Value), CausalDispatchError> {
        let mut request = self.request_json(
            command,
            &serde_json::json!({
                "commandId": command_id,
                "input": input,
            }),
        )?;
        if let Some(user) = session.user_id() {
            request = request.header(USER_ID_KEY, user);
        }
        if let Some(roles) = session.get(ROLE_KEY) {
            request = request.header(ROLE_KEY, roles);
        }
        if let Some((service_id, principal_partition)) = cell_identity {
            if self.internal_secret.is_none() {
                return Err(CausalDispatchError::Internal(
                    "cell wait-path requires an internal HTTP secret".into(),
                ));
            }
            request = request
                .header(CELL_SERVICE_ID_HEADER, service_id)
                .header(CELL_PRINCIPAL_PARTITION_HEADER, principal_partition);
        }
        let response = request.send().await.map_err(|err| {
            CausalDispatchError::Internal(format!("wait-path HTTP failed: {err}"))
        })?;
        Self::response_json(response).await
    }
}

#[async_trait]
impl CommandHost for HttpCommandHost {
    async fn invoke(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: Session,
        principal: VerifiedPrincipal,
        _protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalDispatchResult, CausalDispatchError> {
        validate_principal_session(&session, &principal)?;
        let (status, body) = self
            .post_wait_path(command, command_id, input, &session)
            .await?;
        if status >= 400 {
            let message = body
                .get("error")
                .and_then(Value::as_str)
                .unwrap_or("wait-path rejected")
                .to_string();
            return Err(CausalDispatchError::Rejected {
                code: "REJECTED",
                status,
                message,
            });
        }
        CausalDispatchResult::from_wait_path_wire(body)
    }

    async fn status(
        &self,
        command_id: &str,
        session: &Session,
        principal: VerifiedPrincipal,
        _protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalCommandPublicStatus, CausalDispatchError> {
        validate_principal_session_if_present(session, &principal)?;
        Ok(CausalCommandPublicStatus::unknown(command_id))
    }
}

/// Local dispatcher is a causal [`CommandHost`]. GraphQL must use this
/// trait object, not [`LocalCommandDispatcher::service`].
#[async_trait]
impl CommandHost for super::LocalCommandDispatcher {
    async fn invoke(
        &self,
        command: &str,
        command_id: &str,
        input: Value,
        session: Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalDispatchResult, CausalDispatchError> {
        LocalCommandHost::new(Arc::clone(self.service()))
            .invoke(command, command_id, input, session, principal, protocol)
            .await
    }

    async fn status(
        &self,
        command_id: &str,
        session: &Session,
        principal: VerifiedPrincipal,
        protocol: Option<ProtocolResponseAccumulator>,
    ) -> Result<CausalCommandPublicStatus, CausalDispatchError> {
        LocalCommandHost::new(Arc::clone(self.service()))
            .status(command_id, session, principal, protocol)
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn http_host_rejects_unsafe_base_urls() {
        assert!(HttpCommandHost::new("ftp://writer.example").is_err());
        assert!(HttpCommandHost::new("https://user:pass@writer.example").is_err());
        assert!(HttpCommandHost::new("https://writer.example/path?secret=value").is_err());
        assert!(HttpCommandHost::new("https://writer.example/path#fragment").is_err());
        assert!(HttpCommandHost::new("https://writer.example/path").is_ok());
    }

    #[test]
    fn verified_principal_must_match_any_session_subject() {
        let principal =
            VerifiedPrincipal::test_oidc("https://issuer.example", "alice", &["distributed-tests"]);
        let mut session = Session::new();
        session.set(USER_ID_KEY, "mallory");
        assert!(validate_principal_session(&session, &principal).is_err());
        assert!(validate_principal_session_if_present(&session, &principal).is_err());

        session.set(USER_ID_KEY, "alice");
        assert!(validate_principal_session(&session, &principal).is_ok());
    }
}