@@ -3518,6 +3518,65 @@ impl KubernetesComputeDriver {
35183518 })
35193519 }
35203520
3521+ /// Snapshot proxy credentials from the workload namespace into the same
3522+ /// private generation record as its route and trust, including on takeover.
3523+ async fn shared_upstream_proxy_launch(
3524+ &self,
3525+ namespace: &str,
3526+ ca_bundle: Option<Vec<u8>>,
3527+ ) -> Result<Option<openshell_supervisor_control::UpstreamProxyLaunch>, KubernetesDriverError>
3528+ {
3529+ let Some(url) = &self.config.https_proxy else {
3530+ return Ok(None);
3531+ };
3532+ let credential = match (
3533+ self.config.proxy_auth_secret_name.as_deref(),
3534+ self.config.proxy_auth_secret_key.as_deref(),
3535+ ) {
3536+ (None, None) => None,
3537+ (Some(name), Some(key)) => {
3538+ let secret = Api::<Secret>::namespaced(self.client.clone(), namespace)
3539+ .get(name)
3540+ .await
3541+ .map_err(KubernetesDriverError::from_kube)?;
3542+ let value = secret
3543+ .data
3544+ .and_then(|mut data| data.remove(key))
3545+ .ok_or_else(|| {
3546+ KubernetesDriverError::Precondition(
3547+ "proxy credential Secret is missing its configured key".to_string(),
3548+ )
3549+ })?;
3550+ Some(String::from_utf8(value.0).map_err(|_| {
3551+ KubernetesDriverError::Precondition(
3552+ "proxy credential Secret must contain UTF-8 user:pass".to_string(),
3553+ )
3554+ })?)
3555+ }
3556+ _ => {
3557+ return Err(KubernetesDriverError::Precondition(
3558+ "proxy_auth_secret_name and proxy_auth_secret_key must be set together".into(),
3559+ ));
3560+ }
3561+ };
3562+ let proxy = openshell_supervisor_control::UpstreamProxyLaunch {
3563+ url: url.clone(),
3564+ no_proxy: self.config.no_proxy.clone(),
3565+ credential,
3566+ auth_allow_insecure: self.config.proxy_auth_allow_insecure.unwrap_or(false),
3567+ connect_by_hostname: self.config.proxy_connect_by_hostname.unwrap_or(false),
3568+ ca_bundle_pem: ca_bundle.map(String::from_utf8).transpose().map_err(|_| {
3569+ KubernetesDriverError::Precondition(
3570+ "proxy_ca_bundle must contain UTF-8 PEM".to_string(),
3571+ )
3572+ })?,
3573+ };
3574+ proxy
3575+ .validate()
3576+ .map_err(|error| KubernetesDriverError::Precondition(error.to_string()))?;
3577+ Ok(Some(proxy))
3578+ }
3579+
35213580 /// Create one generation's gated supervisor Pod and return its UID.
35223581 #[allow(clippy::too_many_arguments, clippy::similar_names)]
35233582 async fn create_sandbox_runtime_supervisor(
@@ -4015,6 +4074,9 @@ impl KubernetesComputeDriver {
40154074 .map_err(KubernetesDriverError::from_kube)?;
40164075 }
40174076 GenerationLaunchTarget::Shared { shared, route, .. } => {
4077+ let upstream_proxy = self
4078+ .shared_upstream_proxy_launch(namespace, staged.upstream_proxy_ca_bundle)
4079+ .await?;
40184080 self.create_generation_image_pull_secrets(
40194081 namespace,
40204082 names,
@@ -4029,6 +4091,7 @@ impl KubernetesComputeDriver {
40294091 provisioned.runtime_descriptor,
40304092 launch_authentication,
40314093 proxy_ca,
4094+ upstream_proxy,
40324095 )?;
40334096 // The coordinator persists the launch material with the
40344097 // generation before it assigns the instance, so a restarted
@@ -5889,6 +5952,7 @@ fn shared_instance_registration(
58895952 runtime_descriptor: openshell_sandbox_backend::boundary_protocol::SandboxRuntimeDescriptor,
58905953 launch_authentication: &openshell_core::jwt::SandboxLaunchAuthentication,
58915954 proxy_ca: crate::sandbox_runtime::ProxyCaMaterial,
5955+ upstream_proxy: Option<openshell_supervisor_control::UpstreamProxyLaunch>,
58925956) -> Result<SandboxInstanceRegistration, KubernetesDriverError> {
58935957 Ok(SandboxInstanceRegistration {
58945958 route: route.to_string(),
@@ -5922,6 +5986,7 @@ fn shared_instance_registration(
59225986 certificate_pem: proxy_ca.certificate_pem,
59235987 private_key_pem: openshell_supervisor_control::SecretPem::new(proxy_ca.private_key_pem),
59245988 },
5989+ upstream_proxy,
59255990 trace_context: RegistrationTraceContext::current(),
59265991 })
59275992}
@@ -9134,6 +9199,7 @@ fn spawn_namespace_file_watcher(
91349199#[cfg(test)]
91359200mod tests {
91369201 use super::*;
9202+ use k8s_openapi::ByteString;
91379203
91389204 /// Serve Pod GETs over one reusable HTTP/1 connection per client,
91399205 /// delaying a request for Pod `slow-<millis>` by that many milliseconds.
@@ -13587,6 +13653,90 @@ mod tests {
1358713653 (driver, steps, bodies)
1358813654 }
1358913655
13656+ #[tokio::test]
13657+ async fn shared_proxy_launch_snapshots_credentials_from_each_sandbox_namespace() {
13658+ let ca = generate_proxy_ca_material().unwrap().certificate_pem;
13659+ for (namespace, path) in [
13660+ (
13661+ "workspace-a",
13662+ "/api/v1/namespaces/workspace-a/secrets/egress-auth",
13663+ ),
13664+ (
13665+ "workspace-b",
13666+ "/api/v1/namespaces/workspace-b/secrets/egress-auth",
13667+ ),
13668+ ] {
13669+ let credential = format!("{namespace}:private-password");
13670+ let secret = Secret {
13671+ data: Some(BTreeMap::from([(
13672+ "credentials".to_string(),
13673+ ByteString(credential.as_bytes().to_vec()),
13674+ )])),
13675+ ..Secret::default()
13676+ };
13677+ let config = KubernetesComputeConfig {
13678+ https_proxy: Some("https://proxy.example:3130".to_string()),
13679+ proxy_auth_secret_name: Some("egress-auth".to_string()),
13680+ proxy_auth_secret_key: Some("credentials".to_string()),
13681+ ..KubernetesComputeConfig::default()
13682+ };
13683+ let (driver, pending, _) = scripted_driver(
13684+ config,
13685+ vec![(
13686+ http::Method::GET,
13687+ path,
13688+ kube_test_response(http::StatusCode::OK, serde_json::to_value(secret).unwrap()),
13689+ )],
13690+ );
13691+ let proxy = driver
13692+ .shared_upstream_proxy_launch(namespace, Some(ca.as_bytes().to_vec()))
13693+ .await
13694+ .unwrap()
13695+ .unwrap();
13696+ assert_eq!(proxy.credential.as_deref(), Some(credential.as_str()));
13697+ assert_eq!(proxy.ca_bundle_pem.as_deref(), Some(ca.as_str()));
13698+ assert!(pending.lock().unwrap().is_empty());
13699+ }
13700+ }
13701+
13702+ #[tokio::test]
13703+ async fn shared_proxy_launch_refuses_missing_or_invalid_credentials() {
13704+ let path = "/api/v1/namespaces/workspace-a/secrets/egress-auth";
13705+ let invalid = [
13706+ None,
13707+ Some(vec![0xff]),
13708+ Some(b"private-password".to_vec()),
13709+ Some(vec![b'x'; 4097]),
13710+ ];
13711+ for credential in invalid {
13712+ let secret = Secret {
13713+ data: credential
13714+ .map(|value| BTreeMap::from([("credentials".to_string(), ByteString(value))])),
13715+ ..Secret::default()
13716+ };
13717+ let config = KubernetesComputeConfig {
13718+ https_proxy: Some("https://proxy.example:3130".to_string()),
13719+ proxy_auth_secret_name: Some("egress-auth".to_string()),
13720+ proxy_auth_secret_key: Some("credentials".to_string()),
13721+ ..KubernetesComputeConfig::default()
13722+ };
13723+ let (driver, _, _) = scripted_driver(
13724+ config,
13725+ vec![(
13726+ http::Method::GET,
13727+ path,
13728+ kube_test_response(http::StatusCode::OK, serde_json::to_value(secret).unwrap()),
13729+ )],
13730+ );
13731+ let error = driver
13732+ .shared_upstream_proxy_launch("workspace-a", None)
13733+ .await
13734+ .unwrap_err()
13735+ .to_string();
13736+ assert!(!error.contains("private-password"), "{error}");
13737+ }
13738+ }
13739+
1359013740 fn pod_owner(name: &str, uid: &str) -> OwnerReference {
1359113741 OwnerReference {
1359213742 api_version: "v1".to_string(),
0 commit comments