lean-ctx-cloud 3.12.12

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// SPDX-License-Identifier: Apache-2.0
//! The relay's WebSocket with `wss://<cloud>/v1/bus` (bus v2, C0 2.x):
//! reconnects, backoff and the handshake. One connected session is
//! [`session`].
//!
//! The relay connects only once the instance card (E44) has said that the
//! enrolled Cloud speaks C0 2.x (`relay_v2::speaks_v2`); until then it asks
//! for the card again every [`CARD_RETRY`] and waits.

use std::time::Duration;

use tokio_tungstenite::tungstenite::http::HeaderValue;

use crate::CloudError;
use crate::config::CloudConfig;
use crate::state::CloudStore;
use lean_ctx_protocol::cloud::CLOUD_API_PREFIX;

mod session;

const TICK: Duration = Duration::from_secs(2);
/// How often the relay looks for an announce request (`presence::request_announce`).
const WAKE_POLL: Duration = Duration::from_millis(200);
/// How often the relay asks for the instance card while it does not know
/// yet that the enrolled Cloud speaks C0 2.x.
const CARD_RETRY: Duration = Duration::from_secs(60);
/// The first reconnect after a session that was up. A Cloud restart drops
/// every socket at once and presence only returns with the next hello, so
/// the relay comes back within about a second (24.10. prod deploy: the
/// backoff of an earlier outage had kept relays away for ~5 min).
const RECONNECT_FIRST: Duration = Duration::from_secs(1);
/// Random extra wait on every reconnect, so instances do not storm Cloud.
const RECONNECT_JITTER_MS: u64 = 3_000;
const BACKOFF_MAX: Duration = Duration::from_secs(300);

/// The wait before the next connect: [`RECONNECT_FIRST`] after a session
/// that was up, doubled for every connect that failed since, at most
/// [`BACKOFF_MAX`], plus `jitter_ms`.
fn reconnect_delay(failures: u32, jitter_ms: u64) -> Duration {
    let base = RECONNECT_FIRST
        .saturating_mul(1u32 << failures.min(16))
        .min(BACKOFF_MAX);
    base + Duration::from_millis(jitter_ms.min(RECONNECT_JITTER_MS))
}

fn jitter_ms() -> u64 {
    u64::from_le_bytes(rand_nonce()) % (RECONNECT_JITTER_MS + 1)
}

/// `wss://host/v1/bus` (`ws://` only for loopback origins).
fn ws_url(origin: &str) -> String {
    let rest = origin
        .strip_prefix("https://")
        .map(|host| format!("wss://{host}"))
        .or_else(|| {
            origin
                .strip_prefix("http://")
                .map(|host| format!("ws://{host}"))
        })
        .unwrap_or_else(|| origin.to_string());
    format!("{rest}{CLOUD_API_PREFIX}/bus")
}

/// Whether a quota or policy throttle holds bus publishes right now.
fn bus_held(store: &CloudStore) -> bool {
    crate::throttle::holding(
        store,
        crate::throttle::Scope::Bus,
        lean_ctx_time::UtcDateTime::now(),
    )
    .is_some()
}

/// The error a refused WebSocket handshake means. Cloud answers a revoked
/// instance's upgrade with `401 instance_revoked` and its statement (E12),
/// a quota or policy refusal with its own body: the body decides; a 401 or
/// 403 without a contract body keeps its old meaning.
fn handshake_error(error: tokio_tungstenite::tungstenite::Error) -> CloudError {
    match error {
        tokio_tungstenite::tungstenite::Error::Http(response) => {
            let status = response.status().as_u16();
            let body = response
                .body()
                .as_deref()
                .and_then(|b| serde_json::from_slice::<serde_json::Value>(b).ok())
                .unwrap_or(serde_json::Value::Null);
            match CloudError::from_response(status, &body) {
                CloudError::Server { .. } if status == 401 => CloudError::Unauthorized,
                CloudError::Server { .. } if status == 403 => CloudError::Forbidden,
                CloudError::Server { status, code } if status < 500 && status != 429 => {
                    CloudError::Offline(format!("bus: the Cloud answered {status} ({code})"))
                }
                mapped => mapped,
            }
        }
        other => CloudError::Offline(format!(
            "bus: {}",
            other.to_string().chars().take(100).collect::<String>()
        )),
    }
}

