diff --git a/crates/in-web/src/i18n.rs b/crates/in-web/src/i18n.rs index 83e471f..5cd197d 100644 --- a/crates/in-web/src/i18n.rs +++ b/crates/in-web/src/i18n.rs @@ -164,6 +164,13 @@ pub enum Key { // Settings. Settings, Profile, + Connection, + ConnectionIssuer, + ConnectionClientId, + ConnectionStream, + ConnectionConnected, + ConnectionReconnecting, + ConnectionLastEvent, DisplayName, EmailAddress, QuotaUsage, @@ -335,6 +342,13 @@ pub fn t(lang: Lang, key: Key) -> &'static str { Profile => "Profile", DisplayName => "Name", EmailAddress => "Address", + Connection => "Connection", + ConnectionIssuer => "Issuer", + ConnectionClientId => "Client ID", + ConnectionStream => "Directory stream", + ConnectionConnected => "Connected", + ConnectionReconnecting => "Reconnecting…", + ConnectionLastEvent => "Last event", QuotaUsage => "Storage used", QuotaOf => "of", AdminPanel => "Everyone", @@ -499,6 +513,13 @@ pub fn t(lang: Lang, key: Key) -> &'static str { EmailAddress => "Adres", QuotaUsage => "Kullanılan alan", QuotaOf => "/", + Connection => "Bağlantı", + ConnectionIssuer => "Sağlayıcı", + ConnectionClientId => "İstemci kimliği", + ConnectionStream => "Dizin akışı", + ConnectionConnected => "Bağlı", + ConnectionReconnecting => "Yeniden bağlanıyor…", + ConnectionLastEvent => "Son etkinlik", AdminPanel => "Herkes", AllUsers => "Herkes", SetQuota => "Kotayı ayarla", diff --git a/crates/in-web/src/main.rs b/crates/in-web/src/main.rs index e3ee440..f5cd589 100644 --- a/crates/in-web/src/main.rs +++ b/crates/in-web/src/main.rs @@ -222,7 +222,13 @@ async fn main() { config.oidc.client_secret.clone(), ); let default_quota_bytes = config.default_quota_bytes; - tokio::spawn(identity_sync(store.clone(), directory.clone(), default_quota_bytes)); + let health = in_web::server::DirectoryHealth::new(); + tokio::spawn(identity_sync( + store.clone(), + directory.clone(), + default_quota_bytes, + health.clone(), + )); // The suite the switcher's wordmarks link to is im's to keep: im's // admin panel owns the list and `/family` serves it to registered @@ -272,6 +278,7 @@ async fn main() { shutdown: in_web::live::Shutdown(stopping), link_key: cookie_key, directory, + health, }) .app_context(in_client::LogoutBack(Arc::new(|cx: &Cx| { Box::pin(async move { Some(in_web::server::share_origin(cx).await) }) @@ -392,6 +399,7 @@ async fn identity_sync( store: Arc, client: im_client::directory::DirectoryClient, default_quota_bytes: u64, + health: in_web::server::DirectoryHealth, ) { let mut backoff = std::time::Duration::from_secs(1); loop { @@ -403,29 +411,45 @@ async fn identity_sync( } match client.open_stream().await { Ok(mut stream) => { + health.connected(); while let Some(event) = stream.next().await { match event { Ok(im_client::directory::DirectoryEvent::Profile(member)) => { mirror_member(&store, &member, default_quota_bytes).await; + // Every event is proof the feed is alive; + // the card's age line reads this. + health.note_event(); } // A network failure ends the stream; the full // pass below re-lists before the redial. Err(problem) => { + health.reconnecting(); eprintln!("in: directory stream failed: {problem}"); break; } } } + // The stream closed cleanly — im restarting or its + // window running out. The card goes amber until the + // redial below opens the next one. + health.reconnecting(); + } + Err(problem) => { + health.reconnecting(); + eprintln!("in: directory stream refused: {problem}") } - Err(problem) => eprintln!("in: directory stream refused: {problem}"), } } - Err(problem) => eprintln!("in: directory pass failed: {problem}"), + Err(problem) => { + health.reconnecting(); + eprintln!("in: directory pass failed: {problem}") + } } tokio::time::sleep(backoff).await; backoff = (backoff * 2).min(IDENTITY_BACKOFF_CAP); } } + /// How often the suite list is re-fetched from im. Short enough that a /// service the admin adds over there shows up here before anyone goes /// looking for its wordmark, long enough that the two are not talking diff --git a/crates/in-web/src/server.rs b/crates/in-web/src/server.rs index 05ee1c6..08d379b 100644 --- a/crates/in-web/src/server.rs +++ b/crates/in-web/src/server.rs @@ -45,6 +45,70 @@ pub struct App { /// change feed — authenticated with the same client pair the OIDC side /// holds. Built once at boot; cheap to clone. pub directory: im_client::directory::DirectoryClient, + /// Whether the directory's live stream is up, read by the settings + /// page's Connection card. Atomics, not locks: the sync task writes + /// from its own loop, every settings render reads, neither ever blocks. + pub health: DirectoryHealth, +} + +/// The live state of the identity-directory connection, as the Connection +/// card displays it. Cloned freely; every clone sees the same atomics. +#[derive(Clone, Debug)] +pub struct DirectoryHealth { + /// 1 while `/directory/live` is open, 0 while connecting or + /// re-connecting — the boot default is 0, and every stream end or + /// error returns it there until the next open. + stream: Arc, + /// Unix seconds of the last roster event the stream delivered; 0 is + /// never. The age a card shows is derived at read time, so a stale + /// moment ages honestly without a ticking task behind it. + last_event: Arc, +} + +impl DirectoryHealth { + pub fn new() -> Self { + Self { + stream: Arc::new(std::sync::atomic::AtomicU8::new(0)), + last_event: Arc::new(std::sync::atomic::AtomicI64::new(0)), + } + } + + /// The stream opened. + pub fn connected(&self) { + use std::sync::atomic::Ordering::Relaxed; + self.stream.store(1, Relaxed); + } + + /// The stream ended or failed; the task will dial again. + pub fn reconnecting(&self) { + use std::sync::atomic::Ordering::Relaxed; + self.stream.store(0, Relaxed); + } + + /// A roster event came through the live stream: it is connected, and + /// this is the moment it last said anything. + pub fn note_event(&self) { + use std::sync::atomic::Ordering::Relaxed; + self.stream.store(1, Relaxed); + self.last_event.store(now_unix(), Relaxed); + } + + /// Whether the card shows the green dot. + pub fn stream_connected(&self) -> bool { + use std::sync::atomic::Ordering::Relaxed; + self.stream.load(Relaxed) == 1 + } + + /// Seconds since the last roster event, or `None` before the first one. + pub fn last_event_age_secs(&self) -> Option { + use std::sync::atomic::Ordering::Relaxed; + let at = self.last_event.load(Relaxed); + (at > 0).then(|| (now_unix() - at).max(0)) + } +} + +fn now_unix() -> i64 { + time::OffsetDateTime::now_utc().unix_timestamp() } /// The application context this request runs under. diff --git a/crates/in-web/src/settings.rs b/crates/in-web/src/settings.rs index a586cc1..bea78b7 100644 --- a/crates/in-web/src/settings.rs +++ b/crates/in-web/src/settings.rs @@ -301,7 +301,13 @@ async fn settings(cx: &Cx) -> Result { } }; let language = lang(cx).await; - let store = app(cx).store; + let app = app(cx); + let store = app.store; + // The Connection card reads only what the config already carries and + // the sync task's own atomics — the client secret is never touched on + // any path that reaches this render. + let connected = app.health.stream_connected(); + let last_event = app.health.last_event_age_secs(); // Re-read the row so the quota bar never shows a stale number. let fresh = store.user(&user.id).await?.unwrap_or_else(|| user.clone()); let administers = fresh.admin; @@ -438,6 +444,38 @@ async fn settings(cx: &Cx) -> Result { + // The Connection card: who this app talks to, and whether + // the live feed is up right now. Read-only, every + // signed-in reader — nothing here is per-user data. +
+
+

(t(language, Key::Connection))

+
+
+
+ (t(language, Key::ConnectionIssuer)) + (app.config.oidc.issuer.clone()) +
+
+ (t(language, Key::ConnectionClientId)) + (app.config.oidc.client_id.clone()) +
+
+ (t(language, Key::ConnectionStream)) + + + if connected { + (t(language, Key::ConnectionConnected)) + } else { + (t(language, Key::ConnectionReconnecting)) + } + if let Some(age) = last_event { + (format!("{} · {}s", t(language, Key::ConnectionLastEvent), age)) + } + +
+
+
} if section == Section::Links { if let Some(token) = created { diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index 1e86074..6eb280a 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -312,6 +312,7 @@ struct TestApp { config: Config, client: in_client::Config, fake: FakeIm, + health: in_web::server::DirectoryHealth, stop: tokio::sync::watch::Sender, } @@ -378,6 +379,7 @@ impl TestApp { defaulted: false, }; let (stop, stopping) = tokio::sync::watch::channel(false); + let health = in_web::server::DirectoryHealth::new(); let router = in_client::mount( Router::builder() .discover() @@ -402,6 +404,7 @@ impl TestApp { "in-test".to_string(), "s3cr3t".to_string(), ), + health: health.clone(), }) .app_context(in_client::LogoutBack(Arc::new(|cx: &Cx| { Box::pin(async move { Some(in_web::server::share_origin(cx).await) }) @@ -414,6 +417,7 @@ impl TestApp { config, client, fake, + health, stop, } } @@ -5293,3 +5297,52 @@ async fn an_empty_folder_says_how_to_fill_it() { assert!(body.contains("id=\"upload-form\""), "{body}"); assert!(body.contains("__inDrop"), "{body}"); } + +/// The settings Connection card reads the shared health the sync task +/// writes: the issuer and the client id straight from the config, the +/// stream wording from the state atomics, and never the client secret — +/// the one value the card must not carry onto any page. +#[tokio::test] +async fn connection_card_shows_issuer_client_id_and_stream_state() { + let app = TestApp::build().await; + let cookie = app.sign_in("sub-conn", "conn@in.test", "Conn").await; + + // A fresh process has never opened the stream: the card is amber. + let page = app.get("/settings", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::OK); + let body = page.text(); + assert!(body.contains("Connection"), "no card title: {body}"); + assert!( + body.contains(app.config.oidc.issuer.as_str()), + "no issuer: {body}" + ); + assert!( + body.contains("in-test"), + "no client id: {body}" + ); + assert!(body.contains("Reconnecting"), "no amber wording: {body}"); + assert!(body.contains("status-dot-warn"), "no amber dot: {body}"); + assert!( + !body.contains("s3cr3t"), + "the client secret reached the page: {body}" + ); + + // The sync task opens the stream: green, and the age line appears once + // an event has come through. + app.health.connected(); + app.health.note_event(); + let page = app.get("/settings", Some(&cookie)).await; + let body = page.text(); + assert!(body.contains("Connected"), "no green wording: {body}"); + assert!(body.contains("status-dot-done"), "no green dot: {body}"); + assert!(!body.contains("Reconnecting"), "amber lingered: {body}"); + assert!(body.contains("Last event"), "no age line: {body}"); + + // The stream ends: amber again, no event line retraction needed — the + // last event stays but the wording names the truth. + app.health.reconnecting(); + let page = app.get("/settings", Some(&cookie)).await; + let body = page.text(); + assert!(body.contains("Reconnecting"), "no amber return: {body}"); + assert!(!body.contains("status-dot-done"), "green lingered: {body}"); +} diff --git a/style/main.scss b/style/main.scss index e0cf0d1..70824eb 100644 --- a/style/main.scss +++ b/style/main.scss @@ -2305,6 +2305,7 @@ img.avatar { } .status-dot-done { background: var(--positive); } +.status-dot-warn { background: var(--warn-ink); } // The select carries no chrome of its own: the box around it is the control. .status-select {