vcon-server 0.4.14

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
532
533
534
535
536
537
538
539
540
//! The policy of every route for every role, generated from the one
//! table of roles to scopes (`crate::roles`): a role reaches a route if
//! and only if its scopes hold the scope the route needs.
//!
//! `POLICY` states what each route needs. It is held to the description
//! (`openapi.json`), which the unit tests hold to the router, so a route
//! added without its line here fails. Each role is then given a key
//! holding exactly the role's scopes, the form a membership's scopes take
//! until sessions exist, and every route is called with it: the answer is
//! 403 where the table says the role does not reach the route, and is not
//! 403 where it says it does.

use std::collections::BTreeSet;

use reqwest::Method;

use super::tests::pipeline::{Server, server};
use crate::access::{Scope, Scopes};
use crate::audit::Actor;
use crate::roles::{Role, Roles};

/// What a route asks of its caller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Needs {
    /// A key holding this scope.
    Scope(Scope),
    /// Any valid key, of a tenant or the operator.
    AnyKey,
    /// Nothing: the route takes no key.
    NoKey,
    /// A session's cookie: the route takes no key, whatever its scopes.
    Session,
}

const POLICY: &[(&str, &str, Needs)] = &[
    ("POST", "/vcons", Needs::Scope(Scope::Write)),
    ("GET", "/vcons", Needs::Scope(Scope::Read)),
    ("GET", "/vcons/{uuid}", Needs::Scope(Scope::Read)),
    ("PUT", "/vcons/{uuid}", Needs::Scope(Scope::Write)),
    ("DELETE", "/vcons/{uuid}", Needs::Scope(Scope::Delete)),
    ("GET", "/health", Needs::AnyKey),
    ("GET", "/openapi.json", Needs::AnyKey),
    ("POST", "/mcp", Needs::AnyKey),
    ("GET", "/key", Needs::AnyKey),
    ("GET", "/console", Needs::NoKey),
    ("GET", "/console/", Needs::NoKey),
    ("GET", "/console/console.js", Needs::NoKey),
    ("GET", "/console/console.css", Needs::NoKey),
    ("GET", "/console/format.js", Needs::NoKey),
    ("GET", "/console/player.html", Needs::NoKey),
    ("GET", "/console/player.js", Needs::NoKey),
    ("POST", "/console/csp-report", Needs::NoKey),
    ("GET", "/console/{other}", Needs::NoKey),
    ("POST", "/console/{other}", Needs::NoKey),
    ("PUT", "/console/{other}", Needs::NoKey),
    ("PATCH", "/console/{other}", Needs::NoKey),
    ("DELETE", "/console/{other}", Needs::NoKey),
    ("GET", "/audit", Needs::Scope(Scope::Audit)),
    ("GET", "/admin/audit", Needs::Scope(Scope::Admin)),
    ("GET", "/legal/vcons/{uuid}", Needs::Scope(Scope::Legal)),
    (
        "GET",
        "/legal/vcons/{uuid}/versions",
        Needs::Scope(Scope::Legal),
    ),
    (
        "GET",
        "/legal/vcons/{uuid}/versions/{version}",
        Needs::Scope(Scope::Legal),
    ),
    (
        "POST",
        "/legal/vcons/{uuid}/restore",
        Needs::Scope(Scope::Legal),
    ),
    (
        "PUT",
        "/legal/vcons/{uuid}/hold",
        Needs::Scope(Scope::Legal),
    ),
    (
        "DELETE",
        "/legal/vcons/{uuid}/hold",
        Needs::Scope(Scope::Legal),
    ),
    ("GET", "/admin/tenants", Needs::Scope(Scope::Admin)),
    ("POST", "/admin/tenants", Needs::Scope(Scope::Admin)),
    ("PATCH", "/admin/tenants/{id}", Needs::Scope(Scope::Admin)),
    ("DELETE", "/admin/tenants/{id}", Needs::Scope(Scope::Admin)),
    (
        "PUT",
        "/admin/tenants/{id}/metadata",
        Needs::Scope(Scope::Admin),
    ),
    (
        "GET",
        "/admin/tenants/{id}/keys",
        Needs::Scope(Scope::Admin),
    ),
    (
        "POST",
        "/admin/tenants/{id}/keys",
        Needs::Scope(Scope::Admin),
    ),
    ("DELETE", "/admin/keys/{id}", Needs::Scope(Scope::Admin)),
    ("GET", "/tenant/settings", Needs::Scope(Scope::TenantAdmin)),
    ("PUT", "/tenant/settings", Needs::Scope(Scope::TenantAdmin)),
    ("GET", "/tenant/keys", Needs::Scope(Scope::TenantAdmin)),
    ("POST", "/tenant/keys", Needs::Scope(Scope::TenantAdmin)),
    (
        "DELETE",
        "/tenant/keys/{id}",
        Needs::Scope(Scope::TenantAdmin),
    ),
    (
        "GET",
        "/tenant/memberships",
        Needs::Scope(Scope::TenantAdmin),
    ),
    ("GET", "/session", Needs::Session),
    ("POST", "/session/logout", Needs::Session),
];

