-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrunner.rs
More file actions
449 lines (362 loc) · 12.8 KB
/
runner.rs
File metadata and controls
449 lines (362 loc) · 12.8 KB
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
use std::collections::BTreeMap;
use std::io::{BufWriter, Write};
use std::os::unix::process::CommandExt;
use std::process::Command;
use anyhow::{Result, anyhow};
use libc::{gid_t, pid_t, uid_t};
use mktemp::TempFile;
use crate::config::{
AttachRequest, Capabilities, Configurable, CreateRequest, IdMapping, MountSpec, Mutation,
ProcessResourceLimits,
};
use crate::namespace::Namespace;
fn add_to_cap_list(
value: String,
caps: &mut Option<Capabilities>,
get_list: impl FnOnce(&mut Capabilities) -> &mut Option<Vec<String>>,
) {
if caps.is_none() {
*caps = Some(Capabilities::default());
}
if let Some(caps) = caps {
let list = get_list(caps);
if list.is_none() {
*list = Some(Vec::new());
}
if let Some(list) = list {
list.push(value);
}
}
}
#[derive(Default, Debug)]
pub struct AttachRequestBuilder {
config: AttachRequest,
}
impl AttachRequestBuilder {
pub fn new() -> AttachRequestBuilder {
AttachRequestBuilder::default()
}
pub fn set_pid(mut self, pid: pid_t) -> AttachRequestBuilder {
self.config.pid = pid;
self
}
pub fn set_executable(mut self, executable: &str) -> AttachRequestBuilder {
self.config.exec.executable = executable.to_string().into();
self
}
pub fn set_arguments(mut self, args: Vec<&str>) -> AttachRequestBuilder {
let converted_args: Vec<String> = args.into_iter().map(|arg| arg.to_string()).collect();
self.config.exec.arguments = converted_args.into();
self
}
pub fn set_working_directory(mut self, wd: &str) -> AttachRequestBuilder {
self.config.exec.working_directory = wd.to_string().into();
self
}
pub fn set_workload_id(mut self, workload_id: &str) -> AttachRequestBuilder {
self.config.workload_id = workload_id.to_string().into();
self
}
pub fn set_uid(mut self, uid: uid_t) -> AttachRequestBuilder {
self.config.exec.uid = uid.into();
self
}
pub fn set_gid(mut self, gid: gid_t) -> AttachRequestBuilder {
self.config.exec.gid = gid.into();
self
}
pub fn set_no_new_privs(mut self, no_new_privs: bool) -> AttachRequestBuilder {
self.config.exec.no_new_privs = no_new_privs;
self
}
pub fn push_environment(mut self, key: &str, value: &str) -> AttachRequestBuilder {
if self.config.exec.environment.is_none() {
self.config.exec.environment = BTreeMap::new().into();
}
if let Some(ref mut map) = self.config.exec.environment {
map.insert(key.to_string(), value.to_string());
}
self
}
pub fn set_oom_score_adj(mut self, score: i32) -> AttachRequestBuilder {
self.config.exec.oom_score_adj = Some(score);
self
}
pub fn push_namespace(mut self, ns: Namespace) -> AttachRequestBuilder {
if self.config.namespaces.is_none() {
self.config.namespaces = vec![].into();
}
if let Some(ref mut nsset) = self.config.namespaces {
nsset.push(ns);
}
self
}
pub fn push_raise_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.raise,
);
self
}
pub fn push_raise_ambient_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.raise_ambient,
);
self
}
pub fn push_drop_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.drop,
);
self
}
pub fn to_request(self) -> AttachRequest {
self.config
}
}
#[derive(Default, Debug)]
pub struct CreateRequestBuilder {
/// The configuration object being constructed.
config: CreateRequest,
}
impl CreateRequestBuilder {
pub fn new() -> CreateRequestBuilder {
CreateRequestBuilder::default()
}
pub fn set_rootfs(mut self, rootfs: &str) -> CreateRequestBuilder {
self.config.rootfs = rootfs.to_string().into();
self
}
pub fn set_rootfs_readonly(mut self, rootfs_readonly: bool) -> CreateRequestBuilder {
self.config.rootfs_readonly = Some(rootfs_readonly);
self
}
pub fn set_skip_two_stage_userns(
mut self,
skip_two_stage_userns: bool,
) -> CreateRequestBuilder {
self.config.skip_two_stage_userns = Some(skip_two_stage_userns);
self
}
pub fn set_executable(mut self, executable: &str) -> CreateRequestBuilder {
self.config.exec.executable = executable.to_string().into();
self
}
pub fn set_arguments(mut self, args: Vec<&str>) -> CreateRequestBuilder {
let converted_args: Vec<String> = args.into_iter().map(|arg| arg.to_string()).collect();
self.config.exec.arguments = converted_args.into();
self
}
pub fn set_working_directory(mut self, wd: &str) -> CreateRequestBuilder {
self.config.exec.working_directory = wd.to_string().into();
self
}
pub fn set_workload_id(mut self, workload_id: &str) -> CreateRequestBuilder {
self.config.workload_id = workload_id.to_string().into();
self
}
pub fn set_uid(mut self, uid: uid_t) -> CreateRequestBuilder {
self.config.exec.uid = uid.into();
self
}
pub fn set_gid(mut self, gid: gid_t) -> CreateRequestBuilder {
self.config.exec.gid = gid.into();
self
}
pub fn set_supplemental_gids(mut self, gids: Vec<gid_t>) -> CreateRequestBuilder {
self.config.exec.supplemental_gids = gids.into();
self
}
pub fn set_no_new_privs(mut self, no_new_privs: bool) -> CreateRequestBuilder {
self.config.exec.no_new_privs = no_new_privs;
self
}
pub fn set_oom_score_adj(mut self, score: i32) -> CreateRequestBuilder {
self.config.exec.oom_score_adj = Some(score);
self
}
pub fn set_hostname(mut self, hostname: &str) -> CreateRequestBuilder {
self.config.hostname = hostname.to_string().into();
self
}
pub fn set_setgroups_deny(mut self, setgroups_deny: bool) -> CreateRequestBuilder {
self.config.setgroups_deny = setgroups_deny.into();
self
}
pub fn set_process_resource_limits(
mut self,
prlimits: ProcessResourceLimits,
) -> CreateRequestBuilder {
self.config.exec.process_limits = prlimits.into();
self
}
pub fn push_resource_limit(mut self, key: &str, value: &str) -> CreateRequestBuilder {
if self.config.limits.is_none() {
self.config.limits = BTreeMap::new().into();
}
if let Some(ref mut map) = self.config.limits {
map.insert(key.to_string(), value.to_string());
}
self
}
pub fn push_environment(mut self, key: &str, value: &str) -> CreateRequestBuilder {
if self.config.exec.environment.is_none() {
self.config.exec.environment = BTreeMap::new().into();
}
if let Some(ref mut map) = self.config.exec.environment {
map.insert(key.to_string(), value.to_string());
}
self
}
pub fn push_namespace(mut self, ns: Namespace) -> CreateRequestBuilder {
if self.config.namespaces.is_none() {
self.config.namespaces = vec![].into();
}
if let Some(ref mut nsset) = self.config.namespaces {
nsset.push(ns);
}
self
}
pub fn push_uid_mapping(mut self, mapping: IdMapping) -> CreateRequestBuilder {
if self.config.uid_mappings.is_none() {
self.config.uid_mappings = vec![].into();
}
if let Some(ref mut map) = self.config.uid_mappings {
map.push(mapping);
}
self
}
pub fn push_gid_mapping(mut self, mapping: IdMapping) -> CreateRequestBuilder {
if self.config.gid_mappings.is_none() {
self.config.gid_mappings = vec![].into();
}
if let Some(ref mut map) = self.config.gid_mappings {
map.push(mapping);
}
self
}
pub fn push_mount(mut self, spec: MountSpec) -> CreateRequestBuilder {
if self.config.mounts.is_none() {
self.config.mounts = vec![].into();
}
if let Some(ref mut mounts) = self.config.mounts {
mounts.push(spec);
}
self
}
pub fn push_mutation(mut self, spec: Mutation) -> CreateRequestBuilder {
if self.config.mutations.is_none() {
self.config.mutations = vec![].into();
}
if let Some(ref mut mutations) = self.config.mutations {
mutations.push(spec);
}
self
}
pub fn push_raise_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.raise,
);
self
}
pub fn push_raise_ambient_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.raise_ambient,
);
self
}
pub fn push_drop_capability(mut self, cap: impl AsRef<str>) -> Self {
add_to_cap_list(
cap.as_ref().to_string(),
&mut self.config.capabilities,
|caps| &mut caps.drop,
);
self
}
pub fn to_request(self) -> CreateRequest {
self.config
}
}
#[derive(Debug)]
pub struct Runner {
executable: String,
}
impl Runner {
pub fn new(executable: &str) -> Runner {
Runner {
executable: executable.to_string(),
}
}
fn write_config<T: Configurable>(&self, config: T, config_file: &mut TempFile) -> Result<()> {
config.validate()?;
let encap_config = config.encapsulate()?;
// BufWriter doesn't actually flush the buffer until it is dropped. Ugh.
{
let mut config_writer = BufWriter::new(&mut *config_file);
serde_json::to_writer(&mut config_writer, &encap_config)?;
}
config_file.flush()?;
Ok(())
}
fn create_command(&self, config_file: &TempFile) -> Result<Command> {
let mut command = Command::new(&self.executable);
let config_path = config_file.path().to_string();
command.arg(config_path);
Ok(command)
}
#[cfg(feature = "async")]
fn create_command_async(&self, config_file: &TempFile) -> Result<tokio::process::Command> {
let mut command = tokio::process::Command::new(&self.executable);
let config_path = config_file.path().to_string();
command.arg(config_path);
Ok(command)
}
/// Run the specified container.
/// Returns exit code on success, else error.
pub fn run<T: Configurable>(&self, config: T) -> Result<i32> {
let mut config_file = TempFile::new("styrolite-cfg-", ".json")?;
self.write_config(config, &mut config_file)?;
let status = self.create_command(&config_file)?.status()?;
if let Some(code) = status.code() {
return Ok(code);
}
Err(anyhow!("failed to launch/monitor child process"))
}
#[cfg(feature = "async")]
pub async fn run_async<T: Configurable>(&self, config: T) -> Result<i32> {
let mut config_file = TempFile::new("styrolite-cfg-", ".json")?;
self.write_config(config, &mut config_file)?;
let status = self.create_command_async(&config_file)?.status().await?;
if let Some(code) = status.code() {
return Ok(code);
}
Err(anyhow!("failed to launch/monitor child process"))
}
/// Replace the current process with the styrolite runner directly.
#[cfg(unix)]
pub fn exec<T: Configurable>(&self, config: T) -> Result<()> {
let mut config_file = TempFile::new("styrolite-cfg-", ".json")?;
self.write_config(config, &mut config_file)?;
// Build the command like before
let mut command = self.create_command(&config_file)?;
// NOTE: If exec succeeds, this process image is replaced; no destructors run.
// That means config_file won't be dropped, so a drop-based cleanup won't happen.
// If TempFile is delete-on-drop, the file may be left behind.
let err = command.exec(); // only returns on failure
Err(anyhow!(err))
}
#[cfg(not(unix))]
pub fn exec<T: Configurable>(&self, config: T) -> Result<()> {
let _ = config;
Err(anyhow!("Runner::exec is only supported on unix"))
}
}