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]>
141 lines
4.0 KiB
Go
141 lines
4.0 KiB
Go
// Copyright 2025 Hanzo Industries Inc.
|
|
// SPDX-License-Identifier: AGPL-3.0
|
|
|
|
package common
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
apiclient "github.com/hanzoai/runtime/libs/api-client-go"
|
|
apiclient_cli "github.com/hanzoai/runtime/apps/cli/apiclient"
|
|
"github.com/hanzoai/runtime/apps/cli/pkg/minio"
|
|
)
|
|
|
|
// Create MinIO client from access parameters
|
|
func CreateMinioClient(accessParams *apiclient.StorageAccessDto) (*minio.Client, error) {
|
|
storageURL, err := url.Parse(accessParams.StorageUrl)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid storage URL: %w", err)
|
|
}
|
|
|
|
minioClient, err := minio.NewClient(
|
|
storageURL.Host,
|
|
accessParams.AccessKey,
|
|
accessParams.Secret,
|
|
accessParams.Bucket,
|
|
storageURL.Scheme == "https",
|
|
accessParams.SessionToken,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create storage client: %w", err)
|
|
}
|
|
|
|
return minioClient, nil
|
|
}
|
|
|
|
// List existing objects in MinIO
|
|
func ListExistingObjects(ctx context.Context, minioClient *minio.Client, orgID string) (map[string]bool, error) {
|
|
objects, err := minioClient.ListObjects(ctx, orgID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list objects: %w", err)
|
|
}
|
|
|
|
existingObjects := make(map[string]bool)
|
|
for _, obj := range objects {
|
|
existingObjects[obj] = true
|
|
}
|
|
return existingObjects, nil
|
|
}
|
|
|
|
// getContextHashes processes context paths and returns their hashes
|
|
func getContextHashes(ctx context.Context, apiClient *apiclient.APIClient, contextPaths []string) ([]string, error) {
|
|
contextHashes := []string{}
|
|
if len(contextPaths) == 0 {
|
|
return contextHashes, nil
|
|
}
|
|
|
|
// Get storage access parameters
|
|
accessParams, res, err := apiClient.ObjectStorageAPI.GetPushAccess(ctx).Execute()
|
|
if err != nil {
|
|
return nil, apiclient_cli.HandleErrorResponse(res, err)
|
|
}
|
|
|
|
// Create MinIO client
|
|
minioClient, err := CreateMinioClient(accessParams)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create storage client: %w", err)
|
|
}
|
|
|
|
// List existing objects to avoid re-uploading
|
|
existingObjects, err := ListExistingObjects(ctx, minioClient, accessParams.OrganizationId)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list existing objects: %w", err)
|
|
}
|
|
|
|
// Process each context path
|
|
for _, contextPath := range contextPaths {
|
|
absPath, err := filepath.Abs(contextPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid context path %s: %w", contextPath, err)
|
|
}
|
|
|
|
fileInfo, err := os.Stat(absPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to access context path %s: %w", contextPath, err)
|
|
}
|
|
|
|
if fileInfo.IsDir() {
|
|
// Process directory
|
|
dirHashes, err := minioClient.ProcessDirectory(ctx, absPath, accessParams.OrganizationId, existingObjects)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to process directory %s: %w", absPath, err)
|
|
}
|
|
contextHashes = append(contextHashes, dirHashes...)
|
|
} else {
|
|
// Process single file
|
|
hash, err := minioClient.ProcessFile(ctx, absPath, accessParams.OrganizationId, existingObjects)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to process file %s: %w", absPath, err)
|
|
}
|
|
contextHashes = append(contextHashes, hash)
|
|
}
|
|
}
|
|
|
|
return contextHashes, nil
|
|
}
|
|
|
|
func GetCreateBuildInfoDto(ctx context.Context, dockerfilePath string, contextPaths []string) (*apiclient.CreateBuildInfo, error) {
|
|
dockerfileAbsPath, err := filepath.Abs(dockerfilePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid dockerfile path: %w", err)
|
|
}
|
|
|
|
if _, err := os.Stat(dockerfileAbsPath); os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("dockerfile does not exist: %s", dockerfileAbsPath)
|
|
}
|
|
|
|
dockerfileContent, err := os.ReadFile(dockerfileAbsPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read dockerfile: %w", err)
|
|
}
|
|
|
|
apiClient, err := apiclient_cli.GetApiClient(nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
contextHashes, err := getContextHashes(ctx, apiClient, contextPaths)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &apiclient.CreateBuildInfo{
|
|
DockerfileContent: string(dockerfileContent),
|
|
ContextHashes: contextHashes,
|
|
}, nil
|
|
}
|