/// Errors after which retrying cannot help until the user acts.
fn fatal(error: &CloudError) -> bool {
    matches!(
        error,
        CloudError::NotConnected
            | CloudError::LeaseLapsed
            | CloudError::Revoked(_)
            | CloudError::Forbidden
            | CloudError::Config(_)
            | CloudError::UpgradeRequired { .. }
    )
}

/// The relay ended for good on `error`: a revocation is recorded (every
/// path then fails closed, `revoked.rs`) and named; the line says why, in
/// the words `cloud status` and every CLI path use.
fn stopped(store: &CloudStore, error: CloudError) -> CloudError {
    let error = match (&error, store.instance()) {
        (CloudError::Revoked(_), Ok(Some(instance))) => {
            crate::revoked::note(store, &instance, error)
        }
        _ => error,
    };
    store.record_error(&format!("bus: {error}"));
    tracing::warn!("cloud bus relay stopped: {error}");
    error
}

/// Why a session ended without an error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SessionEnd {
    /// The service closed it.
    Closed,
    /// The subscriptions or the announced workers changed, or Cloud asked
    /// for a resync: the relay reconnects at once with a fresh hello.
    Reannounce,
    /// A bus moved to another place or epoch and was reset to cursor 0: the
    /// relay reconnects at once so Cloud replays it from its start.
    Replay,
}

/// Reconnect until a fatal error (returned, see [`stopped`]), or until
/// `limit` has passed (`None`). `[cloud]` as it was at start is not kept:
/// every connect reads it again. `report` hears what each session sent
/// and why the relay waits or reconnects (#252: it used to say nothing).
pub(super) async fn run(
    store: CloudStore,
    _started_with: CloudConfig,
    limit: Option<Duration>,
    report: super::Report,
) -> Option<CloudError> {
    let _friction = FrictionEmitter::start(store.clone());
    let deadline = limit.map(|limit| tokio::time::Instant::now() + limit);
    // Connects that failed since the last session that was up.
    let mut failures: u32 = 0;
    // When the instance card is asked for next (now, at the first pass).
    let mut card_due: Option<tokio::time::Instant> = None;
    loop {
        // `[cloud]` is read again before every connect: `bus = "off"` or
        // `enabled = false` pauses the relay without a daemon restart, and
        // switching it back on resumes it.
        let Some(config) = super::relay_config() else {
            report.say(
                "bus relay: the bus is off in [cloud] (bus = \"off\" or enabled = false): nothing is sent until it is on again",
            );
            tokio::select! {
                () = tokio::time::sleep(TICK) => continue,
                () = sleep_until(deadline) => return None,
            }
        };
        // Only a Cloud whose instance card answered speaks the bus: until
        // then ask for the card now and then, and wait.
        if !super::relay_v2::speaks_v2(&store) {
            if card_due.is_none_or(|at| at <= tokio::time::Instant::now()) {
                card_due = Some(tokio::time::Instant::now() + CARD_RETRY);
                let (card_store, card_config) = (store.clone(), config.clone());
                let card = tokio::task::spawn_blocking(move || {
                    super::instance_card(&card_store, &card_config)
                })
                .await
                .unwrap_or_else(|e| Err(CloudError::Local(e.to_string())));
                match card {
                    Err(error) if fatal(&error) => return Some(stopped(&store, error)),
                    Err(error) => {
                        report.say(&format!(
                            "bus relay: the instance card failed ({error}); no socket is opened, asking again in {} s",
                            CARD_RETRY.as_secs()
                        ));
                        store.record_error(&format!("bus: instance card: {error}"));
                    }
                    Ok(()) => {}
                }
                if super::relay_v2::speaks_v2(&store) {
                    continue;
                }
                report.say(&format!(
                    "bus relay: the Cloud's instance card did not confirm bus protocol 2.x: no socket is opened, asking again in {} s",
                    CARD_RETRY.as_secs()
                ));
            }
            tokio::select! {
                () = tokio::time::sleep(TICK) => continue,
                () = sleep_until(deadline) => return None,
            }
        }
        let mut connected = false;
        let session = tokio::select! {
            result = session::session_once(&store, &config, &mut connected, &report) => result,
            () = sleep_until(deadline) => return None,
        };
        failures = if connected {
            0
        } else {
            failures.saturating_add(1)
        };
        let delay = reconnect_delay(failures, jitter_ms());
        match session {
            // A fresh hello is due: reconnect right away.
            Ok(SessionEnd::Reannounce) => {
                report.say("bus relay: announcing this machine's agents afresh");
                continue;
            }
            Ok(SessionEnd::Replay) => {
                report.say("bus relay: a bus moved to a new place or epoch; replaying it from the start");
                continue;
            }
            Ok(SessionEnd::Closed) => report.say(&format!(
                "bus relay: the Cloud closed the socket (its workers left the bus); reconnecting in {} s",
                delay.as_secs()
            )),
            Err(error) if fatal(&error) => return Some(stopped(&store, error)),
            Err(error) => {
                report.say(&format!(
                    "bus relay: {error}; trying again in {} s",
                    delay.as_secs()
                ));
                store.record_error(&format!("bus: {error}"));
            }
        }
        tokio::select! {
            () = tokio::time::sleep(delay) => {}
            () = sleep_until(deadline) => return None,
        }
    }
}

