c5_core 2.0.0

Core utilities for c5store secret management CLI, providing cryptographic operations, key management, in-place editing of YAML, TOML and JSON documents, and file I/O.
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
//! YAML read and in-place write.
//!
//! Reading is yaml-rust2's parse. Writing replaces the lines one entry
//! occupies, or adds an entry to the block that should hold it, and leaves
//! every other byte alone. A value written in flow style, `key: {a: 1}`, has
//! no lines of its own to replace and is refused by name rather than reflowed.

use yaml_rust2::parser::{Event, MarkedEventReceiver, Parser};
use yaml_rust2::scanner::Marker;

use crate::document::indent::{leading, unit_of};
use crate::error::C5CoreError;
use crate::path::PathSegment;
use crate::value::Value;

/// Where a node begins. yaml-rust2 reports `line` from one and, despite what
/// its documentation says, `col` from zero.
#[derive(Debug, Clone, Copy)]
struct At {
  line: usize,
  col: usize,
}

impl From<Marker> for At {
  fn from(marker: Marker) -> At {
    At { line: marker.line(), col: marker.col() }
  }
}

#[derive(Debug)]
enum Node {
  Scalar { at: At, value: String },
  Seq { at: At, items: Vec<usize> },
  Map { at: At, entries: Vec<Entry> },
}

#[derive(Debug)]
struct Entry {
  key: String,
  key_at: At,
  value: usize,
}

impl Node {
  fn at(&self) -> At {
    match self {
      Node::Scalar { at, .. } | Node::Seq { at, .. } | Node::Map { at, .. } => *at,
    }
  }
}

/// The document as an arena of nodes, each knowing where it started.
#[derive(Default)]
struct Tree {
  nodes: Vec<Node>,
  root: Option<usize>,
  /// What is being built: a sequence, or a mapping with the key it is waiting on.
  stack: Vec<Frame>,
  done: bool,
}

enum Frame {
  Seq { node: usize },
  Map { node: usize, pending: Option<(String, At)> },
}

impl Tree {
  fn push(&mut self, node: Node) -> usize {
    self.nodes.push(node);
    self.nodes.len() - 1
  }

  /// Hands a finished node to whatever is building above it.
  fn place(&mut self, id: usize) {
    match self.stack.last_mut() {
      None => {
        if self.root.is_none() {
          self.root = Some(id);
        }
      }
      Some(Frame::Seq { node }) => {
        let node = *node;
        if let Node::Seq { items, .. } = &mut self.nodes[node] {
          items.push(id);
        }
      }
      Some(Frame::Map { node, pending }) => {
        let node = *node;
        match pending.take() {
          None => {
            // A key: remembered until its value arrives.
            let key = match &self.nodes[id] {
              Node::Scalar { value, .. } => value.clone(),
              other => format!("{:?}", other.at().line),
            };
            let at = self.nodes[id].at();
            if let Some(Frame::Map { pending, .. }) = self.stack.last_mut() {
              *pending = Some((key, at));
            }
          }
          Some((key, key_at)) => {
            if let Node::Map { entries, .. } = &mut self.nodes[node] {
              entries.push(Entry { key, key_at, value: id });
            }
          }
        }
      }
    }
  }
}

impl MarkedEventReceiver for Tree {
  fn on_event(&mut self, event: Event, mark: Marker) {
    if self.done {
      return;
    }
    let at = At::from(mark);
    match event {
      Event::Scalar(value, _style, _anchor, _tag) => {
        let id = self.push(Node::Scalar { at, value });
        self.place(id);
      }
      Event::SequenceStart(..) => {
        let id = self.push(Node::Seq { at, items: Vec::new() });
        self.stack.push(Frame::Seq { node: id });
      }
      Event::MappingStart(..) => {
        let id = self.push(Node::Map { at, entries: Vec::new() });
        self.stack.push(Frame::Map { node: id, pending: None });
      }
      Event::SequenceEnd | Event::MappingEnd => {
        let id = match self.stack.pop() {
          Some(Frame::Seq { node }) | Some(Frame::Map { node, .. }) => node,
          None => return,
        };
        self.place(id);
      }
      Event::DocumentEnd => self.done = true,
      Event::Alias(_) => {
        let id = self.push(Node::Scalar { at, value: String::new() });
        self.place(id);
      }
      _ => {}
    }
  }
}

