dtshimd 0.9.0

Run host commands from inside a Docker container: the host daemon
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
use std::{os::unix::process::ExitStatusExt, process::Stdio, sync::Arc, time::Duration};

use anyhow::{Context, Result, bail};
use dtshim_core::model::Message;
use futures_util::{SinkExt, StreamExt};
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
    process::{Child, Command},
    time::timeout,
};
use tokio_util::{
    codec::{FramedRead, FramedWrite},
    sync::CancellationToken,
};

use crate::{codec::PostcardCodec, context::Ctx};

const START_TIMEOUT: Duration = Duration::from_secs(10);

pub(crate) async fn run<S>(
    stream: S,
    cancellation: CancellationToken,
    context: Arc<Ctx>,
) -> Result<()>
where
    S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
    let volumes = &context.volumes;
    let debug = context.debug;
    let mut child: Option<Child> = None;

    let result = {
        let connection = async {
            let (rd, wr) = tokio::io::split(stream);
            let mut rx = FramedRead::new(rd, PostcardCodec::new());
            let mut tx = FramedWrite::new(wr, PostcardCodec::new());

            let message = match timeout(START_TIMEOUT, rx.next()).await {
                Ok(Some(message)) => message.context("failed to receive start message")?,
                Ok(None) => bail!("client disconnected before sending start message"),
                Err(_) => {
                    tx.send(Message::Failed("start message timed out".into()))
                        .await?;
                    return Ok(());
                }
            };
            let Message::Start(start) = message else {
                bail!("the first message should be a start");
            };
            if !context.authenticates(start.token.as_deref()) {
                tx.send(Message::Failed("authentication failed".into()))
                    .await
                    .context("failed to send authentication denial")?;
                return Ok(());
            }
            let start = match context.prepare(start) {
                Ok(start) => start,
                Err(err) => {
                    let message = volumes.map_from_host(&format!("policy denied: {err:#}"));
                    tx.send(Message::Failed(message))
                        .await
                        .context("failed to send policy denial")?;
                    return Ok(());
                }
            };
            if debug {
                eprintln!("start: {:?} {:?}", start.command, start.args);
            } else {
                eprintln!("start: {:?}", start.command);
            }
            child = match Command::new(&start.command)
                .args(&start.args)
                .envs(start.env)
                .current_dir(&start.cwd)
                .stderr(Stdio::piped())
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .kill_on_drop(true)
                .spawn()
                .with_context(|| format!("failed to spawn command {:?}", start.command))
            {
                Ok(child) => Some(child),
                Err(err) => {
                    eprintln!("failed: {err:#}");
                    let message = volumes.map_from_host(&format!("{err:#}"));
                    let _ = tx.send(Message::Failed(message)).await;
                    return Err(err);
                }
            };

            let child = child.as_mut().context("child was not initialized")?;
            let mut stdin = Some(child.stdin.take().context("failed to take child stdin")?);
            let mut stdout = child.stdout.take().context("failed to take child stdout")?;
            let mut stderr = child.stderr.take().context("failed to take child stderr")?;
            let mut out_buf = vec![0u8; 8192];
            let mut err_buf = vec![0u8; 8192];

            let input = async {
                let mut mapper = volumes.stream_to_host();
                while let Some(msg) = rx.next().await {
                    match msg.context("failed to receive client message")? {
                        Message::Stdin(data) => {
                            if debug && !data.is_empty() {
                                eprintln!("stdin: {:?}", String::from_utf8_lossy(&data));
                            }
                            let Some(pipe) = stdin.as_mut() else {
                                continue;
                            };
                            let eof = data.is_empty();
                            let data = if eof {
                                mapper.finish()
                            } else {
                                mapper.push(&data)
                            };
                            if let Err(err) = pipe.write_all(&data).await {
                                eprintln!("stdin error: {err}");
                                stdin.take();
                            } else if eof {
                                stdin.take();
                                if debug {
                                    eprintln!("stdin: closed");
                                }
                            }
                        }
                        other => bail!("unexpected message from client: {}", other.kind()),
                    }
                }
                bail!("client disconnected")
            };

            let output = async {
                let mut stdout_done = false;
                let mut stderr_done = false;
                let mut stdout_mapper = volumes.stream_from_host();
                let mut stderr_mapper = volumes.stream_from_host();

                while !stdout_done || !stderr_done {
                    let message = tokio::select! {
                        result = stdout.read(&mut out_buf), if !stdout_done => {
                            match result.context("failed to read child stdout")? {
                                0 => {
                                    stdout_done = true;
                                    if debug {
                                        eprintln!("stdout: closed");
                                    }
                                    let data = stdout_mapper.finish();
                                    (!data.is_empty()).then_some(Message::Stdout(data))
                                }
                                n => {
                                    if debug {
                                        eprintln!("stdout: {:?}", String::from_utf8_lossy(&out_buf[..n]));
                                    }
                                    let data = stdout_mapper.push(&out_buf[..n]);
                                    (!data.is_empty()).then_some(Message::Stdout(data))
                                }
                            }
                        }

                        result = stderr.read(&mut err_buf), if !stderr_done => {
                            match result.context("failed to read child stderr")? {
                                0 => {
                                    stderr_done = true;
                                    if debug {
                                        eprintln!("stderr: closed");
                                    }
                                    let data = stderr_mapper.finish();
                                    (!data.is_empty()).then_some(Message::Stderr(data))
                                }
                                n => {
                                    if debug {
                                        eprintln!("stderr: {:?}", String::from_utf8_lossy(&err_buf[..n]));
                                    }
                                    let data = stderr_mapper.push(&err_buf[..n]);
                                    (!data.is_empty()).then_some(Message::Stderr(data))
                                }
                            }
                        }
                    };

                    if let Some(message) = message {
                        tx.send(message)
                            .await
                            .context("failed to send child output")?;
                    }
                }

                let status = match child
                    .wait()
                    .await
                    .context("failed to wait for child process")
                {
                    Ok(status) => status,
                    Err(err) => {
                        eprintln!("failed: {err:#}");
                        let _ = tx.send(Message::Failed(format!("{err:#}"))).await;
                        return Err(err);
                    }
                };

                if let Some(code) = status.code() {
                    eprintln!("code: {code}");
                    tx.send(Message::Code(code))
                        .await
                        .context("failed to send child exit code")?;
                } else if let Some(signal) = status.signal() {
                    eprintln!("signaled: {signal}");
                    tx.send(Message::Signaled(signal))
                        .await
                        .context("failed to send child termination signal")?;
                }

                anyhow::Ok(())
            };

            tokio::select! {
                result = input => result,
                result = output => result,
            }
        };

        tokio::select! {
            biased;
            _ = cancellation.cancelled() => {
                if debug {
                    eprintln!("cancelled");
                }
                Ok(())
            },
            result = connection => result,
        }
    };

    if let Some(child) = child.as_mut()
        && let Err(err) = child
            .kill()
            .await
            .context("failed to terminate child process")
    {
        if result.is_ok() {
            return Err(err);
        }
        eprintln!("{err:#}");
    }

    result
}

