Files
codex/codex-rs/plugin-archive-verification/tests/cli.rs
T
Alex Ionescu 7670578418 Report remote plugin archive verification in install telemetry (#65330)
## Why

Remote plugin install telemetry needs enough detail to distinguish archive
verification failures from other bundle errors and associate those failures
with a specific release when its ID is available.

## What changed

- Include the archive verification outcome in successful app-server install
  events. Classify verification failures by reason and attach the
  `plugin_release_id` when available. Use the catalog plugin ID for bundle
  events when it is available.
- Export `codex.plugins.archive_verification` through the configured
  OpenTelemetry logger during standalone CLI installs.

## Testing

App-server tests check successful and failed install analytics. A CLI
integration test checks that an archive verification failure exports the
expected event fields and leaves the plugin uninstalled.

GitOrigin-RevId: ea6cab1d04467d63f43fbd17e3d0a122fad15435
2026-12-02 20:34:56 +00:00

199 lines
6.8 KiB
Rust

//! Exercises archive verification and OTLP export from the standalone remote plugin installer.
mod common;
use anyhow::Context;
use anyhow::Result;
use app_test_support::ChatGptAuthFixture;
use app_test_support::write_chatgpt_auth;
use codex_config::types::AuthCredentialsStoreMode;
use codex_utils_cargo_bin::cargo_bin;
use common::ALLOW_HTTP_BUNDLE_DOWNLOADS;
use common::PluginArchiveSigningFixture;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use tempfile::TempDir;
use tokio::process::Command;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::header;
use wiremock::matchers::method;
use wiremock::matchers::path;
use wiremock::matchers::query_param;
const MARKETPLACE: &str = "openai-curated-remote";
const REMOTE_ID: &str = "plugins~Plugin_0123456789abcdef0123456789abcdef";
const RELEASE_ID: &str = "pluginrel_0123456789abcdef0123456789abcdef";
#[tokio::test]
async fn remote_plugin_install_enforces_archive_signatures_before_mutation() -> Result<()> {
let fixture = PluginArchiveSigningFixture::load()?;
let home = TempDir::new()?;
let server = MockServer::start().await;
std::fs::write(
home.path().join("config.toml"),
format!(
r#"
cli_auth_credentials_store = "file"
chatgpt_base_url = "{}/backend-api"
[features]
plugins = true
remote_plugin = true
"#,
server.uri()
),
)?;
write_chatgpt_auth(
home.path(),
ChatGptAuthFixture::new("chatgpt-token")
.account_id("account-123")
.chatgpt_account_id("account-123"),
AuthCredentialsStoreMode::File,
)?;
let private_version = "1.2.3+private-catalog-marker";
let mut plugin = json!({
"id": REMOTE_ID, "name": "linear", "scope": "GLOBAL",
"installation_policy": "AVAILABLE", "authentication_policy": "ON_USE",
"release": {
"id": RELEASE_ID, "version": private_version,
"display_name": "Linear", "description": "Track issues",
"interface": {}, "bundle_download_url": format!("{}/bundle", server.uri()),
}
});
Mock::given(method("GET"))
.and(path("/backend-api/ps/plugins/list"))
.and(query_param("scope", "GLOBAL"))
.and(header("authorization", "Bearer chatgpt-token"))
.and(header("chatgpt-account-id", "account-123"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"plugins": [plugin.clone()], "pagination": {"next_page_token": null}
})))
.expect(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/backend-api/ps/plugins/installed"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"plugins": [], "pagination": {"next_page_token": null}
})))
.mount(&server)
.await;
plugin["release"]["signing"] = fixture.signing;
Mock::given(method("GET"))
.and(path(format!("/backend-api/ps/plugins/{REMOTE_ID}")))
.and(query_param("includeDownloadUrls", "true"))
.and(header("authorization", "Bearer chatgpt-token"))
.and(header("chatgpt-account-id", "account-123"))
.respond_with(ResponseTemplate::new(200).set_body_json(plugin))
.expect(1)
.mount(&server)
.await;
let mut archive = fixture.archive;
// Changing gzip mtime preserves extraction but invalidates the signed digest.
archive[4] ^= 1;
Mock::given(method("GET"))
.and(path("/bundle"))
.respond_with(ResponseTemplate::new(200).set_body_bytes(archive))
.expect(1)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/backend-api/ps/plugins/signing-keys"))
.respond_with(ResponseTemplate::new(200).set_body_json(fixture.signing_keys_response))
.expect(0)
.mount(&server)
.await;
let collector = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/logs"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
.expect(1)
.mount(&collector)
.await;
let log_exporter = format!(
r#"otel.exporter={{otlp-http={{endpoint="{}/v1/logs",protocol="json"}}}}"#,
collector.uri(),
);
let output = Command::new(cargo_bin("codex")?)
.current_dir(home.path())
.env("CODEX_HOME", home.path())
.env("HOME", home.path())
.env("USERPROFILE", home.path())
// Use the synthetic ChatGPT session even on a workload-identity devbox.
.env_remove("OPENAI_API_KEY")
.env_remove("CODEX_API_KEY")
.env_remove("CODEX_ACCESS_TOKEN")
.env_remove("OPENAI_FEDERATION_RULE_ID")
.env_remove("OPENAI_IDENTITY_TOKEN_FILE")
.env_remove("OPENAI_IDENTITY_TOKEN_SOURCE")
.env_remove("OPENAI_SPIFFE_AUDIENCE")
.env_remove("OPENAI_SPIFFE_ID")
.env_remove("OPENAI_WORKLOAD_IDENTITY_CONTEXT")
.env(ALLOW_HTTP_BUNDLE_DOWNLOADS, "1")
.args([
"-c",
&log_exporter,
"plugin",
"add",
"linear",
"-m",
MARKETPLACE,
])
.output()
.await?;
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr)?;
let expected = format!(
"plugin `linear@{MARKETPLACE}` version `{private_version}` failed archive verification: archive digest does not match its signed metadata"
);
assert!(stderr.contains(&expected), "{stderr}");
let logs = collector
.received_requests()
.await
.context("recorded logs")?;
let payload = logs
.first()
.context("missing exported log")?
.body_json::<Value>()?;
assert!(!payload.to_string().contains(private_version));
let attributes = payload["resourceLogs"][0]["scopeLogs"][0]["logRecords"][0]["attributes"]
.as_array()
.context("missing log attributes")?;
let fields = [
("event.name", "codex.plugins.archive_verification"),
("outcome", "digest_mismatch"),
("archive_kind", "unknown"),
("plugin_release_id", RELEASE_ID),
];
assert_eq!(attributes.len(), fields.len());
for (key, value) in fields {
assert!(
attributes.contains(&json!({"key": key, "value": {"stringValue": value}})),
"{attributes:?}"
);
}
let requests = server
.received_requests()
.await
.context("recorded requests")?;
assert!(requests.iter().all(|request| request.method != "POST"));
assert!(
!home
.path()
.join("plugins/cache/openai-curated-remote/linear")
.join(private_version)
.join(".codex-plugin/plugin.json")
.is_file()
);
Ok(())
}