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
|
use std::path::{Path, PathBuf};
use std::process::Command;
use aur_build::config::Config;
/// The exact shape of the bug: a relative `--root`, saved verbatim, read back by
/// the next command.
#[test]
fn a_relative_root_is_absolutized_once_and_never_doubled() {
let rel = PathBuf::from("target").join("config-root-test").join("runtime");
let _ = std::fs::remove_dir_all(&rel);
let cfg = Config {
root: rel.clone(),
..Config::default()
};
cfg.save().unwrap();
let reloaded = Config::load(&rel).unwrap();
assert!(
reloaded.root.is_absolute(),
"a saved root must come back absolute, not relative: {}",
reloaded.root.display()
);
assert_eq!(reloaded.root, std::path::absolute(&rel).unwrap());
assert!(
!rel.join("runtime").exists(),
"the root must never be joined onto itself: {}",
rel.join("runtime").display()
);
// Everything derived hangs off the one root, so the database cannot end up in
// a second tree while the config sits in the first.
assert!(reloaded.db_path().starts_with(&reloaded.root));
assert!(reloaded.repo_arch_dir().starts_with(&reloaded.root));
std::fs::remove_dir_all(&rel).unwrap();
}
/// The legacy shape on disk: a config written by an `init` that saved `--root
/// runtime` verbatim. Loading it must not resurrect the doubled tree.
#[test]
fn a_stale_relative_root_in_the_file_is_ignored() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("config.json"),
"{\n \"root\": \"runtime\"\n}\n",
)
.unwrap();
let cfg = Config::load(tmp.path()).unwrap();
assert_eq!(cfg.root, std::path::absolute(tmp.path()).unwrap());
assert!(!cfg.root.join("runtime").exists());
}
#[test]
fn a_root_with_no_config_is_an_error_not_a_fresh_server() {
let tmp = tempfile::tempdir().unwrap();
let err = Config::load(tmp.path()).unwrap_err();
assert!(
err.to_string().contains("run `aur-build init`"),
"the caller must be able to branch on \"there is no config yet\": {err}"
);
}
/// The CLI half of the same rule: the old fallback invented a default config for
/// *any* command, so a typo'd `--root` quietly produced an empty server.
#[test]
fn the_cli_refuses_a_root_that_was_never_initialized() {
let tmp = tempfile::tempdir().unwrap();
let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
.args(["--root", &tmp.path().display().to_string(), "packages"])
.output()
.unwrap();
assert!(
!out.status.success(),
"a root with no config must fail, not answer from an empty server"
);
assert!(
out.stdout.is_empty(),
"nothing may be printed as though the server existed: {}",
String::from_utf8_lossy(&out.stdout)
);
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("init"), "{err}");
}
/// …and `init` is still allowed to run where there is no config yet.
#[test]
fn init_still_runs_where_there_is_no_config_yet() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("runtime");
let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
.args(["--root", &root.display().to_string(), "init", "--repo", "aur"])
.output()
.unwrap();
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
assert!(root.join("config.json").exists());
assert!(root.join("aur-build.db").exists());
// And the command after it agrees about where the tree is.
let cfg = Config::load(&root).unwrap();
assert_eq!(cfg.root, std::path::absolute(&root).unwrap());
assert!(!root.join("runtime").exists(), "no doubled tree");
}
/// A root that exists but holds nothing but a config still resolves to itself,
/// whichever way it was spelled.
#[test]
fn an_absolute_root_is_used_verbatim() {
let tmp = tempfile::tempdir().unwrap();
let dir: &Path = tmp.path();
std::fs::write(
dir.join("config.json"),
format!("{{\n \"root\": \"{}\"\n}}\n", dir.display()),
)
.unwrap();
assert_eq!(Config::load(dir).unwrap().root, dir);
}
// The sandbox configuration: three kinds, and only three. The kinds are an enum
// now, so a configuration that would silently mean "run the untrusted PKGBUILD as
// the hub's own user" does not parse.
/// Write a config carrying exactly this `sandbox` value and load it.
fn load_sandbox(sandbox: &str) -> Result<Config, String> {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("config.json"),
format!("{{\n \"sandbox\": {sandbox}\n}}\n"),
)
.unwrap();
Config::load(tmp.path()).map_err(|e| e.to_string())
}
/// Load a config written as an older `aur-build init` would have written it.
fn load_raw(body: &str) -> Result<Config, String> {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("config.json"), body).unwrap();
Config::load(tmp.path()).map_err(|e| e.to_string())
}
#[test]
fn each_sandbox_kind_parses_with_its_required_fields() {
match load_sandbox(r#"{"kind":"loopback"}"#).unwrap().sandbox {
aur_build::config::Sandbox::Loopback => {}
other => panic!("expected loopback, got {other:?}"),
}
let remote = load_sandbox(
r#"{"kind":"remote","listen":"127.0.0.1:8787","token":"s3cr3t"}"#,
)
.unwrap();
match &remote.sandbox {
aur_build::config::Sandbox::Remote { listen, token, shared_workspace } => {
assert_eq!(listen, "127.0.0.1:8787");
assert_eq!(token, "s3cr3t");
assert!(!*shared_workspace, "sharing the filesystem is asserted, never assumed");
}
other => panic!("expected remote, got {other:?}"),
}
let abs = load_sandbox(
r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds",
"bearer":"kube-token","image":"aur-build-sandbox:latest",
"listen":"0.0.0.0:8787","token":"s3cr3t","shared_workspace":true}"#,
)
.unwrap();
match &abs.sandbox {
aur_build::config::Sandbox::AgentSandbox { api, namespace, bearer, image, .. } => {
assert_eq!(api, "https://k8s:6443");
assert_eq!(namespace, "builds");
assert_eq!(bearer, "kube-token");
assert_eq!(image, "aur-build-sandbox:latest");
}
other => panic!("expected agent-sandbox, got {other:?}"),
}
assert_eq!(abs.sandbox_image(), "aur-build-sandbox:latest");
}
#[test]
fn a_missing_required_field_names_the_field() {
let err = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:8787"}"#).unwrap_err();
assert!(err.contains("token"), "the error must name the missing field: {err}");
let err = load_sandbox(r#"{"kind":"remote","token":"t"}"#).unwrap_err();
assert!(err.contains("listen"), "the error must name the missing field: {err}");
for missing in ["api", "namespace", "bearer", "image", "listen", "token"] {
let mut fields = vec![
r#""api":"https://k8s:6443""#.to_string(),
r#""namespace":"builds""#.to_string(),
r#""bearer":"kube""#.to_string(),
r#""image":"img""#.to_string(),
r#""listen":"0.0.0.0:1""#.to_string(),
r#""token":"t""#.to_string(),
];
fields.retain(|f| !f.starts_with(&format!("\"{missing}\"")));
let err = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", fields.join(",")))
.unwrap_err();
assert!(err.contains(missing), "dropping {missing} must name it: {err}");
}
}
#[test]
fn a_misspelled_field_is_refused_not_ignored() {
// deny_unknown_fields: `listn` must not parse as "no listen address given".
let err = load_sandbox(r#"{"kind":"remote","listn":"127.0.0.1:1","token":"t"}"#).unwrap_err();
assert!(err.contains("listn"), "a typo'd field name must be named: {err}");
}
#[test]
fn an_unknown_kind_names_the_value_and_the_valid_set() {
// The exact mistake this enum exists to catch: `kata` was a real intent,
// and the old match sent it quietly to loopback.
let err = load_sandbox(r#"{"kind":"kata"}"#).unwrap_err();
assert!(err.contains("kata"), "must name the bad value: {err}");
for kind in aur_build::config::Sandbox::KINDS {
assert!(err.contains(kind), "must name the valid set ({kind} missing): {err}");
}
}
#[test]
fn a_config_with_no_sandbox_object_is_the_local_backend() {
// The contract with every config.json already on disk: saying nothing means
// loopback.
let cfg = load_raw("{\n \"repo_name\": \"aur\"\n}\n").unwrap();
assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback));
assert_eq!(cfg.repo_name, "aur", "the rest of the config is untouched");
assert_eq!(cfg.sandbox_image(), aur_build::config::DEFAULT_SANDBOX_IMAGE);
assert!(cfg.sandbox.shared_workspace(), "loopback shares the filesystem by construction");
}
/// The file an older `aur-build init` wrote, verbatim. It carries the retired keys,
/// but they say what the default already means, so it must still load.
#[test]
fn a_config_init_wrote_before_the_enum_still_loads() {
let cfg = load_raw(
r#"{
"root": "/var/lib/aur-build",
"repo_name": "aur",
"arch": "x86_64",
"signing_key_id": "",
"bind": "127.0.0.1:8484",
"password": "",
"base_url": "http://127.0.0.1:8484",
"build_profile": "extra-x86_64",
"git_bin": "git",
"makepkg_bin": "makepkg",
"pacman_bin": "pacman",
"repo_add_bin": "repo-add",
"gpg_bin": "gpg",
"build_user": "",
"sandbox_backend": "loopback",
"agent_listen": "",
"agent_token": "",
"sandbox_image": "aur-build-sandbox:latest",
"max_concurrent_builds": 1,
"lease_secs": 90,
"min_free_disk_mb": 1024,
"max_transient_attempts": 3,
"dispatch_tick_secs": 2
}"#,
)
.unwrap();
assert!(matches!(cfg.sandbox, aur_build::config::Sandbox::Loopback));
assert_eq!(cfg.lease_secs, 90);
}
/// …but retired keys that *would* have changed behaviour are refused, not quietly
/// reinterpreted.
#[test]
fn retired_sandbox_keys_that_mean_something_are_refused() {
let err = load_raw(r#"{ "sandbox_backend": "remote", "agent_listen": "0.0.0.0:1", "agent_token": "t" }"#)
.unwrap_err();
assert!(err.contains("sandbox_backend"), "must name the retired key: {err}");
assert!(err.contains("sandbox"), "must point at the replacement: {err}");
// Even one retired key with content is enough to refuse.
let err = load_raw(r#"{ "sandbox_backend": "kata" }"#).unwrap_err();
assert!(err.contains("sandbox_backend"), "{err}");
// An empty retired key is noise, not intent, and stays tolerated.
assert!(load_raw(r#"{ "sandbox_backend": "loopback", "agent_token": "" }"#).is_ok());
}
#[test]
fn the_rendezvous_secrets_never_reach_debug() {
// A config that ends up in a log line, a panic, or an audit payload must
// not carry the token or the cluster bearer.
let cfg = load_sandbox(
r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"builds",
"bearer":"kube-token","image":"img","listen":"0.0.0.0:1","token":"s3cr3t"}"#,
)
.unwrap();
let shown = format!("{cfg:?}");
assert!(!shown.contains("s3cr3t"), "the rendezvous token leaked into Debug");
assert!(!shown.contains("kube-token"), "the cluster bearer leaked into Debug");
assert!(shown.contains("builds"), "the non-secret fields are still visible: {shown}");
}
// The startup guard: the enum makes the impossible unrepresentable, this refuses
// what a type cannot see — a field present but empty, and a remote backend that
// was never told the workspace is shared.
/// A config that parses but cannot work must be refused by `validate`, not
/// discovered at the step that needed the thing it is missing.
#[test]
fn a_field_that_is_present_but_empty_is_refused() {
let cfg = load_sandbox(r#"{"kind":"remote","listen":"","token":"t","shared_workspace":true}"#)
.unwrap();
let err = cfg.validate().unwrap_err().to_string();
assert!(err.contains("listen"), "must name the empty field: {err}");
let cfg = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:1","token":" ","shared_workspace":true}"#)
.unwrap();
let err = cfg.validate().unwrap_err().to_string();
assert!(err.contains("token"), "whitespace is not a token: {err}");
// Every agent-sandbox field, one at a time.
for blank in ["api", "namespace", "bearer", "image", "listen", "token"] {
let mut fields = std::collections::BTreeMap::from([
("api", "https://k8s:6443"),
("namespace", "builds"),
("bearer", "kube"),
("image", "img"),
("listen", "0.0.0.0:1"),
("token", "t"),
]);
fields.insert(blank, "");
let body: Vec<String> =
fields.iter().map(|(k, v)| format!("\"{k}\":\"{v}\"")).collect();
let cfg = load_sandbox(&format!("{{\"kind\":\"agent-sandbox\",{}}}", body.join(","))).unwrap();
let err = cfg.validate().unwrap_err().to_string();
assert!(err.contains(blank), "blank {blank} must be named: {err}");
}
}
/// The workspace gap: the build workspace is not shipped to a remote sandbox, so
/// "remote" without this assertion would silently build on the hub.
#[test]
fn a_remote_backend_must_assert_the_shared_workspace() {
let cfg = load_sandbox(r#"{"kind":"remote","listen":"127.0.0.1:1","token":"t"}"#).unwrap();
let err = cfg.validate().unwrap_err().to_string();
assert!(err.contains("shared_workspace"), "must name the missing assertion: {err}");
assert!(err.contains("workspace"), "must explain what is missing: {err}");
assert!(err.contains("remote"), "must say which kind: {err}");
let cfg = load_sandbox(
r#"{"kind":"agent-sandbox","api":"https://k8s:6443","namespace":"b","bearer":"k",
"image":"img","listen":"0.0.0.0:1","token":"t"}"#,
)
.unwrap();
let err = cfg.validate().unwrap_err().to_string();
assert!(err.contains("shared_workspace"), "{err}");
assert!(err.contains("agent-sandbox"), "{err}");
// Asserting it is enough — the guard refuses the silence, not the kind.
assert!(load_sandbox(
r#"{"kind":"remote","listen":"127.0.0.1:1","token":"t","shared_workspace":true}"#
)
.unwrap()
.validate()
.is_ok());
}
#[test]
fn the_local_backend_needs_no_assertion_at_all() {
// The whole point of keeping loopback the default: it shares the filesystem
// by construction, so there is nothing to promise.
assert!(load_sandbox(r#"{"kind":"loopback"}"#).unwrap().validate().is_ok());
assert!(load_raw("{}\n").unwrap().validate().is_ok());
}
/// The guard is wired to the process, not just available as a library call:
/// `aur-build serve` with a remote config that never asserted the shared
/// workspace must refuse rather than come up healthy and build on the hub.
#[test]
fn serve_refuses_a_remote_config_that_never_asserted_the_workspace() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("config.json"),
r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":"t"} }"#,
)
.unwrap();
let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
.args(["--root", &tmp.path().display().to_string(), "serve"])
.output()
.unwrap();
assert!(!out.status.success(), "serve must refuse, not start healthy");
let err = String::from_utf8_lossy(&out.stderr);
assert!(err.contains("shared_workspace"), "{err}");
}
/// …and the same for the CLI build path, which is where the build would
/// actually have run.
#[test]
fn the_cli_build_path_refuses_an_unusable_sandbox_config() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(
tmp.path().join("config.json"),
r#"{ "sandbox": {"kind":"remote","listen":"127.0.0.1:0","token":""} }"#,
)
.unwrap();
let out = Command::new(env!("CARGO_BIN_EXE_aur-build"))
.args(["--root", &tmp.path().display().to_string(), "packages"])
.output()
.unwrap();
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("token"));
}
|