krab_core 0.6.0

Shared runtime for the Krab framework: config, HTTP, database, telemetry, resilience, signals, and protocol governance
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
//! Transport-agnostic service contracts, so one service can call another
//! either in-process or over the network without the caller changing.
//!
//! A contract trait ([`UsersServiceContract`], [`AuthServiceContract`]) is
//! implemented by a local adapter and a remote one; [`TopologyRuntime`]
//! (`KRAB_RUNTIME_TOPOLOGY`, `KRAB_RUNTIME_ENDPOINTS_JSON`) tells the wiring
//! which to use and where remote services live. Failures cross the boundary
//! as [`DomainError`], whatever the transport.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Runtime topology mode used by contract adapter wiring.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ServiceTopology {
    /// Every domain is served in-process; wire local adapters. Parsed from
    /// `monolith`, `single` or `single_service`.
    Monolith,
    /// Domains are separate services; wire remote adapters using
    /// [`TopologyRuntime::endpoints`]. Parsed from `distributed`, `split` or
    /// `split_services`.
    Distributed,
}

impl ServiceTopology {
    /// Parse from env/config string values.
    pub fn parse(value: &str) -> Option<Self> {
        match value.trim().to_ascii_lowercase().as_str() {
            "monolith" | "single" | "single_service" => Some(Self::Monolith),
            "distributed" | "split" | "split_services" => Some(Self::Distributed),
            _ => None,
        }
    }
}

/// Endpoint configuration used by remote adapters.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceEndpoint {
    /// Base URL of the remote service (default `http://127.0.0.1:3000`).
    pub base_url: String,
    /// Per-call timeout, in milliseconds, for the adapter to apply (default
    /// 1500).
    pub timeout_ms: u64,
    /// Retries after the first attempt, for the adapter to apply (default 2).
    pub max_retries: u8,
}

impl Default for ServiceEndpoint {
    fn default() -> Self {
        Self {
            base_url: "http://127.0.0.1:3000".to_string(),
            timeout_ms: 1_500,
            max_retries: 2,
        }
    }
}

/// Topology runtime config used by service adapters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopologyRuntime {
    /// Whether to wire local or remote adapters (default
    /// [`ServiceTopology::Monolith`]).
    pub mode: ServiceTopology,
    /// Domain name => endpoint config
    pub endpoints: HashMap<String, ServiceEndpoint>,
}

impl Default for TopologyRuntime {
    fn default() -> Self {
        Self {
            mode: ServiceTopology::Monolith,
            endpoints: HashMap::new(),
        }
    }
}

impl TopologyRuntime {
    /// Build topology runtime from environment, tolerating invalid values.
    ///
    /// Consumed env vars:
    /// - KRAB_RUNTIME_TOPOLOGY=monolith|distributed
    /// - KRAB_RUNTIME_ENDPOINTS_JSON={"users":{"base_url":"http://127.0.0.1:3002","timeout_ms":1500,"max_retries":2}}
    ///
    /// Malformed values are swallowed with a `tracing::warn!` and replaced by
    /// defaults. Startup paths should prefer [`TopologyRuntime::from_env_checked`],
    /// which surfaces the same conditions as errors.
    pub fn from_env() -> Self {
        let mut out = Self::default();

        if let Ok(raw) = std::env::var("KRAB_RUNTIME_TOPOLOGY") {
            match ServiceTopology::parse(&raw) {
                Some(mode) => out.mode = mode,
                None => tracing::warn!(
                    raw = %raw.trim(),
                    "krab_runtime_topology_unrecognized_falling_back_to_monolith"
                ),
            }
        }

        if let Ok(raw) = std::env::var("KRAB_RUNTIME_ENDPOINTS_JSON") {
            match serde_json::from_str::<HashMap<String, ServiceEndpoint>>(&raw) {
                Ok(endpoints) => out.endpoints = endpoints,
                Err(error) => tracing::warn!(
                    error = %error,
                    "krab_runtime_endpoints_json_unparseable_ignoring"
                ),
            }
        }

        if out.mode == ServiceTopology::Distributed && out.endpoints.is_empty() {
            tracing::warn!("krab_runtime_topology_distributed_with_empty_endpoint_map");
        }

        out
    }

