link-cli 3.1.0

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
//! Sessions of a links server, each with its subject (issue #149).
//!
//! [`LinksSessions`] is what a [`LinksServer`](super::LinksServer) gives
//! every connection, without the network. The C# counterparts are
//! `LinksSessions` and `LinksSession`.

use std::collections::{HashMap, HashSet};
use std::panic::{self, AssertUnwindSafe};

use anyhow::Result;
use links_notation::binary::{format_document, parse_document, LinoDocument};
use links_notation::LiNo;

use super::links_operations::{Change, LinksOperation};
use super::server::{error_document, error_message, execute_observed_request, execute_request};
use super::triplets_operations::{execute_observed_triplets_request, TripletsOperation};
use crate::changes_simplifier::simplify_changes;
use crate::link::Link;
use crate::named_type_links::NamedTypeLinks;
use crate::permissions::{AccessPolicy, PermissionPolicy, PermissionsDecorator};
use crate::query_processor::QueryProcessor;
use crate::triplets::{TripletChange, TripletMarkers, TripletsOverDoublets};
use crate::undoable_links::UndoableLinksDecorator;

/// The subject of a token, or `None` when the token is not valid: answers
/// `(auth: token)`.
///
/// It is given the store the sessions serve, below any permissions, so a
/// [`LinksTokenTable`](crate::LinksTokenTable) kept in the data can read it:
/// `Box::new(|store, token| LinksTokenTable::new(store).authenticate(token))`.
/// An error fails the authentication.
pub type Authenticator = Box<dyn FnMut(&mut dyn NamedTypeLinks, &str) -> Result<Option<u32>>>;

/// A session of a [`LinksSessions`], as [`LinksSessions::open`] returns it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SessionId(u64);

/// What a session keeps between its requests.
#[derive(Debug, Default)]
struct Session {
    subject: u32,
    /// The links the subject created, while they are its own.
    created: HashSet<u32>,
}

enum Store<S: NamedTypeLinks, P: AccessPolicy> {
    Plain(S),
    Permitted(PermissionsDecorator<UndoableLinksDecorator<S>, P>),
}

/// A store served to many sessions, each with its subject (issue #149).
///
/// Without permissions every session sees and changes the whole store,
/// exactly as before permissions existed. With them
/// ([`with_permissions`](Self::with_permissions)), a session sees the store
/// through a [`PermissionsDecorator`] of its subject, and every request is
/// atomic: one that fails is undone whole, the writes the store made on its
/// own (cascades, merges) and the names it took included, even where they
/// touched links the subject cannot see.
///
/// `(auth: token)` sets the subject of the session to the one the
/// [`Authenticator`] gives the token, and answers `(subject: i)`; an unknown
/// token, or sessions without an authenticator, answer
/// `(error: 'Forbidden: authentication failed')`.
///
/// With [`with_triplets`](Self::with_triplets) the sessions read every request
/// as triplets (issue #152; see [`TripletsOperation`]), and permissions judge
/// each triplet as a whole, not the wrappers and pairs that store it.
///
/// Requests run one at a time, as `&mut self` already demands.
pub struct LinksSessions<S: NamedTypeLinks, P: AccessPolicy = PermissionPolicy> {
    store: Store<S, P>,
    processor: QueryProcessor,
    triplets: Option<TripletMarkers>,
    authenticate: Option<Authenticator>,
    subject: u32,
    sessions: HashMap<SessionId, Session>,
    next: u64,
}

impl<S: NamedTypeLinks> LinksSessions<S> {
    /// Sessions that see and change the whole store.
    pub fn new(store: S) -> Self {
        Self::of(Store::Plain(store))
    }
}

impl<S: NamedTypeLinks, P: AccessPolicy> LinksSessions<S, P> {
    /// Sessions that see the store as `policy` lets their subjects.
    pub fn with_permissions(store: S, policy: P) -> Self {
        Self::of(Store::Permitted(PermissionsDecorator::new(
            UndoableLinksDecorator::new(store),
            policy,
            0,
        )))
    }

    fn of(store: Store<S, P>) -> Self {
        Self {
            store,
            processor: QueryProcessor::new(false),
            triplets: None,
            authenticate: None,
            subject: 0,
            sessions: HashMap::new(),
            next: 0,
        }
    }

