cpa-plugin 0.4.4

Part of cliproxy: the native plugin host (CLIProxyAPI C ABI 1, registration schema 6) and plugin store
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Test support shared by this crate's tests and the server's plugin tests (feature
//! `test-support`): builds the Go test plugins in `tests/goplugins` (CLIProxyAPI
//! examples at 6fecc6e plus the recorder) as c-shared libraries.
//!
//! The build never touches the network (see [`go_command`]): modules come from the local
//! cache only, so run `go mod download` in `tests/goplugins` once. It needs Go 1.26 or
//! newer, looked up in `$HOME/sdk/go*/bin` first, then on `PATH`, then
//! `/usr/local/go/bin`. Without one, each native test prints a SKIPPED line naming
//! itself and returns, so a skip is visible in the test output.

use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::OnceLock;

/// The minimum Go version: `tests/goplugins/go.mod` says `go 1.26.0`.
const MIN_GO: (u32, u32) = (1, 26);

/// A `go` command that cannot reach the network: no toolchain download, no module proxy,
/// no direct fetch for private patterns (`GONOPROXY` would otherwise default to an
/// inherited `GOPRIVATE`), no checksum database, and telemetry off in a test-owned
/// directory so an opted-in user's uploader never starts.
pub fn go_command(go: &Path) -> Command {
    let telemetry = std::env::temp_dir().join(format!("cpa-plugin-go-telemetry-{}", std::process::id()));
    let _ = std::fs::create_dir_all(&telemetry);
    let _ = std::fs::write(telemetry.join("mode"), "off");
    let mut cmd = Command::new(go);
    cmd.env("GOTOOLCHAIN", "local")
        .env("GOPROXY", "off")
        .env("GONOPROXY", "none")
        .env("GOPRIVATE", "")
        .env("GOSUMDB", "off")
        .env("GOFLAGS", "-mod=readonly")
        .env("TEST_TELEMETRY_DIR", telemetry);
    cmd
}

/// `(major, minor)` of a Go binary's own toolchain, if it runs.
fn go_version(go: &Path) -> Option<(u32, u32)> {
    let out = go_command(go).args(["env", "GOVERSION"]).output().ok()?;
    let text = String::from_utf8(out.stdout).ok()?;
    let mut parts = text.trim().strip_prefix("go")?.split('.');
    Some((parts.next()?.parse().ok()?, parts.next()?.parse().ok()?))
}

/// The first Go toolchain new enough to build the plugins.
pub fn go_binary() -> Option<PathBuf> {
    let mut sdk: Vec<PathBuf> = std::env::var_os("HOME")
        .map(PathBuf::from)
        .and_then(|home| std::fs::read_dir(home.join("sdk")).ok())
        .into_iter()
        .flatten()
        .filter_map(Result::ok)
        .map(|entry| entry.path().join("bin/go"))
        .collect();
    sdk.sort();
    sdk.reverse();
    let path = std::env::var_os("PATH")
        .into_iter()
        .flat_map(|p| std::env::split_paths(&p).collect::<Vec<_>>())
        .map(|dir| dir.join("go"));
    sdk.into_iter()
        .chain(path)
        .chain([PathBuf::from("/usr/local/go/bin/go")])
        .filter(|p| p.is_file())
        .find(|p| go_version(p).is_some_and(|v| v >= MIN_GO))
}

/// The Go module with the test plugins (`tests/goplugins` in this crate).
pub fn goplugins_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/goplugins")
}