async fn sleep_until(deadline: Option<tokio::time::Instant>) {
    match deadline {
        Some(at) => tokio::time::sleep_until(at).await,
        None => std::future::pending().await,
    }
}

/// How often the relay looks for closed hours to report (V16.1).
const FRICTION_EVERY: Duration = Duration::from_secs(60);

/// Samples merge conflicts and queues the collaboration findings of each
/// closed hour while the relay runs (`core::cloud::friction`) and stops
/// with it.
struct FrictionEmitter(tokio::task::JoinHandle<()>);

impl FrictionEmitter {
    fn start(store: CloudStore) -> Self {
        Self(tokio::spawn(async move {
            let mut every = tokio::time::interval(FRICTION_EVERY);
            let mut conflicted = std::collections::BTreeSet::new();
            loop {
                every.tick().await;
                let store = store.clone();
                let mut seen = std::mem::take(&mut conflicted);
                conflicted = tokio::task::spawn_blocking(move || {
                    crate::friction::sample_merge_conflicts(&store, &mut seen);
                    crate::friction::emit_due(&store, lean_ctx_time::UtcDateTime::now());
                    seen
                })
                .await
                .unwrap_or_default();
            }
        }))
    }
}

impl Drop for FrictionEmitter {
    fn drop(&mut self) {
        self.0.abort();
    }
}

fn header(value: &str) -> Result<HeaderValue, CloudError> {
    HeaderValue::from_str(value).map_err(|_| CloudError::Local("header value".into()))
}

type Socket =
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;

/// The bus WebSocket. Its TLS client comes from the process-default rustls
/// provider, which must be named first (two providers are compiled in).
async fn open_socket(
    request: tokio_tungstenite::tungstenite::handshake::client::Request,
) -> Result<
    (
        Socket,
        tokio_tungstenite::tungstenite::handshake::client::Response,
    ),
    tokio_tungstenite::tungstenite::Error,
> {
    lean_ctx_ext::core::crypto_provider::install_default();
    tokio_tungstenite::connect_async(request).await
}