fn tree_of(text: &str) -> Result<Tree, C5CoreError> {
  let mut tree = Tree::default();
  Parser::new_from_str(text)
    .load(&mut tree, false)
    .map_err(|e| C5CoreError::YamlDeserialize(format!("YAML parsing failed: {e}")))?;
  Ok(tree)
}

pub fn check(text: &str) -> Result<(), C5CoreError> {
  tree_of(text).map(|_| ())
}

/// The node `segments` names, if the document holds one.
fn find(tree: &Tree, segments: &[PathSegment]) -> Option<usize> {
  let mut current = tree.root?;
  for segment in segments {
    current = step(tree, current, segment)?;
  }
  Some(current)
}

/// The one node a segment names, refusing a query that matches more than one:
/// a write has to know which object it is changing.
fn step_once(tree: &Tree, node: usize, segment: &PathSegment) -> Result<Option<usize>, C5CoreError> {
  if let (Node::Seq { items, .. }, PathSegment::Query { key, value }) = (&tree.nodes[node], segment) {
    let matched: Vec<usize> = items
      .iter()
      .copied()
      .filter(|item| {
        matches!(&tree.nodes[*item], Node::Map { entries, .. }
          if entries.iter().any(|e| e.key == *key && matches!(&tree.nodes[e.value], Node::Scalar { value: v, .. } if v == value)))
      })
      .collect();
    return match matched.len() {
      0 => Ok(None),
      1 => Ok(Some(matched[0])),
      n => Err(C5CoreError::YamlNavigation(format!(
        "Query '[{key}={value}]' matched multiple objects ({n}). Path must be unique for encryption."
      ))),
    };
  }
  Ok(step(tree, node, segment))
}

fn step(tree: &Tree, node: usize, segment: &PathSegment) -> Option<usize> {
  match (&tree.nodes[node], segment) {
    (Node::Map { entries, .. }, PathSegment::Key(key)) => entries.iter().find(|e| e.key == *key).map(|e| e.value),
    (Node::Seq { items, .. }, PathSegment::Index(index)) => items.get(*index).copied(),
    (Node::Seq { items, .. }, PathSegment::Query { key, value }) => items.iter().copied().find(|item| {
      matches!(&tree.nodes[*item], Node::Map { entries, .. }
        if entries.iter().any(|e| e.key == *key && matches!(&tree.nodes[e.value], Node::Scalar { value: v, .. } if v == value)))
    }),
    _ => None,
  }
}

fn value_of(tree: &Tree, node: usize) -> Value {
  match &tree.nodes[node] {
    Node::Scalar { value, .. } => Value::String(value.clone()),
    Node::Seq { items, .. } => Value::Array(items.iter().map(|i| value_of(tree, *i)).collect()),
    Node::Map { entries, .. } => {
      Value::Map(entries.iter().map(|e| (e.key.clone(), value_of(tree, e.value))).collect())
    }
  }
}

pub fn get(text: &str, segments: &[PathSegment]) -> Result<Option<Value>, C5CoreError> {
  let tree = tree_of(text)?;
  Ok(find(&tree, segments).map(|node| value_of(&tree, node)))
}

/// Whether a string can be written as it is and read back as the same string.
fn plain(text: &str) -> bool {
  if text.is_empty() || text != text.trim() {
    return false;
  }
  if text.contains(['\n', '\r', '\t']) || text.contains(": ") || text.contains(" #") || text.ends_with(':') {
    return false;
  }
  let first = text.chars().next().expect("checked not empty");
  if "-?:,[]{}#&*!|>'\"%@`".contains(first) {
    return false;
  }
  // `.inf` and `.nan` are floats to a YAML reader, so a leading dot is never
  // written bare.
  if first == '.' {
    return false;
  }
  if matches!(text.to_ascii_lowercase().as_str(), "true" | "false" | "null" | "~" | "yes" | "no" | "on" | "off") {
    return false;
  }
  true
}

/// A value written as it is reads back as the same string, unless it would
/// read back as a number instead.
fn plain_value(text: &str) -> bool {
  plain(text) && text.parse::<f64>().is_err()
}

