sapphire-framework-server 0.24.1

App server skeleton for sapphire-framework: owns a workspace's cache and serves it over IPC
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
//! The served workspace's API keys (#364): the HTTP layer's verifier.
//!
//! The keys live in the workspace (`<root>/.<app>/api_keys/`, see
//! [`sapphire_framework_keys::store_dir`]), so they sync with it and no bridge is asked.

mod import;

pub use import::{IMPORTED_MARKER, ImportReport, import_external_devices};

use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use sapphire_framework_keys::{
    Authenticated, KeyProfiles, KeyVerifier, Verdict, Verifier, fill_missing_profiles, store_dir,
};
use sapphire_workspace::AppContext;

use crate::selection::SelectionFile;

/// Checks tokens against the keys of the workspace this application serves.
///
/// It finds the served root in the application's selection file (`workspace.toml`, which
/// the server rewrites on a switch), parsing it again only when its text changed — not its
/// modification time, which two writes within one tick share — and keeps one
/// [`KeyVerifier`] — and so one cache — per root. With no workspace
/// served, every token is refused. A key's profile gives its [`Access`]
/// ([`KeyProfiles::standard`] unless [`profiles`](Self::profiles) says otherwise); a key
/// of a profile the application does not offer is [`Verdict::Forbidden`].
///
/// [`Access`]: sapphire_framework_keys::Access
pub struct WorkspaceKeyVerifier {
    app: String,
    file: SelectionFile,
    ttl: Option<Duration>,
    profiles: KeyProfiles,
    state: Mutex<State>,
}

#[derive(Default)]
struct State {
    /// The selection file's text when last parsed (`None` inside: no file); `None` before
    /// the first.
    text: Option<Option<String>>,
    root: Option<PathBuf>,
    verifier: Option<Arc<KeyVerifier>>,
}

impl WorkspaceKeyVerifier {
    /// The verifier for `ctx`'s application, on its own selection file.
    pub fn for_app(ctx: &'static AppContext) -> WorkspaceKeyVerifier {
        WorkspaceKeyVerifier::at(ctx.app_name, SelectionFile::for_app(ctx))
    }

    /// The verifier for `app_name`, on the selection file `file`.
    pub fn at(app_name: &str, file: SelectionFile) -> WorkspaceKeyVerifier {
        WorkspaceKeyVerifier {
            app: app_name.to_owned(),
            file,
            ttl: None,
            profiles: KeyProfiles::standard(),
            state: Mutex::new(State::default()),
        }
    }

    /// The application's own profiles, instead of the framework's three.
    pub fn profiles(mut self, profiles: KeyProfiles) -> WorkspaceKeyVerifier {
        self.profiles = profiles;
        self
    }

    /// Trust a success for `ttl` instead of corundum's default.
    pub fn cache_ttl(mut self, ttl: Duration) -> WorkspaceKeyVerifier {
        self.ttl = Some(ttl);
        self
    }

    /// The verifier for the root served now, if one is.
    fn current(&self) -> Option<Arc<KeyVerifier>> {
        // The file is a few lines; reading it is cheaper than being wrong about a switch.
        let text = match std::fs::read_to_string(self.file.path()) {
            Ok(text) => Some(text),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
            Err(err) => {
                tracing::warn!("could not read the served workspace: {err}");
                return None;
            }
        };
        let mut state = self.state.lock().expect("key verifier state");
        if state.text.as_ref() != Some(&text) {
            state.text = Some(text);
            // The same text is not parsed (nor warned about) again.
            let root = match self.file.load(&self.app) {
                Ok(selection) => selection.map(|s| s.root),
                Err(err) => {
                    tracing::warn!("could not read the served workspace: {err}");
                    None
                }
            };
            if root != state.root {
                state.verifier = root.as_deref().map(|root| {
                    let dir = store_dir(&self.app, root);
                    // Keys from before profiles, which corundum would skip.
                    if let Err(err) = fill_missing_profiles(&dir) {
                        tracing::warn!("could not give the API keys a profile: {err}");
                    }
                    let verifier = KeyVerifier::new(&dir);
                    Arc::new(match self.ttl {
                        Some(ttl) => verifier.cache_ttl(ttl),
                        None => verifier,
                    })
                });
                state.root = root;
            }
        }
        state.verifier.clone()
    }
}

/// Import the workgroup's external devices into the workspace at `root`, once per host
/// (see [`import_external_devices`]). Failures are logged and retried at the next start.
pub(crate) fn import_once(ctx: &AppContext, root: &std::path::Path) {
    let bridge_dir = match std::env::var_os("SAPPHIRE_BRIDGE_DIR").filter(|v| !v.is_empty()) {
        Some(dir) => PathBuf::from(dir),
        None => match sapphire_workspace::dirs::data_dir() {
            Some(dir) => dir.join(sapphire_ipc::BRIDGE_DIR_NAME),
            None => return,
        },
    };
    match import_external_devices(ctx.app_name, ctx.data_dir(), &bridge_dir, root) {
        Ok(ImportReport { imported: 0, .. }) => {}
        Ok(report) => tracing::info!(
            imported = report.imported,
            "the workgroup's external devices are now this workspace's API keys; the workgroup's ledger is no longer read: manage them with `api-key`"
        ),
        Err(err) => tracing::warn!("could not import the external devices: {err}"),
    }
}

