diff --git a/crates/openshell-supervisor-network/README.md b/crates/openshell-supervisor-network/README.md index 92257fb24a..5bed7184b8 100644 --- a/crates/openshell-supervisor-network/README.md +++ b/crates/openshell-supervisor-network/README.md @@ -31,6 +31,21 @@ Without a Workload API socket, token grants fail closed. A process hosting several instances serves `/v1/denials` from a per-instance `SandboxInstanceDenialLog` instead of the shared shorthand log files. +## Policy DNS expiry + +Synthetic identities remain allocated after their DNS mappings expire. New +transparent connections lazily refresh expired, policy-backed mappings through +the same trusted resolver and destination-validation path as sandbox DNS +queries. Refreshes for one address share a lock; failed refreshes back off for +one second and never authorize expired upstream addresses. +`PolicyDnsService::lookup_for_connect` owns refresh coordination and returns +the resulting mapping; the underlying store stays synchronous. Observation-only +mappings, wrong ports, and mappings from a previous policy generation cannot refresh. +Refresh eligibility uses the authored ports, independently of which ports the +latest DNS addresses satisfied. Dispatch reauthorizes mapped connections and +rebuilds their connectors from current pins instead of retaining staged pins +across a refresh. + ## Reserved destinations `preauthorize_transparent_open` matches a staged TCP open against the reserved diff --git a/crates/openshell-supervisor-network/src/policy_dns/mod.rs b/crates/openshell-supervisor-network/src/policy_dns/mod.rs index 903f29db2b..ab9c3ace26 100644 --- a/crates/openshell-supervisor-network/src/policy_dns/mod.rs +++ b/crates/openshell-supervisor-network/src/policy_dns/mod.rs @@ -44,7 +44,9 @@ use openshell_ocsf::{ NetworkActivityBuilder, SeverityId, StateId, StatusId, }; use sha2::{Digest, Sha256}; -use std::sync::Arc; +use std::collections::BTreeMap; +use std::net::IpAddr; +use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; pub(crate) const MIN_MAPPING_TTL: Duration = Duration::from_secs(1); @@ -55,7 +57,7 @@ pub(crate) const POLICY_LOCAL_ADDRESS: std::net::Ipv4Addr = std::net::Ipv4Addr:: #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct SyntheticAnswer { - pub(crate) address: std::net::IpAddr, + pub(crate) address: IpAddr, pub(crate) ttl: Duration, pub(crate) mapping_id: uuid::Uuid, pub(crate) mapping_generation: u64, @@ -86,11 +88,24 @@ pub(crate) enum PolicyDnsError { /// /// No socket is bound by this type. A later runtime adapter owns listener and /// namespace lifecycle and calls the bounded wire helpers in this module. -pub(crate) struct PolicyDnsService { +pub(crate) struct PolicyDnsService { policy: Arc, resolver: R, store: Arc, - trusted_host_gateway: Option, + trusted_host_gateway: Option, + refreshes: Mutex>, +} + +type RefreshLock = Arc>>; + +#[derive(Debug, thiserror::Error)] +pub(crate) enum ConnectLookupError { + #[error("transparent destination mapping is unavailable: {0}")] + Mapping(#[from] MappingLookupError), + #[error("transparent TCP mapping refresh failed: {0}")] + Refresh(#[from] PolicyDnsError), + #[error("transparent TCP mapping refresh recently failed")] + Backoff, } impl PolicyDnsService { @@ -98,14 +113,67 @@ impl PolicyDnsService { policy: Arc, resolver: R, store: Arc, - trusted_host_gateway: Option, + trusted_host_gateway: Option, ) -> Self { Self { policy, resolver, store, trusted_host_gateway, + refreshes: Mutex::new(BTreeMap::new()), + } + } + + /// Return connection intent, refreshing only expired policy-backed mappings. + /// The caller must still authorize the process and pin the current mapping. + pub(crate) async fn lookup_for_connect( + &self, + address: IpAddr, + port: u16, + ) -> Result { + let generation = self.policy.current_generation(); + let lookup = || { + self.store + .lookup_intent(address, port, generation, Instant::now()) + }; + match lookup() { + Err(MappingLookupError::Expired) => {} + result => return result.map_err(Into::into), + } + let record = self.store.expired_for_connect(address, port, generation)?; + // Only known, eligible allocations acquire entries, bounded by the + // store's allocation budget. Concurrent connects share one refresh. + let refresh = self + .refreshes + .lock() + .map_err(|_| MappingLookupError::LockPoisoned)? + .entry(address) + .or_default() + .clone(); + let mut failed_at = refresh.lock().await; + match lookup() { + Err(MappingLookupError::Expired) => {} + result => return result.map_err(Into::into), + } + if self.policy.current_generation() != generation { + return Err(MappingLookupError::StalePolicy.into()); + } + if failed_at.is_some_and(|failed| failed.elapsed() < MIN_MAPPING_TTL) { + return Err(ConnectLookupError::Backoff); + } + let answer = self + .answer_query( + record.normalized_name.as_str(), + record.family, + Instant::now(), + ) + .await; + *failed_at = answer.as_ref().err().map(|_| Instant::now()); + let answer = answer?; + if (answer.address, answer.policy_generation) != (record.synthetic_address, generation) { + return Err(MappingLookupError::StalePolicy.into()); } + lookup().map_err(Into::into) } pub(crate) async fn answer_query( @@ -114,6 +182,7 @@ impl PolicyDnsService { family: AddressFamily, now: Instant, ) -> Result { + let started = Instant::now(); let normalized_name = NormalizedName::parse(raw_name).map_err(|_| PolicyDnsError::InvalidName)?; if normalized_name.as_str() == crate::policy_local::POLICY_LOCAL_HOST { @@ -190,7 +259,14 @@ impl PolicyDnsService { } }; let ttl = clamp_mapping_ttl(trusted_answer.ttl); + // Start the lease after resolution: a TTL-zero answer must still be + // usable when the trusted resolver takes longer than the TTL floor. + let now = now + started.elapsed(); let allocation_identity = allocation_identity(&eligible); + let eligible_ports = eligible + .iter() + .flat_map(|endpoint| endpoint.ports.iter().copied()) + .collect(); let mut contracts = Vec::new(); for endpoint in eligible { for port in endpoint.ports { @@ -228,6 +304,7 @@ impl PolicyDnsService { allocation_identity, policy_generation: snapshot.generation, ttl, + eligible_ports, contracts, }; let record = self.publish_guarded( @@ -358,7 +435,7 @@ struct EligibleEndpoint { fn eligible_endpoints( endpoints: &[crate::opa::MatchedEndpoint], name: &NormalizedName, - trusted_host_gateway: Option, + trusted_host_gateway: Option, ) -> Result, PolicyDnsError> { let mut eligible = Vec::new(); for endpoint in endpoints { @@ -764,6 +841,277 @@ landlock: { compatibility: best_effort } process: { run_as_user: sandbox, run_as_group: sandbox } "; + struct ScriptedResolver { + answers: Mutex>>, + calls: AtomicUsize, + } + + impl TrustedResolver for ScriptedResolver { + async fn resolve( + &self, + _: &NormalizedName, + _: AddressFamily, + ) -> Result { + self.calls.fetch_add(1, Ordering::SeqCst); + self.answers + .lock() + .unwrap() + .pop_front() + .expect("unexpected DNS query") + } + } + + fn dns_answer(addresses: &[&str]) -> TrustedAnswer { + TrustedAnswer { + addresses: addresses.iter().map(|ip| ip.parse().unwrap()).collect(), + ttl: Duration::ZERO, + } + } + + fn scripted_service( + policy: &str, + answers: Vec>, + ) -> PolicyDnsService { + let base = service(policy, vec![]); + PolicyDnsService::new( + base.policy, + ScriptedResolver { + answers: Mutex::new(answers.into()), + calls: AtomicUsize::new(0), + }, + base.store, + None, + ) + } + + async fn expired_answer(service: &PolicyDnsService) -> SyntheticAnswer { + service + .answer_query( + "db.example", + AddressFamily::Ipv4, + Instant::now().checked_sub(Duration::from_mins(1)).unwrap(), + ) + .await + .unwrap() + } + + #[tokio::test] + async fn expired_mapping_refresh_validates_backs_off_and_recovers() { + for (case, failure, sweep) in [ + ("NXDOMAIN", Err(resolver::ResolveError::NxDomain), false), + ("forbidden address", Ok(dns_answer(&["127.0.0.1"])), true), + ] { + let service = scripted_service( + BASE_POLICY, + vec![ + Ok(dns_answer(&["8.8.8.8"])), + failure, + Ok(dns_answer(&["8.8.4.4"])), + ], + ); + let answer = expired_answer(&service).await; + if sweep { + assert_eq!(service.store.expire(Instant::now()).unwrap(), 1); + } + assert!( + matches!( + service.lookup_for_connect(answer.address, 5432).await, + Err(ConnectLookupError::Refresh(_)) + ), + "{case}" + ); + assert!(matches!( + service.store.lookup( + answer.address, + 5432, + answer.policy_generation, + Instant::now() + ), + Err(MappingLookupError::Expired) + )); + assert!(matches!( + service.lookup_for_connect(answer.address, 5432).await, + Err(ConnectLookupError::Backoff) + )); + assert_eq!(service.resolver.calls.load(Ordering::SeqCst), 2); + + tokio::time::sleep(Duration::from_millis(1100)).await; + let recovered = service + .lookup_for_connect(answer.address, 5432) + .await + .unwrap(); + assert_eq!(recovered.record.synthetic_address, answer.address); + assert_ne!(recovered.record.mapping_id, answer.mapping_id); + assert_eq!( + recovered.record.contracts[0].pinned_addresses, + ["8.8.4.4".parse::().unwrap()] + ); + let reused = service + .lookup_for_connect(answer.address, 5432) + .await + .unwrap(); + assert_eq!(reused.record.mapping_id, recovered.record.mapping_id); + assert_eq!(service.resolver.calls.load(Ordering::SeqCst), 3); + } + } + + #[tokio::test] + async fn expired_partial_mapping_recovers_every_policy_eligible_port() { + let policy = BASE_POLICY.replace( + "- { host: db.example, port: 5432, protocol: tcp }", + "- { host: db.example, port: 5432, protocol: tcp, allowed_ips: [8.8.8.8/32] }\n - { host: db.example, port: 443, protocol: tcp, allowed_ips: [8.8.4.4/32] }", + ); + let service = scripted_service( + &policy, + vec![ + Ok(dns_answer(&["8.8.8.8", "8.8.4.4"])), + Ok(dns_answer(&["8.8.8.8"])), + Ok(dns_answer(&["8.8.8.8", "8.8.4.4"])), + ], + ); + let answer = expired_answer(&service).await; + service + .lookup_for_connect(answer.address, 5432) + .await + .unwrap(); + assert!(matches!( + service.lookup_for_connect(answer.address, 443).await, + Err(ConnectLookupError::Mapping( + MappingLookupError::PortMismatch + )) + )); + tokio::time::sleep(Duration::from_millis(1100)).await; + assert_eq!(service.store.expire(Instant::now()).unwrap(), 1); + let recovered = service + .lookup_for_connect(answer.address, 443) + .await + .unwrap(); + assert_eq!( + recovered + .pinned_addresses_for(&[PolicyEndpointId { + policy_name: "database".into(), + endpoint_index: 1 + }]) + .unwrap(), + ["8.8.4.4".parse::().unwrap()] + ); + assert_eq!(service.resolver.calls.load(Ordering::SeqCst), 3); + } + + #[tokio::test] + async fn connect_lookup_rejects_ineligible_refresh_without_dns() { + for case in ["wrong port", "expired observation", "stale generation"] { + let service = service(BASE_POLICY, vec!["8.8.8.8".parse().unwrap()]); + let answer = expired_answer(&service).await; + let (address, port, expected) = match case { + "wrong port" => (answer.address, 3306, MappingLookupError::PortMismatch), + "expired observation" => { + let observed = service + .answer_query( + "unknown.example", + AddressFamily::Ipv4, + Instant::now().checked_sub(Duration::from_mins(1)).unwrap(), + ) + .await + .unwrap(); + (observed.address, 5432, MappingLookupError::Expired) + } + _ => { + service + .policy + .reload( + include_str!("../../data/sandbox-policy.rego"), + "network_policies: {}\n", + ) + .unwrap(); + (answer.address, 5432, MappingLookupError::StalePolicy) + } + }; + let result = service.lookup_for_connect(address, port).await; + assert!( + matches!(result, Err(ConnectLookupError::Mapping(error)) if error == expected), + "{case}: {result:?}" + ); + assert_eq!(service.resolver.calls.load(Ordering::SeqCst), 1, "{case}"); + } + } + + #[tokio::test] + async fn concurrent_expired_connections_share_one_refresh() { + let base = service(BASE_POLICY, vec!["8.8.8.8".parse().unwrap()]); + let answer = expired_answer(&base).await; + let started = Arc::new(Notify::new()); + let release = Arc::new(Notify::new()); + let service = Arc::new(PolicyDnsService::new( + base.policy, + BlockingResolver { + started: started.clone(), + release: release.clone(), + }, + base.store, + None, + )); + let mut connects = Vec::new(); + for _ in 0..16 { + let service = service.clone(); + connects.push(tokio::spawn(async move { + service.lookup_for_connect(answer.address, 5432).await + })); + } + started.notified().await; + // Resolution takes longer than the fresh answer's one-second floor. + tokio::time::sleep(Duration::from_millis(1100)).await; + release.notify_one(); + tokio::time::timeout(Duration::from_secs(5), async { + let mut mapping_id = None; + for connect in connects { + let mapping = connect.await.unwrap().unwrap(); + assert_eq!( + *mapping_id.get_or_insert(mapping.record.mapping_id), + mapping.record.mapping_id + ); + } + }) + .await + .expect("one DNS response must satisfy all waiting connects"); + } + + #[tokio::test] + async fn policy_revocation_during_expired_mapping_refresh_fails_closed() { + let base = service(BASE_POLICY, vec!["8.8.8.8".parse().unwrap()]); + let answer = expired_answer(&base).await; + let started = Arc::new(Notify::new()); + let release = Arc::new(Notify::new()); + let service = Arc::new(PolicyDnsService::new( + base.policy, + BlockingResolver { + started: started.clone(), + release: release.clone(), + }, + base.store, + None, + )); + let refreshing = service.clone(); + let connect = + tokio::spawn(async move { refreshing.lookup_for_connect(answer.address, 5432).await }); + started.notified().await; + service + .policy + .reload( + include_str!("../../data/sandbox-policy.rego"), + "network_policies: {}\n", + ) + .unwrap(); + release.notify_one(); + assert!(connect.await.unwrap().is_err()); + assert!( + service + .lookup_for_connect(answer.address, 5432) + .await + .is_err() + ); + } + #[tokio::test] async fn stages_unknown_name_without_upstream_resolution() { let service = service(BASE_POLICY, vec!["8.8.8.8".parse().unwrap()]); @@ -887,7 +1235,7 @@ process: { run_as_user: sandbox, run_as_group: sandbox } .expect("policy-backed names keep their reserved capacity"); } - struct OcsfCapture(Arc>>); + struct OcsfCapture(Arc>>); impl tracing_subscriber::Layer for OcsfCapture { fn on_event(&self, _: &tracing::Event<'_>, _: tracing_subscriber::layer::Context<'_, S>) { @@ -928,7 +1276,7 @@ process: { run_as_user: sandbox, run_as_group: sandbox } } let service = service(BASE_POLICY, Vec::new()); - let events = Arc::new(std::sync::Mutex::new(Vec::new())); + let events = Arc::new(Mutex::new(Vec::new())); let _subscriber = tracing::subscriber::set_default( tracing_subscriber::registry().with(OcsfCapture(Arc::clone(&events))), ); @@ -1434,7 +1782,7 @@ process: { run_as_user: sandbox, run_as_group: sandbox } self.release.notified().await; Ok(TrustedAnswer { addresses: vec!["8.8.8.8".parse().unwrap()], - ttl: Duration::from_secs(10), + ttl: Duration::ZERO, }) } } diff --git a/crates/openshell-supervisor-network/src/policy_dns/resolver.rs b/crates/openshell-supervisor-network/src/policy_dns/resolver.rs index 34f784ade5..789f720235 100644 --- a/crates/openshell-supervisor-network/src/policy_dns/resolver.rs +++ b/crates/openshell-supervisor-network/src/policy_dns/resolver.rs @@ -78,13 +78,12 @@ pub(crate) enum ResolveError { CnameLimit, } -#[allow(async_fn_in_trait)] pub(crate) trait TrustedResolver: Send + Sync { - async fn resolve( + fn resolve( &self, name: &NormalizedName, family: AddressFamily, - ) -> Result; + ) -> impl Future> + Send; } /// A DNS client pinned to one operator-supplied upstream socket address. diff --git a/crates/openshell-supervisor-network/src/policy_dns/runtime.rs b/crates/openshell-supervisor-network/src/policy_dns/runtime.rs index d7888f991e..809a26f50f 100644 --- a/crates/openshell-supervisor-network/src/policy_dns/runtime.rs +++ b/crates/openshell-supervisor-network/src/policy_dns/runtime.rs @@ -87,7 +87,7 @@ impl PolicyDnsRuntimeConfig { } pub(crate) struct PolicyDnsRuntime { - pub(crate) store: Arc, + pub(crate) service: Arc, tasks: Vec>, } @@ -112,6 +112,7 @@ impl PolicyDnsRuntime { store.clone(), trusted_host_gateway, )); + let dns_service = service.clone(); let task = tokio::spawn(async move { if engine_ready.wait_for(|ready| *ready).await.is_err() { return; @@ -127,7 +128,7 @@ impl PolicyDnsRuntime { }; let source = source.clone(); accepts.push(accept_mediated_dns(source).boxed()); - let service = service.clone(); + let service = dns_service.clone(); tokio::spawn(async move { let timing = query.timing.clone(); let response = match query.transport { @@ -161,7 +162,7 @@ impl PolicyDnsRuntime { }); } }); - let expiry_store = store.clone(); + let expiry_store = store; let expiry_task = tokio::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs(1)); loop { @@ -178,7 +179,7 @@ impl PolicyDnsRuntime { .build() ); Ok(Self { - store, + service, tasks: vec![task, expiry_task], }) } @@ -238,6 +239,7 @@ impl PolicyDnsRuntime { }); let mut tcp_engine_ready = engine_ready; + let tcp_service = service.clone(); let tcp_task = tokio::spawn(async move { if tcp_engine_ready.wait_for(|ready| *ready).await.is_err() { return; @@ -247,7 +249,7 @@ impl PolicyDnsRuntime { break; }; set_tcp_nodelay_best_effort(&stream); - let service = service.clone(); + let service = tcp_service.clone(); tokio::spawn(async move { // DNS-over-TCP connections may carry multiple sequential // length-prefixed messages. libc commonly reuses one @@ -275,7 +277,7 @@ impl PolicyDnsRuntime { }); } }); - let expiry_store = store.clone(); + let expiry_store = store; let expiry_task = tokio::spawn(async move { let mut interval = tokio::time::interval(std::time::Duration::from_secs(1)); loop { @@ -293,7 +295,7 @@ impl PolicyDnsRuntime { .build() ); Ok(Self { - store, + service, tasks: vec![udp_task, tcp_task, expiry_task], }) } diff --git a/crates/openshell-supervisor-network/src/policy_dns/store.rs b/crates/openshell-supervisor-network/src/policy_dns/store.rs index 62fe1c19bf..c38b278a6e 100644 --- a/crates/openshell-supervisor-network/src/policy_dns/store.rs +++ b/crates/openshell-supervisor-network/src/policy_dns/store.rs @@ -46,6 +46,8 @@ pub(crate) struct PublishRequest { pub(crate) allocation_identity: [u8; 32], pub(crate) policy_generation: u64, pub(crate) ttl: Duration, + /// Authored ports eligible for refresh, before DNS address filtering. + pub(crate) eligible_ports: BTreeSet, pub(crate) contracts: Vec, } @@ -59,6 +61,7 @@ pub(crate) struct ResolvedEndpointRecord { pub(crate) mapping_id: Uuid, pub(crate) created_at: Instant, pub(crate) expires_at: Instant, + pub(crate) eligible_ports: BTreeSet, pub(crate) contracts: Vec, } @@ -260,13 +263,14 @@ struct Publication { key: AllocationKey, policy_generation: u64, ttl: Duration, + eligible_ports: BTreeSet, contracts: Vec, } struct StoreState { records: BTreeMap, allocations: BTreeMap, - expired_allocations: BTreeSet, + expired_allocations: BTreeMap, next_ipv4: u32, end_ipv4: u32, next_ipv6: u128, @@ -291,7 +295,7 @@ impl ResolvedEndpointStore { state: RwLock::new(StoreState { records: BTreeMap::new(), allocations: BTreeMap::new(), - expired_allocations: BTreeSet::new(), + expired_allocations: BTreeMap::new(), next_ipv4, end_ipv4, next_ipv6, @@ -304,6 +308,34 @@ impl ResolvedEndpointStore { } } + /// Recover refresh identity without authorizing expired upstream addresses. + pub(super) fn expired_for_connect( + &self, + address: IpAddr, + port: u16, + generation: u64, + ) -> Result { + let state = self + .state + .read() + .map_err(|_| MappingLookupError::LockPoisoned)?; + let record = state + .records + .get(&address) + .or_else(|| state.expired_allocations.get(&address)) + .ok_or(MappingLookupError::Missing)?; + if record.policy_generation != generation { + return Err(MappingLookupError::StalePolicy); + } + if record.is_observation() { + return Err(MappingLookupError::Expired); + } + if !record.eligible_ports.contains(&port) { + return Err(MappingLookupError::PortMismatch); + } + Ok(record.clone()) + } + pub(crate) fn publish( &self, request: PublishRequest, @@ -314,9 +346,11 @@ impl ResolvedEndpointStore { return Err(PublishError::StalePolicy); } if request.ttl.is_zero() + || request.eligible_ports.contains(&0) || request.contracts.is_empty() || request.contracts.iter().any(|contract| { contract.port == 0 + || !request.eligible_ports.contains(&contract.port) || contract.pinned_addresses.is_empty() || contract .pinned_addresses @@ -336,6 +370,7 @@ impl ResolvedEndpointStore { }, policy_generation: request.policy_generation, ttl: request.ttl, + eligible_ports: request.eligible_ports, contracts: request.contracts, }, now, @@ -365,6 +400,7 @@ impl ResolvedEndpointStore { }, policy_generation, ttl: super::MAX_MAPPING_TTL, + eligible_ports: BTreeSet::new(), contracts: Vec::new(), }, now, @@ -390,6 +426,7 @@ impl ResolvedEndpointStore { key, policy_generation, ttl, + eligible_ports, contracts, } = publication; let family = key.family; @@ -461,6 +498,7 @@ impl ResolvedEndpointStore { mapping_id: Uuid::new_v4(), created_at: now, expires_at: now + ttl, + eligible_ports, contracts, }; state.expired_allocations.remove(&synthetic_address); @@ -534,8 +572,9 @@ impl ResolvedEndpointStore { .filter_map(|(address, record)| (now >= record.expires_at).then_some(*address)) .collect::>(); for address in &expired { - state.records.remove(address); - state.expired_allocations.insert(*address); + if let Some(record) = state.records.remove(address) { + state.expired_allocations.insert(*address, record); + } } Ok(expired.len()) } @@ -548,7 +587,7 @@ fn live_record( now: Instant, ) -> Result<&ResolvedEndpointRecord, MappingLookupError> { let Some(record) = state.records.get(&synthetic_address) else { - return if state.expired_allocations.contains(&synthetic_address) { + return if state.expired_allocations.contains_key(&synthetic_address) { Err(MappingLookupError::Expired) } else { Err(MappingLookupError::Missing) @@ -622,6 +661,7 @@ mod tests { allocation_identity: [1; 32], policy_generation: generation, ttl, + eligible_ports: BTreeSet::from([5432]), contracts: vec![ResolvedPortContract { endpoint_id: PolicyEndpointId { policy_name: "database".to_string(), @@ -1039,6 +1079,7 @@ mod tests { let mut other_port = second.clone(); other_port.port = 443; other_port.pinned_addresses = vec!["203.0.113.10".parse().unwrap()]; + request.eligible_ports.insert(443); request .contracts .extend([second.clone(), unrelated.clone(), other_port]); diff --git a/crates/openshell-supervisor-network/src/proxy.rs b/crates/openshell-supervisor-network/src/proxy.rs index a72c23c661..96da43aae1 100644 --- a/crates/openshell-supervisor-network/src/proxy.rs +++ b/crates/openshell-supervisor-network/src/proxy.rs @@ -17,8 +17,11 @@ use openshell_sandbox_backend::test_support as rpc_test; use crate::identity::{BinaryIdentityCache, SuppliedIdentityError}; use crate::l7::tls::ProxyTlsState; use crate::opa::{NetworkAction, OpaEngine, PolicyGenerationGuard}; +#[cfg(test)] +use crate::policy_dns::SocketTrustedResolver; use crate::policy_dns::{ - MappingLookup, MappingLookupError, PolicyEndpointId, ResolvedEndpointStore, + ConnectLookupError, MappingLookup, MappingLookupError, PolicyDnsService, PolicyEndpointId, + ResolvedEndpointStore, }; use crate::policy_local::{POLICY_LOCAL_HOST, PolicyLocalContext}; use crate::telemetry::egress_emit; @@ -311,7 +314,7 @@ impl ProxyHandle { upstream_proxy_args: &upstream_proxy::UpstreamProxyArgs, backend_host_gateway: Option, network_mediation_source: Option>, - policy_dns_store: Option>, + policy_dns: Option>, direct_listener_identity: Option, required_proxy_authorization: Option>, reserved_destination: Option, @@ -477,7 +480,7 @@ impl ProxyHandle { match pending { Ok(connection) => { let tx = preauthorized_tx.clone(); - let dns_store = policy_dns_store.clone(); + let dns_service = policy_dns.clone(); let opa = opa_engine.clone(); let cache = identity_cache.clone(); let backend_gateway = *backend_host_gateway; @@ -488,7 +491,7 @@ impl ProxyHandle { tokio::spawn(async move { if let Some(connection) = preauthorize_transparent_open( connection, - dns_store.as_ref(), + dns_service.as_ref(), &opa, &cache, backend_gateway, @@ -548,7 +551,7 @@ impl ProxyHandle { let backend_gw = backend_host_gateway.clone(); let up_proxy = upstream_proxy.clone(); let credentials = provider_credentials.clone(); - let dns_store = policy_dns_store.clone(); + let dns_service = policy_dns.clone(); let resolver = provider_credentials .as_ref() .and_then(ProviderCredentialState::resolver); @@ -563,7 +566,7 @@ impl ProxyHandle { supplied_identity, socket_addrs, transparent_destination, - dns_store, + dns_service, opa, cache, spid, @@ -634,7 +637,7 @@ impl ProxyHandle { #[allow(clippy::too_many_arguments)] async fn preauthorize_transparent_open( connection: PendingTcpOpen, - policy_dns_store: Option<&Arc>, + policy_dns: Option<&Arc>, opa_engine: &OpaEngine, identity_cache: &BinaryIdentityCache, backend_host_gateway: Option, @@ -781,7 +784,7 @@ async fn preauthorize_transparent_open( )); } let TransparentTarget { host, intent } = - match resolve_transparent_target(destination, policy_dns_store, opa_engine) { + match resolve_transparent_target(destination, policy_dns).await { Ok(target) => target, Err(error) => { warn!(%destination, %error, "Denied staged transparent connection"); @@ -868,9 +871,9 @@ async fn preauthorize_transparent_open( // no addresses, so they never reach an upstream dial. let pinned_plan = match &intent { None => None, - Some(intent) => match policy_dns_store + Some(intent) => match policy_dns .ok_or(MappingLookupError::Missing) - .and_then(|store| pinned_transparent_plan(store, destination, &decision)) + .and_then(|service| pinned_transparent_plan(service.store(), destination, &decision)) { Ok(plan) => Some(plan), Err(error) => { @@ -1022,34 +1025,29 @@ struct TransparentTarget { intent: Option, } -fn resolve_transparent_target( +async fn resolve_transparent_target( destination: SocketAddr, - policy_dns_store: Option<&Arc>, - opa_engine: &OpaEngine, + policy_dns: Option<&Arc>, ) -> Result { - let Some(store) = policy_dns_store else { + let Some(service) = policy_dns else { return Ok(TransparentTarget { host: destination.ip().to_string(), intent: None, }); }; - match store.lookup_intent( - destination.ip(), - destination.port(), - opa_engine.current_generation(), - std::time::Instant::now(), - ) { + match service + .lookup_for_connect(destination.ip(), destination.port()) + .await + { Ok(mapping) => Ok(TransparentTarget { host: mapping.record.normalized_name.as_str().to_string(), intent: Some(mapping), }), - Err(MappingLookupError::Missing) => Ok(TransparentTarget { + Err(ConnectLookupError::Mapping(MappingLookupError::Missing)) => Ok(TransparentTarget { host: destination.ip().to_string(), intent: None, }), - Err(error) => Err(miette::miette!( - "transparent destination mapping is unavailable: {error}" - )), + Err(error) => Err(miette::miette!("{error}")), } } @@ -1124,7 +1122,7 @@ impl TransparentTcpHandle { #[allow(clippy::too_many_arguments)] pub(crate) fn start( listeners: Vec, - store: Arc, + dns: Arc, opa_engine: Arc, identity_cache: Arc, entrypoint_pid: Arc, @@ -1140,7 +1138,7 @@ impl TransparentTcpHandle { ); let mut joins = Vec::with_capacity(listeners.len()); for listener in listeners { - let store = store.clone(); + let dns = dns.clone(); let engine = opa_engine.clone(); let cache = identity_cache.clone(); let pid = entrypoint_pid.clone(); @@ -1166,7 +1164,7 @@ impl TransparentTcpHandle { break; }; set_tcp_nodelay_best_effort(&stream); - let store = store.clone(); + let dns = dns.clone(); let engine = engine.clone(); let cache = cache.clone(); let pid = pid.clone(); @@ -1177,7 +1175,7 @@ impl TransparentTcpHandle { tokio::spawn(async move { if let Err(error) = handle_transparent_tcp_connection( stream, - store, + dns, engine, cache, pid, @@ -1214,7 +1212,7 @@ impl Drop for TransparentTcpHandle { #[allow(clippy::too_many_arguments)] async fn handle_transparent_tcp_connection( mut client: TcpStream, - store: Arc, + dns: Arc, opa_engine: Arc, identity_cache: Arc, entrypoint_pid: Arc, @@ -1225,16 +1223,17 @@ async fn handle_transparent_tcp_connection( ) -> Result<()> { let workload_addr = client.peer_addr().into_diagnostic()?; let original = original_destination(&client).into_diagnostic()?; - let current_generation = opa_engine.current_generation(); - let mapping = match store.lookup_intent( - original.ip(), - original.port(), - current_generation, - std::time::Instant::now(), - ) { + let mapping = match dns.lookup_for_connect(original.ip(), original.port()).await { Ok(mapping) => mapping, Err(error) => { - emit_transparent_mapping_denial(workload_addr, original, None, error); + warn!(%original, %error, "Denied transparent connection mapping"); + let cause = match error { + ConnectLookupError::Mapping(error) => error, + ConnectLookupError::Refresh(_) | ConnectLookupError::Backoff => { + MappingLookupError::Expired + } + }; + emit_transparent_mapping_denial(workload_addr, original, None, cause); emit_activity(&activity_tx, true, "transparent_tcp_mapping"); return Ok(()); } @@ -1300,7 +1299,7 @@ async fn handle_transparent_tcp_connection( emit_activity(&activity_tx, true, "transparent_tcp_mapping"); return Ok(()); }; - let mapping = match store.lookup( + let mapping = match dns.store().lookup( original.ip(), original.port(), decision.policy_generation, @@ -2606,7 +2605,7 @@ async fn handle_mediated_connection( supplied_identity: Option>, socket_addrs: Option<(SocketAddr, SocketAddr)>, transparent_open: Option, - policy_dns_store: Option>, + policy_dns: Option>, opa_engine: Arc, identity_cache: Arc, entrypoint_pid: Arc, @@ -2630,6 +2629,7 @@ async fn handle_mediated_connection( .and_then(EndpointObservationSender::capture); let mut policy_local_transparent = false; let mut metadata_transparent = false; + let mut mapped_transparent_destination = None; let (mut preauthorized_decision, prevalidated_connector, preauthorized_span) = if let Some(transparent) = transparent_open { let destination = transparent.destination; @@ -2642,14 +2642,18 @@ async fn handle_mediated_connection( policy_local_transparent = true; (None, None, None) } else { - let host = resolve_transparent_target( - destination, - policy_dns_store.as_ref(), - &opa_engine, - )? - .host; - let (decision, connector) = transparent - .authorization + let target = resolve_transparent_target(destination, policy_dns.as_ref()).await?; + let host = target.host; + // Dispatch may refresh a lease after staging captured its + // connector. Authorize again and build fresh pins below rather + // than pairing a refreshed mapping with the staged addresses. + let authorization = if target.intent.is_some() { + mapped_transparent_destination = Some(destination); + None + } else { + transparent.authorization + }; + let (decision, connector) = authorization .map_or((None, None), |(decision, connector)| { (Some(decision), Some(connector)) }); @@ -2971,8 +2975,27 @@ async fn handle_mediated_connection( let sandbox_entrypoint_pid = entrypoint_pid.load(Ordering::Acquire); if prevalidated_connector.is_none() { - match hydrate_destination_plan(&mut decision, *backend_host_gateway, *trusted_host_gateway) - { + let destination_result = + hydrate_destination_plan(&mut decision, *backend_host_gateway, *trusted_host_gateway) + .and_then(|()| { + if let Some(destination) = mapped_transparent_destination { + let plan = policy_dns + .as_ref() + .ok_or(MappingLookupError::Missing) + .and_then(|service| { + pinned_transparent_plan(service.store(), destination, &decision) + }) + .map_err(|error| DestinationDenial { + kind: DestinationDenialKind::DeclaredEndpoint, + reason: format!( + "transparent destination mapping is unavailable: {error}" + ), + })?; + decision.endpoint.destination = Some(plan); + } + Ok(()) + }); + match destination_result { Ok(()) => {} Err(denial) => { if let Some(observer) = connect_endpoint_observer.as_ref() { @@ -7813,6 +7836,18 @@ process: { run_as_user: sandbox, run_as_group: sandbox } )) } + fn policy_dns_test_service( + store: &Arc, + engine: &Arc, + ) -> Arc { + Arc::new(PolicyDnsService::new( + engine.clone(), + SocketTrustedResolver::new("127.0.0.1:9".parse().unwrap()), + store.clone(), + None, + )) + } + fn publish_mapping( store: &ResolvedEndpointStore, name: &str, @@ -7828,6 +7863,7 @@ process: { run_as_user: sandbox, run_as_group: sandbox } allocation_identity: [1; 32], policy_generation: generation, ttl: std::time::Duration::from_secs(30), + eligible_ports: std::collections::BTreeSet::from([port]), contracts: vec![crate::policy_dns::ResolvedPortContract { endpoint_id: PolicyEndpointId { policy_name: policy_name.to_string(), @@ -8068,11 +8104,13 @@ process: { run_as_user: sandbox, run_as_group: sandbox } #[tokio::test] async fn staged_transparent_open_dials_only_pinned_policy_dns_addresses() { - let engine = native_identity_engine( - include_str!("../data/sandbox-policy.rego"), - &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), - ) - .unwrap(); + let engine = Arc::new( + native_identity_engine( + include_str!("../data/sandbox-policy.rego"), + &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), + ) + .unwrap(), + ); let store = policy_dns_test_store(); let record = publish_mapping( &store, @@ -8088,7 +8126,7 @@ process: { run_as_user: sandbox, run_as_group: sandbox } let (_, _, _, transparent) = preauthorize_transparent_open( open, - Some(&store), + Some(&policy_dns_test_service(&store, &engine)), &engine, &BinaryIdentityCache::new(), None, @@ -8107,6 +8145,153 @@ process: { run_as_user: sandbox, run_as_group: sandbox } assert_eq!(connector.addrs(), &["203.0.113.8:5432".parse().unwrap()]); } + #[tokio::test] + async fn dispatch_rebuilds_staged_connector_after_mapping_expiry() { + use crate::policy_dns::AddressFamily; + use hickory_proto::{ + op::{Message, OpCode}, + rr::{RData, Record, rdata::A}, + }; + use std::time::{Duration, Instant}; + + let Some(fresh_ip) = non_loopback_test_ipv4() else { + return; + }; + // The staged connector targets A; dispatch must dial the refreshed B. + // Both listeners use the same port so only the selected IP differs. + let old_listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let old_address = old_listener.local_addr().unwrap(); + let port = old_address.port(); + let fresh_listener = TcpListener::bind((fresh_ip, port)).await.unwrap(); + let policy = POLICY_DNS_OPEN_POLICY.replace( + "port: 5432, protocol: tcp", + &format!("port: {port}, protocol: tcp, tls: skip"), + ); + let engine = Arc::new( + native_identity_engine( + include_str!("../data/sandbox-policy.rego"), + &policy_with_native_executable_paths(&policy), + ) + .unwrap(), + ); + let cache = Arc::new(BinaryIdentityCache::new()); + let store = policy_dns_test_store(); + let dns_socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap(); + let resolver = SocketTrustedResolver::new(dns_socket.local_addr().unwrap()); + let dns_task = tokio::spawn(async move { + for _ in 0..2 { + let mut wire = [0; 8192]; + let (length, peer) = dns_socket.recv_from(&mut wire).await.unwrap(); + let query = Message::from_vec(&wire[..length]).unwrap(); + let mut response = Message::response(query.metadata.id, OpCode::Query); + response.queries = query.queries.clone(); + response.add_answer(Record::from_rdata( + query.queries[0].name.clone(), + 0, + RData::A(A(fresh_ip)), + )); + dns_socket + .send_to(&response.to_vec().unwrap(), peer) + .await + .unwrap(); + } + }); + let service = Arc::new(PolicyDnsService::new( + engine.clone(), + resolver, + store.clone(), + None, + )); + let answer = service + .answer_query("db.example", AddressFamily::Ipv4, Instant::now()) + .await + .unwrap(); + let destination = SocketAddr::new(answer.address, port); + let (open, _) = staged_curl_open(destination, engine.current_generation()); + let identity = open.binary_identity; + let mut decision = authorize_supplied_identity( + &engine, + &cache, + EgressIntent::connect("db.example".into(), port), + &identity, + ); + assert!(!matches!(decision.action, NetworkAction::Deny { .. })); + decision.endpoint.destination = Some(DestinationValidationPlan { + address_authorization: destination::AddressAuthorization::PinnedResolved(vec![ + old_address.ip(), + ]), + }); + let old_connector = + destination::UpstreamConnector::new("db.example", port, vec![old_address]); + tokio::time::sleep(Duration::from_millis(1100)).await; + let (stream, mut workload) = tokio::io::duplex(4096); + let handler = tokio::spawn(handle_mediated_connection( + tokio::io::BufReader::new(Box::new(stream)), + Some(identity), + None, + Some(TransparentOpen { + destination, + authorization: Some((decision, old_connector)), + connect_span: None, + }), + Some(service.clone()), + engine, + cache, + Arc::new(AtomicU32::new(0)), + None, + None, + AgentProposals::default(), + Arc::new(None), + Arc::new(None), + Arc::new(None), + None, + None, + None, + None, + None, + None, + )); + let server = async { + let mut upstream = tokio::select! { + old = old_listener.accept() => panic!("dispatch dialed stale connector: {old:?}"), + fresh = fresh_listener.accept() => fresh.unwrap().0, + }; + set_tcp_nodelay_best_effort(&upstream); + let mut probe = [0; 5]; + upstream.read_exact(&mut probe).await.unwrap(); + assert_eq!(&probe, b"hello"); + upstream.write_all(b"fresh").await.unwrap(); + upstream.shutdown().await.unwrap(); + }; + let client = async { + workload.write_all(b"hello").await.unwrap(); + workload.shutdown().await.unwrap(); + let mut response = Vec::new(); + workload.read_to_end(&mut response).await.unwrap(); + assert_eq!(response, b"fresh"); + }; + tokio::time::timeout(Duration::from_secs(5), async { + tokio::join!(server, client); + handler.await.unwrap().unwrap(); + }) + .await + .expect("dispatch did not complete"); + let fresh = store + .lookup( + answer.address, + port, + answer.policy_generation, + Instant::now(), + ) + .unwrap(); + assert_eq!( + fresh.record.contracts[0].pinned_addresses, + [IpAddr::from(fresh_ip)] + ); + assert_ne!(fresh.record.mapping_id, answer.mapping_id); + dns_task.await.unwrap(); + } + #[tokio::test] async fn staged_transparent_pins_preserve_process_destination_authorization() { for (family, private, public, second_public) in [ @@ -8161,11 +8346,13 @@ landlock: {{ compatibility: best_effort }} process: {{ run_as_user: sandbox, run_as_group: sandbox }} " ); - let engine = native_identity_engine( - include_str!("../data/sandbox-policy.rego"), - &policy_with_native_executable_paths(&policy), - ) - .unwrap(); + let engine = Arc::new( + native_identity_engine( + include_str!("../data/sandbox-policy.rego"), + &policy_with_native_executable_paths(&policy), + ) + .unwrap(), + ); let store = policy_dns_test_store(); let contract = |policy_name: &str, endpoint_index, addresses: Vec| { crate::policy_dns::ResolvedPortContract { @@ -8207,6 +8394,7 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} allocation_identity: [1; 32], policy_generation: engine.current_generation(), ttl: std::time::Duration::from_secs(30), + eligible_ports: std::collections::BTreeSet::from([5432]), contracts, }, engine.current_generation(), @@ -8219,7 +8407,7 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} ); let accepted = preauthorize_transparent_open( open, - Some(&store), + Some(&policy_dns_test_service(&store, &engine)), &engine, &BinaryIdentityCache::new(), None, @@ -8265,11 +8453,13 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} #[tokio::test] async fn staged_transparent_open_proposes_a_denied_observation_hostname() { - let engine = native_identity_engine( - include_str!("../data/sandbox-policy.rego"), - &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), - ) - .unwrap(); + let engine = Arc::new( + native_identity_engine( + include_str!("../data/sandbox-policy.rego"), + &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), + ) + .unwrap(), + ); let store = policy_dns_test_store(); let record = publish_observation(&store, "unknown.example", engine.current_generation()); let (open, completion) = staged_curl_open( @@ -8281,7 +8471,7 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} assert!( preauthorize_transparent_open( open, - Some(&store), + Some(&policy_dns_test_service(&store, &engine)), &engine, &BinaryIdentityCache::new(), None, @@ -8309,11 +8499,13 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} #[tokio::test] async fn staged_transparent_open_never_relays_an_observation_that_policy_allows() { - let engine = native_identity_engine( - include_str!("../data/sandbox-policy.rego"), - &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), - ) - .unwrap(); + let engine = Arc::new( + native_identity_engine( + include_str!("../data/sandbox-policy.rego"), + &policy_with_native_executable_paths(POLICY_DNS_OPEN_POLICY), + ) + .unwrap(), + ); let identity_cache = BinaryIdentityCache::new(); let (denial_tx, mut denial_rx) = mpsc::unbounded_channel(); let preauthorize = |store: Arc, destination: SocketAddr| { @@ -8324,7 +8516,7 @@ process: {{ run_as_user: sandbox, run_as_group: sandbox }} async move { let admitted = preauthorize_transparent_open( open, - Some(&store), + Some(&policy_dns_test_service(&store, engine)), engine, identity_cache, None, diff --git a/crates/openshell-supervisor-network/src/proxy/tests/ipv4_mapped.rs b/crates/openshell-supervisor-network/src/proxy/tests/ipv4_mapped.rs index 81bf372cb7..8b48e3d455 100644 --- a/crates/openshell-supervisor-network/src/proxy/tests/ipv4_mapped.rs +++ b/crates/openshell-supervisor-network/src/proxy/tests/ipv4_mapped.rs @@ -5,18 +5,22 @@ use super::*; -fn engine_for(host: &str) -> OpaEngine { - native_identity_engine( - include_str!("../../../data/sandbox-policy.rego"), - &policy_with_native_executable_paths(&POLICY_DNS_OPEN_POLICY.replace("db.example", host)), +fn engine_for(host: &str) -> Arc { + Arc::new( + native_identity_engine( + include_str!("../../../data/sandbox-policy.rego"), + &policy_with_native_executable_paths( + &POLICY_DNS_OPEN_POLICY.replace("db.example", host), + ), + ) + .unwrap(), ) - .unwrap() } async fn probe( destination: SocketAddr, store: Option<&Arc>, - engine: &OpaEngine, + engine: &Arc, ) -> ( Option, TcpOpenDecision, @@ -24,9 +28,10 @@ async fn probe( ) { let (open, completion) = staged_curl_open(destination, engine.current_generation()); let (denial_tx, mut denial_rx) = mpsc::unbounded_channel(); + let dns = store.map(|store| policy_dns_test_service(store, engine)); let accepted = preauthorize_transparent_open( open, - store, + dns.as_ref(), engine, &BinaryIdentityCache::new(), None, diff --git a/crates/openshell-supervisor-network/src/run.rs b/crates/openshell-supervisor-network/src/run.rs index 0c342c6bab..169add5c5e 100644 --- a/crates/openshell-supervisor-network/src/run.rs +++ b/crates/openshell-supervisor-network/src/run.rs @@ -607,7 +607,7 @@ pub async fn run_networking( network_mediation_source, mediated_policy_dns .as_ref() - .map(|runtime| runtime.store.clone()), + .map(|runtime| runtime.service.clone()), proxy_listener .as_ref() .map(|proxy| proxy.binary_identity.clone()), @@ -641,7 +641,7 @@ pub async fn run_networking( )?; let transparent = crate::proxy::TransparentTcpHandle::start( runtime.listeners, - dns.store.clone(), + dns.service.clone(), engine, cache, entrypoint_pid, diff --git a/docs/how-it-works/policies/network-rules.mdx b/docs/how-it-works/policies/network-rules.mdx index 119818a395..044d7aa4ec 100644 --- a/docs/how-it-works/policies/network-rules.mdx +++ b/docs/how-it-works/policies/network-rules.mdx @@ -661,11 +661,15 @@ the same connection, because OpenShell cannot check which service a non-HTTP payload addresses. OpenShell answers DNS queries for the hosts in your rules with placeholder -addresses whose TTL is at most 30 seconds. This applies to every endpoint, not +addresses whose TTL is between 1 and 30 seconds. This applies to every endpoint, not only `protocol: tcp`. The sandbox applies no DNS search domains, so request each -hostname exactly as your rules name it. Clients must resolve the hostname again -before reconnecting. A client that caches an address indefinitely can fail -after it expires, even while the endpoint remains allowed. OpenShell returns an +hostname exactly as your rules name it. When a new connection uses a cached +placeholder after its DNS mapping expires, OpenShell refreshes the destination +through its trusted resolver and validates the new addresses before connecting. +The placeholder stays the same while its endpoint policy is unchanged. A failed +refresh denies the connection; OpenShell does not use expired upstream addresses. +Policy changes can still invalidate cached mappings and require a new client DNS +lookup. OpenShell returns an empty answer to AAAA queries, so dual-stack clients must fall back to the A record. diff --git a/docs/observability/logging.mdx b/docs/observability/logging.mdx index ae231edb17..a0b1065b95 100644 --- a/docs/observability/logging.mdx +++ b/docs/observability/logging.mdx @@ -219,6 +219,7 @@ Common reason phrases emitted by the sandbox include: | `resolves to always-blocked address` | An outbound policy endpoint resolved to loopback, link-local, or unspecified. Network policy cannot authorize these destinations, even when they appear in `allowed_ips`; sandbox-local loopback connections do not use this policy path. | | `resolves to which is not in allowed_ips, connection rejected` | The destination resolved to an IP outside the policy's `allowed_ips` allowlist. | | `DNS resolution failed for :` | The proxy could not resolve the destination. | +| `transparent_tcp_mapping_denied` | A transparent connection could not use its synthetic DNS mapping, including an expired mapping whose refresh failed. Check the supervisor diagnostic for the cause; adding an allow rule does not repair a failed DNS refresh. | | `port is a blocked control-plane port, connection rejected` | The destination port matches a control-plane port (etcd, Kubernetes API, kubelet) and is always blocked. | | `request-target contains an encoded '/' (%2F)` | The L7 HTTP parser rejected an encoded slash. Configure `allow_encoded_slash: true` on a REST endpoint when the upstream requires encoded slashes. | | `l7 deny` | An L7 policy rule denied the request. | diff --git a/e2e/rust/tests/transparent_tcp.rs b/e2e/rust/tests/transparent_tcp.rs index d3e1c0843b..0c73ba75ba 100644 --- a/e2e/rust/tests/transparent_tcp.rs +++ b/e2e/rust/tests/transparent_tcp.rs @@ -472,7 +472,7 @@ serve(fixture_listener) .expect("create local-container sandbox"); let script = format!( - r#"import os, socket + r#"import os, socket, time for key in ('ALL_PROXY', 'HTTP_PROXY', 'HTTPS_PROXY', 'all_proxy', 'http_proxy', 'https_proxy'): os.environ.pop(key, None) try: @@ -486,6 +486,15 @@ assert any(ip.startswith('198.18.') or ip.startswith('198.19.') for ip in synthe with socket.create_connection(({host:?}, {port}), timeout=10) as conn: conn.sendall(b'probe') assert conn.recv(1024) == b'native-tcp-ok:probe' +# Reconnect using only the cached numeric address after the maximum mapping +# TTL. Do not perform a client DNS lookup that could republish the mapping. +cached = next(ip for ip in synthetic if ip.startswith(('198.18.', '198.19.'))) +time.sleep(31) +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as conn: + conn.settimeout(10) + conn.connect((cached, {port})) + conn.sendall(b'expired-cache-probe') + assert conn.recv(1024) == b'native-tcp-ok:expired-cache-probe' mapped = next('::ffff:' + ip for ip in synthetic if ip.startswith(('198.18.', '198.19.'))) with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as conn: conn.settimeout(10)