    /// Build topology runtime from environment, rejecting invalid values.
    ///
    /// Reads the same env vars as [`TopologyRuntime::from_env`] but returns an
    /// error when:
    /// - `KRAB_RUNTIME_TOPOLOGY` is set to an unrecognized value,
    /// - `KRAB_RUNTIME_ENDPOINTS_JSON` is set but does not parse, or
    /// - the resolved mode is `distributed`/`split` with an empty endpoint map.
    pub fn from_env_checked() -> anyhow::Result<Self> {
        let mut out = Self::default();

        if let Ok(raw) = std::env::var("KRAB_RUNTIME_TOPOLOGY") {
            out.mode = ServiceTopology::parse(&raw).ok_or_else(|| {
                anyhow::anyhow!(
                    "invalid KRAB_RUNTIME_TOPOLOGY='{}': expected one of monolith|single|single_service|distributed|split|split_services",
                    raw.trim()
                )
            })?;
        }

        if let Ok(raw) = std::env::var("KRAB_RUNTIME_ENDPOINTS_JSON") {
            out.endpoints = serde_json::from_str::<HashMap<String, ServiceEndpoint>>(&raw)
                .map_err(|error| anyhow::anyhow!("invalid KRAB_RUNTIME_ENDPOINTS_JSON: {error}"))?;
        }

        if out.mode == ServiceTopology::Distributed && out.endpoints.is_empty() {
            anyhow::bail!(
                "KRAB_RUNTIME_TOPOLOGY resolves to 'distributed' but the endpoint map is empty; \
                 set KRAB_RUNTIME_ENDPOINTS_JSON with at least one endpoint"
            );
        }

        Ok(out)
    }

    /// The endpoint configured for `domain` (for example `users`), if any.
    pub fn endpoint_for(&self, domain: &str) -> Option<&ServiceEndpoint> {
        self.endpoints.get(domain)
    }
}

/// Stable domain error categories that can be mapped to transport status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DomainErrorKind {
    /// The input was rejected.
    Validation,
    /// The caller is not authenticated.
    Unauthorized,
    /// The caller is authenticated but not allowed.
    Forbidden,
    /// The requested entity does not exist.
    NotFound,
    /// The request conflicts with existing state.
    Conflict,
    /// A downstream call did not finish in time.
    Timeout,
    /// A downstream service could not be reached or failed.
    UpstreamUnavailable,
    /// Any other failure.
    Internal,
}

/// Transport-agnostic domain error payload.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DomainError {
    /// The category, which transports map to a status.
    pub kind: DomainErrorKind,
    /// Machine-readable error code.
    pub code: String,
    /// Human-readable description.
    pub message: String,
}

impl DomainError {
    /// A domain error with the given kind, code and message.
    pub fn new(kind: DomainErrorKind, code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            kind,
            code: code.into(),
            message: message.into(),
        }
    }
}

/// A user as exchanged across the users contract.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UserRecord {
    /// Stable user id.
    pub id: String,
    /// Email address.
    pub email: String,
    /// Name shown in the UI.
    pub display_name: String,
}

/// Input to [`UsersServiceContract::create_user`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NewUserRequest {
    /// Email address of the new user.
    pub email: String,
    /// Name shown in the UI.
    pub display_name: String,
}

/// Transport-agnostic users domain contract.
#[async_trait]
pub trait UsersServiceContract: Send + Sync {
    /// Fetches the user with `id`, under the adapter's own identity — for
    /// service-to-service calls that are not made on behalf of a user.
    async fn get_user(&self, id: &str) -> Result<UserRecord, DomainError>;
    /// Fetches the user with `id` on behalf of the end user whose
    /// `Authorization` header value is `authorization` (`None` when the
    /// request carried none).
    ///
    /// Use this, not [`UsersServiceContract::get_user`], whenever a request
    /// handler serves a caller: a remote adapter must present the caller's
    /// credential to the users service, never its own service identity, or
    /// every caller is answered with data the service account may see (a
    /// confused deputy — `me` resolving to the service account).
    ///
    /// The default ignores `authorization` and calls `get_user`, which is
    /// right for in-process adapters (this process already authenticated the
    /// caller). Remote adapters must override it. Added in 0.6.0.
    async fn get_user_on_behalf_of(
        &self,
        id: &str,
        authorization: Option<&str>,
    ) -> Result<UserRecord, DomainError> {
        let _ = authorization;
        self.get_user(id).await
    }
    /// Creates a user and returns it as stored.
    async fn create_user(&self, request: NewUserRequest) -> Result<UserRecord, DomainError>;
}

/// An issued access token, shaped like an OAuth 2.0 token response.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionToken {
    /// The token itself.
    pub access_token: String,
    /// How to present it, typically `Bearer`.
    pub token_type: String,
    /// Lifetime from issue, in seconds.
    pub expires_in_seconds: u64,
}