#[async_trait::async_trait]
impl Verifier for WorkspaceKeyVerifier {
    async fn verify(&self, token: &str) -> Verdict {
        let Some(verifier) = self.current() else {
            return Verdict::Refused;
        };
        let token = token.to_owned();
        // A miss reads the store's files.
        match tokio::task::spawn_blocking(move || verifier.verify(&token)).await {
            Ok(corundum_framework_keys::Verdict::Accepted(key)) => {
                match self.profiles.access(&key.profile) {
                    Some(access) => Verdict::Accepted(Authenticated {
                        id: key.id,
                        name: key.name,
                        profile: key.profile,
                        access,
                    }),
                    None => Verdict::Forbidden,
                }
            }
            Ok(corundum_framework_keys::Verdict::Refused) => Verdict::Refused,
            Err(err) => {
                tracing::error!("the key check did not finish: {err}");
                Verdict::Refused
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::selection::Selection;
    use sapphire_framework_keys::open_store;

    const APP: &str = "sapphire-keytest";

    fn rw() -> sapphire_framework_keys::ProfileId {
        sapphire_framework_keys::ProfileId::new(sapphire_framework_keys::READ_WRITE).unwrap()
    }

    struct Host {
        _tmp: tempfile::TempDir,
        file: SelectionFile,
        a: PathBuf,
        b: PathBuf,
    }

    fn host() -> Host {
        let tmp = tempfile::tempdir().unwrap();
        let a = tmp.path().join("a");
        let b = tmp.path().join("b");
        std::fs::create_dir_all(&a).unwrap();
        std::fs::create_dir_all(&b).unwrap();
        let file = SelectionFile::at(tmp.path().join("config").join("workspace.toml"));
        Host {
            _tmp: tmp,
            file,
            a,
            b,
        }
    }

    fn select(file: &SelectionFile, root: &std::path::Path) {
        // A switch lands in a later instant than the read before it, as a real one does.
        std::thread::sleep(Duration::from_millis(20));
        file.save(&Selection {
            root: root.to_owned(),
            sync: false,
        })
        .unwrap();
    }

    fn accepted(v: Verdict) -> Option<String> {
        match v {
            Verdict::Accepted(who) => Some(who.name),
            Verdict::Forbidden | Verdict::Refused => None,
        }
    }

    #[tokio::test]
    async fn the_access_is_the_profile_s() {
        let h = host();
        select(&h.file, &h.a);
        let read_only = sapphire_framework_keys::ProfileId::new("read-only").unwrap();
        let (_, token) = open_store(APP, &h.a)
            .unwrap()
            .add("p", None, read_only)
            .unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        let Verdict::Accepted(who) = v.verify(token.expose()).await else {
            panic!("refused");
        };
        assert_eq!(who.profile.as_str(), "read-only");
        assert_eq!(who.access, sapphire_framework_keys::Access::Read);
    }

    #[tokio::test]
    async fn a_key_of_a_profile_the_app_does_not_offer_is_forbidden() {
        let h = host();
        select(&h.file, &h.a);
        let gone = sapphire_framework_keys::ProfileId::new("room-gone").unwrap();
        let (_, token) = open_store(APP, &h.a).unwrap().add("p", None, gone).unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert_eq!(v.verify(token.expose()).await, Verdict::Forbidden);
    }

    #[tokio::test]
    async fn an_app_s_own_profiles_decide() {
        let h = host();
        select(&h.file, &h.a);
        let room = sapphire_framework_keys::ProfileId::new("room-1").unwrap();
        let (_, token) = open_store(APP, &h.a).unwrap().add("p", None, room).unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone()).profiles(
            sapphire_framework_keys::KeyProfiles::new(Vec::new, |id| {
                (id.as_str() == "room-1").then_some(sapphire_framework_keys::Access::Write)
            }),
        );
        let Verdict::Accepted(who) = v.verify(token.expose()).await else {
            panic!("refused");
        };
        assert_eq!(who.access, sapphire_framework_keys::Access::Write);
    }

    // A key file from before profiles, synced into the served workspace: read-only.
    #[tokio::test]
    async fn a_key_without_a_profile_is_read_only() {
        let h = host();
        let (key, token) = open_store(APP, &h.a).unwrap().add("p", None, rw()).unwrap();
        let file = store_dir(APP, &h.a).join(format!("{}.toml", key.id));
        let text = std::fs::read_to_string(&file).unwrap();
        let old: String = text
            .lines()
            .filter(|l| !l.starts_with("profile"))
            .map(|l| format!("{l}\n"))
            .collect();
        std::fs::write(&file, old).unwrap();
        select(&h.file, &h.a);
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        let Verdict::Accepted(who) = v.verify(token.expose()).await else {
            panic!("refused");
        };
        assert_eq!(who.access, sapphire_framework_keys::Access::Read);
    }

    #[tokio::test]
    async fn key_in_workspace_is_accepted() {
        let h = host();
        select(&h.file, &h.a);
        let (_, token) = open_store(APP, &h.a).unwrap().add("p", None, rw()).unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert_eq!(
            accepted(v.verify(token.expose()).await).as_deref(),
            Some("p")
        );
        assert_eq!(accepted(v.verify("sapphire-ak-wrong").await), None);
    }

    #[tokio::test]
    async fn retire_takes_effect_after_ttl() {
        let h = host();
        select(&h.file, &h.a);
        let mut store = open_store(APP, &h.a).unwrap();
        let (_, token) = store.add("p", None, rw()).unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone()).cache_ttl(Duration::from_millis(300));
        assert!(accepted(v.verify(token.expose()).await).is_some());
        store.retire("p").unwrap();
        tokio::time::sleep(Duration::from_millis(500)).await;
        assert_eq!(accepted(v.verify(token.expose()).await), None);
    }

    #[tokio::test]
    async fn copied_key_file_is_accepted() {
        let h = host();
        select(&h.file, &h.a);
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        // Made on another host's copy of the workspace, then synced here.
        let elsewhere = tempfile::tempdir().unwrap();
        let (key, token) = open_store(APP, elsewhere.path())
            .unwrap()
            .add("p", None, rw())
            .unwrap();
        let name = format!("{}.toml", key.id);
        std::fs::create_dir_all(store_dir(APP, &h.a)).unwrap();
        std::fs::copy(
            store_dir(APP, elsewhere.path()).join(&name),
            store_dir(APP, &h.a).join(&name),
        )
        .unwrap();
        assert!(accepted(v.verify(token.expose()).await).is_some());
    }

    #[tokio::test]
    async fn switch_changes_the_keys() {
        let h = host();
        let (_, in_a) = open_store(APP, &h.a).unwrap().add("a", None, rw()).unwrap();
        let (_, in_b) = open_store(APP, &h.b).unwrap().add("b", None, rw()).unwrap();
        select(&h.file, &h.a);
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert!(accepted(v.verify(in_a.expose()).await).is_some());
        select(&h.file, &h.b);
        assert_eq!(
            accepted(v.verify(in_a.expose()).await),
            None,
            "the old workspace's key"
        );
        assert!(accepted(v.verify(in_b.expose()).await).is_some());
    }

    #[tokio::test]
    async fn missing_root_refuses() {
        let h = host();
        let (_, token) = open_store(APP, &h.a).unwrap().add("p", None, rw()).unwrap();
        select(&h.file, &h.a);
        std::fs::remove_dir_all(&h.a).unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert_eq!(accepted(v.verify(token.expose()).await), None);
        assert!(
            !h.a.exists(),
            "the verifier must not recreate the workspace"
        );
    }

    #[tokio::test]
    async fn no_selection_refuses() {
        let h = host();
        let (_, token) = open_store(APP, &h.a).unwrap().add("p", None, rw()).unwrap();
        h.file.clear().unwrap();
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert_eq!(accepted(v.verify(token.expose()).await), None);
    }

    #[tokio::test]
    async fn a_switch_in_the_same_tick_is_seen() {
        let h = host();
        let (_, in_a) = open_store(APP, &h.a).unwrap().add("a", None, rw()).unwrap();
        let (_, in_b) = open_store(APP, &h.b).unwrap().add("b", None, rw()).unwrap();
        let save = |root: &std::path::Path| {
            h.file
                .save(&Selection {
                    root: root.to_owned(),
                    sync: false,
                })
                .unwrap()
        };
        save(&h.a);
        let v = WorkspaceKeyVerifier::at(APP, h.file.clone());
        assert!(accepted(v.verify(in_a.expose()).await).is_some());
        let tick = std::fs::metadata(h.file.path())
            .unwrap()
            .modified()
            .unwrap();
        // Rewritten within the file system's timestamp tick (1 s on some): same mtime.
        save(&h.b);
        std::fs::File::options()
            .write(true)
            .open(h.file.path())
            .unwrap()
            .set_modified(tick)
            .unwrap();
        assert_eq!(
            accepted(v.verify(in_a.expose()).await),
            None,
            "the old workspace's key"
        );
        assert!(accepted(v.verify(in_b.expose()).await).is_some());
    }
}