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
use super::redact::Redactor;
use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
use derive_builder::Builder;
use mockall::automock;
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::fmt::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use thiserror::Error;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::sync::RwLock;
use tracing::{debug, error, info};
use which::which_in;

#[derive(Debug, Default)]
struct RwLockOutput {
    output: RwLock<Vec<(DateTime<Utc>, String)>>,
}

impl RwLockOutput {
    async fn add_line(&self, line: &str) {
        let mut stdout = self.output.write().await;
        stdout.push((Utc::now(), line.to_string()));
    }
}

#[derive(Default, Builder)]
#[builder(setter(into))]
pub struct OutputCapture {
    #[builder(default)]
    pub working_dir: PathBuf,
    #[builder(default)]
    stdout: Vec<(DateTime<Utc>, String)>,
    #[builder(default)]
    stderr: Vec<(DateTime<Utc>, String)>,
    #[builder(default)]
    pub exit_code: Option<i32>,
    #[builder(default)]
    start_time: DateTime<Utc>,
    #[builder(default)]
    end_time: DateTime<Utc>,
    #[builder(default)]
    pub command: String,
}

#[derive(Clone, Debug)]
pub enum OutputDestination {
    StandardOut,
    Logging,
    Null,
}

#[derive(Error, Debug)]
pub enum CaptureError {
    #[error("Unable to process file. {error:?}")]
    IoError {
        #[from]
        error: std::io::Error,
    },
    #[error("File {name} was not executable or it did not exist.")]
    MissingShExec { name: String },
    #[error("Unable to parse UTF-8 output. {error:?}")]
    FromUtf8Error {
        #[from]
        error: std::string::FromUtf8Error,
    },
}

#[automock]
#[async_trait]
pub trait ExecutionProvider: Send + Sync {
    async fn run_command<'a>(&self, opts: CaptureOpts<'a>) -> Result<OutputCapture, CaptureError>;
}

#[derive(Default, Debug)]
pub struct DefaultExecutionProvider {}

#[async_trait]
impl ExecutionProvider for DefaultExecutionProvider {
    async fn run_command<'a>(&self, opts: CaptureOpts<'a>) -> Result<OutputCapture, CaptureError> {
        OutputCapture::capture_output(opts).await
    }
}

pub struct CaptureOpts<'a> {
    pub working_dir: &'a Path,
    pub env_vars: BTreeMap<String, String>,
    pub path: &'a str,
    pub args: &'a [String],
    pub output_dest: OutputDestination,
}

impl<'a> CaptureOpts<'a> {
    fn command(&self) -> String {
        self.args.join(" ")
    }
}

impl OutputCapture {
    pub async fn capture_output(opts: CaptureOpts<'_>) -> Result<Self, CaptureError> {
        check_pre_exec(&opts)?;
        let args = opts.args.to_vec();

        debug!("Executing PATH={} {:?}", &opts.path, &args);

        let start_time = Utc::now();
        let mut command = tokio::process::Command::new("/usr/bin/env");
        let mut child = command
            .arg("-S")
            .args(args)
            .env("PATH", opts.path)
            .envs(&opts.env_vars)
            .stderr(Stdio::piped())
            .stdout(Stdio::piped())
            .current_dir(opts.working_dir)
            .spawn()?;

        let stdout = child.stdout.take().expect("stdout to be available");
        let stderr = child.stderr.take().expect("stdout to be available");

        let stdout = {
            let captured = RwLockOutput::default();
            let output_dest = opts.output_dest.clone();
            async move {
                let mut reader = BufReader::new(stdout).lines();
                while let Some(line) = reader.next_line().await? {
                    captured.add_line(&line).await;
                    match output_dest {
                        OutputDestination::Logging => info!("{}", line),
                        OutputDestination::StandardOut => println!("{}", line),
                        OutputDestination::Null => {}
                    }
                }

                Ok::<_, anyhow::Error>(captured.output.into_inner())
            }
        };

        let stderr = {
            let captured = RwLockOutput::default();
            let output_dest = opts.output_dest.clone();
            async move {
                let mut reader = BufReader::new(stderr).lines();
                while let Some(line) = reader.next_line().await? {
                    captured.add_line(&line).await;
                    match output_dest {
                        OutputDestination::Logging => error!("{}", line),
                        OutputDestination::StandardOut => eprintln!("{}", line),
                        OutputDestination::Null => {}
                    }
                }

                Ok::<_, anyhow::Error>(captured.output.into_inner())
            }
        };

        let (command_result, wait_stdout, wait_stderr) = tokio::join!(child.wait(), stdout, stderr);
        let end_time = Utc::now();
        debug!("join result {:?}", command_result);

        let captured_stdout = wait_stdout.unwrap_or_default();
        let captured_stderr = wait_stderr.unwrap_or_default();

        Ok(Self {
            working_dir: opts.working_dir.to_path_buf(),
            stdout: captured_stdout,
            stderr: captured_stderr,
            exit_code: command_result.ok().and_then(|x| x.code()),
            start_time,
            end_time,
            command: opts.command(),
        })
    }

