galata-vault 0.4.0

End-to-end-encrypted secrets and config documents with no accounts: the galata-vault Rust SDK. A token client and the owner API over a pluggable transport, with an optional in-process backend.
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
//! Which operation a canonical request names: one static table of every
//! endpoint (method, path, authentication scheme), which routes every call
//! the core serves and builds `galata-vault-server`'s own routes, and which a test
//! compares with the endpoint table of `docs/spec/http-api.md#2`.
//!
//! Matching: the most specific path pattern that matches wins (a literal
//! segment before a `{placeholder}`), then the method. An unknown path is 404
//! `not_found`; a known path with another method is 400 `invalid_request`.
//!
//! Authentication runs before any path parameter or query is parsed, so a
//! malformed one gets a JSON error only once the caller is known.

use std::borrow::Cow;

use crate::backend::RecordKind;

use crate::server_core::error::CoreError;
use crate::server_core::{CanonicalRequest, Core, CoreResponse, critical, records, tokens, vaults};

type Answer = Result<CoreResponse, CoreError>;

/// How a request to an endpoint authenticates (`docs/spec/http-api.md#3`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuthScheme {
    /// No `Authorization`; a request that carries one is not authenticated
    /// by it.
    None,
    /// `GV-Sig v=1`, `actor=owner`.
    Owner,
    /// `GV-Sig v=1`, `actor=token`.
    Token,
    /// `GV-Sig v=1`, by the owner or a token; the scope decides the rest.
    OwnerOrToken,
}

impl AuthScheme {
    /// How the specification's endpoint table writes it.
    pub fn as_str(self) -> &'static str {
        match self {
            AuthScheme::None => "none",
            AuthScheme::Owner => "owner",
            AuthScheme::Token => "token",
            AuthScheme::OwnerOrToken => "owner, token",
        }
    }
}

/// Who answers an endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Served {
    /// The core, through [`Core::call`]: every transport.
    Core,
    /// The HTTP shell only (`gv-server`), never the embedded transport.
    Shell,
}

/// What an endpoint does.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RouteId {
    Capabilities,
    Challenges,
    CreateVault,
    Status,
    DeleteVault,
    Descriptors,
    ChildrenGet,
    ChildrenPut,
    Rotate,
    RegisterToken,
    TokenSelf,
    ReportToken,
    RevokeToken,
    Audit,
    RecordList(RecordKind),
    RecordGet(RecordKind),
    RecordPut(RecordKind),
    RecordDelete(RecordKind),
    RecordVersions(RecordKind),
    RecordVersion(RecordKind),
    Healthz,
    Readyz,
}

/// One endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Route {
    pub method: &'static str,
    /// The path, with `{name}` for a segment parameter.
    pub path: &'static str,
    pub auth: AuthScheme,
    pub served: Served,
    pub id: RouteId,
}

const fn core(method: &'static str, path: &'static str, auth: AuthScheme, id: RouteId) -> Route {
    Route {
        method,
        path,
        auth,
        served: Served::Core,
        id,
    }
}

const fn shell(path: &'static str, id: RouteId) -> Route {
    Route {
        method: "GET",
        path,
        auth: AuthScheme::None,
        served: Served::Shell,
        id,
    }
}

use AuthScheme::{None as Unauthenticated, Owner, OwnerOrToken, Token};
use RecordKind::{Config, Secret};