#[cfg(test)]
mod tests {
    use dtshim_core::model::Start;
    use tokio::{
        io::{DuplexStream, duplex},
        task::JoinHandle,
    };
    use tokio_util::codec::Framed;

    use crate::{policy::Policy, volume::Volumes};

    use super::*;

    fn start(script: &str) -> Start {
        Start {
            token: None,
            command: "/bin/sh".into(),
            args: vec!["-c".into(), script.into()],
            env: Vec::new(),
            cwd: "/".into(),
        }
    }

    fn connect(
        context: Ctx,
    ) -> (
        Framed<DuplexStream, PostcardCodec>,
        CancellationToken,
        JoinHandle<Result<()>>,
    ) {
        let (client, server) = duplex(65536);
        let cancellation = CancellationToken::new();
        let task = tokio::spawn(run(server, cancellation.clone(), Arc::new(context)));
        (
            Framed::new(client, PostcardCodec::new()),
            cancellation,
            task,
        )
    }

    fn context(token: Option<&str>) -> Ctx {
        Ctx {
            token: token.map(str::to_owned),
            policy: Policy::default(),
            volumes: Volumes::default(),
            debug: false,
        }
    }

    async fn receive(client: &mut Framed<DuplexStream, PostcardCodec>) -> Message {
        timeout(Duration::from_secs(5), client.next())
            .await
            .unwrap()
            .unwrap()
            .unwrap()
    }

    #[tokio::test]
    async fn authentication_rejects_missing_or_wrong_tokens_before_policy_and_spawn() {
        let directory = tempfile::tempdir().unwrap();
        let marker = directory.path().join("spawned");
        for token in [None, Some("wrong")] {
            let (mut client, _, task) = connect(context(Some("required")));
            let mut request = start(&format!("touch '{}'", marker.display()));
            request.token = token.map(str::to_owned);
            request.cwd = "/does-not-exist".into();
            client.send(Message::Start(request)).await.unwrap();
            assert!(
                matches!(receive(&mut client).await, Message::Failed(message) if message == "authentication failed")
            );
            task.await.unwrap().unwrap();
            assert!(!marker.exists());
        }
    }

