Skip to content

Commit 5256be3

Browse files
committed
Bind proxy cache reuse and persisted keys to representation context
1 parent aa9cbb9 commit 5256be3

18 files changed

Lines changed: 604 additions & 25 deletions

‎crates/fluxheim-server/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description = "Internal Fluxheim listener and server boundary types."
1111
default = []
1212
acme = []
1313
auth-request = ["dep:ureq", "sanitization/alloc"]
14-
openbao-cache-encryption = ["dep:serde_json", "dep:ureq", "sanitization/alloc", "ureq/json"]
14+
openbao-cache-encryption = ["dep:ureq", "sanitization/alloc", "ureq/json"]
1515
privacy-mode = ["fluxheim-observability/privacy-mode"]
1616
compression = ["dep:fluxheim-compression"]
1717
compression-brotli = ["compression", "fluxheim-compression/brotli"]
@@ -68,7 +68,7 @@ ring = "0.17.14"
6868
rustls = { version = "0.23.46", optional = true, default-features = false, features = ["std", "tls12", "logging"] }
6969
rustls-native-certs = { version = "0.8.5", optional = true }
7070
sanitization = { version = "2.1.1", features = ["alloc"] }
71-
serde_json = { version = "1.0.154", optional = true }
71+
serde_json = "1.0.154"
7272
sha2 = "0.11.1"
7373
socket2 = { version = "0.6.5", features = ["all"] }
7474
tokio = { version = "1.54.0", default-features = false, features = ["io-util", "macros", "net", "rt", "sync", "time"] }

‎crates/fluxheim-server/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ mod control;
1212
mod http1;
1313
mod http2;
1414
mod listener;
15+
mod native_cache_representation;
1516
mod native_http1;
1617
#[cfg(feature = "acme")]
1718
mod native_http1_acme;
1819
mod native_http1_cache;
1920
mod native_http1_cache_lifecycle;
21+
pub use native_cache_representation::{
22+
native_cache_representation, native_representation_cache_config,
23+
};
2024
mod native_http1_client;
2125
mod native_http1_cors;
2226
mod native_http1_forwarded;
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}

‎crates/fluxheim-server/src/native_http1_cache_recovery_tests.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn documented_filesystem_recovery_currently_leaves_unindexed_files() {
2323
file.set_modified(SystemTime::UNIX_EPOCH).unwrap();
2424
drop(file);
2525

26-
let recovered = NativeDiskCache::from_config(&config).unwrap();
26+
let recovered = NativeDiskCache::try_from_config(&config).unwrap().unwrap();
2727
assert_eq!(recovered.stats().entries, 1);
2828
assert!(recovered.get("valid", |_| None).is_some());
2929
// documented contract: these files remain outside tracked occupancy/eviction.
@@ -44,7 +44,7 @@ fn documented_storage_bin_missing_index_is_cold_without_object_salvage() {
4444
let index = directory.path().join(".fluxheim-storage-bin-index-v1");
4545
assert!(index.exists());
4646
std::fs::remove_file(index).unwrap();
47-
let recovered = NativeDiskCache::from_config(&config).unwrap();
47+
let recovered = NativeDiskCache::try_from_config(&config).unwrap().unwrap();
4848
assert_eq!(recovered.stats().entries, 0);
4949
assert!(recovered.get("valid", |_| None).is_none());
5050
recovered

‎crates/fluxheim-server/src/native_http1_host_router.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ impl NativeHttp1HostRouter {
246246
.map(|router| (router, load_balancer_services, load_balancer_admin_pools));
247247
}
248248
let trusted_sources = trusted_sources_from_config(config)?;
249+
let cache_representation = crate::native_cache_representation(config).map_err(|error| {
250+
NativeHttp1HostRouterConfigError::RouteProxy(NativeHttp1RouteProxyConfigError::Proxy(
251+
error,
252+
))
253+
})?;
249254
#[cfg(feature = "wasm")]
250255
let wasm_registry = NativeWasmHookRegistry::from_config(config).map_err(|_| {
251256
NativeHttp1HostRouterConfigError::RouteProxy(NativeHttp1RouteProxyConfigError::Wasm)
@@ -255,13 +260,14 @@ impl NativeHttp1HostRouter {
255260
let mut wildcard_hosts = Vec::new();
256261

257262
for vhost in &config.vhosts {
258-
let context = NativeRouteProxyBuildContext::new(
263+
let mut context = NativeRouteProxyBuildContext::new(
259264
&config.headers,
260265
Some(&config.compression),
261266
policy,
262267
pool_max_idle,
263268
&trusted_sources,
264269
);
270+
context.cache_representation = Some(cache_representation);
265271
#[cfg(feature = "wasm")]
266272
let context = context.with_wasm_registry(wasm_registry.as_ref());
267273
let proxy = Arc::new(route_proxy_from_config(

‎crates/fluxheim-server/src/native_http1_proxy_builder.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ impl NativeHttp1Proxy {
256256
let native = native
257257
.map(|proxy| {
258258
if Self::proxy_cache_supported_for_proxy(&config.cache, &config.proxy) {
259-
proxy.with_proxy_cache_config_for(&config.cache, "root", None)
259+
proxy.with_proxy_cache_representation(
260+
&config.cache,
261+
"root",
262+
None,
263+
&crate::native_cache_representation(config)?,
264+
)
260265
} else {
261266
Ok(proxy)
262267
}

‎crates/fluxheim-server/src/native_http1_proxy_cache_support.rs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ impl NativeHttp1Proxy {
1818
Self::proxy_cache_supported(cache) && native_slice_cache_supported_for_proxy(cache, proxy)
1919
}
2020

21-
pub fn with_proxy_cache_config(
21+
#[cfg(test)]
22+
pub(crate) fn with_proxy_cache_config(
2223
self,
2324
cache: &CacheConfig,
2425
) -> Result<Self, crate::NativeHttp1ProxyConfigError> {
2526
self.with_proxy_cache_config_for(cache, "native", None)
2627
}
2728

28-
pub fn with_proxy_cache_config_for(
29+
#[cfg(test)]
30+
pub(crate) fn with_proxy_cache_config_for(
2931
mut self,
3032
cache: &CacheConfig,
3133
vhost: &str,
@@ -34,6 +36,18 @@ impl NativeHttp1Proxy {
3436
self.cache = NativeProxyMemoryCache::try_from_config_with_metrics(cache, vhost, route)?;
3537
Ok(self)
3638
}
39+
40+
pub(crate) fn with_proxy_cache_representation(
41+
mut self,
42+
cache: &CacheConfig,
43+
vhost: &str,
44+
route: Option<&str>,
45+
representation: &[u8; 32],
46+
) -> Result<Self, crate::NativeHttp1ProxyConfigError> {
47+
let cache = crate::native_representation_cache_config(cache, representation, vhost, route);
48+
self.cache = NativeProxyMemoryCache::try_from_config_with_metrics(&cache, vhost, route)?;
49+
Ok(self)
50+
}
3751
}
3852

3953
fn native_slice_cache_supported_for_proxy(

0 commit comments

Comments
 (0)