shep-daemon 0.25.2

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Resolving the `{{secret:...}}` references in a dog's `dogs.toml` section.
//!
//! A dog that asks `Request::DogConfigResolved` gets its section with each
//! reference replaced by the value the shepherd holds for it. The text on
//! disk is never touched, and `Request::DogConfig` still serves it as
//! written, which is what an editor must read.

use core::fmt;
use std::collections::BTreeSet;

use shep_core::config::DogTable;
use shep_core::config::key_path::{push_index, push_key};
use shep_core::config::template::render_secrets;
use shep_core::protocol::DogSectionToml;
use shep_core::secrets::{SecretError, SecretView, references_in};
use toml_edit::{DocumentMut, Formatted, Item, Value};

use super::spec::DogError;

/// Why the secret store could not be read, without the store's own message.
///
/// A [`SecretError::Decode`] message can quote the JSON value that failed to
/// parse, which in `secrets.json` is a secret. This keeps the class of
/// failure and drops the text (IR-41).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StoreFault {
    /// The file could not be opened or read
    Io,
    /// The file is not the JSON the store writes
    Decode,
    /// The file is a version newer than this build understands
    FutureVersion,
    /// Any other [`SecretError`]
    Other,
}

impl From<&SecretError> for StoreFault {
    fn from(err: &SecretError) -> Self {
        match err {
            SecretError::Io(_) => Self::Io,
            SecretError::Decode(_) => Self::Decode,
            SecretError::FutureVersion(_) => Self::FutureVersion,
            _ => Self::Other,
        }
    }
}

impl fmt::Display for StoreFault {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Io => "I/O failed",
            Self::Decode => "its contents do not parse",
            Self::FutureVersion => "it is a newer version than this build understands",
            Self::Other => "unusable",
        })
    }
}

/// Every `{{secret:...}}` reference `raw`, a dog's section, names, as
/// `KEY` or `namespace/KEY`.
///
/// Walks the same strings [`resolve_section`] substitutes into: keys and
/// comments are not among them. A section that does not parse names
/// nothing.
#[must_use]
pub fn section_references(raw: &str) -> BTreeSet<String> {
    let Ok(mut doc) = raw.parse::<DocumentMut>() else {
        return BTreeSet::new();
    };
    let mut found = BTreeSet::new();
    let _ = walk_strings::<core::convert::Infallible>(
        doc.as_table_mut().iter_mut(),
        &mut String::new(),
        &mut |_, text| {
            found.extend(references_in(text));
            Ok(None)
        },
    );
    found
}

/// `raw`, a dog's section, with every `{{secret:...}}` string value replaced
/// by its resolution in `view`.
///
/// The value is replaced, never the text, so `toml_edit` escapes it: a
/// secret holding a quote, a backslash or a newline stays one string and
/// cannot open a table. Keys, comments, key order and every non-string
/// value are untouched, as is a string that holds no reference.
///
/// # Errors
/// - [`DogError::Config`]: `raw` is not TOML.
/// - [`DogError::Reference`]: a reference `view` cannot resolve, or a
///   `{{secret:` token outside the reference grammar. Carries the key path
///   of the first such string and never a value.
pub fn resolve_section(raw: &str, view: &SecretView) -> Result<DogSectionToml, DogError> {
    let mut doc: DocumentMut = raw
        .parse()
        .map_err(|err: toml_edit::TomlError| DogError::Config(err.to_string()))?;
    walk_strings(
        doc.as_table_mut().iter_mut(),
        &mut String::new(),
        &mut |key_path, text| {
            let rendered = render_secrets(text, view).map_err(|source| DogError::Reference {
                key_path: key_path.to_string(),
                source,
            })?;
            // Only a string that changed is replaced, so every other string
            // keeps the quoting the operator chose.
            Ok((rendered != text).then_some(rendered))
        },
    )?;
    Ok(doc.to_string().into())
}

/// The error for a section that names references when the store holding
/// them could not be read.
///
/// Names the key path of the first reference, so an operator knows which
/// line asked. The caller has already established that `raw` names one.
#[must_use]
pub fn store_unreadable(raw: &str, source: &SecretError) -> DogError {
    let key_path = raw
        .parse::<DocumentMut>()
        .ok()
        .and_then(|mut doc| {
            let mut first = None;
            let _ = walk_strings(
                doc.as_table_mut().iter_mut(),
                &mut String::new(),
                &mut |key_path, text| {
                    if first.is_none() && !references_in(text).is_empty() {
                        first = Some(key_path.to_string());
                    }
                    Ok::<_, core::convert::Infallible>(None)
                },
            );
            first
        })
        .unwrap_or_default();
    DogError::SecretStore {
        key_path,
        fault: source.into(),
    }
}

