tocat-plugins 0.2.0

The plugins compiled into tocat, a socat-inspired relay
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
//! `hash` - compute the digest of bytes that traverse this stage
//!
//! ```toml
//! [[plugin]]
//! name = "hash"
//! algo = "sha256"
//! file = "digests.txt"
//! ```
//!
//! ```console
//! $ tocat file:big.iso hash tcp:host:9000
//! $ tocat -f tcp-listen:9000,fork -t file:capture.bin -p 'hash,algo=blake3'
//! ```
//!
//! Like [`tee`](crate::tee) and [`rate`](crate::rate) this never touches the
//! payload: every chunk is passed through untouched and the digest is folded in
//! on the way past. It can therefore sit anywhere in a chain, including on a
//! datagram path.
//!
//! # What is reported, and when
//!
//! `summary`, on by default, writes one line at end of stream: the digest of
//! everything that crossed the stage.
//!
//! `chunks` writes one line per chunk, each the digest of *that chunk alone*
//! rather than a running value. It is for locating where two streams diverge,
//! and it costs a finalisation and a write per chunk, so it is off by default.
//!
//! Lines follow the `sha256sum` shape, two spaces between the digest and what
//! it describes, then the algorithm and the hop this stage sits on:
//!
//! ```text
//! ba78…15ad  stream (sha256) [tcp://example.com:80_10.0.0.4:52134 -> STDIO | hash]
//! ```
//!
//! # There is not always an end of stream
//!
//! `summary` needs one, and two paths never reach it: a datagram source, and a
//! [`pipe`] held open across producers. On those, `chunks` is the only thing
//! that will ever report. That is a property of the path rather than of this
//! stage, and it is the same reason `rate` reports on a timer as well as at the
//! end.

use std::{fmt::Write, path::PathBuf};

use digest::{Digest as Dig, DynDigest};
use serde::{Deserialize, Serialize};
use tocat_api::{
    Boundaries, BuildCtx, ChannelId, ChannelTarget, Ctx, Plugin, PluginError, PluginFactory,
    Result, Stage,
};

pub const NAME: &str = "hash";

/// Which digest to compute.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Algorithm {
    Md5,
    Sha1,
    #[serde(alias = "sha2-224")]
    Sha224,
    #[serde(alias = "sha2", alias = "sha2-256")]
    #[default]
    Sha256,
    #[serde(alias = "sha2-384")]
    Sha384,
    #[serde(alias = "sha2-512")]
    Sha512,
    Sha3_224,
    #[serde(alias = "sha3")]
    Sha3_256,
    Sha3_384,
    Sha3_512,
    #[serde(alias = "blake2b")]
    Blake2,
    #[serde(alias = "blake")]
    Blake3,
}

/// Wrapper around hashers
///
/// This is necessary because blake3 doesn't implement RustCrypto's DynDigest
/// trait, so we must provide a common wrapper ourselves
enum Hasher {
    Dynamic(Box<dyn DynDigest + Send>),
    Blake3(Box<blake3::Hasher>),
}

impl Hasher {
    /// Accumulate data in the hasher
    fn update(&mut self, data: &[u8]) {
        match self {
            Self::Dynamic(digest) => digest.update(data),
            Self::Blake3(hasher) => {
                hasher.update(data);
            }
        }
    }

    /// Compute the digest and reset the hasher
    fn finish_reset(&mut self) -> Box<[u8]> {
        match self {
            Self::Dynamic(digest) => digest.finalize_reset(),
            Self::Blake3(hasher) => {
                let hash = hasher.finalize();
                hasher.reset();
                hash.as_slice().into()
            }
        }
    }
}

impl Algorithm {
    fn hasher(&self) -> Hasher {
        match self {
            Algorithm::Md5 => Hasher::Dynamic(Box::new(md5::Md5::new())),
            Algorithm::Sha1 => Hasher::Dynamic(Box::new(sha1::Sha1::new())),
            Algorithm::Sha224 => Hasher::Dynamic(Box::new(sha2::Sha224::new())),
            Algorithm::Sha256 => Hasher::Dynamic(Box::new(sha2::Sha256::new())),
            Algorithm::Sha384 => Hasher::Dynamic(Box::new(sha2::Sha384::new())),
            Algorithm::Sha512 => Hasher::Dynamic(Box::new(sha2::Sha512::new())),
            Algorithm::Sha3_224 => Hasher::Dynamic(Box::new(sha3::Sha3_224::new())),
            Algorithm::Sha3_256 => Hasher::Dynamic(Box::new(sha3::Sha3_256::new())),
            Algorithm::Sha3_384 => Hasher::Dynamic(Box::new(sha3::Sha3_384::new())),
            Algorithm::Sha3_512 => Hasher::Dynamic(Box::new(sha3::Sha3_512::new())),
            Algorithm::Blake2 => Hasher::Dynamic(Box::new(blake2::Blake2b512::new())),
            Algorithm::Blake3 => Hasher::Blake3(Box::new(blake3::Hasher::new())),
        }
    }
}

