mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
Respect config layers when removing plugin marketplaces (#40683)
## Why Removing a base-user marketplace must not delete a snapshot that is still referenced by another enabled configuration layer. ## What changed - Load the current configuration stack for marketplace commands in the CLI and app server. - Reject removal when a matching marketplace is defined by another enabled layer, and identify the configuration source that must be updated instead. - Allow cleanup of an installed marketplace snapshot even when no base-user entry exists. ## Testing Cover project, enterprise-managed, system, and session-flag configurations, as well as snapshot-only removal through the CLI and app server. GitOrigin-RevId: 4f0853a40c2b5fae0410696b79eee4284239c7a9
This commit is contained in:
committed by
copyberry
parent
f1a6f7c9b3
commit
a6e63f9f32
@@ -3,7 +3,7 @@ use anyhow::Result;
|
||||
use anyhow::bail;
|
||||
use clap::Parser;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::config::find_codex_home;
|
||||
use codex_core::config::LoaderOverrides;
|
||||
use codex_core::plugins_manager_for_config;
|
||||
use codex_core_plugins::PluginMarketplaceUpgradeOutcome;
|
||||
use codex_core_plugins::PluginsConfigInput;
|
||||
@@ -23,6 +23,7 @@ use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::cloud_config;
|
||||
use crate::plugin_cmd::JsonMarketplaceSource;
|
||||
use crate::plugin_cmd::configured_marketplace_snapshot_issues;
|
||||
use crate::plugin_cmd::configured_marketplace_sources;
|
||||
@@ -127,22 +128,21 @@ impl MarketplaceCli {
|
||||
subcommand,
|
||||
} = self;
|
||||
|
||||
let overrides = config_overrides
|
||||
.parse_overrides()
|
||||
.map_err(anyhow::Error::msg)?;
|
||||
let config =
|
||||
cloud_config::load_config(&config_overrides, LoaderOverrides::default()).await?;
|
||||
|
||||
match subcommand {
|
||||
MarketplaceSubcommand::Add(args) => run_add(overrides, args).await?,
|
||||
MarketplaceSubcommand::List(args) => run_list(overrides, args).await?,
|
||||
MarketplaceSubcommand::Upgrade(args) => run_upgrade(overrides, args).await?,
|
||||
MarketplaceSubcommand::Remove(args) => run_remove(args).await?,
|
||||
MarketplaceSubcommand::Add(args) => run_add(config, args).await?,
|
||||
MarketplaceSubcommand::List(args) => run_list(config, args).await?,
|
||||
MarketplaceSubcommand::Upgrade(args) => run_upgrade(config, args).await?,
|
||||
MarketplaceSubcommand::Remove(args) => run_remove(config, args).await?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_add(overrides: Vec<(String, toml::Value)>, args: AddMarketplaceArgs) -> Result<()> {
|
||||
async fn run_add(config: Config, args: AddMarketplaceArgs) -> Result<()> {
|
||||
let AddMarketplaceArgs {
|
||||
source,
|
||||
ref_name,
|
||||
@@ -150,9 +150,6 @@ async fn run_add(overrides: Vec<(String, toml::Value)>, args: AddMarketplaceArgs
|
||||
json,
|
||||
} = args;
|
||||
|
||||
let config = Config::load_with_cli_overrides(overrides)
|
||||
.await
|
||||
.context("failed to load configuration")?;
|
||||
let outcome = add_marketplace(
|
||||
config.codex_home.to_path_buf(),
|
||||
config.config_layer_stack.requirements().clone(),
|
||||
@@ -207,10 +204,7 @@ impl JsonMarketplaceAddOutput {
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_list(overrides: Vec<(String, toml::Value)>, args: ListMarketplaceArgs) -> Result<()> {
|
||||
let config = Config::load_with_cli_overrides(overrides)
|
||||
.await
|
||||
.context("failed to load configuration")?;
|
||||
async fn run_list(config: Config, args: ListMarketplaceArgs) -> Result<()> {
|
||||
let manager = plugins_manager_for_config(&config, load_cli_auth_manager(&config).await?);
|
||||
let plugins_input = config.plugins_config_input();
|
||||
let marketplace_listing = manager
|
||||
@@ -366,17 +360,11 @@ fn configured_marketplace_sources_by_root(
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn run_upgrade(
|
||||
overrides: Vec<(String, toml::Value)>,
|
||||
args: UpgradeMarketplaceArgs,
|
||||
) -> Result<()> {
|
||||
async fn run_upgrade(config: Config, args: UpgradeMarketplaceArgs) -> Result<()> {
|
||||
let UpgradeMarketplaceArgs {
|
||||
marketplace_name,
|
||||
json,
|
||||
} = args;
|
||||
let config = Config::load_with_cli_overrides(overrides)
|
||||
.await
|
||||
.context("failed to load configuration")?;
|
||||
let manager = plugins_manager_for_config(&config, load_cli_auth_manager(&config).await?);
|
||||
let plugins_input = config.plugins_config_input();
|
||||
let outcome = manager
|
||||
@@ -389,14 +377,14 @@ async fn run_upgrade(
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_remove(args: RemoveMarketplaceArgs) -> Result<()> {
|
||||
async fn run_remove(config: Config, args: RemoveMarketplaceArgs) -> Result<()> {
|
||||
let RemoveMarketplaceArgs {
|
||||
marketplace_name,
|
||||
json,
|
||||
} = args;
|
||||
let codex_home = find_codex_home().context("failed to resolve CODEX_HOME")?;
|
||||
let outcome = remove_marketplace(
|
||||
codex_home.to_path_buf(),
|
||||
config.codex_home.to_path_buf(),
|
||||
config.config_layer_stack,
|
||||
MarketplaceRemoveRequest { marketplace_name },
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -11,6 +11,7 @@ use codex_config::ConfigLoadOptions;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_core::config::load_config_toml_with_layer_stack;
|
||||
use codex_core::config::load_global_mcp_servers;
|
||||
use codex_core_plugins::installed_marketplaces::marketplace_install_root;
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::Value;
|
||||
@@ -37,14 +38,14 @@ const MANAGED_SCOPE: &str = "managed.read";
|
||||
const MOCK_ACCESS_TOKEN: &str = "mock-managed-access-token";
|
||||
const MOCK_REFRESH_TOKEN: &str = "mock-managed-refresh-token";
|
||||
|
||||
struct CloudManagedMcpFixture {
|
||||
struct CloudManagedConfigFixture {
|
||||
server: MockServer,
|
||||
codex_home: TempDir,
|
||||
user_config: String,
|
||||
mcp_url: String,
|
||||
}
|
||||
|
||||
impl CloudManagedMcpFixture {
|
||||
impl CloudManagedConfigFixture {
|
||||
async fn new() -> Result<Option<Self>> {
|
||||
let server = MockServer::start().await;
|
||||
let chatgpt_base_url = format!("{}/backend-api", server.uri());
|
||||
@@ -67,7 +68,7 @@ impl CloudManagedMcpFixture {
|
||||
!= Some(chatgpt_base_url.as_str())
|
||||
{
|
||||
eprintln!(
|
||||
"skipping cloud-managed MCP subprocess: host-managed authentication or backend routing prevents isolated mock credentials"
|
||||
"skipping cloud-managed subprocess: host-managed authentication or backend routing prevents isolated mock credentials"
|
||||
);
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -90,7 +91,10 @@ impl CloudManagedMcpFixture {
|
||||
auth = \"oauth\"\n\
|
||||
scopes = [\"{MANAGED_SCOPE}\"]\n\n\
|
||||
[mcp_servers.{MANAGED_SERVER_NAME}.oauth]\n\
|
||||
client_id = \"{MANAGED_CLIENT_ID}\"\n"
|
||||
client_id = \"{MANAGED_CLIENT_ID}\"\n\n\
|
||||
[marketplaces.managed]\n\
|
||||
source_type = \"git\"\n\
|
||||
source = \"https://github.com/owner/repo.git\"\n"
|
||||
);
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/backend-api/wham/config/bundle"))
|
||||
@@ -99,8 +103,8 @@ impl CloudManagedMcpFixture {
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"config_toml": {
|
||||
"enterprise_managed": [{
|
||||
"id": "managed-mcp-config",
|
||||
"name": "Managed MCP servers",
|
||||
"id": "managed-config",
|
||||
"name": "Managed resources",
|
||||
"contents": managed_config,
|
||||
}],
|
||||
},
|
||||
@@ -156,7 +160,7 @@ impl CloudManagedMcpFixture {
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn list_and_get_resolve_cloud_managed_mcp_without_writing_user_config() -> Result<()> {
|
||||
let Some(fixture) = CloudManagedMcpFixture::new().await? else {
|
||||
let Some(fixture) = CloudManagedConfigFixture::new().await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -187,7 +191,7 @@ async fn list_and_get_resolve_cloud_managed_mcp_without_writing_user_config() ->
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn login_and_logout_persist_only_cloud_managed_mcp_oauth_credentials() -> Result<()> {
|
||||
let Some(fixture) = CloudManagedMcpFixture::new().await? else {
|
||||
let Some(fixture) = CloudManagedConfigFixture::new().await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
@@ -382,11 +386,29 @@ async fn login_and_logout_persist_only_cloud_managed_mcp_oauth_credentials() ->
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn add_and_remove_do_not_copy_or_delete_cloud_managed_mcp_servers() -> Result<()> {
|
||||
let Some(fixture) = CloudManagedMcpFixture::new().await? else {
|
||||
async fn add_and_remove_preserve_cloud_managed_resources() -> Result<()> {
|
||||
let Some(fixture) = CloudManagedConfigFixture::new().await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let installed_root = marketplace_install_root(fixture.codex_home.path()).join("managed");
|
||||
std::fs::create_dir_all(&installed_root)?;
|
||||
let marker = installed_root.join("marker.txt");
|
||||
std::fs::write(&marker, "installed")?;
|
||||
// Exercise both the initial fetch and the cached bundle without a user entry.
|
||||
for _ in 0..2 {
|
||||
let output = fixture
|
||||
.command(&["plugin", "marketplace", "remove", "managed"])?
|
||||
.output()
|
||||
.await?;
|
||||
assert!(!output.status.success());
|
||||
assert!(String::from_utf8(output.stderr)?.contains(
|
||||
"marketplace `managed` is configured in enterprise-managed (Managed resources, managed-config); remove it from that configuration source instead"
|
||||
));
|
||||
fixture.assert_user_config_unchanged()?;
|
||||
assert_eq!(std::fs::read_to_string(&marker)?, "installed");
|
||||
}
|
||||
|
||||
fixture
|
||||
.output(&["mcp", "get", MANAGED_SERVER_NAME, "--json"])
|
||||
.await?;
|
||||
@@ -95,3 +95,33 @@ async fn marketplace_remove_rejects_unknown_marketplace() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn marketplace_remove_preserves_marketplace_referenced_by_session_config() -> Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
record_user_marketplace(codex_home.path(), "debug", &configured_marketplace_update())?;
|
||||
write_installed_marketplace(codex_home.path(), "debug")?;
|
||||
let config_path = codex_home.path().join("config.toml");
|
||||
let config = std::fs::read_to_string(&config_path)?;
|
||||
|
||||
codex_command(codex_home.path())?
|
||||
.args([
|
||||
"plugin", "marketplace",
|
||||
"-c", "marketplaces.debug.source_type=\"git\"",
|
||||
"remove", "debug",
|
||||
])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(contains(
|
||||
"marketplace `debug` is configured in session-flags; remove it from that configuration source instead",
|
||||
));
|
||||
|
||||
assert_eq!(std::fs::read_to_string(config_path)?, config);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(
|
||||
marketplace_install_root(codex_home.path()).join("debug/marker.txt")
|
||||
)?,
|
||||
"installed",
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user