mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
cloud-config: simplify cache signing
This commit is contained in:
@@ -20,16 +20,14 @@ use std::time::Duration;
|
||||
use thiserror::Error;
|
||||
use tokio::fs;
|
||||
|
||||
const CLOUD_CONFIG_BUNDLE_CACHE_VERSION: u32 = 1;
|
||||
// Cache v2 signs the complete payload, including managed layers. V1 entries are cache misses.
|
||||
const CLOUD_CONFIG_BUNDLE_CACHE_VERSION: u32 = 2;
|
||||
pub(super) const CLOUD_CONFIG_BUNDLE_CACHE_FILENAME: &str = "cloud-config-bundle-cache.json";
|
||||
const CLOUD_CONFIG_BUNDLE_CACHE_TTL: Duration = Duration::from_secs(60 * 60);
|
||||
const CLOUD_CONFIG_BUNDLE_CACHE_WRITE_HMAC_KEY: &[u8] =
|
||||
b"codex-cloud-config-bundle-cache-v1-6160ae70-bcfd-4ca8-a99b-40f73b3b072e";
|
||||
const CLOUD_CONFIG_BUNDLE_CACHE_READ_HMAC_KEYS: &[&[u8]] =
|
||||
&[CLOUD_CONFIG_BUNDLE_CACHE_WRITE_HMAC_KEY];
|
||||
const MANAGED_LAYERS_CACHE_WRITE_HMAC_KEY: &[u8] =
|
||||
b"codex-cloud-config-bundle-cache-v1-managed-layers-8fe40af0-ec6d-4740-900d-d4bdd771ea2f";
|
||||
const MANAGED_LAYERS_CACHE_READ_HMAC_KEYS: &[&[u8]] = &[MANAGED_LAYERS_CACHE_WRITE_HMAC_KEY];
|
||||
|
||||
type HmacSha256 = Hmac<Sha256>;
|
||||
|
||||
@@ -74,7 +72,7 @@ impl CloudConfigBundleCache {
|
||||
return Err(CacheLoadStatus::CacheParseFailed(err.to_string()));
|
||||
}
|
||||
};
|
||||
let legacy_payload_bytes = match legacy_cache_payload_bytes(&cache_file.signed_payload) {
|
||||
let payload_bytes = match cache_payload_bytes(&cache_file.signed_payload) {
|
||||
Some(payload_bytes) => payload_bytes,
|
||||
None => {
|
||||
return Err(CacheLoadStatus::CacheParseFailed(
|
||||
@@ -82,26 +80,9 @@ impl CloudConfigBundleCache {
|
||||
));
|
||||
}
|
||||
};
|
||||
if !verify_cache_signature(&legacy_payload_bytes, &cache_file.signature) {
|
||||
if !verify_cache_signature(&payload_bytes, &cache_file.signature) {
|
||||
return Err(CacheLoadStatus::CacheSignatureInvalid);
|
||||
}
|
||||
match cache_file.managed_layers_signature.as_deref() {
|
||||
Some(signature) => {
|
||||
let payload_bytes =
|
||||
cache_payload_bytes(&cache_file.signed_payload).ok_or_else(|| {
|
||||
CacheLoadStatus::CacheParseFailed(
|
||||
"failed to serialize cache payload".to_string(),
|
||||
)
|
||||
})?;
|
||||
if !verify_managed_layers_cache_signature(&payload_bytes, signature) {
|
||||
return Err(CacheLoadStatus::CacheManagedLayersSignatureInvalid);
|
||||
}
|
||||
}
|
||||
None if cache_file.signed_payload.bundle.has_managed_layer_buckets() => {
|
||||
return Err(CacheLoadStatus::CacheManagedLayersSignatureInvalid);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
if cache_file.signed_payload.version != CLOUD_CONFIG_BUNDLE_CACHE_VERSION {
|
||||
return Err(CacheLoadStatus::CacheVersionUnsupported(
|
||||
cache_file.signed_payload.version,
|
||||
@@ -136,7 +117,6 @@ impl CloudConfigBundleCache {
|
||||
CacheLoadStatus::CacheReadFailed(_)
|
||||
| CacheLoadStatus::CacheParseFailed(_)
|
||||
| CacheLoadStatus::CacheSignatureInvalid
|
||||
| CacheLoadStatus::CacheManagedLayersSignatureInvalid
|
||||
);
|
||||
|
||||
if warn {
|
||||
@@ -167,17 +147,10 @@ impl CloudConfigBundleCache {
|
||||
account_id,
|
||||
bundle,
|
||||
};
|
||||
let legacy_payload_bytes =
|
||||
legacy_cache_payload_bytes(&signed_payload).ok_or(CloudConfigBundleCacheError)?;
|
||||
let payload_bytes =
|
||||
cache_payload_bytes(&signed_payload).ok_or(CloudConfigBundleCacheError)?;
|
||||
let serialized = serde_json::to_vec_pretty(&CloudConfigBundleCacheFile {
|
||||
signature: sign_cache_payload(&legacy_payload_bytes)
|
||||
.ok_or(CloudConfigBundleCacheError)?,
|
||||
managed_layers_signature: Some(
|
||||
sign_managed_layers_cache_payload(&payload_bytes)
|
||||
.ok_or(CloudConfigBundleCacheError)?,
|
||||
),
|
||||
signature: sign_cache_payload(&payload_bytes).ok_or(CloudConfigBundleCacheError)?,
|
||||
signed_payload,
|
||||
})
|
||||
.map_err(|_| CloudConfigBundleCacheError)?;
|
||||
@@ -207,8 +180,6 @@ pub(super) enum CacheLoadStatus {
|
||||
CacheParseFailed(String),
|
||||
#[error("Cloud config bundle cache failed signature verification.")]
|
||||
CacheSignatureInvalid,
|
||||
#[error("Cloud config bundle cache failed managed-layers signature verification.")]
|
||||
CacheManagedLayersSignatureInvalid,
|
||||
#[error("Ignoring cloud config bundle cache because cached identity is incomplete.")]
|
||||
CacheIdentityIncomplete,
|
||||
#[error("Ignoring cloud config bundle cache for different auth identity.")]
|
||||
@@ -229,8 +200,6 @@ pub(super) struct CloudConfigBundleCacheError;
|
||||
pub(super) struct CloudConfigBundleCacheFile {
|
||||
pub(super) signed_payload: CloudConfigBundleCacheSignedPayload,
|
||||
pub(super) signature: String,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) managed_layers_signature: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
@@ -249,23 +218,10 @@ pub(super) fn cache_payload_bytes(
|
||||
serde_json::to_vec(&payload).ok()
|
||||
}
|
||||
|
||||
pub(super) fn legacy_cache_payload_bytes(
|
||||
payload: &CloudConfigBundleCacheSignedPayload,
|
||||
) -> Option<Vec<u8>> {
|
||||
let mut legacy_payload = payload.clone();
|
||||
legacy_payload.bundle.config_toml.managed_layers = Default::default();
|
||||
legacy_payload.bundle.requirements_toml.managed_layers = Default::default();
|
||||
cache_payload_bytes(&legacy_payload)
|
||||
}
|
||||
|
||||
pub(super) fn sign_cache_payload(payload_bytes: &[u8]) -> Option<String> {
|
||||
sign_cache_payload_with_key(payload_bytes, CLOUD_CONFIG_BUNDLE_CACHE_WRITE_HMAC_KEY)
|
||||
}
|
||||
|
||||
pub(super) fn sign_managed_layers_cache_payload(payload_bytes: &[u8]) -> Option<String> {
|
||||
sign_cache_payload_with_key(payload_bytes, MANAGED_LAYERS_CACHE_WRITE_HMAC_KEY)
|
||||
}
|
||||
|
||||
pub(super) fn verify_cache_signature(payload_bytes: &[u8], signature: &str) -> bool {
|
||||
let signature_bytes = match BASE64_STANDARD.decode(signature) {
|
||||
Ok(signature_bytes) => signature_bytes,
|
||||
@@ -277,17 +233,6 @@ pub(super) fn verify_cache_signature(payload_bytes: &[u8], signature: &str) -> b
|
||||
.any(|key| verify_cache_signature_with_key(payload_bytes, &signature_bytes, key))
|
||||
}
|
||||
|
||||
pub(super) fn verify_managed_layers_cache_signature(payload_bytes: &[u8], signature: &str) -> bool {
|
||||
let signature_bytes = match BASE64_STANDARD.decode(signature) {
|
||||
Ok(signature_bytes) => signature_bytes,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
MANAGED_LAYERS_CACHE_READ_HMAC_KEYS
|
||||
.iter()
|
||||
.any(|key| verify_cache_signature_with_key(payload_bytes, &signature_bytes, key))
|
||||
}
|
||||
|
||||
fn sign_cache_payload_with_key(payload_bytes: &[u8], key: &[u8]) -> Option<String> {
|
||||
let mut mac = HmacSha256::new_from_slice(key).ok()?;
|
||||
mac.update(payload_bytes);
|
||||
|
||||
@@ -35,24 +35,8 @@ fn signed_cache_file(
|
||||
signed_payload: CloudConfigBundleCacheSignedPayload,
|
||||
) -> CloudConfigBundleCacheFile {
|
||||
let payload_bytes = cache_payload_bytes(&signed_payload).expect("payload bytes");
|
||||
let legacy_payload_bytes =
|
||||
legacy_cache_payload_bytes(&signed_payload).expect("legacy payload bytes");
|
||||
CloudConfigBundleCacheFile {
|
||||
signature: sign_cache_payload(&legacy_payload_bytes).expect("signature"),
|
||||
managed_layers_signature: Some(
|
||||
sign_managed_layers_cache_payload(&payload_bytes).expect("managed signature"),
|
||||
),
|
||||
signed_payload,
|
||||
}
|
||||
}
|
||||
|
||||
fn legacy_signed_cache_file(
|
||||
signed_payload: CloudConfigBundleCacheSignedPayload,
|
||||
) -> CloudConfigBundleCacheFile {
|
||||
let payload_bytes = legacy_cache_payload_bytes(&signed_payload).expect("legacy payload bytes");
|
||||
CloudConfigBundleCacheFile {
|
||||
signature: sign_cache_payload(&payload_bytes).expect("signature"),
|
||||
managed_layers_signature: None,
|
||||
signed_payload,
|
||||
}
|
||||
}
|
||||
@@ -102,37 +86,6 @@ fn create_test_cache(codex_home: &Path) -> CloudConfigBundleCache {
|
||||
CloudConfigBundleCache::new(AbsolutePathBuf::resolve_path_against_base(codex_home, "/"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_signature_covers_only_the_legacy_projection() {
|
||||
let payload = valid_managed_signed_payload();
|
||||
let cache_file = signed_cache_file(payload.clone());
|
||||
let legacy_payload_bytes = legacy_cache_payload_bytes(&payload).expect("legacy payload bytes");
|
||||
let payload_bytes = cache_payload_bytes(&payload).expect("payload bytes");
|
||||
let legacy_payload: serde_json::Value =
|
||||
serde_json::from_slice(&legacy_payload_bytes).expect("legacy payload");
|
||||
let full_payload: serde_json::Value =
|
||||
serde_json::from_slice(&payload_bytes).expect("full payload");
|
||||
|
||||
assert!(
|
||||
legacy_payload
|
||||
.pointer("/bundle/config_toml/managed_layers")
|
||||
.is_none()
|
||||
);
|
||||
assert!(
|
||||
full_payload
|
||||
.pointer("/bundle/config_toml/managed_layers")
|
||||
.is_some()
|
||||
);
|
||||
assert!(verify_cache_signature(
|
||||
&legacy_payload_bytes,
|
||||
&cache_file.signature
|
||||
));
|
||||
assert!(!verify_cache_signature(
|
||||
&payload_bytes,
|
||||
&cache_file.signature
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn save_writes_signed_payload_and_loads_for_matching_identity() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
@@ -175,33 +128,22 @@ async fn save_writes_signed_payload_and_loads_for_matching_identity() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_accepts_legacy_cache_without_managed_data() {
|
||||
async fn load_rejects_v1_cache() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let cache = create_test_cache(codex_home.path());
|
||||
let cache_file = legacy_signed_cache_file(valid_signed_payload());
|
||||
let mut signed_payload = valid_signed_payload();
|
||||
signed_payload.version = 1;
|
||||
let cache_file = signed_cache_file(signed_payload);
|
||||
write_cache_file(&cache, &cache_file);
|
||||
|
||||
assert_eq!(
|
||||
cache.load(Some("user-12345"), Some("account-12345")).await,
|
||||
Ok(cache_file.signed_payload)
|
||||
Err(CacheLoadStatus::CacheVersionUnsupported(1))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_requires_full_signature_for_managed_data() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let cache = create_test_cache(codex_home.path());
|
||||
let cache_file = legacy_signed_cache_file(valid_managed_signed_payload());
|
||||
write_cache_file(&cache, &cache_file);
|
||||
|
||||
assert_eq!(
|
||||
cache.load(Some("user-12345"), Some("account-12345")).await,
|
||||
Err(CacheLoadStatus::CacheManagedLayersSignatureInvalid)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_accepts_managed_data_with_both_signatures() {
|
||||
async fn load_accepts_managed_data_with_full_payload_signature() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let cache = create_test_cache(codex_home.path());
|
||||
let cache_file = signed_cache_file(valid_managed_signed_payload());
|
||||
@@ -286,21 +228,21 @@ async fn load_rejects_tampered_managed_data() {
|
||||
|
||||
assert_eq!(
|
||||
cache.load(Some("user-12345"), Some("account-12345")).await,
|
||||
Err(CacheLoadStatus::CacheManagedLayersSignatureInvalid)
|
||||
Err(CacheLoadStatus::CacheSignatureInvalid)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn load_rejects_tampered_managed_layers_signature() {
|
||||
async fn load_rejects_tampered_signature() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let cache = create_test_cache(codex_home.path());
|
||||
let mut cache_file = signed_cache_file(valid_managed_signed_payload());
|
||||
cache_file.managed_layers_signature = Some("tampered".to_string());
|
||||
cache_file.signature = "tampered".to_string();
|
||||
write_cache_file(&cache, &cache_file);
|
||||
|
||||
assert_eq!(
|
||||
cache.load(Some("user-12345"), Some("account-12345")).await,
|
||||
Err(CacheLoadStatus::CacheManagedLayersSignatureInvalid)
|
||||
Err(CacheLoadStatus::CacheSignatureInvalid)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -345,11 +287,13 @@ async fn load_rejects_unsupported_cache_version() {
|
||||
let codex_home = tempdir().expect("tempdir");
|
||||
let cache = create_test_cache(codex_home.path());
|
||||
let mut signed_payload = valid_signed_payload();
|
||||
signed_payload.version = 2;
|
||||
signed_payload.version = CLOUD_CONFIG_BUNDLE_CACHE_VERSION + 1;
|
||||
write_cache_file(&cache, &signed_cache_file(signed_payload));
|
||||
|
||||
assert_eq!(
|
||||
cache.load(Some("user-12345"), Some("account-12345")).await,
|
||||
Err(CacheLoadStatus::CacheVersionUnsupported(2))
|
||||
Err(CacheLoadStatus::CacheVersionUnsupported(
|
||||
CLOUD_CONFIG_BUNDLE_CACHE_VERSION + 1
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -47,15 +47,6 @@ impl CloudConfigBundle {
|
||||
&& requirements_enterprise_managed.is_empty()
|
||||
&& requirements_managed_layers.is_absent()
|
||||
}
|
||||
|
||||
/// Returns true when any managed bucket was present in the backend payload.
|
||||
///
|
||||
/// An explicitly empty bucket still counts as present because it can suppress
|
||||
/// compatibility fallback behavior when the bundle is materialized.
|
||||
pub fn has_managed_layer_buckets(&self) -> bool {
|
||||
!self.config_toml.managed_layers.is_absent()
|
||||
|| !self.requirements_toml.managed_layers.is_absent()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
|
||||
|
||||
@@ -37,7 +37,6 @@ fn bundle_with_explicitly_empty_overlay_is_not_empty() {
|
||||
};
|
||||
|
||||
assert!(!bundle.is_empty());
|
||||
assert!(bundle.has_managed_layer_buckets());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user