fn policy() -> Vec<(&'static str, &'static str, Needs)> {
    POLICY.to_vec()
}

/// Whether a caller holding `scopes` reaches a route that needs `needs`.
fn reaches(scopes: Scopes, needs: Needs) -> bool {
    match needs {
        Needs::Scope(scope) => scopes.contains(scope),
        Needs::AnyKey | Needs::NoKey => true,
        Needs::Session => false,
    }
}

/// A path of the route, with an id no row has in each place.
fn path(template: &str) -> String {
    template
        .replace("{uuid}", "0195a1f0-7c3e-7d4a-8b21-3f5e6a7b8c9d")
        .replace("{version}", "0123456789abcdef0123456789abcdef")
        .replace("/admin/keys/{id}", "/admin/keys/0123456789abcdef")
        .replace("/tenant/keys/{id}", "/tenant/keys/0123456789abcdef")
        .replace("{id}", "00000000000000000000000000000000")
        .replace("{other}", "nothing")
}

/// A key holding exactly `scopes`: an operator key for admin, and a key
/// of the server's tenant for any other set.
fn key(s: &Server, scopes: Scopes) -> String {
    let tenant = (!scopes.contains(Scope::Admin)).then_some(s.tenant);
    s.tenants
        .create_key(Actor::Cli, tenant, scopes, "policy")
        .expect("a key")
        .token
}

async fn status(s: &Server, method: &str, template: &str, token: Option<&str>) -> u16 {
    let method = Method::from_bytes(method.as_bytes()).expect("a method");
    let mut request = s
        .client_not_following()
        .request(method, s.url(&path(template)));
    if let Some(token) = token {
        request = request.bearer_auth(token);
    }
    request.send().await.expect("an answer").status().as_u16()
}

/// Calls every route with a key holding `scopes` and holds each answer to
/// the table.
async fn held_to_the_table(scopes: Scopes, who: &str) {
    let s = server().await;
    let token = key(&s, scopes);
    for (method, template, needs) in policy() {
        let answer = status(&s, method, template, Some(&token)).await;
        if needs == Needs::Session {
            assert_eq!(answer, 401, "{who}: {method} {template} takes no key");
            continue;
        }
        assert_ne!(answer, 401, "{who}: {method} {template} took the key");
        assert_eq!(
            answer != 403,
            reaches(scopes, needs),
            "{who}: {method} {template} needs {needs:?} and answered {answer}"
        );
    }
}

// What must work.

#[test]
fn the_table_names_every_route_the_description_lists() {
    let description: serde_json::Value =
        serde_json::from_str(super::OPENAPI).expect("the description");
    let mut listed = BTreeSet::new();
    for (route, methods) in description["paths"].as_object().expect("paths") {
        for method in methods.as_object().expect("methods").keys() {
            if method != "parameters" {
                listed.insert((method.to_uppercase(), route.clone()));
            }
        }
    }
    let table: BTreeSet<(String, String)> = policy()
        .into_iter()
        .map(|(method, route, _)| (method.to_owned(), route.to_owned()))
        .collect();
    assert_eq!(
        table.len(),
        policy().len(),
        "no route is in the table twice"
    );
    assert_eq!(
        table, listed,
        "the table and openapi.json name the same routes"
    );
}

#[tokio::test]
async fn a_reader_reaches_the_routes_that_need_read() {
    held_to_the_table(Role::Reader.scopes(), "reader").await;
}

#[tokio::test]
async fn a_producer_reaches_the_routes_that_need_write() {
    held_to_the_table(Role::Producer.scopes(), "producer").await;
}

#[tokio::test]
async fn records_reaches_the_route_that_needs_delete() {
    held_to_the_table(Role::Records.scopes(), "records").await;
}

#[tokio::test]
async fn legal_reaches_the_legal_routes() {
    held_to_the_table(Role::Legal.scopes(), "legal").await;
}

#[tokio::test]
async fn an_auditor_reaches_the_tenants_audit_log() {
    held_to_the_table(Role::Auditor.scopes(), "auditor").await;
}