    /// Answers `(auth: token)` with `authenticate`.
    pub fn with_authenticator(
        mut self,
        authenticate: impl FnMut(&mut dyn NamedTypeLinks, &str) -> Result<Option<u32>> + 'static,
    ) -> Self {
        self.authenticate = Some(Box::new(authenticate));
        self
    }

    /// The subject of a session before any `(auth: token)`; the null link
    /// (0, the default) is a guest.
    pub fn with_subject(mut self, subject: u32) -> Self {
        self.subject = subject;
        self
    }

    /// Passed to [`QueryProcessor::with_auto_create_missing_references`].
    pub fn with_auto_create_missing_references(mut self, enabled: bool) -> Self {
        self.processor = QueryProcessor::new(false).with_auto_create_missing_references(enabled);
        self
    }

    /// Reads every request as triplets; the markers of the encoding are
    /// created in the store when they are missing, whatever the subject of a
    /// session may do.
    pub fn with_triplets(mut self) -> Result<Self> {
        self.ensure_triplets()?;
        Ok(self)
    }

    /// Like [`with_triplets`](Self::with_triplets), on sessions already built.
    pub fn ensure_triplets(&mut self) -> Result<TripletMarkers> {
        if let Some(markers) = self.triplets {
            return Ok(markers);
        }
        let markers = TripletMarkers::ensure(self.store_mut())?;
        self.triplets = Some(markers);
        Ok(markers)
    }

    /// Whether the sessions read every request as triplets.
    pub fn has_triplets(&self) -> bool {
        self.triplets.is_some()
    }

    /// Whether the sessions see the store through permissions.
    pub fn has_permissions(&self) -> bool {
        matches!(self.store, Store::Permitted(_))
    }

    /// The store.
    pub fn store(&self) -> &S {
        match &self.store {
            Store::Plain(store) => store,
            Store::Permitted(view) => view.inner().inner(),
        }
    }

    /// The store, to read or write it as no session.
    pub fn store_mut(&mut self) -> &mut S {
        match &mut self.store {
            Store::Plain(store) => store,
            Store::Permitted(view) => view.inner_mut().inner_mut(),
        }
    }

    /// The policy, when there are permissions.
    pub fn policy(&self) -> Option<&P> {
        match &self.store {
            Store::Plain(_) => None,
            Store::Permitted(view) => Some(view.policy()),
        }
    }

    pub fn into_inner(self) -> S {
        match self.store {
            Store::Plain(store) => store,
            Store::Permitted(view) => view.into_inner().into_inner(),
        }
    }

    /// Opens a session of `subject`, by default the one of
    /// [`with_subject`](Self::with_subject).
    pub fn open(&mut self, subject: Option<u32>) -> SessionId {
        let id = SessionId(self.next);
        self.next += 1;
        self.sessions.insert(
            id,
            Session {
                subject: subject.unwrap_or(self.subject),
                created: HashSet::new(),
            },
        );
        id
    }

    /// Closes a session.
    pub fn close(&mut self, session: SessionId) {
        self.sessions.remove(&session);
    }

    /// The sessions that are open.
    pub fn sessions(&self) -> Vec<SessionId> {
        let mut sessions: Vec<SessionId> = self.sessions.keys().copied().collect();
        sessions.sort();
        sessions
    }

    /// The subject the requests of `session` run as, while it is open.
    pub fn subject(&self, session: SessionId) -> Option<u32> {
        self.sessions.get(&session).map(|session| session.subject)
    }

    /// The links `session` created, while they are its own.
    pub fn created(&self, session: SessionId) -> Option<&HashSet<u32>> {
        self.sessions.get(&session).map(|session| &session.created)
    }

    /// Answers a request written as LiNo text, as the text protocol does.
    pub fn request(&mut self, session: SessionId, text: &str) -> String {
        let parsed = match self.store().query_limits() {
            Some(limits) => super::server::parse_document_with_limits(text, limits),
            None => parse_document(text).map_err(Into::into),
        };
        let reply = match parsed {
            Ok(document) => self.execute(session, &document),
            Err(error) => error_document(&error.to_string()),
        };
        format_document(&reply)
    }

    /// Answers one request document of `session`; a session that is not
    /// open is opened.
    pub fn execute(&mut self, session: SessionId, document: &[LiNo<String>]) -> LinoDocument {
        self.execute_observed(session, document, &mut Vec::new())
    }