/// `<dir>/<example>.so` for every example. `None` (after printing a SKIPPED line for
/// `test`) when no suitable Go is installed. `tmp` is the calling test crate's
/// `CARGO_TARGET_TMPDIR`.
///
/// Under nextest, every test runs in its own process, and `.config/nextest.toml` gives
/// the run an empty `CPA_GOPLUGINS_DIR`: the first process to finish the build publishes
/// it there and the later ones load it instead of linking the examples again (3 s on a
/// warm Go build cache). Concurrent first builds each build into a directory of their
/// own, and the first rename into place wins. Nothing outlives the run, so an inherited
/// build setting such as `GOEXPERIMENT` never meets a build made without it. Elsewhere,
/// each process builds into `<tmp>/goplugins`.
pub fn built_plugins(tmp: &Path, test: &str) -> Option<&'static Path> {
    static BUILT: OnceLock<Option<PathBuf>> = OnceLock::new();
    let built = BUILT
        .get_or_init(|| {
            let go = go_binary()?;
            let src = goplugins_dir();
            let build = |dir: &Path| {
                std::fs::create_dir_all(dir).unwrap();
                for entry in std::fs::read_dir(src.join("examples")).unwrap() {
                    let name = entry.unwrap().file_name().to_string_lossy().into_owned();
                    let status = go_command(&go)
                        .current_dir(&src)
                        .args(["build", "-buildmode=c-shared", "-o"])
                        .arg(dir.join(format!("{name}.so")))
                        .arg(format!("./examples/{name}"))
                        .env("CGO_ENABLED", "1")
                        .status()
                        .expect("run go build");
                    assert!(
                        status.success(),
                        "go build of plugin example {name} failed with {}; the build is offline \
                         (GOPROXY=off), so run `go mod download` in {} first if modules are missing",
                        go.display(),
                        src.display()
                    );
                }
            };
            let Some(shared) = std::env::var_os("CPA_GOPLUGINS_DIR").map(PathBuf::from) else {
                let out = tmp.join("goplugins");
                build(&out);
                return Some(out);
            };
            // Only a complete build is ever renamed to `out`.
            let out = shared.join("built");
            if out.is_dir() {
                return Some(out);
            }
            let partial = shared.join(format!("partial-{}", std::process::id()));
            build(&partial);
            if let Err(e) = std::fs::rename(&partial, &out) {
                // Another process of this run published the same build first.
                assert!(out.is_dir(), "move the plugin build to {}: {e}", out.display());
                let _ = std::fs::remove_dir_all(&partial);
            }
            Some(out)
        })
        .as_deref();
    if built.is_none() {
        // CI sets CPA_TEST_NO_SKIP: there a missing Go toolchain fails the test. A skip
        // line alone is not enough, because the harness captures test output and a
        // passing test's output is never shown.
        assert!(
            std::env::var_os("CPA_TEST_NO_SKIP").is_none(),
            "cpa-plugin {test}: no Go {}.{}+ toolchain in $HOME/sdk/go*/bin, PATH or /usr/local/go/bin, \
             and CPA_TEST_NO_SKIP is set",
            MIN_GO.0,
            MIN_GO.1
        );
        // Straight to the process's stderr: the test harness hides `eprintln!` output
        // of passing tests, and a skip must not look like a real run.
        use std::io::Write as _;
        let _ = writeln!(
            std::io::stderr(),
            "SKIPPED: cpa-plugin {test}: no Go {}.{}+ toolchain in $HOME/sdk/go*/bin, PATH or /usr/local/go/bin; \
             the Go plugin comparison did not run",
            MIN_GO.0,
            MIN_GO.1
        );
    }
    built
}