impl std::fmt::Display for Algorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Algorithm::Md5 => write!(f, "md5"),
            Algorithm::Sha1 => write!(f, "sha1"),
            Algorithm::Sha224 => write!(f, "sha224"),
            Algorithm::Sha256 => write!(f, "sha256"),
            Algorithm::Sha384 => write!(f, "sha384"),
            Algorithm::Sha512 => write!(f, "sha512"),
            Algorithm::Sha3_224 => write!(f, "sha3-224"),
            Algorithm::Sha3_256 => write!(f, "sha3-256"),
            Algorithm::Sha3_384 => write!(f, "sha3-384"),
            Algorithm::Sha3_512 => write!(f, "sha3-512"),
            Algorithm::Blake2 => write!(f, "blake2"),
            Algorithm::Blake3 => write!(f, "blake3"),
        }
    }
}

fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct HashConfig {
    /// Algorithm to use
    #[serde(default, alias = "algo", alias = "alg", alias = "hasher")]
    pub algorithm: Algorithm,

    /// Individually hash each inbound chunk
    #[serde(default)]
    pub chunks: bool,

    /// Hash entire stream of data
    #[serde(default = "default_true")]
    pub summary: bool,

    /// Write to specific path
    #[serde(default)]
    pub file: Option<PathBuf>,

    /// Append instead of truncate
    #[serde(default = "default_true")]
    pub append: bool,
}

impl HashConfig {
    /// Resolve `file` to a channel target.
    ///
    /// stdout is rejected rather than supported: on a stdio endpoint it carries
    /// relay payload, and mixing a dump into it corrupts the transfer.
    pub fn target(&self) -> Result<ChannelTarget> {
        let Some(path) = &self.file else {
            return Ok(ChannelTarget::Stderr);
        };

        match path.to_str() {
            Some("-" | "stderr" | "/dev/stderr" | "/dev/fd/2") => Ok(ChannelTarget::Stderr),
            Some("stdout" | "/dev/stdout" | "/dev/fd/1") => Err(PluginError::config(
                NAME,
                "refusing to write to stdout, it may carry relay payload; use `-` for stderr",
            )),
            _ => Ok(ChannelTarget::File {
                path: path.clone(),
                append: self.append,
            }),
        }
    }
}

pub struct Hash {
    algorithm: Algorithm,
    chunk: Option<Hasher>,
    stream: Option<Hasher>,
    channel: ChannelId,
    chunks_seen: u64,
    label: String,
    line: String,
}

impl Hash {
    fn write_line(&mut self, ctx: &mut Ctx<'_>, digest: &[u8], what: std::fmt::Arguments<'_>) {
        self.line.clear();

        for byte in digest {
            const HEX: [u8; 16] = *b"0123456789abcdef";

            self.line.push(HEX[usize::from(byte >> 4)] as char);
            self.line.push(HEX[usize::from(byte & 0x0f)] as char);
        }

        let _ = writeln!(
            self.line,
            "  {what} ({algorithm}) [{label}]",
            algorithm = self.algorithm,
            label = self.label
        );

        ctx.side_write(self.channel, self.line.as_bytes());
    }
}

impl Plugin for Hash {
    fn name(&self) -> &str {
        NAME
    }

    fn on_bytes(&mut self, ctx: &mut Ctx<'_>, input: &[u8]) -> Result<()> {
        ctx.pass_through();

        if let Some(stream) = &mut self.stream {
            stream.update(input);
        }

        let digest = self.chunk.as_mut().map(|chunk| {
            chunk.update(input);
            chunk.finish_reset()
        });

        if let Some(digest) = digest {
            self.chunks_seen += 1;

            let n = self.chunks_seen;
            self.write_line(ctx, &digest, format_args!("chunk {n}"));
        }

        Ok(())
    }

    fn on_eof(&mut self, ctx: &mut Ctx<'_>) -> Result<()> {
        let digest = self.stream.as_mut().map(Hasher::finish_reset);

        if let Some(digest) = digest {
            self.write_line(ctx, &digest, format_args!("stream"));
        }

        Ok(())
    }