    pub fn generate_output(&self) -> String {
        let stdout: Vec<_> = self
            .stdout
            .iter()
            .map(|(time, line)| {
                let offset: Duration = *time - self.start_time;
                (*time, format!("{} OUT: {}", offset, line))
            })
            .collect();

        let stderr: Vec<_> = self
            .stderr
            .iter()
            .map(|(time, line)| {
                let offset: Duration = *time - self.start_time;
                (*time, format!("{} ERR: {}", offset, line))
            })
            .collect();

        let mut output = Vec::new();
        output.extend(stdout);
        output.extend(stderr);

        output.sort_by(|(l_time, _), (r_time, _)| l_time.cmp(r_time));

        let text: String = output
            .iter()
            .map(|(_, line)| line.clone())
            .collect::<Vec<_>>()
            .join("\n");

        Redactor::new().redact_text(&text).to_string()
    }

    pub fn create_report_text(&self) -> anyhow::Result<String> {
        let mut f = String::new();
        writeln!(&mut f, "### Command Results\n")?;
        writeln!(&mut f, "Ran command `/usr/bin/env -S {}`", self.command)?;
        writeln!(
            &mut f,
            "Execution started: {}; finished: {}",
            self.start_time, self.end_time
        )?;
        writeln!(
            &mut f,
            "Result of command: {}",
            self.exit_code.unwrap_or(-1)
        )?;
        writeln!(&mut f)?;
        writeln!(&mut f, "#### Output")?;
        writeln!(&mut f)?;
        writeln!(&mut f, "```text")?;
        writeln!(&mut f, "{}", self.generate_output().trim())?;
        writeln!(&mut f, "```")?;
        writeln!(&mut f)?;
        Ok(f)
    }

    pub fn get_stdout(&self) -> String {
        self.stdout
            .iter()
            .map(|(_, line)| line.clone())
            .collect::<Vec<_>>()
            .join("\n")
    }

    pub fn get_stderr(&self) -> String {
        self.stderr
            .iter()
            .map(|(_, line)| line.clone())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

fn check_pre_exec(opts: &CaptureOpts) -> Result<(), CaptureError> {
    let command = opts.command();
    let found_binary = match command.split(' ').collect::<Vec<_>>().first() {
        None => return Err(CaptureError::MissingShExec { name: command }),
        Some(path) => which_in(path, Some(OsString::from(opts.path)), opts.working_dir),
    };

    let path = match found_binary {
        Ok(path) => path,
        Err(e) => {
            debug!("Unable to find binary {:?}", e);
            return Err(CaptureError::MissingShExec { name: command });
        }
    };

    if !path.exists() {
        return Err(CaptureError::MissingShExec {
            name: path.display().to_string(),
        });
    }
    let metadata = std::fs::metadata(&path)?;
    let permissions = metadata.permissions().mode();
    if permissions & 0x700 == 0 {
        return Err(CaptureError::MissingShExec {
            name: path.display().to_string(),
        });
    }

    Ok(())
}