#[tokio::test]
async fn an_operator_reaches_the_admin_routes() {
    held_to_the_table(Role::Operator.scopes(), "operator").await;
}

#[tokio::test]
async fn two_roles_reach_the_routes_of_either() {
    for (one, other) in [
        (Role::Reader, Role::Auditor),
        (Role::Producer, Role::Records),
        (Role::Legal, Role::TenantAdmin),
    ] {
        let roles = Roles::of(&[one, other]);
        held_to_the_table(roles.scopes(), &roles.to_string()).await;
    }
}

#[tokio::test]
async fn every_tenant_role_together_reaches_every_route_but_the_admin_ones() {
    held_to_the_table(Roles::TENANT.scopes(), "every tenant role").await;
}

#[test]
fn each_scope_is_needed_by_a_route() {
    for scope in Scope::ALL {
        let routes = policy()
            .into_iter()
            .filter(|(_, _, needs)| *needs == Needs::Scope(scope))
            .count();
        assert!(routes > 0, "{scope:?}");
    }
}

#[test]
fn each_route_that_needs_a_scope_is_reached_by_one_role() {
    for (method, route, needs) in policy() {
        let roles: Vec<Role> = Role::ALL
            .into_iter()
            .filter(|role| reaches(role.scopes(), needs))
            .collect();
        match needs {
            Needs::Scope(_) => assert_eq!(roles.len(), 1, "{method} {route}: {roles:?}"),
            Needs::AnyKey | Needs::NoKey => {
                assert_eq!(roles.len(), Role::ALL.len(), "{method} {route}: {roles:?}");
            }
            Needs::Session => assert_eq!(roles.len(), 0, "{method} {route}: {roles:?}"),
        }
    }
}

#[test]
fn the_roles_reach_the_routes_the_plan_gives_them() {
    // The count of routes each role's scope opens, read from the table:
    // a change of a route's scope or a role's scope moves a number here.
    let opened = |role: Role| {
        policy()
            .into_iter()
            .filter(|(_, _, needs)| {
                matches!(needs, Needs::Scope(scope) if role.scopes().contains(*scope))
            })
            .count()
    };
    assert_eq!(
        Role::ALL.map(opened),
        [2, 2, 1, 6, 1, 6, 9],
        "reader, producer, records, legal, auditor, tenant-admin, operator"
    );
}

#[tokio::test]
async fn the_keyless_routes_answer_with_no_key() {
    let s = server().await;
    for (method, template, needs) in policy() {
        if needs == Needs::NoKey {
            let answer = status(&s, method, template, None).await;
            assert!(
                answer != 401 && answer != 403,
                "{method} {template} answered {answer}"
            );
        }
    }
}

// What must be refused.

#[tokio::test]
async fn a_tenant_admin_reaches_the_tenant_routes_and_no_vcon() {
    let scopes = Role::TenantAdmin.scopes();
    let reached: Vec<&str> = policy()
        .into_iter()
        .filter(|(_, _, needs)| matches!(needs, Needs::Scope(_)) && reaches(scopes, *needs))
        .map(|(_, route, _)| route)
        .collect();
    assert_eq!(
        reached.len(),
        6,
        "the six routes under /tenant: {reached:?}"
    );
    assert!(
        reached.iter().all(|route| route.starts_with("/tenant/")),
        "and no route of a vCon, a log or the operator: {reached:?}"
    );
    held_to_the_table(scopes, "tenant-admin").await;
}

#[tokio::test]
async fn every_route_that_takes_a_key_answers_401_with_none() {
    let s = server().await;
    for (method, template, needs) in policy() {
        if needs != Needs::NoKey {
            assert_eq!(
                status(&s, method, template, None).await,
                401,
                "{method} {template}"
            );
        }
    }
}

#[tokio::test]
async fn every_route_that_takes_a_key_answers_401_to_a_token_no_key_has() {
    let s = server().await;
    let unknown = format!("vcs_0123456789abcdef_{}", "A".repeat(43));
    for (method, template, needs) in policy() {
        if needs != Needs::NoKey {
            assert_eq!(
                status(&s, method, template, Some(&unknown)).await,
                401,
                "{method} {template}"
            );
        }
    }
}

#[tokio::test]
async fn a_revoked_key_reaches_no_route_whatever_its_role() {
    let s = server().await;
    let made = s
        .tenants
        .create_key(Actor::Cli, Some(s.tenant), Roles::TENANT.scopes(), "gone")
        .expect("a key");
    s.tenants.revoke_key(Actor::Cli, made.id).expect("revoked");
    for (method, template, needs) in policy() {
        if needs != Needs::NoKey {
            assert_eq!(
                status(&s, method, template, Some(&made.token)).await,
                401,
                "{method} {template}"
            );
        }
    }
}