    /// Safe on a datagram path since hashing a message does not change it
    fn boundaries(&self) -> Boundaries {
        Boundaries::Preserve
    }
}

pub struct HashFactory;

impl PluginFactory for HashFactory {
    fn name(&self) -> &str {
        NAME
    }

    fn description(&self) -> &str {
        "digest the bytes crossing this stage"
    }

    fn build(&self, ctx: &mut BuildCtx<'_>) -> Result<Stage> {
        let config: HashConfig = ctx.config()?;

        if !config.summary && !config.chunks {
            return Err(PluginError::config(
                NAME,
                "summary and chunks are both off, so this stage would do nothing",
            ));
        }

        let channel = ctx.open_channel(config.target()?)?;
        let stage = ctx.stage();
        let label = format!("{} | {}", stage.label(), stage.name);

        Ok(Stage::filter(Hash {
            algorithm: config.algorithm,
            chunk: config.chunks.then(|| config.algorithm.hasher()),
            stream: config.summary.then(|| config.algorithm.hasher()),
            channel,
            chunks_seen: 0,
            label,
            line: String::new(),
        }))
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use tocat_api::{
        ChannelId, ChannelTarget, Direction, EffectSink, Emission, Emit, HostBuilder, LogLevel,
        PipelineMeta, Result as PluginResult, StageInfo,
    };

    use super::*;

    /// Digests are written to a side channel rather than emitted, so this is
    /// what the assertions are about.
    #[derive(Default)]
    struct Recorder {
        written: Vec<String>,
    }

    impl EffectSink for Recorder {
        fn write(&mut self, _channel: ChannelId, bytes: &[u8]) {
            self.written
                .push(String::from_utf8_lossy(bytes).into_owned());
        }

        fn log(&mut self, _level: LogLevel, _stage: &str, _message: &str) {}
    }

    struct NullHost;

    impl HostBuilder for NullHost {
        fn open_channel(&mut self, _target: ChannelTarget) -> PluginResult<ChannelId> {
            Ok(ChannelId(0))
        }
    }

    fn meta() -> PipelineMeta {
        PipelineMeta::new(Direction::SourceToSink, "src", "sink")
    }

    fn stage() -> StageInfo<'static> {
        StageInfo {
            index: 0,
            total: 1,
            name: NAME,
            upstream: "src",
            downstream: "sink",
        }
    }

    fn try_build(config: serde_json::Value) -> PluginResult<Box<dyn Plugin>> {
        let map = config.as_object().expect("object").clone();
        let meta = meta();
        let mut host = NullHost;
        let mut ctx = BuildCtx::new(NAME, &map, &meta, stage(), &mut host);

        match HashFactory.build(&mut ctx)? {
            Stage::Filter(plugin) => Ok(plugin),
            Stage::External(_) => unreachable!("hash is a filter"),
        }
    }

    fn build(config: serde_json::Value) -> Box<dyn Plugin> {
        try_build(config).expect("build")
    }

    fn build_config(config: serde_json::Value) -> HashConfig {
        let map = config.as_object().expect("object").clone();
        let meta = meta();
        let mut host = NullHost;
        let ctx = BuildCtx::new(NAME, &map, &meta, stage(), &mut host);

        ctx.config().expect("config")
    }

    /// One chunk, returning the emission so a test can assert the payload was
    /// never materialised.
    fn feed(plugin: &mut dyn Plugin, sink: &mut Recorder, input: &[u8]) -> Emission {
        let meta = meta();
        let mut emission = Emission::new();

        {
            let mut ctx = Ctx::new(&meta, NAME, input, &mut emission, sink);
            plugin.on_bytes(&mut ctx, input).expect("on_bytes");
        }

        emission
    }

    fn eof(plugin: &mut dyn Plugin, sink: &mut Recorder) {
        let meta = meta();
        let mut emission = Emission::new();

        {
            let mut ctx = Ctx::new(&meta, NAME, &[], &mut emission, sink);
            plugin.on_eof(&mut ctx).expect("on_eof");
        }
    }

    /// The digest out of a line, which is everything before the two spaces.
    fn digest_of(line: &str) -> &str {
        line.split("  ").next().expect("a digest")
    }

    #[test]
    fn the_payload_is_never_materialised() {
        let mut plugin = build(json!({}));
        let mut sink = Recorder::default();

        let emission = feed(&mut *plugin, &mut sink, b"abc");

        assert_eq!(emission.emit(), Emit::Passthrough);
        assert!(emission.bytes().is_empty(), "hash must not copy the stream");
    }