/// A key that is written bare, which leaves a numeric key an integer key,
/// the way c5store has always filed one.
fn quoted_key(text: &str) -> String {
  if plain(text) {
    return text.to_owned();
  }
  escaped(text)
}

/// A YAML string that reads back as itself: as written when that is
/// unambiguous, quoted when it is not.
fn quoted(text: &str) -> String {
  if plain_value(text) {
    return text.to_owned();
  }
  escaped(text)
}

fn escaped(text: &str) -> String {
  let mut out = String::with_capacity(text.len() + 2);
  out.push('"');
  for c in text.chars() {
    match c {
      '"' => out.push_str("\\\""),
      '\\' => out.push_str("\\\\"),
      '\n' => out.push_str("\\n"),
      c => out.push(c),
    }
  }
  out.push('"');
  out
}

/// `value` as block YAML, every line already carrying `at`, the first one
/// written after `head` on its own line.
fn render(head: &str, value: &Value, at: &str, unit: &str, out: &mut Vec<String>) {
  match value {
    Value::Null => out.push(format!("{at}{head} null")),
    Value::Bool(b) => out.push(format!("{at}{head} {b}")),
    Value::Int(n) => out.push(format!("{at}{head} {n}")),
    Value::Float(n) => out.push(format!("{at}{head} {n}")),
    Value::String(s) => out.push(format!("{at}{head} {}", quoted(s))),
    Value::Array(items) if items.is_empty() => out.push(format!("{at}{head} []")),
    Value::Array(items) => {
      out.push(format!("{at}{head}"));
      let inner = format!("{at}{unit}");
      for item in items {
        render("-", item, &inner, unit, out);
      }
    }
    Value::Map(entries) if entries.is_empty() => out.push(format!("{at}{head} {{}}")),
    Value::Map(entries) => {
      out.push(format!("{at}{head}"));
      let inner = format!("{at}{unit}");
      for (key, item) in entries {
        render(&format!("{}:", quoted_key(key)), item, &inner, unit, out);
      }
    }
  }
}

/// The lines an entry occupies: its key line through the last line belonging
/// to its value. Blank lines and comments trailing the value belong to
/// whatever comes next and are left where they are.
fn extent(lines: &[&str], key_line: usize, key_indent: usize) -> (usize, usize) {
  let mut end = key_line + 1;
  let mut last_content = end;
  while end < lines.len() {
    let line = lines[end];
    let trimmed = line.trim_start();
    if trimmed.is_empty() || trimmed.starts_with('#') {
      end += 1;
      continue;
    }
    if leading(line).len() <= key_indent {
      break;
    }
    end += 1;
    last_content = end;
  }
  (key_line, last_content)
}

pub fn set(text: &str, segments: &[PathSegment], value: &Value) -> Result<String, C5CoreError> {
  let tree = tree_of(text)?;
  let unit = unit_of(text);
  let lines: Vec<&str> = text.lines().collect();

  // As far down the path as the document already goes.
  let mut node = tree.root;
  let mut depth = 0;
  while depth < segments.len() {
    let Some(current) = node else { break };
    match step_once(&tree, current, &segments[depth])? {
      Some(next) => {
        node = Some(next);
        depth += 1;
      }
      None => break,
    }
  }

  if depth == segments.len() {
    return replace(text, &lines, &tree, segments, node.expect("walked the whole path"), value, &unit);
  }
  insert(text, &lines, &tree, segments, node, depth, value, &unit)
}

/// The path exists: its entry's lines go, the rendering takes their place.
fn replace(
  text: &str,
  lines: &[&str],
  tree: &Tree,
  segments: &[PathSegment],
  node: usize,
  value: &Value,
  unit: &str,
) -> Result<String, C5CoreError> {
  let (key, key_at) = key_of(tree, segments, node).ok_or_else(|| {
    C5CoreError::YamlNavigation(format!("`{}` is not an entry of a mapping, so it cannot be replaced.", name(segments)))
  })?;
  let value_at = tree.nodes[node].at();
  if value_at.line == key_at.line && !matches!(tree.nodes[node], Node::Scalar { .. }) {
    return Err(C5CoreError::YamlNavigation(format!(
      "`{}` is written in flow style on one line, which cannot be edited in place; rewrite it as a block first.",
      name(segments)
    )));
  }
  let key_line = key_at.line - 1;
  let key_indent = " ".repeat(key_at.col);
  let (start, end) = extent(lines, key_line, key_indent.len());

  let mut rendered = Vec::new();
  render(&format!("{}:", quoted_key(&key)), value, &key_indent, unit, &mut rendered);
  Ok(splice(text, lines, start, end, rendered))
}

