Skip to content

Commit 575810b

Browse files
committed
refactor(isolation): unify typed network mediation
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 88e12a0 commit 575810b

3 files changed

Lines changed: 83 additions & 46 deletions

File tree

‎crates/openshell-isolation-interface/src/contract.rs‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! `attach` is atomic from the caller's perspective: it establishes and binds
2525
//! the boundary, returns `Bound`, or fails closed. It never binds a resource
2626
//! already bound to an active boundary. Binary identity travels on every
27-
//! [`PendingNetworkOpen`], resolved for that exact socket and process
27+
//! [`PendingTcpOpen`], resolved for that exact socket and process
2828
//! generation; an unresolved identity denies the open.
2929
//!
3030
//! The contract is transport-neutral. Concrete topology implementations keep
@@ -352,17 +352,12 @@ pub trait IsolationBackend: Send + Sync {
352352
/// code is running.
353353
#[async_trait]
354354
pub trait BoundBoundary: Send {
355-
/// The mediation service's backend-neutral source of workload connections.
355+
/// The mediation service's backend-neutral source of workload network
356+
/// requests. TCP and DNS remain typed operations so consumers cannot mix
357+
/// their framing, decisions, or response semantics.
356358
/// Retained by the supervisor before consuming `Bound`.
357359
fn network_mediation_source(&self) -> Arc<dyn NetworkMediationSource>;
358360

359-
/// Optional transport for workload DNS exchanges. Backends that expose
360-
/// this source keep DNS inside the supervisor-owned policy path rather
361-
/// than granting the workload access to a resolver socket.
362-
fn dns_mediation_source(&self) -> Option<Arc<dyn DnsMediationSource>> {
363-
None
364-
}
365-
366361
/// Trusted host-side dial target for the well-known host-gateway aliases.
367362
///
368363
/// Backends return this when the mediation service runs outside the
@@ -703,7 +698,7 @@ pub trait BoundaryLoopbackConnector: Send + Sync {
703698
// ============================================================================
704699

705700
/// Executable identity for one accepted connection, resolved by the backend and
706-
/// delivered on [`PendingNetworkOpen`] before the mediation service evaluates
701+
/// delivered on [`PendingTcpOpen`] before the mediation service evaluates
707702
/// policy.
708703
///
709704
/// A missing digest is `None`, never an empty value; policy that requires an
@@ -777,21 +772,36 @@ pub struct NetworkSocketMetadata {
777772

778773
/// Typed supervisor decision for one pending TCP open.
779774
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
780-
pub enum NetworkOpenResult {
775+
pub enum TcpOpenDecision {
781776
/// L4 authorization and a bounded relay handler are ready. L7 policy still
782777
/// applies to bytes after the local connection commits.
783778
RelayReady,
784779
/// The socket remains unchanged and connect returns this positive errno.
785-
Denied { errno: i32 },
780+
Denied(TcpOpenDenial),
781+
}
782+
783+
/// Placement-neutral reason why a staged TCP open was not committed.
784+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
785+
pub enum TcpOpenDenial {
786+
/// The admitted network policy rejected the request.
787+
PolicyDenied,
788+
/// The backend could not resolve authoritative executable identity.
789+
IdentityUnavailable,
790+
/// The requested destination could not be validated.
791+
InvalidDestination,
792+
/// A bounded mediation resource was exhausted.
793+
ResourceExhausted,
794+
/// The mediation path became unavailable before commit.
795+
MediationUnavailable,
786796
}
787797

788798
/// A staged workload TCP open delivered before its local relay is committed.
789799
///
790800
/// An `Err` identity must be denied and audited. The supervisor owns
791801
/// `result`; dropping it cancels the open without changing the workload socket.
792-
pub struct PendingNetworkOpen {
802+
pub struct PendingTcpOpen {
793803
/// Staged byte stream whose workload side is committed only after
794-
/// [`NetworkOpenResult::RelayReady`].
804+
/// [`TcpOpenDecision::RelayReady`].
795805
pub stream: BoundaryDuplexStream,
796806
/// Executable identity, resolved by the backend for this connection.
797807
pub binary_identity: Result<BinaryIdentity, ResolveError>,
@@ -802,7 +812,7 @@ pub struct PendingNetworkOpen {
802812
/// Policy generation under which the request was created.
803813
pub policy_generation: u64,
804814
/// Single-use completion channel back to the sandbox broker.
805-
pub result: oneshot::Sender<NetworkOpenResult>,
815+
pub decision: oneshot::Sender<TcpOpenDecision>,
806816
}
807817

808818
/// A logical per-boundary stream of workload connections, consumed by the
@@ -811,28 +821,33 @@ pub struct PendingNetworkOpen {
811821
/// It may wrap a dedicated listener or a demultiplexed view over shared
812822
/// transport; how it reaches a co-located proxy, a sidecar, or a shared
813823
/// mediation service is backend-private. A trusted backend component associates
814-
/// every returned connection with its active boundary without relying solely on
815-
/// a transport tuple or workload-provided identifier. An `Err` from `accept`
816-
/// means the source itself is unusable and fails the boundary closed.
824+
/// every returned request with its active boundary without relying solely on a
825+
/// transport tuple or workload-provided identifier. TCP and DNS use separate
826+
/// accepts so they can be consumed concurrently with independent backpressure.
827+
/// An `Err` means that mediation lane is unusable and fails closed.
817828
#[async_trait]
818829
pub trait NetworkMediationSource: Send + Sync {
819830
/// Await the next staged workload TCP open.
820-
async fn accept(&self) -> Result<PendingNetworkOpen, BackendError>;
831+
async fn accept_tcp(&self) -> Result<PendingTcpOpen, BackendError>;
832+
833+
/// Await the next workload DNS query.
834+
async fn accept_dns(&self) -> Result<PendingDnsQuery, BackendError>;
821835
}
822836

823837
/// DNS transport used by one workload exchange.
824838
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
825839
pub enum DnsTransport {
826840
/// One DNS wire datagram without a TCP length prefix.
827841
Udp,
828-
/// One two-byte-length-prefixed DNS message.
842+
/// One DNS message received over a TCP resolver connection.
829843
Tcp,
830844
}
831845

832846
/// One workload DNS request and its fail-closed response channel.
833-
pub struct MediatedDnsQuery {
834-
/// DNS request bytes in the framing selected by [`Self::transport`].
835-
pub request: Vec<u8>,
847+
pub struct PendingDnsQuery {
848+
/// Exactly one DNS wire message, without a DNS-over-TCP length prefix.
849+
/// The backend removes and restores transport framing.
850+
pub message: Vec<u8>,
836851
/// Workload DNS transport.
837852
pub transport: DnsTransport,
838853
/// Identity of the process that issued the DNS request when the backend
@@ -844,11 +859,3 @@ pub struct MediatedDnsQuery {
844859
/// Single-use response channel owned by the backend adapter.
845860
pub response: oneshot::Sender<Result<Vec<u8>, BackendError>>,
846861
}
847-
848-
/// Logical per-boundary stream of DNS exchanges. The backend handles syscall,
849-
/// packet, or guest-agent transport details; the supervisor owns policy DNS.
850-
#[async_trait]
851-
pub trait DnsMediationSource: Send + Sync {
852-
/// Await the next DNS query from this boundary.
853-
async fn accept(&self) -> Result<MediatedDnsQuery, BackendError>;
854-
}

‎crates/openshell-isolation-interface/tests/backend_conformance.rs‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ struct MockSource<K>(PhantomData<K>);
9090

9191
#[async_trait]
9292
impl<K: MockKind> NetworkMediationSource for MockSource<K> {
93-
async fn accept(&self) -> Result<PendingNetworkOpen, BackendError> {
93+
async fn accept_tcp(&self) -> Result<PendingTcpOpen, BackendError> {
9494
let (near, _far) = tokio::io::duplex(64);
95-
let (result, _result_rx) = oneshot::channel();
96-
Ok(PendingNetworkOpen {
95+
let (decision, _decision_rx) = oneshot::channel();
96+
Ok(PendingTcpOpen {
9797
stream: Box::new(near),
9898
binary_identity: Ok(BinaryIdentity {
9999
binary_path: PathBuf::from("/usr/bin/agent"),
@@ -109,7 +109,19 @@ impl<K: MockKind> NetworkMediationSource for MockSource<K> {
109109
process_generation: 1,
110110
},
111111
policy_generation: 1,
112-
result,
112+
decision,
113+
})
114+
}
115+
116+
async fn accept_dns(&self) -> Result<PendingDnsQuery, BackendError> {
117+
let (response, _response_rx) = oneshot::channel();
118+
Ok(PendingDnsQuery {
119+
message: vec![0; 12],
120+
transport: DnsTransport::Udp,
121+
binary_identity: Err(ResolveError::Failed(
122+
"mock DNS attribution unavailable".to_string(),
123+
)),
124+
response,
113125
})
114126
}
115127
}
@@ -121,10 +133,10 @@ struct UnattributedSource;
121133

122134
#[async_trait]
123135
impl NetworkMediationSource for UnattributedSource {
124-
async fn accept(&self) -> Result<PendingNetworkOpen, BackendError> {
136+
async fn accept_tcp(&self) -> Result<PendingTcpOpen, BackendError> {
125137
let (near, _far) = tokio::io::duplex(64);
126-
let (result, _result_rx) = oneshot::channel();
127-
Ok(PendingNetworkOpen {
138+
let (decision, _decision_rx) = oneshot::channel();
139+
Ok(PendingTcpOpen {
128140
stream: Box::new(near),
129141
binary_identity: Err(ResolveError::Failed("hash unavailable".to_string())),
130142
destination: "203.0.113.10:443".parse().unwrap(),
@@ -134,9 +146,15 @@ impl NetworkMediationSource for UnattributedSource {
134146
process_generation: 1,
135147
},
136148
policy_generation: 1,
137-
result,
149+
decision,
138150
})
139151
}
152+
153+
async fn accept_dns(&self) -> Result<PendingDnsQuery, BackendError> {
154+
Err(BackendError::Unavailable(
155+
"mock DNS mediation unavailable".to_string(),
156+
))
157+
}
140158
}
141159

142160
struct MockExec;
@@ -560,7 +578,7 @@ async fn runtime_interfaces_survive_lifecycle_consumption() {
560578
.await
561579
.expect("start");
562580

563-
let conn = source.accept().await.expect("accept after consumption");
581+
let conn = source.accept_tcp().await.expect("accept after consumption");
564582
let identity = conn.binary_identity.expect("identity resolves");
565583
assert_eq!(identity.binary_path, PathBuf::from("/usr/bin/agent"));
566584
}
@@ -721,7 +739,7 @@ async fn pending_network_open_carries_socket_bound_identity() {
721739
.expect("attach");
722740
let conn = bound
723741
.network_mediation_source()
724-
.accept()
742+
.accept_tcp()
725743
.await
726744
.expect("accept");
727745
let identity = conn.binary_identity.expect("identity resolves");
@@ -740,7 +758,7 @@ async fn missing_digest_is_none_never_empty() {
740758
// The secondary backend resolves path-only identity: the digest is `None`,
741759
// so policy that requires a digest cannot authorize the connection.
742760
let source = MockSource::<Secondary>(PhantomData);
743-
let conn = source.accept().await.expect("accept");
761+
let conn = source.accept_tcp().await.expect("accept");
744762
let identity = conn.binary_identity.expect("identity resolves");
745763
assert!(identity.binary_digest.is_none());
746764
}
@@ -757,7 +775,7 @@ async fn unresolved_identity_travels_with_the_pending_open_and_fails_closed() {
757775
// Attribution failure does not tear down the source: the connection is
758776
// delivered carrying `Err`, and the mediation service denies it.
759777
let source = UnattributedSource;
760-
let conn = source.accept().await.expect("accept");
778+
let conn = source.accept_tcp().await.expect("accept");
761779
assert!(conn.binary_identity.is_err());
762780
}
763781

‎rfc/0012-isolation-backend/README.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,28 @@ trait BoundaryTerminal: Send + Sync {
251251
```rust
252252
#[async_trait]
253253
trait NetworkMediationSource: Send + Sync {
254-
async fn accept(&self) -> Result<MediatedConnection, BackendError>;
254+
async fn accept_tcp(&self) -> Result<PendingTcpOpen, BackendError>;
255+
async fn accept_dns(&self) -> Result<PendingDnsQuery, BackendError>;
255256
}
256257

257-
struct MediatedConnection {
258+
struct PendingTcpOpen {
258259
stream: BoundaryDuplexStream,
259260
binary_identity: Result<BinaryIdentity, ResolveError>,
261+
destination: SocketAddr,
262+
socket: NetworkSocketMetadata,
263+
policy_generation: u64,
264+
decision: oneshot::Sender<TcpOpenDecision>,
265+
}
266+
267+
struct PendingDnsQuery {
268+
message: Vec<u8>, // exactly one DNS message; no TCP length prefix
269+
transport: DnsTransport,
270+
binary_identity: Result<BinaryIdentity, ResolveError>,
271+
response: oneshot::Sender<Result<Vec<u8>, BackendError>>,
260272
}
261273
```
262274

263-
`NetworkMediationSource` supplies outbound connections from one boundary to supervisor-owned network mediation. The backend routes all workload egress through that source and authoritatively associates each connection with the boundary without relying solely on workload-provided data. Capture, transport, placement, and coordination are backend-private.
275+
`NetworkMediationSource` supplies typed TCP opens and DNS queries from one boundary to supervisor-owned network mediation. The two accepts are independent so the supervisor can consume them concurrently without coupling their ordering or backpressure. The backend routes all workload egress through these sources and authoritatively associates each request with the boundary without relying solely on workload-provided data. Capture, transport, framing, placement, and coordination are backend-private. General UDP mediation is outside this contract.
264276

265277
Every topology may use the same supervisor-owned mediation libraries or services; the source does not require a backend-specific policy engine.
266278

0 commit comments

Comments
 (0)