#[tokio::test]
async fn no_tenant_role_or_set_of_them_reaches_an_admin_route() {
    let s = server().await;
    let token = key(&s, Roles::TENANT.scopes());
    for (method, template, needs) in policy() {
        if needs == Needs::Scope(Scope::Admin) {
            assert_eq!(
                status(&s, method, template, Some(&token)).await,
                403,
                "{method} {template}"
            );
        }
    }
}

#[tokio::test]
async fn an_operator_reaches_no_route_of_a_tenant() {
    let s = server().await;
    let token = key(&s, Role::Operator.scopes());
    for (method, template, needs) in policy() {
        if matches!(needs, Needs::Scope(scope) if scope != Scope::Admin) {
            assert_eq!(
                status(&s, method, template, Some(&token)).await,
                403,
                "{method} {template}"
            );
        }
    }
}

#[tokio::test]
async fn a_role_the_cap_leaves_out_opens_no_route() {
    // A membership of reader and records in a tenant capped at reader has
    // the scopes of reader alone.
    let held = Roles::of(&[Role::Reader, Role::Records]);
    let counted = held.within(Some(Roles::of(&[Role::Reader])));
    assert_eq!(
        counted.scopes(),
        Role::Reader.scopes(),
        "the scopes that count"
    );
    let s = server().await;
    let token = key(&s, counted.scopes());
    assert_eq!(
        status(&s, "DELETE", "/vcons/{uuid}", Some(&token)).await,
        403,
        "delete is refused"
    );
    assert_ne!(
        status(&s, "GET", "/vcons", Some(&token)).await,
        403,
        "read is not"
    );
}

#[tokio::test]
async fn a_membership_wholly_outside_the_cap_has_no_scope_a_key_could_hold() {
    let counted = Roles::of(&[Role::Legal]).within(Some(Roles::of(&[Role::Reader])));
    assert!(counted.scopes().is_empty(), "no scope");
    let s = server().await;
    assert_eq!(
        s.tenants
            .create_key(Actor::Cli, Some(s.tenant), counted.scopes(), "none")
            .err(),
        Some(crate::tenants::TenantError::BadScopes),
        "a key needs a scope"
    );
}

// Limits and odd inputs.

#[test]
fn a_path_is_made_for_every_route_with_no_place_left_open() {
    for (method, template, _) in policy() {
        let made = path(template);
        assert!(
            !made.contains('{') && !made.contains('}'),
            "{method} {template} became {made}"
        );
    }
}

#[test]
fn the_table_has_a_line_for_each_method_of_a_route_apart() {
    let hold: Vec<&str> = policy()
        .into_iter()
        .filter(|(_, route, _)| *route == "/legal/vcons/{uuid}/hold")
        .map(|(method, _, _)| method)
        .collect();
    assert_eq!(hold, ["PUT", "DELETE"], "the two methods of one route");
}

#[test]
fn reaching_is_read_from_the_scopes_alone() {
    let none = Scopes::default();
    assert!(reaches(none, Needs::AnyKey), "any key, whatever it holds");
    assert!(reaches(none, Needs::NoKey), "no key at all");
    for scope in Scope::ALL {
        assert!(
            !reaches(none, Needs::Scope(scope)),
            "{scope:?} with no scope"
        );
        assert!(
            reaches(Scopes::of(&[scope]), Needs::Scope(scope)),
            "{scope:?} with itself"
        );
    }
}

#[test]
fn every_pair_of_tenant_roles_reaches_the_union_of_their_routes() {
    let tenant: Vec<Role> = Role::ALL
        .into_iter()
        .filter(|role| *role != Role::Operator)
        .collect();
    for one in &tenant {
        for other in &tenant {
            let both = Roles::of(&[*one, *other]).scopes();
            for (method, route, needs) in policy() {
                assert_eq!(
                    reaches(both, needs),
                    reaches(one.scopes(), needs) || reaches(other.scopes(), needs),
                    "{one:?} and {other:?}: {method} {route}"
                );
            }
        }
    }
}

#[tokio::test]
async fn a_key_of_one_role_holds_on_the_tenant_listener_of_a_split_server() {
    use super::tests::pipeline::server_on;
    let s = server_on(crate::access::Plane::Tenant).await;
    let token = key(&s, Role::Auditor.scopes());
    assert_ne!(
        status(&s, "GET", "/audit", Some(&token)).await,
        403,
        "the auditor's route"
    );
    assert_eq!(
        status(&s, "GET", "/vcons", Some(&token)).await,
        403,
        "a route of another role"
    );
}