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
use std::collections::HashMap;
use std::io::{ErrorKind, Write};
use std::os::unix::prelude::CommandExt;
use std::path::Path;
use std::{process, thread, time};
use nix::sys::signal;
use nix::unistd::Pid;
use oci_spec::runtime::{Hook, State as OciState};
use crate::container::{State, StateConversionError};
use crate::utils;
#[derive(Debug, thiserror::Error)]
pub enum HookError {
#[error("failed to execute hook command")]
CommandExecute(#[source] std::io::Error),
#[error("failed to encode container state")]
EncodeContainerState(#[source] serde_json::Error),
#[error("hook command exited with non-zero exit code: {0}")]
NonZeroExitCode(i32),
#[error("hook command was killed by a signal")]
Killed,
#[error("failed to execute hook command due to a timeout")]
Timeout,
#[error("container state is required to run hook")]
MissingContainerState,
#[error("failed to write container state to stdin")]
WriteContainerState(#[source] std::io::Error),
#[error("failed to convert state to OCI format")]
StateConversion(#[from] StateConversionError),
}
type Result<T> = std::result::Result<T, HookError>;
pub fn run_hooks(
hooks: Option<&Vec<Hook>>,
state: Option<&State>,
// TODO: Remove the following parameters. To comply with the OCI State, hooks should only depend on structures defined in oci-spec-rs. Cleaning these up ensures proper functional isolation.
cwd: Option<&Path>,
pid: Option<Pid>,
) -> Result<()> {
let base_state = state.ok_or(HookError::MissingContainerState)?;
// High-level container runtimes use OCI state to pass the container state to the hooks.
// So we need to convert the container state to OCI state.
// Ref: https://github.com/containerd/containerd/blob/v2.2.1/cmd/containerd/command/oci-hook.go#L82
let mut oci_state = OciState::try_from(base_state)?;
// The `pid` parameter allows overriding the PID in the state. This is needed because
// high-level container runtimes like containerd set the PID separately for certain hooks.
// Ref: https://github.com/containerd/containerd/blob/main/cmd/containerd/command/oci-hook.go#L90
if let Some(override_pid) = pid {
oci_state.set_pid(Some(override_pid.as_raw()));
}
if let Some(hooks) = hooks {
for hook in hooks {
let mut hook_command = process::Command::new(hook.path());
if let Some(cwd) = cwd {
hook_command.current_dir(cwd);
}
// Based on OCI spec, the first argument of the args vector is the
// arg0, which can be different from the path. For example, path
// may be "/usr/bin/true" and arg0 is set to "true". However, rust
// command differentiates arg0 from args, where rust command arg
// doesn't include arg0. So we have to make the split arg0 from the
// rest of args.
if let Some((arg0, args)) = hook.args().as_ref().and_then(|a| a.split_first()) {
tracing::debug!("run_hooks arg0: {:?}, args: {:?}", arg0, args);
hook_command.arg0(arg0).args(args)
} else {
hook_command.arg0(hook.path().display().to_string())
};
let envs: HashMap<String, String> = if let Some(env) = hook.env() {
utils::parse_env(env)
} else {
HashMap::new()
};
tracing::debug!("run_hooks envs: {:?}", envs);
let mut hook_process = hook_command
.env_clear()
.envs(envs)
.stdin(process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(process::Stdio::inherit())
.spawn()
.map_err(HookError::CommandExecute)?;
let hook_process_pid = Pid::from_raw(hook_process.id() as i32);
// Based on the OCI spec, we need to pipe the container state into
// the hook command through stdin.
if let Some(stdin) = &mut hook_process.stdin {
// We want to ignore BrokenPipe here. A BrokenPipe indicates
// either the hook is crashed/errored or it ran successfully.
// Either way, this is an indication that the hook command
// finished execution. If the hook command was successful,
// which we will check later in this function, we should not
// fail this step here. We still want to check for all the other
// error, in the case that the hook command is waiting for us to
// write to stdin.
let encoded_state =
serde_json::to_string(&oci_state).map_err(HookError::EncodeContainerState)?;
if let Err(e) = stdin.write_all(encoded_state.as_bytes()) {
if e.kind() != ErrorKind::BrokenPipe {
// Not a broken pipe. The hook command may be waiting
// for us.
let _ = signal::kill(hook_process_pid, signal::Signal::SIGKILL);
return Err(HookError::WriteContainerState(e));
}
}
}
let res = if let Some(timeout_sec) = hook.timeout() {
// Rust does not make it easy to handle executing a command and
// timeout. Here we decided to wait for the command in a
// different thread, so the main thread is not blocked. We use a
// channel shared between main thread and the wait thread, since
// the channel has timeout functions out of the box. Rust won't
// let us copy the Command structure, so we can't share it
// between the wait thread and main thread. Therefore, we will
// use pid to identify the process and send a kill signal. This
// is what the Command.kill() does under the hood anyway. When
// timeout, we have to kill the process and clean up properly.
let (s, r) = std::sync::mpsc::channel();
thread::spawn(move || {
let res = hook_process.wait();
let _ = s.send(res);
});
match r.recv_timeout(time::Duration::from_secs(timeout_sec as u64)) {
Ok(res) => res,
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
// Kill the process. There is no need to further clean
// up because we will be error out.
let _ = signal::kill(hook_process_pid, signal::Signal::SIGKILL);
return Err(HookError::Timeout);
}
Err(_) => {
unreachable!();
}
}
} else {
hook_process.wait()
};
match res {
Ok(exit_status) => match exit_status.code() {
Some(0) => Ok(()),
Some(exit_code) => Err(HookError::NonZeroExitCode(exit_code)),
None => Err(HookError::Killed),
},
Err(e) => Err(HookError::CommandExecute(e)),
}?;
}
}
Ok(())
}
#[cfg(test)]
mod test {
use std::{env, fs};
use anyhow::{Context, Result, bail};
use oci_spec::runtime::HookBuilder;
use serial_test::serial;
use super::*;
use crate::container::Container;
fn is_command_in_path(program: &str) -> bool {
if let Ok(path) = env::var("PATH") {
for p in path.split(':') {
let p_str = format!("{p}/{program}");
if fs::metadata(p_str).is_ok() {
return true;
}
}
}
false
}
// Note: the run_hook will require the use of pipe to write the container
// state into stdin of the hook command. When cargo test runs these tests in
// parallel with other tests, the pipe becomes flaky and often we will get
// broken pipe or bad file descriptors. There is not much we can do and we
// decide not to retry in the test. The most sensible way to test this is
// ask cargo test to run these tests in serial.
#[test]
#[serial]
fn test_run_hook() -> Result<()> {
{
let default_container: Container = Default::default();
run_hooks(None, Some(&default_container.state), None, None)
.context("Failed simple test")?;
}
{
assert!(is_command_in_path("true"), "The true was not found.");
let default_container: Container = Default::default();
let hook = HookBuilder::default().path("true").build()?;
let hooks = Some(vec![hook]);
run_hooks(hooks.as_ref(), Some(&default_container.state), None, None)
.context("Failed true")?;
}
{
assert!(
is_command_in_path("printenv"),
"The printenv was not found."
);
// Use `printenv` to make sure the environment is set correctly.
let default_container: Container = Default::default();
let hook = HookBuilder::default()
.path("bash")
.args(vec![
String::from("bash"),
String::from("-c"),
String::from("printenv key > /dev/null"),
])
.env(vec![String::from("key=value")])
.build()?;
let hooks = Some(vec![hook]);
run_hooks(hooks.as_ref(), Some(&default_container.state), None, None)
.context("Failed printenv test")?;
}
{
assert!(is_command_in_path("pwd"), "The pwd was not found.");
let tmp = tempfile::tempdir()?;
let default_container: Container = Default::default();
let hook = HookBuilder::default()
.path("bash")
.args(vec![
String::from("bash"),
String::from("-c"),
format!("test $(pwd) = {:?}", tmp.path()),
])
.build()?;
let hooks = Some(vec![hook]);
run_hooks(
hooks.as_ref(),
Some(&default_container.state),
Some(tmp.path()),
None,
)
.context("Failed pwd test")?;
}
{
let default_container: Container = Default::default();
let expected_pid = Pid::from_raw(1000);
let hook = HookBuilder::default()
.path("bash")
.args(vec![
String::from("bash"),
String::from("-c"),
format!("cat | grep '\"pid\":{}'", expected_pid),
])
.build()?;
let hooks = Some(vec![hook]);
run_hooks(
hooks.as_ref(),
Some(&default_container.state),
None,
Some(expected_pid),
)
.context("Failed pid test")?;
}
Ok(())
}
#[test]
#[serial]
// This will test executing hook with a timeout. Since the timeout is set in
// secs, minimally, the test will run for 1 second to trigger the timeout.
fn test_run_hook_timeout() -> Result<()> {
let default_container: Container = Default::default();
// We use `tail -f /dev/null` here to simulate a hook command that hangs.
let hook = HookBuilder::default()
.path("tail")
.args(vec![
String::from("tail"),
String::from("-f"),
String::from("/dev/null"),
])
.timeout(1)
.build()?;
let hooks = Some(vec![hook]);
match run_hooks(hooks.as_ref(), Some(&default_container.state), None, None) {
Ok(_) => {
bail!(
"The test expects the hook to error out with timeout. Should not execute cleanly"
);
}
Err(HookError::Timeout) => {}
Err(err) => {
bail!(
"The test expects the hook to error out with timeout. Got error: {}",
err
);
}
};
Ok(())
}
}