|
1
|
+ |
//! The Computer subsystem (issue 79) and the API passthrough (issue 81).
|
|
2
|
+ |
//!
|
|
3
|
+ |
//! Two shapes of assertion are deliberately absent here. Nothing asserts
|
|
4
|
+ |
//! `x.is_empty() || !x.is_empty()`, and nothing asserts that a passthrough
|
|
5
|
+ |
//! response "is an object" — an error envelope is an object too, so that
|
|
6
|
+ |
//! assertion passed while every refused request returned a `{"status": N}`
|
|
7
|
+ |
//! stub. Each test below names the field the route actually returns, or the
|
|
8
|
+ |
//! refusal a closed policy actually produces.
|
|
9
|
+ |
|
|
10
|
+ |
use std::io::{Read, Write};
|
|
11
|
+ |
use std::net::TcpListener;
|
|
12
|
+ |
use std::path::PathBuf;
|
|
13
|
+ |
use std::sync::mpsc::{channel, Receiver};
|
|
14
|
+ |
use std::time::Duration;
|
|
15
|
+ |
|
|
16
|
+ |
use openagents_cli::api_passthrough::{
|
|
17
|
+ |
admitted_method, api_error_details, decode_request_body, parse_request_fields,
|
|
18
|
+ |
parse_request_headers, resolve_api_path, resolve_request_method, ApiPassthroughClient,
|
|
19
|
+ |
};
|
|
20
|
+ |
use openagents_cli::computer::{
|
|
21
|
+ |
curated_allowlist, decide, execute_command, format_allowlist, gh_read_only_allowed,
|
|
22
|
+ |
load_config, probe, probe_host, redact, serve, tier_allows, within_root, Cancellation,
|
|
23
|
+ |
CommandRequest, ComputerClient, ComputerPaths, Decision, ExecutionLimits, Journal,
|
|
24
|
+ |
MachineCredentials, PolicyConfig, RefusalReason, Tier,
|
|
25
|
+ |
};
|
|
26
|
+ |
|
|
27
|
+ |
// ---------------------------------------------------------------------------
|
|
28
|
+ |
// policy
|
|
29
|
+ |
// ---------------------------------------------------------------------------
|
|
30
|
+ |
|
|
31
|
+ |
fn config_at(directory: &std::path::Path, tier: Tier, roots: Vec<PathBuf>) -> PolicyConfig {
|
|
32
|
+ |
PolicyConfig {
|
|
33
|
+ |
tier,
|
|
34
|
+ |
roots,
|
|
35
|
+ |
..PolicyConfig::closed(ComputerPaths::in_directory(directory))
|
|
36
|
+ |
}
|
|
37
|
+ |
}
|
|
38
|
+ |
|
|
39
|
+ |
fn refusal(decision: &Decision) -> RefusalReason {
|
|
40
|
+ |
match decision {
|
|
41
|
+ |
Decision::Refused { reason, .. } => *reason,
|
|
42
|
+ |
Decision::Allowed { .. } => panic!("expected a refusal, the command was allowed"),
|
|
43
|
+ |
}
|
|
44
|
+ |
}
|
|
45
|
+ |
|
|
46
|
+ |
fn request(argv: &[&str], cwd: &std::path::Path) -> CommandRequest {
|
|
47
|
+ |
CommandRequest {
|
|
48
|
+ |
argv: argv.iter().map(|value| value.to_string()).collect(),
|
|
49
|
+ |
cwd: cwd.display().to_string(),
|
|
50
|
+ |
}
|
|
51
|
+ |
}
|
|
52
|
+ |
|
|
53
|
+ |
/// The policy this replaces was three unconditional `true`s, so it permitted
|
|
54
|
+ |
/// everything it was ever asked. The default now reaches nothing: no root is
|
|
55
|
+ |
/// declared, so no working directory is reachable, whatever the command is.
|
|
56
|
+ |
#[test]
|
|
57
|
+ |
fn test_default_policy_reaches_nothing() {
|
|
58
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
59
|
+ |
let config = PolicyConfig::closed(ComputerPaths::in_directory(directory.path()));
|
|
60
|
+ |
|
|
61
|
+ |
assert_eq!(config.tier, Tier::Probe);
|
|
62
|
+ |
assert!(config.roots.is_empty());
|
|
63
|
+ |
assert_eq!(
|
|
64
|
+ |
refusal(&decide(
|
|
65
|
+ |
&request(&["git", "status"], directory.path()),
|
|
66
|
+ |
&config
|
|
67
|
+ |
)),
|
|
68
|
+ |
RefusalReason::RootNotDeclared
|
|
69
|
+ |
);
|
|
70
|
+ |
assert_eq!(
|
|
71
|
+ |
refusal(&decide(&request(&["ls"], directory.path()), &config)),
|
|
72
|
+ |
RefusalReason::RootNotDeclared
|
|
73
|
+ |
);
|
|
74
|
+ |
}
|
|
75
|
+ |
|
|
76
|
+ |
/// The allowlist the policy command prints. The first and last lines, the
|
|
77
|
+ |
/// count, and `git`'s options are the contract the TypeScript CLI publishes.
|
|
78
|
+ |
#[test]
|
|
79
|
+ |
fn test_curated_allowlist_is_the_published_one() {
|
|
80
|
+ |
let lines = format_allowlist();
|
|
81
|
+ |
assert_eq!(lines.len(), curated_allowlist().len() + 1);
|
|
82
|
+ |
assert_eq!(
|
|
83
|
+ |
lines[0],
|
|
84
|
+ |
"git: status, log, diff, branch, remote, show, rev-parse, ls-files, --version"
|
|
85
|
+ |
);
|
|
86
|
+ |
assert_eq!(
|
|
87
|
+ |
lines[1],
|
|
88
|
+ |
"uname: no options; path arguments inside declared roots"
|
|
89
|
+ |
);
|
|
90
|
+ |
assert_eq!(lines[lines.len() - 1], "gh: read-only queries only");
|
|
91
|
+ |
assert!(lines.iter().any(|line| line == "npm: --version, ls"));
|
|
92
|
+ |
assert!(lines
|
|
93
|
+ |
.iter()
|
|
94
|
+ |
.any(|line| line == "docker: ps, images, version"));
|
|
95
|
+ |
}
|
|
96
|
+ |
|
|
97
|
+ |
/// A declared root is not enough on its own. The probe tier is fixed discovery,
|
|
98
|
+ |
/// so even `ls` inside the root is refused until the owner raises the ceiling.
|
|
99
|
+ |
#[test]
|
|
100
|
+ |
fn test_probe_tier_refuses_a_curated_command_inside_a_declared_root() {
|
|
101
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
102
|
+ |
let root = directory.path().to_path_buf();
|
|
103
|
+ |
let config = config_at(directory.path(), Tier::Probe, vec![root.clone()]);
|
|
104
|
+ |
assert_eq!(
|
|
105
|
+ |
refusal(&decide(&request(&["ls"], &root), &config)),
|
|
106
|
+ |
RefusalReason::TierInsufficient
|
|
107
|
+ |
);
|
|
108
|
+ |
assert!(tier_allows(Tier::Curated, Tier::Probe));
|
|
109
|
+ |
assert!(!tier_allows(Tier::Probe, Tier::Curated));
|
|
110
|
+ |
assert!(tier_allows(Tier::Shell, Tier::Curated));
|
|
111
|
+ |
}
|
|
112
|
+ |
|
|
113
|
+ |
/// The curated tier is where the allowlist decides. Everything here is a
|
|
114
|
+ |
/// refusal the flat-`true` policy could not have produced.
|
|
115
|
+ |
#[test]
|
|
116
|
+ |
fn test_curated_tier_allows_the_allowlist_and_refuses_the_rest() {
|
|
117
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
118
|
+ |
let root = directory.path().to_path_buf();
|
|
119
|
+ |
let config = config_at(directory.path(), Tier::Curated, vec![root.clone()]);
|
|
120
|
+ |
|
|
121
|
+ |
assert!(decide(&request(&["git", "status"], &root), &config).allowed());
|
|
122
|
+ |
assert!(decide(&request(&["git", "--version"], &root), &config).allowed());
|
|
123
|
+ |
assert!(decide(&request(&["node", "--version"], &root), &config).allowed());
|
|
124
|
+ |
|
|
125
|
+ |
// Not on the list at all.
|
|
126
|
+ |
assert_eq!(
|
|
127
|
+ |
refusal(&decide(
|
|
128
|
+ |
&request(&["curl", "https://example.com"], &root),
|
|
129
|
+ |
&config
|
|
130
|
+ |
)),
|
|
131
|
+ |
RefusalReason::NotAllowlisted
|
|
132
|
+ |
);
|
|
133
|
+ |
// On the list, but not with this subcommand: `git push` writes.
|
|
134
|
+ |
assert_eq!(
|
|
135
|
+ |
refusal(&decide(&request(&["git", "push"], &root), &config)),
|
|
136
|
+ |
RefusalReason::NotAllowlisted
|
|
137
|
+ |
);
|
|
138
|
+ |
// On the list, but not with this option.
|
|
139
|
+ |
assert_eq!(
|
|
140
|
+ |
refusal(&decide(&request(&["node", "-e", "1"], &root), &config)),
|
|
141
|
+ |
RefusalReason::NotAllowlisted
|
|
142
|
+ |
);
|
|
143
|
+ |
// Denied outright, and denied before the tier is consulted.
|
|
144
|
+ |
assert_eq!(
|
|
145
|
+ |
refusal(&decide(&request(&["sudo", "ls"], &root), &config)),
|
|
146
|
+ |
RefusalReason::DeniedCommand
|
|
147
|
+ |
);
|
|
148
|
+ |
// A protected path, named anywhere in the argument vector.
|
|
149
|
+ |
assert_eq!(
|
|
150
|
+ |
refusal(&decide(&request(&["cat", "~/.ssh/id_rsa"], &root), &config)),
|
|
151
|
+ |
RefusalReason::DeniedArgument
|
|
152
|
+ |
);
|
|
153
|
+ |
// Shell metacharacters never reach a shell, because there is no shell.
|
|
154
|
+ |
assert_eq!(
|
|
155
|
+ |
refusal(&decide(&request(&["ls", "; rm -rf /"], &root), &config)),
|
|
156
|
+ |
RefusalReason::ShellMetacharacter
|
|
157
|
+ |
);
|
|
158
|
+ |
// A path argument that climbs out of every declared root.
|
|
159
|
+ |
assert_eq!(
|
|
160
|
+ |
refusal(&decide(
|
|
161
|
+ |
&request(&["cat", "../../etc/hosts"], &root),
|
|
162
|
+ |
&config
|
|
163
|
+ |
)),
|
|
164
|
+ |
RefusalReason::DeniedArgument
|
|
165
|
+ |
);
|
|
166
|
+ |
// A working directory outside every declared root.
|
|
167
|
+ |
assert_eq!(
|
|
168
|
+ |
refusal(&decide(
|
|
169
|
+ |
&CommandRequest {
|
|
170
|
+ |
argv: vec!["git".to_string(), "status".to_string()],
|
|
171
|
+ |
cwd: "/tmp".to_string(),
|
|
172
|
+ |
},
|
|
173
|
+ |
&config
|
|
174
|
+ |
)),
|
|
175
|
+ |
RefusalReason::RootNotDeclared
|
|
176
|
+ |
);
|
|
177
|
+ |
}
|
|
178
|
+ |
|
|
179
|
+ |
/// A denied binary stays denied at the top tier. Raising the ceiling widens
|
|
180
|
+ |
/// what the allowlist admits; it never unlocks `sudo`.
|
|
181
|
+ |
#[test]
|
|
182
|
+ |
fn test_shell_tier_still_refuses_denied_commands_and_asks_before_the_rest() {
|
|
183
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
184
|
+ |
let root = directory.path().to_path_buf();
|
|
185
|
+ |
let mut config = config_at(directory.path(), Tier::Shell, vec![root.clone()]);
|
|
186
|
+ |
|
|
187
|
+ |
assert_eq!(
|
|
188
|
+ |
refusal(&decide(&request(&["sudo", "ls"], &root), &config)),
|
|
189
|
+ |
RefusalReason::DeniedCommand
|
|
190
|
+ |
);
|
|
191
|
+ |
assert_eq!(
|
|
192
|
+ |
decide(&request(&["make", "build"], &root), &config),
|
|
193
|
+ |
Decision::Allowed {
|
|
194
|
+ |
needs_confirmation: true
|
|
195
|
+ |
}
|
|
196
|
+ |
);
|
|
197
|
+ |
config.pre_approved = vec!["make".to_string()];
|
|
198
|
+ |
assert_eq!(
|
|
199
|
+ |
decide(&request(&["make", "build"], &root), &config),
|
|
200
|
+ |
Decision::Allowed {
|
|
201
|
+ |
needs_confirmation: false
|
|
202
|
+ |
}
|
|
203
|
+ |
);
|
|
204
|
+ |
}
|
|
205
|
+ |
|
|
206
|
+ |
#[test]
|
|
207
|
+ |
fn test_gh_is_read_only() {
|
|
208
|
+ |
let read = |args: &[&str]| {
|
|
209
|
+ |
gh_read_only_allowed(&args.iter().map(|v| v.to_string()).collect::<Vec<_>>())
|
|
210
|
+ |
};
|
|
211
|
+ |
assert!(read(&["pr", "list"]));
|
|
212
|
+ |
assert!(read(&["issue", "view"]));
|
|
213
|
+ |
assert!(read(&["status"]));
|
|
214
|
+ |
assert!(!read(&["pr", "merge"]));
|
|
215
|
+ |
assert!(!read(&["api", "/user"]));
|
|
216
|
+ |
assert!(!read(&["issue", "list", "--field", "x"]));
|
|
217
|
+ |
assert!(!read(&[]));
|
|
218
|
+ |
}
|
|
219
|
+ |
|
|
220
|
+ |
#[test]
|
|
221
|
+ |
fn test_root_containment_is_lexical_and_does_not_admit_a_sibling() {
|
|
222
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
223
|
+ |
let root = directory.path().join("work");
|
|
224
|
+ |
std::fs::create_dir_all(root.join("inner")).unwrap();
|
|
225
|
+ |
assert!(within_root(&root, &root));
|
|
226
|
+ |
assert!(within_root(&root.join("inner"), &root));
|
|
227
|
+ |
assert!(!within_root(&directory.path().join("work-other"), &root));
|
|
228
|
+ |
assert!(!within_root(&directory.path().join("elsewhere"), &root));
|
|
229
|
+ |
}
|
|
230
|
+ |
|
|
231
|
+ |
/// A configuration file that cannot be decoded is refused. Falling back to the
|
|
232
|
+ |
/// default would silently replace the owner's policy with a different one.
|
|
233
|
+ |
#[test]
|
|
234
|
+ |
fn test_unreadable_configuration_is_refused_rather_than_defaulted() {
|
|
235
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
236
|
+ |
let paths = ComputerPaths::in_directory(directory.path());
|
|
237
|
+ |
|
|
238
|
+ |
// Missing is a real answer: nothing is declared.
|
|
239
|
+ |
let closed = load_config(&paths).expect("a missing file is the closed default");
|
|
240
|
+ |
assert_eq!(closed.tier, Tier::Probe);
|
|
241
|
+ |
assert!(closed.roots.is_empty());
|
|
242
|
+ |
|
|
243
|
+ |
std::fs::write(&paths.config, "{ not json").unwrap();
|
|
244
|
+ |
let refused = load_config(&paths);
|
|
245
|
+ |
assert!(
|
|
246
|
+ |
refused.is_err(),
|
|
247
|
+ |
"invalid JSON must not read as the default"
|
|
248
|
+ |
);
|
|
249
|
+ |
|
|
250
|
+ |
std::fs::write(&paths.config, r#"{"tier":"root"}"#).unwrap();
|
|
251
|
+ |
let refused = load_config(&paths).unwrap_err();
|
|
252
|
+ |
assert!(
|
|
253
|
+ |
refused.contains("unknown tier"),
|
|
254
|
+ |
"the refusal must name the problem: {refused}"
|
|
255
|
+ |
);
|
|
256
|
+ |
|
|
257
|
+ |
std::fs::write(
|
|
258
|
+ |
&paths.config,
|
|
259
|
+ |
r#"{"tier":"curated","roots":["/tmp/declared"],"pre_approved":["make"]}"#,
|
|
260
|
+ |
)
|
|
261
|
+ |
.unwrap();
|
|
262
|
+ |
let read = load_config(&paths).unwrap();
|
|
263
|
+ |
assert_eq!(read.tier, Tier::Curated);
|
|
264
|
+ |
assert_eq!(read.roots, vec![PathBuf::from("/tmp/declared")]);
|
|
265
|
+ |
assert_eq!(read.pre_approved, vec!["make".to_string()]);
|
|
266
|
+ |
}
|
|
267
|
+ |
|
|
268
|
+ |
// ---------------------------------------------------------------------------
|
|
269
|
+ |
// journal
|
|
270
|
+ |
// ---------------------------------------------------------------------------
|
|
271
|
+ |
|
|
272
|
+ |
#[test]
|
|
273
|
+ |
fn test_journal_records_a_refusal_and_redacts_credentials() {
|
|
274
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
275
|
+ |
let journal = Journal::at(directory.path().join("journal.ndjson"));
|
|
276
|
+ |
|
|
277
|
+ |
assert!(
|
|
278
|
+ |
journal.read(20).unwrap().is_empty(),
|
|
279
|
+ |
"a machine that has been asked nothing has an empty journal"
|
|
280
|
+ |
);
|
|
281
|
+ |
|
|
282
|
+ |
let refused = CommandRequest {
|
|
283
|
+ |
argv: vec!["curl".to_string(), "https://example.com".to_string()],
|
|
284
|
+ |
cwd: "/declared/root".to_string(),
|
|
285
|
+ |
};
|
|
286
|
+ |
journal
|
|
287
|
+ |
.append(
|
|
288
|
+ |
"req-1",
|
|
289
|
+ |
&refused,
|
|
290
|
+ |
"not_allowlisted",
|
|
291
|
+ |
"refused",
|
|
292
|
+ |
"curl is not in the curated allowlist",
|
|
293
|
+ |
)
|
|
294
|
+ |
.unwrap();
|
|
295
|
+ |
journal
|
|
296
|
+ |
.append(
|
|
297
|
+ |
"req-2",
|
|
298
|
+ |
&CommandRequest {
|
|
299
|
+ |
argv: vec!["echo".to_string(), "oa_pat_ABCDEF123456".to_string()],
|
|
300
|
+ |
cwd: "/declared/root".to_string(),
|
|
301
|
+ |
},
|
|
302
|
+ |
"allowed",
|
|
303
|
+ |
"completed",
|
|
304
|
+ |
"Authorization: Bearer oa_pat_ABCDEF123456",
|
|
305
|
+ |
)
|
|
306
|
+ |
.unwrap();
|
|
307
|
+ |
|
|
308
|
+ |
let entries = journal.read(20).unwrap();
|
|
309
|
+ |
assert_eq!(entries.len(), 2);
|
|
310
|
+ |
assert_eq!(entries[0].request_id, "req-1");
|
|
311
|
+ |
assert_eq!(entries[0].decision, "not_allowlisted");
|
|
312
|
+ |
assert_eq!(entries[0].outcome, "refused");
|
|
313
|
+ |
assert_eq!(entries[0].argv, vec!["curl", "https://example.com"]);
|
|
314
|
+ |
assert_eq!(entries[0].detail, "curl is not in the curated allowlist");
|
|
315
|
+ |
|
|
316
|
+ |
let redacted = &entries[1];
|
|
317
|
+ |
assert!(
|
|
318
|
+ |
!redacted.detail.contains("oa_pat_ABCDEF123456"),
|
|
319
|
+ |
"the token survived the journal: {}",
|
|
320
|
+ |
redacted.detail
|
|
321
|
+ |
);
|
|
322
|
+ |
assert!(!redacted.argv[1].contains("ABCDEF123456"));
|
|
323
|
+ |
assert!(redacted.detail.contains("[REDACTED]"));
|
|
324
|
+ |
|
|
325
|
+ |
// Only the tail is returned, newest last.
|
|
326
|
+ |
let tail = journal.read(1).unwrap();
|
|
327
|
+ |
assert_eq!(tail.len(), 1);
|
|
328
|
+ |
assert_eq!(tail[0].request_id, "req-2");
|
|
329
|
+ |
|
|
330
|
+ |
// And the file stays private to the owner.
|
|
331
|
+ |
#[cfg(unix)]
|
|
332
|
+ |
{
|
|
333
|
+ |
use std::os::unix::fs::PermissionsExt;
|
|
334
|
+ |
let mode = std::fs::metadata(journal.path())
|
|
335
|
+ |
.unwrap()
|
|
336
|
+ |
.permissions()
|
|
337
|
+ |
.mode();
|
|
338
|
+ |
assert_eq!(mode & 0o777, 0o600);
|
|
339
|
+ |
}
|
|
340
|
+ |
}
|
|
341
|
+ |
|
|
342
|
+ |
#[test]
|
|
343
|
+ |
fn test_journal_read_of_an_unreadable_file_is_an_error_not_an_empty_list() {
|
|
344
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
345
|
+ |
// A directory where the journal file should be: it exists and cannot be
|
|
346
|
+ |
// read as a file, which must not read back as "nothing was asked".
|
|
347
|
+ |
let path = directory.path().join("journal.ndjson");
|
|
348
|
+ |
std::fs::create_dir(&path).unwrap();
|
|
349
|
+ |
let journal = Journal::at(path);
|
|
350
|
+ |
assert!(journal.read(20).is_err());
|
|
351
|
+ |
}
|
|
352
|
+ |
|
|
353
|
+ |
#[test]
|
|
354
|
+ |
fn test_redaction_removes_the_token_body_not_only_the_prefix() {
|
|
355
|
+ |
let redacted = redact("Bearer oa_pat_998877_TOKENBODY and smct_MACHINEBODY");
|
|
356
|
+ |
assert!(!redacted.contains("TOKENBODY"));
|
|
357
|
+ |
assert!(!redacted.contains("MACHINEBODY"));
|
|
358
|
+ |
assert!(!redacted.contains("998877"));
|
|
359
|
+ |
}
|
|
360
|
+ |
|
|
361
|
+ |
// ---------------------------------------------------------------------------
|
|
362
|
+ |
// executor
|
|
363
|
+ |
// ---------------------------------------------------------------------------
|
|
364
|
+ |
|
|
365
|
+ |
#[test]
|
|
366
|
+ |
fn test_executor_streams_bounded_scrubbed_output() {
|
|
367
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
368
|
+ |
let cancellation = Cancellation::default();
|
|
369
|
+ |
let mut seen = String::new();
|
|
370
|
+ |
let outcome = execute_command(
|
|
371
|
+ |
&[
|
|
372
|
+ |
"/bin/echo".to_string(),
|
|
373
|
+ |
"token oa_pat_LEAKEDSECRET here".to_string(),
|
|
374
|
+ |
],
|
|
375
|
+ |
&directory.path().display().to_string(),
|
|
376
|
+ |
ExecutionLimits::default(),
|
|
377
|
+ |
&cancellation,
|
|
378
|
+ |
|chunk| seen.push_str(chunk),
|
|
379
|
+ |
);
|
|
380
|
+ |
assert_eq!(outcome.exit_code, Some(0));
|
|
381
|
+ |
assert!(!outcome.timed_out);
|
|
382
|
+ |
assert!(seen.contains("token"));
|
|
383
|
+ |
assert!(
|
|
384
|
+ |
!seen.contains("LEAKEDSECRET"),
|
|
385
|
+ |
"a token printed by the command reached the caller: {seen}"
|
|
386
|
+ |
);
|
|
387
|
+ |
}
|
|
388
|
+ |
|
|
389
|
+ |
#[test]
|
|
390
|
+ |
fn test_executor_truncates_at_the_output_ceiling() {
|
|
391
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
392
|
+ |
let cancellation = Cancellation::default();
|
|
393
|
+ |
let mut bytes = 0usize;
|
|
394
|
+ |
let outcome = execute_command(
|
|
395
|
+ |
&[
|
|
396
|
+ |
"/usr/bin/head".to_string(),
|
|
397
|
+ |
"-c".to_string(),
|
|
398
|
+ |
"8192".to_string(),
|
|
399
|
+ |
"/dev/zero".to_string(),
|
|
400
|
+ |
],
|
|
401
|
+ |
&directory.path().display().to_string(),
|
|
402
|
+ |
ExecutionLimits {
|
|
403
|
+ |
timeout: Duration::from_secs(10),
|
|
404
|
+ |
maximum_output_bytes: 64,
|
|
405
|
+ |
},
|
|
406
|
+ |
&cancellation,
|
|
407
|
+ |
|chunk| bytes += chunk.len(),
|
|
408
|
+ |
);
|
|
409
|
+ |
assert!(bytes <= 64, "the output ceiling was crossed: {bytes} bytes");
|
|
410
|
+ |
assert!(outcome.truncated, "a truncated run must say so");
|
|
411
|
+ |
}
|
|
412
|
+ |
|
|
413
|
+ |
#[test]
|
|
414
|
+ |
fn test_executor_stops_a_command_that_outlives_its_timeout() {
|
|
415
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
416
|
+ |
let cancellation = Cancellation::default();
|
|
417
|
+ |
let outcome = execute_command(
|
|
418
|
+ |
&["/bin/sleep".to_string(), "30".to_string()],
|
|
419
|
+ |
&directory.path().display().to_string(),
|
|
420
|
+ |
ExecutionLimits {
|
|
421
|
+ |
timeout: Duration::from_millis(300),
|
|
422
|
+ |
maximum_output_bytes: 1024,
|
|
423
|
+ |
},
|
|
424
|
+ |
&cancellation,
|
|
425
|
+ |
|_| {},
|
|
426
|
+ |
);
|
|
427
|
+ |
assert!(outcome.timed_out, "the command outlived its timeout");
|
|
428
|
+ |
assert!(outcome.duration_ms < 10_000);
|
|
429
|
+ |
}
|
|
430
|
+ |
|
|
431
|
+ |
#[test]
|
|
432
|
+ |
fn test_executor_reports_a_missing_binary_rather_than_succeeding() {
|
|
433
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
434
|
+ |
let outcome = execute_command(
|
|
435
|
+ |
&["/nonexistent/binary-that-is-not-here".to_string()],
|
|
436
|
+ |
&directory.path().display().to_string(),
|
|
437
|
+ |
ExecutionLimits::default(),
|
|
438
|
+ |
&Cancellation::default(),
|
|
439
|
+ |
|_| {},
|
|
440
|
+ |
);
|
|
441
|
+ |
assert_eq!(outcome.exit_code, Some(127));
|
|
442
|
+ |
}
|
|
443
|
+ |
|
|
444
|
+ |
// ---------------------------------------------------------------------------
|
|
445
|
+ |
// probe
|
|
446
|
+ |
// ---------------------------------------------------------------------------
|
|
447
|
+ |
|
|
448
|
+ |
#[test]
|
|
449
|
+ |
fn test_probe_reports_this_host_and_the_roots_it_was_given() {
|
|
450
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
451
|
+ |
std::fs::create_dir_all(directory.path().join("checkout")).unwrap();
|
|
452
|
+ |
let roots = vec![directory.path().join("checkout")];
|
|
453
|
+ |
let report = probe(&roots);
|
|
454
|
+ |
|
|
455
|
+ |
assert_eq!(report.schema, "openagents.computer_probe.v1");
|
|
456
|
+ |
assert!(report.host.cpu_count > 0);
|
|
457
|
+ |
assert!(report.host.total_memory_bytes > 0);
|
|
458
|
+ |
assert!(
|
|
459
|
+ |
!report.host.hostname.is_empty(),
|
|
460
|
+ |
"the probe names this host"
|
|
461
|
+ |
);
|
|
462
|
+ |
assert!(
|
|
463
|
+ |
!report.host.release.is_empty(),
|
|
464
|
+ |
"the probe reads the kernel release"
|
|
465
|
+ |
);
|
|
466
|
+ |
assert_eq!(report.coding_agents.len(), 11);
|
|
467
|
+ |
assert_eq!(report.toolchains.len(), 14);
|
|
468
|
+ |
assert_eq!(report.roots.len(), 1);
|
|
469
|
+ |
assert_eq!(report.worktrees.len(), 1);
|
|
470
|
+ |
assert!(report.worktrees[0].exists);
|
|
471
|
+ |
assert!(!report.worktrees[0].git);
|
|
472
|
+ |
|
|
473
|
+ |
// `git` is on this machine, and the probe reports its real path and version
|
|
474
|
+ |
// rather than only a boolean.
|
|
475
|
+ |
let git = report
|
|
476
|
+ |
.toolchains
|
|
477
|
+ |
.iter()
|
|
478
|
+ |
.find(|tool| tool.name == "git")
|
|
479
|
+ |
.unwrap();
|
|
480
|
+ |
assert!(git.present);
|
|
481
|
+ |
assert!(
|
|
482
|
+ |
git.path.contains("git"),
|
|
483
|
+ |
"the probe resolves a path: {}",
|
|
484
|
+ |
git.path
|
|
485
|
+ |
);
|
|
486
|
+ |
assert!(
|
|
487
|
+ |
git.version.starts_with("git version"),
|
|
488
|
+ |
"the probe reads a version: {}",
|
|
489
|
+ |
git.version
|
|
490
|
+ |
);
|
|
491
|
+ |
|
|
492
|
+ |
// The narrower host summary agrees with the full report.
|
|
493
|
+ |
assert_eq!(probe_host().num_cpus, report.host.cpu_count);
|
|
494
|
+ |
}
|
|
495
|
+ |
|
|
496
|
+ |
// ---------------------------------------------------------------------------
|
|
497
|
+ |
// machine credential
|
|
498
|
+ |
// ---------------------------------------------------------------------------
|
|
499
|
+ |
|
|
500
|
+ |
#[test]
|
|
501
|
+ |
fn test_machine_credentials_round_trip_and_stay_separate_per_origin() {
|
|
502
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
503
|
+ |
let production = MachineCredentials::isolated("https://openagents.com", directory.path());
|
|
504
|
+ |
let staging = MachineCredentials::isolated("https://staging.openagents.com", directory.path());
|
|
505
|
+ |
|
|
506
|
+ |
assert!(production.get().unwrap().is_none());
|
|
507
|
+ |
production
|
|
508
|
+ |
.set(&openagents_cli::auth::Secret::new("smct_machine_token"))
|
|
509
|
+ |
.unwrap();
|
|
510
|
+ |
assert_eq!(
|
|
511
|
+ |
production.get().unwrap().unwrap().expose(),
|
|
512
|
+ |
"smct_machine_token"
|
|
513
|
+ |
);
|
|
514
|
+ |
assert!(
|
|
515
|
+ |
staging.get().unwrap().is_none(),
|
|
516
|
+ |
"a machine paired with production is not offered to staging"
|
|
517
|
+ |
);
|
|
518
|
+ |
|
|
519
|
+ |
assert!(production.remove().unwrap());
|
|
520
|
+ |
assert!(production.get().unwrap().is_none());
|
|
521
|
+ |
assert!(!production.remove().unwrap());
|
|
522
|
+ |
}
|
|
523
|
+ |
|
|
524
|
+ |
// ---------------------------------------------------------------------------
|
|
525
|
+ |
// the controller client
|
|
526
|
+ |
// ---------------------------------------------------------------------------
|
|
527
|
+ |
|
|
528
|
+ |
/// A status read that the server refuses is an error. It is never reported as
|
|
529
|
+ |
/// "not paired", which would send the owner to `computer pair` for a problem
|
|
530
|
+ |
/// that has nothing to do with pairing.
|
|
531
|
+ |
#[tokio::test]
|
|
532
|
+ |
async fn test_computer_status_refuses_rather_than_reporting_unpaired() {
|
|
533
|
+ |
let client = ComputerClient::new("https://openagents.com/no-such-surface");
|
|
534
|
+ |
let result = client
|
|
535
|
+ |
.status(&openagents_cli::auth::Secret::new("smct_not_a_real_token"))
|
|
536
|
+ |
.await;
|
|
537
|
+ |
let refusal = result
|
|
538
|
+ |
.expect_err("a 404 is not an unpaired machine")
|
|
539
|
+ |
.to_string();
|
|
540
|
+ |
assert!(
|
|
541
|
+ |
refusal.contains("could not read this Computer status"),
|
|
542
|
+ |
"the refusal must name what failed: {refusal}"
|
|
543
|
+ |
);
|
|
544
|
+ |
}
|
|
545
|
+ |
|
|
546
|
+ |
/// The live controller surface answers a machine token it does not know with a
|
|
547
|
+ |
/// 401, and only that is reported as "no longer active".
|
|
548
|
+ |
#[tokio::test]
|
|
549
|
+ |
async fn test_live_controller_reports_an_unknown_machine_token_as_inactive() {
|
|
550
|
+ |
let client = ComputerClient::new("https://openagents.com");
|
|
551
|
+ |
let status = client
|
|
552
|
+ |
.status(&openagents_cli::auth::Secret::new("smct_not_a_real_token"))
|
|
553
|
+ |
.await
|
|
554
|
+ |
.expect("the live controller answers");
|
|
555
|
+ |
assert!(
|
|
556
|
+ |
status.is_none(),
|
|
557
|
+ |
"an unknown machine token is not an active machine"
|
|
558
|
+ |
);
|
|
559
|
+ |
}
|
|
560
|
+ |
|
|
561
|
+ |
// ---------------------------------------------------------------------------
|
|
562
|
+ |
// the outbound channel
|
|
563
|
+ |
// ---------------------------------------------------------------------------
|
|
564
|
+ |
|
|
565
|
+ |
/// A Phoenix-shaped controller socket, so the client's join, hello, framing,
|
|
566
|
+ |
/// policy, journal, and exit reporting all run against a live peer.
|
|
567
|
+ |
struct StubController {
|
|
568
|
+ |
origin: String,
|
|
569
|
+ |
frames: Receiver<serde_json::Value>,
|
|
570
|
+ |
}
|
|
571
|
+ |
|
|
572
|
+ |
fn start_stub_controller(machine_id: &str, run_payload: serde_json::Value) -> StubController {
|
|
573
|
+ |
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
574
|
+ |
let port = listener.local_addr().unwrap().port();
|
|
575
|
+ |
let (sender, frames) = channel();
|
|
576
|
+ |
let topic = format!("computer:{machine_id}");
|
|
577
|
+ |
|
|
578
|
+ |
std::thread::spawn(move || {
|
|
579
|
+ |
let Ok((stream, _)) = listener.accept() else {
|
|
580
|
+ |
return;
|
|
581
|
+ |
};
|
|
582
|
+ |
let Ok(mut socket) = tungstenite::accept(stream) else {
|
|
583
|
+ |
return;
|
|
584
|
+ |
};
|
|
585
|
+ |
// phx_join, then the reply the client waits for before it sends hello.
|
|
586
|
+ |
let _ = socket.read();
|
|
587
|
+ |
let reply =
|
|
588
|
+ |
serde_json::json!(["1", "1", topic, "phx_reply", {"status": "ok", "response": {}}]);
|
|
589
|
+ |
let _ = socket.send(tungstenite::Message::Text(reply.to_string().into()));
|
|
590
|
+ |
// hello
|
|
591
|
+ |
let _ = socket.read();
|
|
592
|
+ |
let ask = serde_json::json!([serde_json::Value::Null, "9", topic, "run", run_payload]);
|
|
593
|
+ |
let _ = socket.send(tungstenite::Message::Text(ask.to_string().into()));
|
|
594
|
+ |
|
|
595
|
+ |
let deadline = std::time::Instant::now() + Duration::from_secs(20);
|
|
596
|
+ |
while std::time::Instant::now() < deadline {
|
|
597
|
+ |
match socket.read() {
|
|
598
|
+ |
Ok(tungstenite::Message::Text(text)) => {
|
|
599
|
+ |
if let Ok(value) = serde_json::from_str::<serde_json::Value>(&text) {
|
|
600
|
+ |
let terminal = value
|
|
601
|
+ |
.get(3)
|
|
602
|
+ |
.and_then(|event| event.as_str())
|
|
603
|
+ |
.map(|event| event == "refused" || event == "exit")
|
|
604
|
+ |
.unwrap_or(false);
|
|
605
|
+ |
if sender.send(value).is_err() {
|
|
606
|
+ |
return;
|
|
607
|
+ |
}
|
|
608
|
+ |
if terminal {
|
|
609
|
+ |
break;
|
|
610
|
+ |
}
|
|
611
|
+ |
}
|
|
612
|
+ |
}
|
|
613
|
+ |
Ok(_) => {}
|
|
614
|
+ |
Err(_) => break,
|
|
615
|
+ |
}
|
|
616
|
+ |
}
|
|
617
|
+ |
let _ = socket.close(None);
|
|
618
|
+ |
// Drain the close handshake so the client sees a clean end.
|
|
619
|
+ |
while socket.read().is_ok() {}
|
|
620
|
+ |
});
|
|
621
|
+ |
|
|
622
|
+ |
StubController {
|
|
623
|
+ |
origin: format!("http://127.0.0.1:{port}"),
|
|
624
|
+ |
frames,
|
|
625
|
+ |
}
|
|
626
|
+ |
}
|
|
627
|
+ |
|
|
628
|
+ |
fn next_frame(frames: &Receiver<serde_json::Value>, event: &str) -> serde_json::Value {
|
|
629
|
+ |
let deadline = std::time::Instant::now() + Duration::from_secs(25);
|
|
630
|
+ |
while std::time::Instant::now() < deadline {
|
|
631
|
+ |
match frames.recv_timeout(Duration::from_secs(25)) {
|
|
632
|
+ |
Ok(frame) => {
|
|
633
|
+ |
if frame.get(3).and_then(|value| value.as_str()) == Some(event) {
|
|
634
|
+ |
return frame.get(4).cloned().unwrap_or(serde_json::Value::Null);
|
|
635
|
+ |
}
|
|
636
|
+ |
}
|
|
637
|
+ |
Err(_) => break,
|
|
638
|
+ |
}
|
|
639
|
+ |
}
|
|
640
|
+ |
panic!("the client never sent a {event} frame");
|
|
641
|
+ |
}
|
|
642
|
+ |
|
|
643
|
+ |
/// A command the allowlist does not carry is refused over the wire, and the
|
|
644
|
+ |
/// refusal lands in the local journal with the reason that produced it. The
|
|
645
|
+ |
/// journal never leaves this machine, so this is the only place the owner can
|
|
646
|
+ |
/// read what was asked of it.
|
|
647
|
+ |
#[test]
|
|
648
|
+ |
fn test_up_refuses_a_command_outside_the_allowlist_and_journals_it() {
|
|
649
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
650
|
+ |
let root = directory.path().join("checkout");
|
|
651
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
652
|
+ |
let config = config_at(directory.path(), Tier::Curated, vec![root.clone()]);
|
|
653
|
+ |
let journal = Journal::at(directory.path().join("journal.ndjson"));
|
|
654
|
+ |
|
|
655
|
+ |
let stub = start_stub_controller(
|
|
656
|
+ |
"machine-1",
|
|
657
|
+ |
serde_json::json!({
|
|
658
|
+ |
"request_id": "req-refused",
|
|
659
|
+ |
"argv": ["curl", "https://example.com"],
|
|
660
|
+ |
"cwd": root.display().to_string(),
|
|
661
|
+ |
}),
|
|
662
|
+ |
);
|
|
663
|
+ |
|
|
664
|
+ |
let reason = serve(
|
|
665
|
+ |
&stub.origin,
|
|
666
|
+ |
&openagents_cli::auth::Secret::new("smct_stub"),
|
|
667
|
+ |
"machine-1",
|
|
668
|
+ |
&serde_json::json!({"agent_version": "test"}),
|
|
669
|
+ |
&config,
|
|
670
|
+ |
&journal,
|
|
671
|
+ |
|_| {},
|
|
672
|
+ |
);
|
|
673
|
+ |
|
|
674
|
+ |
let refused = next_frame(&stub.frames, "refused");
|
|
675
|
+ |
assert_eq!(
|
|
676
|
+ |
refused.get("reason").and_then(|v| v.as_str()),
|
|
677
|
+ |
Some("not_allowlisted"),
|
|
678
|
+ |
"the server was told why: {refused}"
|
|
679
|
+ |
);
|
|
680
|
+ |
assert_eq!(
|
|
681
|
+ |
refused.get("request_id").and_then(|v| v.as_str()),
|
|
682
|
+ |
Some("req-refused")
|
|
683
|
+ |
);
|
|
684
|
+ |
assert!(refused
|
|
685
|
+ |
.get("detail")
|
|
686
|
+ |
.and_then(|v| v.as_str())
|
|
687
|
+ |
.unwrap_or_default()
|
|
688
|
+ |
.contains("curated allowlist"));
|
|
689
|
+ |
// The stub serves one connection and then stops listening, so the client
|
|
690
|
+ |
// sees the peer close, retries within its bound, and reports that it ran
|
|
691
|
+ |
// out of retries rather than claiming a clean shutdown.
|
|
692
|
+ |
assert!(
|
|
693
|
+ |
reason.starts_with("transport_retry_exhausted:"),
|
|
694
|
+ |
"the ending names what happened: {reason}"
|
|
695
|
+ |
);
|
|
696
|
+ |
|
|
697
|
+ |
let entries = journal.read(50).unwrap();
|
|
698
|
+ |
let recorded = entries
|
|
699
|
+ |
.iter()
|
|
700
|
+ |
.find(|entry| entry.request_id == "req-refused" && entry.outcome == "refused")
|
|
701
|
+ |
.expect("the refusal is in the local journal");
|
|
702
|
+ |
assert_eq!(recorded.decision, "not_allowlisted");
|
|
703
|
+ |
assert_eq!(recorded.argv, vec!["curl", "https://example.com"]);
|
|
704
|
+ |
assert_eq!(recorded.cwd, root.display().to_string());
|
|
705
|
+ |
eprintln!(
|
|
706
|
+ |
"journal entry: {}",
|
|
707
|
+ |
serde_json::to_string(recorded).unwrap()
|
|
708
|
+ |
);
|
|
709
|
+ |
}
|
|
710
|
+ |
|
|
711
|
+ |
/// An allowed command runs, streams its real output, and reports a real exit.
|
|
712
|
+ |
#[test]
|
|
713
|
+ |
fn test_up_serves_a_bounded_allowed_request() {
|
|
714
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
715
|
+ |
let root = directory.path().join("checkout");
|
|
716
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
717
|
+ |
let config = config_at(directory.path(), Tier::Curated, vec![root.clone()]);
|
|
718
|
+ |
let journal = Journal::at(directory.path().join("journal.ndjson"));
|
|
719
|
+ |
|
|
720
|
+ |
let stub = start_stub_controller(
|
|
721
|
+ |
"machine-2",
|
|
722
|
+ |
serde_json::json!({
|
|
723
|
+ |
"request_id": "req-allowed",
|
|
724
|
+ |
"argv": ["git", "--version"],
|
|
725
|
+ |
"cwd": root.display().to_string(),
|
|
726
|
+ |
}),
|
|
727
|
+ |
);
|
|
728
|
+ |
|
|
729
|
+ |
serve(
|
|
730
|
+ |
&stub.origin,
|
|
731
|
+ |
&openagents_cli::auth::Secret::new("smct_stub"),
|
|
732
|
+ |
"machine-2",
|
|
733
|
+ |
&serde_json::json!({"agent_version": "test"}),
|
|
734
|
+ |
&config,
|
|
735
|
+ |
&journal,
|
|
736
|
+ |
|_| {},
|
|
737
|
+ |
);
|
|
738
|
+ |
|
|
739
|
+ |
let mut chunks = String::new();
|
|
740
|
+ |
let deadline = std::time::Instant::now() + Duration::from_secs(25);
|
|
741
|
+ |
let exit = loop {
|
|
742
|
+ |
assert!(
|
|
743
|
+ |
std::time::Instant::now() < deadline,
|
|
744
|
+ |
"no exit frame arrived"
|
|
745
|
+ |
);
|
|
746
|
+ |
let frame = stub
|
|
747
|
+ |
.frames
|
|
748
|
+ |
.recv_timeout(Duration::from_secs(25))
|
|
749
|
+ |
.expect("the client sends chunk and exit frames");
|
|
750
|
+ |
match frame.get(3).and_then(|value| value.as_str()) {
|
|
751
|
+ |
Some("chunk") => chunks.push_str(
|
|
752
|
+ |
frame
|
|
753
|
+ |
.get(4)
|
|
754
|
+ |
.and_then(|payload| payload.get("text"))
|
|
755
|
+ |
.and_then(|text| text.as_str())
|
|
756
|
+ |
.unwrap_or_default(),
|
|
757
|
+ |
),
|
|
758
|
+ |
Some("exit") => break frame.get(4).cloned().unwrap(),
|
|
759
|
+ |
Some("refused") => panic!("git --version was refused: {frame}"),
|
|
760
|
+ |
_ => {}
|
|
761
|
+ |
}
|
|
762
|
+ |
};
|
|
763
|
+ |
|
|
764
|
+ |
assert!(
|
|
765
|
+ |
chunks.contains("git version"),
|
|
766
|
+ |
"the command's own output reached the server: {chunks:?}"
|
|
767
|
+ |
);
|
|
768
|
+ |
assert_eq!(
|
|
769
|
+ |
exit.get("status").and_then(|v| v.as_str()),
|
|
770
|
+ |
Some("completed")
|
|
771
|
+ |
);
|
|
772
|
+ |
assert_eq!(exit.get("exit_code").and_then(|v| v.as_i64()), Some(0));
|
|
773
|
+ |
assert_eq!(exit.get("timed_out").and_then(|v| v.as_bool()), Some(false));
|
|
774
|
+ |
|
|
775
|
+ |
let entries = journal.read(50).unwrap();
|
|
776
|
+ |
assert!(entries
|
|
777
|
+ |
.iter()
|
|
778
|
+ |
.any(|entry| entry.request_id == "req-allowed" && entry.outcome == "completed"));
|
|
779
|
+ |
}
|
|
780
|
+ |
|
|
781
|
+ |
/// Transport loss retries with bounded backoff and then stops. It does not
|
|
782
|
+ |
/// reconnect forever, and it says the retries were exhausted rather than
|
|
783
|
+ |
/// reporting a clean close.
|
|
784
|
+ |
#[test]
|
|
785
|
+ |
fn test_up_retries_transport_loss_with_bounded_backoff() {
|
|
786
|
+ |
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
787
|
+ |
let port = listener.local_addr().unwrap().port();
|
|
788
|
+ |
let (counted, attempts) = channel();
|
|
789
|
+ |
std::thread::spawn(move || {
|
|
790
|
+ |
for stream in listener.incoming() {
|
|
791
|
+ |
let Ok(stream) = stream else { return };
|
|
792
|
+ |
// Accept the connection and drop it before the handshake completes.
|
|
793
|
+ |
drop(stream);
|
|
794
|
+ |
if counted.send(()).is_err() {
|
|
795
|
+ |
return;
|
|
796
|
+ |
}
|
|
797
|
+ |
}
|
|
798
|
+ |
});
|
|
799
|
+ |
|
|
800
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
801
|
+ |
let config = PolicyConfig::closed(ComputerPaths::in_directory(directory.path()));
|
|
802
|
+ |
let journal = Journal::at(directory.path().join("journal.ndjson"));
|
|
803
|
+ |
let mut events = Vec::new();
|
|
804
|
+ |
|
|
805
|
+ |
let started = std::time::Instant::now();
|
|
806
|
+ |
let reason = serve(
|
|
807
|
+ |
&format!("http://127.0.0.1:{port}"),
|
|
808
|
+ |
&openagents_cli::auth::Secret::new("smct_stub"),
|
|
809
|
+ |
"machine-3",
|
|
810
|
+ |
&serde_json::json!({}),
|
|
811
|
+ |
&config,
|
|
812
|
+ |
&journal,
|
|
813
|
+ |
|event| events.push(event.to_string()),
|
|
814
|
+ |
);
|
|
815
|
+ |
let elapsed = started.elapsed();
|
|
816
|
+ |
|
|
817
|
+ |
assert!(
|
|
818
|
+ |
reason.starts_with("transport_retry_exhausted:"),
|
|
819
|
+ |
"a bounded retry ends by saying so: {reason}"
|
|
820
|
+ |
);
|
|
821
|
+ |
assert_eq!(
|
|
822
|
+ |
events.len(),
|
|
823
|
+ |
3,
|
|
824
|
+ |
"three bounded reconnects, not an unbounded loop: {events:?}"
|
|
825
|
+ |
);
|
|
826
|
+ |
assert!(events[0].starts_with("reconnect:"));
|
|
827
|
+ |
|
|
828
|
+ |
let mut connections = 0;
|
|
829
|
+ |
while attempts.try_recv().is_ok() {
|
|
830
|
+ |
connections += 1;
|
|
831
|
+ |
}
|
|
832
|
+ |
assert_eq!(connections, 4, "one attempt plus three retries");
|
|
833
|
+ |
|
|
834
|
+ |
// 250ms, then 500ms, then 1s. The backoff grows and is bounded.
|
|
835
|
+ |
assert!(
|
|
836
|
+ |
elapsed >= Duration::from_millis(1_700),
|
|
837
|
+ |
"the retries did not back off: {elapsed:?}"
|
|
838
|
+ |
);
|
|
839
|
+ |
assert!(elapsed < Duration::from_secs(30));
|
|
840
|
+ |
|
|
841
|
+ |
assert!(
|
|
842
|
+ |
journal
|
|
843
|
+ |
.read(50)
|
|
844
|
+ |
.unwrap()
|
|
845
|
+ |
.iter()
|
|
846
|
+ |
.filter(|entry| entry.decision == "transport")
|
|
847
|
+ |
.count()
|
|
848
|
+ |
>= 4,
|
|
849
|
+ |
"every transport ending is recorded locally"
|
|
850
|
+ |
);
|
|
851
|
+ |
}
|
|
852
|
+ |
|
|
853
|
+ |
// ---------------------------------------------------------------------------
|
|
854
|
+ |
// the whole command, through the binary
|
|
855
|
+ |
// ---------------------------------------------------------------------------
|
|
856
|
+ |
|
|
857
|
+ |
/// A stand-in for the controller: the `/controller/status` route and the
|
|
858
|
+ |
/// Phoenix socket on one origin, so `oa computer status` and `oa computer up`
|
|
859
|
+ |
/// run end to end against a peer.
|
|
860
|
+ |
///
|
|
861
|
+ |
/// The socket is served once. A second upgrade is answered `403`, which is a
|
|
862
|
+ |
/// decision rather than transport loss, so the client stops instead of
|
|
863
|
+ |
/// reconnecting into a loop.
|
|
864
|
+ |
fn start_controller_host(
|
|
865
|
+ |
machine_id: &'static str,
|
|
866
|
+ |
run_payload: serde_json::Value,
|
|
867
|
+ |
) -> (String, Receiver<serde_json::Value>) {
|
|
868
|
+ |
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
869
|
+ |
let port = listener.local_addr().unwrap().port();
|
|
870
|
+ |
let (sender, frames) = channel();
|
|
871
|
+ |
let topic = format!("computer:{machine_id}");
|
|
872
|
+ |
let served = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
|
873
|
+ |
|
|
874
|
+ |
std::thread::spawn(move || {
|
|
875
|
+ |
for stream in listener.incoming() {
|
|
876
|
+ |
let Ok(mut stream) = stream else { return };
|
|
877
|
+ |
let mut peeked = [0u8; 2048];
|
|
878
|
+ |
let count = stream.peek(&mut peeked).unwrap_or(0);
|
|
879
|
+ |
let head = String::from_utf8_lossy(&peeked[..count]).to_ascii_lowercase();
|
|
880
|
+ |
|
|
881
|
+ |
if !head.contains("upgrade: websocket") {
|
|
882
|
+ |
let mut buffer = [0u8; 4096];
|
|
883
|
+ |
let _ = stream.read(&mut buffer);
|
|
884
|
+ |
let payload = serde_json::json!({
|
|
885
|
+ |
"machine_id": machine_id,
|
|
886
|
+ |
"name": "stub-machine",
|
|
887
|
+ |
"status": "active",
|
|
888
|
+ |
"token_expires_at": "2099-01-01T00:00:00Z",
|
|
889
|
+ |
})
|
|
890
|
+ |
.to_string();
|
|
891
|
+ |
let response = format!(
|
|
892
|
+ |
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{payload}",
|
|
893
|
+ |
payload.len()
|
|
894
|
+ |
);
|
|
895
|
+ |
let _ = stream.write_all(response.as_bytes());
|
|
896
|
+ |
let _ = stream.flush();
|
|
897
|
+ |
continue;
|
|
898
|
+ |
}
|
|
899
|
+ |
|
|
900
|
+ |
if served.swap(true, std::sync::atomic::Ordering::SeqCst) {
|
|
901
|
+ |
let _ = stream.write_all(
|
|
902
|
+ |
b"HTTP/1.1 403 Forbidden\r\ncontent-length: 0\r\nconnection: close\r\n\r\n",
|
|
903
|
+ |
);
|
|
904
|
+ |
let _ = stream.flush();
|
|
905
|
+ |
continue;
|
|
906
|
+ |
}
|
|
907
|
+ |
|
|
908
|
+ |
let Ok(mut socket) = tungstenite::accept(stream) else {
|
|
909
|
+ |
continue;
|
|
910
|
+ |
};
|
|
911
|
+ |
let _ = socket.read();
|
|
912
|
+ |
let reply =
|
|
913
|
+ |
serde_json::json!(["1", "1", topic, "phx_reply", {"status": "ok", "response": {}}]);
|
|
914
|
+ |
let _ = socket.send(tungstenite::Message::Text(reply.to_string().into()));
|
|
915
|
+ |
let _ = socket.read();
|
|
916
|
+ |
let ask = serde_json::json!([serde_json::Value::Null, "9", topic, "run", run_payload]);
|
|
917
|
+ |
let _ = socket.send(tungstenite::Message::Text(ask.to_string().into()));
|
|
918
|
+ |
|
|
919
|
+ |
let deadline = std::time::Instant::now() + Duration::from_secs(20);
|
|
920
|
+ |
while std::time::Instant::now() < deadline {
|
|
921
|
+ |
match socket.read() {
|
|
922
|
+ |
Ok(tungstenite::Message::Text(text)) => {
|
|
923
|
+ |
if let Ok(value) = serde_json::from_str::<serde_json::Value>(&text) {
|
|
924
|
+ |
let event = value
|
|
925
|
+ |
.get(3)
|
|
926
|
+ |
.and_then(|event| event.as_str())
|
|
927
|
+ |
.unwrap_or_default()
|
|
928
|
+ |
.to_string();
|
|
929
|
+ |
let _ = sender.send(value);
|
|
930
|
+ |
if event == "refused" || event == "exit" {
|
|
931
|
+ |
break;
|
|
932
|
+ |
}
|
|
933
|
+ |
}
|
|
934
|
+ |
}
|
|
935
|
+ |
Ok(_) => {}
|
|
936
|
+ |
Err(_) => break,
|
|
937
|
+ |
}
|
|
938
|
+ |
}
|
|
939
|
+ |
let _ = socket.close(None);
|
|
940
|
+ |
}
|
|
941
|
+ |
});
|
|
942
|
+ |
|
|
943
|
+ |
(format!("http://127.0.0.1:{port}"), frames)
|
|
944
|
+ |
}
|
|
945
|
+ |
|
|
946
|
+ |
/// Lay out a private `HOME` holding a Computer policy and a machine token, so
|
|
947
|
+ |
/// the binary reads real files without touching the developer's own.
|
|
948
|
+ |
fn stub_home(directory: &std::path::Path, origin: &str, tier: &str, root: &std::path::Path) {
|
|
949
|
+ |
let config_directory = directory.join(".config").join("openagents");
|
|
950
|
+ |
std::fs::create_dir_all(&config_directory).unwrap();
|
|
951
|
+ |
std::fs::write(
|
|
952
|
+ |
config_directory.join("computer.json"),
|
|
953
|
+ |
serde_json::json!({
|
|
954
|
+ |
"tier": tier,
|
|
955
|
+ |
"roots": [root.display().to_string()],
|
|
956
|
+ |
"pre_approved": [],
|
|
957
|
+ |
})
|
|
958
|
+ |
.to_string(),
|
|
959
|
+ |
)
|
|
960
|
+ |
.unwrap();
|
|
961
|
+ |
std::fs::write(
|
|
962
|
+ |
config_directory.join("cli-credentials.json"),
|
|
963
|
+ |
serde_json::json!({
|
|
964
|
+ |
"version": 1,
|
|
965
|
+ |
"tokens": { format!("computer:{origin}"): "smct_stub_machine_token" },
|
|
966
|
+ |
})
|
|
967
|
+ |
.to_string(),
|
|
968
|
+ |
)
|
|
969
|
+ |
.unwrap();
|
|
970
|
+ |
}
|
|
971
|
+ |
|
|
972
|
+ |
#[test]
|
|
973
|
+ |
fn test_the_binary_reports_real_pairing_state_without_printing_a_secret() {
|
|
974
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
975
|
+ |
let root = directory.path().join("checkout");
|
|
976
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
977
|
+ |
let (origin, _frames) = start_controller_host("machine-status", serde_json::json!({}));
|
|
978
|
+ |
stub_home(directory.path(), &origin, "curated", &root);
|
|
979
|
+ |
|
|
980
|
+ |
let output = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
981
|
+ |
.args(["--api-url", &origin, "--json", "computer", "status"])
|
|
982
|
+ |
.env("HOME", directory.path())
|
|
983
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
984
|
+ |
.output()
|
|
985
|
+ |
.unwrap();
|
|
986
|
+ |
assert!(
|
|
987
|
+ |
output.status.success(),
|
|
988
|
+ |
"computer status failed: {output:?}"
|
|
989
|
+ |
);
|
|
990
|
+ |
|
|
991
|
+ |
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
992
|
+ |
let value: serde_json::Value = serde_json::from_str(&stdout).unwrap();
|
|
993
|
+ |
assert_eq!(value["state"], serde_json::json!("paired"));
|
|
994
|
+ |
assert_eq!(value["paired"], serde_json::json!(true));
|
|
995
|
+ |
assert_eq!(value["machine_id"], serde_json::json!("machine-status"));
|
|
996
|
+ |
assert_eq!(value["tier"], serde_json::json!("curated"));
|
|
997
|
+ |
assert_eq!(
|
|
998
|
+ |
value["paths"]["journal"],
|
|
999
|
+ |
serde_json::json!(directory
|
|
1000
|
+ |
.path()
|
|
1001
|
+ |
.join(".config/openagents/journal.ndjson")
|
|
1002
|
+ |
.display()
|
|
1003
|
+ |
.to_string())
|
|
1004
|
+ |
);
|
|
1005
|
+ |
assert!(
|
|
1006
|
+ |
!stdout.contains("smct_"),
|
|
1007
|
+ |
"the machine token reached the output: {stdout}"
|
|
1008
|
+ |
);
|
|
1009
|
+ |
assert!(!String::from_utf8_lossy(&output.stderr).contains("smct_"));
|
|
1010
|
+ |
}
|
|
1011
|
+ |
|
|
1012
|
+ |
/// `oa computer up` end to end: it opens the outbound connection, serves the
|
|
1013
|
+ |
/// bounded request the peer asks for, and writes what it decided to the local
|
|
1014
|
+ |
/// journal.
|
|
1015
|
+ |
#[test]
|
|
1016
|
+ |
fn test_the_binary_serves_a_bounded_request_over_an_outbound_connection() {
|
|
1017
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
1018
|
+ |
let root = directory.path().join("checkout");
|
|
1019
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
1020
|
+ |
let (origin, frames) = start_controller_host(
|
|
1021
|
+ |
"machine-up",
|
|
1022
|
+ |
serde_json::json!({
|
|
1023
|
+ |
"request_id": "req-binary",
|
|
1024
|
+ |
"argv": ["git", "--version"],
|
|
1025
|
+ |
"cwd": root.display().to_string(),
|
|
1026
|
+ |
}),
|
|
1027
|
+ |
);
|
|
1028
|
+ |
stub_home(directory.path(), &origin, "curated", &root);
|
|
1029
|
+ |
|
|
1030
|
+ |
let output = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1031
|
+ |
.args(["--api-url", &origin, "computer", "up"])
|
|
1032
|
+ |
.env("HOME", directory.path())
|
|
1033
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
1034
|
+ |
.output()
|
|
1035
|
+ |
.unwrap();
|
|
1036
|
+ |
|
|
1037
|
+ |
let mut chunks = String::new();
|
|
1038
|
+ |
let mut exit = None;
|
|
1039
|
+ |
while let Ok(frame) = frames.recv_timeout(Duration::from_secs(5)) {
|
|
1040
|
+ |
match frame.get(3).and_then(|value| value.as_str()) {
|
|
1041
|
+ |
Some("chunk") => chunks.push_str(
|
|
1042
|
+ |
frame[4]
|
|
1043
|
+ |
.get("text")
|
|
1044
|
+ |
.and_then(|text| text.as_str())
|
|
1045
|
+ |
.unwrap_or_default(),
|
|
1046
|
+ |
),
|
|
1047
|
+ |
Some("exit") => {
|
|
1048
|
+ |
exit = Some(frame[4].clone());
|
|
1049
|
+ |
break;
|
|
1050
|
+ |
}
|
|
1051
|
+ |
Some("refused") => panic!("the binary refused an allowed command: {frame}"),
|
|
1052
|
+ |
_ => {}
|
|
1053
|
+ |
}
|
|
1054
|
+ |
}
|
|
1055
|
+ |
let exit = exit.expect("the binary reported a terminal exit to the peer");
|
|
1056
|
+ |
assert!(
|
|
1057
|
+ |
chunks.contains("git version"),
|
|
1058
|
+ |
"output reached the peer: {chunks:?}"
|
|
1059
|
+ |
);
|
|
1060
|
+ |
assert_eq!(exit["status"], serde_json::json!("completed"));
|
|
1061
|
+ |
assert_eq!(exit["exit_code"], serde_json::json!(0));
|
|
1062
|
+ |
|
|
1063
|
+ |
// The second upgrade is refused, so the command stops rather than
|
|
1064
|
+ |
// reconnecting forever, and it says so on stderr with a non-zero status.
|
|
1065
|
+ |
assert_eq!(output.status.code(), Some(2));
|
|
1066
|
+ |
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
1067
|
+ |
assert!(
|
|
1068
|
+ |
stderr.contains("oa: the Computer connection stopped"),
|
|
1069
|
+ |
"the ending is reported on stderr: {stderr}"
|
|
1070
|
+ |
);
|
|
1071
|
+ |
|
|
1072
|
+ |
let journal = Journal::at(directory.path().join(".config/openagents/journal.ndjson"));
|
|
1073
|
+ |
let entries = journal.read(50).unwrap();
|
|
1074
|
+ |
assert!(entries
|
|
1075
|
+ |
.iter()
|
|
1076
|
+ |
.any(|entry| entry.request_id == "req-binary" && entry.outcome == "completed"));
|
|
1077
|
+ |
}
|
|
1078
|
+ |
|
|
1079
|
+ |
/// The refusal path through the binary, and the journal entry it leaves.
|
|
1080
|
+ |
#[test]
|
|
1081
|
+ |
fn test_the_binary_refuses_a_command_outside_the_allowlist() {
|
|
1082
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
1083
|
+ |
let root = directory.path().join("checkout");
|
|
1084
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
1085
|
+ |
let (origin, frames) = start_controller_host(
|
|
1086
|
+ |
"machine-refuse",
|
|
1087
|
+ |
serde_json::json!({
|
|
1088
|
+ |
"request_id": "req-binary-refused",
|
|
1089
|
+ |
"argv": ["curl", "https://example.com"],
|
|
1090
|
+ |
"cwd": root.display().to_string(),
|
|
1091
|
+ |
}),
|
|
1092
|
+ |
);
|
|
1093
|
+ |
stub_home(directory.path(), &origin, "curated", &root);
|
|
1094
|
+ |
|
|
1095
|
+ |
let _ = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1096
|
+ |
.args(["--api-url", &origin, "computer", "up"])
|
|
1097
|
+ |
.env("HOME", directory.path())
|
|
1098
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
1099
|
+ |
.output()
|
|
1100
|
+ |
.unwrap();
|
|
1101
|
+ |
|
|
1102
|
+ |
let refused = next_frame(&frames, "refused");
|
|
1103
|
+ |
assert_eq!(refused["reason"], serde_json::json!("not_allowlisted"));
|
|
1104
|
+ |
assert_eq!(
|
|
1105
|
+ |
refused["request_id"],
|
|
1106
|
+ |
serde_json::json!("req-binary-refused")
|
|
1107
|
+ |
);
|
|
1108
|
+ |
|
|
1109
|
+ |
let journal = Journal::at(directory.path().join(".config/openagents/journal.ndjson"));
|
|
1110
|
+ |
let recorded = journal
|
|
1111
|
+ |
.read(50)
|
|
1112
|
+ |
.unwrap()
|
|
1113
|
+ |
.into_iter()
|
|
1114
|
+ |
.find(|entry| entry.request_id == "req-binary-refused" && entry.outcome == "refused")
|
|
1115
|
+ |
.expect("the refusal is journaled locally");
|
|
1116
|
+ |
assert_eq!(recorded.decision, "not_allowlisted");
|
|
1117
|
+ |
|
|
1118
|
+ |
// And `oa computer journal` reads it back.
|
|
1119
|
+ |
let listed = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1120
|
+ |
.args(["computer", "journal"])
|
|
1121
|
+ |
.env("HOME", directory.path())
|
|
1122
|
+ |
.output()
|
|
1123
|
+ |
.unwrap();
|
|
1124
|
+ |
let text = String::from_utf8_lossy(&listed.stdout).to_string();
|
|
1125
|
+ |
assert!(
|
|
1126
|
+ |
text.contains("not_allowlisted/refused") && text.contains("curl https://example.com"),
|
|
1127
|
+ |
"the journal command shows the refusal: {text}"
|
|
1128
|
+ |
);
|
|
1129
|
+ |
}
|
|
1130
|
+ |
|
|
1131
|
+ |
/// Without a machine token there is nothing to serve, and the command says so
|
|
1132
|
+ |
/// rather than pretending to run a daemon. The version this replaces printed
|
|
1133
|
+ |
/// `Computer agent daemon launched.` and exited zero.
|
|
1134
|
+ |
#[test]
|
|
1135
|
+ |
fn test_the_binary_refuses_to_serve_when_it_is_not_paired() {
|
|
1136
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
1137
|
+ |
std::fs::create_dir_all(directory.path().join(".config/openagents")).unwrap();
|
|
1138
|
+ |
let output = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1139
|
+ |
.args(["--api-url", "http://127.0.0.1:1", "computer", "up"])
|
|
1140
|
+ |
.env("HOME", directory.path())
|
|
1141
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
1142
|
+ |
.output()
|
|
1143
|
+ |
.unwrap();
|
|
1144
|
+ |
assert_eq!(output.status.code(), Some(2));
|
|
1145
|
+ |
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
1146
|
+ |
assert!(
|
|
1147
|
+ |
stderr.contains("not paired") && stderr.contains("computer pair"),
|
|
1148
|
+ |
"the refusal names the next step: {stderr}"
|
|
1149
|
+ |
);
|
|
1150
|
+ |
}
|
|
1151
|
+ |
|
|
1152
|
+ |
/// `logout` removes the machine token, and `status` then reports the local
|
|
1153
|
+ |
/// state rather than a stale pairing.
|
|
1154
|
+ |
#[test]
|
|
1155
|
+ |
fn test_the_binary_logout_removes_the_machine_token() {
|
|
1156
|
+ |
let directory = tempfile::tempdir().unwrap();
|
|
1157
|
+ |
let root = directory.path().join("checkout");
|
|
1158
|
+ |
std::fs::create_dir_all(&root).unwrap();
|
|
1159
|
+ |
let (origin, _frames) = start_controller_host("machine-logout", serde_json::json!({}));
|
|
1160
|
+ |
stub_home(directory.path(), &origin, "curated", &root);
|
|
1161
|
+ |
|
|
1162
|
+ |
let removed = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1163
|
+ |
.args(["--api-url", &origin, "--json", "computer", "logout"])
|
|
1164
|
+ |
.env("HOME", directory.path())
|
|
1165
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
1166
|
+ |
.output()
|
|
1167
|
+ |
.unwrap();
|
|
1168
|
+ |
assert!(removed.status.success());
|
|
1169
|
+ |
let value: serde_json::Value =
|
|
1170
|
+ |
serde_json::from_str(&String::from_utf8_lossy(&removed.stdout)).unwrap();
|
|
1171
|
+ |
assert_eq!(value["removed"], serde_json::json!(true));
|
|
1172
|
+ |
|
|
1173
|
+ |
let after = std::process::Command::new(env!("CARGO_BIN_EXE_oa"))
|
|
1174
|
+ |
.args(["--api-url", &origin, "--json", "computer", "status"])
|
|
1175
|
+ |
.env("HOME", directory.path())
|
|
1176
|
+ |
.env_remove("OPENAGENTS_TOKEN")
|
|
1177
|
+ |
.output()
|
|
1178
|
+ |
.unwrap();
|
|
1179
|
+ |
let value: serde_json::Value =
|
|
1180
|
+ |
serde_json::from_str(&String::from_utf8_lossy(&after.stdout)).unwrap();
|
|
1181
|
+ |
assert_eq!(value["state"], serde_json::json!("local"));
|
|
1182
|
+ |
assert_eq!(value["paired"], serde_json::json!(false));
|
|
1183
|
+ |
}
|
|
1184
|
+ |
|
|
1185
|
+ |
// ---------------------------------------------------------------------------
|
|
1186
|
+ |
// api passthrough: path resolution
|
|
1187
|
+ |
// ---------------------------------------------------------------------------
|
|
1188
|
+ |
|
|
1189
|
+ |
const ORIGIN: &str = "https://openagents.com";
|
|
1190
|
+ |
|
|
1191
|
+ |
/// The bug this issue was reopened for. Both spellings of the same route now
|
|
1192
|
+ |
/// resolve to it; the version this replaces turned `/api/v1/user` into
|
|
1193
|
+ |
/// `/api/v1/api/v1/user` and 404ed on the one form its help text advertised.
|
|
1194
|
+ |
#[test]
|
|
1195
|
+ |
fn test_absolute_and_relative_paths_name_the_same_route() {
|
|
1196
|
+ |
assert_eq!(
|
|
1197
|
+ |
resolve_api_path(ORIGIN, "/api/v1/user").unwrap(),
|
|
1198
|
+ |
"/api/v1/user"
|
|
1199
|
+ |
);
|
|
1200
|
+ |
assert_eq!(resolve_api_path(ORIGIN, "user").unwrap(), "/api/v1/user");
|
|
1201
|
+ |
assert_eq!(
|
|
1202
|
+ |
resolve_api_path(ORIGIN, "repos/OpenAgentsInc/openagents/issues").unwrap(),
|
|
1203
|
+ |
"/api/v1/repos/OpenAgentsInc/openagents/issues"
|
|
1204
|
+ |
);
|
|
1205
|
+ |
assert_eq!(
|
|
1206
|
+ |
resolve_api_path(ORIGIN, "/api/v1/repos/OpenAgentsInc/openagents/issues").unwrap(),
|
|
1207
|
+ |
"/api/v1/repos/OpenAgentsInc/openagents/issues"
|
|
1208
|
+ |
);
|
|
1209
|
+ |
// A query survives resolution.
|
|
1210
|
+ |
assert_eq!(
|
|
1211
|
+ |
resolve_api_path(ORIGIN, "issues?state=closed").unwrap(),
|
|
1212
|
+ |
"/api/v1/issues?state=closed"
|
|
1213
|
+ |
);
|
|
1214
|
+ |
// A complete URL on the configured origin is accepted.
|
|
1215
|
+ |
assert_eq!(
|
|
1216
|
+ |
resolve_api_path(ORIGIN, "https://openagents.com/api/v1/user").unwrap(),
|
|
1217
|
+ |
"/api/v1/user"
|
|
1218
|
+ |
);
|
|
1219
|
+ |
}
|
|
1220
|
+ |
|
|
1221
|
+ |
#[test]
|
|
1222
|
+ |
fn test_a_path_that_leaves_the_api_is_refused() {
|
|
1223
|
+ |
// The website, not the API.
|
|
1224
|
+ |
let refusal = resolve_api_path(ORIGIN, "/user").unwrap_err();
|
|
1225
|
+ |
assert!(
|
|
1226
|
+ |
refusal.contains("must start with /api/"),
|
|
1227
|
+ |
"the refusal must say how to write it: {refusal}"
|
|
1228
|
+ |
);
|
|
1229
|
+ |
// Another origin entirely.
|
|
1230
|
+ |
assert!(resolve_api_path(ORIGIN, "https://example.com/api/v1/user")
|
|
1231
|
+ |
.unwrap_err()
|
|
1232
|
+ |
.contains("leaves the configured API origin"));
|
|
1233
|
+ |
// A protocol-relative path is another origin in disguise.
|
|
1234
|
+ |
assert!(resolve_api_path(ORIGIN, "//example.com/api/v1/user").is_err());
|
|
1235
|
+ |
// A relative path that climbs out of the API prefix.
|
|
1236
|
+ |
assert!(resolve_api_path(ORIGIN, "../../admin")
|
|
1237
|
+ |
.unwrap_err()
|
|
1238
|
+ |
.contains("resolves outside"));
|
|
1239
|
+ |
assert!(resolve_api_path(ORIGIN, " ").is_err());
|
|
1240
|
+ |
assert!(resolve_api_path(ORIGIN, "ftp://openagents.com/api/v1/user").is_err());
|
|
1241
|
+ |
}
|
|
1242
|
+ |
|
|
1243
|
+ |
#[test]
|
|
1244
|
+ |
fn test_an_unknown_method_is_refused_rather_than_performed_as_a_get() {
|
|
1245
|
+ |
assert_eq!(admitted_method("post").unwrap(), "POST");
|
|
1246
|
+ |
assert_eq!(admitted_method(" delete ").unwrap(), "DELETE");
|
|
1247
|
+ |
let refusal = admitted_method("POSTT").unwrap_err();
|
|
1248
|
+ |
assert!(
|
|
1249
|
+ |
refusal.contains("not a supported method"),
|
|
1250
|
+ |
"the refusal must name the problem: {refusal}"
|
|
1251
|
+ |
);
|
|
1252
|
+ |
assert!(admitted_method("HEAD").is_err());
|
|
1253
|
+ |
assert!(admitted_method("OPTIONS").is_err());
|
|
1254
|
+ |
assert_eq!(resolve_request_method(None, false), "GET");
|
|
1255
|
+ |
assert_eq!(resolve_request_method(None, true), "POST");
|
|
1256
|
+ |
assert_eq!(resolve_request_method(Some("PATCH"), true), "PATCH");
|
|
1257
|
+ |
}
|
|
1258
|
+ |
|
|
1259
|
+ |
#[test]
|
|
1260
|
+ |
fn test_request_fields_and_headers_are_parsed_not_guessed() {
|
|
1261
|
+ |
let fields =
|
|
1262
|
+ |
parse_request_fields(&["title=Hello".to_string(), "body=World".to_string()]).unwrap();
|
|
1263
|
+ |
assert_eq!(fields["title"], serde_json::json!("Hello"));
|
|
1264
|
+ |
assert_eq!(fields["body"], serde_json::json!("World"));
|
|
1265
|
+ |
// Every value is a JSON string; `--input` carries anything else.
|
|
1266
|
+ |
let typed = parse_request_fields(&["count=3".to_string()]).unwrap();
|
|
1267
|
+ |
assert_eq!(typed["count"], serde_json::json!("3"));
|
|
1268
|
+ |
|
|
1269
|
+ |
assert!(parse_request_fields(&["nope".to_string()]).is_err());
|
|
1270
|
+ |
assert!(parse_request_fields(&["=value".to_string()]).is_err());
|
|
1271
|
+ |
assert!(
|
|
1272
|
+ |
parse_request_fields(&["a=1".to_string(), "a=2".to_string()])
|
|
1273
|
+ |
.unwrap_err()
|
|
1274
|
+ |
.contains("more than once")
|
|
1275
|
+ |
);
|
|
1276
|
+ |
|
|
1277
|
+ |
let headers = parse_request_headers(&["X-Trace: abc ".to_string()]).unwrap();
|
|
1278
|
+ |
assert_eq!(headers, vec![("x-trace".to_string(), "abc".to_string())]);
|
|
1279
|
+ |
// The session owns the authorization header.
|
|
1280
|
+ |
assert!(
|
|
1281
|
+ |
parse_request_headers(&["Authorization: Bearer x".to_string()])
|
|
1282
|
+ |
.unwrap_err()
|
|
1283
|
+ |
.contains("Remove --header authorization")
|
|
1284
|
+ |
);
|
|
1285
|
+ |
assert!(parse_request_headers(&["no colon".to_string()]).is_err());
|
|
1286
|
+ |
assert!(parse_request_headers(&["bad name: x".to_string()]).is_err());
|
|
1287
|
+ |
|
|
1288
|
+ |
assert!(decode_request_body(" ", "standard input").is_err());
|
|
1289
|
+ |
assert!(decode_request_body("not json", "standard input").is_err());
|
|
1290
|
+ |
assert_eq!(
|
|
1291
|
+ |
decode_request_body(r#"{"a":[1,2]}"#, "standard input").unwrap(),
|
|
1292
|
+ |
serde_json::json!({"a": [1, 2]})
|
|
1293
|
+ |
);
|
|
1294
|
+ |
}
|
|
1295
|
+ |
|
|
1296
|
+ |
#[test]
|
|
1297
|
+ |
fn test_error_details_read_the_servers_own_envelope() {
|
|
1298
|
+ |
let body = serde_json::json!({
|
|
1299
|
+ |
"code": "not_found",
|
|
1300
|
+ |
"message": "Repository not found",
|
|
1301
|
+ |
"request_id": "GM9-abc",
|
|
1302
|
+ |
});
|
|
1303
|
+ |
let details = api_error_details(Some(&body));
|
|
1304
|
+ |
assert_eq!(details.message.as_deref(), Some("Repository not found"));
|
|
1305
|
+ |
assert_eq!(details.code.as_deref(), Some("not_found"));
|
|
1306
|
+ |
assert_eq!(details.request_id.as_deref(), Some("GM9-abc"));
|
|
1307
|
+ |
|
|
1308
|
+ |
// A body with no envelope yields nothing rather than an invented message.
|
|
1309
|
+ |
assert_eq!(
|
|
1310
|
+ |
api_error_details(Some(&serde_json::json!({"ok": true}))).message,
|
|
1311
|
+ |
None
|
|
1312
|
+ |
);
|
|
1313
|
+ |
assert_eq!(api_error_details(None).message, None);
|
|
1314
|
+ |
}
|
|
1315
|
+ |
|
|
1316
|
+ |
// ---------------------------------------------------------------------------
|
|
1317
|
+ |
// api passthrough: transport
|
|
1318
|
+ |
// ---------------------------------------------------------------------------
|
|
1319
|
+ |
|
|
1320
|
+ |
/// A stub API that answers with the request line and every header it received,
|
|
1321
|
+ |
/// so header injection is asserted against a peer rather than against the
|
|
1322
|
+ |
/// client's own intent.
|
|
1323
|
+ |
fn start_echo_api() -> String {
|
|
1324
|
+ |
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
1325
|
+ |
let port = listener.local_addr().unwrap().port();
|
|
1326
|
+ |
std::thread::spawn(move || {
|
|
1327
|
+ |
for stream in listener.incoming() {
|
|
1328
|
+ |
let Ok(mut stream) = stream else { return };
|
|
1329
|
+ |
let mut buffer = [0u8; 8192];
|
|
1330
|
+ |
let Ok(count) = stream.read(&mut buffer) else {
|
|
1331
|
+ |
continue;
|
|
1332
|
+ |
};
|
|
1333
|
+ |
let request = String::from_utf8_lossy(&buffer[..count]).to_string();
|
|
1334
|
+ |
let mut lines = request.split("\r\n");
|
|
1335
|
+ |
let start = lines.next().unwrap_or_default().to_string();
|
|
1336
|
+ |
let mut headers = serde_json::Map::new();
|
|
1337
|
+ |
for line in lines {
|
|
1338
|
+ |
if line.is_empty() {
|
|
1339
|
+ |
break;
|
|
1340
|
+ |
}
|
|
1341
|
+ |
if let Some((name, value)) = line.split_once(':') {
|
|
1342
|
+ |
headers.insert(
|
|
1343
|
+ |
name.trim().to_ascii_lowercase(),
|
|
1344
|
+ |
serde_json::Value::String(value.trim().to_string()),
|
|
1345
|
+ |
);
|
|
1346
|
+ |
}
|
|
1347
|
+ |
}
|
|
1348
|
+ |
let body = request
|
|
1349
|
+ |
.split_once("\r\n\r\n")
|
|
1350
|
+ |
.map(|(_, body)| body.to_string())
|
|
1351
|
+ |
.unwrap_or_default();
|
|
1352
|
+ |
let payload = serde_json::json!({
|
|
1353
|
+ |
"request_line": start,
|
|
1354
|
+ |
"headers": serde_json::Value::Object(headers),
|
|
1355
|
+ |
"body": body,
|
|
1356
|
+ |
})
|
|
1357
|
+ |
.to_string();
|
|
1358
|
+ |
let response = format!(
|
|
1359
|
+ |
"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{payload}",
|
|
1360
|
+ |
payload.len()
|
|
1361
|
+ |
);
|
|
1362
|
+ |
let _ = stream.write_all(response.as_bytes());
|
|
1363
|
+ |
let _ = stream.flush();
|
|
1364
|
+ |
}
|
|
1365
|
+ |
});
|
|
1366
|
+ |
format!("http://127.0.0.1:{port}")
|
|
1367
|
+ |
}
|
|
1368
|
+ |
|
|
1369
|
+ |
#[tokio::test]
|
|
1370
|
+ |
async fn test_headers_and_body_reach_the_server() {
|
|
1371
|
+ |
let origin = start_echo_api();
|
|
1372
|
+ |
let client = ApiPassthroughClient::new(&origin, Some("oa_pat_test".to_string()));
|
|
1373
|
+ |
let response = client
|
|
1374
|
+ |
.send(
|
|
1375
|
+ |
"POST",
|
|
1376
|
+ |
"memories",
|
|
1377
|
+ |
&[("x-trace".to_string(), "header-proof".to_string())],
|
|
1378
|
+ |
Some(&serde_json::json!({"body": "hello"})),
|
|
1379
|
+ |
)
|
|
1380
|
+ |
.await
|
|
1381
|
+ |
.unwrap();
|
|
1382
|
+ |
|
|
1383
|
+ |
assert_eq!(response.status, 200);
|
|
1384
|
+ |
let echoed = response.body.expect("the stub answers with JSON");
|
|
1385
|
+ |
assert_eq!(
|
|
1386
|
+ |
echoed["request_line"].as_str().unwrap(),
|
|
1387
|
+ |
"POST /api/v1/memories HTTP/1.1",
|
|
1388
|
+ |
"the method and the resolved path both reached the server"
|
|
1389
|
+ |
);
|
|
1390
|
+ |
assert_eq!(
|
|
1391
|
+ |
echoed["headers"]["x-trace"],
|
|
1392
|
+ |
serde_json::json!("header-proof")
|
|
1393
|
+ |
);
|
|
1394
|
+ |
assert_eq!(
|
|
1395
|
+ |
echoed["headers"]["authorization"],
|
|
1396
|
+ |
serde_json::json!("Bearer oa_pat_test"),
|
|
1397
|
+ |
"authentication is forwarded from the session"
|
|
1398
|
+ |
);
|
|
1399
|
+ |
assert_eq!(
|
|
1400
|
+ |
echoed["body"].as_str().unwrap(),
|
|
1401
|
+ |
r#"{"body":"hello"}"#,
|
|
1402
|
+ |
"the JSON body reached the server"
|
|
1403
|
+ |
);
|
|
1404
|
+ |
}
|
|
1405
|
+ |
|
|
1406
|
+ |
/// The replacement for `assert!(res.is_object())`. That assertion held while
|
|
1407
|
+ |
/// every refused request returned `{"status": N}` — an object — so it could
|
|
1408
|
+ |
/// not tell a route from an error. This names the field the route returns.
|
|
1409
|
+ |
#[tokio::test]
|
|
1410
|
+ |
async fn test_api_passthrough_returns_the_repository_the_route_names() {
|
|
1411
|
+ |
let client = ApiPassthroughClient::new(ORIGIN, None);
|
|
1412
|
+ |
let value = client
|
|
1413
|
+ |
.execute_request("GET", "repos/OpenAgentsInc/openagents", None)
|
|
1414
|
+ |
.await
|
|
1415
|
+ |
.expect("the public repository route answers");
|
|
1416
|
+ |
assert_eq!(
|
|
1417
|
+ |
value.get("full_name").and_then(|v| v.as_str()),
|
|
1418
|
+ |
Some("OpenAgentsInc/openagents")
|
|
1419
|
+ |
);
|
|
1420
|
+ |
assert!(!value
|
|
1421
|
+ |
.get("default_branch")
|
|
1422
|
+ |
.and_then(|v| v.as_str())
|
|
1423
|
+ |
.unwrap_or_default()
|
|
1424
|
+ |
.is_empty());
|
|
1425
|
+ |
assert_eq!(
|
|
1426
|
+ |
value.get("visibility").and_then(|v| v.as_str()),
|
|
1427
|
+ |
Some("public")
|
|
1428
|
+ |
);
|
|
1429
|
+ |
}
|
|
1430
|
+ |
|
|
1431
|
+ |
/// A route the server does not serve is an error carrying the server's own
|
|
1432
|
+ |
/// message, not a value. The `{"status": 404}` stub this replaces satisfied
|
|
1433
|
+ |
/// every assertion a caller could write about the success path.
|
|
1434
|
+ |
#[tokio::test]
|
|
1435
|
+ |
async fn test_api_passthrough_refuses_a_route_the_server_does_not_serve() {
|
|
1436
|
+ |
let client = ApiPassthroughClient::new(ORIGIN, None);
|
|
1437
|
+ |
let refused = client
|
|
1438
|
+ |
.execute_request("GET", "repos/OpenAgentsInc/no-such-repository-here", None)
|
|
1439
|
+ |
.await;
|
|
1440
|
+ |
let message = refused
|
|
1441
|
+ |
.expect_err("a 404 must not read as a value")
|
|
1442
|
+ |
.to_string();
|
|
1443
|
+ |
assert!(
|
|
1444
|
+ |
message.contains("404"),
|
|
1445
|
+ |
"the refusal names the status: {message}"
|
|
1446
|
+ |
);
|
|
1447
|
+ |
assert!(
|
|
1448
|
+ |
message.contains("Repository not found"),
|
|
1449
|
+ |
"the refusal carries the server's own message: {message}"
|
|
1450
|
+ |
);
|
|
1451
|
+ |
|
|
1452
|
+ |
// And the envelope keeps the server's body so the command can print it.
|
|
1453
|
+ |
let response = client
|
|
1454
|
+ |
.send(
|
|
1455
|
+ |
"GET",
|
|
1456
|
+ |
"repos/OpenAgentsInc/no-such-repository-here",
|
|
1457
|
+ |
&[],
|
|
1458
|
+ |
None,
|
|
1459
|
+ |
)
|
|
1460
|
+ |
.await
|
|
1461
|
+ |
.unwrap();
|
|
1462
|
+ |
assert_eq!(response.status, 404);
|
|
1463
|
+ |
assert!(!response.successful());
|
|
1464
|
+ |
assert_eq!(
|
|
1465
|
+ |
response
|
|
1466
|
+ |
.body
|
|
1467
|
+ |
.as_ref()
|
|
1468
|
+ |
.and_then(|body| body.get("code"))
|
|
1469
|
+ |
.and_then(|code| code.as_str()),
|
|
1470
|
+ |
Some("not_found")
|
|
1471
|
+ |
);
|
|
1472
|
+ |
}
|
|
1473
|
+ |
|
|
1474
|
+ |
/// A host that answers nothing is a transport failure, and it stays one. It
|
|
1475
|
+ |
/// does not become an empty body or a plausible status.
|
|
1476
|
+ |
#[tokio::test]
|
|
1477
|
+ |
async fn test_api_passthrough_reports_transport_failure() {
|
|
1478
|
+ |
// A port nothing is listening on.
|
|
1479
|
+ |
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
1480
|
+ |
let port = listener.local_addr().unwrap().port();
|
|
1481
|
+ |
drop(listener);
|
|
1482
|
+ |
|
|
1483
|
+ |
let client = ApiPassthroughClient::new(&format!("http://127.0.0.1:{port}"), None);
|
|
1484
|
+ |
let refusal = client
|
|
1485
|
+ |
.send("GET", "user", &[], None)
|
|
1486
|
+ |
.await
|
|
1487
|
+ |
.expect_err("nothing is listening");
|
|
1488
|
+ |
assert!(
|
|
1489
|
+ |
refusal.contains("could not reach"),
|
|
1490
|
+ |
"the refusal names the transport: {refusal}"
|
|
1491
|
+ |
);
|
|
1492
|
+ |
}
|
|
1493
|
+ |
|
|
1494
|
+ |
/// The client keeps the origin whichever way it was given, so an absolute path
|
|
1495
|
+ |
/// is not prefixed twice.
|
|
1496
|
+ |
#[test]
|
|
1497
|
+ |
fn test_client_reduces_a_legacy_api_base_to_its_origin() {
|
|
1498
|
+ |
assert_eq!(
|
|
1499
|
+ |
ApiPassthroughClient::new("https://openagents.com/api/v1", None).origin,
|
|
1500
|
+ |
"https://openagents.com"
|
|
1501
|
+ |
);
|
|
1502
|
+ |
assert_eq!(
|
|
1503
|
+ |
ApiPassthroughClient::new("https://openagents.com", None).origin,
|
|
1504
|
+ |
"https://openagents.com"
|
|
1505
|
+ |
);
|
|
1506
|
+ |
}
|