/// Transport-agnostic auth domain contract.
#[async_trait]
pub trait AuthServiceContract: Send + Sync {
    /// Issues an access token for `user_id`.
    async fn issue_token(&self, user_id: &str) -> Result<SessionToken, DomainError>;
    /// Verifies `token` and returns the user id it was issued for.
    async fn verify_token(&self, token: &str) -> Result<String, DomainError>;
}

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

    #[test]
    fn topology_parse_is_case_insensitive_and_compatible() {
        assert_eq!(
            ServiceTopology::parse("monolith"),
            Some(ServiceTopology::Monolith)
        );
        assert_eq!(
            ServiceTopology::parse("SINGLE_SERVICE"),
            Some(ServiceTopology::Monolith)
        );
        assert_eq!(
            ServiceTopology::parse("distributed"),
            Some(ServiceTopology::Distributed)
        );
        assert_eq!(
            ServiceTopology::parse("split_services"),
            Some(ServiceTopology::Distributed)
        );
        assert_eq!(ServiceTopology::parse("unknown"), None);
    }

    // Serialized: mutates process-global env vars, which races the other
    // `#[serial]` env-reading suites when run concurrently.
    #[test]
    #[serial_test::serial]
    fn topology_runtime_reads_endpoint_map() {
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "distributed");
        std::env::set_var(
            "KRAB_RUNTIME_ENDPOINTS_JSON",
            r#"{"users":{"base_url":"http://127.0.0.1:3002","timeout_ms":1200,"max_retries":1}}"#,
        );

        let runtime = TopologyRuntime::from_env();
        assert_eq!(runtime.mode, ServiceTopology::Distributed);
        let users = runtime
            .endpoint_for("users")
            .expect("users endpoint missing");
        assert_eq!(users.base_url, "http://127.0.0.1:3002");
        assert_eq!(users.timeout_ms, 1200);
        assert_eq!(users.max_retries, 1);

        std::env::remove_var("KRAB_RUNTIME_TOPOLOGY");
        std::env::remove_var("KRAB_RUNTIME_ENDPOINTS_JSON");
    }

    fn clear_topology_env() {
        std::env::remove_var("KRAB_RUNTIME_TOPOLOGY");
        std::env::remove_var("KRAB_RUNTIME_ENDPOINTS_JSON");
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_accepts_valid_distributed_configuration() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "split");
        std::env::set_var(
            "KRAB_RUNTIME_ENDPOINTS_JSON",
            r#"{"users":{"base_url":"http://127.0.0.1:3002","timeout_ms":1200,"max_retries":1}}"#,
        );

        let runtime = TopologyRuntime::from_env_checked().expect("valid env must parse");
        assert_eq!(runtime.mode, ServiceTopology::Distributed);
        assert!(runtime.endpoint_for("users").is_some());
        clear_topology_env();
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_defaults_to_monolith_when_env_is_absent() {
        clear_topology_env();

        let runtime = TopologyRuntime::from_env_checked().expect("absent env means defaults");
        assert_eq!(runtime.mode, ServiceTopology::Monolith);
        assert!(runtime.endpoints.is_empty());
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_rejects_unrecognized_topology() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "mesh");

        let err = TopologyRuntime::from_env_checked()
            .expect_err("unrecognized topology must be rejected")
            .to_string();
        assert!(
            err.contains("invalid KRAB_RUNTIME_TOPOLOGY='mesh'"),
            "{err}"
        );
        clear_topology_env();
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_rejects_malformed_endpoints_json() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_ENDPOINTS_JSON", "{not json");

        let err = TopologyRuntime::from_env_checked()
            .expect_err("malformed endpoints JSON must be rejected")
            .to_string();
        assert!(err.contains("invalid KRAB_RUNTIME_ENDPOINTS_JSON"), "{err}");
        clear_topology_env();
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_rejects_split_mode_with_empty_endpoint_map() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "split");
        std::env::set_var("KRAB_RUNTIME_ENDPOINTS_JSON", "{}");

        let err = TopologyRuntime::from_env_checked()
            .expect_err("distributed mode with no endpoints must be rejected")
            .to_string();
        assert!(err.contains("endpoint map is empty"), "{err}");
        clear_topology_env();
    }

    #[test]
    #[serial_test::serial]
    fn from_env_checked_rejects_split_mode_with_no_endpoints_var() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "distributed");

        let err = TopologyRuntime::from_env_checked()
            .expect_err("distributed mode with no endpoint env must be rejected")
            .to_string();
        assert!(err.contains("endpoint map is empty"), "{err}");
        clear_topology_env();
    }

    #[test]
    #[serial_test::serial]
    fn lenient_from_env_swallows_malformed_values_with_defaults() {
        clear_topology_env();
        std::env::set_var("KRAB_RUNTIME_TOPOLOGY", "mesh");
        std::env::set_var("KRAB_RUNTIME_ENDPOINTS_JSON", "{not json");

        let runtime = TopologyRuntime::from_env();
        assert_eq!(runtime.mode, ServiceTopology::Monolith);
        assert!(runtime.endpoints.is_empty());
        clear_topology_env();
    }
}