mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
## What changed - Load `application.network` requirements at startup and explicit config/account reloads, applying local policy before authentication and cloud configuration bootstrap. - Block traffic when policy loading fails, cancel requests that a new policy no longer permits, and reject configuration loads based on superseded policy snapshots. Local requirements edits take effect on the next explicit reload or restart. - Bind authenticated clients to the current account and revoke them when account ownership changes. Carry policy enforcement through backend requests, realtime connections, workload identity exchanges, and code-mode gRPC streams while preserving gRPC trailers. - Treat denied credential refreshes as policy errors without retrying them or invalidating stored credentials. ## Testing Add regression coverage for startup restrictions, reload ordering, cancellation after restrictive or malformed requirements edits, account revocation, gRPC destination denial, workload identity cancellation, and credential preservation after denied OAuth refresh. GitOrigin-RevId: 0cb4c720a906397e9df06675f01ecf60d741101f
141 lines
4.3 KiB
Rust
141 lines
4.3 KiB
Rust
use codex_backend_client::Client as BackendClient;
|
|
use codex_backend_client::ConfigBundleResponse;
|
|
use codex_backend_client::DeliveredTomlFragment;
|
|
use codex_config::CloudConfigBundle;
|
|
use codex_config::CloudConfigFragment;
|
|
use codex_config::CloudConfigTomlBundle;
|
|
use codex_config::CloudRequirementsFragment;
|
|
use codex_config::CloudRequirementsTomlBundle;
|
|
use codex_http_client::HttpClientFactory;
|
|
use codex_login::CodexAuth;
|
|
use std::future::Future;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub(crate) enum RetryableFailureKind {
|
|
Request { status_code: Option<u16> },
|
|
}
|
|
|
|
impl RetryableFailureKind {
|
|
pub(crate) fn status_code(self) -> Option<u16> {
|
|
match self {
|
|
Self::Request { status_code } => status_code,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub(crate) enum BundleRequestError {
|
|
Policy(codex_http_client::NetworkPolicyDenied),
|
|
Retryable(RetryableFailureKind),
|
|
Unauthorized {
|
|
status_code: Option<u16>,
|
|
message: String,
|
|
},
|
|
}
|
|
|
|
/// Retrieves one cloud config bundle from the backend.
|
|
///
|
|
/// Implementations should return the backend-selected bundle exactly as delivered and leave
|
|
/// validation, caching, and config/requirements parsing decisions to the service layer.
|
|
pub(crate) trait BundleClient: Send + Sync {
|
|
fn get_bundle(
|
|
&self,
|
|
auth: &CodexAuth,
|
|
) -> impl Future<Output = Result<CloudConfigBundle, BundleRequestError>> + Send;
|
|
}
|
|
|
|
pub(crate) struct BackendBundleClient {
|
|
base_url: String,
|
|
http_client_factory: HttpClientFactory,
|
|
}
|
|
|
|
impl BackendBundleClient {
|
|
pub(crate) fn new(base_url: String, http_client_factory: HttpClientFactory) -> Self {
|
|
Self {
|
|
base_url,
|
|
http_client_factory,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl BundleClient for BackendBundleClient {
|
|
async fn get_bundle(&self, auth: &CodexAuth) -> Result<CloudConfigBundle, BundleRequestError> {
|
|
let client = BackendClient::from_auth(
|
|
self.base_url.clone(),
|
|
auth,
|
|
self.http_client_factory.clone(),
|
|
);
|
|
|
|
let response = client
|
|
.get_config_bundle()
|
|
.await
|
|
.inspect_err(|err| {
|
|
tracing::warn!(error = %err, "Failed to fetch cloud config bundle");
|
|
})
|
|
.map_err(|err| {
|
|
if let codex_backend_client::RequestError::Policy(denied) = &err {
|
|
return BundleRequestError::Policy(*denied);
|
|
}
|
|
let status_code = err.status().map(|status| status.as_u16());
|
|
if err.is_unauthorized() {
|
|
BundleRequestError::Unauthorized {
|
|
status_code,
|
|
message: err.to_string(),
|
|
}
|
|
} else {
|
|
BundleRequestError::Retryable(RetryableFailureKind::Request { status_code })
|
|
}
|
|
})?;
|
|
|
|
Ok(bundle_from_response(response))
|
|
}
|
|
}
|
|
|
|
pub(crate) fn bundle_from_response(response: ConfigBundleResponse) -> CloudConfigBundle {
|
|
let config_toml = response
|
|
.config_toml
|
|
.flatten()
|
|
.map(|config_toml| *config_toml)
|
|
.and_then(|config_toml| config_toml.enterprise_managed.flatten())
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(config_fragment_from_delivered)
|
|
.collect();
|
|
let requirements_toml = response
|
|
.requirements_toml
|
|
.flatten()
|
|
.map(|requirements_toml| *requirements_toml)
|
|
.and_then(|requirements_toml| requirements_toml.enterprise_managed.flatten())
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(requirements_fragment_from_delivered)
|
|
.collect();
|
|
|
|
CloudConfigBundle {
|
|
config_toml: CloudConfigTomlBundle {
|
|
enterprise_managed: config_toml,
|
|
},
|
|
requirements_toml: CloudRequirementsTomlBundle {
|
|
enterprise_managed: requirements_toml,
|
|
},
|
|
}
|
|
}
|
|
|
|
fn config_fragment_from_delivered(fragment: DeliveredTomlFragment) -> CloudConfigFragment {
|
|
CloudConfigFragment {
|
|
id: fragment.id,
|
|
name: fragment.name,
|
|
contents: fragment.contents,
|
|
}
|
|
}
|
|
|
|
fn requirements_fragment_from_delivered(
|
|
fragment: DeliveredTomlFragment,
|
|
) -> CloudRequirementsFragment {
|
|
CloudRequirementsFragment {
|
|
id: fragment.id,
|
|
name: fragment.name,
|
|
contents: fragment.contents,
|
|
}
|
|
}
|