    /// Like [`execute`](Self::execute), adding the net change of every link
    /// the request changed in the store to `changed`, with plain numbers:
    /// the cascades and merges a write led to as well, even where the
    /// subject cannot see them. A request undone for failing changed nothing.
    pub fn execute_observed(
        &mut self,
        session: SessionId,
        document: &[LiNo<String>],
        changed: &mut Vec<Change>,
    ) -> LinoDocument {
        self.run(session, document, changed, &mut Vec::new())
    }

    /// Like [`execute`](Self::execute) on sessions that read requests as
    /// triplets, adding every triplet the request changed to `changed`, with
    /// plain numbers: those it took along too, even where the subject cannot
    /// see them. A request undone for failing changed nothing.
    pub fn execute_triplets_observed(
        &mut self,
        session: SessionId,
        document: &[LiNo<String>],
        changed: &mut Vec<TripletChange>,
    ) -> LinoDocument {
        self.run(session, document, &mut Vec::new(), changed)
    }

    fn run(
        &mut self,
        session: SessionId,
        document: &[LiNo<String>],
        changed: &mut Vec<Change>,
        triplets_changed: &mut Vec<TripletChange>,
    ) -> LinoDocument {
        if let Some(limits) = self.store().query_limits() {
            if let Err(error) = super::server::admit_document(document, limits) {
                return error_document(&error.to_string());
            }
        }
        let mut state = self.sessions.remove(&session).unwrap_or(Session {
            subject: self.subject,
            created: HashSet::new(),
        });
        let reply = self.execute_as(&mut state, document, changed, triplets_changed);
        self.sessions.insert(session, state);
        reply
    }

    /// Whether the subject of `session` may read `link`: always, without
    /// permissions; a session that is not open is a guest.
    pub fn can_read(&mut self, session: SessionId, link: &Link) -> bool {
        let view = match &mut self.store {
            Store::Plain(_) => return true,
            Store::Permitted(view) => view,
        };
        let (subject, created) = match self.sessions.get_mut(&session) {
            Some(state) => (state.subject, std::mem::take(&mut state.created)),
            None => (self.subject, HashSet::new()),
        };
        view.set_subject(subject);
        let created = view.replace_created(created);
        let readable = if self.triplets.is_some() {
            view.can_read_triplet(link)
        } else {
            view.can_read(link)
        };
        let created = view.replace_created(created);
        if let Some(state) = self.sessions.get_mut(&session) {
            state.created = created;
        }
        readable
    }

    fn execute_as(
        &mut self,
        session: &mut Session,
        document: &[LiNo<String>],
        changed: &mut Vec<Change>,
        triplets_changed: &mut Vec<TripletChange>,
    ) -> LinoDocument {
        if let [LiNo::Link {
            id: Some(id),
            values,
        }] = document
        {
            if id == "auth" {
                return self.authenticate(session, values);
            }
        }
        let markers = self.triplets;
        let view = match &mut self.store {
            Store::Plain(store) => {
                return match markers {
                    Some(markers) => execute_observed_triplets_request(
                        &mut TripletsOverDoublets::with_markers(store, markers),
                        document,
                        triplets_changed,
                    ),
                    None => execute_observed_request(store, &self.processor, document, changed),
                };
            }
            Store::Permitted(view) => view,
        };
        view.set_subject(session.subject);
        view.replace_created(std::mem::take(&mut session.created));
        view.inner_mut().begin();
        let processor = &self.processor;
        let observed = triplets_changed.len();
        let outcome = panic::catch_unwind(AssertUnwindSafe(|| match markers {
            Some(markers) => triplets_request(view, markers, document, triplets_changed),
            None => request(view, document, |view, document| {
                execute_request(view, processor, document)
            }),
        }));
        let reply = match outcome {
            Ok(reply) if error_message(&reply).is_none() => {
                let deleted: HashSet<u32> = view
                    .inner()
                    .log()
                    .changes()
                    .iter()
                    .filter_map(|change| match change {
                        (Some(before), None) => Some(before.index),
                        _ => None,
                    })
                    .collect();
                let changes = view
                    .inner()
                    .log()
                    .changes()
                    .iter()
                    .map(|(before, after)| {
                        (
                            before.unwrap_or_else(Link::null),
                            after.unwrap_or_else(Link::null),
                        )
                    })
                    .collect();
                changed.extend(
                    simplify_changes(changes)
                        .into_iter()
                        .filter(|(before, after)| before != after),
                );
                view.inner_mut().commit();
                // The links are gone: no other session owns them any more.
                for other in self.sessions.values_mut() {
                    other.created.retain(|index| !deleted.contains(index));
                }
                reply
            }
            Ok(reply) => {
                triplets_changed.truncate(observed);
                match undo(view) {
                    Ok(()) => reply,
                    Err(error) => error_document(&format!(
                        "{}; undoing it failed: {error:#}",
                        error_message(&reply).unwrap_or_default()
                    )),
                }
            }
            Err(panicked) => {
                let _ = undo(view);
                session.created = view.replace_created(HashSet::new());
                panic::resume_unwind(panicked);
            }
        };
        session.created = view.replace_created(HashSet::new());
        reply
    }