    #[tokio::test]
    async fn token_is_optional_when_disabled_and_compared_before_volume_mapping() {
        for (expected, provided) in [
            (None, None),
            (None, Some("anything")),
            (Some("/app/token"), Some("/app/token")),
        ] {
            let mut ctx = context(expected);
            ctx.volumes = Volumes::new(vec!["/host:/app".parse().unwrap()]).unwrap();
            let (mut client, _, task) = connect(ctx);
            let mut request = start("exit 0");
            request.token = provided.map(str::to_owned);
            client.send(Message::Start(request)).await.unwrap();
            assert!(matches!(receive(&mut client).await, Message::Code(0)));
            task.await.unwrap().unwrap();
        }
    }

    #[tokio::test]
    async fn explicit_stdin_eof_keeps_session_alive_and_drains_both_outputs_before_status() {
        let (mut client, _, task) = connect(context(None));
        client
            .send(Message::Start(start(
                "cat >/dev/null; printf 'final stdout'; printf 'final stderr' >&2; exit 7",
            )))
            .await
            .unwrap();
        client
            .send(Message::Stdin(b"input".to_vec()))
            .await
            .unwrap();
        client.send(Message::Stdin(Vec::new())).await.unwrap();
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        loop {
            match receive(&mut client).await {
                Message::Stdout(data) => stdout.extend(data),
                Message::Stderr(data) => stderr.extend(data),
                Message::Code(7) => break,
                other => panic!("unexpected message: {other:?}"),
            }
        }
        assert_eq!(stdout, b"final stdout");
        assert_eq!(stderr, b"final stderr");
        task.await.unwrap().unwrap();
    }

    #[tokio::test]
    async fn disconnect_after_stdin_eof_or_broken_pipe_ends_the_session() {
        for (script, eof) in [
            ("cat >/dev/null; echo ready; exec sleep 60", true),
            ("exec 0<&-; echo ready; exec sleep 60", false),
        ] {
            let (mut client, _, task) = connect(context(None));
            client.send(Message::Start(start(script))).await.unwrap();
            if eof {
                client.send(Message::Stdin(Vec::new())).await.unwrap();
            }
            let mut output = String::new();
            while !output.contains('\n') {
                match receive(&mut client).await {
                    Message::Stdout(data) => output.push_str(std::str::from_utf8(&data).unwrap()),
                    other => panic!("unexpected message: {other:?}"),
                }
            }
            assert_eq!(output, "ready\n");
            if !eof {
                client
                    .send(Message::Stdin(b"write to closed stdin".to_vec()))
                    .await
                    .unwrap();
                tokio::task::yield_now().await;
            }
            drop(client);
            let result = timeout(Duration::from_secs(5), task)
                .await
                .unwrap()
                .unwrap();
            assert!(result.is_err());
        }
    }

    #[tokio::test]
    async fn handshake_deadline_does_not_limit_a_running_command_and_shutdown_ends_the_session() {
        let (mut client, cancellation, task) = connect(context(None));
        client
            .send(Message::Start(start("echo ready; exec sleep 60")))
            .await
            .unwrap();
        let mut output = String::new();
        while !output.contains('\n') {
            match receive(&mut client).await {
                Message::Stdout(data) => output.push_str(std::str::from_utf8(&data).unwrap()),
                other => panic!("unexpected message: {other:?}"),
            }
        }
        assert_eq!(output, "ready\n");
        tokio::time::pause();
        tokio::time::advance(START_TIMEOUT + Duration::from_secs(1)).await;
        tokio::task::yield_now().await;
        assert!(!task.is_finished());
        tokio::time::resume();
        cancellation.cancel();
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test(start_paused = true)]
    async fn first_complete_start_frame_has_a_deadline() {
        let (client, _, task) = connect(context(None));
        let mut stream = client.into_inner();
        stream.write_all(&[0, 0]).await.unwrap();
        tokio::task::yield_now().await;
        tokio::time::advance(START_TIMEOUT + Duration::from_secs(1)).await;
        let mut client = Framed::new(stream, PostcardCodec::new());
        assert!(
            matches!(receive(&mut client).await, Message::Failed(message) if message == "start message timed out")
        );
        task.await.unwrap().unwrap();
    }

    #[tokio::test]
    async fn handshake_rejects_disconnect_wrong_message_and_invalid_encoding() {
        let (client, _, task) = connect(context(None));
        drop(client);
        let error = timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap_err();
        assert_eq!(
            error.to_string(),
            "client disconnected before sending start message"
        );

        for message in [Message::Code(0), Message::Stdin(vec![])] {
            let (mut client, _, task) = connect(context(None));
            client.send(message).await.unwrap();
            let error = timeout(Duration::from_secs(5), task)
                .await
                .unwrap()
                .unwrap()
                .unwrap_err();
            assert_eq!(error.to_string(), "the first message should be a start");
        }

        let (client, _, task) = connect(context(None));
        client
            .into_inner()
            .write_all(&[0, 0, 0, 1, 255])
            .await
            .unwrap();
        let error = timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap_err();
        assert_eq!(error.to_string(), "failed to receive start message");
    }

