|
| 1 | +use std::io::{self, Write}; |
| 2 | + |
| 3 | +use fluxheim_config::{CacheConfig, Config}; |
| 4 | +use sha2::{Digest, Sha256}; |
| 5 | + |
| 6 | +use crate::NativeHttp1ProxyConfigError; |
| 7 | + |
| 8 | +/// Stable identity for configuration-owned representation selection. Operational |
| 9 | +/// listeners and presentation-only cache headers do not affect origin content. |
| 10 | +pub fn native_cache_representation( |
| 11 | + config: &Config, |
| 12 | +) -> Result<[u8; 32], NativeHttp1ProxyConfigError> { |
| 13 | + let caching = config.cache.enabled |
| 14 | + || config.vhosts.iter().any(|vhost| { |
| 15 | + vhost.cache.enabled |
| 16 | + || vhost |
| 17 | + .routes |
| 18 | + .iter() |
| 19 | + .any(|route| route.cache.as_ref().is_some_and(|cache| cache.enabled)) |
| 20 | + }); |
| 21 | + if caching |
| 22 | + && config.wasm.enabled |
| 23 | + && config.wasm.plugins.iter().any(|plugin| { |
| 24 | + !plugin.sha256.as_ref().is_some_and(|digest| { |
| 25 | + digest.len() == 64 && digest.bytes().all(|byte| byte.is_ascii_hexdigit()) |
| 26 | + }) |
| 27 | + }) |
| 28 | + { |
| 29 | + // A pathname alone cannot identify code which modifies representations. |
| 30 | + return Err(NativeHttp1ProxyConfigError::CachePolicy); |
| 31 | + } |
| 32 | + let mut normalized = config.clone(); |
| 33 | + normalized.admin = Default::default(); |
| 34 | + normalized.logging = Default::default(); |
| 35 | + normalized.logging.access = config.logging.access.clone(); |
| 36 | + normalized.metrics = Default::default(); |
| 37 | + normalized.server.listen = Default::default(); |
| 38 | + normalized.server.process = Default::default(); |
| 39 | + normalize_cache(&mut normalized.cache); |
| 40 | + for vhost in &mut normalized.vhosts { |
| 41 | + normalize_cache(&mut vhost.cache); |
| 42 | + for route in &mut vhost.routes { |
| 43 | + if let Some(cache) = &mut route.cache { |
| 44 | + normalize_cache(cache); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + let mut writer = DigestWriter(Sha256::new()); |
| 49 | + writer.0.update(b"fluxheim-representation-v1\0"); |
| 50 | + // Configuration maps are ordered. Stream into the digest instead of keeping |
| 51 | + // a serialized copy of injected credentials in another allocation. |
| 52 | + serde_json::to_writer(&mut writer, &normalized) |
| 53 | + .map_err(|_| NativeHttp1ProxyConfigError::CacheInitialization)?; |
| 54 | + Ok(writer.0.finalize().into()) |
| 55 | +} |
| 56 | + |
| 57 | +fn normalize_cache(cache: &mut CacheConfig) { |
| 58 | + // Storage location, peer topology and admission tuning do not identify the |
| 59 | + // representation. The caller's explicit namespace is bound separately. |
| 60 | + *cache = CacheConfig::default(); |
| 61 | +} |
| 62 | + |
| 63 | +/// Apply the same identity to serving, offline inspection and exact purge keys. |
| 64 | +pub fn native_representation_cache_config( |
| 65 | + cache: &CacheConfig, |
| 66 | + representation: &[u8; 32], |
| 67 | + vhost: &str, |
| 68 | + route: Option<&str>, |
| 69 | +) -> CacheConfig { |
| 70 | + let mut hash = Sha256::new(); |
| 71 | + hash.update(b"fluxheim-cache-context-v1\0"); |
| 72 | + hash.update(representation); |
| 73 | + for value in [Some(vhost), route, cache.key_namespace.as_deref()] { |
| 74 | + hash.update([u8::from(value.is_some())]); |
| 75 | + let value = value.unwrap_or_default().as_bytes(); |
| 76 | + hash.update((value.len() as u64).to_be_bytes()); |
| 77 | + hash.update(value); |
| 78 | + } |
| 79 | + let mut scoped = cache.clone(); |
| 80 | + let hex: String = hash |
| 81 | + .finalize() |
| 82 | + .iter() |
| 83 | + .map(|byte| format!("{byte:02x}")) |
| 84 | + .collect(); |
| 85 | + scoped.key_namespace = Some(format!("context-v1-{hex}")); |
| 86 | + scoped |
| 87 | +} |
| 88 | + |
| 89 | +struct DigestWriter(Sha256); |
| 90 | + |
| 91 | +impl Write for DigestWriter { |
| 92 | + fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { |
| 93 | + self.0.update(bytes); |
| 94 | + Ok(bytes.len()) |
| 95 | + } |
| 96 | + |
| 97 | + fn flush(&mut self) -> io::Result<()> { |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + |
| 106 | + fn fixture() -> Config { |
| 107 | + serde_json::from_value(serde_json::json!({ |
| 108 | + "vhosts": [{"name": "tenant", "hosts": ["tenant.test"], "routes": [{ |
| 109 | + "name": "assets", "path_prefix": "/", "proxy": {"upstream": "127.0.0.1:9001", "upstream_tls": false} |
| 110 | + }]}] |
| 111 | + })).unwrap() |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn representation_binds_origins_headers_rewrites_auth_access_and_wasm() { |
| 116 | + let original = fixture(); |
| 117 | + let identity = native_cache_representation(&original).unwrap(); |
| 118 | + for mutation in 0..8 { |
| 119 | + let mut changed = original.clone(); |
| 120 | + let route = &mut changed.vhosts[0].routes[0]; |
| 121 | + match mutation { |
| 122 | + 0 => route.proxy.as_mut().unwrap().upstream = Some("127.0.0.1:9002".into()), |
| 123 | + 1 => route.rewrite_template = Some("/private{path}".into()), |
| 124 | + 2 => { |
| 125 | + route |
| 126 | + .headers |
| 127 | + .request |
| 128 | + .set |
| 129 | + .insert("authorization".into(), "secret-canary".into()); |
| 130 | + } |
| 131 | + 3 => { |
| 132 | + route.proxy.as_mut().unwrap().auth_request.url = |
| 133 | + Some("https://auth.example/check".into()) |
| 134 | + } |
| 135 | + 4 => route.access.require_client_cert = true, |
| 136 | + 5 => { |
| 137 | + changed |
| 138 | + .headers |
| 139 | + .request |
| 140 | + .set |
| 141 | + .insert("x-tenant".into(), "new-tenant".into()); |
| 142 | + } |
| 143 | + 6 => changed.server.trusted_proxies = vec!["127.0.0.1".into()], |
| 144 | + _ => changed.wasm.enabled = true, |
| 145 | + } |
| 146 | + assert_ne!( |
| 147 | + identity, |
| 148 | + native_cache_representation(&changed).unwrap(), |
| 149 | + "mutation {mutation}" |
| 150 | + ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn representation_is_stable_and_namespace_is_bounded_and_scope_bound() { |
| 156 | + let original = fixture(); |
| 157 | + let identity = native_cache_representation(&original).unwrap(); |
| 158 | + let mut operational = original.clone(); |
| 159 | + operational.admin.enabled = true; |
| 160 | + operational.vhosts[0].cache.status_header = Some("x-next-cache".into()); |
| 161 | + operational.vhosts[0].cache.disk.path = Some("/different/root".into()); |
| 162 | + assert_eq!(identity, native_cache_representation(&operational).unwrap()); |
| 163 | + let cache = CacheConfig::default(); |
| 164 | + let scoped = native_representation_cache_config(&cache, &identity, "tenant", None); |
| 165 | + assert_eq!(scoped.key_namespace.as_ref().unwrap().len(), 75); |
| 166 | + assert_ne!( |
| 167 | + scoped.key_namespace, |
| 168 | + native_representation_cache_config(&cache, &identity, "tenant", Some("")).key_namespace |
| 169 | + ); |
| 170 | + let mut explicit = cache.clone(); |
| 171 | + explicit.key_namespace = Some("cutover".into()); |
| 172 | + assert_ne!( |
| 173 | + scoped.key_namespace, |
| 174 | + native_representation_cache_config(&explicit, &identity, "tenant", None).key_namespace |
| 175 | + ); |
| 176 | + let roundtrip: Config = |
| 177 | + serde_json::from_slice(&serde_json::to_vec(&original).unwrap()).unwrap(); |
| 178 | + assert_eq!(identity, native_cache_representation(&roundtrip).unwrap()); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn cached_wasm_requires_a_content_pin_and_pin_changes_invalidate_identity() { |
| 183 | + let mut config = fixture(); |
| 184 | + config.cache.enabled = true; |
| 185 | + config.wasm.enabled = true; |
| 186 | + config.wasm.plugins.push( |
| 187 | + serde_json::from_value(serde_json::json!({ |
| 188 | + "name": "headers", "path": "/private/plugins/headers.wasm" |
| 189 | + })) |
| 190 | + .unwrap(), |
| 191 | + ); |
| 192 | + assert!(native_cache_representation(&config).is_err()); |
| 193 | + config.wasm.plugins[0].sha256 = Some("a".repeat(64)); |
| 194 | + let first = native_cache_representation(&config).unwrap(); |
| 195 | + config.wasm.plugins[0].sha256 = Some("b".repeat(64)); |
| 196 | + assert_ne!(first, native_cache_representation(&config).unwrap()); |
| 197 | + } |
| 198 | +} |
0 commit comments