/// Every endpoint, literal paths before the parameterised ones they could
/// also match.
pub const ROUTES: &[Route] = &[
    core(
        "GET",
        "/v1/capabilities",
        Unauthenticated,
        RouteId::Capabilities,
    ),
    core(
        "POST",
        "/v1/challenges",
        Unauthenticated,
        RouteId::Challenges,
    ),
    core("POST", "/v1/vaults", Owner, RouteId::CreateVault),
    core("GET", "/v1/vault", OwnerOrToken, RouteId::Status),
    core("DELETE", "/v1/vault", Owner, RouteId::DeleteVault),
    core(
        "GET",
        "/v1/vault/descriptors",
        OwnerOrToken,
        RouteId::Descriptors,
    ),
    core("GET", "/v1/vault/children", Owner, RouteId::ChildrenGet),
    core("PUT", "/v1/vault/children", Owner, RouteId::ChildrenPut),
    core("POST", "/v1/vault/rotations", Owner, RouteId::Rotate),
    core("POST", "/v1/tokens", Owner, RouteId::RegisterToken),
    core("GET", "/v1/tokens/self", Token, RouteId::TokenSelf),
    core(
        "POST",
        "/v1/tokens/report",
        Unauthenticated,
        RouteId::ReportToken,
    ),
    core(
        "DELETE",
        "/v1/tokens/{token_id}",
        OwnerOrToken,
        RouteId::RevokeToken,
    ),
    core("GET", "/v1/audit", OwnerOrToken, RouteId::Audit),
    core(
        "GET",
        "/v1/secrets",
        OwnerOrToken,
        RouteId::RecordList(Secret),
    ),
    core(
        "GET",
        "/v1/secrets/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordGet(Secret),
    ),
    core(
        "PUT",
        "/v1/secrets/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordPut(Secret),
    ),
    core(
        "DELETE",
        "/v1/secrets/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordDelete(Secret),
    ),
    core(
        "GET",
        "/v1/secrets/{name_hmac}/versions",
        OwnerOrToken,
        RouteId::RecordVersions(Secret),
    ),
    core(
        "GET",
        "/v1/secrets/{name_hmac}/versions/{version}",
        OwnerOrToken,
        RouteId::RecordVersion(Secret),
    ),
    core(
        "GET",
        "/v1/configs",
        OwnerOrToken,
        RouteId::RecordList(Config),
    ),
    core(
        "GET",
        "/v1/configs/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordGet(Config),
    ),
    core(
        "PUT",
        "/v1/configs/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordPut(Config),
    ),
    core(
        "DELETE",
        "/v1/configs/{name_hmac}",
        OwnerOrToken,
        RouteId::RecordDelete(Config),
    ),
    core(
        "GET",
        "/v1/configs/{name_hmac}/versions",
        OwnerOrToken,
        RouteId::RecordVersions(Config),
    ),
    core(
        "GET",
        "/v1/configs/{name_hmac}/versions/{version}",
        OwnerOrToken,
        RouteId::RecordVersion(Config),
    ),
    shell("/healthz", RouteId::Healthz),
    shell("/readyz", RouteId::Readyz),
];

/// What a path and method name.
#[derive(Debug, PartialEq, Eq)]
pub enum Lookup<'a> {
    /// This endpoint, with its `{parameter}` segments in order.
    Found(&'static Route, Vec<&'a str>),
    /// The path is an endpoint's, the method is not.
    MethodNotAllowed,
    /// No endpoint has this path.
    NotFound,
}

/// The parameters `pattern` binds in `path`, if it matches. A parameter
/// matches one non-empty segment.
fn bind<'a>(pattern: &str, path: &'a str) -> Option<Vec<&'a str>> {
    let mut want = pattern.split('/');
    let mut got = path.split('/');
    let mut params = Vec::new();
    loop {
        match (want.next(), got.next()) {
            (None, None) => return Some(params),
            (Some(w), Some(g)) if w.starts_with('{') => {
                if g.is_empty() {
                    return None;
                }
                params.push(g);
            }
            (Some(w), Some(g)) if w == g => {}
            _ => return None,
        }
    }
}

/// The endpoint `method` and `path` (no query) name.
pub fn lookup<'a>(method: &str, path: &'a str) -> Lookup<'a> {
    let Some((first, params)) = ROUTES
        .iter()
        .find_map(|r| bind(r.path, path).map(|params| (r, params)))
    else {
        return Lookup::NotFound;
    };
    match ROUTES
        .iter()
        .find(|r| r.path == first.path && r.method == method)
    {
        Some(route) => Lookup::Found(route, params),
        None => Lookup::MethodNotAllowed,
    }
}

