Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions crates/openshell-driver-podman/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,58 @@ binary extraction path. `supervisor_image` supplies the dynamically linked
glibc `/openshell-supervisor` binary outside the workload. Image and request
environment belong to agent children, never the supervisor process.

## Verified provider workload identity

The driver advertises `openshell.compute.provider-workload-identity` when all
of these hold at startup:

- The mode is selected. A launched driver needs
`verified_provider_workload_identity = true` in `Configure`. A standalone
driver receives no `Configure`, so its socket setting selects the mode.
- `provider_spiffe_workload_api_socket` is an absolute Unix socket path.
- Supervisors reach the gateway over HTTPS or numeric loopback HTTP.
- The supervisor image answers the probe. The driver runs
`/openshell-supervisor provider-workload-identity-probe <socket>` in a
one-shot container with the socket's directory mounted as a supervisor
mounts it. Exit 70 (socket visible) and 71 (not visible) both show that the
image supports the mode. Any other exit withholds the capability.

Whether a supervisor sees the socket depends on its workload's UID, GID, and
groups. Create and supervisor replacement therefore run the same probe as that
user and require exit 70.

When a launch bundle carries provider identities, the driver checks that they
belong to the sandbox and generation, then labels the supervisor container
`openshell.ai/provider-launch-spiffe-id=<launch SPIFFE ID>`. The workload
container carries the label with an empty value, so an image label cannot
claim a launch. A template cannot set the label on either container.

The launch SPIFFE ID ends in the runtime generation, so it changes when the
gateway starts a new generation and stays the same across a gateway restart
and a retry. Podman fixes labels at creation, so a start whose bundle names a
launch acts on the supervisor it finds:

| Supervisor | Start |
|---|---|
| Running, labeled for this launch | Restarts the pair in place, as a start without provider identity does. |
| Stopped, labeled for this launch | The same. If that fails, creates a new supervisor. |
| Missing, never started, or labeled for another launch or for none | Creates a new supervisor. |
| Stopped pair whose supervisor holds stale destination trust | Creates a new supervisor. |

To create one, the driver first does everything that can fail without
changing the pair. It resolves the kept workload's pinned image and identity,
pulls and pins the supervisor image, and runs the probe as the workload's
user. It then stops and removes the supervisor under the canonical name, so a
sandbox never has two. With no supervisor left to mount them, it rewrites the
proxy-credential and gateway-CA secrets from the operator's current files,
then creates and starts the new supervisor. The workload container keeps its
volumes, devices, and runtime binary: the driver pulls no runtime image,
extracts no binary, and selects no GPU.

A failure after the removal leaves no supervisor, or one that the next start
removes, so a retry with the same bundle finishes the job. The driver keeps no
launch state outside Podman.

## OCI working directory