    #[test]
    fn the_summary_is_the_digest_of_everything() {
        let mut plugin = build(json!({"algo": "sha256"}));
        let mut sink = Recorder::default();

        feed(&mut *plugin, &mut sink, b"a");
        feed(&mut *plugin, &mut sink, b"bc");
        assert!(sink.written.is_empty(), "nothing is reported until the end");

        eof(&mut *plugin, &mut sink);

        assert_eq!(sink.written.len(), 1);
        assert_eq!(
            digest_of(&sink.written[0]),
            "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
            "sha256 of \"abc\", however it was chunked",
        );
        assert!(sink.written[0].contains("stream (sha256)"));
        assert!(sink.written[0].ends_with("[src -> sink | hash]\n"));
    }

    #[test]
    fn the_algorithm_is_the_one_that_was_asked_for() {
        let mut plugin = build(json!({"algo": "sha1"}));
        let mut sink = Recorder::default();

        feed(&mut *plugin, &mut sink, b"abc");
        eof(&mut *plugin, &mut sink);

        assert_eq!(
            digest_of(&sink.written[0]),
            "a9993e364706816aba3e25717850c26c9cd0d89d",
        );
    }

    #[test]
    fn chunks_are_digested_one_at_a_time() {
        let mut plugin = build(json!({"chunks": true, "summary": false}));
        let mut sink = Recorder::default();

        feed(&mut *plugin, &mut sink, b"abc");
        feed(&mut *plugin, &mut sink, b"def");

        assert_eq!(sink.written.len(), 2);
        assert!(sink.written[0].contains("chunk 1"), "chunks are one-based");
        assert!(sink.written[1].contains("chunk 2"));
        assert_ne!(
            digest_of(&sink.written[0]),
            digest_of(&sink.written[1]),
            "each line is that chunk alone, not a running value",
        );
    }

    /// The one relationship between the two modes that has to hold: with a
    /// single chunk, that chunk's digest is the stream's.
    #[test]
    fn one_chunk_digests_the_same_either_way() {
        let mut plugin = build(json!({"chunks": true}));
        let mut sink = Recorder::default();

        feed(&mut *plugin, &mut sink, b"abc");
        eof(&mut *plugin, &mut sink);

        assert_eq!(sink.written.len(), 2);
        assert_eq!(digest_of(&sink.written[0]), digest_of(&sink.written[1]));
    }

    #[test]
    fn a_hasher_left_over_from_one_stream_does_not_taint_the_next() {
        let mut plugin = build(json!({"chunks": true, "summary": false}));
        let mut sink = Recorder::default();

        feed(&mut *plugin, &mut sink, b"abc");
        feed(&mut *plugin, &mut sink, b"abc");

        assert_eq!(
            digest_of(&sink.written[0]),
            digest_of(&sink.written[1]),
            "finish_reset must leave the hasher empty",
        );
    }

    #[test]
    fn the_algorithm_is_spelled_however_you_like() {
        for spelling in ["sha256", "SHA-256", "sha_256", "sha2"] {
            assert_eq!(
                build_config(json!({ "algo": spelling })).algorithm,
                Algorithm::Sha256,
                "{spelling} did not name sha256",
            );
        }

        assert_eq!(
            build_config(json!({"algo": "sha3-512"})).algorithm,
            Algorithm::Sha3_512,
        );
        assert_eq!(
            build_config(json!({"algo": "blake"})).algorithm,
            Algorithm::Blake3,
        );
    }

    #[test]
    fn the_defaults_are_a_stream_digest_on_stderr() {
        let config = build_config(json!({}));

        assert_eq!(config.algorithm, Algorithm::Sha256);
        assert!(config.summary);
        assert!(!config.chunks);
        assert!(config.append);
        assert!(matches!(config.target(), Ok(ChannelTarget::Stderr)));
    }

    #[test]
    fn an_unknown_algorithm_or_option_is_rejected() {
        assert!(try_build(json!({"algo": "crc32"})).is_err());
        assert!(try_build(json!({"algorithm": "sha256", "bytes": 8})).is_err());
    }

    #[test]
    fn a_stage_that_would_report_nothing_is_rejected() {
        assert!(
            try_build(json!({"summary": false})).is_err(),
            "neither summary nor chunks is a typo, not an intent",
        );
    }

    #[test]
    fn stdout_is_refused_because_it_may_carry_payload() {
        assert!(try_build(json!({"file": "stdout"})).is_err());
        assert!(try_build(json!({"file": "-"})).is_ok(), "`-` is stderr");
    }
}