pub(crate) fn dispatch(core: &Core, request: &CanonicalRequest<'_>) -> Answer {
    let (path, query) = match request.path_and_query.split_once('?') {
        Some((path, query)) => (path, Some(query)),
        None => (request.path_and_query, None),
    };
    let (route, params) = match lookup(request.method, path) {
        Lookup::Found(route, params) => (route, params),
        Lookup::MethodNotAllowed => return Err(CoreError::method_not_allowed()),
        Lookup::NotFound => return Err(CoreError::no_endpoint()),
    };
    let arg = |i: usize| params.get(i).copied().unwrap_or("");
    match route.id {
        RouteId::Capabilities => vaults::capabilities(core),
        RouteId::Challenges => vaults::challenge(core, request),
        RouteId::CreateVault => vaults::create(core, request),
        RouteId::Status => core.authed(request, |c| vaults::status(core, c)),
        RouteId::DeleteVault => core.authed(request, |c| critical::delete_vault(core, c)),
        RouteId::Descriptors => core.authed(request, |c| vaults::descriptors(core, c, query)),
        RouteId::ChildrenGet => core.authed(request, |c| vaults::children(core, c)),
        RouteId::ChildrenPut => core.authed(request, |c| vaults::put_children(core, c, request)),
        RouteId::Rotate => core.authed(request, |c| critical::rotate(core, c, request.body)),
        RouteId::RegisterToken => core.authed(request, |c| tokens::register(core, c, request.body)),
        RouteId::TokenSelf => core.authed(request, tokens::whoami),
        RouteId::ReportToken => critical::report(core, request.body),
        RouteId::RevokeToken => {
            core.authed(request, |c| critical::revoke(core, c, &param(arg(0))?))
        }
        RouteId::Audit => core.authed(request, |c| records::audit(core, c, query)),
        RouteId::RecordList(kind) => core.authed(request, |c| records::list(core, c, kind, query)),
        RouteId::RecordGet(kind) => core.authed(request, |c| {
            records::read(core, c, kind, &param(arg(0))?, None)
        }),
        RouteId::RecordPut(kind) => core.authed(request, |c| {
            records::put(core, c, kind, &param(arg(0))?, request)
        }),
        RouteId::RecordDelete(kind) => core.authed(request, |c| {
            records::delete(core, c, kind, &param(arg(0))?, request)
        }),
        RouteId::RecordVersions(kind) => core.authed(request, |c| {
            records::versions(core, c, kind, &param(arg(0))?)
        }),
        RouteId::RecordVersion(kind) => core.authed(request, |c| {
            records::read(core, c, kind, &param(arg(0))?, Some(&param(arg(1))?))
        }),
        // The HTTP shell's own endpoints: no transport reaches them here.
        RouteId::Healthz | RouteId::Readyz => Err(CoreError::no_endpoint()),
    }
}

/// A path parameter, percent-decoded as the HTTP router decoded it.
fn param(segment: &str) -> Result<Cow<'_, str>, CoreError> {
    if !segment.contains('%') {
        return Ok(Cow::Borrowed(segment));
    }
    let bad = || CoreError::invalid("a path segment is not valid percent-encoding");
    let bytes = segment.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            let hex = bytes
                .get(i + 1..i + 3)
                .filter(|h| h.iter().all(u8::is_ascii_hexdigit))
                .ok_or_else(bad)?;
            let hex = std::str::from_utf8(hex).map_err(|_| bad())?;
            out.push(u8::from_str_radix(hex, 16).map_err(|_| bad())?);
            i += 3;
        } else {
            out.push(bytes[i]);
            i += 1;
        }
    }
    String::from_utf8(out).map(Cow::Owned).map_err(|_| bad())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn path_parameters_decode_like_the_router() {
        assert_eq!(param("abc").unwrap(), "abc");
        assert_eq!(param("a%62c").unwrap(), "abc");
        for bad in ["%", "%4", "%zz", "%ff"] {
            assert!(param(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn the_most_specific_path_wins_then_the_method() {
        let found = |m, p| match lookup(m, p) {
            Lookup::Found(r, params) => Some((r.id, params)),
            _ => None,
        };
        assert_eq!(
            found("GET", "/v1/tokens/self"),
            Some((RouteId::TokenSelf, vec![]))
        );
        assert_eq!(
            lookup("DELETE", "/v1/tokens/self"),
            Lookup::MethodNotAllowed
        );
        assert_eq!(lookup("GET", "/v1/tokens/report"), Lookup::MethodNotAllowed);
        assert_eq!(
            found("DELETE", "/v1/tokens/abcd"),
            Some((RouteId::RevokeToken, vec!["abcd"]))
        );
        assert_eq!(
            found("GET", "/v1/configs/ab/versions/3"),
            Some((RouteId::RecordVersion(Config), vec!["ab", "3"]))
        );
        for missing in [
            "/v2/capabilities",
            "/v0/vault",
            "/v1/secrets/",
            "/v1/secrets/a/b",
            "/",
            "",
        ] {
            assert_eq!(lookup("GET", missing), Lookup::NotFound, "{missing}");
        }
        assert_eq!(lookup("PATCH", "/v1/vault"), Lookup::MethodNotAllowed);
    }

    #[test]
    fn the_table_is_unambiguous() {
        let mut seen = std::collections::BTreeSet::new();
        for r in ROUTES {
            assert!(
                seen.insert((r.method, r.path)),
                "{} {} twice",
                r.method,
                r.path
            );
            assert!(r.path.starts_with('/'), "{}", r.path);
            // Every route is reachable: no earlier pattern shadows its path.
            let concrete = r
                .path
                .replace("{token_id}", "t")
                .replace("{name_hmac}", "n")
                .replace("{version}", "1");
            assert!(
                matches!(lookup(r.method, &concrete), Lookup::Found(found, _) if found == r),
                "{} {} is shadowed",
                r.method,
                r.path
            );
        }
    }
}