/// Eight random bytes for the reconnect jitter (zeros if the OS refuses).
fn rand_nonce() -> [u8; 8] {
    let mut bytes = [0u8; 8];
    let _ = getrandom::fill(&mut bytes);
    bytes
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio_tungstenite::tungstenite::client::IntoClientRequest as _;

    const PROVIDER_CHILD: &str = "LEAN_CTX_TEST_WS_PROVIDER_CHILD";

    /// A revoked engine's relay stops instead of reconnecting forever.
    #[test]
    fn a_revocation_ends_the_relay() {
        assert!(fatal(&CloudError::Revoked(crate::Revocation::default())));
        assert!(!fatal(&CloudError::Offline("reset".into())));
    }

    const REVOKED_BODY: &str = include_str!(
        "../../../../../crates/lean-ctx-protocol/tests/fixtures/cloud-v2/routes/instance_revoked.error.json"
    );

    fn refused_upgrade(status: u16, body: Option<&str>) -> CloudError {
        let response = tokio_tungstenite::tungstenite::http::Response::builder()
            .status(status)
            .body(body.map(|b| b.as_bytes().to_vec()))
            .unwrap();
        handshake_error(tokio_tungstenite::tungstenite::Error::Http(Box::new(
            response,
        )))
    }

    /// Cloud answers a revoked instance's upgrade with `401
    /// instance_revoked`: the relay reads the body and stops at once,
    /// instead of taking it for an expired token and reconnecting.
    #[test]
    fn a_refused_upgrade_is_read_by_its_body() {
        let revoked = refused_upgrade(401, Some(REVOKED_BODY));
        assert!(
            matches!(&revoked, CloudError::Revoked(r) if r.reason.as_deref() == Some("key_compromise"))
        );
        assert!(fatal(&revoked));
        assert_eq!(refused_upgrade(401, None), CloudError::Unauthorized);
        assert_eq!(refused_upgrade(403, Some("<html>")), CloudError::Forbidden);
        let denied = refused_upgrade(
            403,
            Some(
                r#"{"error":{"code":"policy_denied","message":"x","member":"sharing.allowed_vaults","level":"org"}}"#,
            ),
        );
        assert!(matches!(denied, CloudError::PolicyDenied(_)));
        assert!(
            !fatal(&denied),
            "a policy may change: the relay keeps reconnecting with its backoff"
        );
        let busy = refused_upgrade(503, None);
        assert!(busy.is_transient() && !fatal(&busy));
        let quota = refused_upgrade(
            429,
            Some(
                r#"{"error":{"code":"quota_exceeded","message":"x","resource":"transfer_bytes","retry_after_s":60}}"#,
            ),
        );
        assert!(quota.is_transient() && !fatal(&quota));
        assert!(matches!(refused_upgrade(404, None), CloudError::Offline(_)));
    }

    fn revoked_store() -> (tempfile::TempDir, CloudStore, CloudConfig) {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("c"), dir.path().join("d"));
        let instance = crate::state::InstanceRecord {
            instance_id: "inst_8e3c0d5f2b4e4f609b8c1d2e3f4a5b6c".into(),
            org_id: "4b6d8f0a-2c4e-4f6a-8b0c-2e4f6a8c0e1a".into(),
            org_name: "acme".into(),
            label: "laptop".into(),
            url: lean_ctx_protocol::cloud::CLOUD_DEFAULT_URL.into(),
            org_salt_id: "s".into(),
            enrolled_at: "2026-10-25T10:00:00Z".into(),
            web_url: None,
            org_kind: None,
            company_name: None,
        };
        store
            .write_config(crate::state::INSTANCE_FILE, &instance)
            .unwrap();
        let config = CloudConfig {
            root_key: Some("BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc=".into()),
            ..CloudConfig::default()
        };
        (dir, store, config)
    }

    const REVOKED_LINE: &str = "this instance was revoked by acme (key compromise); reconnect with \
         `lean-ctx cloud connect` after an admin allows it";

    /// A revocation frame ends the session; the relay records it (every
    /// path then fails closed) and says so in the CLI's own words.
    #[test]
    fn a_stopped_relay_records_the_revocation_and_names_it() {
        let (_dir, store, _) = revoked_store();
        let shown = stopped(&store, refused_upgrade(401, Some(REVOKED_BODY)));
        assert_eq!(shown.to_string(), REVOKED_LINE);
        let instance = store.instance().unwrap().unwrap();
        assert_eq!(
            crate::revoked::recorded(&store, &instance),
            Some(shown.clone())
        );
        assert_eq!(
            store.activity().last_error.as_deref(),
            Some(format!("bus: {REVOKED_LINE}").as_str())
        );
    }

    /// Once recorded, the relay stops at its first connect, without
    /// waiting out its deadline and without any network call.
    #[test]
    fn a_recorded_revocation_stops_the_relay_at_once() {
        let (_dir, store, config) = revoked_store();
        let _ = stopped(&store, refused_upgrade(401, Some(REVOKED_BODY)));
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        let started = std::time::Instant::now();
        let end = runtime.block_on(run(
            store,
            config,
            Some(Duration::from_secs(60)),
            crate::bus::Report::quiet(),
        ));
        assert_eq!(end.map(|e| e.to_string()).as_deref(), Some(REVOKED_LINE));
        assert!(
            started.elapsed() < Duration::from_secs(30),
            "no reconnect loop"
        );
    }

    /// The relay's wss:// connect must not panic on the rustls provider. In a
    /// fresh process (this test re-runs itself, so no other test installed a
    /// default first) the TLS handshake against a peer that closes at once
    /// ends in an error, never in "Could not automatically determine the
    /// process-level CryptoProvider".
    #[test]
    fn a_wss_connect_names_the_crypto_provider_itself() {
        if std::env::var_os(PROVIDER_CHILD).is_some() {
            assert!(
                rustls::crypto::CryptoProvider::get_default().is_none(),
                "fresh process"
            );
            let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("listen");
            let port = listener.local_addr().expect("addr").port();
            std::thread::spawn(move || {
                if let Ok((stream, _)) = listener.accept() {
                    drop(stream);
                }
            });
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("runtime");
            let request = format!("wss://127.0.0.1:{port}/v1/bus")
                .into_client_request()
                .expect("request");
            assert!(runtime.block_on(open_socket(request)).is_err());
            return;
        }
        let output = std::process::Command::new(std::env::current_exe().expect("test binary"))
            .args([
                "--exact",
                "bus::ws::tests::a_wss_connect_names_the_crypto_provider_itself",
                "--nocapture",
                "--test-threads=1",
            ])
            .env(PROVIDER_CHILD, "1")
            .output()
            .expect("child test");
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(output.status.success(), "{stderr}");
        assert!(!stderr.contains("CryptoProvider"), "{stderr}");
    }

    /// 24.10. prod deploy: after a Cloud restart, presence came back only
    /// ~5 min later, because a session that ended with an error kept the
    /// backoff of an earlier outage. After a session that was up the relay
    /// reconnects within a second plus at most 3 s of jitter; only failed
    /// connects double the wait, up to the cap.
    #[test]
    fn a_relay_that_was_up_reconnects_within_seconds() {
        assert_eq!(reconnect_delay(0, 0), Duration::from_secs(1));
        assert_eq!(reconnect_delay(0, 3_000), Duration::from_secs(4));
        assert_eq!(
            reconnect_delay(0, 999_999),
            Duration::from_secs(4),
            "jitter is bounded"
        );
        assert_eq!(reconnect_delay(1, 0), Duration::from_secs(2));
        assert_eq!(reconnect_delay(3, 0), Duration::from_secs(8));
        assert_eq!(reconnect_delay(9, 0), BACKOFF_MAX);
        assert_eq!(reconnect_delay(u32::MAX, 0), BACKOFF_MAX);
        for _ in 0..100 {
            assert!(jitter_ms() <= RECONNECT_JITTER_MS);
        }
    }

    #[test]
    fn bus_urls_follow_the_origin_scheme() {
        assert_eq!(
            ws_url("https://cloud.leanctx.com"),
            "wss://cloud.leanctx.com/v1/bus"
        );
        assert_eq!(
            ws_url("http://127.0.0.1:8100"),
            "ws://127.0.0.1:8100/v1/bus"
        );
    }
}