/// The path stops short: the deepest map that exists gains an entry, and the
/// rest of the path is written under it.
fn insert(
  text: &str,
  lines: &[&str],
  tree: &Tree,
  segments: &[PathSegment],
  node: Option<usize>,
  depth: usize,
  value: &Value,
  unit: &str,
) -> Result<String, C5CoreError> {
  // Everything the path still has to say, innermost first.
  let mut nested = value.clone();
  for segment in segments[depth + 1..].iter().rev() {
    let PathSegment::Key(key) = segment else {
      return Err(uncreatable(segment));
    };
    nested = Value::Map(vec![((*key).to_owned(), nested)]);
  }
  let PathSegment::Key(key) = &segments[depth] else {
    return Err(uncreatable(&segments[depth]));
  };

  let Some(parent) = node else {
    // An empty document: the whole path is the document.
    let mut rendered = Vec::new();
    render(&format!("{}:", quoted_key(key)), &nested, "", unit, &mut rendered);
    let mut out = rendered.join("\n");
    out.push('\n');
    return Ok(out);
  };

  let Node::Map { entries, at } = &tree.nodes[parent] else {
    return Err(C5CoreError::YamlNavigation(format!(
      "`{}` is not a mapping, so `{key}` cannot be added to it.",
      name(&segments[..depth])
    )));
  };

  let (indent, after) = match entries.last() {
    Some(last) => {
      let line = last.key_at.line - 1;
      let indent = " ".repeat(last.key_at.col);
      let (_, end) = extent(lines, line, indent.len());
      (indent, end)
    }
    // A mapping with no entries at all is only the document itself.
    None => (" ".repeat(at.col), lines.len()),
  };

  let mut rendered = Vec::new();
  render(&format!("{}:", quoted_key(key)), &nested, &indent, unit, &mut rendered);
  Ok(splice(text, lines, after, after, rendered))
}

/// The key an entry is filed under, and where that key is written.
fn key_of(tree: &Tree, segments: &[PathSegment], node: usize) -> Option<(String, At)> {
  let PathSegment::Key(_) = segments.last()? else { return None };
  tree
    .nodes
    .iter()
    .find_map(|n| match n {
      Node::Map { entries, .. } => entries.iter().find(|e| e.value == node).map(|e| (e.key.clone(), e.key_at)),
      _ => None,
    })
}

/// A segment that names something the document has to already hold.
fn uncreatable(segment: &PathSegment) -> C5CoreError {
  C5CoreError::YamlNavigation(match segment {
    PathSegment::Query { key, value } => {
      format!("Query '[{key}={value}]' matched no objects. Cannot encrypt.")
    }
    PathSegment::Index(index) => format!("Index [{index}] is out of bounds. Cannot encrypt."),
    PathSegment::Key(key) => format!("`{key}` cannot be created here."),
  })
}

fn name(segments: &[PathSegment]) -> String {
  segments
    .iter()
    .map(|s| match s {
      PathSegment::Key(k) => (*k).to_owned(),
      PathSegment::Index(i) => format!("[{i}]"),
      PathSegment::Query { key, value } => format!("[{key}={value}]"),
    })
    .collect::<Vec<_>>()
    .join(".")
}

/// Lines `start..end` become `rendered`; the trailing newline the file had is
/// the trailing newline it keeps.
fn splice(text: &str, lines: &[&str], start: usize, end: usize, rendered: Vec<String>) -> String {
  let mut out: Vec<String> = Vec::with_capacity(lines.len() + rendered.len());
  out.extend(lines[..start].iter().map(|l| (*l).to_owned()));
  out.extend(rendered);
  out.extend(lines[end..].iter().map(|l| (*l).to_owned()));
  let mut joined = out.join("\n");
  if text.ends_with('\n') || text.is_empty() {
    joined.push('\n');
  }
  joined
}