/// A fresh scratch directory under `tmp`.
pub fn scratch(tmp: &Path, name: &str) -> PathBuf {
    let dir = tmp.join(format!("{name}-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// The raw HTTP/1.1 upstream the `host.http.*` goldens use (the Go generator runs the
/// same server, `reference/pluginhost/upstream.go`): `/echo` answers 200 with the exact
/// request bytes received (this server's address written as `UPSTREAM`), `/stream` sends "one", "two", "three" as chunks 50 ms apart,
/// anything else is an empty 404. Every response closes the connection and has no
/// Date. Returns `host:port`; the server runs until the runtime ends.
pub async fn raw_upstream() -> String {
    use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap().to_string();
    let own = addr.clone();
    tokio::spawn(async move {
        loop {
            let Ok((conn, _)) = listener.accept().await else {
                return;
            };
            let own = own.clone();
            tokio::spawn(async move {
                let mut reader = BufReader::new(conn);
                let mut head = Vec::new();
                let (mut content_length, mut chunked) = (0usize, false);
                loop {
                    let mut line = String::new();
                    if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
                        return;
                    }
                    head.extend_from_slice(line.as_bytes());
                    let trimmed = line.trim_end_matches(['\r', '\n']);
                    if trimmed.is_empty() {
                        break;
                    }
                    if let Some((name, value)) = trimmed.split_once(':') {
                        match name.trim().to_ascii_lowercase().as_str() {
                            "content-length" => content_length = value.trim().parse().unwrap_or(0),
                            "transfer-encoding" => chunked = value.to_ascii_lowercase().contains("chunked"),
                            _ => {}
                        }
                    }
                }
                let mut body = Vec::new();
                if chunked {
                    loop {
                        let mut line = String::new();
                        if reader.read_line(&mut line).await.unwrap_or(0) == 0 {
                            return;
                        }
                        body.extend_from_slice(line.as_bytes());
                        if line.trim_end_matches(['\r', '\n']) == "0" {
                            let mut end = String::new();
                            let _ = reader.read_line(&mut end).await;
                            body.extend_from_slice(end.as_bytes());
                            break;
                        }
                    }
                } else if content_length > 0 {
                    body.resize(content_length, 0);
                    if reader.read_exact(&mut body).await.is_err() {
                        return;
                    }
                }
                let text = String::from_utf8_lossy(&head).into_owned();
                let path = text.split(' ').nth(1).unwrap_or_default().to_owned();
                let mut conn = reader.into_inner();
                if path.starts_with("/echo") {
                    let mut raw = head;
                    raw.extend_from_slice(&body);
                    // The echo names this server UPSTREAM so it compares across runs.
                    let echo = String::from_utf8_lossy(&raw).replace(&own, "UPSTREAM").into_bytes();
                    let reply = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nX-Multi: a\r\nX-Multi: b\r\nConnection: close\r\nContent-Length: {}\r\n\r\n",
                        echo.len()
                    );
                    let _ = conn.write_all(reply.as_bytes()).await;
                    let _ = conn.write_all(&echo).await;
                } else if path.starts_with("/stream") {
                    let _ = conn
                        .write_all(b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n")
                        .await;
                    for chunk in ["one", "two", "three"] {
                        let _ = conn
                            .write_all(format!("{:x}\r\n{chunk}\r\n", chunk.len()).as_bytes())
                            .await;
                        let _ = conn.flush().await;
                        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                    }
                    let _ = conn.write_all(b"0\r\n\r\n").await;
                } else if path.starts_with("/slow") {
                    let _ = conn
                        .write_all(
                            b"HTTP/1.1 200 OK\r\nConnection: close\r\nTransfer-Encoding: chunked\r\n\r\n3\r\none\r\n",
                        )
                        .await;
                    let _ = conn.flush().await;
                    tokio::time::sleep(std::time::Duration::from_secs(3)).await;
                    let _ = conn.write_all(b"3\r\ntwo\r\n0\r\n\r\n").await;
                } else if let Some(reply) = canned(&path) {
                    let _ = conn.write_all(&reply).await;
                } else if !path.starts_with("/hangup") {
                    let _ = conn
                        .write_all(b"HTTP/1.1 404 Not Found\r\nConnection: close\r\nContent-Length: 0\r\n\r\n")
                        .await;
                }
                let _ = conn.shutdown().await;
            });
        }
    });
    addr
}

/// The raw upstream's fixed replies (tests/reference/pluginhost/upstream.go).
fn canned(path: &str) -> Option<Vec<u8>> {
    let reply: &[u8] = if path.starts_with("/truncated") {
        b"HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 10\r\n\r\nabc"
    } else if path.starts_with("/http10") {
        b"HTTP/1.0 200 OK\r\nConnection: close\r\nTrailer: X-T\r\nContent-Length: 2\r\n\r\nok"
    } else if path.starts_with("/redirect") {
        b"HTTP/1.1 302 Found\r\nLocation: http://127.0.0.1:1/after\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
    } else if path.starts_with("/trailer") {
        b"HTTP/1.1 200 OK\r\nConnection: close\r\nTrailer: X-T\r\nTransfer-Encoding: chunked\r\n\r\n2\r\nok\r\n0\r\nX-T: v\r\n\r\n"
    } else {
        return None;
    };
    Some(reply.to_vec())
}

/// Opens a callback context for `plugin_id`'s `instance`, as a capability call does
/// before calling the plugin. The context closes when the guard drops.
pub fn callback_context(
    host: &crate::Host,
    plugin_id: &str,
    instance: std::sync::Arc<crate::client::CallbackInstance>,
    scope: crate::callbacks::RequestScope,
) -> crate::callbacks::ContextGuard {
    // Plugins get the dispatcher when they load; this records the runtime the same way.
    let _ = host.inner.callbacks.handler();
    host.inner.callbacks.open(plugin_id, Some(instance), scope)
}

/// One `host.*` callback as the plugin `plugin_id`'s `instance` makes it. Call it from
/// a blocking thread, as plugins do.
pub fn call_from_plugin(
    host: &crate::Host,
    plugin_id: &str,
    instance: &std::sync::Arc<crate::client::CallbackInstance>,
    method: &str,
    request: &[u8],
) -> Result<bytes::Bytes, crate::client::CallbackError> {
    let caller = crate::callbacks::Caller {
        plugin_id: plugin_id.to_owned(),
        instance: instance.clone(),
    };
    host.call_from_plugin(&caller, method, request)
}

/// A zip archive of stored (uncompressed) entries, like Go's `zip.Writer` with
/// `zip.Store`: `(name, data, mode)`, where a non-zero mode is recorded as Unix
/// permission bits of a regular file (`FileHeader.SetMode`).
pub fn stored_zip(entries: &[(&str, &[u8], u32)]) -> Vec<u8> {
    fn u16le(out: &mut Vec<u8>, v: u16) {
        out.extend_from_slice(&v.to_le_bytes());
    }
    fn u32le(out: &mut Vec<u8>, v: u32) {
        out.extend_from_slice(&v.to_le_bytes());
    }
    let mut out = Vec::new();
    let mut central = Vec::new();
    for (name, data, mode) in entries {
        let mut crc = flate2::Crc::new();
        crc.update(data);
        let (crc, size, offset) = (crc.sum(), data.len() as u32, out.len() as u32);
        u32le(&mut out, 0x0403_4b50);
        for v in [20, 0, 0, 0, 0] {
            u16le(&mut out, v);
        }
        for v in [crc, size, size] {
            u32le(&mut out, v);
        }
        u16le(&mut out, name.len() as u16);
        u16le(&mut out, 0);
        out.extend_from_slice(name.as_bytes());
        out.extend_from_slice(data);
        // Central directory: Unix creator when a mode is set, so readers take the
        // permission bits from the external attributes.
        let creator = if *mode != 0 { (3 << 8) | 20 } else { 20 };
        let external = if *mode != 0 { (0o100000 | mode) << 16 } else { 0 };
        u32le(&mut central, 0x0201_4b50);
        for v in [creator, 20, 0, 0, 0, 0] {
            u16le(&mut central, v);
        }
        for v in [crc, size, size] {
            u32le(&mut central, v);
        }
        for v in [name.len() as u16, 0, 0, 0, 0] {
            u16le(&mut central, v);
        }
        u32le(&mut central, external);
        u32le(&mut central, offset);
        central.extend_from_slice(name.as_bytes());
    }
    let (central_offset, central_size) = (out.len() as u32, central.len() as u32);
    out.extend_from_slice(&central);
    u32le(&mut out, 0x0605_4b50);
    for v in [0, 0, entries.len() as u16, entries.len() as u16] {
        u16le(&mut out, v);
    }
    u32le(&mut out, central_size);
    u32le(&mut out, central_offset);
    u16le(&mut out, 0);
    out
}

/// In-process plugins with scripted answers, for deterministic host tests: each
/// plugin registers with its capabilities, answers each method with a canned result,
/// and can fuse a plugin when a given method is called.
pub mod scripted {
    use std::collections::BTreeMap;
    use std::path::Path;
    use std::sync::{Arc, Mutex, OnceLock};

    use bytes::Bytes;
    use cpa_core::config::Config;

    use crate::Host;
    use crate::client::{CallbackHandler, CallbackInstance, PluginClient};
    use crate::host::Loader;
    use crate::platform::{self, PluginFile};

    pub type Log = Arc<Mutex<Vec<String>>>;

    /// One scripted plugin: its capabilities, canned results per method, and an optional
    /// plugin it fuses when a given method is called.
    #[derive(Clone, Default)]
    pub struct Script {
        pub caps: &'static str,
        pub results: BTreeMap<&'static str, &'static str>,
        pub fuse_on: Option<(&'static str, &'static str)>,
    }

    struct ScriptClient {
        id: String,
        script: Script,
        host: Arc<OnceLock<Host>>,
        log: Log,
    }

    impl PluginClient for ScriptClient {
        fn call(&self, method: &str, _: &[u8]) -> Result<Bytes, String> {
            if method == "plugin.register" || method == "plugin.reconfigure" {
                return Ok(Bytes::from(format!(
                    r#"{{"ok":true,"result":{{"schema_version":6,"metadata":{{"Name":"{}","Version":"1","Author":"t","GitHubRepository":"r"}},"capabilities":{{{}}}}}}}"#,
                    self.id, self.script.caps
                )));
            }
            self.log.lock().unwrap().push(format!("{} {method}", self.id));
            if let Some((on, victim)) = self.script.fuse_on
                && on == method
            {
                self.host.get().unwrap().fuse(victim, method, "test");
            }
            let result = self.script.results.get(method).copied().unwrap_or("{}");
            Ok(Bytes::from(format!(r#"{{"ok":true,"result":{result}}}"#)))
        }
        fn shutdown(&self) {}
    }

    struct ScriptLoader {
        scripts: BTreeMap<&'static str, Script>,
        host: Arc<OnceLock<Host>>,
        log: Log,
    }

    impl Loader for ScriptLoader {
        fn open(
            &self,
            file: &PluginFile,
            _: Arc<dyn CallbackHandler>,
            _: Arc<CallbackInstance>,
        ) -> Result<Arc<dyn PluginClient>, String> {
            Ok(Arc::new(ScriptClient {
                id: file.id.clone(),
                script: self.scripts[file.id.as_str()].clone(),
                host: self.host.clone(),
                log: self.log.clone(),
            }))
        }
    }

    /// A host with the scripted plugins loaded from `dir` (recreated), in key order with
    /// descending priorities (5, 1, -3, ...); the log starts empty.
    pub async fn host_with(dir: &Path, scripts: BTreeMap<&'static str, Script>) -> (Host, Log) {
        let _ = std::fs::remove_dir_all(dir);
        std::fs::create_dir_all(dir).unwrap();
        let mut yaml = format!("plugins:\n  enabled: true\n  dir: {}\n  configs:\n", dir.display());
        for (i, id) in scripts.keys().enumerate() {
            std::fs::write(dir.join(format!("{id}{}", platform::extension(platform::goos()))), b"").unwrap();
            yaml.push_str(&format!(
                "    {id}:\n      enabled: true\n      priority: {}\n",
                5 - 4 * i as i64
            ));
        }
        let slot = Arc::new(OnceLock::new());
        let log: Log = Arc::default();
        let host = Host::with_loader(Arc::new(ScriptLoader {
            scripts,
            host: slot.clone(),
            log: log.clone(),
        }));
        let _ = slot.set(host.clone());
        host.apply_config(Arc::new(Config::parse(&yaml).unwrap())).await;
        log.lock().unwrap().clear();
        (host, log)
    }
}