mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Simplify requirements.toml managed fields
This commit is contained in:
@@ -366,7 +366,9 @@ pub struct ConfigReadResponse {
|
||||
pub layers: Option<Vec<ConfigLayer>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema, TS, ExperimentalApi)]
|
||||
#[derive(
|
||||
Serialize, Deserialize, Debug, Clone, Default, PartialEq, JsonSchema, TS, ExperimentalApi,
|
||||
)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[ts(export_to = "v2/")]
|
||||
pub struct ConfigRequirements {
|
||||
|
||||
@@ -1696,33 +1696,7 @@ fn config_requirements_granular_allowed_approval_policy_is_marked_experimental()
|
||||
request_permissions: false,
|
||||
mcp_elicitations: false,
|
||||
}]),
|
||||
allowed_approvals_reviewers: None,
|
||||
allowed_sandbox_modes: None,
|
||||
allowed_windows_sandbox_implementations: None,
|
||||
allowed_permission_profiles: None,
|
||||
default_permissions: None,
|
||||
allowed_web_search_modes: None,
|
||||
allow_managed_hooks_only: None,
|
||||
allow_appshots: None,
|
||||
allow_remote_control: None,
|
||||
computer_use: None,
|
||||
feature_requirements: None,
|
||||
hooks: None,
|
||||
enforce_residency: None,
|
||||
network: None,
|
||||
allowed_login_methods: None,
|
||||
allowed_chatgpt_workspaces: None,
|
||||
cli_auth_credentials_store: None,
|
||||
chatgpt_base_url: None,
|
||||
sqlite_home: None,
|
||||
log_dir: None,
|
||||
model_catalog_json: None,
|
||||
check_for_update_on_startup: None,
|
||||
otel: None,
|
||||
allow_login_shell: None,
|
||||
shell_environment_policy: None,
|
||||
feedback: None,
|
||||
windows_sandbox_private_desktop: None,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_eq!(reason, Some("askForApproval.granular"));
|
||||
|
||||
@@ -602,15 +602,9 @@ mod tests {
|
||||
use codex_config::ComputerUseRequirementsToml;
|
||||
use codex_config::ConfigRequirementsToml;
|
||||
use codex_config::WindowsRequirementsToml;
|
||||
use codex_config::types::OtelConfigToml;
|
||||
use codex_config::types::OtelExporterKind;
|
||||
use codex_config::types::OtelHttpProtocol as CoreOtelHttpProtocol;
|
||||
use codex_config::types::ShellEnvironmentPolicyToml;
|
||||
use codex_protocol::config_types::ShellEnvironmentPolicyInherit as CoreShellEnvironmentPolicyInherit;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn requirements_api_includes_allow_managed_hooks_only() {
|
||||
@@ -710,34 +704,28 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn requirements_api_preserves_otel_headers_and_shell_environment_policy() {
|
||||
let headers = HashMap::from([("Authorization".to_string(), "Bearer secret".to_string())]);
|
||||
let shell_set = HashMap::from([("MANAGED".to_string(), "true".to_string())]);
|
||||
let requirements = toml::from_str(
|
||||
r#"
|
||||
[otel]
|
||||
log_user_prompt = false
|
||||
environment = "production"
|
||||
exporter = { otlp-http = { endpoint = "https://otel.example.com/v1/logs", headers = { Authorization = "Bearer secret" }, protocol = "json" } }
|
||||
|
||||
let mapped = map_requirements_toml_to_api(ConfigRequirementsToml {
|
||||
otel: Some(OtelConfigToml {
|
||||
log_user_prompt: Some(false),
|
||||
environment: Some("production".to_string()),
|
||||
exporter: Some(OtelExporterKind::OtlpHttp {
|
||||
endpoint: "https://otel.example.com/v1/logs".to_string(),
|
||||
headers,
|
||||
protocol: CoreOtelHttpProtocol::Json,
|
||||
tls: None,
|
||||
}),
|
||||
trace_exporter: None,
|
||||
metrics_exporter: None,
|
||||
span_attributes: Some(BTreeMap::from([("team".to_string(), "codex".to_string())])),
|
||||
tracestate: None,
|
||||
}),
|
||||
shell_environment_policy: Some(ShellEnvironmentPolicyToml {
|
||||
inherit: Some(CoreShellEnvironmentPolicyInherit::Core),
|
||||
ignore_default_excludes: Some(false),
|
||||
exclude: Some(vec!["*SECRET*".to_string()]),
|
||||
r#set: Some(shell_set.clone()),
|
||||
include_only: None,
|
||||
experimental_use_profile: Some(false),
|
||||
}),
|
||||
..ConfigRequirementsToml::default()
|
||||
});
|
||||
[otel.span_attributes]
|
||||
team = "codex"
|
||||
|
||||
[shell_environment_policy]
|
||||
inherit = "core"
|
||||
ignore_default_excludes = false
|
||||
exclude = ["*SECRET*"]
|
||||
experimental_use_profile = false
|
||||
|
||||
[shell_environment_policy.set]
|
||||
MANAGED = "true"
|
||||
"#,
|
||||
)
|
||||
.expect("valid requirements TOML");
|
||||
let mapped = map_requirements_toml_to_api(requirements);
|
||||
|
||||
assert_eq!(
|
||||
mapped
|
||||
@@ -752,7 +740,7 @@ mod tests {
|
||||
"inherit": "core",
|
||||
"ignore_default_excludes": false,
|
||||
"exclude": ["*SECRET*"],
|
||||
"set": shell_set,
|
||||
"set": {"MANAGED": "true"},
|
||||
"include_only": null,
|
||||
"experimental_use_profile": false
|
||||
}))
|
||||
|
||||
@@ -9300,6 +9300,20 @@ async fn explicit_approval_policy_falls_back_when_disallowed_by_requirements() -
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn load_with_enterprise_requirement(
|
||||
codex_home: &TempDir,
|
||||
requirements: impl Into<String>,
|
||||
) -> std::io::Result<Config> {
|
||||
ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(requirements),
|
||||
)
|
||||
.build()
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn exact_requirements_override_only_managed_leaves() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
@@ -9387,14 +9401,7 @@ sandbox_private_desktop = false
|
||||
required_log_dir.display(),
|
||||
catalog_path.display(),
|
||||
);
|
||||
let config = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(requirements),
|
||||
)
|
||||
.build()
|
||||
.await?;
|
||||
let config = load_with_enterprise_requirement(&codex_home, requirements).await?;
|
||||
|
||||
let expected_shell_environment_policy: ShellEnvironmentPolicy =
|
||||
codex_config::types::ShellEnvironmentPolicyToml {
|
||||
@@ -9454,17 +9461,12 @@ sandbox_private_desktop = false
|
||||
#[tokio::test]
|
||||
async fn invalid_required_otel_trace_metadata_fails_closed() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let err = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(
|
||||
"[otel.span_attributes]\n\"\" = \"missing-key\"\n",
|
||||
),
|
||||
)
|
||||
.build()
|
||||
.await
|
||||
.expect_err("invalid managed OTEL metadata should fail startup");
|
||||
let err = load_with_enterprise_requirement(
|
||||
&codex_home,
|
||||
"[otel.span_attributes]\n\"\" = \"missing-key\"\n",
|
||||
)
|
||||
.await
|
||||
.expect_err("invalid managed OTEL metadata should fail startup");
|
||||
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
assert!(
|
||||
@@ -9492,14 +9494,7 @@ zz_overflow = {configured_overflow:?}
|
||||
let required_value = "r".repeat(/*n*/ 120);
|
||||
let requirements = format!("[otel.tracestate.vendor]\nrequired = {required_value:?}\n");
|
||||
|
||||
let config = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(requirements),
|
||||
)
|
||||
.build()
|
||||
.await?;
|
||||
let config = load_with_enterprise_requirement(&codex_home, requirements).await?;
|
||||
|
||||
assert_eq!(
|
||||
config.otel.tracestate,
|
||||
@@ -9525,17 +9520,10 @@ async fn conflicting_login_method_requirements_fail_closed() -> std::io::Result<
|
||||
codex_home.path().join(CONFIG_TOML_FILE),
|
||||
"forced_login_method = \"api\"\n",
|
||||
)?;
|
||||
let err = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(
|
||||
"[allowed_login_methods]\nchatgpt = true\n",
|
||||
),
|
||||
)
|
||||
.build()
|
||||
.await
|
||||
.expect_err("conflicting login restrictions should fail closed");
|
||||
let err =
|
||||
load_with_enterprise_requirement(&codex_home, "[allowed_login_methods]\nchatgpt = true\n")
|
||||
.await
|
||||
.expect_err("conflicting login restrictions should fail closed");
|
||||
|
||||
assert_eq!(err.kind(), ErrorKind::InvalidInput);
|
||||
assert!(
|
||||
@@ -9548,19 +9536,14 @@ async fn conflicting_login_method_requirements_fail_closed() -> std::io::Result<
|
||||
#[tokio::test]
|
||||
async fn workspace_requirements_can_disable_all_chatgpt_workspaces() -> std::io::Result<()> {
|
||||
let codex_home = TempDir::new()?;
|
||||
let config = ConfigBuilder::without_managed_config_for_tests()
|
||||
.codex_home(codex_home.path().to_path_buf())
|
||||
.fallback_cwd(Some(codex_home.path().to_path_buf()))
|
||||
.cloud_config_bundle(
|
||||
CloudConfigBundleFixture::loader_with_enterprise_requirement(
|
||||
r#"
|
||||
let config = load_with_enterprise_requirement(
|
||||
&codex_home,
|
||||
r#"
|
||||
[allowed_chatgpt_workspaces]
|
||||
"A" = false
|
||||
"#,
|
||||
),
|
||||
)
|
||||
.build()
|
||||
.await?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(config.forced_chatgpt_workspace_id, Some(Vec::new()));
|
||||
Ok(())
|
||||
|
||||
@@ -1701,24 +1701,6 @@ pub async fn load_config_toml_with_layer_stack(
|
||||
})
|
||||
}
|
||||
|
||||
/// Load the raw merged config needed to bootstrap cloud requirements, applying
|
||||
/// exact auth requirements from every layer available to this load.
|
||||
///
|
||||
/// The initial bootstrap call has no cloud loader, so system and MDM
|
||||
/// requirements determine how the cloud bundle is fetched. A later call may
|
||||
/// include cloud requirements, but those values cannot retroactively change
|
||||
/// the loader that fetched them.
|
||||
pub async fn load_bootstrap_config_as_toml_with_cli_and_load_options(
|
||||
codex_home: &Path,
|
||||
cwd: Option<&AbsolutePathBuf>,
|
||||
cli_overrides: Vec<(String, TomlValue)>,
|
||||
options: impl Into<ConfigLoadOptions>,
|
||||
) -> std::io::Result<ConfigToml> {
|
||||
load_bootstrap_config_toml_with_layer_stack(codex_home, cwd, cli_overrides, options)
|
||||
.await
|
||||
.map(|result| result.config_toml)
|
||||
}
|
||||
|
||||
/// Load the bootstrap config together with its layer stack, applying exact
|
||||
/// auth requirements needed before cloud requirements can be fetched.
|
||||
pub async fn load_bootstrap_config_toml_with_layer_stack(
|
||||
@@ -2664,13 +2646,15 @@ impl Config {
|
||||
.startup_warnings()
|
||||
.unwrap_or_default()
|
||||
.to_vec();
|
||||
let applied_config_requirements = requirements::apply_to_config(
|
||||
let requirements::AppliedConfigRequirements {
|
||||
cli_auth_credentials_store_mode,
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
} = requirements::apply_to_config(
|
||||
&mut cfg,
|
||||
config_layer_stack.requirements(),
|
||||
&mut startup_warnings,
|
||||
)?;
|
||||
let configured_auth_restrictions =
|
||||
requirements::ConfiguredAuthRestrictions::from_config(&cfg);
|
||||
|
||||
// Destructure every field to ensure ConfigRequirements additions are
|
||||
// either applied above or handled while constructing the final Config.
|
||||
@@ -3283,17 +3267,6 @@ impl Config {
|
||||
|
||||
let use_experimental_unified_exec_tool = features.enabled(Feature::UnifiedExec);
|
||||
|
||||
let requirements::ResolvedAuthRestrictions {
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
} = requirements::resolve_auth_restrictions(
|
||||
configured_auth_restrictions,
|
||||
config_layer_stack.requirements(),
|
||||
)?;
|
||||
let requirements::AppliedConfigRequirements {
|
||||
auth_credentials_store,
|
||||
} = applied_config_requirements;
|
||||
|
||||
let model = model.or(cfg.model);
|
||||
let notices = cfg.notice.unwrap_or_default();
|
||||
let service_tier = match service_tier_override {
|
||||
@@ -3552,8 +3525,7 @@ impl Config {
|
||||
include_environment_context,
|
||||
// The config.toml omits "_mode" because it's a config file. However, "_mode"
|
||||
// is important in code to differentiate the mode from the store implementation.
|
||||
cli_auth_credentials_store_mode: auth_credentials_store
|
||||
.resolve(cfg.cli_auth_credentials_store.unwrap_or_default()),
|
||||
cli_auth_credentials_store_mode,
|
||||
mcp_servers,
|
||||
// The config.toml omits "_mode" because it's a config file. However, "_mode"
|
||||
// is important in code to differentiate the mode from the store implementation.
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Display;
|
||||
|
||||
use codex_config::RequirementSource;
|
||||
use codex_config::Sourced;
|
||||
use codex_config::types::DEFAULT_OTEL_ENVIRONMENT;
|
||||
use codex_config::types::OtelConfig;
|
||||
use codex_config::types::OtelConfigToml;
|
||||
@@ -36,15 +38,104 @@ pub(crate) fn resolve_config(
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_requirement(
|
||||
configured: &mut Option<OtelConfigToml>,
|
||||
requirement: &Sourced<OtelConfigToml>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) -> std::io::Result<()> {
|
||||
let Sourced {
|
||||
value: required,
|
||||
source,
|
||||
} = requirement;
|
||||
validate_requirement(required)?;
|
||||
|
||||
let OtelConfigToml {
|
||||
log_user_prompt,
|
||||
environment,
|
||||
exporter,
|
||||
trace_exporter,
|
||||
metrics_exporter,
|
||||
span_attributes,
|
||||
tracestate,
|
||||
} = required;
|
||||
let configured = configured.get_or_insert_default();
|
||||
let mut conflict = false;
|
||||
|
||||
conflict |= super::requirements::replace_required_leaf(
|
||||
&mut configured.log_user_prompt,
|
||||
log_user_prompt,
|
||||
);
|
||||
conflict |=
|
||||
super::requirements::replace_required_leaf(&mut configured.environment, environment);
|
||||
conflict |= super::requirements::replace_required_leaf(&mut configured.exporter, exporter);
|
||||
conflict |=
|
||||
super::requirements::replace_required_leaf(&mut configured.trace_exporter, trace_exporter);
|
||||
conflict |= super::requirements::replace_required_leaf(
|
||||
&mut configured.metrics_exporter,
|
||||
metrics_exporter,
|
||||
);
|
||||
|
||||
if let Some(required) = span_attributes.as_ref() {
|
||||
let configured = configured.span_attributes.get_or_insert_default();
|
||||
conflict |= required
|
||||
.iter()
|
||||
.any(|(key, value)| configured.get(key).is_some_and(|current| current != value));
|
||||
configured.extend(required.clone());
|
||||
}
|
||||
if let Some(required) = tracestate.as_ref() {
|
||||
let configured_tracestate = configured.tracestate.take().unwrap_or_default();
|
||||
conflict |= required.iter().any(|(member, required_fields)| {
|
||||
configured_tracestate
|
||||
.get(member)
|
||||
.is_some_and(|configured_fields| {
|
||||
required_fields.iter().any(|(key, value)| {
|
||||
configured_fields
|
||||
.get(key)
|
||||
.is_some_and(|current| current != value)
|
||||
})
|
||||
})
|
||||
});
|
||||
configured.tracestate = Some(merge_required_tracestate(
|
||||
configured_tracestate,
|
||||
required.clone(),
|
||||
source,
|
||||
startup_warnings,
|
||||
));
|
||||
}
|
||||
|
||||
super::requirements::push_leaf_override_warning("otel", conflict, source, startup_warnings);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn validate_requirement(requirement: &OtelConfigToml) -> std::io::Result<()> {
|
||||
if let Some(span_attributes) = requirement.span_attributes.as_ref() {
|
||||
codex_otel::validate_span_attributes(span_attributes).map_err(|err| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid required `otel.span_attributes`: {err}"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
if let Some(tracestate) = requirement.tracestate.as_ref() {
|
||||
codex_otel::validate_tracestate_entries(tracestate).map_err(|err| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid required `otel.tracestate`: {err}"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Merges user-configured tracestate into a validated required base.
|
||||
///
|
||||
/// Required member fields always win. Other configured fields are admitted one
|
||||
/// at a time only when the complete candidate remains W3C-valid; rejected
|
||||
/// fields produce startup warnings rather than erasing required metadata.
|
||||
pub(crate) fn merge_required_tracestate(
|
||||
fn merge_required_tracestate(
|
||||
configured: BTreeMap<String, BTreeMap<String, String>>,
|
||||
required: BTreeMap<String, BTreeMap<String, String>>,
|
||||
source: &impl Display,
|
||||
source: &RequirementSource,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) -> BTreeMap<String, BTreeMap<String, String>> {
|
||||
let mut effective = required.clone();
|
||||
@@ -62,39 +153,22 @@ pub(crate) fn merge_required_tracestate(
|
||||
.entry(member_key.clone())
|
||||
.or_default()
|
||||
.insert(field_key.clone(), value);
|
||||
retain_valid_configured_tracestate(
|
||||
candidate,
|
||||
&format!("otel.tracestate.{member_key}.{field_key}"),
|
||||
source,
|
||||
startup_warnings,
|
||||
&mut effective,
|
||||
);
|
||||
match codex_otel::validate_tracestate_entries(&candidate) {
|
||||
Ok(()) => effective = candidate,
|
||||
Err(err) => {
|
||||
let config_key = format!("otel.tracestate.{member_key}.{field_key}");
|
||||
let message = format!(
|
||||
"Ignoring configured `{config_key}` because it would invalidate tracestate required by {source}: {err}"
|
||||
);
|
||||
tracing::warn!("{message}");
|
||||
startup_warnings.push(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
effective
|
||||
}
|
||||
|
||||
fn retain_valid_configured_tracestate(
|
||||
candidate: BTreeMap<String, BTreeMap<String, String>>,
|
||||
config_key: &str,
|
||||
source: &impl Display,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
effective: &mut BTreeMap<String, BTreeMap<String, String>>,
|
||||
) {
|
||||
match codex_otel::validate_tracestate_entries(&candidate) {
|
||||
Ok(()) => {
|
||||
*effective = candidate;
|
||||
}
|
||||
Err(err) => {
|
||||
let message = format!(
|
||||
"Ignoring configured `{config_key}` because it would invalidate tracestate required by {source}: {err}"
|
||||
);
|
||||
tracing::warn!("{message}");
|
||||
startup_warnings.push(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_span_attributes(
|
||||
span_attributes: Option<BTreeMap<String, String>>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
|
||||
@@ -4,64 +4,17 @@ use codex_config::Sourced;
|
||||
use codex_config::config_toml::ConfigToml;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_config::types::FeedbackConfigToml;
|
||||
use codex_config::types::OtelConfigToml;
|
||||
use codex_config::types::ShellEnvironmentPolicyToml;
|
||||
use codex_protocol::config_types::ForcedLoginMethod;
|
||||
|
||||
use super::otel;
|
||||
|
||||
pub(super) struct AppliedConfigRequirements {
|
||||
pub auth_credentials_store: AuthCredentialsStoreRequirement,
|
||||
}
|
||||
|
||||
pub(super) enum AuthCredentialsStoreRequirement {
|
||||
Exact,
|
||||
UserConfigured,
|
||||
}
|
||||
|
||||
impl AuthCredentialsStoreRequirement {
|
||||
pub fn resolve(self, configured: AuthCredentialsStoreMode) -> AuthCredentialsStoreMode {
|
||||
match self {
|
||||
Self::Exact => configured,
|
||||
Self::UserConfigured => super::resolve_cli_auth_credentials_store_mode(
|
||||
configured,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct ResolvedAuthRestrictions {
|
||||
pub cli_auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
pub forced_login_method: Option<ForcedLoginMethod>,
|
||||
pub forced_chatgpt_workspace_id: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
pub(super) struct ConfiguredAuthRestrictions {
|
||||
forced_login_method: Option<ForcedLoginMethod>,
|
||||
forced_chatgpt_workspace_id: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl ConfiguredAuthRestrictions {
|
||||
pub fn from_config(config: &ConfigToml) -> Self {
|
||||
let forced_chatgpt_workspace_id = config
|
||||
.forced_chatgpt_workspace_id
|
||||
.clone()
|
||||
.map(codex_config::config_toml::ForcedChatgptWorkspaceIds::into_vec)
|
||||
.map(|values| {
|
||||
values
|
||||
.into_iter()
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.filter(|values| !values.is_empty());
|
||||
Self {
|
||||
forced_login_method: config.forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_to_config(
|
||||
config: &mut ConfigToml,
|
||||
requirements: &ConfigRequirements,
|
||||
@@ -91,8 +44,7 @@ pub(super) fn apply_to_config(
|
||||
startup_warnings,
|
||||
);
|
||||
if let Some(requirement) = requirements.otel.as_ref() {
|
||||
validate_otel_requirement(&requirement.value)?;
|
||||
apply_otel_requirement(&mut config.otel, requirement, startup_warnings);
|
||||
otel::apply_requirement(&mut config.otel, requirement, startup_warnings)?;
|
||||
}
|
||||
apply_feedback_requirement(
|
||||
&mut config.feedback,
|
||||
@@ -111,12 +63,22 @@ pub(super) fn apply_to_config(
|
||||
);
|
||||
}
|
||||
|
||||
let (forced_login_method, forced_chatgpt_workspace_id) =
|
||||
resolve_auth_restrictions(config, requirements)?;
|
||||
let configured_auth_credentials_store = config.cli_auth_credentials_store.unwrap_or_default();
|
||||
let cli_auth_credentials_store_mode = if requirements.cli_auth_credentials_store.is_some() {
|
||||
configured_auth_credentials_store
|
||||
} else {
|
||||
super::resolve_cli_auth_credentials_store_mode(
|
||||
configured_auth_credentials_store,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
)
|
||||
};
|
||||
|
||||
Ok(AppliedConfigRequirements {
|
||||
auth_credentials_store: if requirements.cli_auth_credentials_store.is_some() {
|
||||
AuthCredentialsStoreRequirement::Exact
|
||||
} else {
|
||||
AuthCredentialsStoreRequirement::UserConfigured
|
||||
},
|
||||
cli_auth_credentials_store_mode,
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -147,6 +109,20 @@ fn apply_exact_requirement<T>(
|
||||
*configured_value = Some(value.clone());
|
||||
}
|
||||
|
||||
pub(super) fn replace_required_leaf<T: Clone + PartialEq>(
|
||||
configured: &mut Option<T>,
|
||||
required: &Option<T>,
|
||||
) -> bool {
|
||||
let Some(required) = required else {
|
||||
return false;
|
||||
};
|
||||
let conflict = configured
|
||||
.as_ref()
|
||||
.is_some_and(|configured| configured != required);
|
||||
*configured = Some(required.clone());
|
||||
conflict
|
||||
}
|
||||
|
||||
fn apply_shell_environment_policy_requirement(
|
||||
configured: &mut ShellEnvironmentPolicyToml,
|
||||
requirement: Option<&Sourced<ShellEnvironmentPolicyToml>>,
|
||||
@@ -165,23 +141,17 @@ fn apply_shell_environment_policy_requirement(
|
||||
} = value;
|
||||
let mut conflict = false;
|
||||
|
||||
macro_rules! replace_leaf {
|
||||
($field:ident) => {
|
||||
if let Some(required) = $field.as_ref() {
|
||||
conflict |= configured
|
||||
.$field
|
||||
.as_ref()
|
||||
.is_some_and(|configured| configured != required);
|
||||
configured.$field = Some(required.clone());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
replace_leaf!(inherit);
|
||||
replace_leaf!(ignore_default_excludes);
|
||||
replace_leaf!(exclude);
|
||||
replace_leaf!(include_only);
|
||||
replace_leaf!(experimental_use_profile);
|
||||
conflict |= replace_required_leaf(&mut configured.inherit, inherit);
|
||||
conflict |= replace_required_leaf(
|
||||
&mut configured.ignore_default_excludes,
|
||||
ignore_default_excludes,
|
||||
);
|
||||
conflict |= replace_required_leaf(&mut configured.exclude, exclude);
|
||||
conflict |= replace_required_leaf(&mut configured.include_only, include_only);
|
||||
conflict |= replace_required_leaf(
|
||||
&mut configured.experimental_use_profile,
|
||||
experimental_use_profile,
|
||||
);
|
||||
|
||||
if let Some(required) = required_set.as_ref() {
|
||||
let configured_set = configured.r#set.get_or_insert_default();
|
||||
@@ -211,117 +181,11 @@ fn apply_feedback_requirement(
|
||||
};
|
||||
let FeedbackConfigToml { enabled } = value;
|
||||
let configured = configured.get_or_insert_default();
|
||||
let conflict = if let Some(required) = *enabled {
|
||||
let conflict = configured
|
||||
.enabled
|
||||
.is_some_and(|configured| configured != required);
|
||||
configured.enabled = Some(required);
|
||||
conflict
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let conflict = replace_required_leaf(&mut configured.enabled, enabled);
|
||||
push_leaf_override_warning("feedback", conflict, source, startup_warnings);
|
||||
}
|
||||
|
||||
fn validate_otel_requirement(requirement: &OtelConfigToml) -> std::io::Result<()> {
|
||||
if let Some(span_attributes) = requirement.span_attributes.as_ref() {
|
||||
codex_otel::validate_span_attributes(span_attributes).map_err(|err| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid required `otel.span_attributes`: {err}"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
if let Some(tracestate) = requirement.tracestate.as_ref() {
|
||||
codex_otel::validate_tracestate_entries(tracestate).map_err(|err| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidInput,
|
||||
format!("invalid required `otel.tracestate`: {err}"),
|
||||
)
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_otel_requirement(
|
||||
configured: &mut Option<OtelConfigToml>,
|
||||
requirement: &Sourced<OtelConfigToml>,
|
||||
startup_warnings: &mut Vec<String>,
|
||||
) {
|
||||
let Sourced {
|
||||
value: required,
|
||||
source,
|
||||
} = requirement;
|
||||
let OtelConfigToml {
|
||||
log_user_prompt,
|
||||
environment,
|
||||
exporter,
|
||||
trace_exporter,
|
||||
metrics_exporter,
|
||||
span_attributes,
|
||||
tracestate,
|
||||
} = required;
|
||||
let configured = configured.get_or_insert_default();
|
||||
let mut conflict = false;
|
||||
|
||||
macro_rules! replace_leaf {
|
||||
($field:ident) => {
|
||||
if let Some(required) = $field.as_ref() {
|
||||
conflict |= configured
|
||||
.$field
|
||||
.as_ref()
|
||||
.is_some_and(|configured| configured != required);
|
||||
configured.$field = Some(required.clone());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
replace_leaf!(log_user_prompt);
|
||||
replace_leaf!(environment);
|
||||
replace_leaf!(exporter);
|
||||
replace_leaf!(trace_exporter);
|
||||
replace_leaf!(metrics_exporter);
|
||||
|
||||
if let Some(required) = span_attributes.as_ref() {
|
||||
let configured = configured.span_attributes.get_or_insert_default();
|
||||
conflict |= required
|
||||
.iter()
|
||||
.any(|(key, value)| configured.get(key).is_some_and(|current| current != value));
|
||||
configured.extend(required.clone());
|
||||
}
|
||||
if let Some(required) = tracestate.as_ref() {
|
||||
let configured_tracestate = configured.tracestate.take().unwrap_or_default();
|
||||
conflict |= required.iter().any(|(member, required_fields)| {
|
||||
configured_tracestate
|
||||
.get(member)
|
||||
.is_some_and(|configured_fields| {
|
||||
required_fields.iter().any(|(key, value)| {
|
||||
configured_fields
|
||||
.get(key)
|
||||
.is_some_and(|current| current != value)
|
||||
})
|
||||
})
|
||||
});
|
||||
configured.tracestate = Some(otel::merge_required_tracestate(
|
||||
configured_tracestate,
|
||||
required.clone(),
|
||||
source,
|
||||
startup_warnings,
|
||||
));
|
||||
}
|
||||
|
||||
if conflict {
|
||||
tracing::warn!(
|
||||
?source,
|
||||
"configured OTEL leaves are overridden by exact requirements"
|
||||
);
|
||||
startup_warnings.push(format!(
|
||||
"Configured leaves under `otel` are overridden by requirements from {source}."
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn push_leaf_override_warning(
|
||||
pub(super) fn push_leaf_override_warning(
|
||||
field_name: &str,
|
||||
conflict: bool,
|
||||
source: &RequirementSource,
|
||||
@@ -339,12 +203,24 @@ fn push_leaf_override_warning(
|
||||
));
|
||||
}
|
||||
|
||||
pub(super) fn resolve_auth_restrictions(
|
||||
configured: ConfiguredAuthRestrictions,
|
||||
fn resolve_auth_restrictions(
|
||||
config: &ConfigToml,
|
||||
requirements: &ConfigRequirements,
|
||||
) -> std::io::Result<ResolvedAuthRestrictions> {
|
||||
) -> std::io::Result<(Option<ForcedLoginMethod>, Option<Vec<String>>)> {
|
||||
let configured_forced_chatgpt_workspace_id = config
|
||||
.forced_chatgpt_workspace_id
|
||||
.clone()
|
||||
.map(codex_config::config_toml::ForcedChatgptWorkspaceIds::into_vec)
|
||||
.map(|values| {
|
||||
values
|
||||
.into_iter()
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.filter(|values| !values.is_empty());
|
||||
let forced_chatgpt_workspace_id = match requirements.allowed_chatgpt_workspaces.as_ref() {
|
||||
Some(requirement) => Some(match configured.forced_chatgpt_workspace_id {
|
||||
Some(requirement) => Some(match configured_forced_chatgpt_workspace_id {
|
||||
Some(configured) => configured
|
||||
.into_iter()
|
||||
.filter(|workspace| requirement.value.get(workspace).copied().unwrap_or(false))
|
||||
@@ -355,14 +231,14 @@ pub(super) fn resolve_auth_restrictions(
|
||||
.filter_map(|(workspace, allowed)| allowed.then_some(workspace.clone()))
|
||||
.collect(),
|
||||
}),
|
||||
None => configured.forced_chatgpt_workspace_id,
|
||||
None => configured_forced_chatgpt_workspace_id,
|
||||
};
|
||||
|
||||
let forced_login_method = match requirements.allowed_login_methods.as_ref() {
|
||||
Some(requirement) => {
|
||||
let chatgpt_allowed = requirement.value.get("chatgpt").copied().unwrap_or(false);
|
||||
let api_allowed = requirement.value.get("api").copied().unwrap_or(false);
|
||||
match configured.forced_login_method {
|
||||
match config.forced_login_method {
|
||||
Some(ForcedLoginMethod::Chatgpt) if chatgpt_allowed => {
|
||||
Some(ForcedLoginMethod::Chatgpt)
|
||||
}
|
||||
@@ -392,11 +268,8 @@ pub(super) fn resolve_auth_restrictions(
|
||||
},
|
||||
}
|
||||
}
|
||||
None => configured.forced_login_method,
|
||||
None => config.forced_login_method,
|
||||
};
|
||||
|
||||
Ok(ResolvedAuthRestrictions {
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
})
|
||||
Ok((forced_login_method, forced_chatgpt_workspace_id))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user