    #[tokio::test]
    async fn cancelled_session_does_not_wait_for_a_start_message() {
        let (_client, cancellation, task) = connect(context(None));
        cancellation.cancel();
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn policy_denial_is_sent_without_spawning_a_child() {
        let (mut client, _, task) = connect(context(None));
        let mut request = start("exit 0");
        request.command.clear();
        client.send(Message::Start(request)).await.unwrap();
        assert!(
            matches!(receive(&mut client).await, Message::Failed(message) if message == "policy denied: command is empty")
        );
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn spawn_failures_are_sent_with_container_paths() {
        let directory = tempfile::tempdir_in("/tmp").unwrap();
        let mut ctx = context(None);
        ctx.volumes = Volumes::new(vec![
            format!("{}:/app", directory.path().display())
                .parse()
                .unwrap(),
        ])
        .unwrap();
        let (mut client, _, task) = connect(ctx);
        let mut request = start("exit 0");
        request.command = directory
            .path()
            .join("missing-command")
            .to_str()
            .unwrap()
            .into();
        client.send(Message::Start(request)).await.unwrap();
        let Message::Failed(message) = receive(&mut client).await else {
            panic!("expected spawn failure");
        };
        assert!(message.contains("/app/missing-command"), "{message}");
        assert!(
            !message.contains(directory.path().to_str().unwrap()),
            "{message}"
        );
        let error = timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap_err();
        assert!(error.to_string().starts_with("failed to spawn command"));
    }

    #[tokio::test]
    async fn child_signal_is_reported_as_a_signal_not_an_exit_code() {
        let (mut client, _, task) = connect(context(None));
        client
            .send(Message::Start(start("kill -TERM $$")))
            .await
            .unwrap();
        assert!(matches!(receive(&mut client).await, Message::Signaled(15)));
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn non_stdin_messages_end_the_session_without_exposing_the_payload() {
        let (mut client, _, task) = connect(context(None));
        client
            .send(Message::Start(start("printf ready; exec cat")))
            .await
            .unwrap();
        let mut ready = Vec::new();
        while ready.len() < b"ready".len() {
            match receive(&mut client).await {
                Message::Stdout(data) => ready.extend(data),
                other => panic!("unexpected message: {other:?}"),
            }
        }
        assert_eq!(ready, b"ready");
        let mut unexpected = start("echo unexpected argument");
        unexpected.token = Some("unexpected token".into());
        client.send(Message::Start(unexpected)).await.unwrap();
        let error = timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap_err();
        assert_eq!(error.to_string(), "unexpected message from client: Start");
    }

    #[tokio::test]
    async fn maps_stdin_across_frames_and_flushes_pending_output_at_eof() {
        let mut ctx = context(None);
        ctx.volumes = Volumes::new(vec!["/host:/app".parse().unwrap()]).unwrap();
        let (mut client, _, task) = connect(ctx);
        let mut request = start(
            "read -r value; [ \"$value\" = \"$EXPECTED\" ] || exit 42; printf '%s' \"$EXPECTED\"; printf '%s' \"$EXPECTED\" >&2",
        );
        request.env = vec![("EXPECTED".into(), "/host".into())];
        client.send(Message::Start(request)).await.unwrap();
        client.send(Message::Stdin(b"/ap".to_vec())).await.unwrap();
        client.send(Message::Stdin(b"p\n".to_vec())).await.unwrap();
        client.send(Message::Stdin(vec![])).await.unwrap();
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        loop {
            match receive(&mut client).await {
                Message::Stdout(data) => stdout.extend(data),
                Message::Stderr(data) => stderr.extend(data),
                Message::Code(0) => break,
                other => panic!("unexpected message: {other:?}"),
            }
        }
        assert_eq!(stdout, b"/app");
        assert_eq!(stderr, b"/app");
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn stdin_eof_flushes_an_exact_pending_path_and_ignores_later_stdin() {
        let mut ctx = context(None);
        ctx.volumes = Volumes::new(vec!["/host:/app".parse().unwrap()]).unwrap();
        let (mut client, _, task) = connect(ctx);
        let mut request = start("value=$(cat); [ \"$value\" = \"$EXPECTED\" ]");
        request.env = vec![("EXPECTED".into(), "/host".into())];
        client.send(Message::Start(request)).await.unwrap();
        client.send(Message::Stdin(b"/app".to_vec())).await.unwrap();
        client.send(Message::Stdin(vec![])).await.unwrap();
        client
            .send(Message::Stdin(b"ignored".to_vec()))
            .await
            .unwrap();
        assert!(matches!(receive(&mut client).await, Message::Code(0)));
        timeout(Duration::from_secs(5), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }
}