mirror of
https://github.com/hanzoai/runtime.git
synced 2026-10-30 18:23:13 +00:00
Every sandbox was created privileged under the daemon's default runtime, and the setting that would have changed it defaulted to an empty string that silently meant runc. For a service whose whole premise is running code somebody else wrote, that is not a boundary. An org sets its isolation and the create request carries it, so a sandbox records what it was made with: changing the policy moves new sandboxes and leaves running ones alone. gvisor is the default. firecracker is delivered through kata's shim, which is why it is one name rather than two. runc is refused unless the runner names that org, and an empty list names nobody. Absent means the default, unknown is a refusal, and a runtime the daemon does not have is a refusal. Nothing substitutes quietly. The runner took an organization in a field called UserId with a note beside it saying so. Deciding who may have runc through a field named after something else is not reviewable. Privileged is replaced by dropping everything and granting the eight the daemon uses. Nothing the sandbox does broke; what stopped working is mount, mknod, raw sockets, chroot, writing to proc, and seeing the host's disks. AUDIT_WRITE is among them because without it sudo works and complains every time. The creation histogram's lowest bucket was a tenth of a second, so the metric could not observe the claim the product makes about its own speed. It can now, and it carries the isolation, so comparing them is a query. Co-authored-by: Hanzo Dev <[email protected]>
145 lines
3.3 KiB
Go
145 lines
3.3 KiB
Go
// Copyright 2025 Hanzo Industries Inc.
|
|
// SPDX-License-Identifier: AGPL-3.0
|
|
|
|
package docker
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/hanzoai/runtime/libs/common-go/pkg/timer"
|
|
"github.com/hanzoai/runtime/apps/runner/pkg/common"
|
|
"github.com/hanzoai/runtime/apps/runner/pkg/models/enums"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (d *DockerClient) Start(ctx context.Context, containerId string) error {
|
|
defer timer.Timer()()
|
|
d.cache.SetSandboxState(ctx, containerId, enums.SandboxStateStarting)
|
|
|
|
c, err := d.ContainerInspect(ctx, containerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.State.Running {
|
|
containerIP, err := getContainerIP(&c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = d.waitForDaemonRunning(ctx, containerIP, 10*time.Second)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.cache.SetSandboxState(ctx, containerId, enums.SandboxStateStarted)
|
|
return nil
|
|
}
|
|
|
|
err = d.apiClient.ContainerStart(ctx, containerId, container.StartOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// make sure container is running
|
|
err = d.waitForContainerRunning(ctx, containerId, 10*time.Second)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c, err = d.ContainerInspect(ctx, containerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
containerIP, err := getContainerIP(&c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
processesCtx := context.Background()
|
|
go func() {
|
|
if err := d.startRuntimeDaemon(processesCtx, containerId); err != nil {
|
|
log.Errorf("Failed to start Runtime daemon: %s\n", err.Error())
|
|
}
|
|
}()
|
|
|
|
err = d.waitForDaemonRunning(ctx, containerIP, 10*time.Second)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.cache.SetSandboxState(ctx, containerId, enums.SandboxStateStarted)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *DockerClient) waitForContainerRunning(ctx context.Context, containerId string, timeout time.Duration) error {
|
|
defer timer.Timer()()
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-timeoutCtx.Done():
|
|
return fmt.Errorf("timeout waiting for container %s to start", containerId)
|
|
case <-ticker.C:
|
|
c, err := d.ContainerInspect(ctx, containerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.State.Running {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *DockerClient) waitForDaemonRunning(ctx context.Context, containerIP string, timeout time.Duration) error {
|
|
defer timer.Timer()()
|
|
|
|
// Build the target URL
|
|
targetURL := fmt.Sprintf("http://%s:2280/version", containerIP)
|
|
target, err := url.Parse(targetURL)
|
|
if err != nil {
|
|
return common.NewBadRequestError(fmt.Errorf("failed to parse target URL: %w", err))
|
|
}
|
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
for {
|
|
select {
|
|
case <-timeoutCtx.Done():
|
|
return fmt.Errorf("timeout waiting for daemon to start")
|
|
default:
|
|
conn, err := net.DialTimeout("tcp", target.Host, 1*time.Second)
|
|
if err != nil {
|
|
time.Sleep(5 * time.Millisecond)
|
|
continue
|
|
}
|
|
conn.Close()
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func getContainerIP(container *types.ContainerJSON) (string, error) {
|
|
for _, network := range container.NetworkSettings.Networks {
|
|
return network.IPAddress, nil
|
|
}
|
|
return "", fmt.Errorf("container has no IP address, it might not be running")
|
|
}
|