OpenShell reads `WORKDIR` from the workload image. If it is unset, `/`, or
Expand Down Expand Up @@ -129,7 +181,10 @@ driver-owned volumes and secrets.
Stop retains both containers, workspace, channel, and secrets. Start restores
the consumed sandbox bootstrap from a copy in the supervisor's private
filesystem, verifies the fence, and starts the same pair. A failed supervisor
start stops the workload. Delete removes the companion first, then the workload,
start stops the workload. A start for a new provider launch replaces the
supervisor instead; see
[Verified provider workload identity](#verified-provider-workload-identity).
Delete removes the companion first, then the workload,
channel, workspace, and driver-owned secrets. User-owned volumes are retained.
Delete keeps a channel, control, or workspace volume whose
`openshell.ai/sandbox-id` label is missing or names another sandbox, and logs a
Expand Down Expand Up @@ -176,4 +231,4 @@ The gateway passes the `[openshell.drivers.podman]` table on stdin and gateway m

`serve` resolves the Podman socket from `OPENSHELL_PODMAN_SOCKET`, `socket_path`, or discovery, and pings it before it binds. The ping gets 20 seconds, so the gateway, which waits 30 seconds for a driver to start, receives the report instead of killing the driver. It exits 69 when Podman is not reachable and 78 when the table is invalid. Until `Configure` succeeds, `ComputeDriver` calls return `UNAVAILABLE` and the driver creates no networks, volumes, or secrets. `preflight` validates the table without contacting Podman.

The gateway port and name, trusted runtime images, and OTLP settings come from `Configure`. Trusted runtime images from the gateway's environment take precedence over the table, as they did when the driver ran in process. The gateway supplies `gateway_port` and the CA, so the table rejects `gateway_port`, `guest_tls_ca` and the admission keys (`allow_driver_config`, `resource_admission`), which the gateway delivers in `Configure`. `grpc_endpoint` remains an optional override, and an `https://` endpoint needs the gateway CA.
The gateway port and name, trusted runtime images, OTLP settings, and the selection of verified provider workload identity come from `Configure`. Trusted runtime images from the gateway's environment take precedence over the table, as they did when the driver ran in process. The gateway supplies `gateway_port` and the CA, so the table rejects `gateway_port`, `guest_tls_ca` and the admission keys (`allow_driver_config`, `resource_admission`), which the gateway delivers in `Configure`. `grpc_endpoint` remains an optional override, and an `https://` endpoint needs the gateway CA.
11 changes: 11 additions & 0 deletions crates/openshell-driver-podman/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,17 @@ impl PodmanClient {
.await
}

/// Wait for a started container to exit and return its exit status.
pub(crate) async fn wait_for_container_exit(&self, name: &str) -> Result<i32, PodmanApiError> {
validate_name(name)?;
self.request_json(
hyper::Method::POST,
&format!("/libpod/containers/{name}/wait?condition=exited"),
None,
)
.await
}

/// Stop a container with a grace period in seconds.
pub async fn stop_container(
&self,
Expand Down
168 changes: 157 additions & 11 deletions crates/openshell-driver-podman/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn is_selinux_enabled() -> bool {
}

pub use openshell_core::driver_utils::{
LABEL_SANDBOX_ID, LABEL_SANDBOX_NAME, LABEL_SANDBOX_NAMESPACE, LABEL_SANDBOX_WORKSPACE,
LABEL_PROVIDER_LAUNCH_SPIFFE_ID, LABEL_SANDBOX_ID, LABEL_SANDBOX_NAME, LABEL_SANDBOX_NAMESPACE,
LABEL_SANDBOX_WORKSPACE,
};

/// Label applied to all managed containers.
Expand Down Expand Up @@ -1513,16 +1514,8 @@ fn build_base_spec(
options: opts,
});
}
if let Some(path) = provider_spiffe_workload_api_socket_mount_source(config) {
// No SELinux relabel - the SPIRE agent socket is shared host
// infrastructure and must keep its existing SELinux context.
let ro = vec!["ro".into(), "rbind".into()];
m.push(Mount {
kind: "bind".into(),
source: path.display().to_string(),
destination: PROVIDER_SPIFFE_WORKLOAD_API_SOCKET_MOUNT_DIR.into(),
options: ro,
});
if let Some(mount) = provider_spiffe_workload_api_socket_mount(config) {
m.push(mount);
}
m.extend(user_mounts.mounts);
m
Expand Down Expand Up @@ -1592,6 +1585,14 @@ pub struct IsolationSpecs {
}

impl IsolationSpecs {
/// Mark the supervisor with the launch SPIFFE ID from its launch bundle.
pub(crate) fn record_provider_launch(&mut self, launch_spiffe_id: &str) {
self.supervisor.labels.insert(
LABEL_PROVIDER_LAUNCH_SPIFFE_ID.into(),
launch_spiffe_id.into(),
);
}

pub(crate) fn record_resource_identities(
&mut self,
identities: &BTreeMap<String, Value>,
Expand Down Expand Up @@ -1627,6 +1628,13 @@ pub fn build_isolation_specs(
workload
.labels
.insert(crate::isolation::LABEL_ROLE.into(), "sandbox".into());
// Only a supervisor carries a launch SPIFFE ID, and only the one in its
// launch bundle. The empty value keeps an image or template label from
// passing the workload off as one; a template cannot set the supervisor's.
workload
.labels
.insert(LABEL_PROVIDER_LAUNCH_SPIFFE_ID.into(), String::new());
supervisor.labels.remove(LABEL_PROVIDER_LAUNCH_SPIFFE_ID);
workload.env = BTreeMap::new();
workload.unsetenv = input
.image
Expand Down Expand Up @@ -1858,6 +1866,61 @@ fn provider_spiffe_workload_api_socket_mount_source(config: &PodmanComputeConfig
host_path.parent()
}

fn provider_spiffe_workload_api_socket_mount(config: &PodmanComputeConfig) -> Option<Mount> {
let source = provider_spiffe_workload_api_socket_mount_source(config)?;
// No SELinux relabel - the SPIRE agent socket is shared host
// infrastructure and must keep its existing SELinux context.
Some(Mount {
kind: "bind".into(),
source: source.display().to_string(),
destination: PROVIDER_SPIFFE_WORKLOAD_API_SOCKET_MOUNT_DIR.into(),
options: vec!["ro".into(), "rbind".into()],
})
}

/// A one-shot container that runs the supervisor image's probe subcommand
/// against the Workload API socket, mounted as a supervisor's is. It runs as
/// `identity` when given, as that workload's supervisor would, and otherwise
/// as the engine's root user.
pub fn provider_workload_identity_probe_spec(
config: &PodmanComputeConfig,
supervisor_image: &str,
identity: Option<&openshell_isolation_interface::contract::ResolvedWorkloadIdentity>,
) -> Result<Value, ComputeDriverError> {
let (Some(socket), Some(mount)) = (
provider_spiffe_workload_api_socket_env_value(config),
provider_spiffe_workload_api_socket_mount(config),
) else {
return Err(ComputeDriverError::Precondition(
"verified provider workload identity needs a Workload API Unix socket path".into(),
));
};
let mut spec = serde_json::json!({
"name": format!("openshell-provider-probe-{}", uuid::Uuid::new_v4()),
"image": supervisor_image,
"entrypoint": ["/openshell-supervisor"],
"command": [openshell_core::driver_utils::PROVIDER_WORKLOAD_IDENTITY_PROBE, socket],
"mounts": [mount],
"user": "0:0",
"userns": {"nsmode": "host"},
"netns": {"nsmode": "none"},
"cap_drop": ["ALL"],
"no_new_privileges": true,
"read_only_filesystem": true,
"healthconfig": {"test": ["NONE"]},
"work_dir": "/",
});
if let Some(identity) = identity {
spec["user"] = format!("{}:{}", identity.uid, identity.gid).into();
spec["groups"] = identity
.supplementary_gids
.iter()
.map(ToString::to_string)
.collect();
}
Ok(spec)
}

fn hostadd_entries(config: &PodmanComputeConfig) -> Vec<String> {
let host_gateway_ip = config.host_gateway_ip.trim();
if host_gateway_ip.is_empty() {
Expand Down Expand Up @@ -4212,6 +4275,89 @@ mod tests {
}));
}

#[test]
fn only_the_supervisor_carries_its_bundles_launch_spiffe_id() {
use openshell_core::proto::compute::v1::DriverSandboxSpec;
let mut sandbox = test_sandbox("launch-id", "launch-name");
sandbox.spec = Some(DriverSandboxSpec {
template: Some(DriverSandboxTemplate {
labels: std::iter::once((
LABEL_PROVIDER_LAUNCH_SPIFFE_ID.to_string(),
"spiffe://example.test/spoofed".to_string(),
))
.collect(),
..Default::default()
}),
..Default::default()
});
let mut config = test_config();
config.provider_spiffe_workload_api_socket = Some("/host/spire-agent.sock".into());
let identity = openshell_isolation_interface::contract::ResolvedWorkloadIdentity::new(
1000,
1001,
vec![2000],
"image".into(),
"sha256:image".into(),
)
.unwrap();
let mut specs = build_isolation_specs(IsolationSpecInput {
sandbox: &sandbox,
config: &config,
token_secret: None,
resolver_secret: "resolver",
gpu_devices: None,
image: &ResolvedPodmanImage::new("sha256:image"),
supervisor_bin: None,
tls_secrets: None,
network_trust: None,
identity: &identity,
})
.unwrap();
let launch =
|spec: &ContainerSpec| spec.labels.get(LABEL_PROVIDER_LAUNCH_SPIFFE_ID).cloned();
assert_eq!(launch(&specs.workload).as_deref(), Some(""));
assert_eq!(launch(&specs.supervisor), None);
specs.record_provider_launch("spiffe://example.test/launch");
assert_eq!(launch(&specs.workload).as_deref(), Some(""));
assert_eq!(
launch(&specs.supervisor).as_deref(),
Some("spiffe://example.test/launch")
);

// The probe sees the socket through the supervisor's own mount, as
// the supervisor's user.
let supervisor = serde_json::to_value(&specs.supervisor).unwrap();
let probe =
provider_workload_identity_probe_spec(&config, "sha256:supervisor", Some(&identity))
.unwrap();
let socket_mount = |spec: &Value| {
spec["mounts"]
.as_array()
.unwrap()
.iter()
.find(|mount| {
mount["destination"].as_str()
== Some(PROVIDER_SPIFFE_WORKLOAD_API_SOCKET_MOUNT_DIR)
})
.cloned()
};
assert!(socket_mount(&probe).is_some());
assert_eq!(socket_mount(&probe), socket_mount(&supervisor));
for field in ["user", "groups", "userns"] {
assert_eq!(probe[field], supervisor[field], "{field}");
}
assert_eq!(
probe["command"],
serde_json::json!([
openshell_core::driver_utils::PROVIDER_WORKLOAD_IDENTITY_PROBE,
"/spiffe-workload-api/spire-agent.sock"
])
);

config.provider_spiffe_workload_api_socket = Some("tcp:127.0.0.1:8081".into());
assert!(provider_workload_identity_probe_spec(&config, "sha256:supervisor", None).is_err());
}

#[test]
fn container_spec_omits_tls_without_config() {
let sandbox = test_sandbox("notls-id", "notls-name");
Expand Down
Loading
Loading