    fn authenticate(&mut self, session: &mut Session, values: &[LiNo<String>]) -> LinoDocument {
        let found = match (values, self.authenticate.as_mut()) {
            ([LiNo::Ref(token)], Some(authenticate)) => {
                let store: &mut dyn NamedTypeLinks = match &mut self.store {
                    Store::Plain(store) => store,
                    Store::Permitted(view) => view.inner_mut().inner_mut(),
                };
                authenticate(store, token).ok().flatten()
            }
            _ => None,
        };
        match found {
            Some(subject) => {
                session.subject = subject;
                vec![LiNo::Link {
                    id: Some("subject".to_string()),
                    values: vec![LiNo::Ref(subject.to_string())],
                }]
            }
            None => error_document("Forbidden: authentication failed"),
        }
    }
}

/// Undoes the request, and forgets the links it created.
fn undo<S: NamedTypeLinks, P: AccessPolicy>(
    view: &mut PermissionsDecorator<UndoableLinksDecorator<S>, P>,
) -> Result<()> {
    for created in view.inner_mut().rollback()? {
        view.forget(created);
    }
    Ok(())
}

/// A request as the subject sees the store: names of links it cannot read
/// are not found, and `run` answers everything else.
fn request<L: NamedTypeLinks, P: AccessPolicy>(
    view: &mut PermissionsDecorator<L, P>,
    document: &[LiNo<String>],
    run: impl FnOnce(&mut PermissionsDecorator<L, P>, &[LiNo<String>]) -> LinoDocument,
) -> LinoDocument {
    let operation = match LinksOperation::from_document(document) {
        Ok(operation) => operation,
        Err(error) => return error_document(&error.to_string()),
    };
    match operation {
        Some(LinksOperation::GetName(link)) if !view.exists(link) => Vec::new(),
        Some(LinksOperation::GetByName(name)) => match view.get_by_name(&name) {
            Ok(Some(link)) if link != 0 && view.exists(link) => run(view, document),
            Ok(_) => Vec::new(),
            Err(error) => error_document(&format!("{error:#}")),
        },
        _ => run(view, document),
    }
}

/// A request over triplets as the subject sees the store: the permissions move
/// above the triplets, which write their wrappers and pairs straight to the
/// store below, still inside the transaction of the request.
fn triplets_request<S: NamedTypeLinks, P: AccessPolicy>(
    view: &mut PermissionsDecorator<UndoableLinksDecorator<S>, P>,
    markers: TripletMarkers,
    document: &[LiNo<String>],
    changed: &mut Vec<TripletChange>,
) -> LinoDocument {
    let subject = view.subject();
    let created = view.replace_created(HashSet::new());
    let (links, policy) = view.parts_mut();
    let mut layer = PermissionsDecorator::new(
        TripletsOverDoublets::with_markers(links, markers),
        policy,
        subject,
    )
    .with_created(created);
    // A triplet operation holds four parts where one over doublets holds three:
    // only what is left, the names, is for `request` to look at.
    let reply = if matches!(TripletsOperation::from_document(document), Ok(None)) {
        request(&mut layer, document, |layer, document| {
            execute_observed_triplets_request(layer, document, changed)
        })
    } else {
        execute_observed_triplets_request(&mut layer, document, changed)
    };
    let created = layer.replace_created(HashSet::new());
    view.replace_created(created);
    reply
}