|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Kubernetes provisioning for the shared authenticated boundary protocol. |
| 5 | +//! |
| 6 | +//! This module deliberately contains no lifecycle, process, network, identity, |
| 7 | +//! or wire implementation. The driver chooses the proxy-pod placement, binds |
| 8 | +//! immutable Kubernetes resource identities, and provisions TCP coordinates; |
| 9 | +//! `openshell-isolation-interface` and `openshell-sandbox` provide the common |
| 10 | +//! control and boundary behavior. |
| 11 | +
|
| 12 | +use std::collections::{BTreeMap, HashMap}; |
| 13 | +use std::net::{IpAddr, SocketAddr}; |
| 14 | +use std::path::PathBuf; |
| 15 | + |
| 16 | +use openshell_isolation_interface::boundary_protocol::{ |
| 17 | + BOUNDARY_PROTOCOL_VERSION, BoundaryAgentIdentity, BoundaryConfig, BoundaryListener, |
| 18 | + BoundaryTopology, BoundaryTransport, |
| 19 | +}; |
| 20 | + |
| 21 | +/// Registered RFC 0012 backend name for the proxy-pod topology. |
| 22 | +pub const BACKEND_NAME: &str = "kubernetes-proxy-pod"; |
| 23 | + |
| 24 | +/// Driver-owned inputs that bind one workload/proxy pair to one boundary. |
| 25 | +/// |
| 26 | +/// The driver constructs this only after Kubernetes has assigned every UID and |
| 27 | +/// after it has observed the exact egress policy resource version. The workload |
| 28 | +/// stays held until the matching boundary config and control topology have been |
| 29 | +/// installed. |
| 30 | +pub struct KubernetesProxyPodBoundarySpec { |
| 31 | + pub boundary_id: String, |
| 32 | + pub bootstrap_token: String, |
| 33 | + pub namespace_uid: String, |
| 34 | + pub workload_pod_uid: String, |
| 35 | + pub proxy_pod_uid: String, |
| 36 | + pub egress_policy_uid: String, |
| 37 | + pub egress_policy_resource_version: String, |
| 38 | + pub boundary_listener: SocketAddr, |
| 39 | + pub control_address: SocketAddr, |
| 40 | + pub host_gateway_ip: Option<IpAddr>, |
| 41 | + pub agent_uid: u32, |
| 42 | + pub agent_gid: u32, |
| 43 | + pub trusted_runtime_root: PathBuf, |
| 44 | + pub child_env: HashMap<String, String>, |
| 45 | +} |
| 46 | + |
| 47 | +/// Protected workload-pod config and matching proxy-pod descriptor. |
| 48 | +pub struct KubernetesProxyPodBoundaryProvisioning { |
| 49 | + pub boundary_config: BoundaryConfig, |
| 50 | + pub topology: BoundaryTopology, |
| 51 | +} |
| 52 | + |
| 53 | +impl KubernetesProxyPodBoundarySpec { |
| 54 | + /// Produce both sides of the common protocol from one observed Kubernetes |
| 55 | + /// resource set so a stale or recreated object cannot be attached. |
| 56 | + #[must_use] |
| 57 | + pub fn provision(self) -> KubernetesProxyPodBoundaryProvisioning { |
| 58 | + let resource_claims = BTreeMap::from([ |
| 59 | + ("kubernetes.namespace_uid".to_string(), self.namespace_uid), |
| 60 | + ( |
| 61 | + "kubernetes.workload_pod_uid".to_string(), |
| 62 | + self.workload_pod_uid, |
| 63 | + ), |
| 64 | + ("kubernetes.proxy_pod_uid".to_string(), self.proxy_pod_uid), |
| 65 | + ( |
| 66 | + "kubernetes.egress_policy_uid".to_string(), |
| 67 | + self.egress_policy_uid, |
| 68 | + ), |
| 69 | + ( |
| 70 | + "kubernetes.egress_policy_resource_version".to_string(), |
| 71 | + self.egress_policy_resource_version, |
| 72 | + ), |
| 73 | + ]); |
| 74 | + KubernetesProxyPodBoundaryProvisioning { |
| 75 | + boundary_config: BoundaryConfig { |
| 76 | + protocol_version: BOUNDARY_PROTOCOL_VERSION, |
| 77 | + boundary_id: self.boundary_id.clone(), |
| 78 | + bootstrap_token: self.bootstrap_token.clone(), |
| 79 | + listener: BoundaryListener::Tcp { |
| 80 | + address: self.boundary_listener, |
| 81 | + }, |
| 82 | + resource_claims: resource_claims.clone(), |
| 83 | + agent_identity: BoundaryAgentIdentity::Resolved { |
| 84 | + uid: self.agent_uid, |
| 85 | + gid: self.agent_gid, |
| 86 | + }, |
| 87 | + protect_config_file: true, |
| 88 | + trusted_runtime_root: self.trusted_runtime_root, |
| 89 | + child_env: self.child_env, |
| 90 | + }, |
| 91 | + topology: BoundaryTopology { |
| 92 | + protocol_version: BOUNDARY_PROTOCOL_VERSION, |
| 93 | + boundary_id: self.boundary_id, |
| 94 | + transport: BoundaryTransport::Tcp { |
| 95 | + address: self.control_address, |
| 96 | + }, |
| 97 | + host_gateway_ip: self.host_gateway_ip, |
| 98 | + resource_claims, |
| 99 | + bootstrap_token: self.bootstrap_token, |
| 100 | + }, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + |
| 109 | + fn spec() -> KubernetesProxyPodBoundarySpec { |
| 110 | + KubernetesProxyPodBoundarySpec { |
| 111 | + boundary_id: "sandbox-1".to_string(), |
| 112 | + bootstrap_token: "a".repeat(64), |
| 113 | + namespace_uid: "namespace-uid".to_string(), |
| 114 | + workload_pod_uid: "workload-pod-uid".to_string(), |
| 115 | + proxy_pod_uid: "proxy-pod-uid".to_string(), |
| 116 | + egress_policy_uid: "network-policy-uid".to_string(), |
| 117 | + egress_policy_resource_version: "1945".to_string(), |
| 118 | + boundary_listener: "0.0.0.0:5500".parse().expect("valid listener"), |
| 119 | + control_address: "10.42.0.7:5500".parse().expect("valid target"), |
| 120 | + host_gateway_ip: Some("10.42.0.1".parse().expect("valid gateway IP")), |
| 121 | + agent_uid: 1000, |
| 122 | + agent_gid: 1000, |
| 123 | + trusted_runtime_root: PathBuf::from("/opt/openshell/bin/openshell-runtime"), |
| 124 | + child_env: HashMap::new(), |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn provisioning_binds_identical_kubernetes_resource_claims() { |
| 130 | + let provisioned = spec().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["kubernetes.workload_pod_uid"], |
| 138 | + "workload-pod-uid" |
| 139 | + ); |
| 140 | + assert_eq!( |
| 141 | + provisioned.topology.resource_claims["kubernetes.egress_policy_resource_version"], |
| 142 | + "1945" |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn provisioning_uses_one_shared_tcp_protocol_across_pods() { |
| 148 | + let provisioned = spec().provision(); |
| 149 | + |
| 150 | + assert_eq!( |
| 151 | + provisioned.boundary_config.listener, |
| 152 | + BoundaryListener::Tcp { |
| 153 | + address: "0.0.0.0:5500".parse().expect("valid listener"), |
| 154 | + } |
| 155 | + ); |
| 156 | + assert_eq!( |
| 157 | + provisioned.topology.transport, |
| 158 | + BoundaryTransport::Tcp { |
| 159 | + address: "10.42.0.7:5500".parse().expect("valid target"), |
| 160 | + } |
| 161 | + ); |
| 162 | + } |
| 163 | +} |
0 commit comments