/// `table`, a sheep's per-dog table, with every `{{secret:...}}` string
/// value replaced by its resolution in `view`.
///
/// The JSON counterpart of [`resolve_section`]: strings in nested objects
/// and arrays are walked, a string that changed is replaced, and keys and
/// every non-string value are carried over. `root` is the table's key path
/// in the message (`dogs.deploy`), so a refusal names where the string sits.
///
/// # Errors
/// - [`DogError::Reference`]: a reference `view` cannot resolve, or a
///   `{{secret:` token outside the reference grammar. Carries the key path
///   of the first such string and never a value.
pub fn resolve_table(
    table: &DogTable,
    root: &str,
    view: &SecretView,
) -> Result<DogTable, DogError> {
    table.try_map_strings(root, |key_path, text| {
        let rendered = render_secrets(text, view).map_err(|source| DogError::Reference {
            key_path: key_path.to_string(),
            source,
        })?;
        Ok((rendered != text).then_some(rendered))
    })
}

/// The error for a table that names references when the store holding them
/// could not be read.
///
/// Names the key path of the first reference under `root`. The caller has
/// already established that `table` names one.
#[must_use]
pub fn table_store_unreadable(table: &DogTable, root: &str, source: &SecretError) -> DogError {
    DogError::SecretStore {
        key_path: table.first_reference_path(root).unwrap_or_default(),
        fault: source.into(),
    }
}

/// What a visitor does with one string value: the key path it sits at and
/// its text in, the replacement out, or `None` to leave it.
type Visit<'a, E> = dyn FnMut(&str, &str) -> Result<Option<String>, E> + 'a;

/// Calls `visit` on every string value under `entries`: tables, sub-tables,
/// inline tables, arrays and arrays of tables. Keys are never visited.
///
/// `path` is the key path of the entries' parent, restored on return.
fn walk_strings<'a, E>(
    entries: impl Iterator<Item = (toml_edit::KeyMut<'a>, &'a mut Item)>,
    path: &mut String,
    visit: &mut Visit<'_, E>,
) -> Result<(), E> {
    for (key, item) in entries {
        let mark = path.len();
        push_key(path, key.get());
        let walked = walk_item(item, path, visit);
        path.truncate(mark);
        walked?;
    }
    Ok(())
}

fn walk_item<E>(item: &mut Item, path: &mut String, visit: &mut Visit<'_, E>) -> Result<(), E> {
    match item {
        Item::Value(value) => walk_value(value, path, visit),
        Item::Table(table) => walk_strings(table.iter_mut(), path, visit),
        Item::ArrayOfTables(tables) => {
            for (index, table) in tables.iter_mut().enumerate() {
                let mark = path.len();
                push_index(path, index);
                let walked = walk_strings(table.iter_mut(), path, visit);
                path.truncate(mark);
                walked?;
            }
            Ok(())
        }
        Item::None => Ok(()),
    }
}

fn walk_value<E>(value: &mut Value, path: &mut String, visit: &mut Visit<'_, E>) -> Result<(), E> {
    match value {
        Value::String(formatted) => {
            if let Some(replacement) = visit(path, formatted.value())? {
                // A fresh `Formatted` has no representation, so `toml_edit`
                // picks one that escapes the text; the old decor (a comment
                // after the value) is carried across.
                let decor = formatted.decor().clone();
                let mut replaced = Formatted::new(replacement);
                *replaced.decor_mut() = decor;
                *formatted = replaced;
            }
            Ok(())
        }
        Value::Array(array) => {
            for (index, element) in array.iter_mut().enumerate() {
                let mark = path.len();
                push_index(path, index);
                let walked = walk_value(element, path, visit);
                path.truncate(mark);
                walked?;
            }
            Ok(())
        }
        Value::InlineTable(table) => {
            for (key, inner) in table.iter_mut() {
                let mark = path.len();
                push_key(path, key.get());
                let walked = walk_value(inner, path, visit);
                path.truncate(mark);
                walked?;
            }
            Ok(())
        }
        _ => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use shep_core::secrets::ProviderCache;

    use super::*;

    /// A value no message may carry.
    const SENTINEL: &str = "sentinel-7f3a9c";

    fn view(environment: &str, pairs: &[(&str, &str)]) -> SecretView {
        let store = pairs
            .iter()
            .map(|(key, value)| {
                (
                    (*key).to_string(),
                    BTreeMap::from([(environment.to_string(), (*value).to_string())]),
                )
            })
            .collect();
        SecretView::new(environment.to_string(), store, ProviderCache::default())
    }

    fn parsed(section: &DogSectionToml) -> toml::Table {
        section.as_str().parse().unwrap()
    }

    #[test]
    fn a_reference_in_a_nested_table_an_array_and_an_array_of_tables_resolves() {
        let raw = "\
token = \"{{secret:T}}\"
port = 9615
kinds = [\"exit\", \"{{secret:T}}\"]

[sinks.local]
url = \"https://x.invalid/{{secret:T}}\"
inline = { deep = \"{{secret:T}}\", n = 1 }

[[rules]]
url = \"plain\"

[[rules]]
url = \"{{secret:T}}\"
";
        let section = resolve_section(raw, &view("production", &[("T", "s3cret")])).unwrap();
        let table = parsed(&section);
        assert_eq!(table["token"].as_str(), Some("s3cret"));
        assert_eq!(table["port"].as_integer(), Some(9615));
        assert_eq!(table["kinds"][1].as_str(), Some("s3cret"));
        assert_eq!(
            table["sinks"]["local"]["url"].as_str(),
            Some("https://x.invalid/s3cret")
        );
        assert_eq!(
            table["sinks"]["local"]["inline"]["deep"].as_str(),
            Some("s3cret")
        );
        assert_eq!(table["rules"][0]["url"].as_str(), Some("plain"));
        assert_eq!(table["rules"][1]["url"].as_str(), Some("s3cret"));
    }

    #[test]
    fn a_value_with_a_quote_a_backslash_a_newline_and_a_table_header_stays_one_string() {
        let hostile = "x\"\n[evil]\nk = \"y";
        let section = resolve_section(
            "token = \"{{secret:T}}\"\n",
            &view("production", &[("T", hostile)]),
        )
        .unwrap();
        let table = parsed(&section);
        assert_eq!(table["token"].as_str(), Some(hostile));
        assert_eq!(table.len(), 1, "no extra table: {}", section.as_str());

        let with_backslash = "C:\\dir\\\"q\"";
        let section = resolve_section(
            "token = \"{{secret:T}}\"\n",
            &view("production", &[("T", with_backslash)]),
        )
        .unwrap();
        assert_eq!(parsed(&section)["token"].as_str(), Some(with_backslash));
    }

    #[test]
    fn comments_and_key_order_survive_resolution() {
        let raw = "\
# above z
z = \"{{secret:T}}\" # beside z
a = 1

# above the table
[b]
y = 'literal'
c = \"{{secret:T}}\"
";
        let section = resolve_section(raw, &view("production", &[("T", "v")])).unwrap();
        assert_eq!(
            section.as_str(),
            "\
# above z
z = \"v\" # beside z
a = 1

# above the table
[b]
y = 'literal'
c = \"v\"
"
        );
    }

    #[test]
    fn a_reference_in_a_key_or_a_comment_is_left_alone() {
        let raw = "\
# {{secret:ABSENT}}
\"{{secret:ABSENT}}\" = 1
[\"{{secret:OTHER}}\"]
k = \"{{{{secret:ABSENT}}}}\"
";
        let empty = SecretView::empty("production".to_string());
        assert!(section_references(raw).is_empty());
        assert_eq!(resolve_section(raw, &empty).unwrap().as_str(), raw);
    }

    #[test]
    fn section_references_agrees_with_what_resolve_substitutes() {
        let raw = "\
a = \"{{secret:ONE}}\"
b = [\"{{secret:vercel/TWO}}\", \"{{name}}\"]
c = \"{{secret:ONE}} {{{{secret:NOPE}}}} {{secret:\"
[t]
d = { e = \"{{secret:THREE}}\" }
";
        let found = section_references(raw);
        assert_eq!(
            found,
            BTreeSet::from([
                "ONE".to_string(),
                "THREE".to_string(),
                "vercel/TWO".to_string()
            ])
        );

        // Resolving against a view holding exactly those references succeeds;
        // dropping any one of them is refused, so none is a reference the
        // resolver ignores.
        let providers = ProviderCache::new(
            BTreeMap::from([(
                "vercel".to_string(),
                BTreeMap::from([(
                    "TWO".to_string(),
                    BTreeMap::from([("production".to_string(), "2".to_string())]),
                )]),
            )]),
            BTreeMap::from([(
                "vercel".to_string(),
                BTreeSet::from(["production".to_string()]),
            )]),
        );
        let store = |keys: &[&str]| {
            keys.iter()
                .map(|key| {
                    (
                        (*key).to_string(),
                        BTreeMap::from([("production".to_string(), "1".to_string())]),
                    )
                })
                .collect()
        };
        let full = SecretView::new(
            "production".to_string(),
            store(&["ONE", "THREE"]),
            providers.clone(),
        );
        let section = resolve_section(raw, &full).unwrap();
        assert!(section_references(section.as_str()).is_empty());
        for missing in [&["THREE"][..], &["ONE"][..]] {
            let partial =
                SecretView::new("production".to_string(), store(missing), providers.clone());
            assert!(resolve_section(raw, &partial).is_err());
        }
    }

    #[test]
    fn an_unresolved_reference_is_refused_naming_its_key_path() {
        let raw = "[sinks.local]\nurl = \"{{secret:ABSENT}}\"\n\n[[rules]]\nurl = \"ok\"\n\n[[rules]]\nurl = \"{{secret:ABSENT}}\"\n";
        let err = resolve_section(raw, &view("production", &[("K", SENTINEL)])).unwrap_err();
        assert_eq!(
            err.to_string(),
            "`sinks.local.url`: `{{secret:ABSENT}}` has no value in the `production` environment"
        );
        let later = resolve_section(
            "[[rules]]\nurl = \"ok\"\n[[rules]]\nurl = \"{{secret:ABSENT}}\"\n",
            &view("production", &[]),
        )
        .unwrap_err();
        assert!(later.to_string().starts_with("`rules[1].url`: "), "{later}");
    }

    /// IR-41: exact strings, with a sentinel in the store that appears in
    /// neither rendering.
    #[test]
    fn the_secret_errors_print_exact_strings_and_never_a_value() {
        let store = view("production", &[("PRESENT", SENTINEL)]);

        let unresolved = resolve_section("k = \"{{secret:ABSENT}}\"\n", &store).unwrap_err();
        assert_eq!(
            unresolved.to_string(),
            "`k`: `{{secret:ABSENT}}` has no value in the `production` environment"
        );
        assert_eq!(
            format!("{unresolved:?}"),
            "Reference { key_path: \"k\", source: Unresolved { reference: \"{{secret:ABSENT}}\", \
             environment: \"production\" } }"
        );

        let unready = resolve_section("k = \"{{secret:paddock/owner}}\"\n", &store).unwrap_err();
        assert_eq!(
            unready.to_string(),
            "`k`: `{{secret:paddock/owner}}` reads the `paddock` namespace, which no provider \
             dog has pushed to for the `production` environment yet"
        );

        let malformed = resolve_section("k = \"{{secret:bad key}}\"\n", &store).unwrap_err();
        assert_eq!(
            malformed.to_string(),
            "`k`: `{{secret:bad key}}` is not a valid secret reference: write \
             `{{secret:KEY}}` or `{{secret:namespace/KEY}}`"
        );

        let decode = serde_json::from_str::<u32>(&format!("\"{SENTINEL}\"")).unwrap_err();
        assert!(decode.to_string().contains(SENTINEL), "the premise");
        let unreadable = store_unreadable(
            "[a]\nk = \"{{secret:PRESENT}}\"\n",
            &SecretError::Decode(decode),
        );
        assert_eq!(
            unreadable.to_string(),
            "`a.k`: the secret store could not be read (its contents do not parse)"
        );
        assert_eq!(
            format!("{unreadable:?}"),
            "SecretStore { key_path: \"a.k\", fault: Decode }"
        );

        for rendered in [
            unresolved.to_string(),
            format!("{unresolved:?}"),
            unready.to_string(),
            malformed.to_string(),
            unreadable.to_string(),
            format!("{unreadable:?}"),
        ] {
            assert!(!rendered.contains(SENTINEL), "{rendered}");
        }
    }

    #[test]
    fn a_section_that_is_not_toml_is_a_config_error() {
        let err =
            resolve_section("k = ", &SecretView::empty("production".to_string())).unwrap_err();
        assert!(matches!(err, DogError::Config(_)), "{err}");
        assert!(section_references("k = ").is_empty());
    }

    fn table_of(value: serde_json::Value) -> DogTable {
        serde_json::from_value(value).unwrap()
    }

    #[test]
    fn a_table_reference_in_a_nested_object_and_an_array_resolves() {
        let table = table_of(serde_json::json!({
            "token": "{{secret:T}}",
            "port": 9615,
            "on": true,
            "none": null,
            "plain": "unchanged",
            "kinds": ["exit", "{{secret:T}}", 3],
            "env": { "deep": "https://x.invalid/{{secret:T}}", "n": 1.5 },
            "rules": [{ "url": "{{secret:T}}" }],
        }));
        let resolved = resolve_table(
            &table,
            "dogs.deploy",
            &view("production", &[("T", "s3cret")]),
        )
        .unwrap();
        assert_eq!(
            serde_json::to_value(&resolved).unwrap(),
            serde_json::json!({
                "token": "s3cret",
                "port": 9615,
                "on": true,
                "none": null,
                "plain": "unchanged",
                "kinds": ["exit", "s3cret", 3],
                "env": { "deep": "https://x.invalid/s3cret", "n": 1.5 },
                "rules": [{ "url": "s3cret" }],
            })
        );
    }

    #[test]
    fn a_table_reference_is_refused_naming_its_key_path_under_the_root() {
        let table = table_of(serde_json::json!({
            "env": { "TOKEN": "{{secret:TOKEN}}" },
            "kinds": ["ok", "{{secret:TOKEN}}"],
        }));
        let err =
            resolve_table(&table, "dogs.deploy", &view("staging", &[("K", SENTINEL)])).unwrap_err();
        assert_eq!(
            err.to_string(),
            "`dogs.deploy.env.TOKEN`: `{{secret:TOKEN}}` has no value in the `staging` environment"
        );
        assert_eq!(
            table.first_reference_path("dogs.deploy").as_deref(),
            Some("dogs.deploy.env.TOKEN")
        );
        let unreadable = table_store_unreadable(
            &table,
            "dogs.deploy",
            &SecretError::Io(std::io::Error::other(SENTINEL)),
        );
        assert_eq!(
            unreadable.to_string(),
            "`dogs.deploy.env.TOKEN`: the secret store could not be read (I/O failed)"
        );
    }

    #[test]
    fn table_references_agrees_with_what_resolve_table_substitutes() {
        let table = table_of(serde_json::json!({
            "a": "{{secret:ONE}}",
            "b": ["{{secret:vercel/TWO}}", "{{name}}"],
            "c": "{{secret:ONE}} {{{{secret:NOPE}}}} {{secret:",
            "{{secret:IN_A_KEY}}": 1,
            "t": { "d": { "e": "{{secret:THREE}}" } },
        }));
        assert_eq!(
            table.references(),
            BTreeSet::from([
                "ONE".to_string(),
                "THREE".to_string(),
                "vercel/TWO".to_string()
            ])
        );

        let providers = ProviderCache::new(
            BTreeMap::from([(
                "vercel".to_string(),
                BTreeMap::from([(
                    "TWO".to_string(),
                    BTreeMap::from([("production".to_string(), "2".to_string())]),
                )]),
            )]),
            BTreeMap::from([(
                "vercel".to_string(),
                BTreeSet::from(["production".to_string()]),
            )]),
        );
        let store = |keys: &[&str]| {
            keys.iter()
                .map(|key| {
                    (
                        (*key).to_string(),
                        BTreeMap::from([("production".to_string(), "1".to_string())]),
                    )
                })
                .collect()
        };
        let full = SecretView::new(
            "production".to_string(),
            store(&["ONE", "THREE"]),
            providers.clone(),
        );
        let resolved = resolve_table(&table, "dogs.d", &full).unwrap();
        assert!(resolved.references().is_empty());
        for missing in [&["THREE"][..], &["ONE"][..]] {
            let partial =
                SecretView::new("production".to_string(), store(missing), providers.clone());
            assert!(resolve_table(&table, "dogs.d", &partial).is_err());
        }
    }
}