microsandbox-rb 0.17.4 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +378 -1
- data/Cargo.lock +747 -1134
- data/DESIGN.md +52 -36
- data/README.md +27 -20
- data/SECURITY.md +22 -6
- data/docs/configuration.md +153 -17
- data/docs/errors.md +8 -2
- data/docs/execution.md +17 -0
- data/docs/filesystem.md +2 -1
- data/docs/images.md +74 -2
- data/docs/lifecycle.md +130 -6
- data/docs/observability.md +9 -7
- data/docs/releasing.md +1 -1
- data/docs/runtime.md +126 -43
- data/docs/surface.md +55 -14
- data/ext/microsandbox/Cargo.toml +22 -20
- data/ext/microsandbox/extconf.rb +9 -8
- data/ext/microsandbox/src/backend.rs +21 -15
- data/ext/microsandbox/src/error.rs +59 -1
- data/ext/microsandbox/src/host_path.rs +37 -28
- data/ext/microsandbox/src/lib.rs +314 -42
- data/ext/microsandbox/src/runtime.rs +5 -5
- data/ext/microsandbox/src/sandbox.rs +508 -241
- data/ext/microsandbox/src/snapshot.rs +63 -20
- data/ext/microsandbox/src/storage.rs +44 -0
- data/lib/microsandbox/errors.rb +63 -0
- data/lib/microsandbox/exec_handle.rb +2 -1
- data/lib/microsandbox/runtime_info.rb +65 -0
- data/lib/microsandbox/sandbox.rb +544 -158
- data/lib/microsandbox/snapshot.rb +101 -28
- data/lib/microsandbox/storage.rb +205 -0
- data/lib/microsandbox/version.rb +5 -4
- data/lib/microsandbox.rb +259 -60
- data/rust-toolchain.toml +1 -1
- data/sig/microsandbox.rbs +113 -7
- metadata +5 -2
|
@@ -19,16 +19,18 @@ use microsandbox::logs::{
|
|
|
19
19
|
LogCursor, LogEntry, LogOptions, LogSource, LogStreamOptions, LogStreamStart,
|
|
20
20
|
};
|
|
21
21
|
use microsandbox::sandbox::{
|
|
22
|
-
AttachOptionsBuilder, DestroyOptions, DiskImageFormat, EnvVar,
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
AttachOptionsBuilder, DestroyOptions, DiskImageFormat, EnvVar, ExternalMountRestorePolicy,
|
|
23
|
+
FlatClone, FsEntry, FsEntryKind, FsMetadata, HostPermissions, HttpDenyResponseFormat,
|
|
24
|
+
MountBuilder, Patch, PullPolicy, PullProgress, PullProgressHandle, RestartOptions,
|
|
25
|
+
RlimitResource, RootDiskBuilder, SandboxBuilder, SandboxHandle, SandboxMetrics,
|
|
25
26
|
SandboxModificationBuilder, SandboxModificationPatch, SandboxStatus, SandboxStopResult,
|
|
26
27
|
SecretBuilder, SecretModificationPatch, SecretSource, SecurityProfile, StatVirtualization,
|
|
27
28
|
};
|
|
29
|
+
use microsandbox::snapshot::SnapshotReference;
|
|
28
30
|
use microsandbox::LogLevel;
|
|
29
31
|
use microsandbox::MicrosandboxResult;
|
|
30
32
|
use microsandbox::RegistryAuth;
|
|
31
|
-
use
|
|
33
|
+
use microsandbox::SecretViolationAction;
|
|
32
34
|
use microsandbox_network::dns::Nameserver;
|
|
33
35
|
use microsandbox_network::policy::{
|
|
34
36
|
Action, Destination, DestinationGroup, Direction, NetworkPolicy, NetworkProfile, PortRange,
|
|
@@ -51,6 +53,83 @@ use crate::stream::{LogStream, MetricsStream};
|
|
|
51
53
|
/// bound, so a large finite value would still reach (and panic) the native
|
|
52
54
|
/// layer; this keeps the native layer panic-free regardless of the Ruby layer,
|
|
53
55
|
/// mirroring `agent::dur`.
|
|
56
|
+
/// Workaround for runtime v0.7.14 (docs/runtime.md, "Known issues at the
|
|
57
|
+
/// pinned runtime"): only the OCI-image path of a local create runs
|
|
58
|
+
/// `create_dir_all` on the sandbox directory; the directory- and disk-image
|
|
59
|
+
/// paths create `<sandboxes>/<name>` with a non-recursive mkdir, so on a
|
|
60
|
+
/// fresh runtime home (no install ever ran there: the runtime came from
|
|
61
|
+
/// `MSB_PATH` or the binaries gem) they fail with ENOENT. Create the
|
|
62
|
+
/// sandboxes directory first, the same way the OCI path does (a plain
|
|
63
|
+
/// `create_dir_all`, so the same mode: `0o777 & !umask`).
|
|
64
|
+
///
|
|
65
|
+
/// Upstream issue: https://github.com/superradcompany/microsandbox/issues/2041
|
|
66
|
+
///
|
|
67
|
+
/// Only for a local backend whose own captured configuration resolves a
|
|
68
|
+
/// runtime pair: when nothing resolves, the create is about to fail with
|
|
69
|
+
/// `RuntimeNotInstalledError` and must leave the home untouched. A no-op for
|
|
70
|
+
/// a cloud backend and on the OCI path (the directory then already exists).
|
|
71
|
+
fn prepare_local_sandboxes_dir() -> Result<(), Error> {
|
|
72
|
+
crate::backend::ensure_this_process_backend()?;
|
|
73
|
+
let backend = microsandbox::default_backend();
|
|
74
|
+
let Some(local) = backend.as_local() else {
|
|
75
|
+
return Ok(());
|
|
76
|
+
};
|
|
77
|
+
if microsandbox::setup::resolve_runtime(local.config()).is_err() {
|
|
78
|
+
return Ok(());
|
|
79
|
+
}
|
|
80
|
+
std::fs::create_dir_all(local.sandboxes_dir()).map_err(|error| error::to_ruby(error.into()))
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/// The `http:` create option, read from the Ruby layer's normalized Hash.
|
|
84
|
+
struct HttpDeny {
|
|
85
|
+
deny_response: Option<bool>,
|
|
86
|
+
format: Option<HttpDenyResponseFormat>,
|
|
87
|
+
network_message: Option<String>,
|
|
88
|
+
secret_message: Option<String>,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn parse_http_deny(h: RHash) -> Result<HttpDeny, Error> {
|
|
92
|
+
let format = match conv::opt_string(h, "format")?.as_deref() {
|
|
93
|
+
None => None,
|
|
94
|
+
Some("json") => Some(HttpDenyResponseFormat::Json),
|
|
95
|
+
Some("text") => Some(HttpDenyResponseFormat::Text),
|
|
96
|
+
Some(other) => {
|
|
97
|
+
return Err(error::base_error(format!(
|
|
98
|
+
"http format must be json or text, got {other:?}"
|
|
99
|
+
)));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
Ok(HttpDeny {
|
|
103
|
+
deny_response: conv::opt::<bool>(h, "deny_response")?,
|
|
104
|
+
format,
|
|
105
|
+
network_message: conv::opt_string(h, "network_message")?,
|
|
106
|
+
secret_message: conv::opt_string(h, "secret_message")?,
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Run a whole kill under one deadline: the name's transition lock, the
|
|
111
|
+
/// dispatch and the stopped-state observation. Upstream `kill_with_timeout`
|
|
112
|
+
/// bounds only the observation; its `request_kill` waits for the transition
|
|
113
|
+
/// lock without a deadline, so a held lock would block the caller forever.
|
|
114
|
+
///
|
|
115
|
+
/// Dropping the kill future on expiry is safe at v0.7.14: before the lock is
|
|
116
|
+
/// taken it only closes the lock file; afterwards the guard's drop releases
|
|
117
|
+
/// the lock, SIGKILL (if already sent) is synchronous, and a row left
|
|
118
|
+
/// `Running` is reconciled from the dead pid by the next `get`/`list`/`start`.
|
|
119
|
+
async fn kill_within<F>(name: &str, duration: Duration, kill: F) -> MicrosandboxResult<()>
|
|
120
|
+
where
|
|
121
|
+
F: std::future::Future<Output = MicrosandboxResult<()>>,
|
|
122
|
+
{
|
|
123
|
+
match tokio::time::timeout(duration, kill).await {
|
|
124
|
+
Ok(result) => result,
|
|
125
|
+
Err(_) => Err(microsandbox::MicrosandboxError::Runtime(format!(
|
|
126
|
+
"timed out killing sandbox '{name}' within {:.3}s (the budget covers the \
|
|
127
|
+
transition lock, the kill request and observing the stop)",
|
|
128
|
+
duration.as_secs_f64()
|
|
129
|
+
))),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
54
133
|
fn secs_to_duration(secs: f64) -> Result<Duration, Error> {
|
|
55
134
|
Duration::try_from_secs_f64(secs).map_err(|e| {
|
|
56
135
|
error::base_error(format!(
|
|
@@ -102,9 +181,6 @@ impl Sandbox {
|
|
|
102
181
|
};
|
|
103
182
|
}
|
|
104
183
|
}
|
|
105
|
-
if let Some(v) = conv::opt_string(opts, "from_snapshot")? {
|
|
106
|
-
b = b.from_snapshot(host_paths.pin_snapshot(v)?);
|
|
107
|
-
}
|
|
108
184
|
if let Some(v) = conv::opt_u8(opts, "cpus")? {
|
|
109
185
|
b = b.cpus(v);
|
|
110
186
|
}
|
|
@@ -177,119 +253,9 @@ impl Sandbox {
|
|
|
177
253
|
}
|
|
178
254
|
}
|
|
179
255
|
}
|
|
180
|
-
// volumes:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
// readonly/noexec/nosuid/nodev (bool), stat_virtualization,
|
|
184
|
-
// host_permissions, override_uid/override_gid (u32 pair).
|
|
185
|
-
// Enum-valued options are validated up front (the
|
|
186
|
-
// volume closure can't return an error); the core validates the rest
|
|
187
|
-
// (e.g. rejecting stat_virtualization on tmpfs/disk) at create().
|
|
188
|
-
for m in conv::opt_hash_vec(opts, "volumes")? {
|
|
189
|
-
let guest = conv::opt_string(m, "guest")?
|
|
190
|
-
.ok_or_else(|| error::base_error("volume mount is missing :guest"))?;
|
|
191
|
-
let kind = conv::opt_string(m, "kind")?
|
|
192
|
-
.ok_or_else(|| error::base_error("volume mount is missing :kind"))?;
|
|
193
|
-
let source = conv::opt_string(m, "source")?;
|
|
194
|
-
let size_mib = conv::opt_u32(m, "size_mib")?;
|
|
195
|
-
// Bind-mount guest-write quota override (MiB). The core's MountBuilder
|
|
196
|
-
// applies it to bind mounts only and rejects it on tmpfs/disk/named at
|
|
197
|
-
// build(); we forward it as-is so that validation lives in one place.
|
|
198
|
-
let quota_mib = conv::opt_u32(m, "quota_mib")?;
|
|
199
|
-
let fstype = conv::opt_string(m, "fstype")?;
|
|
200
|
-
let readonly = conv::opt_bool(m, "readonly")?;
|
|
201
|
-
let noexec = conv::opt_bool(m, "noexec")?;
|
|
202
|
-
let nosuid = conv::opt_bool(m, "nosuid")?;
|
|
203
|
-
let nodev = conv::opt_bool(m, "nodev")?;
|
|
204
|
-
let format = conv::opt_string(m, "format")?
|
|
205
|
-
.map(|f| disk_format_from_str(&f))
|
|
206
|
-
.transpose()?;
|
|
207
|
-
let stat_virt = conv::opt_string(m, "stat_virtualization")?
|
|
208
|
-
.map(|s| stat_virtualization_from_str(&s))
|
|
209
|
-
.transpose()?;
|
|
210
|
-
let host_perms = conv::opt_string(m, "host_permissions")?
|
|
211
|
-
.map(|s| host_permissions_from_str(&s))
|
|
212
|
-
.transpose()?;
|
|
213
|
-
// v0.6.15: the fallback guest owner presented for host files that
|
|
214
|
-
// carry no per-file stat override. The Ruby layer validates the pair
|
|
215
|
-
// (both-or-neither, u32 range, bind/named only, not with
|
|
216
|
-
// stat_virtualization=off); whether a *named* volume is directory-
|
|
217
|
-
// or disk-backed is known only to the core, which rejects the
|
|
218
|
-
// disk-backed case at build().
|
|
219
|
-
let override_uid = conv::opt_u32(m, "override_uid")?;
|
|
220
|
-
let override_gid = conv::opt_u32(m, "override_gid")?;
|
|
221
|
-
// v0.6.7: mount-root symlink protection is on by default; this is
|
|
222
|
-
// the per-mount opt-out. Valid for bind/named-directory mounts —
|
|
223
|
-
// the core rejects it elsewhere at build().
|
|
224
|
-
let follow_root_symlinks = conv::opt::<bool>(m, "follow_root_symlinks")?;
|
|
225
|
-
// bind/named/disk require a source; tmpfs must not have one. A
|
|
226
|
-
// bind or disk source is a host path; a named source is a volume
|
|
227
|
-
// name and is never resolved.
|
|
228
|
-
let backing = match (kind.as_str(), source) {
|
|
229
|
-
("bind", Some(host)) => {
|
|
230
|
-
MountBacking::Bind(host_paths.pin(host, "bind mount host path")?)
|
|
231
|
-
}
|
|
232
|
-
("disk", Some(host)) => {
|
|
233
|
-
MountBacking::Disk(host_paths.pin(host, "disk mount host path")?)
|
|
234
|
-
}
|
|
235
|
-
("named", Some(name)) => MountBacking::Named(name),
|
|
236
|
-
("bind" | "named" | "disk", None) => {
|
|
237
|
-
return Err(error::base_error(format!(
|
|
238
|
-
"volume mount kind {kind:?} requires a source"
|
|
239
|
-
)))
|
|
240
|
-
}
|
|
241
|
-
("tmpfs", _) => MountBacking::Tmpfs,
|
|
242
|
-
(other, _) => {
|
|
243
|
-
return Err(error::base_error(format!(
|
|
244
|
-
"unknown volume mount kind {other:?} (expected bind/named/tmpfs/disk)"
|
|
245
|
-
)))
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
b = b.volume(guest, move |mut mb| {
|
|
249
|
-
mb = match backing {
|
|
250
|
-
MountBacking::Named(name) => mb.named(name),
|
|
251
|
-
MountBacking::Tmpfs => mb.tmpfs(),
|
|
252
|
-
MountBacking::Disk(host) => mb.disk(host),
|
|
253
|
-
MountBacking::Bind(host) => mb.bind(host),
|
|
254
|
-
};
|
|
255
|
-
if let Some(n) = size_mib {
|
|
256
|
-
mb = mb.size(n);
|
|
257
|
-
}
|
|
258
|
-
if let Some(q) = quota_mib {
|
|
259
|
-
mb = mb.quota(q);
|
|
260
|
-
}
|
|
261
|
-
if let Some(f) = format {
|
|
262
|
-
mb = mb.format(f);
|
|
263
|
-
}
|
|
264
|
-
if let Some(ft) = fstype {
|
|
265
|
-
mb = mb.fstype(ft);
|
|
266
|
-
}
|
|
267
|
-
if readonly {
|
|
268
|
-
mb = mb.readonly();
|
|
269
|
-
}
|
|
270
|
-
if noexec {
|
|
271
|
-
mb = mb.noexec();
|
|
272
|
-
}
|
|
273
|
-
if nosuid {
|
|
274
|
-
mb = mb.nosuid();
|
|
275
|
-
}
|
|
276
|
-
if nodev {
|
|
277
|
-
mb = mb.nodev();
|
|
278
|
-
}
|
|
279
|
-
if let Some(sv) = stat_virt {
|
|
280
|
-
mb = mb.stat_virtualization(sv);
|
|
281
|
-
}
|
|
282
|
-
if let Some(hp) = host_perms {
|
|
283
|
-
mb = mb.host_permissions(hp);
|
|
284
|
-
}
|
|
285
|
-
if let (Some(uid), Some(gid)) = (override_uid, override_gid) {
|
|
286
|
-
mb = mb.owner(uid, gid);
|
|
287
|
-
}
|
|
288
|
-
if let Some(follow) = follow_root_symlinks {
|
|
289
|
-
mb = mb.follow_root_symlinks(follow);
|
|
290
|
-
}
|
|
291
|
-
mb
|
|
292
|
-
});
|
|
256
|
+
// volumes: see `parse_volumes` (shared with `restore`).
|
|
257
|
+
for (guest, spec) in parse_volumes(opts, host_paths)? {
|
|
258
|
+
b = b.volume(guest, move |mb| spec.apply(mb));
|
|
293
259
|
}
|
|
294
260
|
// patches: rootfs modifications applied before boot. The Ruby layer
|
|
295
261
|
// normalizes each `Microsandbox::Patch.*` into a string-keyed Hash with
|
|
@@ -372,20 +338,21 @@ impl Sandbox {
|
|
|
372
338
|
b = b.rlimit_range(resource, soft, hard);
|
|
373
339
|
}
|
|
374
340
|
// secrets: each entry is normalized by the Ruby layer to a string-keyed
|
|
375
|
-
// Hash — env (req), value (req),
|
|
376
|
-
//
|
|
377
|
-
//
|
|
378
|
-
//
|
|
341
|
+
// Hash — env (req), value (req), allow (req, non-empty), and optional
|
|
342
|
+
// allow_placeholder_for, placeholder, require_tls_identity,
|
|
343
|
+
// violation_action, substitution {headers, header_fields, query, body}.
|
|
344
|
+
// Routed through the full secret builder (`b.secret`), which
|
|
345
|
+
// auto-enables TLS interception. Mirrors the Python SecretEntry.
|
|
379
346
|
for h in conv::opt_hash_vec(opts, "secrets")? {
|
|
380
347
|
let spec = parse_secret(h)?;
|
|
381
348
|
b = b.secret(move |s| spec.apply(s));
|
|
382
349
|
}
|
|
383
|
-
// Sandbox-level secret-leak
|
|
384
|
-
// block_and_terminate
|
|
385
|
-
//
|
|
386
|
-
if let Some(v) = conv::
|
|
387
|
-
let
|
|
388
|
-
b = b.network(move |n| n.
|
|
350
|
+
// Sandbox-level secret-leak action (block / block_and_log /
|
|
351
|
+
// block_and_terminate). Applied via the network builder, which
|
|
352
|
+
// accumulates on top of any policy/dns/tls already configured.
|
|
353
|
+
if let Some(v) = conv::opt_string(opts, "secret_violation_action")? {
|
|
354
|
+
let action = parse_violation_action(&v)?;
|
|
355
|
+
b = b.network(move |n| n.secret_violation_action(action));
|
|
389
356
|
}
|
|
390
357
|
// Advanced network configuration (custom DNS, TLS-interception tuning,
|
|
391
358
|
// guest IP pools, connection cap, host-CA trust), applied via the network
|
|
@@ -410,7 +377,10 @@ impl Sandbox {
|
|
|
410
377
|
.map_err(|e| error::base_error(format!("invalid ipv6_pool {s:?}: {e}")))
|
|
411
378
|
})
|
|
412
379
|
.transpose()?;
|
|
413
|
-
|
|
380
|
+
// Connection caps (runtime v0.7): 0 selects unlimited. The Ruby layer
|
|
381
|
+
// maps the deprecated `max_connections:` onto the TCP cap.
|
|
382
|
+
let max_tcp_connections = conv::opt::<usize>(opts, "max_tcp_connections")?;
|
|
383
|
+
let max_udp_connections = conv::opt::<usize>(opts, "max_udp_connections")?;
|
|
414
384
|
let trust_host_cas = conv::opt::<bool>(opts, "trust_host_cas")?;
|
|
415
385
|
// strict: fail-closed hostname-policy enforcement (v0.6.18). A
|
|
416
386
|
// hostname-rule allow must be backed by an inspectable request
|
|
@@ -421,13 +391,21 @@ impl Sandbox {
|
|
|
421
391
|
// core's generated network config patch (ConfigPatch derive) does
|
|
422
392
|
// carry a `strict` field, so this is SDK parity, not a core limit.
|
|
423
393
|
let strict = conv::opt::<bool>(opts, "strict")?;
|
|
394
|
+
// http: how a policy-denied request is answered (runtime v0.7). The
|
|
395
|
+
// Ruby layer validated it; the deprecated text `deny_message` is not
|
|
396
|
+
// bound.
|
|
397
|
+
let http = conv::opt::<RHash>(opts, "http")?
|
|
398
|
+
.map(parse_http_deny)
|
|
399
|
+
.transpose()?;
|
|
424
400
|
if dns.is_some()
|
|
425
401
|
|| tls.is_some()
|
|
426
402
|
|| ipv4_pool.is_some()
|
|
427
403
|
|| ipv6_pool.is_some()
|
|
428
|
-
||
|
|
404
|
+
|| max_tcp_connections.is_some()
|
|
405
|
+
|| max_udp_connections.is_some()
|
|
429
406
|
|| trust_host_cas.is_some()
|
|
430
407
|
|| strict.is_some()
|
|
408
|
+
|| http.is_some()
|
|
431
409
|
{
|
|
432
410
|
b = b.network(move |mut n| {
|
|
433
411
|
if let Some(dns) = dns {
|
|
@@ -476,8 +454,11 @@ impl Sandbox {
|
|
|
476
454
|
if let Some(p) = ipv6_pool {
|
|
477
455
|
n = n.ipv6_pool(p);
|
|
478
456
|
}
|
|
479
|
-
if let Some(m) =
|
|
480
|
-
n = n.
|
|
457
|
+
if let Some(m) = max_tcp_connections {
|
|
458
|
+
n = n.max_tcp_connections(m);
|
|
459
|
+
}
|
|
460
|
+
if let Some(m) = max_udp_connections {
|
|
461
|
+
n = n.max_udp_connections(m);
|
|
481
462
|
}
|
|
482
463
|
if let Some(t) = trust_host_cas {
|
|
483
464
|
n = n.trust_host_cas(t);
|
|
@@ -485,6 +466,23 @@ impl Sandbox {
|
|
|
485
466
|
if let Some(s) = strict {
|
|
486
467
|
n = n.strict(s);
|
|
487
468
|
}
|
|
469
|
+
if let Some(http) = http {
|
|
470
|
+
n = n.http(move |mut h| {
|
|
471
|
+
if let Some(enabled) = http.deny_response {
|
|
472
|
+
h = h.deny_response(enabled);
|
|
473
|
+
}
|
|
474
|
+
if let Some(format) = http.format {
|
|
475
|
+
h = h.deny_response_format(format);
|
|
476
|
+
}
|
|
477
|
+
if let Some(message) = http.network_message {
|
|
478
|
+
h = h.network_deny_message(message);
|
|
479
|
+
}
|
|
480
|
+
if let Some(message) = http.secret_message {
|
|
481
|
+
h = h.secret_deny_message(message);
|
|
482
|
+
}
|
|
483
|
+
h
|
|
484
|
+
});
|
|
485
|
+
}
|
|
488
486
|
n
|
|
489
487
|
});
|
|
490
488
|
}
|
|
@@ -551,6 +549,7 @@ impl Sandbox {
|
|
|
551
549
|
/// Create and boot a sandbox. `opts` is a string-keyed options Hash.
|
|
552
550
|
fn create(name: String, opts: RHash) -> Result<Sandbox, Error> {
|
|
553
551
|
let b = Self::build_builder(name, opts)?;
|
|
552
|
+
prepare_local_sandboxes_dir()?;
|
|
554
553
|
let inner = block_on(b.create())?.map_err(error::to_ruby)?;
|
|
555
554
|
Ok(Sandbox::from_inner(inner))
|
|
556
555
|
}
|
|
@@ -561,6 +560,7 @@ impl Sandbox {
|
|
|
561
560
|
/// Node `createWithPullProgress`.
|
|
562
561
|
fn create_with_progress(name: String, opts: RHash) -> Result<PullSession, Error> {
|
|
563
562
|
let b = Self::build_builder(name, opts)?;
|
|
563
|
+
prepare_local_sandboxes_dir()?;
|
|
564
564
|
// `create_with_pull_progress` spawns a tokio task, so it must run inside
|
|
565
565
|
// the runtime context even though the call itself is synchronous.
|
|
566
566
|
let (handle, join) =
|
|
@@ -574,10 +574,96 @@ impl Sandbox {
|
|
|
574
574
|
/// the combination with `replace`.
|
|
575
575
|
fn connect_or_create(name: String, opts: RHash) -> Result<Sandbox, Error> {
|
|
576
576
|
let b = Self::build_builder(name, opts)?;
|
|
577
|
+
prepare_local_sandboxes_dir()?;
|
|
577
578
|
let inner = block_on(b.connect_or_create())?.map_err(error::to_ruby)?;
|
|
578
579
|
Ok(Sandbox::from_inner(inner))
|
|
579
580
|
}
|
|
580
581
|
|
|
582
|
+
/// Boot a new sandbox from a snapshot (runtime v0.7 `Sandbox::restore`).
|
|
583
|
+
/// `snapshot` is a selector — an id, `group:member`, digest or path — read
|
|
584
|
+
/// as `snapshot_kind` says: "auto" (the default, the core decides), "id"
|
|
585
|
+
/// or "path" (a `SnapshotInfo#reference` keeps its kind). `opts` is the
|
|
586
|
+
/// Ruby layer's whitelist: name (required), cpus, memory, user, log_level,
|
|
587
|
+
/// max_duration, idle_timeout, volumes, ports, disable_network,
|
|
588
|
+
/// cow_memory, disk_only, snapshot_base, allow_missing_resources,
|
|
589
|
+
/// external_mount_policy. Upstream always restores detached.
|
|
590
|
+
fn restore(snapshot: String, opts: RHash) -> Result<Sandbox, Error> {
|
|
591
|
+
// `Sandbox::restore` builds a `SandboxBuilder`, which reads the default
|
|
592
|
+
// backend synchronously, like `build_builder`.
|
|
593
|
+
crate::backend::ensure_this_process_backend()?;
|
|
594
|
+
let host_paths = HostPaths::for_default_backend()?;
|
|
595
|
+
let reference = match conv::opt_string(opts, "snapshot_kind")?.as_deref() {
|
|
596
|
+
None | Some("auto") => SnapshotReference::auto(host_paths.pin_snapshot(snapshot)?),
|
|
597
|
+
Some("id") => SnapshotReference::id(snapshot),
|
|
598
|
+
Some("path") => SnapshotReference::path(
|
|
599
|
+
host_paths
|
|
600
|
+
.pin(snapshot, "snapshot path")?
|
|
601
|
+
.to_string_lossy()
|
|
602
|
+
.into_owned(),
|
|
603
|
+
),
|
|
604
|
+
Some(other) => {
|
|
605
|
+
return Err(error::base_error(format!(
|
|
606
|
+
"unknown snapshot reference kind {other:?} (expected auto/id/path)"
|
|
607
|
+
)))
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
let name = conv::opt_string(opts, "name")?
|
|
611
|
+
.ok_or_else(|| error::base_error("restore needs name: (the new sandbox name)"))?;
|
|
612
|
+
let mut b = microsandbox::Sandbox::restore_ref(reference).name(name);
|
|
613
|
+
if let Some(v) = conv::opt_u8(opts, "cpus")? {
|
|
614
|
+
b = b.cpus(v);
|
|
615
|
+
}
|
|
616
|
+
if let Some(v) = conv::opt_u32(opts, "memory")? {
|
|
617
|
+
b = b.memory(v);
|
|
618
|
+
}
|
|
619
|
+
if let Some(v) = conv::opt_string(opts, "user")? {
|
|
620
|
+
b = b.user(v);
|
|
621
|
+
}
|
|
622
|
+
if let Some(level) = conv::opt_string(opts, "log_level")? {
|
|
623
|
+
b = b.log_level(log_level_from_str(&level)?);
|
|
624
|
+
}
|
|
625
|
+
if let Some(secs) = conv::opt::<u64>(opts, "max_duration")? {
|
|
626
|
+
b = b.max_duration(secs);
|
|
627
|
+
}
|
|
628
|
+
if let Some(secs) = conv::opt::<u64>(opts, "idle_timeout")? {
|
|
629
|
+
b = b.idle_timeout(secs);
|
|
630
|
+
}
|
|
631
|
+
for (guest, spec) in parse_volumes(opts, host_paths)? {
|
|
632
|
+
b = b.volume(guest, move |mb| spec.apply(mb));
|
|
633
|
+
}
|
|
634
|
+
for (host, guest) in conv::opt_port_map(opts, "ports")? {
|
|
635
|
+
b = b.port(host, guest);
|
|
636
|
+
}
|
|
637
|
+
if conv::opt_bool(opts, "disable_network")? {
|
|
638
|
+
b = b.disable_network();
|
|
639
|
+
}
|
|
640
|
+
if conv::opt_bool(opts, "cow_memory")? {
|
|
641
|
+
b = b.cow_memory();
|
|
642
|
+
}
|
|
643
|
+
if conv::opt_bool(opts, "disk_only")? {
|
|
644
|
+
b = b.disk_only();
|
|
645
|
+
}
|
|
646
|
+
if let Some(base) = conv::opt_string(opts, "snapshot_base")? {
|
|
647
|
+
b = b.snapshot_base(host_paths.pin_snapshot(base)?);
|
|
648
|
+
}
|
|
649
|
+
if conv::opt_bool(opts, "allow_missing_resources")? {
|
|
650
|
+
b = b.allow_missing_resources();
|
|
651
|
+
}
|
|
652
|
+
match conv::opt_string(opts, "external_mount_policy")?.as_deref() {
|
|
653
|
+
None => {}
|
|
654
|
+
Some("strict") => b = b.external_mount_policy(ExternalMountRestorePolicy::Strict),
|
|
655
|
+
Some("relaxed") => b = b.external_mount_policy(ExternalMountRestorePolicy::Relaxed),
|
|
656
|
+
Some(other) => {
|
|
657
|
+
return Err(error::base_error(format!(
|
|
658
|
+
"unknown external_mount_policy {other:?} (expected strict/relaxed)"
|
|
659
|
+
)))
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
prepare_local_sandboxes_dir()?;
|
|
663
|
+
let inner = block_on(b.restore())?.map_err(error::to_ruby)?;
|
|
664
|
+
Ok(Sandbox::from_inner(inner))
|
|
665
|
+
}
|
|
666
|
+
|
|
581
667
|
/// Restart a previously-defined sandbox by name.
|
|
582
668
|
fn start(name: String, opts: RHash) -> Result<Sandbox, Error> {
|
|
583
669
|
let detached = conv::opt_bool(opts, "detached")?;
|
|
@@ -722,7 +808,8 @@ impl Sandbox {
|
|
|
722
808
|
Ok(ExecHandle::from_core(handle))
|
|
723
809
|
}
|
|
724
810
|
|
|
725
|
-
/// Graceful stop
|
|
811
|
+
/// Graceful stop, waiting without a deadline (runtime v0.7 never escalates
|
|
812
|
+
/// to a kill; see `stop_with_timeout`), routed
|
|
726
813
|
/// straight through the live sandbox like every other lifecycle method here
|
|
727
814
|
/// and like both official bindings (`sdk/python/src/sandbox.rs`,
|
|
728
815
|
/// `sdk/ruby/ext/.../lib.rs`).
|
|
@@ -750,12 +837,35 @@ impl Sandbox {
|
|
|
750
837
|
Ok(exit_status_to_hash(status))
|
|
751
838
|
}
|
|
752
839
|
|
|
753
|
-
///
|
|
840
|
+
/// Graceful stop under one budget (seconds), including dispatch and the
|
|
841
|
+
/// runtime's release. Expiry is `StopTimeoutError` and kills nothing; a
|
|
842
|
+
/// zero budget times out before dispatch (runtime v0.7).
|
|
843
|
+
fn stop_with_timeout(&self, secs: f64) -> Result<(), Error> {
|
|
844
|
+
let duration = secs_to_duration(secs)?;
|
|
845
|
+
let inner = self.inner.get()?;
|
|
846
|
+
block_on(inner.stop_with_timeout(duration))?.map_err(error::to_ruby)
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/// Force kill (upstream `kill`): waits without a deadline for the name's
|
|
850
|
+
/// transition lock, then up to 5 s for the stopped state.
|
|
754
851
|
fn kill(&self) -> Result<(), Error> {
|
|
755
852
|
let inner = self.inner.get()?;
|
|
756
853
|
block_on(inner.kill())?.map_err(error::to_ruby)
|
|
757
854
|
}
|
|
758
855
|
|
|
856
|
+
/// Force kill under one budget of `secs`, transition lock and dispatch
|
|
857
|
+
/// included (see `kill_within`).
|
|
858
|
+
fn kill_with_timeout(&self, secs: f64) -> Result<(), Error> {
|
|
859
|
+
let duration = secs_to_duration(secs)?;
|
|
860
|
+
let inner = self.inner.get()?;
|
|
861
|
+
block_on(kill_within(
|
|
862
|
+
inner.name(),
|
|
863
|
+
duration,
|
|
864
|
+
inner.kill_with_timeout(duration),
|
|
865
|
+
))?
|
|
866
|
+
.map_err(error::to_ruby)
|
|
867
|
+
}
|
|
868
|
+
|
|
759
869
|
/// Trigger a graceful drain (SIGUSR1 on local).
|
|
760
870
|
fn drain(&self) -> Result<(), Error> {
|
|
761
871
|
let inner = self.inner.get()?;
|
|
@@ -1342,81 +1452,39 @@ fn parse_patches(opts: RHash, host_paths: HostPaths) -> Result<Vec<Patch>, Error
|
|
|
1342
1452
|
// Secret parsing (mirrors the Python/Node SecretEntry surface)
|
|
1343
1453
|
//--------------------------------------------------------------------------------------------------
|
|
1344
1454
|
|
|
1345
|
-
///
|
|
1346
|
-
///
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
impl ViolationSpec {
|
|
1359
|
-
fn apply(self, mut v: ViolationActionBuilder) -> ViolationActionBuilder {
|
|
1360
|
-
match self {
|
|
1361
|
-
ViolationSpec::Block => v.block(),
|
|
1362
|
-
ViolationSpec::BlockAndLog => v.block_and_log(),
|
|
1363
|
-
ViolationSpec::BlockAndTerminate => v.block_and_terminate(),
|
|
1364
|
-
ViolationSpec::Passthrough {
|
|
1365
|
-
hosts,
|
|
1366
|
-
patterns,
|
|
1367
|
-
all,
|
|
1368
|
-
} => {
|
|
1369
|
-
for h in hosts {
|
|
1370
|
-
v = v.passthrough_host(h);
|
|
1371
|
-
}
|
|
1372
|
-
for p in patterns {
|
|
1373
|
-
v = v.passthrough_host_pattern(p);
|
|
1374
|
-
}
|
|
1375
|
-
if all {
|
|
1376
|
-
v = v.passthrough_all_hosts(true);
|
|
1377
|
-
}
|
|
1378
|
-
v
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1455
|
+
/// Parse a secret-leak action. The Ruby layer normalizes the spelling (kebab
|
|
1456
|
+
/// or snake case) before it gets here; this rejects anything else so a typo
|
|
1457
|
+
/// can never silently fall back to the default action.
|
|
1458
|
+
fn parse_violation_action(v: &str) -> Result<SecretViolationAction, Error> {
|
|
1459
|
+
match v {
|
|
1460
|
+
"block" => Ok(SecretViolationAction::Block),
|
|
1461
|
+
"block_and_log" => Ok(SecretViolationAction::BlockAndLog),
|
|
1462
|
+
"block_and_terminate" => Ok(SecretViolationAction::BlockAndTerminate),
|
|
1463
|
+
other => Err(error::base_error(format!(
|
|
1464
|
+
"unknown secret violation action {other:?} (expected block, block_and_log, or block_and_terminate)"
|
|
1465
|
+
))),
|
|
1381
1466
|
}
|
|
1382
1467
|
}
|
|
1383
1468
|
|
|
1384
|
-
///
|
|
1385
|
-
///
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
other => Err(error::base_error(format!(
|
|
1393
|
-
"unknown on_violation {other:?} (expected block/block_and_log/\
|
|
1394
|
-
block_and_terminate, or a Hash with :passthrough_hosts/\
|
|
1395
|
-
:passthrough_host_patterns/:passthrough_all_hosts)"
|
|
1396
|
-
))),
|
|
1397
|
-
};
|
|
1398
|
-
}
|
|
1399
|
-
let h = RHash::try_convert(v)
|
|
1400
|
-
.map_err(|_| error::base_error("on_violation must be a String or a Hash"))?;
|
|
1401
|
-
Ok(ViolationSpec::Passthrough {
|
|
1402
|
-
hosts: conv::opt_string_vec(h, "passthrough_hosts")?,
|
|
1403
|
-
patterns: conv::opt_string_vec(h, "passthrough_host_patterns")?,
|
|
1404
|
-
all: conv::opt_bool(h, "passthrough_all_hosts")?,
|
|
1405
|
-
})
|
|
1469
|
+
/// Which request locations a secret is substituted in. `None` leaves the
|
|
1470
|
+
/// builder default (headers on, query/body off).
|
|
1471
|
+
#[derive(Default)]
|
|
1472
|
+
struct SubstitutionSpec {
|
|
1473
|
+
headers: Option<bool>,
|
|
1474
|
+
header_fields: Option<Vec<String>>,
|
|
1475
|
+
query: Option<bool>,
|
|
1476
|
+
body: Option<bool>,
|
|
1406
1477
|
}
|
|
1407
1478
|
|
|
1408
1479
|
struct SecretSpec {
|
|
1409
1480
|
env: String,
|
|
1410
1481
|
value: String,
|
|
1411
|
-
|
|
1412
|
-
|
|
1482
|
+
allow: Vec<String>,
|
|
1483
|
+
allow_placeholder_for: Vec<String>,
|
|
1413
1484
|
placeholder: Option<String>,
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
inject_query: Option<bool>,
|
|
1418
|
-
inject_body: Option<bool>,
|
|
1419
|
-
on_violation: Option<ViolationSpec>,
|
|
1485
|
+
require_tls_identity: Option<bool>,
|
|
1486
|
+
violation_action: Option<SecretViolationAction>,
|
|
1487
|
+
substitution: Option<SubstitutionSpec>,
|
|
1420
1488
|
}
|
|
1421
1489
|
|
|
1422
1490
|
fn parse_secret(h: RHash) -> Result<SecretSpec, Error> {
|
|
@@ -1424,60 +1492,78 @@ fn parse_secret(h: RHash) -> Result<SecretSpec, Error> {
|
|
|
1424
1492
|
conv::opt_string(h, "env")?.ok_or_else(|| error::base_error("secret requires :env"))?;
|
|
1425
1493
|
let value =
|
|
1426
1494
|
conv::opt_string(h, "value")?.ok_or_else(|| error::base_error("secret requires :value"))?;
|
|
1427
|
-
let
|
|
1428
|
-
|
|
1429
|
-
|
|
1495
|
+
let allow = conv::opt_string_vec(h, "allow")?;
|
|
1496
|
+
// The Ruby layer already rejects this; checked again here because the
|
|
1497
|
+
// upstream builder asserts on an empty allow list (a panic across FFI).
|
|
1498
|
+
if allow.is_empty() {
|
|
1430
1499
|
return Err(error::base_error(
|
|
1431
|
-
"secret requires at least one allowed host (:
|
|
1500
|
+
"secret requires at least one allowed host (:allow)",
|
|
1432
1501
|
));
|
|
1433
1502
|
}
|
|
1434
|
-
let
|
|
1435
|
-
.map(
|
|
1503
|
+
let violation_action = conv::opt_string(h, "violation_action")?
|
|
1504
|
+
.map(|v| parse_violation_action(&v))
|
|
1436
1505
|
.transpose()?;
|
|
1506
|
+
let substitution = match conv::opt::<RHash>(h, "substitution")? {
|
|
1507
|
+
Some(sub) => Some(SubstitutionSpec {
|
|
1508
|
+
headers: conv::opt::<bool>(sub, "headers")?,
|
|
1509
|
+
header_fields: conv::opt::<Vec<String>>(sub, "header_fields")?,
|
|
1510
|
+
query: conv::opt::<bool>(sub, "query")?,
|
|
1511
|
+
body: conv::opt::<bool>(sub, "body")?,
|
|
1512
|
+
}),
|
|
1513
|
+
None => None,
|
|
1514
|
+
};
|
|
1437
1515
|
Ok(SecretSpec {
|
|
1438
1516
|
env,
|
|
1439
1517
|
value,
|
|
1440
|
-
|
|
1441
|
-
|
|
1518
|
+
allow,
|
|
1519
|
+
allow_placeholder_for: conv::opt_string_vec(h, "allow_placeholder_for")?,
|
|
1442
1520
|
placeholder: conv::opt_string(h, "placeholder")?,
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
inject_query: conv::opt::<bool>(h, "inject_query")?,
|
|
1447
|
-
inject_body: conv::opt::<bool>(h, "inject_body")?,
|
|
1448
|
-
on_violation,
|
|
1521
|
+
require_tls_identity: conv::opt::<bool>(h, "require_tls_identity")?,
|
|
1522
|
+
violation_action,
|
|
1523
|
+
substitution,
|
|
1449
1524
|
})
|
|
1450
1525
|
}
|
|
1451
1526
|
|
|
1452
1527
|
impl SecretSpec {
|
|
1528
|
+
/// Apply in the order the Python SDK uses (`sdk/python/src/helpers.rs`).
|
|
1453
1529
|
fn apply(self, mut s: SecretBuilder) -> SecretBuilder {
|
|
1454
1530
|
s = s.env(self.env).value(self.value);
|
|
1455
|
-
for host in self.
|
|
1456
|
-
|
|
1531
|
+
for host in self.allow {
|
|
1532
|
+
// `allow("*")` asserts upstream; the any-host form is an explicit,
|
|
1533
|
+
// separately named call.
|
|
1534
|
+
s = if host == "*" {
|
|
1535
|
+
s.allow_any_host_dangerous(true)
|
|
1536
|
+
} else {
|
|
1537
|
+
s.allow(host)
|
|
1538
|
+
};
|
|
1539
|
+
}
|
|
1540
|
+
for host in self.allow_placeholder_for {
|
|
1541
|
+
s = s.allow_placeholder_for(host);
|
|
1457
1542
|
}
|
|
1458
|
-
|
|
1459
|
-
s = s.
|
|
1543
|
+
if let Some(action) = self.violation_action {
|
|
1544
|
+
s = s.violation_action(action);
|
|
1460
1545
|
}
|
|
1461
1546
|
if let Some(p) = self.placeholder {
|
|
1462
1547
|
s = s.placeholder(p);
|
|
1463
1548
|
}
|
|
1464
|
-
if let Some(
|
|
1465
|
-
s = s.require_tls_identity(
|
|
1466
|
-
}
|
|
1467
|
-
if let Some(enabled) = self.inject_headers {
|
|
1468
|
-
s = s.inject_headers(enabled);
|
|
1469
|
-
}
|
|
1470
|
-
if let Some(enabled) = self.inject_basic_auth {
|
|
1471
|
-
s = s.inject_basic_auth(enabled);
|
|
1549
|
+
if let Some(required) = self.require_tls_identity {
|
|
1550
|
+
s = s.require_tls_identity(required);
|
|
1472
1551
|
}
|
|
1473
|
-
if let Some(
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1552
|
+
if let Some(sub) = self.substitution {
|
|
1553
|
+
if let Some(fields) = sub.header_fields {
|
|
1554
|
+
s = s.substitute_in_header_fields(fields);
|
|
1555
|
+
}
|
|
1556
|
+
// After the field list: `substitute_in_header_fields` turns header
|
|
1557
|
+
// substitution on, and an explicit `headers: false` must still win.
|
|
1558
|
+
if let Some(enabled) = sub.headers {
|
|
1559
|
+
s = s.substitute_in_headers(enabled);
|
|
1560
|
+
}
|
|
1561
|
+
if let Some(enabled) = sub.query {
|
|
1562
|
+
s = s.substitute_in_query(enabled);
|
|
1563
|
+
}
|
|
1564
|
+
if let Some(enabled) = sub.body {
|
|
1565
|
+
s = s.substitute_in_body(enabled);
|
|
1566
|
+
}
|
|
1481
1567
|
}
|
|
1482
1568
|
s
|
|
1483
1569
|
}
|
|
@@ -1787,6 +1873,167 @@ enum MountBacking {
|
|
|
1787
1873
|
Tmpfs,
|
|
1788
1874
|
}
|
|
1789
1875
|
|
|
1876
|
+
/// One parsed `volumes` entry (see [`parse_volumes`]).
|
|
1877
|
+
struct VolumeSpec {
|
|
1878
|
+
backing: MountBacking,
|
|
1879
|
+
size_mib: Option<u32>,
|
|
1880
|
+
quota_mib: Option<u32>,
|
|
1881
|
+
format: Option<DiskImageFormat>,
|
|
1882
|
+
fstype: Option<String>,
|
|
1883
|
+
readonly: bool,
|
|
1884
|
+
noexec: bool,
|
|
1885
|
+
nosuid: bool,
|
|
1886
|
+
nodev: bool,
|
|
1887
|
+
stat_virt: Option<StatVirtualization>,
|
|
1888
|
+
host_perms: Option<HostPermissions>,
|
|
1889
|
+
override_uid: Option<u32>,
|
|
1890
|
+
override_gid: Option<u32>,
|
|
1891
|
+
follow_root_symlinks: Option<bool>,
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
impl VolumeSpec {
|
|
1895
|
+
/// Configure the core's `MountBuilder`. Create and restore share this:
|
|
1896
|
+
/// both builders take `volume(guest, |m| …)`.
|
|
1897
|
+
fn apply(self, mut mb: MountBuilder) -> MountBuilder {
|
|
1898
|
+
mb = match self.backing {
|
|
1899
|
+
MountBacking::Named(name) => mb.named(name),
|
|
1900
|
+
MountBacking::Tmpfs => mb.tmpfs(),
|
|
1901
|
+
MountBacking::Disk(host) => mb.disk(host),
|
|
1902
|
+
MountBacking::Bind(host) => mb.bind(host),
|
|
1903
|
+
};
|
|
1904
|
+
if let Some(n) = self.size_mib {
|
|
1905
|
+
mb = mb.size(n);
|
|
1906
|
+
}
|
|
1907
|
+
if let Some(q) = self.quota_mib {
|
|
1908
|
+
mb = mb.quota(q);
|
|
1909
|
+
}
|
|
1910
|
+
if let Some(f) = self.format {
|
|
1911
|
+
mb = mb.format(f);
|
|
1912
|
+
}
|
|
1913
|
+
if let Some(ft) = self.fstype {
|
|
1914
|
+
mb = mb.fstype(ft);
|
|
1915
|
+
}
|
|
1916
|
+
if self.readonly {
|
|
1917
|
+
mb = mb.readonly();
|
|
1918
|
+
}
|
|
1919
|
+
if self.noexec {
|
|
1920
|
+
mb = mb.noexec();
|
|
1921
|
+
}
|
|
1922
|
+
if self.nosuid {
|
|
1923
|
+
mb = mb.nosuid();
|
|
1924
|
+
}
|
|
1925
|
+
if self.nodev {
|
|
1926
|
+
mb = mb.nodev();
|
|
1927
|
+
}
|
|
1928
|
+
if let Some(sv) = self.stat_virt {
|
|
1929
|
+
mb = mb.stat_virtualization(sv);
|
|
1930
|
+
}
|
|
1931
|
+
if let Some(hp) = self.host_perms {
|
|
1932
|
+
mb = mb.host_permissions(hp);
|
|
1933
|
+
}
|
|
1934
|
+
if let (Some(uid), Some(gid)) = (self.override_uid, self.override_gid) {
|
|
1935
|
+
mb = mb.owner(uid, gid);
|
|
1936
|
+
}
|
|
1937
|
+
if let Some(follow) = self.follow_root_symlinks {
|
|
1938
|
+
mb = mb.follow_root_symlinks(follow);
|
|
1939
|
+
}
|
|
1940
|
+
mb
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
/// Parse the `volumes` option: each mount is normalized by the Ruby layer to a string-keyed
|
|
1945
|
+
/// Hash — guest (req), kind ("bind"/"named"/"tmpfs"/"disk"), source
|
|
1946
|
+
/// (bind/named/disk), size_mib (tmpfs/disk), format + fstype (disk),
|
|
1947
|
+
/// readonly/noexec/nosuid/nodev (bool), stat_virtualization,
|
|
1948
|
+
/// host_permissions, override_uid/override_gid (u32 pair).
|
|
1949
|
+
/// Enum-valued options are validated up front (the
|
|
1950
|
+
/// volume closure can't return an error); the core validates the rest
|
|
1951
|
+
/// (e.g. rejecting stat_virtualization on tmpfs/disk) at create().
|
|
1952
|
+
fn parse_volumes(opts: RHash, host_paths: HostPaths) -> Result<Vec<(String, VolumeSpec)>, Error> {
|
|
1953
|
+
let mut out = Vec::new();
|
|
1954
|
+
for m in conv::opt_hash_vec(opts, "volumes")? {
|
|
1955
|
+
let guest = conv::opt_string(m, "guest")?
|
|
1956
|
+
.ok_or_else(|| error::base_error("volume mount is missing :guest"))?;
|
|
1957
|
+
let kind = conv::opt_string(m, "kind")?
|
|
1958
|
+
.ok_or_else(|| error::base_error("volume mount is missing :kind"))?;
|
|
1959
|
+
let source = conv::opt_string(m, "source")?;
|
|
1960
|
+
let size_mib = conv::opt_u32(m, "size_mib")?;
|
|
1961
|
+
// Bind-mount guest-write quota override (MiB). The core's MountBuilder
|
|
1962
|
+
// applies it to bind mounts only and rejects it on tmpfs/disk/named at
|
|
1963
|
+
// build(); we forward it as-is so that validation lives in one place.
|
|
1964
|
+
let quota_mib = conv::opt_u32(m, "quota_mib")?;
|
|
1965
|
+
let fstype = conv::opt_string(m, "fstype")?;
|
|
1966
|
+
let readonly = conv::opt_bool(m, "readonly")?;
|
|
1967
|
+
let noexec = conv::opt_bool(m, "noexec")?;
|
|
1968
|
+
let nosuid = conv::opt_bool(m, "nosuid")?;
|
|
1969
|
+
let nodev = conv::opt_bool(m, "nodev")?;
|
|
1970
|
+
let format = conv::opt_string(m, "format")?
|
|
1971
|
+
.map(|f| disk_format_from_str(&f))
|
|
1972
|
+
.transpose()?;
|
|
1973
|
+
let stat_virt = conv::opt_string(m, "stat_virtualization")?
|
|
1974
|
+
.map(|s| stat_virtualization_from_str(&s))
|
|
1975
|
+
.transpose()?;
|
|
1976
|
+
let host_perms = conv::opt_string(m, "host_permissions")?
|
|
1977
|
+
.map(|s| host_permissions_from_str(&s))
|
|
1978
|
+
.transpose()?;
|
|
1979
|
+
// v0.6.15: the fallback guest owner presented for host files that
|
|
1980
|
+
// carry no per-file stat override. The Ruby layer validates the pair
|
|
1981
|
+
// (both-or-neither, u32 range, bind/named only, not with
|
|
1982
|
+
// stat_virtualization=off); whether a *named* volume is directory-
|
|
1983
|
+
// or disk-backed is known only to the core, which rejects the
|
|
1984
|
+
// disk-backed case at build().
|
|
1985
|
+
let override_uid = conv::opt_u32(m, "override_uid")?;
|
|
1986
|
+
let override_gid = conv::opt_u32(m, "override_gid")?;
|
|
1987
|
+
// v0.6.7: mount-root symlink protection is on by default; this is
|
|
1988
|
+
// the per-mount opt-out. Valid for bind/named-directory mounts —
|
|
1989
|
+
// the core rejects it elsewhere at build().
|
|
1990
|
+
let follow_root_symlinks = conv::opt::<bool>(m, "follow_root_symlinks")?;
|
|
1991
|
+
// bind/named/disk require a source; tmpfs must not have one. A
|
|
1992
|
+
// bind or disk source is a host path; a named source is a volume
|
|
1993
|
+
// name and is never resolved.
|
|
1994
|
+
let backing = match (kind.as_str(), source) {
|
|
1995
|
+
("bind", Some(host)) => {
|
|
1996
|
+
MountBacking::Bind(host_paths.pin(host, "bind mount host path")?)
|
|
1997
|
+
}
|
|
1998
|
+
("disk", Some(host)) => {
|
|
1999
|
+
MountBacking::Disk(host_paths.pin(host, "disk mount host path")?)
|
|
2000
|
+
}
|
|
2001
|
+
("named", Some(name)) => MountBacking::Named(name),
|
|
2002
|
+
("bind" | "named" | "disk", None) => {
|
|
2003
|
+
return Err(error::base_error(format!(
|
|
2004
|
+
"volume mount kind {kind:?} requires a source"
|
|
2005
|
+
)))
|
|
2006
|
+
}
|
|
2007
|
+
("tmpfs", _) => MountBacking::Tmpfs,
|
|
2008
|
+
(other, _) => {
|
|
2009
|
+
return Err(error::base_error(format!(
|
|
2010
|
+
"unknown volume mount kind {other:?} (expected bind/named/tmpfs/disk)"
|
|
2011
|
+
)))
|
|
2012
|
+
}
|
|
2013
|
+
};
|
|
2014
|
+
out.push((
|
|
2015
|
+
guest,
|
|
2016
|
+
VolumeSpec {
|
|
2017
|
+
backing,
|
|
2018
|
+
size_mib,
|
|
2019
|
+
quota_mib,
|
|
2020
|
+
format,
|
|
2021
|
+
fstype,
|
|
2022
|
+
readonly,
|
|
2023
|
+
noexec,
|
|
2024
|
+
nosuid,
|
|
2025
|
+
nodev,
|
|
2026
|
+
stat_virt,
|
|
2027
|
+
host_perms,
|
|
2028
|
+
override_uid,
|
|
2029
|
+
override_gid,
|
|
2030
|
+
follow_root_symlinks,
|
|
2031
|
+
},
|
|
2032
|
+
));
|
|
2033
|
+
}
|
|
2034
|
+
Ok(out)
|
|
2035
|
+
}
|
|
2036
|
+
|
|
1790
2037
|
/// Parsed `root_disk` create option. Integer shorthand = managed upper of N
|
|
1791
2038
|
/// MiB (mirrors Python's `root_disk=8192` and the CLI bare-size form); Hash =
|
|
1792
2039
|
/// explicit kind. Kind/field cross-validation (size on a disk image, etc.)
|
|
@@ -2455,6 +2702,9 @@ fn build_modify_patch(opts: RHash) -> Result<SandboxModificationPatch, Error> {
|
|
|
2455
2702
|
workdir: conv::opt_string(opts, "workdir")?,
|
|
2456
2703
|
secrets: parse_modify_secrets(opts)?,
|
|
2457
2704
|
secrets_remove: conv::opt_string_vec(opts, "remove_secrets")?,
|
|
2705
|
+
// Published-port edits (`ports`/`ports_remove`, runtime v0.7) are not
|
|
2706
|
+
// exposed yet: empty means "leave the ports as they are".
|
|
2707
|
+
..Default::default()
|
|
2458
2708
|
})
|
|
2459
2709
|
}
|
|
2460
2710
|
|
|
@@ -2490,6 +2740,10 @@ fn parse_modify_secrets(opts: RHash) -> Result<Vec<SecretModificationPatch>, Err
|
|
|
2490
2740
|
value: value.unwrap_or_default().into(),
|
|
2491
2741
|
placeholder: conv::opt_string(h, "placeholder")?,
|
|
2492
2742
|
allowed_hosts: conv::opt_string_vec(h, "allowed_hosts")?,
|
|
2743
|
+
// The v0.7 policy fields (substitution, placeholder hosts,
|
|
2744
|
+
// violation action, TLS-identity requirement) are not exposed on
|
|
2745
|
+
// modify yet; None/empty keeps the stored values.
|
|
2746
|
+
..Default::default()
|
|
2493
2747
|
});
|
|
2494
2748
|
}
|
|
2495
2749
|
Ok(out)
|
|
@@ -2740,28 +2994,38 @@ impl SbHandle {
|
|
|
2740
2994
|
.map(|dt| dt.timestamp_millis())
|
|
2741
2995
|
}
|
|
2742
2996
|
|
|
2743
|
-
/// Graceful stop
|
|
2997
|
+
/// Graceful stop, waiting without a deadline (runtime v0.7: no escalation
|
|
2998
|
+
/// to a kill).
|
|
2744
2999
|
fn stop(&self) -> Result<(), Error> {
|
|
2745
3000
|
let inner = self.inner.get()?;
|
|
2746
3001
|
block_on(inner.stop())?.map_err(error::to_ruby)
|
|
2747
3002
|
}
|
|
2748
3003
|
|
|
2749
|
-
/// Graceful stop
|
|
3004
|
+
/// Graceful stop under one budget (seconds); expiry is `StopTimeoutError`
|
|
3005
|
+
/// and kills nothing.
|
|
2750
3006
|
fn stop_with_timeout(&self, secs: f64) -> Result<(), Error> {
|
|
2751
3007
|
let inner = self.inner.get()?;
|
|
2752
3008
|
block_on(inner.stop_with_timeout(secs_to_duration(secs)?))?.map_err(error::to_ruby)
|
|
2753
3009
|
}
|
|
2754
3010
|
|
|
2755
|
-
/// Force kill (
|
|
3011
|
+
/// Force kill (upstream `kill`): waits without a deadline for the name's
|
|
3012
|
+
/// transition lock, then up to 5 s for the stopped state.
|
|
2756
3013
|
fn kill(&self) -> Result<(), Error> {
|
|
2757
3014
|
let inner = self.inner.get()?;
|
|
2758
3015
|
block_on(inner.kill())?.map_err(error::to_ruby)
|
|
2759
3016
|
}
|
|
2760
3017
|
|
|
2761
|
-
/// Force kill
|
|
3018
|
+
/// Force kill under one budget of `secs`, transition lock and dispatch
|
|
3019
|
+
/// included (see `kill_within`).
|
|
2762
3020
|
fn kill_with_timeout(&self, secs: f64) -> Result<(), Error> {
|
|
3021
|
+
let duration = secs_to_duration(secs)?;
|
|
2763
3022
|
let inner = self.inner.get()?;
|
|
2764
|
-
block_on(
|
|
3023
|
+
block_on(kill_within(
|
|
3024
|
+
inner.name(),
|
|
3025
|
+
duration,
|
|
3026
|
+
inner.kill_with_timeout(duration),
|
|
3027
|
+
))?
|
|
3028
|
+
.map_err(error::to_ruby)
|
|
2765
3029
|
}
|
|
2766
3030
|
|
|
2767
3031
|
/// Send the graceful-shutdown request and return without waiting.
|
|
@@ -2974,6 +3238,7 @@ pub fn define(ruby: &Ruby, native: &RModule) -> Result<(), Error> {
|
|
|
2974
3238
|
function!(Sandbox::connect_or_create, 2),
|
|
2975
3239
|
)?;
|
|
2976
3240
|
class.define_singleton_method("start", function!(Sandbox::start, 2))?;
|
|
3241
|
+
class.define_singleton_method("restore", function!(Sandbox::restore, 2))?;
|
|
2977
3242
|
class.define_singleton_method("get", function!(Sandbox::get, 1))?;
|
|
2978
3243
|
class.define_singleton_method("list", function!(Sandbox::list, 0))?;
|
|
2979
3244
|
class.define_singleton_method("list_with", function!(Sandbox::list_with, 1))?;
|
|
@@ -2993,6 +3258,8 @@ pub fn define(ruby: &Ruby, native: &RModule) -> Result<(), Error> {
|
|
|
2993
3258
|
class.define_method("stop", method!(Sandbox::stop, 0))?;
|
|
2994
3259
|
class.define_method("stop_and_wait", method!(Sandbox::stop_and_wait, 0))?;
|
|
2995
3260
|
class.define_method("kill", method!(Sandbox::kill, 0))?;
|
|
3261
|
+
class.define_method("stop_with_timeout", method!(Sandbox::stop_with_timeout, 1))?;
|
|
3262
|
+
class.define_method("kill_with_timeout", method!(Sandbox::kill_with_timeout, 1))?;
|
|
2996
3263
|
class.define_method("drain", method!(Sandbox::drain, 0))?;
|
|
2997
3264
|
class.define_method("wait", method!(Sandbox::wait, 0))?;
|
|
2998
3265
|
class.define_method("wait_for_status", method!(Sandbox::wait_for_status, 1))?;
|