|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Docker provisioning for the shared authenticated boundary protocol. |
| 5 | +//! |
| 6 | +//! Docker owns only the container/socket topology and immutable OCI resource |
| 7 | +//! claims. Lifecycle, process, network, identity, and wire behavior live in |
| 8 | +//! `openshell-isolation-interface` and `openshell-sandbox`. |
| 9 | +
|
| 10 | +use std::collections::{BTreeMap, HashMap}; |
| 11 | +use std::net::IpAddr; |
| 12 | +use std::path::PathBuf; |
| 13 | + |
| 14 | +use openshell_isolation_interface::boundary_protocol::{ |
| 15 | + BoundaryClientTls, BoundaryConfig, BoundaryListener, BoundaryServerTls, BoundaryTopology, |
| 16 | + BoundaryTransport, |
| 17 | +}; |
| 18 | +use openshell_isolation_interface::contract::{DriverFenceEvidence, ResolvedWorkloadIdentity}; |
| 19 | + |
| 20 | +/// Driver-owned inputs that bind one Docker container to one boundary. |
| 21 | +pub struct DockerBoundarySpec { |
| 22 | + pub boundary_id: String, |
| 23 | + pub bootstrap_token: String, |
| 24 | + pub generation: String, |
| 25 | + pub session_epoch: String, |
| 26 | + pub container_id: String, |
| 27 | + pub image_identity: String, |
| 28 | + pub listener_socket: PathBuf, |
| 29 | + pub control_socket: PathBuf, |
| 30 | + pub sandbox_tls: BoundaryServerTls, |
| 31 | + pub supervisor_tls: BoundaryClientTls, |
| 32 | + pub host_gateway_ip: Option<IpAddr>, |
| 33 | + pub workload_identity: ResolvedWorkloadIdentity, |
| 34 | + pub child_env: HashMap<String, String>, |
| 35 | +} |
| 36 | + |
| 37 | +/// Protected container config and matching host descriptor. |
| 38 | +pub struct DockerBoundaryProvisioning { |
| 39 | + pub boundary_config: BoundaryConfig, |
| 40 | + pub topology: BoundaryTopology, |
| 41 | +} |
| 42 | + |
| 43 | +impl DockerBoundarySpec { |
| 44 | + /// Produce both sides of the common protocol from the same immutable |
| 45 | + /// Docker coordinates so attach cannot bind a different container. |
| 46 | + #[must_use] |
| 47 | + pub fn provision(self) -> DockerBoundaryProvisioning { |
| 48 | + let resource_claims = BTreeMap::from([ |
| 49 | + ("docker.container_id".to_string(), self.container_id), |
| 50 | + ("docker.image_identity".to_string(), self.image_identity), |
| 51 | + ]); |
| 52 | + let driver_fence = DriverFenceEvidence::Docker { |
| 53 | + container_id: resource_claims["docker.container_id"].clone(), |
| 54 | + network_mode: "none".to_string(), |
| 55 | + unexpected_networks: Vec::new(), |
| 56 | + }; |
| 57 | + DockerBoundaryProvisioning { |
| 58 | + boundary_config: BoundaryConfig { |
| 59 | + boundary_id: self.boundary_id.clone(), |
| 60 | + generation: self.generation.clone(), |
| 61 | + session_epoch: self.session_epoch.clone(), |
| 62 | + bootstrap_token: self.bootstrap_token.clone(), |
| 63 | + listener: BoundaryListener::Unix { |
| 64 | + socket_path: self.listener_socket, |
| 65 | + tls: self.sandbox_tls, |
| 66 | + }, |
| 67 | + resource_claims: resource_claims.clone(), |
| 68 | + resource_claim_files: BTreeMap::new(), |
| 69 | + workload_identity: self.workload_identity.clone(), |
| 70 | + driver_fence: driver_fence.clone(), |
| 71 | + child_env: self.child_env, |
| 72 | + }, |
| 73 | + topology: BoundaryTopology { |
| 74 | + boundary_id: self.boundary_id, |
| 75 | + generation: self.generation, |
| 76 | + session_epoch: self.session_epoch, |
| 77 | + workload_identity: self.workload_identity, |
| 78 | + transport: BoundaryTransport::Unix { |
| 79 | + socket_path: self.control_socket, |
| 80 | + tls: self.supervisor_tls, |
| 81 | + }, |
| 82 | + host_gateway_ip: self.host_gateway_ip, |
| 83 | + resource_claims, |
| 84 | + driver_fence, |
| 85 | + bootstrap_token: self.bootstrap_token, |
| 86 | + }, |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn provisioning_binds_container_and_image_claims() { |
| 97 | + let tls = openshell_isolation_interface::boundary_protocol::generate_boundary_mutual_tls_material() |
| 98 | + .unwrap(); |
| 99 | + let provisioned = DockerBoundarySpec { |
| 100 | + boundary_id: "sandbox-1".to_string(), |
| 101 | + bootstrap_token: "a".repeat(64), |
| 102 | + generation: "generation-1".to_string(), |
| 103 | + session_epoch: "epoch-1".to_string(), |
| 104 | + container_id: "sha256:container".to_string(), |
| 105 | + image_identity: "sha256:image".to_string(), |
| 106 | + listener_socket: PathBuf::from("/run/openshell/boundary/control.sock"), |
| 107 | + control_socket: PathBuf::from("/host/control.sock"), |
| 108 | + sandbox_tls: BoundaryServerTls { |
| 109 | + certificate_chain_path: PathBuf::from("/run/openshell/boundary/server.crt"), |
| 110 | + private_key_path: PathBuf::from("/run/openshell/boundary/server.key"), |
| 111 | + client_ca_certificate_path: PathBuf::from("/run/openshell/boundary/client-ca.crt"), |
| 112 | + }, |
| 113 | + supervisor_tls: BoundaryClientTls { |
| 114 | + server_name: tls.server_name, |
| 115 | + ca_certificate_pem: tls.ca_certificate_pem, |
| 116 | + certificate_chain_pem: tls.supervisor_certificate_pem, |
| 117 | + private_key_pem: tls.supervisor_private_key_pem, |
| 118 | + }, |
| 119 | + host_gateway_ip: Some(IpAddr::from([127, 0, 0, 1])), |
| 120 | + workload_identity: ResolvedWorkloadIdentity::new( |
| 121 | + 1000, |
| 122 | + 1000, |
| 123 | + Vec::new(), |
| 124 | + "image".to_string(), |
| 125 | + "sha256:image".to_string(), |
| 126 | + ) |
| 127 | + .unwrap(), |
| 128 | + child_env: HashMap::new(), |
| 129 | + } |
| 130 | + .provision(); |
| 131 | + |
| 132 | + assert_eq!( |
| 133 | + provisioned.boundary_config.resource_claims, |
| 134 | + provisioned.topology.resource_claims |
| 135 | + ); |
| 136 | + assert_eq!( |
| 137 | + provisioned.topology.resource_claims["docker.container_id"], |
| 138 | + "sha256:container" |
| 139 | + ); |
| 140 | + assert_eq!( |
| 141 | + provisioned.boundary_config.driver_fence, |
| 142 | + provisioned.topology.driver_fence |
| 143 | + ); |
| 144 | + assert!( |
| 145 | + provisioned |
| 146 | + .topology |
| 147 | + .driver_fence |
| 148 | + .validate_for_backend("docker") |
| 149 | + .is_ok() |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments