From efa9dec05179004c40d5a53c42d565129cce8652 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 18 Dec 2025 16:06:13 -0500 Subject: [PATCH 01/42] poc: add build system for source-to-image builds Proof of concept for a secure build system that runs rootless BuildKit inside ephemeral Cloud Hypervisor microVMs for multi-tenant isolation. Components: - lib/builds/: Core build system (queue, storage, manager, cache) - lib/builds/builder_agent/: Guest binary for running BuildKit - lib/builds/templates/: Dockerfile generation for Node.js/Python - lib/builds/images/: Builder image Dockerfiles API endpoints: - POST /v1/builds: Submit build job - GET /v1/builds: List builds - GET /v1/builds/{id}: Get build details - DELETE /v1/builds/{id}: Cancel build - GET /v1/builds/{id}/logs: Stream logs (SSE) --- cmd/api/api/api.go | 4 + cmd/api/api/builds.go | 284 ++++ cmd/api/api/registry_test.go | 2 +- cmd/api/config/config.go | 12 + cmd/api/wire.go | 3 + cmd/api/wire_gen.go | 14 +- go.sum | 2 + lib/builds/README.md | 232 +++ lib/builds/builder_agent/main.go | 497 +++++++ lib/builds/cache.go | 176 +++ lib/builds/cache_test.go | 188 +++ lib/builds/errors.go | 46 + lib/builds/images/base/Dockerfile | 29 + lib/builds/images/nodejs20/Dockerfile | 58 + lib/builds/images/python312/Dockerfile | 61 + lib/builds/manager.go | 529 +++++++ lib/builds/metrics.go | 86 ++ lib/builds/queue.go | 171 +++ lib/builds/queue_test.go | 230 +++ lib/builds/storage.go | 227 +++ lib/builds/templates/templates.go | 230 +++ lib/builds/templates/templates_test.go | 180 +++ lib/builds/types.go | 200 +++ lib/builds/vsock_handler.go | 249 ++++ lib/oapi/oapi.go | 1820 ++++++++++++++++++++---- lib/paths/paths.go | 37 + lib/providers/providers.go | 28 + openapi.yaml | 327 +++++ 28 files changed, 5664 insertions(+), 258 deletions(-) create mode 100644 cmd/api/api/builds.go create mode 100644 lib/builds/README.md create mode 100644 lib/builds/builder_agent/main.go create mode 100644 lib/builds/cache.go create mode 100644 lib/builds/cache_test.go create mode 100644 lib/builds/errors.go create mode 100644 lib/builds/images/base/Dockerfile create mode 100644 lib/builds/images/nodejs20/Dockerfile create mode 100644 lib/builds/images/python312/Dockerfile create mode 100644 lib/builds/manager.go create mode 100644 lib/builds/metrics.go create mode 100644 lib/builds/queue.go create mode 100644 lib/builds/queue_test.go create mode 100644 lib/builds/storage.go create mode 100644 lib/builds/templates/templates.go create mode 100644 lib/builds/templates/templates_test.go create mode 100644 lib/builds/types.go create mode 100644 lib/builds/vsock_handler.go diff --git a/cmd/api/api/api.go b/cmd/api/api/api.go index f511cbfc..f014ba55 100644 --- a/cmd/api/api/api.go +++ b/cmd/api/api/api.go @@ -2,6 +2,7 @@ package api import ( "github.com/onkernel/hypeman/cmd/api/config" + "github.com/onkernel/hypeman/lib/builds" "github.com/onkernel/hypeman/lib/devices" "github.com/onkernel/hypeman/lib/images" "github.com/onkernel/hypeman/lib/ingress" @@ -20,6 +21,7 @@ type ApiService struct { NetworkManager network.Manager DeviceManager devices.Manager IngressManager ingress.Manager + BuildManager builds.Manager } var _ oapi.StrictServerInterface = (*ApiService)(nil) @@ -33,6 +35,7 @@ func New( networkManager network.Manager, deviceManager devices.Manager, ingressManager ingress.Manager, + buildManager builds.Manager, ) *ApiService { return &ApiService{ Config: config, @@ -42,5 +45,6 @@ func New( NetworkManager: networkManager, DeviceManager: deviceManager, IngressManager: ingressManager, + BuildManager: buildManager, } } diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go new file mode 100644 index 00000000..a1654d15 --- /dev/null +++ b/cmd/api/api/builds.go @@ -0,0 +1,284 @@ +package api + +import ( + "bytes" + "context" + "errors" + "io" + "strconv" + + "github.com/onkernel/hypeman/lib/builds" + "github.com/onkernel/hypeman/lib/logger" + "github.com/onkernel/hypeman/lib/oapi" +) + +// ListBuilds returns all builds +func (s *ApiService) ListBuilds(ctx context.Context, request oapi.ListBuildsRequestObject) (oapi.ListBuildsResponseObject, error) { + log := logger.FromContext(ctx) + + domainBuilds, err := s.BuildManager.ListBuilds(ctx) + if err != nil { + log.ErrorContext(ctx, "failed to list builds", "error", err) + return oapi.ListBuilds500JSONResponse{ + Code: "internal_error", + Message: "failed to list builds", + }, nil + } + + oapiBuilds := make([]oapi.Build, len(domainBuilds)) + for i, b := range domainBuilds { + oapiBuilds[i] = buildToOAPI(b) + } + + return oapi.ListBuilds200JSONResponse(oapiBuilds), nil +} + +// CreateBuild creates a new build job +func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRequestObject) (oapi.CreateBuildResponseObject, error) { + log := logger.FromContext(ctx) + + // Parse multipart form fields + var sourceData []byte + var runtime string + var baseImageDigest, cacheScope, dockerfile string + var timeoutSeconds int + + for { + part, err := request.Body.NextPart() + if err == io.EOF { + break + } + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to parse multipart form", + }, nil + } + + switch part.FormName() { + case "source": + sourceData, err = io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_source", + Message: "failed to read source data", + }, nil + } + case "runtime": + var buf bytes.Buffer + io.Copy(&buf, part) + runtime = buf.String() + case "base_image_digest": + var buf bytes.Buffer + io.Copy(&buf, part) + baseImageDigest = buf.String() + case "cache_scope": + var buf bytes.Buffer + io.Copy(&buf, part) + cacheScope = buf.String() + case "dockerfile": + var buf bytes.Buffer + io.Copy(&buf, part) + dockerfile = buf.String() + case "timeout_seconds": + var buf bytes.Buffer + io.Copy(&buf, part) + if v, err := strconv.Atoi(buf.String()); err == nil { + timeoutSeconds = v + } + } + part.Close() + } + + if runtime == "" { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "runtime is required", + }, nil + } + + if len(sourceData) == 0 { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "source is required", + }, nil + } + + // Build domain request + domainReq := builds.CreateBuildRequest{ + Runtime: runtime, + BaseImageDigest: baseImageDigest, + CacheScope: cacheScope, + Dockerfile: dockerfile, + } + + // Apply timeout if provided + if timeoutSeconds > 0 { + domainReq.BuildPolicy = &builds.BuildPolicy{ + TimeoutSeconds: timeoutSeconds, + } + } + + build, err := s.BuildManager.CreateBuild(ctx, domainReq, sourceData) + if err != nil { + switch { + case errors.Is(err, builds.ErrInvalidRuntime): + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_runtime", + Message: err.Error(), + }, nil + case errors.Is(err, builds.ErrInvalidSource): + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_source", + Message: err.Error(), + }, nil + default: + log.ErrorContext(ctx, "failed to create build", "error", err) + return oapi.CreateBuild500JSONResponse{ + Code: "internal_error", + Message: "failed to create build", + }, nil + } + } + + return oapi.CreateBuild202JSONResponse(buildToOAPI(build)), nil +} + +// GetBuild gets build details +func (s *ApiService) GetBuild(ctx context.Context, request oapi.GetBuildRequestObject) (oapi.GetBuildResponseObject, error) { + log := logger.FromContext(ctx) + + build, err := s.BuildManager.GetBuild(ctx, request.Id) + if err != nil { + if errors.Is(err, builds.ErrNotFound) { + return oapi.GetBuild404JSONResponse{ + Code: "not_found", + Message: "build not found", + }, nil + } + log.ErrorContext(ctx, "failed to get build", "error", err, "id", request.Id) + return oapi.GetBuild500JSONResponse{ + Code: "internal_error", + Message: "failed to get build", + }, nil + } + + return oapi.GetBuild200JSONResponse(buildToOAPI(build)), nil +} + +// CancelBuild cancels a build +func (s *ApiService) CancelBuild(ctx context.Context, request oapi.CancelBuildRequestObject) (oapi.CancelBuildResponseObject, error) { + log := logger.FromContext(ctx) + + err := s.BuildManager.CancelBuild(ctx, request.Id) + if err != nil { + switch { + case errors.Is(err, builds.ErrNotFound): + return oapi.CancelBuild404JSONResponse{ + Code: "not_found", + Message: "build not found", + }, nil + case errors.Is(err, builds.ErrBuildInProgress): + return oapi.CancelBuild409JSONResponse{ + Code: "conflict", + Message: "build already in progress", + }, nil + default: + log.ErrorContext(ctx, "failed to cancel build", "error", err, "id", request.Id) + return oapi.CancelBuild500JSONResponse{ + Code: "internal_error", + Message: "failed to cancel build", + }, nil + } + } + + return oapi.CancelBuild204Response{}, nil +} + +// GetBuildLogs streams build logs +func (s *ApiService) GetBuildLogs(ctx context.Context, request oapi.GetBuildLogsRequestObject) (oapi.GetBuildLogsResponseObject, error) { + log := logger.FromContext(ctx) + + logs, err := s.BuildManager.GetBuildLogs(ctx, request.Id) + if err != nil { + if errors.Is(err, builds.ErrNotFound) { + return oapi.GetBuildLogs404JSONResponse{ + Code: "not_found", + Message: "build not found", + }, nil + } + log.ErrorContext(ctx, "failed to get build logs", "error", err, "id", request.Id) + return oapi.GetBuildLogs500JSONResponse{ + Code: "internal_error", + Message: "failed to get build logs", + }, nil + } + + // Return logs as SSE + // For simplicity, return all logs at once + // TODO: Implement proper SSE streaming with follow support + return oapi.GetBuildLogs200TexteventStreamResponse{ + Body: stringReader(string(logs)), + ContentLength: int64(len(logs)), + }, nil +} + +// buildToOAPI converts a domain Build to OAPI Build +func buildToOAPI(b *builds.Build) oapi.Build { + oapiBuild := oapi.Build{ + Id: b.ID, + Status: oapi.BuildStatus(b.Status), + Runtime: b.Runtime, + QueuePosition: b.QueuePosition, + ImageDigest: b.ImageDigest, + ImageRef: b.ImageRef, + Error: b.Error, + CreatedAt: b.CreatedAt, + StartedAt: b.StartedAt, + CompletedAt: b.CompletedAt, + DurationMs: b.DurationMS, + } + + if b.Provenance != nil { + oapiBuild.Provenance = &oapi.BuildProvenance{ + BaseImageDigest: &b.Provenance.BaseImageDigest, + SourceHash: &b.Provenance.SourceHash, + ToolchainVersion: &b.Provenance.ToolchainVersion, + BuildkitVersion: &b.Provenance.BuildkitVersion, + Timestamp: &b.Provenance.Timestamp, + } + if len(b.Provenance.LockfileHashes) > 0 { + oapiBuild.Provenance.LockfileHashes = &b.Provenance.LockfileHashes + } + } + + return oapiBuild +} + +// deref safely dereferences a pointer, returning empty string if nil +func deref(s *string) string { + if s == nil { + return "" + } + return *s +} + +// stringReader wraps a string as an io.Reader +type stringReaderImpl struct { + s string + i int +} + +func stringReader(s string) io.Reader { + return &stringReaderImpl{s: s} +} + +func (r *stringReaderImpl) Read(p []byte) (n int, err error) { + if r.i >= len(r.s) { + return 0, io.EOF + } + n = copy(p, r.s[r.i:]) + r.i += n + return n, nil +} + diff --git a/cmd/api/api/registry_test.go b/cmd/api/api/registry_test.go index 1e9e2554..4b9c27df 100644 --- a/cmd/api/api/registry_test.go +++ b/cmd/api/api/registry_test.go @@ -373,7 +373,7 @@ func TestRegistryTagPush(t *testing.T) { for _, img := range images { if img.Digest == digest.String() { found = true - assert.Equal(t, oapi.Ready, img.Status, "image in list should have Ready status") + assert.Equal(t, oapi.ImageStatusReady, img.Status, "image in list should have Ready status") assert.NotNil(t, img.SizeBytes, "ready image should have size") t.Logf("Image found in ListImages: %s (status=%s, size=%d)", img.Name, img.Status, *img.SizeBytes) break diff --git a/cmd/api/config/config.go b/cmd/api/config/config.go index a3e5a556..daa896f9 100644 --- a/cmd/api/config/config.go +++ b/cmd/api/config/config.go @@ -101,6 +101,12 @@ type Config struct { // Cloudflare configuration (if AcmeDnsProvider=cloudflare) CloudflareApiToken string // Cloudflare API token + + // Build system configuration + MaxConcurrentSourceBuilds int // Max concurrent source-to-image builds + BuilderImage string // OCI image for builder VMs + RegistryURL string // URL of registry for built images + BuildTimeout int // Default build timeout in seconds } // Load loads configuration from environment variables @@ -163,6 +169,12 @@ func Load() *Config { // Cloudflare configuration CloudflareApiToken: getEnv("CLOUDFLARE_API_TOKEN", ""), + + // Build system configuration + MaxConcurrentSourceBuilds: getEnvInt("MAX_CONCURRENT_SOURCE_BUILDS", 2), + BuilderImage: getEnv("BUILDER_IMAGE", "hypeman/builder:latest"), + RegistryURL: getEnv("REGISTRY_URL", "localhost:8080"), + BuildTimeout: getEnvInt("BUILD_TIMEOUT", 600), } return cfg diff --git a/cmd/api/wire.go b/cmd/api/wire.go index dfa2fc15..746ef9b4 100644 --- a/cmd/api/wire.go +++ b/cmd/api/wire.go @@ -9,6 +9,7 @@ import ( "github.com/google/wire" "github.com/onkernel/hypeman/cmd/api/api" "github.com/onkernel/hypeman/cmd/api/config" + "github.com/onkernel/hypeman/lib/builds" "github.com/onkernel/hypeman/lib/devices" "github.com/onkernel/hypeman/lib/images" "github.com/onkernel/hypeman/lib/ingress" @@ -32,6 +33,7 @@ type application struct { InstanceManager instances.Manager VolumeManager volumes.Manager IngressManager ingress.Manager + BuildManager builds.Manager Registry *registry.Registry ApiService *api.ApiService } @@ -50,6 +52,7 @@ func initializeApp() (*application, func(), error) { providers.ProvideInstanceManager, providers.ProvideVolumeManager, providers.ProvideIngressManager, + providers.ProvideBuildManager, providers.ProvideRegistry, api.New, wire.Struct(new(application), "*"), diff --git a/cmd/api/wire_gen.go b/cmd/api/wire_gen.go index 6b3e81ad..19ea6621 100644 --- a/cmd/api/wire_gen.go +++ b/cmd/api/wire_gen.go @@ -8,10 +8,9 @@ package main import ( "context" - "log/slog" - "github.com/onkernel/hypeman/cmd/api/api" "github.com/onkernel/hypeman/cmd/api/config" + "github.com/onkernel/hypeman/lib/builds" "github.com/onkernel/hypeman/lib/devices" "github.com/onkernel/hypeman/lib/images" "github.com/onkernel/hypeman/lib/ingress" @@ -21,7 +20,10 @@ import ( "github.com/onkernel/hypeman/lib/registry" "github.com/onkernel/hypeman/lib/system" "github.com/onkernel/hypeman/lib/volumes" + "log/slog" +) +import ( _ "embed" ) @@ -52,11 +54,15 @@ func initializeApp() (*application, func(), error) { if err != nil { return nil, nil, err } + buildsManager, err := providers.ProvideBuildManager(paths, config, instancesManager, volumesManager, logger) + if err != nil { + return nil, nil, err + } registry, err := providers.ProvideRegistry(paths, manager) if err != nil { return nil, nil, err } - apiService := api.New(config, manager, instancesManager, volumesManager, networkManager, devicesManager, ingressManager) + apiService := api.New(config, manager, instancesManager, volumesManager, networkManager, devicesManager, ingressManager, buildsManager) mainApplication := &application{ Ctx: context, Logger: logger, @@ -68,6 +74,7 @@ func initializeApp() (*application, func(), error) { InstanceManager: instancesManager, VolumeManager: volumesManager, IngressManager: ingressManager, + BuildManager: buildsManager, Registry: registry, ApiService: apiService, } @@ -89,6 +96,7 @@ type application struct { InstanceManager instances.Manager VolumeManager volumes.Manager IngressManager ingress.Manager + BuildManager builds.Manager Registry *registry.Registry ApiService *api.ApiService } diff --git a/go.sum b/go.sum index 6772c9ed..7bab349e 100644 --- a/go.sum +++ b/go.sum @@ -92,6 +92,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-containerregistry v0.20.6 h1:cvWX87UxxLgaH76b4hIvya6Dzz9qHB31qAwjAohdSTU= github.com/google/go-containerregistry v0.20.6/go.mod h1:T0x8MuoAoKX/873bkeSfLD2FAkwCDf9/HZgsFJ02E2Y= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/lib/builds/README.md b/lib/builds/README.md new file mode 100644 index 00000000..575ef2a0 --- /dev/null +++ b/lib/builds/README.md @@ -0,0 +1,232 @@ +# Build System + +The build system provides source-to-image builds inside ephemeral Cloud Hypervisor microVMs, enabling secure multi-tenant isolation with rootless BuildKit. + +## Architecture + +``` +┌─────────────────────────────────────────────────────────────────┐ +│ Hypeman API │ +│ POST /v1/builds → BuildManager → BuildQueue │ +└─────────────────────────────────────────────────────────────────┘ + │ + ▼ +┌─────────────────────────────────────────────────────────────────┐ +│ Builder MicroVM │ +│ ┌─────────────────────────────────────────────────────────────┐│ +│ │ Builder Agent ││ +│ │ ┌─────────────┐ ┌──────────────┐ ┌────────────────────┐ ││ +│ │ │ Load Config │→ │ Generate │→ │ Run BuildKit │ ││ +│ │ │ from disk │ │ Dockerfile │ │ (rootless) │ ││ +│ │ └─────────────┘ └──────────────┘ └────────────────────┘ ││ +│ │ │ ││ +│ │ ▼ ││ +│ │ Push to Registry ││ +│ │ │ ││ +│ │ ▼ ││ +│ │ Report via vsock ││ +│ └─────────────────────────────────────────────────────────────┘│ +└─────────────────────────────────────────────────────────────────┘ + │ + ▼ +┌─────────────────────────────────────────────────────────────────┐ +│ OCI Registry │ +│ localhost:8080/builds/{build-id} │ +└─────────────────────────────────────────────────────────────────┘ +``` + +## Components + +### Core Types (`types.go`) + +| Type | Description | +|------|-------------| +| `Build` | Build job status and metadata | +| `CreateBuildRequest` | API request to create a build | +| `BuildConfig` | Configuration passed to builder VM | +| `BuildResult` | Result returned by builder agent | +| `BuildProvenance` | Audit trail for reproducibility | +| `BuildPolicy` | Resource limits and network policy | + +### Build Queue (`queue.go`) + +In-memory queue with configurable concurrency: + +```go +queue := NewBuildQueue(maxConcurrent) +position := queue.Enqueue(buildID, request, startFunc) +queue.Cancel(buildID) +queue.GetPosition(buildID) +``` + +**Recovery**: On startup, `listPendingBuilds()` scans disk metadata for incomplete builds and re-enqueues them in FIFO order. + +### Storage (`storage.go`) + +Builds are persisted to `$DATA_DIR/builds/{id}/`: + +``` +builds/ +└── {build-id}/ + ├── metadata.json # Build status, provenance + ├── config.json # Config for builder VM + ├── source/ + │ └── source.tar.gz + └── logs/ + └── build.log +``` + +### Build Manager (`manager.go`) + +Orchestrates the build lifecycle: + +1. Validate request and store source +2. Enqueue build job +3. Create builder VM with source volume attached +4. Wait for result via vsock +5. Update metadata and cleanup + +### Dockerfile Templates (`templates/`) + +Auto-generates Dockerfiles based on runtime and detected lockfiles: + +| Runtime | Package Managers | +|---------|-----------------| +| `nodejs20` | npm, yarn, pnpm | +| `python312` | pip, poetry, pipenv | + +```go +gen, _ := templates.GetGenerator("nodejs20") +dockerfile, _ := gen.Generate(sourceDir, baseImageDigest) +``` + +### Cache System (`cache.go`) + +Registry-based caching with tenant isolation: + +``` +{registry}/cache/{tenant_scope}/{runtime}/{lockfile_hash} +``` + +```go +gen := NewCacheKeyGenerator("localhost:8080") +key, _ := gen.GenerateCacheKey("my-tenant", "nodejs20", lockfileHashes) +// key.ImportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/nodejs20/abc123" +// key.ExportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/nodejs20/abc123,mode=max" +``` + +### Builder Agent (`builder_agent/main.go`) + +Guest binary that runs inside builder VMs: + +1. Reads config from `/config/build.json` +2. Fetches secrets from host via vsock +3. Generates Dockerfile (if not provided) +4. Runs `buildctl-daemonless.sh` with cache flags +5. Computes provenance (lockfile hashes, source hash) +6. Reports result back via vsock + +## API Endpoints + +| Method | Path | Description | +|--------|------|-------------| +| `POST` | `/v1/builds` | Submit build (multipart form) | +| `GET` | `/v1/builds` | List all builds | +| `GET` | `/v1/builds/{id}` | Get build details | +| `DELETE` | `/v1/builds/{id}` | Cancel build | +| `GET` | `/v1/builds/{id}/logs` | Stream logs (SSE) | + +### Submit Build Example + +```bash +curl -X POST http://localhost:8080/v1/builds \ + -H "Authorization: Bearer $TOKEN" \ + -F "runtime=nodejs20" \ + -F "source=@source.tar.gz" \ + -F "cache_scope=tenant-123" \ + -F "timeout_seconds=300" +``` + +### Response + +```json +{ + "id": "abc123", + "status": "queued", + "runtime": "nodejs20", + "queue_position": 1, + "created_at": "2025-01-15T10:00:00Z" +} +``` + +## Configuration + +| Environment Variable | Default | Description | +|---------------------|---------|-------------| +| `MAX_CONCURRENT_SOURCE_BUILDS` | `2` | Max parallel builds | +| `BUILDER_IMAGE` | `hypeman/builder:latest` | Builder VM image | +| `REGISTRY_URL` | `localhost:8080` | Registry for built images | +| `BUILD_TIMEOUT` | `600` | Default timeout (seconds) | + +## Build Status Flow + +``` +queued → building → pushing → ready + ↘ ↗ + failed + ↑ + cancelled +``` + +## Security Model + +1. **Isolation**: Each build runs in a fresh microVM (Cloud Hypervisor) +2. **Rootless**: BuildKit runs without root privileges +3. **Network Control**: `network_mode: isolated` or `egress` with optional domain allowlist +4. **Secret Handling**: Secrets fetched via vsock, never written to disk in guest +5. **Cache Isolation**: Per-tenant cache scopes prevent cross-tenant cache poisoning + +## Builder Images + +Builder images are in `images/`: + +- `base/Dockerfile` - BuildKit base +- `nodejs20/Dockerfile` - Node.js 20 + BuildKit + agent +- `python312/Dockerfile` - Python 3.12 + BuildKit + agent + +Build and push: + +```bash +cd lib/builds/images/nodejs20 +docker build -t hypeman/builder-nodejs20:latest -f Dockerfile ../../../.. +``` + +## Provenance + +Each build records provenance for reproducibility: + +```json +{ + "base_image_digest": "sha256:abc123...", + "source_hash": "sha256:def456...", + "lockfile_hashes": { + "package-lock.json": "sha256:..." + }, + "toolchain_version": "v20.10.0", + "buildkit_version": "v0.12.0", + "timestamp": "2025-01-15T10:05:00Z" +} +``` + +## Testing + +```bash +# Run unit tests +go test ./lib/builds/... -v + +# Test specific components +go test ./lib/builds/queue_test.go ./lib/builds/queue.go ./lib/builds/types.go -v +go test ./lib/builds/cache_test.go ./lib/builds/cache.go ./lib/builds/types.go ./lib/builds/errors.go -v +go test ./lib/builds/templates/... -v +``` + diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go new file mode 100644 index 00000000..19df99c2 --- /dev/null +++ b/lib/builds/builder_agent/main.go @@ -0,0 +1,497 @@ +// Package main implements the builder agent that runs inside builder microVMs. +// It reads build configuration from the config disk, runs BuildKit to build +// the image, and reports results back to the host via vsock. +package main + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "log" + "net" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + + "github.com/mdlayher/vsock" +) + +const ( + configPath = "/config/build.json" + vsockPort = 5001 // Build agent port (different from exec agent) + hostCID = 2 // VMADDR_CID_HOST +) + +// BuildConfig matches the BuildConfig type from lib/builds/types.go +type BuildConfig struct { + JobID string `json:"job_id"` + Runtime string `json:"runtime"` + BaseImageDigest string `json:"base_image_digest,omitempty"` + RegistryURL string `json:"registry_url"` + CacheScope string `json:"cache_scope,omitempty"` + SourcePath string `json:"source_path"` + Dockerfile string `json:"dockerfile,omitempty"` + BuildArgs map[string]string `json:"build_args,omitempty"` + Secrets []SecretRef `json:"secrets,omitempty"` + TimeoutSeconds int `json:"timeout_seconds"` + NetworkMode string `json:"network_mode"` +} + +// SecretRef references a secret to inject during build +type SecretRef struct { + ID string `json:"id"` + EnvVar string `json:"env_var,omitempty"` +} + +// BuildResult is sent back to the host +type BuildResult struct { + Success bool `json:"success"` + ImageDigest string `json:"image_digest,omitempty"` + Error string `json:"error,omitempty"` + Logs string `json:"logs,omitempty"` + Provenance BuildProvenance `json:"provenance"` + DurationMS int64 `json:"duration_ms"` +} + +// BuildProvenance records build inputs +type BuildProvenance struct { + BaseImageDigest string `json:"base_image_digest"` + SourceHash string `json:"source_hash"` + LockfileHashes map[string]string `json:"lockfile_hashes,omitempty"` + ToolchainVersion string `json:"toolchain_version,omitempty"` + BuildkitVersion string `json:"buildkit_version,omitempty"` + Timestamp time.Time `json:"timestamp"` +} + +// VsockMessage is the envelope for vsock communication +type VsockMessage struct { + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` +} + +func main() { + start := time.Now() + var logs bytes.Buffer + logWriter := io.MultiWriter(os.Stdout, &logs) + + log.SetOutput(logWriter) + log.Println("=== Builder Agent Starting ===") + + // Load build config + config, err := loadConfig() + if err != nil { + sendResult(BuildResult{ + Success: false, + Error: fmt.Sprintf("load config: %v", err), + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + os.Exit(1) + } + log.Printf("Job: %s, Runtime: %s", config.JobID, config.Runtime) + + // Setup timeout context + ctx := context.Background() + if config.TimeoutSeconds > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, time.Duration(config.TimeoutSeconds)*time.Second) + defer cancel() + } + + // Fetch secrets from host if needed + if err := fetchSecrets(ctx, config.Secrets); err != nil { + sendResult(BuildResult{ + Success: false, + Error: fmt.Sprintf("fetch secrets: %v", err), + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + os.Exit(1) + } + + // Generate Dockerfile if not provided + dockerfile := config.Dockerfile + if dockerfile == "" { + dockerfile, err = generateDockerfile(config) + if err != nil { + sendResult(BuildResult{ + Success: false, + Error: fmt.Sprintf("generate dockerfile: %v", err), + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + os.Exit(1) + } + // Write generated Dockerfile + dockerfilePath := filepath.Join(config.SourcePath, "Dockerfile") + if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil { + sendResult(BuildResult{ + Success: false, + Error: fmt.Sprintf("write dockerfile: %v", err), + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + os.Exit(1) + } + log.Println("Generated Dockerfile for runtime:", config.Runtime) + } + + // Compute provenance + provenance := computeProvenance(config) + + // Run the build + log.Println("=== Starting Build ===") + digest, buildLogs, err := runBuild(ctx, config, logWriter) + logs.WriteString(buildLogs) + + duration := time.Since(start).Milliseconds() + + if err != nil { + sendResult(BuildResult{ + Success: false, + Error: err.Error(), + Logs: logs.String(), + Provenance: provenance, + DurationMS: duration, + }) + os.Exit(1) + } + + // Success! + log.Printf("=== Build Complete: %s ===", digest) + provenance.Timestamp = time.Now() + + sendResult(BuildResult{ + Success: true, + ImageDigest: digest, + Logs: logs.String(), + Provenance: provenance, + DurationMS: duration, + }) +} + +func loadConfig() (*BuildConfig, error) { + data, err := os.ReadFile(configPath) + if err != nil { + return nil, err + } + var config BuildConfig + if err := json.Unmarshal(data, &config); err != nil { + return nil, err + } + return &config, nil +} + +func generateDockerfile(config *BuildConfig) (string, error) { + switch { + case strings.HasPrefix(config.Runtime, "nodejs"): + return generateNodeDockerfile(config) + case strings.HasPrefix(config.Runtime, "python"): + return generatePythonDockerfile(config) + default: + return "", fmt.Errorf("unsupported runtime: %s", config.Runtime) + } +} + +func generateNodeDockerfile(config *BuildConfig) (string, error) { + version := strings.TrimPrefix(config.Runtime, "nodejs") + baseImage := config.BaseImageDigest + if baseImage == "" { + baseImage = fmt.Sprintf("node:%s-alpine", version) + } + + // Detect lockfile + lockfile := "package-lock.json" + installCmd := "npm ci" + if _, err := os.Stat(filepath.Join(config.SourcePath, "pnpm-lock.yaml")); err == nil { + lockfile = "pnpm-lock.yaml" + installCmd = "corepack enable && pnpm install --frozen-lockfile" + } else if _, err := os.Stat(filepath.Join(config.SourcePath, "yarn.lock")); err == nil { + lockfile = "yarn.lock" + installCmd = "yarn install --frozen-lockfile" + } + + return fmt.Sprintf(`FROM %s + +WORKDIR /app + +COPY package.json %s ./ + +RUN %s + +COPY . . + +CMD ["node", "index.js"] +`, baseImage, lockfile, installCmd), nil +} + +func generatePythonDockerfile(config *BuildConfig) (string, error) { + version := strings.TrimPrefix(config.Runtime, "python") + baseImage := config.BaseImageDigest + if baseImage == "" { + baseImage = fmt.Sprintf("python:%s-slim", version) + } + + reqPath := filepath.Join(config.SourcePath, "requirements.txt") + hasHashes := false + if data, err := os.ReadFile(reqPath); err == nil { + hasHashes = strings.Contains(string(data), "--hash=") + } + + var installCmd string + if hasHashes { + installCmd = "pip install --require-hashes --only-binary :all: -r requirements.txt" + } else { + installCmd = "pip install --no-cache-dir -r requirements.txt" + } + + return fmt.Sprintf(`FROM %s + +WORKDIR /app + +COPY requirements.txt ./ + +RUN %s + +COPY . . + +CMD ["python", "main.py"] +`, baseImage, installCmd), nil +} + +func runBuild(ctx context.Context, config *BuildConfig, logWriter io.Writer) (string, string, error) { + var buildLogs bytes.Buffer + + // Build output reference + outputRef := fmt.Sprintf("%s/builds/%s", config.RegistryURL, config.JobID) + + // Build arguments + args := []string{ + "build", + "--frontend", "dockerfile.v0", + "--local", "context=" + config.SourcePath, + "--local", "dockerfile=" + config.SourcePath, + "--output", fmt.Sprintf("type=image,name=%s,push=true", outputRef), + "--metadata-file", "/tmp/build-metadata.json", + } + + // Add cache if scope is set + if config.CacheScope != "" { + cacheRef := fmt.Sprintf("%s/cache/%s", config.RegistryURL, config.CacheScope) + args = append(args, "--import-cache", fmt.Sprintf("type=registry,ref=%s", cacheRef)) + args = append(args, "--export-cache", fmt.Sprintf("type=registry,ref=%s,mode=max", cacheRef)) + } + + // Add secret mounts + for _, secret := range config.Secrets { + secretPath := fmt.Sprintf("/run/secrets/%s", secret.ID) + args = append(args, "--secret", fmt.Sprintf("id=%s,src=%s", secret.ID, secretPath)) + } + + // Add build args + for k, v := range config.BuildArgs { + args = append(args, "--opt", fmt.Sprintf("build-arg:%s=%s", k, v)) + } + + log.Printf("Running: buildctl-daemonless.sh %s", strings.Join(args, " ")) + + // Run buildctl-daemonless.sh + cmd := exec.CommandContext(ctx, "buildctl-daemonless.sh", args...) + cmd.Stdout = io.MultiWriter(logWriter, &buildLogs) + cmd.Stderr = io.MultiWriter(logWriter, &buildLogs) + cmd.Env = append(os.Environ(), "BUILDKITD_FLAGS=--oci-worker-no-process-sandbox") + + if err := cmd.Run(); err != nil { + return "", buildLogs.String(), fmt.Errorf("buildctl failed: %w", err) + } + + // Extract digest from metadata + digest, err := extractDigest("/tmp/build-metadata.json") + if err != nil { + return "", buildLogs.String(), fmt.Errorf("extract digest: %w", err) + } + + return digest, buildLogs.String(), nil +} + +func extractDigest(metadataPath string) (string, error) { + data, err := os.ReadFile(metadataPath) + if err != nil { + return "", err + } + + var metadata struct { + ContainerImageDigest string `json:"containerimage.digest"` + } + if err := json.Unmarshal(data, &metadata); err != nil { + return "", err + } + + if metadata.ContainerImageDigest == "" { + return "", fmt.Errorf("no digest in metadata") + } + + return metadata.ContainerImageDigest, nil +} + +func computeProvenance(config *BuildConfig) BuildProvenance { + prov := BuildProvenance{ + BaseImageDigest: config.BaseImageDigest, + LockfileHashes: make(map[string]string), + BuildkitVersion: getBuildkitVersion(), + ToolchainVersion: getToolchainVersion(config.Runtime), + } + + // Hash lockfiles + lockfiles := []string{ + "package-lock.json", "yarn.lock", "pnpm-lock.yaml", + "requirements.txt", "poetry.lock", "Pipfile.lock", + } + for _, lf := range lockfiles { + path := filepath.Join(config.SourcePath, lf) + if hash, err := hashFile(path); err == nil { + prov.LockfileHashes[lf] = hash + } + } + + // Hash source directory + prov.SourceHash, _ = hashDirectory(config.SourcePath) + + return prov +} + +func hashFile(path string) (string, error) { + data, err := os.ReadFile(path) + if err != nil { + return "", err + } + sum := sha256.Sum256(data) + return hex.EncodeToString(sum[:]), nil +} + +func hashDirectory(path string) (string, error) { + h := sha256.New() + err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error { + if err != nil { + return nil + } + if info.IsDir() { + return nil + } + // Skip Dockerfile (generated) and hidden files + name := filepath.Base(p) + if name == "Dockerfile" || strings.HasPrefix(name, ".") { + return nil + } + data, err := os.ReadFile(p) + if err != nil { + return nil + } + relPath, _ := filepath.Rel(path, p) + h.Write([]byte(relPath)) + h.Write(data) + return nil + }) + if err != nil { + return "", err + } + return hex.EncodeToString(h.Sum(nil)), nil +} + +func getBuildkitVersion() string { + cmd := exec.Command("buildctl", "--version") + out, _ := cmd.Output() + return strings.TrimSpace(string(out)) +} + +func getToolchainVersion(runtime string) string { + switch { + case strings.HasPrefix(runtime, "nodejs"): + out, _ := exec.Command("node", "--version").Output() + return strings.TrimSpace(string(out)) + case strings.HasPrefix(runtime, "python"): + out, _ := exec.Command("python", "--version").Output() + return strings.TrimSpace(string(out)) + } + return "unknown" +} + +func fetchSecrets(ctx context.Context, secrets []SecretRef) error { + if len(secrets) == 0 { + return nil + } + + conn, err := dialVsock() + if err != nil { + return fmt.Errorf("dial vsock: %w", err) + } + defer conn.Close() + + // Request secrets + secretIDs := make([]string, len(secrets)) + for i, s := range secrets { + secretIDs[i] = s.ID + } + + req := VsockMessage{ + Type: "get_secrets", + } + if err := json.NewEncoder(conn).Encode(req); err != nil { + return err + } + + // Receive response + var resp struct { + Secrets map[string]string `json:"secrets"` + } + if err := json.NewDecoder(conn).Decode(&resp); err != nil { + return err + } + + // Write secrets to files + if err := os.MkdirAll("/run/secrets", 0700); err != nil { + return err + } + for _, s := range secrets { + value, ok := resp.Secrets[s.ID] + if !ok { + return fmt.Errorf("secret not found: %s", s.ID) + } + path := fmt.Sprintf("/run/secrets/%s", s.ID) + if err := os.WriteFile(path, []byte(value), 0600); err != nil { + return err + } + } + + return nil +} + +func sendResult(result BuildResult) { + conn, err := dialVsock() + if err != nil { + log.Printf("Failed to dial vsock: %v", err) + return + } + defer conn.Close() + + msg := VsockMessage{ + Type: "build_result", + Result: &result, + } + + if err := json.NewEncoder(conn).Encode(msg); err != nil { + log.Printf("Failed to send result: %v", err) + } +} + +func dialVsock() (net.Conn, error) { + return vsock.Dial(hostCID, vsockPort, nil) +} + diff --git a/lib/builds/cache.go b/lib/builds/cache.go new file mode 100644 index 00000000..c044f03c --- /dev/null +++ b/lib/builds/cache.go @@ -0,0 +1,176 @@ +package builds + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" + "regexp" + "strings" +) + +// CacheKeyGenerator generates cache keys for builds with tenant isolation +type CacheKeyGenerator struct { + registryURL string +} + +// NewCacheKeyGenerator creates a new cache key generator +func NewCacheKeyGenerator(registryURL string) *CacheKeyGenerator { + return &CacheKeyGenerator{registryURL: registryURL} +} + +// CacheKey represents a validated cache key +type CacheKey struct { + // Full reference for BuildKit --import-cache / --export-cache + Reference string + + // Components + TenantScope string + Runtime string + LockfileHash string +} + +// GenerateCacheKey generates a cache key for a build. +// +// Cache key structure: +// {registry}/cache/{tenant_scope}/{runtime}/{lockfile_hash} +// +// This structure provides: +// - Tenant isolation: each tenant's cache is isolated by scope +// - Runtime separation: Node.js and Python caches don't mix +// - Lockfile-based keying: same lockfile = cache hit +func (g *CacheKeyGenerator) GenerateCacheKey(tenantScope, runtime string, lockfileHashes map[string]string) (*CacheKey, error) { + if tenantScope == "" { + return nil, fmt.Errorf("tenant scope is required for caching") + } + + if !IsSupportedRuntime(runtime) { + return nil, fmt.Errorf("unsupported runtime: %s", runtime) + } + + // Normalize tenant scope (alphanumeric + hyphen only) + normalizedScope := normalizeCacheScope(tenantScope) + if normalizedScope == "" { + return nil, fmt.Errorf("invalid tenant scope: %s", tenantScope) + } + + // Compute lockfile hash from all lockfile hashes + lockfileHash := computeCombinedHash(lockfileHashes) + + // Build the reference + reference := fmt.Sprintf("%s/cache/%s/%s/%s", + g.registryURL, + normalizedScope, + runtime, + lockfileHash[:16], // Use first 16 chars for brevity + ) + + return &CacheKey{ + Reference: reference, + TenantScope: normalizedScope, + Runtime: runtime, + LockfileHash: lockfileHash, + }, nil +} + +// ValidateCacheScope validates that a cache scope is safe to use +func ValidateCacheScope(scope string) error { + if scope == "" { + return fmt.Errorf("cache scope is required") + } + + normalized := normalizeCacheScope(scope) + if normalized == "" { + return fmt.Errorf("cache scope contains only invalid characters") + } + + if len(normalized) < 3 { + return fmt.Errorf("cache scope must be at least 3 characters") + } + + if len(normalized) > 64 { + return fmt.Errorf("cache scope must be at most 64 characters") + } + + return nil +} + +// ImportCacheArg returns the BuildKit --import-cache argument +func (k *CacheKey) ImportCacheArg() string { + return fmt.Sprintf("type=registry,ref=%s", k.Reference) +} + +// ExportCacheArg returns the BuildKit --export-cache argument +func (k *CacheKey) ExportCacheArg() string { + return fmt.Sprintf("type=registry,ref=%s,mode=max", k.Reference) +} + +// normalizeCacheScope normalizes a cache scope to only contain safe characters +// for use in registry paths (alphanumeric and hyphens) +func normalizeCacheScope(scope string) string { + // Convert to lowercase and replace unsafe characters + scope = strings.ToLower(scope) + + // Keep only alphanumeric and hyphens + re := regexp.MustCompile(`[^a-z0-9-]`) + normalized := re.ReplaceAllString(scope, "-") + + // Remove consecutive hyphens + re = regexp.MustCompile(`-+`) + normalized = re.ReplaceAllString(normalized, "-") + + // Trim leading/trailing hyphens + normalized = strings.Trim(normalized, "-") + + return normalized +} + +// computeCombinedHash computes a combined hash from multiple lockfile hashes +func computeCombinedHash(lockfileHashes map[string]string) string { + if len(lockfileHashes) == 0 { + return "empty" + } + + // Sort keys for determinism + h := sha256.New() + for _, name := range sortedKeys(lockfileHashes) { + h.Write([]byte(name)) + h.Write([]byte(":")) + h.Write([]byte(lockfileHashes[name])) + h.Write([]byte("\n")) + } + + return hex.EncodeToString(h.Sum(nil)) +} + +// sortedKeys returns the keys of a map in sorted order +func sortedKeys(m map[string]string) []string { + keys := make([]string, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + // Simple bubble sort for small maps (lockfiles are typically 1-3) + for i := 0; i < len(keys)-1; i++ { + for j := i + 1; j < len(keys); j++ { + if keys[i] > keys[j] { + keys[i], keys[j] = keys[j], keys[i] + } + } + } + return keys +} + +// GetCacheKeyFromConfig extracts cache configuration for the builder agent +func GetCacheKeyFromConfig(registryURL, cacheScope, runtime string, lockfileHashes map[string]string) (importArg, exportArg string, err error) { + if cacheScope == "" { + return "", "", nil // Caching disabled + } + + gen := NewCacheKeyGenerator(registryURL) + key, err := gen.GenerateCacheKey(cacheScope, runtime, lockfileHashes) + if err != nil { + return "", "", err + } + + return key.ImportCacheArg(), key.ExportCacheArg(), nil +} + diff --git a/lib/builds/cache_test.go b/lib/builds/cache_test.go new file mode 100644 index 00000000..a776eeee --- /dev/null +++ b/lib/builds/cache_test.go @@ -0,0 +1,188 @@ +package builds + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { + gen := NewCacheKeyGenerator("localhost:8080") + + tests := []struct { + name string + tenantScope string + runtime string + lockfileHashes map[string]string + wantErr bool + wantPrefix string + }{ + { + name: "valid nodejs build", + tenantScope: "tenant-abc", + runtime: "nodejs20", + lockfileHashes: map[string]string{ + "package-lock.json": "abc123", + }, + wantPrefix: "localhost:8080/cache/tenant-abc/nodejs20/", + }, + { + name: "valid python build", + tenantScope: "my-team", + runtime: "python312", + lockfileHashes: map[string]string{ + "requirements.txt": "def456", + }, + wantPrefix: "localhost:8080/cache/my-team/python312/", + }, + { + name: "empty tenant scope", + tenantScope: "", + runtime: "nodejs20", + wantErr: true, + }, + { + name: "invalid runtime", + tenantScope: "tenant", + runtime: "ruby", + wantErr: true, + }, + { + name: "scope with special chars", + tenantScope: "My Team!@#$%", + runtime: "nodejs20", + lockfileHashes: map[string]string{ + "package-lock.json": "abc", + }, + wantPrefix: "localhost:8080/cache/my-team/nodejs20/", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + key, err := gen.GenerateCacheKey(tt.tenantScope, tt.runtime, tt.lockfileHashes) + + if tt.wantErr { + require.Error(t, err) + return + } + + require.NoError(t, err) + assert.Contains(t, key.Reference, tt.wantPrefix) + }) + } +} + +func TestCacheKey_Args(t *testing.T) { + key := &CacheKey{ + Reference: "localhost:8080/cache/tenant/nodejs20/abc123", + TenantScope: "tenant", + Runtime: "nodejs20", + LockfileHash: "abc123", + } + + importArg := key.ImportCacheArg() + assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs20/abc123", importArg) + + exportArg := key.ExportCacheArg() + assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs20/abc123,mode=max", exportArg) +} + +func TestValidateCacheScope(t *testing.T) { + tests := []struct { + scope string + wantErr bool + }{ + {"valid-scope", false}, + {"abc", false}, + {"my-team-123", false}, + {"", true}, // Empty + {"ab", true}, // Too short + {"a", true}, // Too short + {string(make([]byte, 65)), true}, // Too long + } + + for _, tt := range tests { + t.Run(tt.scope, func(t *testing.T) { + err := ValidateCacheScope(tt.scope) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestNormalizeCacheScope(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"simple", "simple"}, + {"with-hyphens", "with-hyphens"}, + {"MixedCase", "mixedcase"}, + {"with spaces", "with-spaces"}, + {"special!@#chars", "special-chars"}, + {"multiple---hyphens", "multiple-hyphens"}, + {"-leading-trailing-", "leading-trailing"}, + {"123numbers", "123numbers"}, + } + + for _, tt := range tests { + t.Run(tt.input, func(t *testing.T) { + result := normalizeCacheScope(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestComputeCombinedHash(t *testing.T) { + // Same inputs should produce same hash + hash1 := computeCombinedHash(map[string]string{ + "package-lock.json": "abc123", + "yarn.lock": "def456", + }) + hash2 := computeCombinedHash(map[string]string{ + "yarn.lock": "def456", + "package-lock.json": "abc123", + }) + assert.Equal(t, hash1, hash2, "hash should be deterministic regardless of map order") + + // Different inputs should produce different hashes + hash3 := computeCombinedHash(map[string]string{ + "package-lock.json": "different", + }) + assert.NotEqual(t, hash1, hash3) + + // Empty map should return "empty" + emptyHash := computeCombinedHash(map[string]string{}) + assert.Equal(t, "empty", emptyHash) +} + +func TestGetCacheKeyFromConfig(t *testing.T) { + // With cache scope + importArg, exportArg, err := GetCacheKeyFromConfig( + "localhost:8080", + "my-tenant", + "nodejs20", + map[string]string{"package-lock.json": "abc"}, + ) + require.NoError(t, err) + assert.NotEmpty(t, importArg) + assert.NotEmpty(t, exportArg) + assert.Contains(t, importArg, "type=registry") + assert.Contains(t, exportArg, "mode=max") + + // Without cache scope (caching disabled) + importArg, exportArg, err = GetCacheKeyFromConfig( + "localhost:8080", + "", // Empty = no caching + "nodejs20", + nil, + ) + require.NoError(t, err) + assert.Empty(t, importArg) + assert.Empty(t, exportArg) +} diff --git a/lib/builds/errors.go b/lib/builds/errors.go new file mode 100644 index 00000000..a5c1dfd0 --- /dev/null +++ b/lib/builds/errors.go @@ -0,0 +1,46 @@ +package builds + +import "errors" + +var ( + // ErrNotFound is returned when a build is not found + ErrNotFound = errors.New("build not found") + + // ErrAlreadyExists is returned when a build with the same ID already exists + ErrAlreadyExists = errors.New("build already exists") + + // ErrInvalidRuntime is returned when an unsupported runtime is specified + ErrInvalidRuntime = errors.New("invalid runtime") + + // ErrBuildFailed is returned when a build fails + ErrBuildFailed = errors.New("build failed") + + // ErrBuildTimeout is returned when a build exceeds its timeout + ErrBuildTimeout = errors.New("build timeout") + + // ErrBuildCancelled is returned when a build is cancelled + ErrBuildCancelled = errors.New("build cancelled") + + // ErrInvalidSource is returned when the source tarball is invalid + ErrInvalidSource = errors.New("invalid source") + + // ErrSourceHashMismatch is returned when the source hash doesn't match + ErrSourceHashMismatch = errors.New("source hash mismatch") + + // ErrBuilderNotReady is returned when the builder image is not available + ErrBuilderNotReady = errors.New("builder image not ready") + + // ErrBuildInProgress is returned when trying to cancel a build that's already complete + ErrBuildInProgress = errors.New("build in progress") +) + +// IsSupportedRuntime returns true if the runtime is supported +func IsSupportedRuntime(runtime string) bool { + switch runtime { + case RuntimeNodeJS20, RuntimePython312: + return true + default: + return false + } +} + diff --git a/lib/builds/images/base/Dockerfile b/lib/builds/images/base/Dockerfile new file mode 100644 index 00000000..07c495f3 --- /dev/null +++ b/lib/builds/images/base/Dockerfile @@ -0,0 +1,29 @@ +# Base builder image with rootless BuildKit +# This serves as the foundation for all runtime-specific builder images + +FROM moby/buildkit:rootless + +# Switch to root temporarily to install additional packages +USER root + +# Install common build dependencies +RUN apk add --no-cache \ + ca-certificates \ + git \ + curl \ + jq \ + tar \ + gzip + +# Create directories for the builder agent +RUN mkdir -p /config /run/secrets + +# Switch back to unprivileged user +USER 1000 + +# Set buildkit flags for rootless operation +ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" + +# Default entrypoint is buildkitd, but we'll override for builder-agent +ENTRYPOINT ["/usr/bin/buildctl-daemonless.sh"] + diff --git a/lib/builds/images/nodejs20/Dockerfile b/lib/builds/images/nodejs20/Dockerfile new file mode 100644 index 00000000..d5665145 --- /dev/null +++ b/lib/builds/images/nodejs20/Dockerfile @@ -0,0 +1,58 @@ +# Node.js 20 Builder Image +# Contains rootless BuildKit + Node.js toolchain + builder agent + +FROM moby/buildkit:rootless AS buildkit + +# Build the builder-agent (multi-stage build from hypeman repo) +FROM golang:1.25-alpine AS agent-builder + +WORKDIR /app +COPY lib/builds/builder_agent/ ./ +RUN go build -ldflags="-s -w" -o /builder-agent . + +# Final builder image +FROM node:20-alpine + +# Copy BuildKit from official image +COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl +COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh +COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd + +# Copy builder agent +COPY --from=agent-builder /builder-agent /usr/bin/builder-agent + +# Install additional dependencies +RUN apk add --no-cache \ + ca-certificates \ + git \ + curl \ + jq \ + tar \ + gzip \ + shadow \ + fuse-overlayfs + +# Create unprivileged user for rootless BuildKit +RUN adduser -D -u 1000 builder && \ + mkdir -p /home/builder/.local/share/buildkit && \ + chown -R builder:builder /home/builder + +# Create directories for build +RUN mkdir -p /config /run/secrets /src && \ + chown -R builder:builder /config /run/secrets /src + +# Enable corepack for pnpm/yarn support +RUN corepack enable + +# Switch to unprivileged user +USER builder +WORKDIR /src + +# Set environment for rootless buildkit +ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" +ENV HOME=/home/builder +ENV XDG_RUNTIME_DIR=/home/builder/.local/share + +# Run builder agent as entrypoint +ENTRYPOINT ["/usr/bin/builder-agent"] + diff --git a/lib/builds/images/python312/Dockerfile b/lib/builds/images/python312/Dockerfile new file mode 100644 index 00000000..bf6266eb --- /dev/null +++ b/lib/builds/images/python312/Dockerfile @@ -0,0 +1,61 @@ +# Python 3.12 Builder Image +# Contains rootless BuildKit + Python toolchain + builder agent + +FROM moby/buildkit:rootless AS buildkit + +# Build the builder-agent (multi-stage build from hypeman repo) +FROM golang:1.25-alpine AS agent-builder + +WORKDIR /app +COPY lib/builds/builder_agent/ ./ +RUN go build -ldflags="-s -w" -o /builder-agent . + +# Final builder image +FROM python:3.12-slim + +# Copy BuildKit from official image +COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl +COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh +COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd + +# Copy builder agent +COPY --from=agent-builder /builder-agent /usr/bin/builder-agent + +# Install additional dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ + git \ + curl \ + jq \ + tar \ + gzip \ + fuse-overlayfs \ + && rm -rf /var/lib/apt/lists/* + +# Create unprivileged user for rootless BuildKit +RUN useradd -m -u 1000 builder && \ + mkdir -p /home/builder/.local/share/buildkit && \ + chown -R builder:builder /home/builder + +# Create directories for build +RUN mkdir -p /config /run/secrets /src && \ + chown -R builder:builder /config /run/secrets /src + +# Install common Python tools +RUN pip install --no-cache-dir \ + pip-tools \ + poetry \ + pipenv + +# Switch to unprivileged user +USER builder +WORKDIR /src + +# Set environment for rootless buildkit +ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" +ENV HOME=/home/builder +ENV XDG_RUNTIME_DIR=/home/builder/.local/share + +# Run builder agent as entrypoint +ENTRYPOINT ["/usr/bin/builder-agent"] + diff --git a/lib/builds/manager.go b/lib/builds/manager.go new file mode 100644 index 00000000..0f54539c --- /dev/null +++ b/lib/builds/manager.go @@ -0,0 +1,529 @@ +package builds + +import ( + "context" + "fmt" + "log/slog" + "os" + "sync" + "time" + + "github.com/nrednav/cuid2" + "github.com/onkernel/hypeman/lib/instances" + "github.com/onkernel/hypeman/lib/paths" + "github.com/onkernel/hypeman/lib/volumes" + "go.opentelemetry.io/otel/metric" +) + +// Manager interface for the build system +type Manager interface { + // CreateBuild starts a new build job + CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) + + // GetBuild returns a build by ID + GetBuild(ctx context.Context, id string) (*Build, error) + + // ListBuilds returns all builds + ListBuilds(ctx context.Context) ([]*Build, error) + + // CancelBuild cancels a pending or running build + CancelBuild(ctx context.Context, id string) error + + // GetBuildLogs returns the logs for a build + GetBuildLogs(ctx context.Context, id string) ([]byte, error) + + // RecoverPendingBuilds recovers builds that were interrupted on restart + RecoverPendingBuilds() +} + +// Config holds configuration for the build manager +type Config struct { + // MaxConcurrentBuilds is the maximum number of concurrent builds + MaxConcurrentBuilds int + + // BuilderImage is the OCI image to use for builder VMs + // This should contain rootless BuildKit and the builder agent + BuilderImage string + + // RegistryURL is the URL of the registry to push built images to + RegistryURL string + + // DefaultTimeout is the default build timeout in seconds + DefaultTimeout int +} + +// DefaultConfig returns the default build manager configuration +func DefaultConfig() Config { + return Config{ + MaxConcurrentBuilds: 2, + BuilderImage: "hypeman/builder:latest", + RegistryURL: "localhost:8080", + DefaultTimeout: 600, // 10 minutes + } +} + +type manager struct { + config Config + paths *paths.Paths + queue *BuildQueue + instanceManager instances.Manager + volumeManager volumes.Manager + secretProvider SecretProvider + vsockHandler *VsockHandler + logger *slog.Logger + metrics *Metrics + createMu sync.Mutex +} + +// NewManager creates a new build manager +func NewManager( + p *paths.Paths, + config Config, + instanceMgr instances.Manager, + volumeMgr volumes.Manager, + secretProvider SecretProvider, + logger *slog.Logger, + meter metric.Meter, +) (Manager, error) { + if logger == nil { + logger = slog.Default() + } + + m := &manager{ + config: config, + paths: p, + queue: NewBuildQueue(config.MaxConcurrentBuilds), + instanceManager: instanceMgr, + volumeManager: volumeMgr, + secretProvider: secretProvider, + vsockHandler: NewVsockHandler(secretProvider, logger), + logger: logger, + } + + // Initialize metrics if meter is provided + if meter != nil { + metrics, err := NewMetrics(meter) + if err != nil { + return nil, fmt.Errorf("create metrics: %w", err) + } + m.metrics = metrics + } + + // Recover any pending builds from disk + m.RecoverPendingBuilds() + + return m, nil +} + +// CreateBuild starts a new build job +func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) { + m.logger.Info("creating build", "runtime", req.Runtime) + + // Validate runtime + if !IsSupportedRuntime(req.Runtime) { + return nil, fmt.Errorf("%w: %s", ErrInvalidRuntime, req.Runtime) + } + + // Apply defaults to build policy + policy := req.BuildPolicy + if policy == nil { + defaultPolicy := DefaultBuildPolicy() + policy = &defaultPolicy + } else { + policy.ApplyDefaults() + } + + m.createMu.Lock() + defer m.createMu.Unlock() + + // Generate build ID + id := cuid2.Generate() + + // Create build metadata + meta := &buildMetadata{ + ID: id, + Status: StatusQueued, + Runtime: req.Runtime, + Request: &req, + CreatedAt: time.Now(), + } + + // Write initial metadata + if err := writeMetadata(m.paths, meta); err != nil { + return nil, fmt.Errorf("write metadata: %w", err) + } + + // Store source data + if err := m.storeSource(id, sourceData); err != nil { + deleteBuild(m.paths, id) + return nil, fmt.Errorf("store source: %w", err) + } + + // Write build config for the builder agent + buildConfig := &BuildConfig{ + JobID: id, + Runtime: req.Runtime, + BaseImageDigest: req.BaseImageDigest, + RegistryURL: m.config.RegistryURL, + CacheScope: req.CacheScope, + SourcePath: "/src", + Dockerfile: req.Dockerfile, + BuildArgs: req.BuildArgs, + Secrets: req.Secrets, + TimeoutSeconds: policy.TimeoutSeconds, + NetworkMode: policy.NetworkMode, + } + if err := writeBuildConfig(m.paths, id, buildConfig); err != nil { + deleteBuild(m.paths, id) + return nil, fmt.Errorf("write build config: %w", err) + } + + // Enqueue the build + queuePos := m.queue.Enqueue(id, req, func() { + m.runBuild(context.Background(), id, req, policy) + }) + + build := meta.toBuild() + if queuePos > 0 { + build.QueuePosition = &queuePos + } + + m.logger.Info("build created", "id", id, "queue_position", queuePos) + return build, nil +} + +// storeSource stores the source tarball for a build +func (m *manager) storeSource(buildID string, data []byte) error { + sourceDir := m.paths.BuildSourceDir(buildID) + if err := ensureDir(sourceDir); err != nil { + return err + } + + // Write source tarball + sourcePath := sourceDir + "/source.tar.gz" + return writeFile(sourcePath, data) +} + +// runBuild executes a build in a builder VM +func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildRequest, policy *BuildPolicy) { + start := time.Now() + m.logger.Info("starting build", "id", id) + + // Update status to building + m.updateStatus(id, StatusBuilding, nil) + + // Create timeout context + buildCtx, cancel := context.WithTimeout(ctx, time.Duration(policy.TimeoutSeconds)*time.Second) + defer cancel() + + // Run the build in a builder VM + result, err := m.executeBuild(buildCtx, id, req, policy) + + duration := time.Since(start) + durationMS := duration.Milliseconds() + + if err != nil { + m.logger.Error("build failed", "id", id, "error", err, "duration", duration) + errMsg := err.Error() + m.updateBuildComplete(id, StatusFailed, nil, &errMsg, nil, &durationMS) + if m.metrics != nil { + m.metrics.RecordBuild(ctx, "failed", req.Runtime, duration) + } + return + } + + if !result.Success { + m.logger.Error("build failed", "id", id, "error", result.Error, "duration", duration) + m.updateBuildComplete(id, StatusFailed, nil, &result.Error, &result.Provenance, &durationMS) + if m.metrics != nil { + m.metrics.RecordBuild(ctx, "failed", req.Runtime, duration) + } + return + } + + m.logger.Info("build succeeded", "id", id, "digest", result.ImageDigest, "duration", duration) + imageRef := fmt.Sprintf("%s/builds/%s", m.config.RegistryURL, id) + m.updateBuildComplete(id, StatusReady, &result.ImageDigest, nil, &result.Provenance, &durationMS) + + // Update with image ref + if meta, err := readMetadata(m.paths, id); err == nil { + meta.ImageRef = &imageRef + writeMetadata(m.paths, meta) + } + + if m.metrics != nil { + m.metrics.RecordBuild(ctx, "success", req.Runtime, duration) + } +} + +// executeBuild runs the build in a builder VM +func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRequest, policy *BuildPolicy) (*BuildResult, error) { + // Create a volume with the source data + sourceVolID := fmt.Sprintf("build-source-%s", id) + sourcePath := m.paths.BuildSourceDir(id) + "/source.tar.gz" + + // Open source tarball + sourceFile, err := os.Open(sourcePath) + if err != nil { + return nil, fmt.Errorf("open source: %w", err) + } + defer sourceFile.Close() + + // Create volume with source (using the volume manager's archive import) + _, err = m.volumeManager.CreateVolumeFromArchive(ctx, volumes.CreateVolumeFromArchiveRequest{ + Id: &sourceVolID, + Name: sourceVolID, + SizeGb: 10, // 10GB should be enough for most source bundles + }, sourceFile) + if err != nil { + return nil, fmt.Errorf("create source volume: %w", err) + } + defer m.volumeManager.DeleteVolume(ctx, sourceVolID) + + // Create builder instance + builderName := fmt.Sprintf("builder-%s", id) + networkEnabled := policy.NetworkMode == "egress" + + inst, err := m.instanceManager.CreateInstance(ctx, instances.CreateInstanceRequest{ + Name: builderName, + Image: m.config.BuilderImage, + Size: int64(policy.MemoryMB) * 1024 * 1024, + Vcpus: policy.CPUs, + NetworkEnabled: networkEnabled, + Volumes: []instances.VolumeAttachment{ + { + VolumeID: sourceVolID, + MountPath: "/src", + Readonly: true, + }, + }, + }) + if err != nil { + return nil, fmt.Errorf("create builder instance: %w", err) + } + + // Update metadata with builder instance + if meta, err := readMetadata(m.paths, id); err == nil { + meta.BuilderInstance = &inst.Id + writeMetadata(m.paths, meta) + } + + // Ensure cleanup + defer func() { + m.instanceManager.DeleteInstance(context.Background(), inst.Id) + }() + + // Wait for build result via vsock + // The builder agent will send the result when complete + result, err := m.waitForResult(ctx, inst) + if err != nil { + return nil, fmt.Errorf("wait for result: %w", err) + } + + return result, nil +} + +// waitForResult waits for the build result from the builder agent +func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) (*BuildResult, error) { + // Poll for the build result + // In a production system, you'd use vsock for real-time communication + // For now, we'll poll the instance state and check for completion + + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + + timeout := time.After(30 * time.Minute) // Maximum wait time + + for { + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-timeout: + return nil, ErrBuildTimeout + case <-ticker.C: + // Check if instance is still running + current, err := m.instanceManager.GetInstance(ctx, inst.Id) + if err != nil { + // Instance might have been deleted + return nil, fmt.Errorf("check instance: %w", err) + } + + // If instance stopped, check for result in logs + if current.State == instances.StateStopped || current.State == instances.StateShutdown { + // Try to parse result from logs + // This is a fallback - ideally vsock would be used + return &BuildResult{ + Success: false, + Error: "builder instance stopped unexpectedly", + }, nil + } + } + } +} + +// updateStatus updates the build status +func (m *manager) updateStatus(id string, status string, err error) { + meta, readErr := readMetadata(m.paths, id) + if readErr != nil { + m.logger.Error("read metadata for status update", "id", id, "error", readErr) + return + } + + meta.Status = status + if status == StatusBuilding && meta.StartedAt == nil { + now := time.Now() + meta.StartedAt = &now + } + if err != nil { + errMsg := err.Error() + meta.Error = &errMsg + } + + if writeErr := writeMetadata(m.paths, meta); writeErr != nil { + m.logger.Error("write metadata for status update", "id", id, "error", writeErr) + } +} + +// updateBuildComplete updates the build with final results +func (m *manager) updateBuildComplete(id string, status string, digest *string, errMsg *string, provenance *BuildProvenance, durationMS *int64) { + meta, readErr := readMetadata(m.paths, id) + if readErr != nil { + m.logger.Error("read metadata for completion", "id", id, "error", readErr) + return + } + + meta.Status = status + meta.ImageDigest = digest + meta.Error = errMsg + meta.Provenance = provenance + meta.DurationMS = durationMS + + now := time.Now() + meta.CompletedAt = &now + + if writeErr := writeMetadata(m.paths, meta); writeErr != nil { + m.logger.Error("write metadata for completion", "id", id, "error", writeErr) + } +} + +// GetBuild returns a build by ID +func (m *manager) GetBuild(ctx context.Context, id string) (*Build, error) { + meta, err := readMetadata(m.paths, id) + if err != nil { + return nil, err + } + + build := meta.toBuild() + + // Add queue position if queued + if meta.Status == StatusQueued { + build.QueuePosition = m.queue.GetPosition(id) + } + + return build, nil +} + +// ListBuilds returns all builds +func (m *manager) ListBuilds(ctx context.Context) ([]*Build, error) { + metas, err := listAllBuilds(m.paths) + if err != nil { + return nil, err + } + + builds := make([]*Build, 0, len(metas)) + for _, meta := range metas { + build := meta.toBuild() + if meta.Status == StatusQueued { + build.QueuePosition = m.queue.GetPosition(meta.ID) + } + builds = append(builds, build) + } + + return builds, nil +} + +// CancelBuild cancels a pending build +func (m *manager) CancelBuild(ctx context.Context, id string) error { + meta, err := readMetadata(m.paths, id) + if err != nil { + return err + } + + switch meta.Status { + case StatusQueued: + // Remove from queue + if m.queue.Cancel(id) { + m.updateStatus(id, StatusCancelled, nil) + return nil + } + return ErrBuildInProgress // Was already picked up + + case StatusBuilding, StatusPushing: + // Can't cancel a running build easily + // Would need to terminate the builder instance + if meta.BuilderInstance != nil { + m.instanceManager.DeleteInstance(ctx, *meta.BuilderInstance) + } + m.updateStatus(id, StatusCancelled, nil) + return nil + + case StatusReady, StatusFailed, StatusCancelled: + return fmt.Errorf("build already completed with status: %s", meta.Status) + + default: + return fmt.Errorf("unknown build status: %s", meta.Status) + } +} + +// GetBuildLogs returns the logs for a build +func (m *manager) GetBuildLogs(ctx context.Context, id string) ([]byte, error) { + _, err := readMetadata(m.paths, id) + if err != nil { + return nil, err + } + + return readLog(m.paths, id) +} + +// RecoverPendingBuilds recovers builds that were interrupted on restart +func (m *manager) RecoverPendingBuilds() { + pending, err := listPendingBuilds(m.paths) + if err != nil { + m.logger.Error("list pending builds for recovery", "error", err) + return + } + + for _, meta := range pending { + m.logger.Info("recovering build", "id", meta.ID, "status", meta.Status) + + // Re-enqueue the build + if meta.Request != nil { + m.queue.Enqueue(meta.ID, *meta.Request, func() { + policy := DefaultBuildPolicy() + if meta.Request.BuildPolicy != nil { + policy = *meta.Request.BuildPolicy + } + m.runBuild(context.Background(), meta.ID, *meta.Request, &policy) + }) + } + } + + if len(pending) > 0 { + m.logger.Info("recovered pending builds", "count", len(pending)) + } +} + +// Helper functions + +func ensureDir(path string) error { + return os.MkdirAll(path, 0755) +} + +func writeFile(path string, data []byte) error { + return os.WriteFile(path, data, 0644) +} + +func readFile(path string) ([]byte, error) { + return os.ReadFile(path) +} + diff --git a/lib/builds/metrics.go b/lib/builds/metrics.go new file mode 100644 index 00000000..f6c4227b --- /dev/null +++ b/lib/builds/metrics.go @@ -0,0 +1,86 @@ +package builds + +import ( + "context" + "time" + + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/metric" +) + +// Metrics provides Prometheus metrics for the build system +type Metrics struct { + buildDuration metric.Float64Histogram + buildTotal metric.Int64Counter + queueLength metric.Int64ObservableGauge + activeBuilds metric.Int64ObservableGauge +} + +// NewMetrics creates a new Metrics instance +func NewMetrics(meter metric.Meter) (*Metrics, error) { + buildDuration, err := meter.Float64Histogram( + "hypeman_build_duration_seconds", + metric.WithDescription("Duration of builds in seconds"), + metric.WithUnit("s"), + ) + if err != nil { + return nil, err + } + + buildTotal, err := meter.Int64Counter( + "hypeman_builds_total", + metric.WithDescription("Total number of builds"), + ) + if err != nil { + return nil, err + } + + queueLength, err := meter.Int64ObservableGauge( + "hypeman_build_queue_length", + metric.WithDescription("Number of builds in queue"), + ) + if err != nil { + return nil, err + } + + activeBuilds, err := meter.Int64ObservableGauge( + "hypeman_builds_active", + metric.WithDescription("Number of currently running builds"), + ) + if err != nil { + return nil, err + } + + return &Metrics{ + buildDuration: buildDuration, + buildTotal: buildTotal, + queueLength: queueLength, + activeBuilds: activeBuilds, + }, nil +} + +// RecordBuild records metrics for a completed build +func (m *Metrics) RecordBuild(ctx context.Context, status string, runtime string, duration time.Duration) { + attrs := []attribute.KeyValue{ + attribute.String("status", status), + attribute.String("runtime", runtime), + } + + m.buildDuration.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...)) + m.buildTotal.Add(ctx, 1, metric.WithAttributes(attrs...)) +} + +// RegisterQueueCallbacks registers callbacks for queue metrics +func (m *Metrics) RegisterQueueCallbacks(queue *BuildQueue, meter metric.Meter) error { + _, err := meter.RegisterCallback( + func(ctx context.Context, observer metric.Observer) error { + observer.ObserveInt64(m.queueLength, int64(queue.PendingCount())) + observer.ObserveInt64(m.activeBuilds, int64(queue.ActiveCount())) + return nil + }, + m.queueLength, + m.activeBuilds, + ) + return err +} + diff --git a/lib/builds/queue.go b/lib/builds/queue.go new file mode 100644 index 00000000..2fee288b --- /dev/null +++ b/lib/builds/queue.go @@ -0,0 +1,171 @@ +package builds + +import "sync" + +// QueuedBuild represents a build waiting to be executed +type QueuedBuild struct { + BuildID string + Request CreateBuildRequest + StartFn func() +} + +// BuildQueue manages concurrent builds with a configurable limit. +// Following the pattern from lib/images/queue.go. +// +// Design notes (see plan for full context): +// - Queue state is in-memory (lost on restart) +// - Build metadata is persisted to disk +// - On startup, pending builds are recovered via listPendingBuilds() +// +// Future migration path if needed: +// - Add BuildQueue interface with Enqueue/Dequeue/Ack/Nack +// - Implement adapters: memoryQueue, redisQueue, natsQueue +// - Use BUILD_QUEUE_BACKEND env var to select implementation +type BuildQueue struct { + maxConcurrent int + active map[string]bool + pending []QueuedBuild + mu sync.Mutex +} + +// NewBuildQueue creates a new build queue with the given concurrency limit +func NewBuildQueue(maxConcurrent int) *BuildQueue { + if maxConcurrent < 1 { + maxConcurrent = 1 + } + return &BuildQueue{ + maxConcurrent: maxConcurrent, + active: make(map[string]bool), + pending: make([]QueuedBuild, 0), + } +} + +// Enqueue adds a build to the queue. Returns queue position (0 if started immediately, >0 if queued). +// If the build is already building or queued, returns its current position without re-enqueueing. +func (q *BuildQueue) Enqueue(buildID string, req CreateBuildRequest, startFn func()) int { + q.mu.Lock() + defer q.mu.Unlock() + + // Check if already building (position 0, actively running) + if q.active[buildID] { + return 0 + } + + // Check if already in pending queue + for i, build := range q.pending { + if build.BuildID == buildID { + return i + 1 // Return existing queue position + } + } + + // Wrap the function to auto-complete + wrappedFn := func() { + defer q.MarkComplete(buildID) + startFn() + } + + build := QueuedBuild{ + BuildID: buildID, + Request: req, + StartFn: wrappedFn, + } + + // Start immediately if under concurrency limit + if len(q.active) < q.maxConcurrent { + q.active[buildID] = true + go wrappedFn() + return 0 + } + + // Otherwise queue it + q.pending = append(q.pending, build) + return len(q.pending) +} + +// MarkComplete marks a build as complete and starts the next pending build if any +func (q *BuildQueue) MarkComplete(buildID string) { + q.mu.Lock() + defer q.mu.Unlock() + + delete(q.active, buildID) + + // Start next pending build if we have capacity + if len(q.pending) > 0 && len(q.active) < q.maxConcurrent { + next := q.pending[0] + q.pending = q.pending[1:] + q.active[next.BuildID] = true + go next.StartFn() + } +} + +// GetPosition returns the queue position for a build. +// Returns nil if the build is actively running or not in queue. +func (q *BuildQueue) GetPosition(buildID string) *int { + q.mu.Lock() + defer q.mu.Unlock() + + if q.active[buildID] { + return nil // Actively running, not queued + } + + for i, build := range q.pending { + if build.BuildID == buildID { + pos := i + 1 + return &pos + } + } + + return nil // Not in queue +} + +// Cancel removes a build from the pending queue. +// Returns true if the build was cancelled, false if it was not in the queue +// (already running or not found). +func (q *BuildQueue) Cancel(buildID string) bool { + q.mu.Lock() + defer q.mu.Unlock() + + // Can't cancel if actively running + if q.active[buildID] { + return false + } + + // Find and remove from pending + for i, build := range q.pending { + if build.BuildID == buildID { + q.pending = append(q.pending[:i], q.pending[i+1:]...) + return true + } + } + + return false +} + +// IsActive returns true if the build is actively running +func (q *BuildQueue) IsActive(buildID string) bool { + q.mu.Lock() + defer q.mu.Unlock() + return q.active[buildID] +} + +// ActiveCount returns the number of actively building builds +func (q *BuildQueue) ActiveCount() int { + q.mu.Lock() + defer q.mu.Unlock() + return len(q.active) +} + +// PendingCount returns the number of queued builds +func (q *BuildQueue) PendingCount() int { + q.mu.Lock() + defer q.mu.Unlock() + return len(q.pending) +} + +// QueueLength returns the total number of builds (active + pending) +func (q *BuildQueue) QueueLength() int { + q.mu.Lock() + defer q.mu.Unlock() + return len(q.active) + len(q.pending) +} + diff --git a/lib/builds/queue_test.go b/lib/builds/queue_test.go new file mode 100644 index 00000000..d5f3fd6e --- /dev/null +++ b/lib/builds/queue_test.go @@ -0,0 +1,230 @@ +package builds + +import ( + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBuildQueue_EnqueueStartsImmediately(t *testing.T) { + queue := NewBuildQueue(2) + + started := make(chan string, 2) + done := make(chan struct{}) + + // Enqueue first build - should start immediately + pos := queue.Enqueue("build-1", CreateBuildRequest{Runtime: "nodejs20"}, func() { + started <- "build-1" + <-done // Wait for signal + }) + + assert.Equal(t, 0, pos, "first build should start immediately (position 0)") + + // Wait for it to start + select { + case id := <-started: + assert.Equal(t, "build-1", id) + case <-time.After(time.Second): + t.Fatal("build-1 did not start") + } + + close(done) +} + +func TestBuildQueue_QueueWhenAtCapacity(t *testing.T) { + queue := NewBuildQueue(1) // Max 1 concurrent + + var wg sync.WaitGroup + done := make(chan struct{}) + + // Start first build + wg.Add(1) + pos1 := queue.Enqueue("build-1", CreateBuildRequest{}, func() { + wg.Done() + <-done // Block + }) + assert.Equal(t, 0, pos1) + + // Wait for first to actually start + wg.Wait() + + // Second build should be queued + pos2 := queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + assert.Equal(t, 1, pos2, "second build should be queued at position 1") + + // Third build should be queued at position 2 + pos3 := queue.Enqueue("build-3", CreateBuildRequest{}, func() {}) + assert.Equal(t, 2, pos3, "third build should be queued at position 2") + + close(done) +} + +func TestBuildQueue_DeduplicationActive(t *testing.T) { + queue := NewBuildQueue(2) + done := make(chan struct{}) + + // Start a build + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + <-done + }) + + // Wait for it to become active + time.Sleep(10 * time.Millisecond) + + // Try to enqueue the same build again - should return position 0 (active) + pos := queue.Enqueue("build-1", CreateBuildRequest{}, func() {}) + assert.Equal(t, 0, pos, "re-enqueueing active build should return position 0") + + close(done) +} + +func TestBuildQueue_DeduplicationPending(t *testing.T) { + queue := NewBuildQueue(1) + done := make(chan struct{}) + + // Fill the queue + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + <-done + }) + + // Add a second build to pending + pos1 := queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + assert.Equal(t, 1, pos1) + + // Try to enqueue build-2 again - should return same position + pos2 := queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + assert.Equal(t, 1, pos2, "re-enqueueing pending build should return same position") + + close(done) +} + +func TestBuildQueue_Cancel(t *testing.T) { + queue := NewBuildQueue(1) + done := make(chan struct{}) + + // Fill the queue + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + <-done + }) + + // Add to pending + queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + queue.Enqueue("build-3", CreateBuildRequest{}, func() {}) + + // Cancel build-2 + cancelled := queue.Cancel("build-2") + require.True(t, cancelled, "should be able to cancel pending build") + + // Verify build-3 moved up + pos := queue.GetPosition("build-3") + require.NotNil(t, pos) + assert.Equal(t, 1, *pos, "build-3 should move to position 1") + + // Can't cancel active build + cancelled = queue.Cancel("build-1") + assert.False(t, cancelled, "should not be able to cancel active build") + + close(done) +} + +func TestBuildQueue_GetPosition(t *testing.T) { + queue := NewBuildQueue(1) + done := make(chan struct{}) + + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + <-done + }) + queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + queue.Enqueue("build-3", CreateBuildRequest{}, func() {}) + + // Active build has no position (returns nil) + pos1 := queue.GetPosition("build-1") + assert.Nil(t, pos1, "active build should have no position") + + // Pending builds have positions + pos2 := queue.GetPosition("build-2") + require.NotNil(t, pos2) + assert.Equal(t, 1, *pos2) + + pos3 := queue.GetPosition("build-3") + require.NotNil(t, pos3) + assert.Equal(t, 2, *pos3) + + // Non-existent build has no position + pos4 := queue.GetPosition("build-4") + assert.Nil(t, pos4) + + close(done) +} + +func TestBuildQueue_AutoStartNextOnComplete(t *testing.T) { + queue := NewBuildQueue(1) + + started := make(chan string, 3) + var mu sync.Mutex + completionOrder := []string{} + + // Add builds + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + started <- "build-1" + time.Sleep(10 * time.Millisecond) + mu.Lock() + completionOrder = append(completionOrder, "build-1") + mu.Unlock() + }) + queue.Enqueue("build-2", CreateBuildRequest{}, func() { + started <- "build-2" + time.Sleep(10 * time.Millisecond) + mu.Lock() + completionOrder = append(completionOrder, "build-2") + mu.Unlock() + }) + + // Wait for both to complete + for i := 0; i < 2; i++ { + select { + case <-started: + case <-time.After(2 * time.Second): + t.Fatal("builds did not complete in time") + } + } + + // Give time for completion + time.Sleep(50 * time.Millisecond) + + mu.Lock() + defer mu.Unlock() + assert.Equal(t, []string{"build-1", "build-2"}, completionOrder) +} + +func TestBuildQueue_Counts(t *testing.T) { + queue := NewBuildQueue(2) + + assert.Equal(t, 0, queue.ActiveCount()) + assert.Equal(t, 0, queue.PendingCount()) + assert.Equal(t, 0, queue.QueueLength()) + + done := make(chan struct{}) + queue.Enqueue("build-1", CreateBuildRequest{}, func() { <-done }) + queue.Enqueue("build-2", CreateBuildRequest{}, func() { <-done }) + + // Wait for them to start + time.Sleep(10 * time.Millisecond) + + assert.Equal(t, 2, queue.ActiveCount()) + assert.Equal(t, 0, queue.PendingCount()) + assert.Equal(t, 2, queue.QueueLength()) + + // Add a pending one + queue.Enqueue("build-3", CreateBuildRequest{}, func() {}) + + assert.Equal(t, 2, queue.ActiveCount()) + assert.Equal(t, 1, queue.PendingCount()) + assert.Equal(t, 3, queue.QueueLength()) + + close(done) +} + diff --git a/lib/builds/storage.go b/lib/builds/storage.go new file mode 100644 index 00000000..2643b20e --- /dev/null +++ b/lib/builds/storage.go @@ -0,0 +1,227 @@ +package builds + +import ( + "encoding/json" + "fmt" + "os" + "sort" + "time" + + "github.com/onkernel/hypeman/lib/paths" +) + +// buildMetadata is the internal representation stored on disk +type buildMetadata struct { + ID string `json:"id"` + Status string `json:"status"` + Runtime string `json:"runtime"` + Request *CreateBuildRequest `json:"request,omitempty"` + ImageDigest *string `json:"image_digest,omitempty"` + ImageRef *string `json:"image_ref,omitempty"` + Error *string `json:"error,omitempty"` + Provenance *BuildProvenance `json:"provenance,omitempty"` + CreatedAt time.Time `json:"created_at"` + StartedAt *time.Time `json:"started_at,omitempty"` + CompletedAt *time.Time `json:"completed_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` + BuilderInstance *string `json:"builder_instance,omitempty"` // Instance ID of builder VM +} + +// toBuild converts internal metadata to the public Build type +func (m *buildMetadata) toBuild() *Build { + return &Build{ + ID: m.ID, + Status: m.Status, + Runtime: m.Runtime, + ImageDigest: m.ImageDigest, + ImageRef: m.ImageRef, + Error: m.Error, + Provenance: m.Provenance, + CreatedAt: m.CreatedAt, + StartedAt: m.StartedAt, + CompletedAt: m.CompletedAt, + DurationMS: m.DurationMS, + } +} + +// writeMetadata writes build metadata to disk atomically +func writeMetadata(p *paths.Paths, meta *buildMetadata) error { + dir := p.BuildDir(meta.ID) + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("create build directory: %w", err) + } + + data, err := json.MarshalIndent(meta, "", " ") + if err != nil { + return fmt.Errorf("marshal metadata: %w", err) + } + + // Write atomically via temp file + tempPath := p.BuildMetadata(meta.ID) + ".tmp" + if err := os.WriteFile(tempPath, data, 0644); err != nil { + return fmt.Errorf("write temp metadata: %w", err) + } + + finalPath := p.BuildMetadata(meta.ID) + if err := os.Rename(tempPath, finalPath); err != nil { + os.Remove(tempPath) + return fmt.Errorf("rename metadata: %w", err) + } + + return nil +} + +// readMetadata reads build metadata from disk +func readMetadata(p *paths.Paths, id string) (*buildMetadata, error) { + path := p.BuildMetadata(id) + data, err := os.ReadFile(path) + if err != nil { + if os.IsNotExist(err) { + return nil, ErrNotFound + } + return nil, fmt.Errorf("read metadata: %w", err) + } + + var meta buildMetadata + if err := json.Unmarshal(data, &meta); err != nil { + return nil, fmt.Errorf("unmarshal metadata: %w", err) + } + + return &meta, nil +} + +// listAllBuilds returns all builds sorted by creation time (newest first) +func listAllBuilds(p *paths.Paths) ([]*buildMetadata, error) { + buildsDir := p.BuildsDir() + + entries, err := os.ReadDir(buildsDir) + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, fmt.Errorf("read builds directory: %w", err) + } + + var metas []*buildMetadata + for _, entry := range entries { + if !entry.IsDir() { + continue + } + + meta, err := readMetadata(p, entry.Name()) + if err != nil { + continue // Skip invalid entries + } + metas = append(metas, meta) + } + + // Sort by created_at descending (newest first) + sort.Slice(metas, func(i, j int) bool { + return metas[i].CreatedAt.After(metas[j].CreatedAt) + }) + + return metas, nil +} + +// listPendingBuilds returns builds that need to be recovered on startup +// Returns builds with status queued/building, sorted by created_at (oldest first for FIFO) +func listPendingBuilds(p *paths.Paths) ([]*buildMetadata, error) { + all, err := listAllBuilds(p) + if err != nil { + return nil, err + } + + var pending []*buildMetadata + for _, meta := range all { + switch meta.Status { + case StatusQueued, StatusBuilding, StatusPushing: + pending = append(pending, meta) + } + } + + // Sort by created_at ascending (oldest first for FIFO recovery) + sort.Slice(pending, func(i, j int) bool { + return pending[i].CreatedAt.Before(pending[j].CreatedAt) + }) + + return pending, nil +} + +// deleteBuild removes a build's data from disk +func deleteBuild(p *paths.Paths, id string) error { + dir := p.BuildDir(id) + + // Check if exists + if _, err := os.Stat(dir); err != nil { + if os.IsNotExist(err) { + return ErrNotFound + } + return fmt.Errorf("stat build directory: %w", err) + } + + if err := os.RemoveAll(dir); err != nil { + return fmt.Errorf("remove build directory: %w", err) + } + + return nil +} + +// ensureLogsDir ensures the logs directory exists for a build +func ensureLogsDir(p *paths.Paths, id string) error { + logsDir := p.BuildLogs(id) + return os.MkdirAll(logsDir, 0755) +} + +// appendLog appends log data to the build log file +func appendLog(p *paths.Paths, id string, data []byte) error { + if err := ensureLogsDir(p, id); err != nil { + return err + } + + logPath := p.BuildLog(id) + f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("open log file: %w", err) + } + defer f.Close() + + if _, err := f.Write(data); err != nil { + return fmt.Errorf("write log: %w", err) + } + + return nil +} + +// readLog reads the build log file +func readLog(p *paths.Paths, id string) ([]byte, error) { + logPath := p.BuildLog(id) + data, err := os.ReadFile(logPath) + if err != nil { + if os.IsNotExist(err) { + return nil, nil // No logs yet + } + return nil, fmt.Errorf("read log: %w", err) + } + return data, nil +} + +// writeBuildConfig writes the build config for the builder VM +func writeBuildConfig(p *paths.Paths, id string, config *BuildConfig) error { + dir := p.BuildDir(id) + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("create build directory: %w", err) + } + + data, err := json.MarshalIndent(config, "", " ") + if err != nil { + return fmt.Errorf("marshal build config: %w", err) + } + + configPath := p.BuildConfig(id) + if err := os.WriteFile(configPath, data, 0644); err != nil { + return fmt.Errorf("write build config: %w", err) + } + + return nil +} + diff --git a/lib/builds/templates/templates.go b/lib/builds/templates/templates.go new file mode 100644 index 00000000..92611d85 --- /dev/null +++ b/lib/builds/templates/templates.go @@ -0,0 +1,230 @@ +// Package templates provides Dockerfile generation for different runtimes. +package templates + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// Generator generates Dockerfiles for a specific runtime +type Generator interface { + // Generate creates a Dockerfile for the given source directory + Generate(sourceDir string, baseImageDigest string) (string, error) + + // DetectLockfile returns the detected lockfile type and path + DetectLockfile(sourceDir string) (string, string) +} + +// GetGenerator returns a Generator for the given runtime +func GetGenerator(runtime string) (Generator, error) { + switch runtime { + case "nodejs20": + return &NodeJSGenerator{Version: "20"}, nil + case "python312": + return &PythonGenerator{Version: "3.12"}, nil + default: + return nil, fmt.Errorf("unsupported runtime: %s", runtime) + } +} + +// NodeJSGenerator generates Dockerfiles for Node.js applications +type NodeJSGenerator struct { + Version string +} + +// DetectLockfile detects which package manager lockfile is present +func (g *NodeJSGenerator) DetectLockfile(sourceDir string) (string, string) { + lockfiles := []struct { + name string + manager string + }{ + {"pnpm-lock.yaml", "pnpm"}, + {"yarn.lock", "yarn"}, + {"package-lock.json", "npm"}, + } + + for _, lf := range lockfiles { + path := filepath.Join(sourceDir, lf.name) + if _, err := os.Stat(path); err == nil { + return lf.manager, lf.name + } + } + + return "npm", "package-lock.json" +} + +// Generate creates a Dockerfile for a Node.js application +func (g *NodeJSGenerator) Generate(sourceDir string, baseImageDigest string) (string, error) { + manager, lockfile := g.DetectLockfile(sourceDir) + + // Determine base image + baseImage := baseImageDigest + if baseImage == "" { + baseImage = fmt.Sprintf("node:%s-alpine", g.Version) + } + + // Determine install command based on package manager + var installCmd string + switch manager { + case "pnpm": + installCmd = "corepack enable && pnpm install --frozen-lockfile" + case "yarn": + installCmd = "yarn install --frozen-lockfile" + default: + installCmd = "npm ci" + } + + // Check if package.json exists + if _, err := os.Stat(filepath.Join(sourceDir, "package.json")); err != nil { + return "", fmt.Errorf("package.json not found in source directory") + } + + // Detect entry point + entryPoint := detectNodeEntryPoint(sourceDir) + + dockerfile := fmt.Sprintf(`FROM %s + +WORKDIR /app + +# Copy dependency files first (cache layer) +COPY package.json %s ./ + +# Install dependencies (strict mode from lockfile) +RUN %s + +# Copy application source +COPY . . + +# Default command +CMD ["node", "%s"] +`, baseImage, lockfile, installCmd, entryPoint) + + return dockerfile, nil +} + +// detectNodeEntryPoint tries to detect the entry point for a Node.js app +func detectNodeEntryPoint(sourceDir string) string { + // Check common entry points + candidates := []string{"index.js", "src/index.js", "main.js", "app.js", "server.js"} + for _, candidate := range candidates { + if _, err := os.Stat(filepath.Join(sourceDir, candidate)); err == nil { + return candidate + } + } + return "index.js" +} + +// PythonGenerator generates Dockerfiles for Python applications +type PythonGenerator struct { + Version string +} + +// DetectLockfile detects which Python dependency file is present +func (g *PythonGenerator) DetectLockfile(sourceDir string) (string, string) { + lockfiles := []struct { + name string + manager string + }{ + {"poetry.lock", "poetry"}, + {"Pipfile.lock", "pipenv"}, + {"requirements.txt", "pip"}, + } + + for _, lf := range lockfiles { + path := filepath.Join(sourceDir, lf.name) + if _, err := os.Stat(path); err == nil { + return lf.manager, lf.name + } + } + + return "pip", "requirements.txt" +} + +// Generate creates a Dockerfile for a Python application +func (g *PythonGenerator) Generate(sourceDir string, baseImageDigest string) (string, error) { + manager, lockfile := g.DetectLockfile(sourceDir) + + // Determine base image + baseImage := baseImageDigest + if baseImage == "" { + baseImage = fmt.Sprintf("python:%s-slim", g.Version) + } + + var installCmd string + var copyFiles string + + switch manager { + case "poetry": + // Poetry requires pyproject.toml and poetry.lock + copyFiles = "pyproject.toml poetry.lock" + installCmd = `pip install poetry && \ + poetry config virtualenvs.create false && \ + poetry install --no-dev --no-interaction --no-ansi` + case "pipenv": + copyFiles = "Pipfile Pipfile.lock" + installCmd = `pip install pipenv && \ + pipenv install --system --deploy --ignore-pipfile` + default: + // Check if requirements.txt has hashes for strict mode + hasHashes := checkRequirementsHasHashes(sourceDir) + copyFiles = "requirements.txt" + if hasHashes { + // Strict mode: require hashes, prefer binary packages + installCmd = "pip install --require-hashes --only-binary :all: -r requirements.txt" + } else { + installCmd = "pip install --no-cache-dir -r requirements.txt" + } + } + + // Check if lockfile exists + if _, err := os.Stat(filepath.Join(sourceDir, lockfile)); err != nil { + return "", fmt.Errorf("%s not found in source directory", lockfile) + } + + // Detect entry point + entryPoint := detectPythonEntryPoint(sourceDir) + + dockerfile := fmt.Sprintf(`FROM %s + +WORKDIR /app + +# Copy dependency files first (cache layer) +COPY %s ./ + +# Install dependencies +RUN %s + +# Copy application source +COPY . . + +# Default command +CMD ["python", "%s"] +`, baseImage, copyFiles, installCmd, entryPoint) + + return dockerfile, nil +} + +// checkRequirementsHasHashes checks if requirements.txt contains hash pins +func checkRequirementsHasHashes(sourceDir string) bool { + reqPath := filepath.Join(sourceDir, "requirements.txt") + data, err := os.ReadFile(reqPath) + if err != nil { + return false + } + return strings.Contains(string(data), "--hash=") +} + +// detectPythonEntryPoint tries to detect the entry point for a Python app +func detectPythonEntryPoint(sourceDir string) string { + // Check common entry points + candidates := []string{"main.py", "app.py", "run.py", "server.py", "src/main.py"} + for _, candidate := range candidates { + if _, err := os.Stat(filepath.Join(sourceDir, candidate)); err == nil { + return candidate + } + } + return "main.py" +} + diff --git a/lib/builds/templates/templates_test.go b/lib/builds/templates/templates_test.go new file mode 100644 index 00000000..55472ec4 --- /dev/null +++ b/lib/builds/templates/templates_test.go @@ -0,0 +1,180 @@ +package templates + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetGenerator(t *testing.T) { + tests := []struct { + runtime string + wantErr bool + }{ + {"nodejs20", false}, + {"python312", false}, + {"ruby", true}, + {"java", true}, + {"", true}, + } + + for _, tt := range tests { + t.Run(tt.runtime, func(t *testing.T) { + gen, err := GetGenerator(tt.runtime) + if tt.wantErr { + assert.Error(t, err) + assert.Nil(t, gen) + } else { + assert.NoError(t, err) + assert.NotNil(t, gen) + } + }) + } +} + +func TestNodeJSGenerator_DetectLockfile(t *testing.T) { + // Create temp directory + tmpDir := t.TempDir() + + gen := &NodeJSGenerator{Version: "20"} + + // Default to npm when no lockfile + manager, lockfile := gen.DetectLockfile(tmpDir) + assert.Equal(t, "npm", manager) + assert.Equal(t, "package-lock.json", lockfile) + + // Detect pnpm + os.WriteFile(filepath.Join(tmpDir, "pnpm-lock.yaml"), []byte{}, 0644) + manager, lockfile = gen.DetectLockfile(tmpDir) + assert.Equal(t, "pnpm", manager) + assert.Equal(t, "pnpm-lock.yaml", lockfile) + + // Remove pnpm, add yarn + os.Remove(filepath.Join(tmpDir, "pnpm-lock.yaml")) + os.WriteFile(filepath.Join(tmpDir, "yarn.lock"), []byte{}, 0644) + manager, lockfile = gen.DetectLockfile(tmpDir) + assert.Equal(t, "yarn", manager) + assert.Equal(t, "yarn.lock", lockfile) +} + +func TestNodeJSGenerator_Generate(t *testing.T) { + tmpDir := t.TempDir() + + // Create package.json + err := os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte(`{"name": "test"}`), 0644) + require.NoError(t, err) + + // Create package-lock.json + err = os.WriteFile(filepath.Join(tmpDir, "package-lock.json"), []byte(`{}`), 0644) + require.NoError(t, err) + + // Create index.js + err = os.WriteFile(filepath.Join(tmpDir, "index.js"), []byte(`console.log("hello")`), 0644) + require.NoError(t, err) + + gen := &NodeJSGenerator{Version: "20"} + dockerfile, err := gen.Generate(tmpDir, "") + require.NoError(t, err) + + // Check Dockerfile contents + assert.Contains(t, dockerfile, "FROM node:20-alpine") + assert.Contains(t, dockerfile, "npm ci") + assert.Contains(t, dockerfile, "COPY package.json package-lock.json") + assert.Contains(t, dockerfile, "CMD [\"node\", \"index.js\"]") +} + +func TestNodeJSGenerator_GenerateWithCustomBase(t *testing.T) { + tmpDir := t.TempDir() + + os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte(`{}`), 0644) + os.WriteFile(filepath.Join(tmpDir, "package-lock.json"), []byte(`{}`), 0644) + + gen := &NodeJSGenerator{Version: "20"} + dockerfile, err := gen.Generate(tmpDir, "node@sha256:abc123") + require.NoError(t, err) + + assert.Contains(t, dockerfile, "FROM node@sha256:abc123") +} + +func TestNodeJSGenerator_MissingPackageJson(t *testing.T) { + tmpDir := t.TempDir() + + gen := &NodeJSGenerator{Version: "20"} + _, err := gen.Generate(tmpDir, "") + assert.Error(t, err) + assert.Contains(t, err.Error(), "package.json not found") +} + +func TestPythonGenerator_DetectLockfile(t *testing.T) { + tmpDir := t.TempDir() + + gen := &PythonGenerator{Version: "3.12"} + + // Default to pip when no lockfile + manager, lockfile := gen.DetectLockfile(tmpDir) + assert.Equal(t, "pip", manager) + assert.Equal(t, "requirements.txt", lockfile) + + // Detect poetry + os.WriteFile(filepath.Join(tmpDir, "poetry.lock"), []byte{}, 0644) + manager, lockfile = gen.DetectLockfile(tmpDir) + assert.Equal(t, "poetry", manager) + assert.Equal(t, "poetry.lock", lockfile) + + // Remove poetry, add pipenv + os.Remove(filepath.Join(tmpDir, "poetry.lock")) + os.WriteFile(filepath.Join(tmpDir, "Pipfile.lock"), []byte{}, 0644) + manager, lockfile = gen.DetectLockfile(tmpDir) + assert.Equal(t, "pipenv", manager) + assert.Equal(t, "Pipfile.lock", lockfile) +} + +func TestPythonGenerator_Generate(t *testing.T) { + tmpDir := t.TempDir() + + // Create requirements.txt + err := os.WriteFile(filepath.Join(tmpDir, "requirements.txt"), []byte("flask==2.0.0"), 0644) + require.NoError(t, err) + + // Create main.py + err = os.WriteFile(filepath.Join(tmpDir, "main.py"), []byte(`print("hello")`), 0644) + require.NoError(t, err) + + gen := &PythonGenerator{Version: "3.12"} + dockerfile, err := gen.Generate(tmpDir, "") + require.NoError(t, err) + + assert.Contains(t, dockerfile, "FROM python:3.12-slim") + assert.Contains(t, dockerfile, "pip install --no-cache-dir -r requirements.txt") + assert.Contains(t, dockerfile, "COPY requirements.txt") + assert.Contains(t, dockerfile, "CMD [\"python\", \"main.py\"]") +} + +func TestPythonGenerator_GenerateWithHashes(t *testing.T) { + tmpDir := t.TempDir() + + // Create requirements.txt with hashes + err := os.WriteFile(filepath.Join(tmpDir, "requirements.txt"), []byte(`flask==2.0.0 --hash=sha256:abc123`), 0644) + require.NoError(t, err) + + gen := &PythonGenerator{Version: "3.12"} + dockerfile, err := gen.Generate(tmpDir, "") + require.NoError(t, err) + + // Should use strict mode with hashes + assert.Contains(t, dockerfile, "--require-hashes") + assert.Contains(t, dockerfile, "--only-binary") +} + +func TestPythonGenerator_MissingRequirements(t *testing.T) { + tmpDir := t.TempDir() + + gen := &PythonGenerator{Version: "3.12"} + _, err := gen.Generate(tmpDir, "") + assert.Error(t, err) + assert.Contains(t, err.Error(), "requirements.txt not found") +} + diff --git a/lib/builds/types.go b/lib/builds/types.go new file mode 100644 index 00000000..2fc47831 --- /dev/null +++ b/lib/builds/types.go @@ -0,0 +1,200 @@ +// Package builds implements a secure build system that runs rootless BuildKit +// inside ephemeral Cloud Hypervisor microVMs for multi-tenant isolation. +package builds + +import "time" + +// Build status constants +const ( + StatusQueued = "queued" + StatusBuilding = "building" + StatusPushing = "pushing" + StatusReady = "ready" + StatusFailed = "failed" + StatusCancelled = "cancelled" +) + +// Runtime constants for supported build runtimes +const ( + RuntimeNodeJS20 = "nodejs20" + RuntimePython312 = "python312" +) + +// Build represents a source-to-image build job +type Build struct { + ID string `json:"id"` + Status string `json:"status"` + Runtime string `json:"runtime"` + QueuePosition *int `json:"queue_position,omitempty"` + ImageDigest *string `json:"image_digest,omitempty"` + ImageRef *string `json:"image_ref,omitempty"` + Error *string `json:"error,omitempty"` + Provenance *BuildProvenance `json:"provenance,omitempty"` + CreatedAt time.Time `json:"created_at"` + StartedAt *time.Time `json:"started_at,omitempty"` + CompletedAt *time.Time `json:"completed_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` +} + +// CreateBuildRequest represents a request to create a new build +type CreateBuildRequest struct { + // Runtime specifies the build runtime (e.g., nodejs20, python312) + Runtime string `json:"runtime"` + + // BaseImageDigest optionally pins the base image by digest for reproducibility + BaseImageDigest string `json:"base_image_digest,omitempty"` + + // SourceHash is the SHA256 hash of the source tarball for verification + SourceHash string `json:"source_hash,omitempty"` + + // BuildPolicy contains resource limits and network policy for the build + BuildPolicy *BuildPolicy `json:"build_policy,omitempty"` + + // CacheScope is the tenant-specific cache key prefix for isolation + CacheScope string `json:"cache_scope,omitempty"` + + // Dockerfile is an optional custom Dockerfile (if not provided, one is generated) + Dockerfile string `json:"dockerfile,omitempty"` + + // BuildArgs are ARG values to pass to the Dockerfile + BuildArgs map[string]string `json:"build_args,omitempty"` + + // Secrets are secret references to inject during build + Secrets []SecretRef `json:"secrets,omitempty"` +} + +// BuildPolicy defines resource limits and network policy for a build +type BuildPolicy struct { + // TimeoutSeconds is the maximum build duration (default: 600) + TimeoutSeconds int `json:"timeout_seconds,omitempty"` + + // MemoryMB is the memory limit for the builder VM (default: 2048) + MemoryMB int `json:"memory_mb,omitempty"` + + // CPUs is the number of vCPUs for the builder VM (default: 2) + CPUs int `json:"cpus,omitempty"` + + // NetworkMode controls network access during build + // "isolated" = no network, "egress" = outbound allowed + NetworkMode string `json:"network_mode,omitempty"` + + // AllowedDomains restricts egress to specific domains (only when NetworkMode="egress") + AllowedDomains []string `json:"allowed_domains,omitempty"` +} + +// SecretRef references a secret to inject during build +type SecretRef struct { + // ID is the secret identifier (used in --mount=type=secret,id=...) + ID string `json:"id"` + + // EnvVar is the environment variable name to expose the secret as + EnvVar string `json:"env_var,omitempty"` +} + +// BuildProvenance records the inputs and toolchain used for a build +// This enables reproducibility verification and audit trails +type BuildProvenance struct { + // BaseImageDigest is the pinned base image used + BaseImageDigest string `json:"base_image_digest"` + + // SourceHash is the SHA256 of the source tarball + SourceHash string `json:"source_hash"` + + // LockfileHashes maps lockfile names to their SHA256 hashes + LockfileHashes map[string]string `json:"lockfile_hashes,omitempty"` + + // ToolchainVersion is the runtime version (e.g., "node v20.10.0") + ToolchainVersion string `json:"toolchain_version,omitempty"` + + // BuildkitVersion is the BuildKit version used + BuildkitVersion string `json:"buildkit_version,omitempty"` + + // Timestamp is when the build completed + Timestamp time.Time `json:"timestamp"` +} + +// BuildConfig is the configuration passed to the builder VM via config disk +// This is read by the builder agent inside the guest +type BuildConfig struct { + // JobID is the build job identifier + JobID string `json:"job_id"` + + // Runtime is the build runtime (nodejs20, python312) + Runtime string `json:"runtime"` + + // BaseImageDigest optionally pins the base image + BaseImageDigest string `json:"base_image_digest,omitempty"` + + // RegistryURL is where to push the built image + RegistryURL string `json:"registry_url"` + + // CacheScope is the tenant-specific cache key prefix + CacheScope string `json:"cache_scope,omitempty"` + + // SourcePath is the path to source in the guest (typically /src) + SourcePath string `json:"source_path"` + + // Dockerfile is an optional custom Dockerfile content + Dockerfile string `json:"dockerfile,omitempty"` + + // BuildArgs are ARG values for the Dockerfile + BuildArgs map[string]string `json:"build_args,omitempty"` + + // Secrets are secret references to fetch from host + Secrets []SecretRef `json:"secrets,omitempty"` + + // TimeoutSeconds is the build timeout + TimeoutSeconds int `json:"timeout_seconds"` + + // NetworkMode is "isolated" or "egress" + NetworkMode string `json:"network_mode"` +} + +// BuildResult is returned by the builder agent after a build completes +type BuildResult struct { + // Success indicates whether the build succeeded + Success bool `json:"success"` + + // ImageDigest is the digest of the pushed image (only on success) + ImageDigest string `json:"image_digest,omitempty"` + + // Error is the error message (only on failure) + Error string `json:"error,omitempty"` + + // Logs is the full build log output + Logs string `json:"logs,omitempty"` + + // Provenance records build inputs for reproducibility + Provenance BuildProvenance `json:"provenance"` + + // DurationMS is the build duration in milliseconds + DurationMS int64 `json:"duration_ms"` +} + +// DefaultBuildPolicy returns the default build policy +func DefaultBuildPolicy() BuildPolicy { + return BuildPolicy{ + TimeoutSeconds: 600, // 10 minutes + MemoryMB: 2048, // 2GB + CPUs: 2, + NetworkMode: "egress", // Allow outbound for dependency downloads + } +} + +// ApplyDefaults fills in default values for a build policy +func (p *BuildPolicy) ApplyDefaults() { + defaults := DefaultBuildPolicy() + if p.TimeoutSeconds == 0 { + p.TimeoutSeconds = defaults.TimeoutSeconds + } + if p.MemoryMB == 0 { + p.MemoryMB = defaults.MemoryMB + } + if p.CPUs == 0 { + p.CPUs = defaults.CPUs + } + if p.NetworkMode == "" { + p.NetworkMode = defaults.NetworkMode + } +} + diff --git a/lib/builds/vsock_handler.go b/lib/builds/vsock_handler.go new file mode 100644 index 00000000..6c31dc4d --- /dev/null +++ b/lib/builds/vsock_handler.go @@ -0,0 +1,249 @@ +package builds + +import ( + "context" + "encoding/json" + "fmt" + "io" + "log/slog" + "net" + "sync" + + "github.com/mdlayher/vsock" +) + +const ( + // BuildAgentVsockPort is the port the builder agent listens on + BuildAgentVsockPort = 5001 +) + +// VsockMessage is the envelope for vsock communication with builder agents +type VsockMessage struct { + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` +} + +// SecretsRequest is sent by the builder agent to fetch secrets +type SecretsRequest struct { + SecretIDs []string `json:"secret_ids"` +} + +// SecretsResponse contains the requested secrets +type SecretsResponse struct { + Secrets map[string]string `json:"secrets"` +} + +// SecretProvider provides secrets for builds +type SecretProvider interface { + // GetSecrets returns the values for the given secret IDs + GetSecrets(ctx context.Context, secretIDs []string) (map[string]string, error) +} + +// NoOpSecretProvider returns empty secrets (for builds without secrets) +type NoOpSecretProvider struct{} + +func (p *NoOpSecretProvider) GetSecrets(ctx context.Context, secretIDs []string) (map[string]string, error) { + return make(map[string]string), nil +} + +// BuildResultHandler is called when a build completes +type BuildResultHandler func(result *BuildResult) + +// BuildLogHandler is called for each log line from the builder +type BuildLogHandler func(line string) + +// VsockHandler handles vsock communication with builder agents +type VsockHandler struct { + secretProvider SecretProvider + resultHandlers map[string]BuildResultHandler + logHandlers map[string]BuildLogHandler + mu sync.RWMutex + logger *slog.Logger +} + +// NewVsockHandler creates a new vsock handler +func NewVsockHandler(secretProvider SecretProvider, logger *slog.Logger) *VsockHandler { + if secretProvider == nil { + secretProvider = &NoOpSecretProvider{} + } + if logger == nil { + logger = slog.Default() + } + return &VsockHandler{ + secretProvider: secretProvider, + resultHandlers: make(map[string]BuildResultHandler), + logHandlers: make(map[string]BuildLogHandler), + logger: logger, + } +} + +// RegisterHandlers registers handlers for a specific build +func (h *VsockHandler) RegisterHandlers(buildID string, resultHandler BuildResultHandler, logHandler BuildLogHandler) { + h.mu.Lock() + defer h.mu.Unlock() + if resultHandler != nil { + h.resultHandlers[buildID] = resultHandler + } + if logHandler != nil { + h.logHandlers[buildID] = logHandler + } +} + +// UnregisterHandlers removes handlers for a build +func (h *VsockHandler) UnregisterHandlers(buildID string) { + h.mu.Lock() + defer h.mu.Unlock() + delete(h.resultHandlers, buildID) + delete(h.logHandlers, buildID) +} + +// ListenAndServe starts listening for vsock connections +// This should be called once and runs until the context is cancelled +func (h *VsockHandler) ListenAndServe(ctx context.Context) error { + l, err := vsock.Listen(BuildAgentVsockPort, nil) + if err != nil { + return fmt.Errorf("listen vsock: %w", err) + } + defer l.Close() + + h.logger.Info("vsock handler listening", "port", BuildAgentVsockPort) + + // Handle context cancellation + go func() { + <-ctx.Done() + l.Close() + }() + + for { + conn, err := l.Accept() + if err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + h.logger.Error("accept vsock connection", "error", err) + continue + } + go h.handleConnection(ctx, conn) + } +} + +// handleConnection handles a single vsock connection +func (h *VsockHandler) handleConnection(ctx context.Context, conn net.Conn) { + defer conn.Close() + + decoder := json.NewDecoder(conn) + encoder := json.NewEncoder(conn) + + for { + var msg VsockMessage + if err := decoder.Decode(&msg); err != nil { + if err == io.EOF { + return + } + h.logger.Error("decode vsock message", "error", err) + return + } + + switch msg.Type { + case "get_secrets": + // Decode the actual request + var req SecretsRequest + // Re-read to get the full message - for simplicity we expect + // the secrets list in a separate field or we can use the same connection + secrets, err := h.secretProvider.GetSecrets(ctx, req.SecretIDs) + if err != nil { + h.logger.Error("get secrets", "error", err) + encoder.Encode(SecretsResponse{Secrets: make(map[string]string)}) + continue + } + encoder.Encode(SecretsResponse{Secrets: secrets}) + + case "build_result": + if msg.Result != nil { + h.handleBuildResult(msg.Result) + } + + case "log": + if msg.Log != "" { + h.handleLog(msg.Log) + } + + default: + h.logger.Warn("unknown vsock message type", "type", msg.Type) + } + } +} + +// handleBuildResult dispatches a build result to the registered handler +func (h *VsockHandler) handleBuildResult(result *BuildResult) { + // For now, we broadcast to all handlers since we don't have build ID in the message + // In a production system, you'd include the build ID in the result + h.mu.RLock() + handlers := make([]BuildResultHandler, 0, len(h.resultHandlers)) + for _, handler := range h.resultHandlers { + handlers = append(handlers, handler) + } + h.mu.RUnlock() + + for _, handler := range handlers { + handler(result) + } +} + +// handleLog dispatches a log line to the registered handler +func (h *VsockHandler) handleLog(line string) { + h.mu.RLock() + handlers := make([]BuildLogHandler, 0, len(h.logHandlers)) + for _, handler := range h.logHandlers { + handlers = append(handlers, handler) + } + h.mu.RUnlock() + + for _, handler := range handlers { + handler(line) + } +} + +// ConnectToBuilder connects to a builder agent via vsock +// This is used to communicate with a specific builder VM +func ConnectToBuilder(cid uint32) (net.Conn, error) { + return vsock.Dial(cid, BuildAgentVsockPort, nil) +} + +// WaitForBuildResult waits for a build result from a specific builder +// It connects to the builder's vsock and reads the result +func WaitForBuildResult(ctx context.Context, cid uint32) (*BuildResult, error) { + conn, err := vsock.Dial(cid, BuildAgentVsockPort, nil) + if err != nil { + return nil, fmt.Errorf("dial builder: %w", err) + } + defer conn.Close() + + // Set read deadline based on context + if deadline, ok := ctx.Deadline(); ok { + conn.SetReadDeadline(deadline) + } + + decoder := json.NewDecoder(conn) + for { + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: + } + + var msg VsockMessage + if err := decoder.Decode(&msg); err != nil { + if err == io.EOF { + continue + } + return nil, fmt.Errorf("decode message: %w", err) + } + + if msg.Type == "build_result" && msg.Result != nil { + return msg.Result, nil + } + } +} + diff --git a/lib/oapi/oapi.go b/lib/oapi/oapi.go index 0401b78c..7ab98508 100644 --- a/lib/oapi/oapi.go +++ b/lib/oapi/oapi.go @@ -29,6 +29,16 @@ const ( BearerAuthScopes = "bearerAuth.Scopes" ) +// Defines values for BuildStatus. +const ( + BuildStatusBuilding BuildStatus = "building" + BuildStatusCancelled BuildStatus = "cancelled" + BuildStatusFailed BuildStatus = "failed" + BuildStatusPushing BuildStatus = "pushing" + BuildStatusQueued BuildStatus = "queued" + BuildStatusReady BuildStatus = "ready" +) + // Defines values for DeviceType. const ( Gpu DeviceType = "gpu" @@ -42,11 +52,11 @@ const ( // Defines values for ImageStatus. const ( - Converting ImageStatus = "converting" - Failed ImageStatus = "failed" - Pending ImageStatus = "pending" - Pulling ImageStatus = "pulling" - Ready ImageStatus = "ready" + ImageStatusConverting ImageStatus = "converting" + ImageStatusFailed ImageStatus = "failed" + ImageStatusPending ImageStatus = "pending" + ImageStatusPulling ImageStatus = "pulling" + ImageStatusReady ImageStatus = "ready" ) // Defines values for InstanceState. @@ -60,6 +70,12 @@ const ( Unknown InstanceState = "Unknown" ) +// Defines values for CreateBuildMultipartBodyRuntime. +const ( + Nodejs20 CreateBuildMultipartBodyRuntime = "nodejs20" + Python312 CreateBuildMultipartBodyRuntime = "python312" +) + // Defines values for GetInstanceLogsParamsSource. const ( App GetInstanceLogsParamsSource = "app" @@ -100,6 +116,67 @@ type AvailableDevice struct { VendorName *string `json:"vendor_name,omitempty"` } +// Build defines model for Build. +type Build struct { + // CompletedAt Build completion timestamp + CompletedAt *time.Time `json:"completed_at"` + + // CreatedAt Build creation timestamp + CreatedAt time.Time `json:"created_at"` + + // DurationMs Build duration in milliseconds + DurationMs *int64 `json:"duration_ms"` + + // Error Error message (only when status is failed) + Error *string `json:"error"` + + // Id Build job identifier + Id string `json:"id"` + + // ImageDigest Digest of built image (only when status is ready) + ImageDigest *string `json:"image_digest"` + + // ImageRef Full image reference (only when status is ready) + ImageRef *string `json:"image_ref"` + Provenance *BuildProvenance `json:"provenance,omitempty"` + + // QueuePosition Position in build queue (only when status is queued) + QueuePosition *int `json:"queue_position"` + + // Runtime Build runtime + Runtime string `json:"runtime"` + + // StartedAt Build start timestamp + StartedAt *time.Time `json:"started_at"` + + // Status Build job status + Status BuildStatus `json:"status"` +} + +// BuildProvenance defines model for BuildProvenance. +type BuildProvenance struct { + // BaseImageDigest Pinned base image digest used + BaseImageDigest *string `json:"base_image_digest,omitempty"` + + // BuildkitVersion BuildKit version used + BuildkitVersion *string `json:"buildkit_version,omitempty"` + + // LockfileHashes Map of lockfile names to SHA256 hashes + LockfileHashes *map[string]string `json:"lockfile_hashes,omitempty"` + + // SourceHash SHA256 hash of source tarball + SourceHash *string `json:"source_hash,omitempty"` + + // Timestamp Build completion timestamp + Timestamp *time.Time `json:"timestamp,omitempty"` + + // ToolchainVersion Runtime version (e.g., "node v20.10.0") + ToolchainVersion *string `json:"toolchain_version,omitempty"` +} + +// BuildStatus Build job status +type BuildStatus string + // CreateDeviceRequest defines model for CreateDeviceRequest. type CreateDeviceRequest struct { // Name Optional globally unique device name. If not provided, a name is auto-generated from the PCI address (e.g., "pci-0000-a2-00-0") @@ -462,6 +539,36 @@ type VolumeMount struct { VolumeId string `json:"volume_id"` } +// CreateBuildMultipartBody defines parameters for CreateBuild. +type CreateBuildMultipartBody struct { + // BaseImageDigest Optional pinned base image digest + BaseImageDigest *string `json:"base_image_digest,omitempty"` + + // CacheScope Tenant-specific cache key prefix + CacheScope *string `json:"cache_scope,omitempty"` + + // Dockerfile Optional custom Dockerfile content + Dockerfile *string `json:"dockerfile,omitempty"` + + // Runtime Build runtime + Runtime CreateBuildMultipartBodyRuntime `json:"runtime"` + + // Source Source tarball (tar.gz) + Source openapi_types.File `json:"source"` + + // TimeoutSeconds Build timeout (default 600) + TimeoutSeconds *int `json:"timeout_seconds,omitempty"` +} + +// CreateBuildMultipartBodyRuntime defines parameters for CreateBuild. +type CreateBuildMultipartBodyRuntime string + +// GetBuildLogsParams defines parameters for GetBuildLogs. +type GetBuildLogsParams struct { + // Follow Continue streaming new lines after initial output + Follow *bool `form:"follow,omitempty" json:"follow,omitempty"` +} + // GetInstanceLogsParams defines parameters for GetInstanceLogs. type GetInstanceLogsParams struct { // Tail Number of lines to return from end @@ -495,6 +602,9 @@ type CreateVolumeMultipartBody struct { SizeGb int `json:"size_gb"` } +// CreateBuildMultipartRequestBody defines body for CreateBuild for multipart/form-data ContentType. +type CreateBuildMultipartRequestBody CreateBuildMultipartBody + // CreateDeviceJSONRequestBody defines body for CreateDevice for application/json ContentType. type CreateDeviceJSONRequestBody = CreateDeviceRequest @@ -589,6 +699,21 @@ func WithRequestEditorFn(fn RequestEditorFn) ClientOption { // The interface specification for the client above. type ClientInterface interface { + // ListBuilds request + ListBuilds(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) + + // CreateBuildWithBody request with any body + CreateBuildWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) + + // CancelBuild request + CancelBuild(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) + + // GetBuild request + GetBuild(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) + + // GetBuildLogs request + GetBuildLogs(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*http.Response, error) + // ListDevices request ListDevices(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -689,6 +814,66 @@ type ClientInterface interface { GetVolume(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) } +func (c *Client) ListBuilds(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewListBuildsRequest(c.Server) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +func (c *Client) CreateBuildWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCreateBuildRequestWithBody(c.Server, contentType, body) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +func (c *Client) CancelBuild(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewCancelBuildRequest(c.Server, id) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +func (c *Client) GetBuild(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetBuildRequest(c.Server, id) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + +func (c *Client) GetBuildLogs(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetBuildLogsRequest(c.Server, id, params) + if err != nil { + return nil, err + } + req = req.WithContext(ctx) + if err := c.applyEditors(ctx, req, reqEditors); err != nil { + return nil, err + } + return c.Client.Do(req) +} + func (c *Client) ListDevices(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewListDevicesRequest(c.Server) if err != nil { @@ -1109,8 +1294,8 @@ func (c *Client) GetVolume(ctx context.Context, id string, reqEditors ...Request return c.Client.Do(req) } -// NewListDevicesRequest generates requests for ListDevices -func NewListDevicesRequest(server string) (*http.Request, error) { +// NewListBuildsRequest generates requests for ListBuilds +func NewListBuildsRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1118,7 +1303,7 @@ func NewListDevicesRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/devices") + operationPath := fmt.Sprintf("/builds") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1136,19 +1321,8 @@ func NewListDevicesRequest(server string) (*http.Request, error) { return req, nil } -// NewCreateDeviceRequest calls the generic CreateDevice builder with application/json body -func NewCreateDeviceRequest(server string, body CreateDeviceJSONRequestBody) (*http.Request, error) { - var bodyReader io.Reader - buf, err := json.Marshal(body) - if err != nil { - return nil, err - } - bodyReader = bytes.NewReader(buf) - return NewCreateDeviceRequestWithBody(server, "application/json", bodyReader) -} - -// NewCreateDeviceRequestWithBody generates requests for CreateDevice with any type of body -func NewCreateDeviceRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewCreateBuildRequestWithBody generates requests for CreateBuild with any type of body +func NewCreateBuildRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1156,7 +1330,7 @@ func NewCreateDeviceRequestWithBody(server string, contentType string, body io.R return nil, err } - operationPath := fmt.Sprintf("/devices") + operationPath := fmt.Sprintf("/builds") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1176,16 +1350,23 @@ func NewCreateDeviceRequestWithBody(server string, contentType string, body io.R return req, nil } -// NewListAvailableDevicesRequest generates requests for ListAvailableDevices -func NewListAvailableDevicesRequest(server string) (*http.Request, error) { +// NewCancelBuildRequest generates requests for CancelBuild +func NewCancelBuildRequest(server string, id string) (*http.Request, error) { var err error + var pathParam0 string + + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "id", runtime.ParamLocationPath, id) + if err != nil { + return nil, err + } + serverURL, err := url.Parse(server) if err != nil { return nil, err } - operationPath := fmt.Sprintf("/devices/available") + operationPath := fmt.Sprintf("/builds/%s", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1195,7 +1376,7 @@ func NewListAvailableDevicesRequest(server string) (*http.Request, error) { return nil, err } - req, err := http.NewRequest("GET", queryURL.String(), nil) + req, err := http.NewRequest("DELETE", queryURL.String(), nil) if err != nil { return nil, err } @@ -1203,8 +1384,8 @@ func NewListAvailableDevicesRequest(server string) (*http.Request, error) { return req, nil } -// NewDeleteDeviceRequest generates requests for DeleteDevice -func NewDeleteDeviceRequest(server string, id string) (*http.Request, error) { +// NewGetBuildRequest generates requests for GetBuild +func NewGetBuildRequest(server string, id string) (*http.Request, error) { var err error var pathParam0 string @@ -1219,7 +1400,7 @@ func NewDeleteDeviceRequest(server string, id string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/devices/%s", pathParam0) + operationPath := fmt.Sprintf("/builds/%s", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1229,7 +1410,7 @@ func NewDeleteDeviceRequest(server string, id string) (*http.Request, error) { return nil, err } - req, err := http.NewRequest("DELETE", queryURL.String(), nil) + req, err := http.NewRequest("GET", queryURL.String(), nil) if err != nil { return nil, err } @@ -1237,8 +1418,8 @@ func NewDeleteDeviceRequest(server string, id string) (*http.Request, error) { return req, nil } -// NewGetDeviceRequest generates requests for GetDevice -func NewGetDeviceRequest(server string, id string) (*http.Request, error) { +// NewGetBuildLogsRequest generates requests for GetBuildLogs +func NewGetBuildLogsRequest(server string, id string, params *GetBuildLogsParams) (*http.Request, error) { var err error var pathParam0 string @@ -1253,7 +1434,7 @@ func NewGetDeviceRequest(server string, id string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/devices/%s", pathParam0) + operationPath := fmt.Sprintf("/builds/%s/logs", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1263,6 +1444,28 @@ func NewGetDeviceRequest(server string, id string) (*http.Request, error) { return nil, err } + if params != nil { + queryValues := queryURL.Query() + + if params.Follow != nil { + + if queryFrag, err := runtime.StyleParamWithLocation("form", true, "follow", runtime.ParamLocationQuery, *params.Follow); err != nil { + return nil, err + } else if parsed, err := url.ParseQuery(queryFrag); err != nil { + return nil, err + } else { + for k, v := range parsed { + for _, v2 := range v { + queryValues.Add(k, v2) + } + } + } + + } + + queryURL.RawQuery = queryValues.Encode() + } + req, err := http.NewRequest("GET", queryURL.String(), nil) if err != nil { return nil, err @@ -1271,8 +1474,8 @@ func NewGetDeviceRequest(server string, id string) (*http.Request, error) { return req, nil } -// NewGetHealthRequest generates requests for GetHealth -func NewGetHealthRequest(server string) (*http.Request, error) { +// NewListDevicesRequest generates requests for ListDevices +func NewListDevicesRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1280,7 +1483,7 @@ func NewGetHealthRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/health") + operationPath := fmt.Sprintf("/devices") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1298,8 +1501,19 @@ func NewGetHealthRequest(server string) (*http.Request, error) { return req, nil } -// NewListImagesRequest generates requests for ListImages -func NewListImagesRequest(server string) (*http.Request, error) { +// NewCreateDeviceRequest calls the generic CreateDevice builder with application/json body +func NewCreateDeviceRequest(server string, body CreateDeviceJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewCreateDeviceRequestWithBody(server, "application/json", bodyReader) +} + +// NewCreateDeviceRequestWithBody generates requests for CreateDevice with any type of body +func NewCreateDeviceRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1307,7 +1521,7 @@ func NewListImagesRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/images") + operationPath := fmt.Sprintf("/devices") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1317,27 +1531,18 @@ func NewListImagesRequest(server string) (*http.Request, error) { return nil, err } - req, err := http.NewRequest("GET", queryURL.String(), nil) + req, err := http.NewRequest("POST", queryURL.String(), body) if err != nil { return nil, err } - return req, nil -} + req.Header.Add("Content-Type", contentType) -// NewCreateImageRequest calls the generic CreateImage builder with application/json body -func NewCreateImageRequest(server string, body CreateImageJSONRequestBody) (*http.Request, error) { - var bodyReader io.Reader - buf, err := json.Marshal(body) - if err != nil { - return nil, err - } - bodyReader = bytes.NewReader(buf) - return NewCreateImageRequestWithBody(server, "application/json", bodyReader) + return req, nil } -// NewCreateImageRequestWithBody generates requests for CreateImage with any type of body -func NewCreateImageRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewListAvailableDevicesRequest generates requests for ListAvailableDevices +func NewListAvailableDevicesRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1345,7 +1550,7 @@ func NewCreateImageRequestWithBody(server string, contentType string, body io.Re return nil, err } - operationPath := fmt.Sprintf("/images") + operationPath := fmt.Sprintf("/devices/available") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1355,23 +1560,21 @@ func NewCreateImageRequestWithBody(server string, contentType string, body io.Re return nil, err } - req, err := http.NewRequest("POST", queryURL.String(), body) + req, err := http.NewRequest("GET", queryURL.String(), nil) if err != nil { return nil, err } - req.Header.Add("Content-Type", contentType) - return req, nil } -// NewDeleteImageRequest generates requests for DeleteImage -func NewDeleteImageRequest(server string, name string) (*http.Request, error) { +// NewDeleteDeviceRequest generates requests for DeleteDevice +func NewDeleteDeviceRequest(server string, id string) (*http.Request, error) { var err error var pathParam0 string - pathParam0, err = runtime.StyleParamWithLocation("simple", false, "name", runtime.ParamLocationPath, name) + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "id", runtime.ParamLocationPath, id) if err != nil { return nil, err } @@ -1381,7 +1584,7 @@ func NewDeleteImageRequest(server string, name string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/images/%s", pathParam0) + operationPath := fmt.Sprintf("/devices/%s", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1399,13 +1602,13 @@ func NewDeleteImageRequest(server string, name string) (*http.Request, error) { return req, nil } -// NewGetImageRequest generates requests for GetImage -func NewGetImageRequest(server string, name string) (*http.Request, error) { +// NewGetDeviceRequest generates requests for GetDevice +func NewGetDeviceRequest(server string, id string) (*http.Request, error) { var err error var pathParam0 string - pathParam0, err = runtime.StyleParamWithLocation("simple", false, "name", runtime.ParamLocationPath, name) + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "id", runtime.ParamLocationPath, id) if err != nil { return nil, err } @@ -1415,7 +1618,7 @@ func NewGetImageRequest(server string, name string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/images/%s", pathParam0) + operationPath := fmt.Sprintf("/devices/%s", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1433,8 +1636,8 @@ func NewGetImageRequest(server string, name string) (*http.Request, error) { return req, nil } -// NewListIngressesRequest generates requests for ListIngresses -func NewListIngressesRequest(server string) (*http.Request, error) { +// NewGetHealthRequest generates requests for GetHealth +func NewGetHealthRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1442,7 +1645,7 @@ func NewListIngressesRequest(server string) (*http.Request, error) { return nil, err } - operationPath := fmt.Sprintf("/ingresses") + operationPath := fmt.Sprintf("/health") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1460,19 +1663,8 @@ func NewListIngressesRequest(server string) (*http.Request, error) { return req, nil } -// NewCreateIngressRequest calls the generic CreateIngress builder with application/json body -func NewCreateIngressRequest(server string, body CreateIngressJSONRequestBody) (*http.Request, error) { - var bodyReader io.Reader - buf, err := json.Marshal(body) - if err != nil { - return nil, err - } - bodyReader = bytes.NewReader(buf) - return NewCreateIngressRequestWithBody(server, "application/json", bodyReader) -} - -// NewCreateIngressRequestWithBody generates requests for CreateIngress with any type of body -func NewCreateIngressRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { +// NewListImagesRequest generates requests for ListImages +func NewListImagesRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) @@ -1480,7 +1672,7 @@ func NewCreateIngressRequestWithBody(server string, contentType string, body io. return nil, err } - operationPath := fmt.Sprintf("/ingresses") + operationPath := fmt.Sprintf("/images") if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -1490,23 +1682,196 @@ func NewCreateIngressRequestWithBody(server string, contentType string, body io. return nil, err } - req, err := http.NewRequest("POST", queryURL.String(), body) + req, err := http.NewRequest("GET", queryURL.String(), nil) if err != nil { return nil, err } - req.Header.Add("Content-Type", contentType) - return req, nil } -// NewDeleteIngressRequest generates requests for DeleteIngress -func NewDeleteIngressRequest(server string, id string) (*http.Request, error) { - var err error - - var pathParam0 string - - pathParam0, err = runtime.StyleParamWithLocation("simple", false, "id", runtime.ParamLocationPath, id) +// NewCreateImageRequest calls the generic CreateImage builder with application/json body +func NewCreateImageRequest(server string, body CreateImageJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewCreateImageRequestWithBody(server, "application/json", bodyReader) +} + +// NewCreateImageRequestWithBody generates requests for CreateImage with any type of body +func NewCreateImageRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/images") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", queryURL.String(), body) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) + + return req, nil +} + +// NewDeleteImageRequest generates requests for DeleteImage +func NewDeleteImageRequest(server string, name string) (*http.Request, error) { + var err error + + var pathParam0 string + + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "name", runtime.ParamLocationPath, name) + if err != nil { + return nil, err + } + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/images/%s", pathParam0) + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("DELETE", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + +// NewGetImageRequest generates requests for GetImage +func NewGetImageRequest(server string, name string) (*http.Request, error) { + var err error + + var pathParam0 string + + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "name", runtime.ParamLocationPath, name) + if err != nil { + return nil, err + } + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/images/%s", pathParam0) + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + +// NewListIngressesRequest generates requests for ListIngresses +func NewListIngressesRequest(server string) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/ingresses") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", queryURL.String(), nil) + if err != nil { + return nil, err + } + + return req, nil +} + +// NewCreateIngressRequest calls the generic CreateIngress builder with application/json body +func NewCreateIngressRequest(server string, body CreateIngressJSONRequestBody) (*http.Request, error) { + var bodyReader io.Reader + buf, err := json.Marshal(body) + if err != nil { + return nil, err + } + bodyReader = bytes.NewReader(buf) + return NewCreateIngressRequestWithBody(server, "application/json", bodyReader) +} + +// NewCreateIngressRequestWithBody generates requests for CreateIngress with any type of body +func NewCreateIngressRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { + var err error + + serverURL, err := url.Parse(server) + if err != nil { + return nil, err + } + + operationPath := fmt.Sprintf("/ingresses") + if operationPath[0] == '/' { + operationPath = "." + operationPath + } + + queryURL, err := serverURL.Parse(operationPath) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("POST", queryURL.String(), body) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) + + return req, nil +} + +// NewDeleteIngressRequest generates requests for DeleteIngress +func NewDeleteIngressRequest(server string, id string) (*http.Request, error) { + var err error + + var pathParam0 string + + pathParam0, err = runtime.StyleParamWithLocation("simple", false, "id", runtime.ParamLocationPath, id) if err != nil { return nil, err } @@ -2200,6 +2565,21 @@ func WithBaseURL(baseURL string) ClientOption { // ClientWithResponsesInterface is the interface specification for the client with responses above. type ClientWithResponsesInterface interface { + // ListBuildsWithResponse request + ListBuildsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListBuildsResponse, error) + + // CreateBuildWithBodyWithResponse request with any body + CreateBuildWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateBuildResponse, error) + + // CancelBuildWithResponse request + CancelBuildWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*CancelBuildResponse, error) + + // GetBuildWithResponse request + GetBuildWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetBuildResponse, error) + + // GetBuildLogsWithResponse request + GetBuildLogsWithResponse(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*GetBuildLogsResponse, error) + // ListDevicesWithResponse request ListDevicesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListDevicesResponse, error) @@ -2300,6 +2680,126 @@ type ClientWithResponsesInterface interface { GetVolumeWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetVolumeResponse, error) } +type ListBuildsResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *[]Build + JSON401 *Error + JSON500 *Error +} + +// Status returns HTTPResponse.Status +func (r ListBuildsResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r ListBuildsResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +type CreateBuildResponse struct { + Body []byte + HTTPResponse *http.Response + JSON202 *Build + JSON400 *Error + JSON401 *Error + JSON500 *Error +} + +// Status returns HTTPResponse.Status +func (r CreateBuildResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r CreateBuildResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +type CancelBuildResponse struct { + Body []byte + HTTPResponse *http.Response + JSON404 *Error + JSON409 *Error + JSON500 *Error +} + +// Status returns HTTPResponse.Status +func (r CancelBuildResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r CancelBuildResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +type GetBuildResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *Build + JSON404 *Error + JSON500 *Error +} + +// Status returns HTTPResponse.Status +func (r GetBuildResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r GetBuildResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + +type GetBuildLogsResponse struct { + Body []byte + HTTPResponse *http.Response + JSON404 *Error + JSON500 *Error +} + +// Status returns HTTPResponse.Status +func (r GetBuildLogsResponse) Status() string { + if r.HTTPResponse != nil { + return r.HTTPResponse.Status + } + return http.StatusText(0) +} + +// StatusCode returns HTTPResponse.StatusCode +func (r GetBuildLogsResponse) StatusCode() int { + if r.HTTPResponse != nil { + return r.HTTPResponse.StatusCode + } + return 0 +} + type ListDevicesResponse struct { Body []byte HTTPResponse *http.Response @@ -3007,6 +3507,51 @@ func (r GetVolumeResponse) StatusCode() int { return 0 } +// ListBuildsWithResponse request returning *ListBuildsResponse +func (c *ClientWithResponses) ListBuildsWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListBuildsResponse, error) { + rsp, err := c.ListBuilds(ctx, reqEditors...) + if err != nil { + return nil, err + } + return ParseListBuildsResponse(rsp) +} + +// CreateBuildWithBodyWithResponse request with arbitrary body returning *CreateBuildResponse +func (c *ClientWithResponses) CreateBuildWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateBuildResponse, error) { + rsp, err := c.CreateBuildWithBody(ctx, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateBuildResponse(rsp) +} + +// CancelBuildWithResponse request returning *CancelBuildResponse +func (c *ClientWithResponses) CancelBuildWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*CancelBuildResponse, error) { + rsp, err := c.CancelBuild(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseCancelBuildResponse(rsp) +} + +// GetBuildWithResponse request returning *GetBuildResponse +func (c *ClientWithResponses) GetBuildWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetBuildResponse, error) { + rsp, err := c.GetBuild(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseGetBuildResponse(rsp) +} + +// GetBuildLogsWithResponse request returning *GetBuildLogsResponse +func (c *ClientWithResponses) GetBuildLogsWithResponse(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*GetBuildLogsResponse, error) { + rsp, err := c.GetBuildLogs(ctx, id, params, reqEditors...) + if err != nil { + return nil, err + } + return ParseGetBuildLogsResponse(rsp) +} + // ListDevicesWithResponse request returning *ListDevicesResponse func (c *ClientWithResponses) ListDevicesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListDevicesResponse, error) { rsp, err := c.ListDevices(ctx, reqEditors...) @@ -3228,92 +3773,292 @@ func (c *ClientWithResponses) StandbyInstanceWithResponse(ctx context.Context, i return ParseStandbyInstanceResponse(rsp) } -// StartInstanceWithResponse request returning *StartInstanceResponse -func (c *ClientWithResponses) StartInstanceWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*StartInstanceResponse, error) { - rsp, err := c.StartInstance(ctx, id, reqEditors...) - if err != nil { - return nil, err +// StartInstanceWithResponse request returning *StartInstanceResponse +func (c *ClientWithResponses) StartInstanceWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*StartInstanceResponse, error) { + rsp, err := c.StartInstance(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseStartInstanceResponse(rsp) +} + +// StopInstanceWithResponse request returning *StopInstanceResponse +func (c *ClientWithResponses) StopInstanceWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*StopInstanceResponse, error) { + rsp, err := c.StopInstance(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseStopInstanceResponse(rsp) +} + +// DetachVolumeWithResponse request returning *DetachVolumeResponse +func (c *ClientWithResponses) DetachVolumeWithResponse(ctx context.Context, id string, volumeId string, reqEditors ...RequestEditorFn) (*DetachVolumeResponse, error) { + rsp, err := c.DetachVolume(ctx, id, volumeId, reqEditors...) + if err != nil { + return nil, err + } + return ParseDetachVolumeResponse(rsp) +} + +// AttachVolumeWithBodyWithResponse request with arbitrary body returning *AttachVolumeResponse +func (c *ClientWithResponses) AttachVolumeWithBodyWithResponse(ctx context.Context, id string, volumeId string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*AttachVolumeResponse, error) { + rsp, err := c.AttachVolumeWithBody(ctx, id, volumeId, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseAttachVolumeResponse(rsp) +} + +func (c *ClientWithResponses) AttachVolumeWithResponse(ctx context.Context, id string, volumeId string, body AttachVolumeJSONRequestBody, reqEditors ...RequestEditorFn) (*AttachVolumeResponse, error) { + rsp, err := c.AttachVolume(ctx, id, volumeId, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseAttachVolumeResponse(rsp) +} + +// ListVolumesWithResponse request returning *ListVolumesResponse +func (c *ClientWithResponses) ListVolumesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListVolumesResponse, error) { + rsp, err := c.ListVolumes(ctx, reqEditors...) + if err != nil { + return nil, err + } + return ParseListVolumesResponse(rsp) +} + +// CreateVolumeWithBodyWithResponse request with arbitrary body returning *CreateVolumeResponse +func (c *ClientWithResponses) CreateVolumeWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateVolumeResponse, error) { + rsp, err := c.CreateVolumeWithBody(ctx, contentType, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateVolumeResponse(rsp) +} + +func (c *ClientWithResponses) CreateVolumeWithResponse(ctx context.Context, body CreateVolumeJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateVolumeResponse, error) { + rsp, err := c.CreateVolume(ctx, body, reqEditors...) + if err != nil { + return nil, err + } + return ParseCreateVolumeResponse(rsp) +} + +// DeleteVolumeWithResponse request returning *DeleteVolumeResponse +func (c *ClientWithResponses) DeleteVolumeWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*DeleteVolumeResponse, error) { + rsp, err := c.DeleteVolume(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseDeleteVolumeResponse(rsp) +} + +// GetVolumeWithResponse request returning *GetVolumeResponse +func (c *ClientWithResponses) GetVolumeWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetVolumeResponse, error) { + rsp, err := c.GetVolume(ctx, id, reqEditors...) + if err != nil { + return nil, err + } + return ParseGetVolumeResponse(rsp) +} + +// ParseListBuildsResponse parses an HTTP response from a ListBuildsWithResponse call +func ParseListBuildsResponse(rsp *http.Response) (*ListBuildsResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &ListBuildsResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest []Build + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 401: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON401 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + } + + return response, nil +} + +// ParseCreateBuildResponse parses an HTTP response from a CreateBuildWithResponse call +func ParseCreateBuildResponse(rsp *http.Response) (*CreateBuildResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &CreateBuildResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 202: + var dest Build + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON202 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 400: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON400 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 401: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON401 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + + } + + return response, nil +} + +// ParseCancelBuildResponse parses an HTTP response from a CancelBuildWithResponse call +func ParseCancelBuildResponse(rsp *http.Response) (*CancelBuildResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() + if err != nil { + return nil, err + } + + response := &CancelBuildResponse{ + Body: bodyBytes, + HTTPResponse: rsp, + } + + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON404 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 409: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON409 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + } - return ParseStartInstanceResponse(rsp) -} -// StopInstanceWithResponse request returning *StopInstanceResponse -func (c *ClientWithResponses) StopInstanceWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*StopInstanceResponse, error) { - rsp, err := c.StopInstance(ctx, id, reqEditors...) - if err != nil { - return nil, err - } - return ParseStopInstanceResponse(rsp) + return response, nil } -// DetachVolumeWithResponse request returning *DetachVolumeResponse -func (c *ClientWithResponses) DetachVolumeWithResponse(ctx context.Context, id string, volumeId string, reqEditors ...RequestEditorFn) (*DetachVolumeResponse, error) { - rsp, err := c.DetachVolume(ctx, id, volumeId, reqEditors...) +// ParseGetBuildResponse parses an HTTP response from a GetBuildWithResponse call +func ParseGetBuildResponse(rsp *http.Response) (*GetBuildResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - return ParseDetachVolumeResponse(rsp) -} -// AttachVolumeWithBodyWithResponse request with arbitrary body returning *AttachVolumeResponse -func (c *ClientWithResponses) AttachVolumeWithBodyWithResponse(ctx context.Context, id string, volumeId string, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*AttachVolumeResponse, error) { - rsp, err := c.AttachVolumeWithBody(ctx, id, volumeId, contentType, body, reqEditors...) - if err != nil { - return nil, err + response := &GetBuildResponse{ + Body: bodyBytes, + HTTPResponse: rsp, } - return ParseAttachVolumeResponse(rsp) -} -func (c *ClientWithResponses) AttachVolumeWithResponse(ctx context.Context, id string, volumeId string, body AttachVolumeJSONRequestBody, reqEditors ...RequestEditorFn) (*AttachVolumeResponse, error) { - rsp, err := c.AttachVolume(ctx, id, volumeId, body, reqEditors...) - if err != nil { - return nil, err - } - return ParseAttachVolumeResponse(rsp) -} + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: + var dest Build + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest -// ListVolumesWithResponse request returning *ListVolumesResponse -func (c *ClientWithResponses) ListVolumesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListVolumesResponse, error) { - rsp, err := c.ListVolumes(ctx, reqEditors...) - if err != nil { - return nil, err - } - return ParseListVolumesResponse(rsp) -} + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON404 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest -// CreateVolumeWithBodyWithResponse request with arbitrary body returning *CreateVolumeResponse -func (c *ClientWithResponses) CreateVolumeWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateVolumeResponse, error) { - rsp, err := c.CreateVolumeWithBody(ctx, contentType, body, reqEditors...) - if err != nil { - return nil, err } - return ParseCreateVolumeResponse(rsp) + + return response, nil } -func (c *ClientWithResponses) CreateVolumeWithResponse(ctx context.Context, body CreateVolumeJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateVolumeResponse, error) { - rsp, err := c.CreateVolume(ctx, body, reqEditors...) +// ParseGetBuildLogsResponse parses an HTTP response from a GetBuildLogsWithResponse call +func ParseGetBuildLogsResponse(rsp *http.Response) (*GetBuildLogsResponse, error) { + bodyBytes, err := io.ReadAll(rsp.Body) + defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - return ParseCreateVolumeResponse(rsp) -} -// DeleteVolumeWithResponse request returning *DeleteVolumeResponse -func (c *ClientWithResponses) DeleteVolumeWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*DeleteVolumeResponse, error) { - rsp, err := c.DeleteVolume(ctx, id, reqEditors...) - if err != nil { - return nil, err + response := &GetBuildLogsResponse{ + Body: bodyBytes, + HTTPResponse: rsp, } - return ParseDeleteVolumeResponse(rsp) -} -// GetVolumeWithResponse request returning *GetVolumeResponse -func (c *ClientWithResponses) GetVolumeWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetVolumeResponse, error) { - rsp, err := c.GetVolume(ctx, id, reqEditors...) - if err != nil { - return nil, err + switch { + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 404: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON404 = &dest + + case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 500: + var dest Error + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON500 = &dest + } - return ParseGetVolumeResponse(rsp) + + return response, nil } // ParseListDevicesResponse parses an HTTP response from a ListDevicesWithResponse call @@ -4555,6 +5300,21 @@ func ParseGetVolumeResponse(rsp *http.Response) (*GetVolumeResponse, error) { // ServerInterface represents all server handlers. type ServerInterface interface { + // List builds + // (GET /builds) + ListBuilds(w http.ResponseWriter, r *http.Request) + // Create a new build + // (POST /builds) + CreateBuild(w http.ResponseWriter, r *http.Request) + // Cancel build + // (DELETE /builds/{id}) + CancelBuild(w http.ResponseWriter, r *http.Request, id string) + // Get build details + // (GET /builds/{id}) + GetBuild(w http.ResponseWriter, r *http.Request, id string) + // Stream build logs (SSE) + // (GET /builds/{id}/logs) + GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) // List registered devices // (GET /devices) ListDevices(w http.ResponseWriter, r *http.Request) @@ -4648,6 +5408,36 @@ type ServerInterface interface { type Unimplemented struct{} +// List builds +// (GET /builds) +func (_ Unimplemented) ListBuilds(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotImplemented) +} + +// Create a new build +// (POST /builds) +func (_ Unimplemented) CreateBuild(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotImplemented) +} + +// Cancel build +// (DELETE /builds/{id}) +func (_ Unimplemented) CancelBuild(w http.ResponseWriter, r *http.Request, id string) { + w.WriteHeader(http.StatusNotImplemented) +} + +// Get build details +// (GET /builds/{id}) +func (_ Unimplemented) GetBuild(w http.ResponseWriter, r *http.Request, id string) { + w.WriteHeader(http.StatusNotImplemented) +} + +// Stream build logs (SSE) +// (GET /builds/{id}/logs) +func (_ Unimplemented) GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) { + w.WriteHeader(http.StatusNotImplemented) +} + // List registered devices // (GET /devices) func (_ Unimplemented) ListDevices(w http.ResponseWriter, r *http.Request) { @@ -4831,6 +5621,150 @@ type ServerInterfaceWrapper struct { type MiddlewareFunc func(http.Handler) http.Handler +// ListBuilds operation middleware +func (siw *ServerInterfaceWrapper) ListBuilds(w http.ResponseWriter, r *http.Request) { + + ctx := r.Context() + + ctx = context.WithValue(ctx, BearerAuthScopes, []string{}) + + r = r.WithContext(ctx) + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.ListBuilds(w, r) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r) +} + +// CreateBuild operation middleware +func (siw *ServerInterfaceWrapper) CreateBuild(w http.ResponseWriter, r *http.Request) { + + ctx := r.Context() + + ctx = context.WithValue(ctx, BearerAuthScopes, []string{}) + + r = r.WithContext(ctx) + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.CreateBuild(w, r) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r) +} + +// CancelBuild operation middleware +func (siw *ServerInterfaceWrapper) CancelBuild(w http.ResponseWriter, r *http.Request) { + + var err error + + // ------------- Path parameter "id" ------------- + var id string + + err = runtime.BindStyledParameterWithOptions("simple", "id", chi.URLParam(r, "id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err}) + return + } + + ctx := r.Context() + + ctx = context.WithValue(ctx, BearerAuthScopes, []string{}) + + r = r.WithContext(ctx) + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.CancelBuild(w, r, id) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r) +} + +// GetBuild operation middleware +func (siw *ServerInterfaceWrapper) GetBuild(w http.ResponseWriter, r *http.Request) { + + var err error + + // ------------- Path parameter "id" ------------- + var id string + + err = runtime.BindStyledParameterWithOptions("simple", "id", chi.URLParam(r, "id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err}) + return + } + + ctx := r.Context() + + ctx = context.WithValue(ctx, BearerAuthScopes, []string{}) + + r = r.WithContext(ctx) + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.GetBuild(w, r, id) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r) +} + +// GetBuildLogs operation middleware +func (siw *ServerInterfaceWrapper) GetBuildLogs(w http.ResponseWriter, r *http.Request) { + + var err error + + // ------------- Path parameter "id" ------------- + var id string + + err = runtime.BindStyledParameterWithOptions("simple", "id", chi.URLParam(r, "id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true}) + if err != nil { + siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "id", Err: err}) + return + } + + ctx := r.Context() + + ctx = context.WithValue(ctx, BearerAuthScopes, []string{}) + + r = r.WithContext(ctx) + + // Parameter object where we will unmarshal all parameters from the context + var params GetBuildLogsParams + + // ------------- Optional query parameter "follow" ------------- + + err = runtime.BindQueryParameter("form", true, false, "follow", r.URL.Query(), ¶ms.Follow) + if err != nil { + siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "follow", Err: err}) + return + } + + handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + siw.Handler.GetBuildLogs(w, r, id, params) + })) + + for _, middleware := range siw.HandlerMiddlewares { + handler = middleware(handler) + } + + handler.ServeHTTP(w, r) +} + // ListDevices operation middleware func (siw *ServerInterfaceWrapper) ListDevices(w http.ResponseWriter, r *http.Request) { @@ -5750,6 +6684,21 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl ErrorHandlerFunc: options.ErrorHandlerFunc, } + r.Group(func(r chi.Router) { + r.Get(options.BaseURL+"/builds", wrapper.ListBuilds) + }) + r.Group(func(r chi.Router) { + r.Post(options.BaseURL+"/builds", wrapper.CreateBuild) + }) + r.Group(func(r chi.Router) { + r.Delete(options.BaseURL+"/builds/{id}", wrapper.CancelBuild) + }) + r.Group(func(r chi.Router) { + r.Get(options.BaseURL+"/builds/{id}", wrapper.GetBuild) + }) + r.Group(func(r chi.Router) { + r.Get(options.BaseURL+"/builds/{id}/logs", wrapper.GetBuildLogs) + }) r.Group(func(r chi.Router) { r.Get(options.BaseURL+"/devices", wrapper.ListDevices) }) @@ -5841,6 +6790,208 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl return r } +type ListBuildsRequestObject struct { +} + +type ListBuildsResponseObject interface { + VisitListBuildsResponse(w http.ResponseWriter) error +} + +type ListBuilds200JSONResponse []Build + +func (response ListBuilds200JSONResponse) VisitListBuildsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + + return json.NewEncoder(w).Encode(response) +} + +type ListBuilds401JSONResponse Error + +func (response ListBuilds401JSONResponse) VisitListBuildsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(401) + + return json.NewEncoder(w).Encode(response) +} + +type ListBuilds500JSONResponse Error + +func (response ListBuilds500JSONResponse) VisitListBuildsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(500) + + return json.NewEncoder(w).Encode(response) +} + +type CreateBuildRequestObject struct { + Body *multipart.Reader +} + +type CreateBuildResponseObject interface { + VisitCreateBuildResponse(w http.ResponseWriter) error +} + +type CreateBuild202JSONResponse Build + +func (response CreateBuild202JSONResponse) VisitCreateBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(202) + + return json.NewEncoder(w).Encode(response) +} + +type CreateBuild400JSONResponse Error + +func (response CreateBuild400JSONResponse) VisitCreateBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(400) + + return json.NewEncoder(w).Encode(response) +} + +type CreateBuild401JSONResponse Error + +func (response CreateBuild401JSONResponse) VisitCreateBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(401) + + return json.NewEncoder(w).Encode(response) +} + +type CreateBuild500JSONResponse Error + +func (response CreateBuild500JSONResponse) VisitCreateBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(500) + + return json.NewEncoder(w).Encode(response) +} + +type CancelBuildRequestObject struct { + Id string `json:"id"` +} + +type CancelBuildResponseObject interface { + VisitCancelBuildResponse(w http.ResponseWriter) error +} + +type CancelBuild204Response struct { +} + +func (response CancelBuild204Response) VisitCancelBuildResponse(w http.ResponseWriter) error { + w.WriteHeader(204) + return nil +} + +type CancelBuild404JSONResponse Error + +func (response CancelBuild404JSONResponse) VisitCancelBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(404) + + return json.NewEncoder(w).Encode(response) +} + +type CancelBuild409JSONResponse Error + +func (response CancelBuild409JSONResponse) VisitCancelBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(409) + + return json.NewEncoder(w).Encode(response) +} + +type CancelBuild500JSONResponse Error + +func (response CancelBuild500JSONResponse) VisitCancelBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(500) + + return json.NewEncoder(w).Encode(response) +} + +type GetBuildRequestObject struct { + Id string `json:"id"` +} + +type GetBuildResponseObject interface { + VisitGetBuildResponse(w http.ResponseWriter) error +} + +type GetBuild200JSONResponse Build + +func (response GetBuild200JSONResponse) VisitGetBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + + return json.NewEncoder(w).Encode(response) +} + +type GetBuild404JSONResponse Error + +func (response GetBuild404JSONResponse) VisitGetBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(404) + + return json.NewEncoder(w).Encode(response) +} + +type GetBuild500JSONResponse Error + +func (response GetBuild500JSONResponse) VisitGetBuildResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(500) + + return json.NewEncoder(w).Encode(response) +} + +type GetBuildLogsRequestObject struct { + Id string `json:"id"` + Params GetBuildLogsParams +} + +type GetBuildLogsResponseObject interface { + VisitGetBuildLogsResponse(w http.ResponseWriter) error +} + +type GetBuildLogs200TexteventStreamResponse struct { + Body io.Reader + ContentLength int64 +} + +func (response GetBuildLogs200TexteventStreamResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "text/event-stream") + if response.ContentLength != 0 { + w.Header().Set("Content-Length", fmt.Sprint(response.ContentLength)) + } + w.WriteHeader(200) + + if closer, ok := response.Body.(io.ReadCloser); ok { + defer closer.Close() + } + _, err := io.Copy(w, response.Body) + return err +} + +type GetBuildLogs404JSONResponse Error + +func (response GetBuildLogs404JSONResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(404) + + return json.NewEncoder(w).Encode(response) +} + +type GetBuildLogs500JSONResponse Error + +func (response GetBuildLogs500JSONResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(500) + + return json.NewEncoder(w).Encode(response) +} + type ListDevicesRequestObject struct { } @@ -7014,6 +8165,21 @@ func (response GetVolume500JSONResponse) VisitGetVolumeResponse(w http.ResponseW // StrictServerInterface represents all server handlers. type StrictServerInterface interface { + // List builds + // (GET /builds) + ListBuilds(ctx context.Context, request ListBuildsRequestObject) (ListBuildsResponseObject, error) + // Create a new build + // (POST /builds) + CreateBuild(ctx context.Context, request CreateBuildRequestObject) (CreateBuildResponseObject, error) + // Cancel build + // (DELETE /builds/{id}) + CancelBuild(ctx context.Context, request CancelBuildRequestObject) (CancelBuildResponseObject, error) + // Get build details + // (GET /builds/{id}) + GetBuild(ctx context.Context, request GetBuildRequestObject) (GetBuildResponseObject, error) + // Stream build logs (SSE) + // (GET /builds/{id}/logs) + GetBuildLogs(ctx context.Context, request GetBuildLogsRequestObject) (GetBuildLogsResponseObject, error) // List registered devices // (GET /devices) ListDevices(ctx context.Context, request ListDevicesRequestObject) (ListDevicesResponseObject, error) @@ -7132,6 +8298,140 @@ type strictHandler struct { options StrictHTTPServerOptions } +// ListBuilds operation middleware +func (sh *strictHandler) ListBuilds(w http.ResponseWriter, r *http.Request) { + var request ListBuildsRequestObject + + handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { + return sh.ssi.ListBuilds(ctx, request.(ListBuildsRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "ListBuilds") + } + + response, err := handler(r.Context(), w, r, request) + + if err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } else if validResponse, ok := response.(ListBuildsResponseObject); ok { + if err := validResponse.VisitListBuildsResponse(w); err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } + } else if response != nil { + sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) + } +} + +// CreateBuild operation middleware +func (sh *strictHandler) CreateBuild(w http.ResponseWriter, r *http.Request) { + var request CreateBuildRequestObject + + if reader, err := r.MultipartReader(); err != nil { + sh.options.RequestErrorHandlerFunc(w, r, fmt.Errorf("can't decode multipart body: %w", err)) + return + } else { + request.Body = reader + } + + handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { + return sh.ssi.CreateBuild(ctx, request.(CreateBuildRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "CreateBuild") + } + + response, err := handler(r.Context(), w, r, request) + + if err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } else if validResponse, ok := response.(CreateBuildResponseObject); ok { + if err := validResponse.VisitCreateBuildResponse(w); err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } + } else if response != nil { + sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) + } +} + +// CancelBuild operation middleware +func (sh *strictHandler) CancelBuild(w http.ResponseWriter, r *http.Request, id string) { + var request CancelBuildRequestObject + + request.Id = id + + handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { + return sh.ssi.CancelBuild(ctx, request.(CancelBuildRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "CancelBuild") + } + + response, err := handler(r.Context(), w, r, request) + + if err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } else if validResponse, ok := response.(CancelBuildResponseObject); ok { + if err := validResponse.VisitCancelBuildResponse(w); err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } + } else if response != nil { + sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) + } +} + +// GetBuild operation middleware +func (sh *strictHandler) GetBuild(w http.ResponseWriter, r *http.Request, id string) { + var request GetBuildRequestObject + + request.Id = id + + handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { + return sh.ssi.GetBuild(ctx, request.(GetBuildRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "GetBuild") + } + + response, err := handler(r.Context(), w, r, request) + + if err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } else if validResponse, ok := response.(GetBuildResponseObject); ok { + if err := validResponse.VisitGetBuildResponse(w); err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } + } else if response != nil { + sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) + } +} + +// GetBuildLogs operation middleware +func (sh *strictHandler) GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) { + var request GetBuildLogsRequestObject + + request.Id = id + request.Params = params + + handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { + return sh.ssi.GetBuildLogs(ctx, request.(GetBuildLogsRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "GetBuildLogs") + } + + response, err := handler(r.Context(), w, r, request) + + if err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } else if validResponse, ok := response.(GetBuildLogsResponseObject); ok { + if err := validResponse.VisitGetBuildLogsResponse(w); err != nil { + sh.options.ResponseErrorHandlerFunc(w, r, err) + } + } else if response != nil { + sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response)) + } +} + // ListDevices operation middleware func (sh *strictHandler) ListDevices(w http.ResponseWriter, r *http.Request) { var request ListDevicesRequestObject @@ -7921,104 +9221,116 @@ func (sh *strictHandler) GetVolume(w http.ResponseWriter, r *http.Request, id st // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+x9C3MTO7L/V+ma/26t81+/kgAL3rp1KycBjk8RSBHIubsn3CDPtG0dZqRB0jgxVL77", - "LT3mafkRIIYsqaIKx6ORulu/bnW3WvLnIORJyhkyJYPB50CGU0yI+XigFAmnZzzOEnyNHzOUSn+dCp6i", - "UBRNo4RnTF2kRE31XxHKUNBUUc6CQXBC1BQupygQZqYXkFOexRGMEMx7GAXtAK9IksYYDIJewlQvIooE", - "7UDNU/2VVIKySXDdDgSSiLN4bocZkyxWwWBMYontxrDHumsgEvQrHfNO0d+I8xgJC65Njx8zKjAKBn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3edNw7ZmhFtd9", - "bUQ2si9rDAeJU8pwqeVo/yjafsnFh5iTqLP7jZWdodJ9L7L40j6AkLMxnWTWQXBqj0Cdmum1r4ZXZFoi", - "UQ0w1tOsd//7FNUURUXD8i71V3alM69DTmFFIjXXteqEL4CYz1DEZO4B8W7fg+LfBVVmRt17EFH5AfTL", - "ayCse7MYfthfBHHfj2IPUR6aftGIcjq1CSUFIbt7x+7j3qZ6NQvTTNZI2muS89J40todmVGhMhLD4cnb", - "msnxOtY2ZPOYXRsRVk2tm/8CD0RBqG27xp+iZhXYaKmxPZv4bdHw+lcXa1eWry5rwlefh194rGEmFU+A", - "RsgUHVMdrzWcUVp3W+szNuNxR0ezxgJsaKYsuYuefzK3XdlJWQbNi8losctTjUDKYEInZDRX9cVmt784", - "9X5B5/37RL0sKrbwwOhCcU+wl6NleKTlmLfdJOI1MfSF4hezMfX0XFiq0vumEsJGCO5Aq7vopCF1IXkb", - "LqdU2zYJuRCMCT07rjoR3XPWAU3cAI6KAYpuiy71IqKV3i6tLS4qRFAGmUQYzXeAwNlxF94U1P5NAiOK", - "zjBPE0yJhBEig4zpJQUjM75JflQJyKT29qhqvu4cdptR2DG+EnfPuvDrPMWEMLikcWxirYQoGppAbUQb", - "/FxOkbmJ0iNpA8AKre+esyqyXGqmafLbgbEMGF0Q5fFYcUKlEqXlkIokKbRePzvc399/0jTSew87/d3O", - "7sM3u/1BX//7d9AOrHHVvgNR2HHmZxtJE19fB3V74ULfqkU5fDs82nMrQn0c9ekBefL46oqoJ4/opXzy", - "KRmJyZ/7ZCtpFb95OipjdmhlEkUnN30aVb5IvRIQL4nEvzjAvlFGx36xevmx3L3RLW8jB9SwqybxYpq0", - "vyBL0zSCNb1abqPfODHU+dHfav+gRL5mh2WJptPlS0Ja6baU61MhuPDkQ3nkGecgTWMaGu3uyBRDOqYh", - "oO4B9AvQSoxlwcJTqot1RKIL4VZyr0orQmMPZiqRjh3MtYSWNstJFiuaxmifGZRu5KwYzo9MT74okTKG", -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NCuj33D/Jokvb4P9", + "5+ELWwz25+ELUw725wf7eTfs20GWwbZY87a11zuMfEp5xWWgadZkKps3aXvZqK0ofLaFxE1UvmyD91pf", + "G62vCK61il/WzeMWVb9yp/wtxwkyZPNBWz9yyVS/mMq3XdeTxUiTrayLJEq+eFutpHvB28YEplftHcz2", + "whnGFflvSx9qTpBrtQOHuuPDru05YTpFZAmkW/Koun1sXUu0627fnbofT/E8ZakoNgbQLUaQyNt6lhjw", + "XdNfc/HcqMH+xFg62Kbo2LqCeo/3t6Q6Vy/UMG8TFtmkPLtR21Ge81BNe+3Z7fBee26lPRfAtV57ziru", + "b1N9Ln+qaOv6s8M3H8BtieOvqEHftRoE6r58mQd7SzyutYKad/FZL/vzL21sPdCfLb59vdR1b7ubyacs", + "MR+hcppgLmuaVcGfDR8G2+V921cB7zKKvSx2S/QrWzcoJMhmWltL4ForfjC1fR9AhqhAMiAQQaG0X+Ah", + "TH9hxmSt62Y0H2CSfMjKCXdG4KVO7yxA16ZpC8QxJCBkVDBimrp8WMbxh1G9ZPvs6Ei/pMcsTHH2h1H2", + "VZyMxoQadU7zagh1CgKFBK9tjUBHXThnhKAITFfgg4Jn4Xw7tmKC2QpRsjqnG6snPhTKJz401E84JGxT", + "QnFblN9tbrRkziIZ4Bpwpu0l0t9R8JVP2G88eIon9gbeziM/Ux1H94uv7MKW2pZQGSZJW/S129RYvIzj", + "NTgMOov8j0JGLJV/FTJC3HRDttjdhNygA0PzDwkvTO/eUsdI02rIBypbp+sFVWA6lLvCZfOvZRwH3cDu", + "x1O3/O9cEHPX1RINsjKzL5TFVCSHbXWli/29xtuJGfDLay6uJ9gPRsPthyIKu8C6WyGNpiv7ibqs0e2d", + "qgnQF5mfTMs7ey4vjbhnjTRie7T98jSS48cvTiUh47qdvHDtQ+9O8lbB4iiQe0d3dsw7Jnad1Xt2dLTT", + "RDSmI3kjyfB7c9jmUf7yMkU3u7x71GK6F8PsAOuchbtq0Dp6YMk9Odiup/fC404KD+0RzU7TmXMYollK", + "dJPfSHe29tGFbd28+8X8GG/yq0sYLs5cy9Sfw5diOyxuWsYd8E4QpT1ThOxHv7dOkyxrgnlH85v1h/ft", + "EbSNUYwQ+KWAaa77q2H39w8GF+F4o1DwVmkr+6D+z0Jb25Z8dg8ur7EIj7tC5gbT3Ekkq+iAhe8SNKbE", + "2E8UbCUhxrKWG6TDuBPcZw60SIYpAKtNU1gzvA9O0yRhXAogLxmIWYSEDkH84/TNazBl0WoEsvcoMI35", + "LcLZjuq2O6qyofBnpN49KnWKLUzg3kw46iUsSYlujakzjS2MjbCq96BtaDObSavby+qpMvLuTTvXFvZS", + "vo/yGUHWBhZi/ekLBVsLr7w5bIv+q7423NXGs8tMrHZg+Vta9qML5jPr1W8I/SRfIDiCVzhO4+wLwS+f", + "gY79+KX+zr3+ej+eZTiFrkKEIqEDVjs3/FpB/UMF9i6+ru/s92Nijps2SvgfmOqV91RSV6wkvkNyyRgg", + "kM/Rzi9TUGFpLa+nGB9WqinuYJLa0mFfrme0TEtrZ2C01PtvIyUtMz63m5B29vPoxIW2M3ewKmKZqZlN", + "mXA/FwoOticStp0Bd3aHfSgvkVOpC9lvegI1ow9hXrEQEhChJSIs0d/jMWODbpByYr8uMtrdJWrcggmp", + "v30bXL+//v8AAAD//9xUPPoopwAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/lib/paths/paths.go b/lib/paths/paths.go index ce06aeb0..55aaac9d 100644 --- a/lib/paths/paths.go +++ b/lib/paths/paths.go @@ -282,3 +282,40 @@ func (p *Paths) IngressesDir() string { func (p *Paths) IngressMetadata(id string) string { return filepath.Join(p.IngressesDir(), id+".json") } + +// Build path methods + +// BuildsDir returns the root builds directory. +func (p *Paths) BuildsDir() string { + return filepath.Join(p.dataDir, "builds") +} + +// BuildDir returns the directory for a specific build. +func (p *Paths) BuildDir(id string) string { + return filepath.Join(p.BuildsDir(), id) +} + +// BuildMetadata returns the path to build metadata.json. +func (p *Paths) BuildMetadata(id string) string { + return filepath.Join(p.BuildDir(id), "metadata.json") +} + +// BuildLogs returns the path to build logs directory. +func (p *Paths) BuildLogs(id string) string { + return filepath.Join(p.BuildDir(id), "logs") +} + +// BuildLog returns the path to the main build log file. +func (p *Paths) BuildLog(id string) string { + return filepath.Join(p.BuildLogs(id), "build.log") +} + +// BuildSourceDir returns the path to the source directory for a build. +func (p *Paths) BuildSourceDir(id string) string { + return filepath.Join(p.BuildDir(id), "source") +} + +// BuildConfig returns the path to the build config file (passed to builder VM). +func (p *Paths) BuildConfig(id string) string { + return filepath.Join(p.BuildDir(id), "config.json") +} diff --git a/lib/providers/providers.go b/lib/providers/providers.go index ecbeb708..19c13feb 100644 --- a/lib/providers/providers.go +++ b/lib/providers/providers.go @@ -8,6 +8,7 @@ import ( "github.com/c2h5oh/datasize" "github.com/onkernel/hypeman/cmd/api/config" + "github.com/onkernel/hypeman/lib/builds" "github.com/onkernel/hypeman/lib/devices" "github.com/onkernel/hypeman/lib/images" "github.com/onkernel/hypeman/lib/ingress" @@ -187,3 +188,30 @@ func ProvideIngressManager(p *paths.Paths, cfg *config.Config, instanceManager i resolver := instances.NewIngressResolver(instanceManager) return ingress.NewManager(p, ingressConfig, resolver, otelLogger), nil } + +// ProvideBuildManager provides the build manager +func ProvideBuildManager(p *paths.Paths, cfg *config.Config, instanceManager instances.Manager, volumeManager volumes.Manager, log *slog.Logger) (builds.Manager, error) { + buildConfig := builds.Config{ + MaxConcurrentBuilds: cfg.MaxConcurrentSourceBuilds, + BuilderImage: cfg.BuilderImage, + RegistryURL: cfg.RegistryURL, + DefaultTimeout: cfg.BuildTimeout, + } + + // Apply defaults if not set + if buildConfig.MaxConcurrentBuilds == 0 { + buildConfig.MaxConcurrentBuilds = 2 + } + if buildConfig.BuilderImage == "" { + buildConfig.BuilderImage = "hypeman/builder:latest" + } + if buildConfig.RegistryURL == "" { + buildConfig.RegistryURL = "localhost:8080" + } + if buildConfig.DefaultTimeout == 0 { + buildConfig.DefaultTimeout = 600 + } + + meter := otel.GetMeterProvider().Meter("hypeman") + return builds.NewManager(p, buildConfig, instanceManager, volumeManager, nil, log, meter) +} diff --git a/openapi.yaml b/openapi.yaml index a096083c..2222b084 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -609,6 +609,135 @@ components: nullable: true example: "nvidia" + BuildStatus: + type: string + enum: [queued, building, pushing, ready, failed, cancelled] + description: Build job status + + BuildPolicy: + type: object + properties: + timeout_seconds: + type: integer + description: Maximum build duration (default 600) + default: 600 + memory_mb: + type: integer + description: Memory limit for builder VM (default 2048) + default: 2048 + cpus: + type: integer + description: Number of vCPUs for builder VM (default 2) + default: 2 + network_mode: + type: string + enum: [isolated, egress] + description: Network access during build + default: egress + + BuildProvenance: + type: object + properties: + base_image_digest: + type: string + description: Pinned base image digest used + source_hash: + type: string + description: SHA256 hash of source tarball + lockfile_hashes: + type: object + additionalProperties: + type: string + description: Map of lockfile names to SHA256 hashes + toolchain_version: + type: string + description: Runtime version (e.g., "node v20.10.0") + buildkit_version: + type: string + description: BuildKit version used + timestamp: + type: string + format: date-time + description: Build completion timestamp + + CreateBuildRequest: + type: object + required: [runtime] + properties: + runtime: + type: string + enum: [nodejs20, python312] + description: Build runtime + example: nodejs20 + base_image_digest: + type: string + description: Optional pinned base image digest for reproducibility + cache_scope: + type: string + description: Tenant-specific cache key prefix for isolation + dockerfile: + type: string + description: Optional custom Dockerfile content + build_args: + type: object + additionalProperties: + type: string + description: Build arguments to pass to Dockerfile + build_policy: + $ref: "#/components/schemas/BuildPolicy" + + Build: + type: object + required: [id, status, runtime, created_at] + properties: + id: + type: string + description: Build job identifier + example: "build-abc123" + status: + $ref: "#/components/schemas/BuildStatus" + runtime: + type: string + description: Build runtime + example: nodejs20 + queue_position: + type: integer + description: Position in build queue (only when status is queued) + nullable: true + image_digest: + type: string + description: Digest of built image (only when status is ready) + nullable: true + image_ref: + type: string + description: Full image reference (only when status is ready) + nullable: true + error: + type: string + description: Error message (only when status is failed) + nullable: true + provenance: + $ref: "#/components/schemas/BuildProvenance" + created_at: + type: string + format: date-time + description: Build creation timestamp + started_at: + type: string + format: date-time + description: Build start timestamp + nullable: true + completed_at: + type: string + format: date-time + description: Build completion timestamp + nullable: true + duration_ms: + type: integer + format: int64 + description: Build duration in milliseconds + nullable: true + paths: /health: get: @@ -1665,4 +1794,202 @@ paths: schema: $ref: "#/components/schemas/Error" + /builds: + get: + summary: List builds + operationId: listBuilds + security: + - bearerAuth: [] + responses: + 200: + description: List of builds + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/Build" + 401: + description: Unauthorized + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 500: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + post: + summary: Create a new build + description: | + Creates a new build job. Source code should be uploaded as a tar.gz archive + in the multipart form data. + operationId: createBuild + security: + - bearerAuth: [] + requestBody: + required: true + content: + multipart/form-data: + schema: + type: object + required: + - runtime + - source + properties: + runtime: + type: string + enum: [nodejs20, python312] + description: Build runtime + source: + type: string + format: binary + description: Source tarball (tar.gz) + base_image_digest: + type: string + description: Optional pinned base image digest + cache_scope: + type: string + description: Tenant-specific cache key prefix + dockerfile: + type: string + description: Optional custom Dockerfile content + timeout_seconds: + type: integer + description: Build timeout (default 600) + responses: + 202: + description: Build created and queued + content: + application/json: + schema: + $ref: "#/components/schemas/Build" + 400: + description: Bad request + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 401: + description: Unauthorized + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 500: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + + /builds/{id}: + get: + summary: Get build details + operationId: getBuild + security: + - bearerAuth: [] + parameters: + - name: id + in: path + required: true + schema: + type: string + description: Build ID + responses: + 200: + description: Build details + content: + application/json: + schema: + $ref: "#/components/schemas/Build" + 404: + description: Build not found + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 500: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + delete: + summary: Cancel build + operationId: cancelBuild + security: + - bearerAuth: [] + parameters: + - name: id + in: path + required: true + schema: + type: string + description: Build ID + responses: + 204: + description: Build cancelled + 404: + description: Build not found + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 409: + description: Build already completed + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 500: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + /builds/{id}/logs: + get: + summary: Stream build logs (SSE) + description: | + Streams build logs as Server-Sent Events. + Returns existing logs, then continues streaming new lines if follow=true. + operationId: getBuildLogs + security: + - bearerAuth: [] + parameters: + - name: id + in: path + required: true + schema: + type: string + description: Build ID + - name: follow + in: query + required: false + schema: + type: boolean + default: false + description: Continue streaming new lines after initial output + responses: + 200: + description: Log stream (SSE) + content: + text/event-stream: + schema: + type: string + 404: + description: Build not found + content: + application/json: + schema: + $ref: "#/components/schemas/Error" + 500: + description: Internal server error + content: + application/json: + schema: + $ref: "#/components/schemas/Error" From bd81a393a7060d3833acafc85a439feb89bc3ef9 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Fri, 19 Dec 2025 15:13:28 -0500 Subject: [PATCH 02/42] fix: complete build system E2E functionality - Start vsock handler when build manager starts for builder VM communication - Create config volume with build.json mounted at /config in builder VMs - Mount source volume as read-write for generated Dockerfile writes - Fix builder image Dockerfile: copy buildkit-runc as /usr/bin/runc - Mount cgroups (v2 with v1 fallback) in microVM init script for runc - Configure insecure registry flag in builder agent for HTTP registry push - Add auth bypass for internal VM network (10.102.x.x) registry pushes - Update README with comprehensive E2E testing guide and troubleshooting --- Makefile | 16 ++ cmd/api/main.go | 6 + lib/builds/README.md | 236 ++++++++++++++++++++++--- lib/builds/builder_agent/main.go | 23 +-- lib/builds/images/nodejs20/Dockerfile | 26 ++- lib/builds/images/python312/Dockerfile | 12 +- lib/builds/manager.go | 115 +++++++++++- lib/middleware/oapi_auth.go | 37 +++- lib/system/init_script.go | 17 +- scripts/e2e-build-test.sh | 234 ++++++++++++++++++++++++ 10 files changed, 674 insertions(+), 48 deletions(-) create mode 100755 scripts/e2e-build-test.sh diff --git a/Makefile b/Makefile index 58e67156..0a049ad8 100644 --- a/Makefile +++ b/Makefile @@ -189,6 +189,22 @@ test: ensure-ch-binaries ensure-caddy-binaries lib/system/exec_agent/exec-agent gen-jwt: $(GODOTENV) @$(GODOTENV) -f .env go run ./cmd/gen-jwt -user-id $${USER_ID:-test-user} +# Build the nodejs20 builder image for builds +build-builder-nodejs20: + docker build -t hypeman/builder-nodejs20:latest -f lib/builds/images/nodejs20/Dockerfile . + docker tag hypeman/builder-nodejs20:latest hypeman/builder:latest + +# Build the python312 builder image for builds +build-builder-python312: + docker build -t hypeman/builder-python312:latest -f lib/builds/images/python312/Dockerfile . + +# Build all builder images +build-builders: build-builder-nodejs20 build-builder-python312 + +# Run E2E build system test (requires server running: make dev) +e2e-build-test: + @./scripts/e2e-build-test.sh + # Clean generated files and binaries clean: rm -rf $(BIN_DIR) diff --git a/cmd/api/main.go b/cmd/api/main.go index 48c9e312..ab642eb3 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -328,6 +328,12 @@ func run() error { // Error group for coordinated shutdown grp, gctx := errgroup.WithContext(ctx) + // Start build manager background services (vsock handler for builder VMs) + if err := app.BuildManager.Start(gctx); err != nil { + logger.Error("failed to start build manager", "error", err) + return err + } + // Run the server grp.Go(func() error { logger.Info("starting hypeman API", "port", app.Config.Port) diff --git a/lib/builds/README.md b/lib/builds/README.md index 575ef2a0..add6d907 100644 --- a/lib/builds/README.md +++ b/lib/builds/README.md @@ -7,31 +7,36 @@ The build system provides source-to-image builds inside ephemeral Cloud Hypervis ``` ┌─────────────────────────────────────────────────────────────────┐ │ Hypeman API │ -│ POST /v1/builds → BuildManager → BuildQueue │ +│ POST /builds → BuildManager → BuildQueue │ +│ │ │ +│ Start() → VsockHandler (port 5001) │ └─────────────────────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ Builder MicroVM │ │ ┌─────────────────────────────────────────────────────────────┐│ +│ │ Volumes Mounted: ││ +│ │ - /src (source code, read-write) ││ +│ │ - /config/build.json (build configuration, read-only) ││ +│ ├─────────────────────────────────────────────────────────────┤│ │ │ Builder Agent ││ │ │ ┌─────────────┐ ┌──────────────┐ ┌────────────────────┐ ││ │ │ │ Load Config │→ │ Generate │→ │ Run BuildKit │ ││ -│ │ │ from disk │ │ Dockerfile │ │ (rootless) │ ││ +│ │ │ /config/ │ │ Dockerfile │ │ (buildctl) │ ││ │ │ └─────────────┘ └──────────────┘ └────────────────────┘ ││ │ │ │ ││ │ │ ▼ ││ │ │ Push to Registry ││ -│ │ │ ││ -│ │ ▼ ││ -│ │ Report via vsock ││ +│ │ (HTTP, insecure) ││ │ └─────────────────────────────────────────────────────────────┘│ └─────────────────────────────────────────────────────────────────┘ │ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ OCI Registry │ -│ localhost:8080/builds/{build-id} │ +│ {REGISTRY_URL}/builds/{build-id} │ +│ (default: 10.102.0.1:8083 from VM) │ └─────────────────────────────────────────────────────────────────┘ ``` @@ -81,10 +86,15 @@ builds/ Orchestrates the build lifecycle: 1. Validate request and store source -2. Enqueue build job -3. Create builder VM with source volume attached -4. Wait for result via vsock -5. Update metadata and cleanup +2. Write build config to disk +3. Enqueue build job +4. Create source volume from archive +5. Create config volume with `build.json` +6. Create builder VM with both volumes attached +7. Wait for build completion +8. Update metadata and cleanup + +**Important**: The `Start()` method must be called to start the vsock handler for builder communication. ### Dockerfile Templates (`templates/`) @@ -120,26 +130,32 @@ key, _ := gen.GenerateCacheKey("my-tenant", "nodejs20", lockfileHashes) Guest binary that runs inside builder VMs: 1. Reads config from `/config/build.json` -2. Fetches secrets from host via vsock +2. Fetches secrets from host via vsock (if any) 3. Generates Dockerfile (if not provided) -4. Runs `buildctl-daemonless.sh` with cache flags +4. Runs `buildctl-daemonless.sh` with cache and insecure registry flags 5. Computes provenance (lockfile hashes, source hash) 6. Reports result back via vsock +**Key Details**: +- Config path: `/config/build.json` +- Source path: `/src` +- Uses `registry.insecure=true` for HTTP registries +- Inherits `BUILDKITD_FLAGS` from environment + ## API Endpoints | Method | Path | Description | |--------|------|-------------| -| `POST` | `/v1/builds` | Submit build (multipart form) | -| `GET` | `/v1/builds` | List all builds | -| `GET` | `/v1/builds/{id}` | Get build details | -| `DELETE` | `/v1/builds/{id}` | Cancel build | -| `GET` | `/v1/builds/{id}/logs` | Stream logs (SSE) | +| `POST` | `/builds` | Submit build (multipart form) | +| `GET` | `/builds` | List all builds | +| `GET` | `/builds/{id}` | Get build details | +| `DELETE` | `/builds/{id}` | Cancel build | +| `GET` | `/builds/{id}/logs` | Stream logs (SSE) | ### Submit Build Example ```bash -curl -X POST http://localhost:8080/v1/builds \ +curl -X POST http://localhost:8083/builds \ -H "Authorization: Bearer $TOKEN" \ -F "runtime=nodejs20" \ -F "source=@source.tar.gz" \ @@ -168,6 +184,17 @@ curl -X POST http://localhost:8080/v1/builds \ | `REGISTRY_URL` | `localhost:8080` | Registry for built images | | `BUILD_TIMEOUT` | `600` | Default timeout (seconds) | +### Registry URL Configuration + +The `REGISTRY_URL` must be accessible from inside builder VMs. Since `localhost` in the VM refers to the VM itself, you need to use the host's gateway IP: + +```bash +# In .env +REGISTRY_URL=10.102.0.1:8083 # Gateway IP accessible from VM network +``` + +The middleware allows unauthenticated registry pushes from the VM network (10.102.x.x). + ## Build Status Flow ``` @@ -194,13 +221,77 @@ Builder images are in `images/`: - `nodejs20/Dockerfile` - Node.js 20 + BuildKit + agent - `python312/Dockerfile` - Python 3.12 + BuildKit + agent -Build and push: +### Required Components + +Builder images must include: + +| Component | Source | Purpose | +|-----------|--------|---------| +| `buildctl` | `moby/buildkit:rootless` | BuildKit CLI | +| `buildctl-daemonless.sh` | `moby/buildkit:rootless` | Daemonless wrapper | +| `buildkitd` | `moby/buildkit:rootless` | BuildKit daemon | +| `buildkit-runc` | `moby/buildkit:rootless` | Container runtime (as `/usr/bin/runc`) | +| `builder-agent` | Built from `builder_agent/main.go` | Hypeman agent | +| `fuse-overlayfs` | apk/apt | Overlay filesystem support | + +### Build and Push (OCI Format) + +Builder images must be pushed in OCI format (not Docker v2 manifest): ```bash -cd lib/builds/images/nodejs20 -docker build -t hypeman/builder-nodejs20:latest -f Dockerfile ../../../.. +# Build with OCI output +docker buildx build --platform linux/amd64 \ + -t myregistry/builder-nodejs20:latest \ + -f lib/builds/images/nodejs20/Dockerfile \ + --output type=oci,dest=/tmp/builder.tar \ + . + +# Extract and push with crane +mkdir -p /tmp/oci-builder +tar -xf /tmp/builder.tar -C /tmp/oci-builder +crane push /tmp/oci-builder myregistry/builder-nodejs20:latest +``` + +### Environment Variables + +The builder image should set: + +```dockerfile +# Empty or minimal flags - cgroups are mounted in microVM +ENV BUILDKITD_FLAGS="" +ENV HOME=/home/builder +ENV XDG_RUNTIME_DIR=/home/builder/.local/share ``` +## MicroVM Requirements + +Builder VMs require specific kernel and init script features: + +### Cgroups + +The init script mounts cgroups for BuildKit/runc: + +```bash +# Cgroup v2 (preferred) +mount -t cgroup2 none /sys/fs/cgroup + +# Or cgroup v1 fallback +mount -t tmpfs cgroup /sys/fs/cgroup +for ctrl in cpu cpuacct memory devices freezer blkio pids; do + mkdir -p /sys/fs/cgroup/$ctrl + mount -t cgroup -o $ctrl cgroup /sys/fs/cgroup/$ctrl +done +``` + +### Volume Mounts + +Two volumes are attached to builder VMs: + +1. **Source volume** (`/src`, read-write): Contains extracted source tarball +2. **Config volume** (`/config`, read-only): Contains `build.json` + +The source is mounted read-write so the generated Dockerfile can be written. + ## Provenance Each build records provenance for reproducibility: @@ -220,6 +311,8 @@ Each build records provenance for reproducibility: ## Testing +### Unit Tests + ```bash # Run unit tests go test ./lib/builds/... -v @@ -230,3 +323,104 @@ go test ./lib/builds/cache_test.go ./lib/builds/cache.go ./lib/builds/types.go . go test ./lib/builds/templates/... -v ``` +### E2E Testing + +1. **Start the server**: + ```bash + make dev + ``` + +2. **Ensure builder image is available**: + ```bash + TOKEN=$(make gen-jwt | tail -1) + curl -X POST http://localhost:8083/images \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d '{"name": "hirokernel/builder-nodejs20:latest"}' + ``` + +3. **Create test source**: + ```bash + mkdir -p /tmp/test-app + echo '{"name": "test", "version": "1.0.0", "dependencies": {}}' > /tmp/test-app/package.json + echo '{"lockfileVersion": 3, "packages": {}}' > /tmp/test-app/package-lock.json + echo 'console.log("Hello!");' > /tmp/test-app/index.js + tar -czf /tmp/source.tar.gz -C /tmp/test-app . + ``` + +4. **Submit build**: + ```bash + curl -X POST http://localhost:8083/builds \ + -H "Authorization: Bearer $TOKEN" \ + -F "runtime=nodejs20" \ + -F "source=@/tmp/source.tar.gz" + ``` + +5. **Poll for completion**: + ```bash + BUILD_ID="" + curl http://localhost:8083/builds/$BUILD_ID \ + -H "Authorization: Bearer $TOKEN" + ``` + +6. **Run the built image**: + ```bash + curl -X POST http://localhost:8083/instances \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "test-app", + "image": "builds/'$BUILD_ID':latest", + "size": "1GB", + "vcpus": 1 + }' + ``` + +## Troubleshooting + +### Common Issues + +| Error | Cause | Solution | +|-------|-------|----------| +| `image not found` | Builder image not in OCI format | Push with `crane` after `docker buildx --output type=oci` | +| `no cgroup mount found` | Cgroups not mounted in VM | Update init script to mount cgroups | +| `http: server gave HTTP response to HTTPS client` | BuildKit using HTTPS for HTTP registry | Add `registry.insecure=true` to output flags | +| `connection refused` to localhost:8080 | Registry URL not accessible from VM | Use gateway IP (10.102.0.1) instead of localhost | +| `authorization header required` | Registry auth blocking VM push | Ensure auth bypass for 10.102.x.x IPs | +| `No space left on device` | Instance memory too small for image | Use at least 1GB RAM for Node.js images | +| `can't enable NoProcessSandbox without Rootless` | Wrong BUILDKITD_FLAGS | Use empty flags or remove the flag | + +### Debug Builder VM + +Check logs of the builder instance: + +```bash +# List instances +curl http://localhost:8083/instances -H "Authorization: Bearer $TOKEN" | jq + +# Get builder instance logs +INSTANCE_ID="" +curl http://localhost:8083/instances/$INSTANCE_ID/logs \ + -H "Authorization: Bearer $TOKEN" +``` + +### Verify Build Config + +Check the config volume contents: + +```bash +cat $DATA_DIR/builds/$BUILD_ID/config.json +``` + +Expected format: +```json +{ + "job_id": "abc123", + "runtime": "nodejs20", + "registry_url": "10.102.0.1:8083", + "cache_scope": "my-tenant", + "source_path": "/src", + "timeout_seconds": 300, + "network_mode": "egress" +} +``` diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index 19df99c2..d693e118 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -23,9 +23,9 @@ import ( ) const ( - configPath = "/config/build.json" - vsockPort = 5001 // Build agent port (different from exec agent) - hostCID = 2 // VMADDR_CID_HOST + configPath = "/config/build.json" + vsockPort = 5001 // Build agent port (different from exec agent) + hostCID = 2 // VMADDR_CID_HOST ) // BuildConfig matches the BuildConfig type from lib/builds/types.go @@ -71,9 +71,9 @@ type BuildProvenance struct { // VsockMessage is the envelope for vsock communication type VsockMessage struct { - Type string `json:"type"` - Result *BuildResult `json:"result,omitempty"` - Log string `json:"log,omitempty"` + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` } func main() { @@ -273,20 +273,21 @@ func runBuild(ctx context.Context, config *BuildConfig, logWriter io.Writer) (st outputRef := fmt.Sprintf("%s/builds/%s", config.RegistryURL, config.JobID) // Build arguments + // Use registry.insecure=true for internal HTTP registries args := []string{ "build", "--frontend", "dockerfile.v0", "--local", "context=" + config.SourcePath, "--local", "dockerfile=" + config.SourcePath, - "--output", fmt.Sprintf("type=image,name=%s,push=true", outputRef), + "--output", fmt.Sprintf("type=image,name=%s,push=true,registry.insecure=true", outputRef), "--metadata-file", "/tmp/build-metadata.json", } // Add cache if scope is set if config.CacheScope != "" { cacheRef := fmt.Sprintf("%s/cache/%s", config.RegistryURL, config.CacheScope) - args = append(args, "--import-cache", fmt.Sprintf("type=registry,ref=%s", cacheRef)) - args = append(args, "--export-cache", fmt.Sprintf("type=registry,ref=%s,mode=max", cacheRef)) + args = append(args, "--import-cache", fmt.Sprintf("type=registry,ref=%s,registry.insecure=true", cacheRef)) + args = append(args, "--export-cache", fmt.Sprintf("type=registry,ref=%s,mode=max,registry.insecure=true", cacheRef)) } // Add secret mounts @@ -306,7 +307,8 @@ func runBuild(ctx context.Context, config *BuildConfig, logWriter io.Writer) (st cmd := exec.CommandContext(ctx, "buildctl-daemonless.sh", args...) cmd.Stdout = io.MultiWriter(logWriter, &buildLogs) cmd.Stderr = io.MultiWriter(logWriter, &buildLogs) - cmd.Env = append(os.Environ(), "BUILDKITD_FLAGS=--oci-worker-no-process-sandbox") + // Use BUILDKITD_FLAGS from environment (set in Dockerfile) or empty for default + cmd.Env = os.Environ() if err := cmd.Run(); err != nil { return "", buildLogs.String(), fmt.Errorf("buildctl failed: %w", err) @@ -494,4 +496,3 @@ func sendResult(result BuildResult) { func dialVsock() (net.Conn, error) { return vsock.Dial(hostCID, vsockPort, nil) } - diff --git a/lib/builds/images/nodejs20/Dockerfile b/lib/builds/images/nodejs20/Dockerfile index d5665145..aad38d0e 100644 --- a/lib/builds/images/nodejs20/Dockerfile +++ b/lib/builds/images/nodejs20/Dockerfile @@ -7,16 +7,25 @@ FROM moby/buildkit:rootless AS buildkit FROM golang:1.25-alpine AS agent-builder WORKDIR /app -COPY lib/builds/builder_agent/ ./ -RUN go build -ldflags="-s -w" -o /builder-agent . + +# Copy go.mod and go.sum first for better layer caching +COPY go.mod go.sum ./ +RUN go mod download + +# Copy only the builder_agent source +COPY lib/builds/builder_agent/ ./lib/builds/builder_agent/ + +# Build the agent +RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /builder-agent ./lib/builds/builder_agent # Final builder image FROM node:20-alpine -# Copy BuildKit from official image +# Copy BuildKit and runc from official image COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd +COPY --from=buildkit /usr/bin/buildkit-runc /usr/bin/runc # Copy builder agent COPY --from=agent-builder /builder-agent /usr/bin/builder-agent @@ -32,8 +41,10 @@ RUN apk add --no-cache \ shadow \ fuse-overlayfs -# Create unprivileged user for rootless BuildKit -RUN adduser -D -u 1000 builder && \ +# Use existing node user (uid 1000) for rootless BuildKit +# Rename node -> builder for clarity and setup buildkit directories +RUN deluser --remove-home node 2>/dev/null || true && \ + adduser -D -u 1000 builder && \ mkdir -p /home/builder/.local/share/buildkit && \ chown -R builder:builder /home/builder @@ -48,8 +59,9 @@ RUN corepack enable USER builder WORKDIR /src -# Set environment for rootless buildkit -ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" +# Set environment for buildkit in microVM +# Empty flags - use default buildkit behavior with cgroups +ENV BUILDKITD_FLAGS="" ENV HOME=/home/builder ENV XDG_RUNTIME_DIR=/home/builder/.local/share diff --git a/lib/builds/images/python312/Dockerfile b/lib/builds/images/python312/Dockerfile index bf6266eb..8e3a9a31 100644 --- a/lib/builds/images/python312/Dockerfile +++ b/lib/builds/images/python312/Dockerfile @@ -7,8 +7,16 @@ FROM moby/buildkit:rootless AS buildkit FROM golang:1.25-alpine AS agent-builder WORKDIR /app -COPY lib/builds/builder_agent/ ./ -RUN go build -ldflags="-s -w" -o /builder-agent . + +# Copy go.mod and go.sum first for better layer caching +COPY go.mod go.sum ./ +RUN go mod download + +# Copy only the builder_agent source +COPY lib/builds/builder_agent/ ./lib/builds/builder_agent/ + +# Build the agent +RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /builder-agent ./lib/builds/builder_agent # Final builder image FROM python:3.12-slim diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 0f54539c..ac49b7ff 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -2,13 +2,16 @@ package builds import ( "context" + "encoding/json" "fmt" "log/slog" "os" + "path/filepath" "sync" "time" "github.com/nrednav/cuid2" + "github.com/onkernel/hypeman/lib/images" "github.com/onkernel/hypeman/lib/instances" "github.com/onkernel/hypeman/lib/paths" "github.com/onkernel/hypeman/lib/volumes" @@ -17,6 +20,10 @@ import ( // Manager interface for the build system type Manager interface { + // Start starts the build manager's background services (vsock handler, etc.) + // This should be called once when the API server starts. + Start(ctx context.Context) error + // CreateBuild starts a new build job CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) @@ -58,7 +65,7 @@ func DefaultConfig() Config { MaxConcurrentBuilds: 2, BuilderImage: "hypeman/builder:latest", RegistryURL: "localhost:8080", - DefaultTimeout: 600, // 10 minutes + DefaultTimeout: 600, // 10 minutes } } @@ -115,6 +122,21 @@ func NewManager( return m, nil } +// Start starts the build manager's background services +func (m *manager) Start(ctx context.Context) error { + // Start the vsock handler in a goroutine + go func() { + if err := m.vsockHandler.ListenAndServe(ctx); err != nil { + if ctx.Err() == nil { + m.logger.Error("vsock handler error", "error", err) + } + } + }() + + m.logger.Info("build manager started", "vsock_port", BuildAgentVsockPort) + return nil +} + // CreateBuild starts a new build job func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) { m.logger.Info("creating build", "runtime", req.Runtime) @@ -280,6 +302,37 @@ func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRe } defer m.volumeManager.DeleteVolume(ctx, sourceVolID) + // Create config volume with build.json for the builder agent + configVolID := fmt.Sprintf("build-config-%s", id) + configVolPath, err := m.createBuildConfigVolume(id, configVolID) + if err != nil { + return nil, fmt.Errorf("create config volume: %w", err) + } + defer os.Remove(configVolPath) // Clean up the config disk file + + // Register the config volume with the volume manager + _, err = m.volumeManager.CreateVolume(ctx, volumes.CreateVolumeRequest{ + Id: &configVolID, + Name: configVolID, + SizeGb: 1, + }) + if err != nil { + // If volume creation fails, try to use the disk file directly + // by copying it to the expected location + volPath := m.paths.VolumeData(configVolID) + if copyErr := copyFile(configVolPath, volPath); copyErr != nil { + return nil, fmt.Errorf("setup config volume: %w", err) + } + } else { + // Copy our config disk over the empty volume + volPath := m.paths.VolumeData(configVolID) + if err := copyFile(configVolPath, volPath); err != nil { + m.volumeManager.DeleteVolume(ctx, configVolID) + return nil, fmt.Errorf("write config to volume: %w", err) + } + } + defer m.volumeManager.DeleteVolume(ctx, configVolID) + // Create builder instance builderName := fmt.Sprintf("builder-%s", id) networkEnabled := policy.NetworkMode == "egress" @@ -294,6 +347,11 @@ func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRe { VolumeID: sourceVolID, MountPath: "/src", + Readonly: false, // Builder needs to write generated Dockerfile + }, + { + VolumeID: configVolID, + MountPath: "/config", Readonly: true, }, }, @@ -527,3 +585,58 @@ func readFile(path string) ([]byte, error) { return os.ReadFile(path) } +// createBuildConfigVolume creates an ext4 disk containing the build.json config file +// Returns the path to the disk file +func (m *manager) createBuildConfigVolume(buildID, volID string) (string, error) { + // Read the build config + configPath := m.paths.BuildConfig(buildID) + configData, err := os.ReadFile(configPath) + if err != nil { + return "", fmt.Errorf("read build config: %w", err) + } + + // Create temp directory with config file + tmpDir, err := os.MkdirTemp("", "hypeman-build-config-*") + if err != nil { + return "", fmt.Errorf("create temp dir: %w", err) + } + defer os.RemoveAll(tmpDir) + + // Write build.json to temp directory + buildJSONPath := filepath.Join(tmpDir, "build.json") + if err := os.WriteFile(buildJSONPath, configData, 0644); err != nil { + return "", fmt.Errorf("write build.json: %w", err) + } + + // Also write a metadata file for debugging + metadata := map[string]interface{}{ + "build_id": buildID, + "created_at": time.Now().Format(time.RFC3339), + } + metadataData, _ := json.MarshalIndent(metadata, "", " ") + metadataPath := filepath.Join(tmpDir, "metadata.json") + os.WriteFile(metadataPath, metadataData, 0644) + + // Create ext4 disk from the directory + diskPath := filepath.Join(os.TempDir(), fmt.Sprintf("build-config-%s.ext4", buildID)) + _, err = images.ExportRootfs(tmpDir, diskPath, images.FormatExt4) + if err != nil { + return "", fmt.Errorf("create config disk: %w", err) + } + + return diskPath, nil +} + +// copyFile copies a file from src to dst +func copyFile(src, dst string) error { + // Ensure parent directory exists + if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { + return err + } + + data, err := os.ReadFile(src) + if err != nil { + return err + } + return os.WriteFile(dst, data, 0644) +} diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 8a39b2e9..496b8217 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -73,7 +73,7 @@ func OapiAuthenticationFunc(jwtSecret string) openapi3filter.AuthenticationFunc // Update the context with user ID newCtx := context.WithValue(ctx, userIDKey, userID) - + // Update the request with the new context *input.RequestValidationInput.Request = *input.RequestValidationInput.Request.WithContext(newCtx) @@ -86,10 +86,10 @@ func OapiAuthenticationFunc(jwtSecret string) openapi3filter.AuthenticationFunc func OapiErrorHandler(w http.ResponseWriter, message string, statusCode int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) - + // Return a simple JSON error response matching our Error schema - fmt.Fprintf(w, `{"code":"%s","message":"%s"}`, - http.StatusText(statusCode), + fmt.Fprintf(w, `{"code":"%s","message":"%s"}`, + http.StatusText(statusCode), message) } @@ -116,12 +116,40 @@ func GetUserIDFromContext(ctx context.Context) string { return "" } +// isInternalVMRequest checks if the request is from an internal VM network (10.102.x.x) +// This is used to allow builder VMs to push images without authentication +func isInternalVMRequest(r *http.Request) bool { + // Get the real client IP (RealIP middleware sets X-Real-IP) + ip := r.Header.Get("X-Real-IP") + if ip == "" { + // Fall back to RemoteAddr + ip = r.RemoteAddr + // Remove port if present + if idx := strings.LastIndex(ip, ":"); idx != -1 { + ip = ip[:idx] + } + } + + // Check if it's from the VM network (10.102.x.x) + return strings.HasPrefix(ip, "10.102.") +} + // JwtAuth creates a chi middleware that validates JWT bearer tokens func JwtAuth(jwtSecret string) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log := logger.FromContext(r.Context()) + // Allow internal VM network (10.102.x.x) to bypass auth for registry pushes + // This enables builder VMs to push images without authentication + if isInternalVMRequest(r) { + log.DebugContext(r.Context(), "allowing internal VM request without auth", "remote_addr", r.RemoteAddr) + // Set a system user ID for internal requests + ctx := context.WithValue(r.Context(), userIDKey, "internal-builder") + next.ServeHTTP(w, r.WithContext(ctx)) + return + } + // Extract token from Authorization header authHeader := r.Header.Get("Authorization") if authHeader == "" { @@ -174,4 +202,3 @@ func JwtAuth(jwtSecret string) func(http.Handler) http.Handler { }) } } - diff --git a/lib/system/init_script.go b/lib/system/init_script.go index ebe9f8b4..0a305e2f 100644 --- a/lib/system/init_script.go +++ b/lib/system/init_script.go @@ -33,7 +33,18 @@ mkdir -p /dev/pts /dev/shm mount -t devpts devpts /dev/pts chmod 1777 /dev/shm -echo "overlay-init: mounted proc/sys/dev" > /dev/kmsg +# Mount cgroup v2 (unified hierarchy) - needed for container runtimes like runc/buildkit +mkdir -p /sys/fs/cgroup +mount -t cgroup2 none /sys/fs/cgroup 2>/dev/null || { + # Fallback: mount cgroup v1 controllers if v2 not supported + mount -t tmpfs cgroup /sys/fs/cgroup + for ctrl in cpu cpuacct memory devices freezer blkio pids; do + mkdir -p /sys/fs/cgroup/$ctrl + mount -t cgroup -o $ctrl cgroup /sys/fs/cgroup/$ctrl 2>/dev/null || true + done +} + +echo "overlay-init: mounted proc/sys/dev/cgroup" > /dev/kmsg # Redirect all output to serial console exec >/dev/ttyS0 2>&1 @@ -226,6 +237,10 @@ mount --bind /sys /overlay/newroot/sys mount --bind /dev /overlay/newroot/dev mount --bind /dev/pts /overlay/newroot/dev/pts +# Ensure cgroups are available in the container (needed for runc/buildkit) +mkdir -p /overlay/newroot/sys/fs/cgroup +mount --bind /sys/fs/cgroup /overlay/newroot/sys/fs/cgroup 2>/dev/null || true + echo "overlay-init: bound mounts to new root" # Set up /dev symlinks for process substitution inside the container diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh new file mode 100755 index 00000000..bbd0bc01 --- /dev/null +++ b/scripts/e2e-build-test.sh @@ -0,0 +1,234 @@ +#!/bin/bash +# E2E Build System Test +# Usage: ./scripts/e2e-build-test.sh +# +# Prerequisites: +# - API server running (make dev or sudo ./bin/hypeman) +# - Builder images built (docker build -t hypeman/builder:latest ...) +# - .env file configured + +set -e + +# Configuration +API_URL="${API_URL:-http://localhost:8080}" +TIMEOUT_POLLS=60 +POLL_INTERVAL=5 + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +log() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; } + +# Check prerequisites +check_prerequisites() { + log "Checking prerequisites..." + + # Check if API is reachable + if ! curl -s "$API_URL/health" | grep -q "ok"; then + error "API server not reachable at $API_URL" + error "Start the server with: make dev" + exit 1 + fi + log "✓ API server is running" + + # Check if builder image exists + if ! docker images hypeman/builder:latest --format "{{.Repository}}" | grep -q hypeman; then + error "Builder image not found" + error "Build it with: docker build -t hypeman/builder:latest -f lib/builds/images/nodejs20/Dockerfile ." + exit 1 + fi + log "✓ Builder image available" +} + +# Generate JWT token +generate_token() { + cd "$(dirname "$0")/.." + + # Try using make gen-jwt + if command -v make &> /dev/null && [ -f Makefile ]; then + TOKEN=$(make gen-jwt 2>/dev/null | tail -1) + if [ -n "$TOKEN" ] && [ "$TOKEN" != "make:" ]; then + echo "$TOKEN" + return + fi + fi + + # Fallback: run directly + if [ -f ./bin/godotenv ]; then + TOKEN=$(./bin/godotenv -f .env go run ./cmd/gen-jwt -user-id e2e-test 2>/dev/null | tail -1) + echo "$TOKEN" + return + fi + + echo "" +} + +# Create test source +create_test_source() { + TEST_DIR=$(mktemp -d) + + cat > "$TEST_DIR/package.json" << 'EOF' +{ + "name": "e2e-test-app", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "start": "node index.js" + }, + "dependencies": {} +} +EOF + + cat > "$TEST_DIR/index.js" << 'EOF' +console.log("E2E Build Test - Success!"); +console.log("Built at:", new Date().toISOString()); +EOF + + # Create tarball + TARBALL=$(mktemp --suffix=.tar.gz) + tar -czvf "$TARBALL" -C "$TEST_DIR" . > /dev/null 2>&1 + + rm -rf "$TEST_DIR" + echo "$TARBALL" +} + +# Submit build +submit_build() { + local token="$1" + local source="$2" + + log "Submitting build..." + + RESPONSE=$(curl -s -X POST "$API_URL/builds" \ + -H "Authorization: Bearer $token" \ + -F "runtime=nodejs20" \ + -F "source=@$source" \ + -F "cache_scope=e2e-test" \ + -F "timeout_seconds=300") + + BUILD_ID=$(echo "$RESPONSE" | jq -r '.id // empty') + + if [ -z "$BUILD_ID" ]; then + error "Failed to submit build" + echo "$RESPONSE" | jq . + exit 1 + fi + + log "Build submitted: $BUILD_ID" + echo "$BUILD_ID" +} + +# Poll for build completion +wait_for_build() { + local token="$1" + local build_id="$2" + + log "Waiting for build to complete..." + + for i in $(seq 1 $TIMEOUT_POLLS); do + RESPONSE=$(curl -s "$API_URL/builds/$build_id" \ + -H "Authorization: Bearer $token") + + STATUS=$(echo "$RESPONSE" | jq -r '.status') + + case "$STATUS" in + "ready") + log "✅ Build succeeded!" + echo "$RESPONSE" | jq . + return 0 + ;; + "failed") + error "❌ Build failed!" + echo "$RESPONSE" | jq . + return 1 + ;; + "cancelled") + warn "Build was cancelled" + return 1 + ;; + "queued"|"building"|"pushing") + echo -ne "\r Status: $STATUS (poll $i/$TIMEOUT_POLLS)..." + ;; + *) + warn "Unknown status: $STATUS" + ;; + esac + + sleep $POLL_INTERVAL + done + + error "Build timed out after $((TIMEOUT_POLLS * POLL_INTERVAL)) seconds" + return 1 +} + +# Get build logs +get_logs() { + local token="$1" + local build_id="$2" + + log "Fetching build logs..." + curl -s "$API_URL/builds/$build_id/logs" \ + -H "Authorization: Bearer $token" +} + +# Main +main() { + log "=== E2E Build System Test ===" + echo "" + + # Check prerequisites + check_prerequisites + echo "" + + # Generate token + log "Generating JWT token..." + TOKEN=$(generate_token) + if [ -z "$TOKEN" ]; then + error "Failed to generate token" + error "Run: make gen-jwt" + exit 1 + fi + log "✓ Token generated" + echo "" + + # Create test source + log "Creating test Node.js source..." + SOURCE=$(create_test_source) + log "✓ Test source created: $SOURCE" + echo "" + + # Submit build + BUILD_ID=$(submit_build "$TOKEN" "$SOURCE") + echo "" + + # Wait for completion + if wait_for_build "$TOKEN" "$BUILD_ID"; then + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" + log "=== E2E Test PASSED ===" + + # Cleanup + rm -f "$SOURCE" + exit 0 + else + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" + error "=== E2E Test FAILED ===" + + # Cleanup + rm -f "$SOURCE" + exit 1 + fi +} + +main "$@" + From 55d96abcc74cd62d14b425def1e85451378b2aae Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Fri, 19 Dec 2025 15:19:46 -0500 Subject: [PATCH 03/42] docs: add builder images guide Comprehensive documentation for creating, building, and testing builder images including required components, OCI format build process, and troubleshooting common issues. --- lib/builds/images/README.md | 226 ++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 lib/builds/images/README.md diff --git a/lib/builds/images/README.md b/lib/builds/images/README.md new file mode 100644 index 00000000..dad65ebf --- /dev/null +++ b/lib/builds/images/README.md @@ -0,0 +1,226 @@ +# Builder Images + +Builder images run inside Hypeman microVMs to execute source-to-image builds using BuildKit. + +## Available Images + +| Image | Runtime | Use Case | +|-------|---------|----------| +| `nodejs20/` | Node.js 20.x | npm, yarn, pnpm projects | +| `python312/` | Python 3.12 | pip, poetry, pipenv projects | +| `base/` | None | Base BuildKit image (for custom runtimes) | + +## Creating a Builder Image + +### Step 1: Create Dockerfile + +Create a new directory under `images/` with a Dockerfile: + +```dockerfile +# Use BuildKit rootless as base for build tools +FROM moby/buildkit:rootless AS buildkit + +# Use your runtime base image +FROM node:20-alpine + +# Install required dependencies +RUN apk add --no-cache \ + fuse-overlayfs \ + shadow \ + newuidmap \ + ca-certificates + +# Create non-root builder user +RUN adduser -D -u 1000 builder && \ + mkdir -p /home/builder/.local/share/buildkit && \ + chown -R builder:builder /home/builder + +# Copy BuildKit binaries (these specific paths are required) +COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl +COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh +COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd +COPY --from=buildkit /usr/bin/buildkit-runc /usr/bin/runc + +# Copy the builder agent (built during image build) +COPY builder-agent /usr/bin/builder-agent + +# Set environment variables +ENV HOME=/home/builder +ENV XDG_RUNTIME_DIR=/home/builder/.local/share +ENV BUILDKITD_FLAGS="" + +# Run as builder user +USER builder +WORKDIR /home/builder + +# The agent is the entrypoint +ENTRYPOINT ["/usr/bin/builder-agent"] +``` + +### Step 2: Required Components + +Every builder image **must** include: + +| Component | Path | Source | Purpose | +|-----------|------|--------|---------| +| `buildctl` | `/usr/bin/buildctl` | `moby/buildkit:rootless` | BuildKit CLI | +| `buildctl-daemonless.sh` | `/usr/bin/buildctl-daemonless.sh` | `moby/buildkit:rootless` | Runs buildkitd + buildctl together | +| `buildkitd` | `/usr/bin/buildkitd` | `moby/buildkit:rootless` | BuildKit daemon | +| `runc` | `/usr/bin/runc` | `moby/buildkit:rootless` (as `buildkit-runc`) | Container runtime | +| `builder-agent` | `/usr/bin/builder-agent` | Built from Go source | Hypeman orchestration agent | +| `fuse-overlayfs` | System package | apk/apt | Overlay filesystem for rootless builds | + +### Step 3: Build the Agent + +The builder agent must be compiled for the target architecture: + +```bash +# From repository root +cd lib/builds/builder_agent +GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o builder-agent . +``` + +### Step 4: Build the Image (OCI Format) + +**Important**: Hypeman uses `umoci` to extract images, which requires OCI format (not Docker v2 manifest). + +```bash +# From repository root + +# Build agent first +cd lib/builds/builder_agent +GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o builder-agent . +cd ../../.. + +# Build image with OCI output +docker buildx build --platform linux/amd64 \ + -t yourregistry/builder-nodejs20:latest \ + -f lib/builds/images/nodejs20/Dockerfile \ + --output type=oci,dest=/tmp/builder.tar \ + . +``` + +### Step 5: Push to Registry + +Use `crane` (from go-containerregistry) to push in OCI format: + +```bash +# Extract the OCI tarball +mkdir -p /tmp/oci-builder +tar -xf /tmp/builder.tar -C /tmp/oci-builder + +# Push to registry +crane push /tmp/oci-builder yourregistry/builder-nodejs20:latest +``` + +### Step 6: Configure Hypeman + +Set the builder image in your `.env`: + +```bash +BUILDER_IMAGE=yourregistry/builder-nodejs20:latest +``` + +## Testing Your Builder Image + +### 1. Pull the Image into Hypeman + +```bash +TOKEN=$(make gen-jwt | tail -1) +curl -X POST http://localhost:8083/images \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d '{"name": "yourregistry/builder-nodejs20:latest"}' +``` + +### 2. Submit a Test Build + +```bash +# Create minimal test source +mkdir -p /tmp/test-app +echo '{"name": "test", "version": "1.0.0"}' > /tmp/test-app/package.json +echo '{"lockfileVersion": 3, "packages": {}}' > /tmp/test-app/package-lock.json +echo 'console.log("Hello from test build!");' > /tmp/test-app/index.js +tar -czf /tmp/source.tar.gz -C /tmp/test-app . + +# Submit build +curl -X POST http://localhost:8083/builds \ + -H "Authorization: Bearer $TOKEN" \ + -F "runtime=nodejs20" \ + -F "source=@/tmp/source.tar.gz" +``` + +### 3. Check Build Status + +```bash +BUILD_ID="" +curl http://localhost:8083/builds/$BUILD_ID \ + -H "Authorization: Bearer $TOKEN" | jq +``` + +### 4. Debug Failed Builds + +If the build fails, check the builder instance logs: + +```bash +# Find the builder instance +curl http://localhost:8083/instances \ + -H "Authorization: Bearer $TOKEN" | jq '.[] | select(.name | startswith("builder-"))' + +# Get its logs +INSTANCE_ID="" +curl "http://localhost:8083/instances/$INSTANCE_ID/logs" \ + -H "Authorization: Bearer $TOKEN" +``` + +## Environment Variables + +Builder images should configure these environment variables: + +| Variable | Value | Purpose | +|----------|-------|---------| +| `HOME` | `/home/builder` | User home directory | +| `XDG_RUNTIME_DIR` | `/home/builder/.local/share` | Runtime directory for BuildKit | +| `BUILDKITD_FLAGS` | `""` (empty) | BuildKit daemon flags (cgroups are mounted in VM) | + +## MicroVM Runtime Environment + +When the builder image runs inside a Hypeman microVM: + +1. **Volumes mounted**: + - `/src` - Source code (read-write) + - `/config/build.json` - Build configuration (read-only) + +2. **Cgroups**: Mounted by init script at `/sys/fs/cgroup` (v2 preferred, v1 fallback) + +3. **Network**: Access to host registry via gateway IP `10.102.0.1` + +4. **Registry**: HTTP (insecure) - agent adds `registry.insecure=true` flag + +## Troubleshooting + +| Issue | Cause | Solution | +|-------|-------|----------| +| `runc: not found` | Missing or wrong path | Copy `buildkit-runc` to `/usr/bin/runc` | +| `no cgroup mount found` | Cgroups not available | Ensure VM init script mounts cgroups | +| `fuse-overlayfs: not found` | Missing package | Add `fuse-overlayfs` to image | +| `permission denied` on buildkit | Wrong user/permissions | Run as non-root user with proper home dir | +| `can't enable NoProcessSandbox without Rootless` | Wrong BUILDKITD_FLAGS | Set `BUILDKITD_FLAGS=""` | + +## Adding a New Runtime + +To add support for a new runtime (e.g., Ruby, Go): + +1. Create `images/ruby32/Dockerfile` based on the template above +2. Add Dockerfile template in `templates/templates.go`: + ```go + var ruby32Template = `FROM {{.BaseImage}} + COPY . /app + WORKDIR /app + RUN bundle install + CMD ["ruby", "app.rb"] + ` + ``` +3. Register the generator in `templates/templates.go` +4. Build and push the builder image +5. Test with a sample project From 8ff96786e166276f0bb7f4fc0947e15084223d10 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Fri, 19 Dec 2025 17:43:41 -0500 Subject: [PATCH 04/42] docs: add build system roadmap and security model Includes planned phases for cache optimization, security hardening, additional runtimes, and observability. Documents threat model and open design questions. --- lib/builds/PLAN.md | 181 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 lib/builds/PLAN.md diff --git a/lib/builds/PLAN.md b/lib/builds/PLAN.md new file mode 100644 index 00000000..acacc122 --- /dev/null +++ b/lib/builds/PLAN.md @@ -0,0 +1,181 @@ +# Build System Roadmap + +## Current State (v0.1) + +- ✅ Source-to-image builds in isolated microVMs +- ✅ BuildKit-based builds with daemonless execution +- ✅ Tenant-isolated registry caching +- ✅ Node.js 20 and Python 3.12 runtimes +- ✅ Vsock communication for build results +- ✅ Cgroup mounting for container runtime support + +## Planned Improvements + +### Phase 1: Cache Optimization + +**Goal**: Reduce build times by sharing common base layers across tenants. + +#### Multi-tier Cache Strategy + +``` +Import order (first match wins): +1. shared/{runtime}/base ← Pre-warmed with OS + runtime layers (read-only) +2. {tenant}/{runtime}/{hash} ← Tenant-specific dependency layers + +Export to: +→ {tenant}/{runtime}/{hash} ← Only tenant-specific layers +``` + +#### Benefits +- **Fast builds**: Common layers (apt packages, Node.js binary, etc.) are shared +- **Tenant isolation**: Application dependencies remain isolated +- **No cross-tenant poisoning**: Tenants can only write to their own scope +- **Controlled shared cache**: Only operators can update the shared base cache + +#### Implementation Tasks +- [ ] Update `cache.go` with `ImportCacheArgs() []string` returning multiple args +- [ ] Update `builder_agent/main.go` to handle multiple `--import-cache` flags +- [ ] Add CLI/API endpoint for pre-warming shared cache +- [ ] Create cron job or webhook to refresh shared cache on base image updates +- [ ] Document cache warming process in README + +### Phase 2: Security Hardening + +#### Secret Management +- [ ] Implement vsock-based secret injection (secrets never written to disk) +- [ ] Add secret scoping per build (which secrets a build can access) +- [ ] Audit logging for secret access during builds +- [ ] Integration with external secret managers (Vault, AWS Secrets Manager) + +#### Network Policy +- [ ] Implement domain allowlist for `egress` mode +- [ ] Add `isolated` mode (no network access during build phase) +- [ ] Rate limiting on registry pushes to prevent abuse +- [ ] DNS filtering for allowed domains + +#### Build Provenance & Supply Chain Security +- [ ] Sign build provenance with Sigstore/cosign +- [ ] SLSA Level 2 compliance (authenticated build process) +- [ ] SBOM (Software Bill of Materials) generation during builds +- [ ] Vulnerability scanning of built images before push + +### Phase 3: Additional Runtimes + +| Runtime | Package Managers | Priority | +|---------|-----------------|----------| +| Go 1.22+ | go mod | High | +| Ruby 3.3+ | bundler, gem | Medium | +| Rust | cargo | Medium | +| Java 21+ | Maven, Gradle | Medium | +| PHP 8.3+ | composer | Low | +| Custom Dockerfile | N/A | High | + +#### Custom Dockerfile Support +- [ ] Allow users to provide their own Dockerfile +- [ ] Security review: sandbox custom Dockerfiles more strictly +- [ ] Validate Dockerfile doesn't use dangerous instructions +- [ ] Consider read-only base image allowlist + +### Phase 4: Performance & Observability + +#### Metrics (Prometheus) +- [ ] `hypeman_build_duration_seconds` - histogram by runtime, status +- [ ] `hypeman_build_cache_hits_total` - counter for cache hits/misses +- [ ] `hypeman_build_queue_wait_seconds` - time spent in queue +- [ ] `hypeman_build_vm_boot_seconds` - microVM boot time +- [ ] `hypeman_build_push_duration_seconds` - registry push time + +#### Logging Improvements +- [ ] Structured JSON logs from builder agent +- [ ] Log streaming during build (not just after completion) +- [ ] Build log retention policy + +#### Distributed Builds +- [ ] Build worker pool across multiple hosts +- [ ] Load balancing for build queue (consistent hashing by tenant?) +- [ ] Horizontal scaling of build capacity +- [ ] Worker health checks and automatic failover + +## Security Model + +### Threat Model + +| Threat | Mitigation | Status | +|--------|------------|--------| +| Container escape to host | MicroVM isolation (separate kernel) | ✅ Implemented | +| Cross-tenant cache poisoning | Tenant-scoped cache paths | ✅ Implemented | +| Host kernel exploit | Separate kernel per VM | ✅ Implemented | +| Malicious dependency exfiltration | Network isolation (egress control) | 🔄 Partial | +| Secret theft during build | Vsock-only secret injection | 📋 Planned | +| Registry credential theft | Per-build short-lived tokens | 📋 Planned | +| Resource exhaustion (DoS) | VM resource limits | ✅ Implemented | +| Build log information leak | Tenant-scoped log access | ✅ Implemented | + +### Security Boundaries + +``` +┌─────────────────────────────────────────────────────────────┐ +│ Host System │ +│ ┌─────────────────────────────────────────────────────────┐│ +│ │ Hypeman API ││ +│ │ - JWT authentication ││ +│ │ - Tenant isolation at API level ││ +│ └─────────────────────────────────────────────────────────┘│ +│ │ │ +│ ┌───────────────────────────┼───────────────────────────┐ │ +│ │ MicroVM Boundary (Cloud Hypervisor) │ │ +│ │ ┌─────────────────────────────────────────────────┐ │ │ +│ │ │ Builder VM │ │ │ +│ │ │ - Separate kernel │ │ │ +│ │ │ - Ephemeral (destroyed after build) │ │ │ +│ │ │ - Limited network (egress only to registry) │ │ │ +│ │ │ - No access to other tenants' data │ │ │ +│ │ │ ┌─────────────────────────────────────────┐ │ │ │ +│ │ │ │ BuildKit (rootless) │ │ │ │ +│ │ │ │ - User namespace isolation │ │ │ │ +│ │ │ │ - No real root privileges │ │ │ │ +│ │ │ └─────────────────────────────────────────┘ │ │ │ +│ │ └─────────────────────────────────────────────────┘ │ │ +│ └────────────────────────────────────────────────────────┘ │ +└─────────────────────────────────────────────────────────────┘ +``` + +### Not Protected (By Design) + +These are inherent to the build process and cannot be fully mitigated: + +1. **Malicious code execution during package install** - `npm install` and `pip install` execute arbitrary code by design +2. **Supply chain attacks on upstream packages** - Typosquatting, compromised maintainers, etc. +3. **Tenant poisoning their own cache** - A tenant can push malicious layers to their own cache scope +4. **Information leakage via build output** - Malicious deps can encode secrets in build artifacts + +## Open Questions + +1. **Custom Dockerfiles**: Should we support user-provided Dockerfiles? + - Pro: Flexibility for advanced users + - Con: Larger attack surface, harder to secure + - Possible middle ground: Allowlist of base images + +2. **Cache TTL Policy**: How long should tenant caches be retained? + - Options: 7 days, 30 days, size-based eviction, never (until explicit delete) + - Consider: Storage costs vs build speed + +3. **Build Artifact Signing**: Required for all builds or opt-in? + - Required: Better security posture, SLSA compliance + - Opt-in: Less friction for getting started + +4. **Multi-arch Builds**: Worth the complexity? + - Use case: Deploy same image to ARM and x86 + - Complexity: Requires QEMU or cross-compilation support + +5. **Build Concurrency Limits**: Per-tenant or global? + - Per-tenant: Fair sharing, prevents noisy neighbor + - Global: Simpler, but one tenant could starve others + +## References + +- [BuildKit GitHub](https://github.com/moby/buildkit) +- [Rootless Containers](https://rootlesscontaine.rs/) +- [SLSA Framework](https://slsa.dev/) +- [Sigstore](https://www.sigstore.dev/) +- [Cloud Hypervisor](https://github.com/cloud-hypervisor/cloud-hypervisor) From b93e3ba86304f4f5a535b20a4b91878c22354df3 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Mon, 22 Dec 2025 09:53:20 -0500 Subject: [PATCH 05/42] modify plans --- lib/builds/PLAN.md | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/builds/PLAN.md b/lib/builds/PLAN.md index acacc122..8fc622f0 100644 --- a/lib/builds/PLAN.md +++ b/lib/builds/PLAN.md @@ -179,3 +179,4 @@ These are inherent to the build process and cannot be fully mitigated: - [SLSA Framework](https://slsa.dev/) - [Sigstore](https://www.sigstore.dev/) - [Cloud Hypervisor](https://github.com/cloud-hypervisor/cloud-hypervisor) + From a86ea65ffbe82542a4d08953d0122853b2849e75 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Mon, 5 Jan 2026 14:25:06 -0500 Subject: [PATCH 06/42] fix(builds): correct vsock communication pattern for Cloud Hypervisor - Update builder_agent to LISTEN on vsock port 5001 instead of dialing out - Update manager to connect TO builder VM's vsock socket with CH handshake - Simplify vsock_handler to only contain message types Cloud Hypervisor's vsock implementation requires host to dial guest, not the other way around. This matches the pattern used by exec_agent. Build results (digest, provenance, logs) now properly returned via vsock. --- lib/builds/builder_agent/main.go | 238 +++++++++++++++++++------------ lib/builds/manager.go | 159 +++++++++++++++------ lib/builds/vsock_handler.go | 218 +--------------------------- 3 files changed, 267 insertions(+), 348 deletions(-) diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index d693e118..d6e799b5 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -1,9 +1,15 @@ // Package main implements the builder agent that runs inside builder microVMs. // It reads build configuration from the config disk, runs BuildKit to build // the image, and reports results back to the host via vsock. +// +// Communication model: +// - Agent LISTENS on vsock port 5001 +// - Host CONNECTS to the agent via the VM's vsock.sock file +// - This follows the Cloud Hypervisor vsock pattern (host initiates) package main import ( + "bufio" "bytes" "context" "crypto/sha256" @@ -17,6 +23,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync" "time" "github.com/mdlayher/vsock" @@ -25,7 +32,6 @@ import ( const ( configPath = "/config/build.json" vsockPort = 5001 // Build agent port (different from exec agent) - hostCID = 2 // VMADDR_CID_HOST ) // BuildConfig matches the BuildConfig type from lib/builds/types.go @@ -71,29 +77,140 @@ type BuildProvenance struct { // VsockMessage is the envelope for vsock communication type VsockMessage struct { - Type string `json:"type"` - Result *BuildResult `json:"result,omitempty"` - Log string `json:"log,omitempty"` + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` + Secrets map[string]string `json:"secrets,omitempty"` // For secrets response from host } +// Global state for the result to send when host connects +var ( + buildResult *BuildResult + buildResultLock sync.Mutex + buildDone = make(chan struct{}) +) + func main() { + log.Println("=== Builder Agent Starting ===") + + // Start vsock listener first (so host can connect as soon as VM is ready) + listener, err := startVsockListener() + if err != nil { + log.Fatalf("Failed to start vsock listener: %v", err) + } + defer listener.Close() + log.Printf("Listening on vsock port %d", vsockPort) + + // Run the build in background + go runBuildProcess() + + // Accept connections from host + for { + conn, err := listener.Accept() + if err != nil { + log.Printf("Accept error: %v", err) + continue + } + go handleHostConnection(conn) + } +} + +// startVsockListener starts listening on vsock with retries (like exec-agent) +func startVsockListener() (*vsock.Listener, error) { + var l *vsock.Listener + var err error + + for i := 0; i < 10; i++ { + l, err = vsock.Listen(vsockPort, nil) + if err == nil { + return l, nil + } + log.Printf("vsock listen attempt %d/10 failed: %v (retrying in 1s)", i+1, err) + time.Sleep(1 * time.Second) + } + + return nil, fmt.Errorf("failed to listen on vsock port %d after retries: %v", vsockPort, err) +} + +// handleHostConnection handles a connection from the host +func handleHostConnection(conn net.Conn) { + defer conn.Close() + + reader := bufio.NewReader(conn) + encoder := json.NewEncoder(conn) + decoder := json.NewDecoder(reader) + + for { + var msg VsockMessage + if err := decoder.Decode(&msg); err != nil { + if err == io.EOF { + return + } + log.Printf("Decode error: %v", err) + return + } + + switch msg.Type { + case "get_result": + // Host is asking for the build result + // Wait for build to complete if not done yet + <-buildDone + + buildResultLock.Lock() + result := buildResult + buildResultLock.Unlock() + + response := VsockMessage{ + Type: "build_result", + Result: result, + } + if err := encoder.Encode(response); err != nil { + log.Printf("Failed to send result: %v", err) + } + return // Close connection after sending result + + case "get_status": + // Host is checking if build is still running + select { + case <-buildDone: + encoder.Encode(VsockMessage{Type: "status", Log: "completed"}) + default: + encoder.Encode(VsockMessage{Type: "status", Log: "building"}) + } + + case "secrets_response": + // Host is sending secrets we requested + // This is handled inline during secret fetching + log.Printf("Received secrets response") + + default: + log.Printf("Unknown message type: %s", msg.Type) + } + } +} + +// runBuildProcess runs the actual build and stores the result +func runBuildProcess() { start := time.Now() var logs bytes.Buffer logWriter := io.MultiWriter(os.Stdout, &logs) log.SetOutput(logWriter) - log.Println("=== Builder Agent Starting ===") + + defer func() { + close(buildDone) + }() // Load build config config, err := loadConfig() if err != nil { - sendResult(BuildResult{ + setResult(BuildResult{ Success: false, Error: fmt.Sprintf("load config: %v", err), Logs: logs.String(), DurationMS: time.Since(start).Milliseconds(), }) - os.Exit(1) + return } log.Printf("Job: %s, Runtime: %s", config.JobID, config.Runtime) @@ -105,15 +222,11 @@ func main() { defer cancel() } - // Fetch secrets from host if needed - if err := fetchSecrets(ctx, config.Secrets); err != nil { - sendResult(BuildResult{ - Success: false, - Error: fmt.Sprintf("fetch secrets: %v", err), - Logs: logs.String(), - DurationMS: time.Since(start).Milliseconds(), - }) - os.Exit(1) + // Note: Secret fetching would need the host connection + // For now, we skip secrets if they require host communication + // TODO: Implement bidirectional secret fetching + if len(config.Secrets) > 0 { + log.Printf("Warning: Secrets requested but vsock secret fetching not yet implemented in new model") } // Generate Dockerfile if not provided @@ -121,24 +234,24 @@ func main() { if dockerfile == "" { dockerfile, err = generateDockerfile(config) if err != nil { - sendResult(BuildResult{ + setResult(BuildResult{ Success: false, Error: fmt.Sprintf("generate dockerfile: %v", err), Logs: logs.String(), DurationMS: time.Since(start).Milliseconds(), }) - os.Exit(1) + return } // Write generated Dockerfile dockerfilePath := filepath.Join(config.SourcePath, "Dockerfile") if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil { - sendResult(BuildResult{ + setResult(BuildResult{ Success: false, Error: fmt.Sprintf("write dockerfile: %v", err), Logs: logs.String(), DurationMS: time.Since(start).Milliseconds(), }) - os.Exit(1) + return } log.Println("Generated Dockerfile for runtime:", config.Runtime) } @@ -154,21 +267,21 @@ func main() { duration := time.Since(start).Milliseconds() if err != nil { - sendResult(BuildResult{ + setResult(BuildResult{ Success: false, Error: err.Error(), Logs: logs.String(), Provenance: provenance, DurationMS: duration, }) - os.Exit(1) + return } // Success! log.Printf("=== Build Complete: %s ===", digest) provenance.Timestamp = time.Now() - sendResult(BuildResult{ + setResult(BuildResult{ Success: true, ImageDigest: digest, Logs: logs.String(), @@ -177,6 +290,13 @@ func main() { }) } +// setResult stores the build result for the host to retrieve +func setResult(result BuildResult) { + buildResultLock.Lock() + defer buildResultLock.Unlock() + buildResult = &result +} + func loadConfig() (*BuildConfig, error) { data, err := os.ReadFile(configPath) if err != nil { @@ -424,75 +544,3 @@ func getToolchainVersion(runtime string) string { } return "unknown" } - -func fetchSecrets(ctx context.Context, secrets []SecretRef) error { - if len(secrets) == 0 { - return nil - } - - conn, err := dialVsock() - if err != nil { - return fmt.Errorf("dial vsock: %w", err) - } - defer conn.Close() - - // Request secrets - secretIDs := make([]string, len(secrets)) - for i, s := range secrets { - secretIDs[i] = s.ID - } - - req := VsockMessage{ - Type: "get_secrets", - } - if err := json.NewEncoder(conn).Encode(req); err != nil { - return err - } - - // Receive response - var resp struct { - Secrets map[string]string `json:"secrets"` - } - if err := json.NewDecoder(conn).Decode(&resp); err != nil { - return err - } - - // Write secrets to files - if err := os.MkdirAll("/run/secrets", 0700); err != nil { - return err - } - for _, s := range secrets { - value, ok := resp.Secrets[s.ID] - if !ok { - return fmt.Errorf("secret not found: %s", s.ID) - } - path := fmt.Sprintf("/run/secrets/%s", s.ID) - if err := os.WriteFile(path, []byte(value), 0600); err != nil { - return err - } - } - - return nil -} - -func sendResult(result BuildResult) { - conn, err := dialVsock() - if err != nil { - log.Printf("Failed to dial vsock: %v", err) - return - } - defer conn.Close() - - msg := VsockMessage{ - Type: "build_result", - Result: &result, - } - - if err := json.NewEncoder(conn).Encode(msg); err != nil { - log.Printf("Failed to send result: %v", err) - } -} - -func dialVsock() (net.Conn, error) { - return vsock.Dial(hostCID, vsockPort, nil) -} diff --git a/lib/builds/manager.go b/lib/builds/manager.go index ac49b7ff..0eaac3e7 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -1,12 +1,15 @@ package builds import ( + "bufio" "context" "encoding/json" "fmt" "log/slog" + "net" "os" "path/filepath" + "strings" "sync" "time" @@ -76,7 +79,6 @@ type manager struct { instanceManager instances.Manager volumeManager volumes.Manager secretProvider SecretProvider - vsockHandler *VsockHandler logger *slog.Logger metrics *Metrics createMu sync.Mutex @@ -103,7 +105,6 @@ func NewManager( instanceManager: instanceMgr, volumeManager: volumeMgr, secretProvider: secretProvider, - vsockHandler: NewVsockHandler(secretProvider, logger), logger: logger, } @@ -124,16 +125,10 @@ func NewManager( // Start starts the build manager's background services func (m *manager) Start(ctx context.Context) error { - // Start the vsock handler in a goroutine - go func() { - if err := m.vsockHandler.ListenAndServe(ctx); err != nil { - if ctx.Err() == nil { - m.logger.Error("vsock handler error", "error", err) - } - } - }() - - m.logger.Info("build manager started", "vsock_port", BuildAgentVsockPort) + // Note: We no longer use a global vsock listener. + // Instead, we connect TO each builder VM's vsock socket directly. + // This follows the Cloud Hypervisor vsock pattern where host initiates connections. + m.logger.Info("build manager started") return nil } @@ -381,42 +376,126 @@ func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRe return result, nil } -// waitForResult waits for the build result from the builder agent +// waitForResult waits for the build result from the builder agent via vsock func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) (*BuildResult, error) { - // Poll for the build result - // In a production system, you'd use vsock for real-time communication - // For now, we'll poll the instance state and check for completion + // Wait a bit for the VM to start and the builder agent to listen on vsock + time.Sleep(3 * time.Second) - ticker := time.NewTicker(5 * time.Second) - defer ticker.Stop() + // Try to connect to the builder agent with retries + var conn net.Conn + var err error - timeout := time.After(30 * time.Minute) // Maximum wait time - - for { + for attempt := 0; attempt < 30; attempt++ { select { case <-ctx.Done(): return nil, ctx.Err() - case <-timeout: - return nil, ErrBuildTimeout - case <-ticker.C: - // Check if instance is still running - current, err := m.instanceManager.GetInstance(ctx, inst.Id) - if err != nil { - // Instance might have been deleted - return nil, fmt.Errorf("check instance: %w", err) - } - - // If instance stopped, check for result in logs - if current.State == instances.StateStopped || current.State == instances.StateShutdown { - // Try to parse result from logs - // This is a fallback - ideally vsock would be used - return &BuildResult{ - Success: false, - Error: "builder instance stopped unexpectedly", - }, nil - } + default: + } + + conn, err = m.dialBuilderVsock(inst.VsockSocket) + if err == nil { + break + } + + m.logger.Debug("waiting for builder agent", "attempt", attempt+1, "error", err) + time.Sleep(2 * time.Second) + + // Check if instance is still running + current, checkErr := m.instanceManager.GetInstance(ctx, inst.Id) + if checkErr != nil { + return nil, fmt.Errorf("check instance: %w", checkErr) + } + if current.State == instances.StateStopped || current.State == instances.StateShutdown { + return &BuildResult{ + Success: false, + Error: "builder instance stopped unexpectedly", + }, nil } } + + if conn == nil { + return nil, fmt.Errorf("failed to connect to builder agent after retries: %w", err) + } + defer conn.Close() + + m.logger.Info("connected to builder agent", "instance", inst.Id) + + // Send request for result + encoder := json.NewEncoder(conn) + decoder := json.NewDecoder(conn) + + // Request the build result (this will block until build completes) + if err := encoder.Encode(VsockMessage{Type: "get_result"}); err != nil { + return nil, fmt.Errorf("send get_result request: %w", err) + } + + // Wait for response + var response VsockMessage + if err := decoder.Decode(&response); err != nil { + return nil, fmt.Errorf("read result: %w", err) + } + + if response.Type != "build_result" || response.Result == nil { + return nil, fmt.Errorf("unexpected response type: %s", response.Type) + } + + return response.Result, nil +} + +// dialBuilderVsock connects to a builder VM's vsock socket using Cloud Hypervisor's handshake +func (m *manager) dialBuilderVsock(vsockSocketPath string) (net.Conn, error) { + // Connect to the Cloud Hypervisor vsock Unix socket + conn, err := net.DialTimeout("unix", vsockSocketPath, 5*time.Second) + if err != nil { + return nil, fmt.Errorf("dial vsock socket %s: %w", vsockSocketPath, err) + } + + // Set deadline for handshake + if err := conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil { + conn.Close() + return nil, fmt.Errorf("set handshake deadline: %w", err) + } + + // Perform Cloud Hypervisor vsock handshake + // Format: "CONNECT \n" -> "OK \n" + handshakeCmd := fmt.Sprintf("CONNECT %d\n", BuildAgentVsockPort) + if _, err := conn.Write([]byte(handshakeCmd)); err != nil { + conn.Close() + return nil, fmt.Errorf("send vsock handshake: %w", err) + } + + // Read handshake response + reader := bufio.NewReader(conn) + response, err := reader.ReadString('\n') + if err != nil { + conn.Close() + return nil, fmt.Errorf("read vsock handshake response: %w", err) + } + + // Clear deadline after successful handshake + if err := conn.SetDeadline(time.Time{}); err != nil { + conn.Close() + return nil, fmt.Errorf("clear deadline: %w", err) + } + + response = strings.TrimSpace(response) + if !strings.HasPrefix(response, "OK ") { + conn.Close() + return nil, fmt.Errorf("vsock handshake failed: %s", response) + } + + return &bufferedConn{Conn: conn, reader: reader}, nil +} + +// bufferedConn wraps a net.Conn with a bufio.Reader to ensure any buffered +// data from the handshake is properly drained before reading from the connection +type bufferedConn struct { + net.Conn + reader *bufio.Reader +} + +func (c *bufferedConn) Read(p []byte) (int, error) { + return c.reader.Read(p) } // updateStatus updates the build status diff --git a/lib/builds/vsock_handler.go b/lib/builds/vsock_handler.go index 6c31dc4d..bfe25de9 100644 --- a/lib/builds/vsock_handler.go +++ b/lib/builds/vsock_handler.go @@ -2,26 +2,19 @@ package builds import ( "context" - "encoding/json" - "fmt" - "io" - "log/slog" - "net" - "sync" - - "github.com/mdlayher/vsock" ) const ( - // BuildAgentVsockPort is the port the builder agent listens on + // BuildAgentVsockPort is the port the builder agent listens on inside the guest BuildAgentVsockPort = 5001 ) // VsockMessage is the envelope for vsock communication with builder agents type VsockMessage struct { - Type string `json:"type"` - Result *BuildResult `json:"result,omitempty"` - Log string `json:"log,omitempty"` + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` + Secrets map[string]string `json:"secrets,omitempty"` // For secrets response } // SecretsRequest is sent by the builder agent to fetch secrets @@ -46,204 +39,3 @@ type NoOpSecretProvider struct{} func (p *NoOpSecretProvider) GetSecrets(ctx context.Context, secretIDs []string) (map[string]string, error) { return make(map[string]string), nil } - -// BuildResultHandler is called when a build completes -type BuildResultHandler func(result *BuildResult) - -// BuildLogHandler is called for each log line from the builder -type BuildLogHandler func(line string) - -// VsockHandler handles vsock communication with builder agents -type VsockHandler struct { - secretProvider SecretProvider - resultHandlers map[string]BuildResultHandler - logHandlers map[string]BuildLogHandler - mu sync.RWMutex - logger *slog.Logger -} - -// NewVsockHandler creates a new vsock handler -func NewVsockHandler(secretProvider SecretProvider, logger *slog.Logger) *VsockHandler { - if secretProvider == nil { - secretProvider = &NoOpSecretProvider{} - } - if logger == nil { - logger = slog.Default() - } - return &VsockHandler{ - secretProvider: secretProvider, - resultHandlers: make(map[string]BuildResultHandler), - logHandlers: make(map[string]BuildLogHandler), - logger: logger, - } -} - -// RegisterHandlers registers handlers for a specific build -func (h *VsockHandler) RegisterHandlers(buildID string, resultHandler BuildResultHandler, logHandler BuildLogHandler) { - h.mu.Lock() - defer h.mu.Unlock() - if resultHandler != nil { - h.resultHandlers[buildID] = resultHandler - } - if logHandler != nil { - h.logHandlers[buildID] = logHandler - } -} - -// UnregisterHandlers removes handlers for a build -func (h *VsockHandler) UnregisterHandlers(buildID string) { - h.mu.Lock() - defer h.mu.Unlock() - delete(h.resultHandlers, buildID) - delete(h.logHandlers, buildID) -} - -// ListenAndServe starts listening for vsock connections -// This should be called once and runs until the context is cancelled -func (h *VsockHandler) ListenAndServe(ctx context.Context) error { - l, err := vsock.Listen(BuildAgentVsockPort, nil) - if err != nil { - return fmt.Errorf("listen vsock: %w", err) - } - defer l.Close() - - h.logger.Info("vsock handler listening", "port", BuildAgentVsockPort) - - // Handle context cancellation - go func() { - <-ctx.Done() - l.Close() - }() - - for { - conn, err := l.Accept() - if err != nil { - if ctx.Err() != nil { - return ctx.Err() - } - h.logger.Error("accept vsock connection", "error", err) - continue - } - go h.handleConnection(ctx, conn) - } -} - -// handleConnection handles a single vsock connection -func (h *VsockHandler) handleConnection(ctx context.Context, conn net.Conn) { - defer conn.Close() - - decoder := json.NewDecoder(conn) - encoder := json.NewEncoder(conn) - - for { - var msg VsockMessage - if err := decoder.Decode(&msg); err != nil { - if err == io.EOF { - return - } - h.logger.Error("decode vsock message", "error", err) - return - } - - switch msg.Type { - case "get_secrets": - // Decode the actual request - var req SecretsRequest - // Re-read to get the full message - for simplicity we expect - // the secrets list in a separate field or we can use the same connection - secrets, err := h.secretProvider.GetSecrets(ctx, req.SecretIDs) - if err != nil { - h.logger.Error("get secrets", "error", err) - encoder.Encode(SecretsResponse{Secrets: make(map[string]string)}) - continue - } - encoder.Encode(SecretsResponse{Secrets: secrets}) - - case "build_result": - if msg.Result != nil { - h.handleBuildResult(msg.Result) - } - - case "log": - if msg.Log != "" { - h.handleLog(msg.Log) - } - - default: - h.logger.Warn("unknown vsock message type", "type", msg.Type) - } - } -} - -// handleBuildResult dispatches a build result to the registered handler -func (h *VsockHandler) handleBuildResult(result *BuildResult) { - // For now, we broadcast to all handlers since we don't have build ID in the message - // In a production system, you'd include the build ID in the result - h.mu.RLock() - handlers := make([]BuildResultHandler, 0, len(h.resultHandlers)) - for _, handler := range h.resultHandlers { - handlers = append(handlers, handler) - } - h.mu.RUnlock() - - for _, handler := range handlers { - handler(result) - } -} - -// handleLog dispatches a log line to the registered handler -func (h *VsockHandler) handleLog(line string) { - h.mu.RLock() - handlers := make([]BuildLogHandler, 0, len(h.logHandlers)) - for _, handler := range h.logHandlers { - handlers = append(handlers, handler) - } - h.mu.RUnlock() - - for _, handler := range handlers { - handler(line) - } -} - -// ConnectToBuilder connects to a builder agent via vsock -// This is used to communicate with a specific builder VM -func ConnectToBuilder(cid uint32) (net.Conn, error) { - return vsock.Dial(cid, BuildAgentVsockPort, nil) -} - -// WaitForBuildResult waits for a build result from a specific builder -// It connects to the builder's vsock and reads the result -func WaitForBuildResult(ctx context.Context, cid uint32) (*BuildResult, error) { - conn, err := vsock.Dial(cid, BuildAgentVsockPort, nil) - if err != nil { - return nil, fmt.Errorf("dial builder: %w", err) - } - defer conn.Close() - - // Set read deadline based on context - if deadline, ok := ctx.Deadline(); ok { - conn.SetReadDeadline(deadline) - } - - decoder := json.NewDecoder(conn) - for { - select { - case <-ctx.Done(): - return nil, ctx.Err() - default: - } - - var msg VsockMessage - if err := decoder.Decode(&msg); err != nil { - if err == io.EOF { - continue - } - return nil, fmt.Errorf("decode message: %w", err) - } - - if msg.Type == "build_result" && msg.Result != nil { - return msg.Result, nil - } - } -} - From b1bc4ac11c9f57210ad838bc117e11843dd1450d Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Mon, 5 Jan 2026 16:00:11 -0500 Subject: [PATCH 07/42] fix(e2e): update e2e-build-test.sh for current config - Fix API port to 8083 (was 8080) - Update builder image to hirokernel/builder-nodejs20:latest - Add explicit Dockerfile to test source - Fix log functions to output to stderr (avoid mixing with return values) - Add environment variables documentation --- scripts/e2e-build-test.sh | 61 +++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index bbd0bc01..93078f2a 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -3,14 +3,18 @@ # Usage: ./scripts/e2e-build-test.sh # # Prerequisites: -# - API server running (make dev or sudo ./bin/hypeman) -# - Builder images built (docker build -t hypeman/builder:latest ...) +# - API server running (make dev) +# - Builder image imported into Hypeman registry (hirokernel/builder-nodejs20:latest) # - .env file configured +# +# Environment variables: +# API_URL - API endpoint (default: http://localhost:8083) +# BUILDER_IMAGE - Builder image to check (default: hirokernel/builder-nodejs20:latest) set -e # Configuration -API_URL="${API_URL:-http://localhost:8080}" +API_URL="${API_URL:-http://localhost:8083}" TIMEOUT_POLLS=60 POLL_INTERVAL=5 @@ -20,9 +24,9 @@ GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color -log() { echo -e "${GREEN}[INFO]${NC} $1"; } -warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } -error() { echo -e "${RED}[ERROR]${NC} $1"; } +log() { echo -e "${GREEN}[INFO]${NC} $1" >&2; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1" >&2; } +error() { echo -e "${RED}[ERROR]${NC} $1" >&2; } # Check prerequisites check_prerequisites() { @@ -36,13 +40,14 @@ check_prerequisites() { fi log "✓ API server is running" - # Check if builder image exists - if ! docker images hypeman/builder:latest --format "{{.Repository}}" | grep -q hypeman; then - error "Builder image not found" - error "Build it with: docker build -t hypeman/builder:latest -f lib/builds/images/nodejs20/Dockerfile ." - exit 1 + # Check if builder image exists (check both local and Docker Hub versions) + BUILDER_IMAGE="${BUILDER_IMAGE:-hirokernel/builder-nodejs20:latest}" + if ! docker images "$BUILDER_IMAGE" --format "{{.Repository}}" | grep -q .; then + warn "Builder image not found locally, will be pulled from registry" + warn "Or build it with: make build-builder-nodejs20" + else + log "✓ Builder image available locally" fi - log "✓ Builder image available" } # Generate JWT token @@ -87,6 +92,13 @@ EOF cat > "$TEST_DIR/index.js" << 'EOF' console.log("E2E Build Test - Success!"); console.log("Built at:", new Date().toISOString()); +EOF + + cat > "$TEST_DIR/Dockerfile" << 'EOF' +FROM node:20-alpine +WORKDIR /app +COPY package.json index.js ./ +CMD ["node", "index.js"] EOF # Create tarball @@ -104,12 +116,25 @@ submit_build() { log "Submitting build..." - RESPONSE=$(curl -s -X POST "$API_URL/builds" \ - -H "Authorization: Bearer $token" \ - -F "runtime=nodejs20" \ - -F "source=@$source" \ - -F "cache_scope=e2e-test" \ - -F "timeout_seconds=300") + # Extract Dockerfile from source tarball + DOCKERFILE_CONTENT=$(tar -xzf "$source" -O ./Dockerfile 2>/dev/null || echo "") + + if [ -n "$DOCKERFILE_CONTENT" ]; then + RESPONSE=$(curl -s -X POST "$API_URL/builds" \ + -H "Authorization: Bearer $token" \ + -F "runtime=nodejs20" \ + -F "source=@$source" \ + -F "dockerfile=$DOCKERFILE_CONTENT" \ + -F "cache_scope=e2e-test" \ + -F "timeout_seconds=300") + else + RESPONSE=$(curl -s -X POST "$API_URL/builds" \ + -H "Authorization: Bearer $token" \ + -F "runtime=nodejs20" \ + -F "source=@$source" \ + -F "cache_scope=e2e-test" \ + -F "timeout_seconds=300") + fi BUILD_ID=$(echo "$RESPONSE" | jq -r '.id // empty') From 5663636ba1a413dbafcb779f0a7bd3a81575894d Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Tue, 6 Jan 2026 15:30:11 -0500 Subject: [PATCH 08/42] chore(e2e): update test script for generic builder system - Remove deprecated runtime parameter from build submission - Require Dockerfile in source tarball (fail early if missing) - Update builder image reference to hypeman/builder:latest - Update comments to reflect generic builder approach --- scripts/e2e-build-test.sh | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index 93078f2a..143d000c 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -4,12 +4,12 @@ # # Prerequisites: # - API server running (make dev) -# - Builder image imported into Hypeman registry (hirokernel/builder-nodejs20:latest) +# - Generic builder image imported into Hypeman registry # - .env file configured # # Environment variables: # API_URL - API endpoint (default: http://localhost:8083) -# BUILDER_IMAGE - Builder image to check (default: hirokernel/builder-nodejs20:latest) +# BUILDER_IMAGE - Builder image to check (default: hypeman/builder:latest) set -e @@ -40,11 +40,11 @@ check_prerequisites() { fi log "✓ API server is running" - # Check if builder image exists (check both local and Docker Hub versions) - BUILDER_IMAGE="${BUILDER_IMAGE:-hirokernel/builder-nodejs20:latest}" + # Check if generic builder image exists + BUILDER_IMAGE="${BUILDER_IMAGE:-hypeman/builder:latest}" if ! docker images "$BUILDER_IMAGE" --format "{{.Repository}}" | grep -q .; then - warn "Builder image not found locally, will be pulled from registry" - warn "Or build it with: make build-builder-nodejs20" + warn "Builder image not found locally" + warn "Build it with: docker build -t hypeman/builder:latest -f lib/builds/images/generic/Dockerfile ." else log "✓ Builder image available locally" fi @@ -73,10 +73,12 @@ generate_token() { echo "" } -# Create test source +# Create test source with Dockerfile +# The generic builder requires a Dockerfile to be provided create_test_source() { TEST_DIR=$(mktemp -d) + # Application code cat > "$TEST_DIR/package.json" << 'EOF' { "name": "e2e-test-app", @@ -94,6 +96,8 @@ console.log("E2E Build Test - Success!"); console.log("Built at:", new Date().toISOString()); EOF + # Dockerfile is REQUIRED for the generic builder + # Users control their runtime version here cat > "$TEST_DIR/Dockerfile" << 'EOF' FROM node:20-alpine WORKDIR /app @@ -120,20 +124,17 @@ submit_build() { DOCKERFILE_CONTENT=$(tar -xzf "$source" -O ./Dockerfile 2>/dev/null || echo "") if [ -n "$DOCKERFILE_CONTENT" ]; then + # Dockerfile found in source - pass it explicitly for reliability RESPONSE=$(curl -s -X POST "$API_URL/builds" \ -H "Authorization: Bearer $token" \ - -F "runtime=nodejs20" \ -F "source=@$source" \ -F "dockerfile=$DOCKERFILE_CONTENT" \ -F "cache_scope=e2e-test" \ -F "timeout_seconds=300") else - RESPONSE=$(curl -s -X POST "$API_URL/builds" \ - -H "Authorization: Bearer $token" \ - -F "runtime=nodejs20" \ - -F "source=@$source" \ - -F "cache_scope=e2e-test" \ - -F "timeout_seconds=300") + # No Dockerfile in source - will fail if not provided + error "No Dockerfile found in source tarball" + exit 1 fi BUILD_ID=$(echo "$RESPONSE" | jq -r '.id // empty') From f0dd9245eb33144cab4b8c00c813387712d5e9bf Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Tue, 6 Jan 2026 23:49:27 -0500 Subject: [PATCH 09/42] feat(builds): implement generic builder with registry token auth - Replace runtime-specific builders (nodejs20, python312) with generic builder - Users now provide their own Dockerfile instead of auto-generation - Add JWT-based registry token authentication for builder VMs - Tokens scoped to specific build and cache repositories - 30-minute expiry for security - Support both Bearer and Basic auth (for BuildKit compatibility) - Update builder agent to configure registry auth from token - Fix auth middleware to handle Basic auth for registry paths - Update API to make 'runtime' optional (deprecated) - Add comprehensive documentation for building OCI-format images - Delete deprecated: templates/, base/, nodejs20/, python312/ Dockerfiles Breaking changes: - Dockerfile is now required (in source tarball or as API parameter) - Builder image must be built with 'docker buildx --output type=registry,oci-mediatypes=true' --- cmd/api/api/builds.go | 23 +- lib/builds/README.md | 72 ++-- lib/builds/builder_agent/main.go | 146 +++---- lib/builds/cache.go | 5 +- lib/builds/cache_test.go | 7 +- lib/builds/errors.go | 18 +- lib/builds/images/README.md | 373 +++++++++++------- lib/builds/images/base/Dockerfile | 29 -- .../images/{nodejs20 => generic}/Dockerfile | 37 +- lib/builds/images/python312/Dockerfile | 69 ---- lib/builds/manager.go | 30 ++ lib/builds/registry_token.go | 108 +++++ lib/builds/registry_token_test.go | 113 ++++++ lib/builds/templates/templates.go | 230 ----------- lib/builds/templates/templates_test.go | 180 --------- lib/builds/types.go | 34 +- lib/middleware/oapi_auth.go | 176 ++++++++- lib/oapi/oapi.go | 242 ++++++------ lib/providers/providers.go | 1 + openapi.yaml | 34 +- 20 files changed, 936 insertions(+), 991 deletions(-) delete mode 100644 lib/builds/images/base/Dockerfile rename lib/builds/images/{nodejs20 => generic}/Dockerfile (58%) delete mode 100644 lib/builds/images/python312/Dockerfile create mode 100644 lib/builds/registry_token.go create mode 100644 lib/builds/registry_token_test.go delete mode 100644 lib/builds/templates/templates.go delete mode 100644 lib/builds/templates/templates_test.go diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go index a1654d15..d8c5d3f5 100644 --- a/cmd/api/api/builds.go +++ b/cmd/api/api/builds.go @@ -90,11 +90,10 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe part.Close() } + // Note: runtime is deprecated and optional. The generic builder accepts any Dockerfile. + // If runtime is empty, we use "generic" as a placeholder for logging/caching purposes. if runtime == "" { - return oapi.CreateBuild400JSONResponse{ - Code: "invalid_request", - Message: "runtime is required", - }, nil + runtime = "generic" } if len(sourceData) == 0 { @@ -104,6 +103,9 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe }, nil } + // Note: Dockerfile validation happens in the builder agent. + // It will check if Dockerfile is in the source tarball or provided via dockerfile parameter. + // Build domain request domainReq := builds.CreateBuildRequest{ Runtime: runtime, @@ -123,10 +125,16 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe if err != nil { switch { case errors.Is(err, builds.ErrInvalidRuntime): + // Deprecated: Runtime validation no longer occurs, but kept for compatibility return oapi.CreateBuild400JSONResponse{ Code: "invalid_runtime", Message: err.Error(), }, nil + case errors.Is(err, builds.ErrDockerfileRequired): + return oapi.CreateBuild400JSONResponse{ + Code: "dockerfile_required", + Message: err.Error(), + }, nil case errors.Is(err, builds.ErrInvalidSource): return oapi.CreateBuild400JSONResponse{ Code: "invalid_source", @@ -225,10 +233,15 @@ func (s *ApiService) GetBuildLogs(ctx context.Context, request oapi.GetBuildLogs // buildToOAPI converts a domain Build to OAPI Build func buildToOAPI(b *builds.Build) oapi.Build { + var runtimePtr *string + if b.Runtime != "" { + runtimePtr = &b.Runtime + } + oapiBuild := oapi.Build{ Id: b.ID, Status: oapi.BuildStatus(b.Status), - Runtime: b.Runtime, + Runtime: runtimePtr, QueuePosition: b.QueuePosition, ImageDigest: b.ImageDigest, ImageRef: b.ImageRef, diff --git a/lib/builds/README.md b/lib/builds/README.md index add6d907..e37dca72 100644 --- a/lib/builds/README.md +++ b/lib/builds/README.md @@ -22,7 +22,7 @@ The build system provides source-to-image builds inside ephemeral Cloud Hypervis │ ├─────────────────────────────────────────────────────────────┤│ │ │ Builder Agent ││ │ │ ┌─────────────┐ ┌──────────────┐ ┌────────────────────┐ ││ -│ │ │ Load Config │→ │ Generate │→ │ Run BuildKit │ ││ +│ │ │ Load Config │→ │ Read User's │→ │ Run BuildKit │ ││ │ │ │ /config/ │ │ Dockerfile │ │ (buildctl) │ ││ │ │ └─────────────┘ └──────────────┘ └────────────────────┘ ││ │ │ │ ││ @@ -96,20 +96,6 @@ Orchestrates the build lifecycle: **Important**: The `Start()` method must be called to start the vsock handler for builder communication. -### Dockerfile Templates (`templates/`) - -Auto-generates Dockerfiles based on runtime and detected lockfiles: - -| Runtime | Package Managers | -|---------|-----------------| -| `nodejs20` | npm, yarn, pnpm | -| `python312` | pip, poetry, pipenv | - -```go -gen, _ := templates.GetGenerator("nodejs20") -dockerfile, _ := gen.Generate(sourceDir, baseImageDigest) -``` - ### Cache System (`cache.go`) Registry-based caching with tenant isolation: @@ -131,11 +117,13 @@ Guest binary that runs inside builder VMs: 1. Reads config from `/config/build.json` 2. Fetches secrets from host via vsock (if any) -3. Generates Dockerfile (if not provided) +3. Uses user-provided Dockerfile (from source or config) 4. Runs `buildctl-daemonless.sh` with cache and insecure registry flags 5. Computes provenance (lockfile hashes, source hash) 6. Reports result back via vsock +**Note**: The agent requires a Dockerfile to be provided. It can be included in the source tarball or passed via the `dockerfile` config parameter. + **Key Details**: - Config path: `/config/build.json` - Source path: `/src` @@ -155,12 +143,23 @@ Guest binary that runs inside builder VMs: ### Submit Build Example ```bash +# Option 1: Dockerfile in source tarball curl -X POST http://localhost:8083/builds \ -H "Authorization: Bearer $TOKEN" \ - -F "runtime=nodejs20" \ -F "source=@source.tar.gz" \ -F "cache_scope=tenant-123" \ -F "timeout_seconds=300" + +# Option 2: Dockerfile as parameter +curl -X POST http://localhost:8083/builds \ + -H "Authorization: Bearer $TOKEN" \ + -F "source=@source.tar.gz" \ + -F "dockerfile=FROM node:20-alpine +WORKDIR /app +COPY . . +RUN npm ci +CMD [\"node\", \"index.js\"]" \ + -F "cache_scope=tenant-123" ``` ### Response @@ -169,8 +168,6 @@ curl -X POST http://localhost:8083/builds \ { "id": "abc123", "status": "queued", - "runtime": "nodejs20", - "queue_position": 1, "created_at": "2025-01-15T10:00:00Z" } ``` @@ -215,11 +212,11 @@ queued → building → pushing → ready ## Builder Images -Builder images are in `images/`: +The generic builder image is in `images/generic/`: + +- `generic/Dockerfile` - Minimal Alpine + BuildKit + agent (runtime-agnostic) -- `base/Dockerfile` - BuildKit base -- `nodejs20/Dockerfile` - Node.js 20 + BuildKit + agent -- `python312/Dockerfile` - Python 3.12 + BuildKit + agent +The generic builder does not include any runtime (Node.js, Python, etc.). Users provide their own Dockerfile which specifies the runtime. BuildKit pulls the runtime as part of the build process. ### Required Components @@ -234,22 +231,17 @@ Builder images must include: | `builder-agent` | Built from `builder_agent/main.go` | Hypeman agent | | `fuse-overlayfs` | apk/apt | Overlay filesystem support | -### Build and Push (OCI Format) - -Builder images must be pushed in OCI format (not Docker v2 manifest): +### Build and Push ```bash -# Build with OCI output -docker buildx build --platform linux/amd64 \ - -t myregistry/builder-nodejs20:latest \ - -f lib/builds/images/nodejs20/Dockerfile \ - --output type=oci,dest=/tmp/builder.tar \ +# Build the generic builder image +docker build \ + -t hypeman/builder:latest \ + -f lib/builds/images/generic/Dockerfile \ . -# Extract and push with crane -mkdir -p /tmp/oci-builder -tar -xf /tmp/builder.tar -C /tmp/oci-builder -crane push /tmp/oci-builder myregistry/builder-nodejs20:latest +# Push to your registry +docker push hypeman/builder:latest ``` ### Environment Variables @@ -339,12 +331,17 @@ go test ./lib/builds/templates/... -v -d '{"name": "hirokernel/builder-nodejs20:latest"}' ``` -3. **Create test source**: +3. **Create test source with Dockerfile**: ```bash mkdir -p /tmp/test-app echo '{"name": "test", "version": "1.0.0", "dependencies": {}}' > /tmp/test-app/package.json - echo '{"lockfileVersion": 3, "packages": {}}' > /tmp/test-app/package-lock.json echo 'console.log("Hello!");' > /tmp/test-app/index.js + cat > /tmp/test-app/Dockerfile << 'EOF' + FROM node:20-alpine + WORKDIR /app + COPY package.json index.js ./ + CMD ["node", "index.js"] + EOF tar -czf /tmp/source.tar.gz -C /tmp/test-app . ``` @@ -352,7 +349,6 @@ go test ./lib/builds/templates/... -v ```bash curl -X POST http://localhost:8083/builds \ -H "Authorization: Bearer $TOKEN" \ - -F "runtime=nodejs20" \ -F "source=@/tmp/source.tar.gz" ``` diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index d6e799b5..5b4d59c1 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -13,6 +13,7 @@ import ( "bytes" "context" "crypto/sha256" + "encoding/base64" "encoding/hex" "encoding/json" "fmt" @@ -40,6 +41,7 @@ type BuildConfig struct { Runtime string `json:"runtime"` BaseImageDigest string `json:"base_image_digest,omitempty"` RegistryURL string `json:"registry_url"` + RegistryToken string `json:"registry_token,omitempty"` CacheScope string `json:"cache_scope,omitempty"` SourcePath string `json:"source_path"` Dockerfile string `json:"dockerfile,omitempty"` @@ -212,7 +214,18 @@ func runBuildProcess() { }) return } - log.Printf("Job: %s, Runtime: %s", config.JobID, config.Runtime) + log.Printf("Job: %s", config.JobID) + + // Setup registry authentication before running the build + if err := setupRegistryAuth(config.RegistryURL, config.RegistryToken); err != nil { + setResult(BuildResult{ + Success: false, + Error: fmt.Sprintf("setup registry auth: %v", err), + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + return + } // Setup timeout context ctx := context.Background() @@ -229,22 +242,21 @@ func runBuildProcess() { log.Printf("Warning: Secrets requested but vsock secret fetching not yet implemented in new model") } - // Generate Dockerfile if not provided - dockerfile := config.Dockerfile - if dockerfile == "" { - dockerfile, err = generateDockerfile(config) - if err != nil { + // Ensure Dockerfile exists (either in source or provided via config) + dockerfilePath := filepath.Join(config.SourcePath, "Dockerfile") + if _, err := os.Stat(dockerfilePath); os.IsNotExist(err) { + // Check if Dockerfile was provided in config + if config.Dockerfile == "" { setResult(BuildResult{ Success: false, - Error: fmt.Sprintf("generate dockerfile: %v", err), + Error: "Dockerfile required: provide dockerfile parameter or include Dockerfile in source tarball", Logs: logs.String(), DurationMS: time.Since(start).Milliseconds(), }) return } - // Write generated Dockerfile - dockerfilePath := filepath.Join(config.SourcePath, "Dockerfile") - if err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0644); err != nil { + // Write provided Dockerfile to source directory + if err := os.WriteFile(dockerfilePath, []byte(config.Dockerfile), 0644); err != nil { setResult(BuildResult{ Success: false, Error: fmt.Sprintf("write dockerfile: %v", err), @@ -253,7 +265,9 @@ func runBuildProcess() { }) return } - log.Println("Generated Dockerfile for runtime:", config.Runtime) + log.Println("Using Dockerfile from config") + } else { + log.Println("Using Dockerfile from source") } // Compute provenance @@ -309,81 +323,47 @@ func loadConfig() (*BuildConfig, error) { return &config, nil } -func generateDockerfile(config *BuildConfig) (string, error) { - switch { - case strings.HasPrefix(config.Runtime, "nodejs"): - return generateNodeDockerfile(config) - case strings.HasPrefix(config.Runtime, "python"): - return generatePythonDockerfile(config) - default: - return "", fmt.Errorf("unsupported runtime: %s", config.Runtime) +// setupRegistryAuth creates a Docker config.json with the registry token for authentication. +// BuildKit uses this file to authenticate when pushing images. +func setupRegistryAuth(registryURL, token string) error { + if token == "" { + log.Println("No registry token provided, skipping auth setup") + return nil } -} -func generateNodeDockerfile(config *BuildConfig) (string, error) { - version := strings.TrimPrefix(config.Runtime, "nodejs") - baseImage := config.BaseImageDigest - if baseImage == "" { - baseImage = fmt.Sprintf("node:%s-alpine", version) - } + // Docker config format expects base64-encoded "username:password" or just the token + // For bearer tokens, we use the token directly as the "auth" value + // Format: base64(token + ":") - empty password + authValue := base64.StdEncoding.EncodeToString([]byte(token + ":")) - // Detect lockfile - lockfile := "package-lock.json" - installCmd := "npm ci" - if _, err := os.Stat(filepath.Join(config.SourcePath, "pnpm-lock.yaml")); err == nil { - lockfile = "pnpm-lock.yaml" - installCmd = "corepack enable && pnpm install --frozen-lockfile" - } else if _, err := os.Stat(filepath.Join(config.SourcePath, "yarn.lock")); err == nil { - lockfile = "yarn.lock" - installCmd = "yarn install --frozen-lockfile" + // Create the Docker config structure + dockerConfig := map[string]interface{}{ + "auths": map[string]interface{}{ + registryURL: map[string]string{ + "auth": authValue, + }, + }, } - return fmt.Sprintf(`FROM %s - -WORKDIR /app - -COPY package.json %s ./ - -RUN %s - -COPY . . - -CMD ["node", "index.js"] -`, baseImage, lockfile, installCmd), nil -} - -func generatePythonDockerfile(config *BuildConfig) (string, error) { - version := strings.TrimPrefix(config.Runtime, "python") - baseImage := config.BaseImageDigest - if baseImage == "" { - baseImage = fmt.Sprintf("python:%s-slim", version) + configData, err := json.MarshalIndent(dockerConfig, "", " ") + if err != nil { + return fmt.Errorf("marshal docker config: %w", err) } - reqPath := filepath.Join(config.SourcePath, "requirements.txt") - hasHashes := false - if data, err := os.ReadFile(reqPath); err == nil { - hasHashes = strings.Contains(string(data), "--hash=") + // Ensure ~/.docker directory exists + dockerDir := "/home/builder/.docker" + if err := os.MkdirAll(dockerDir, 0700); err != nil { + return fmt.Errorf("create docker config dir: %w", err) } - var installCmd string - if hasHashes { - installCmd = "pip install --require-hashes --only-binary :all: -r requirements.txt" - } else { - installCmd = "pip install --no-cache-dir -r requirements.txt" + // Write config.json + configPath := filepath.Join(dockerDir, "config.json") + if err := os.WriteFile(configPath, configData, 0600); err != nil { + return fmt.Errorf("write docker config: %w", err) } - return fmt.Sprintf(`FROM %s - -WORKDIR /app - -COPY requirements.txt ./ - -RUN %s - -COPY . . - -CMD ["python", "main.py"] -`, baseImage, installCmd), nil + log.Printf("Registry auth configured for %s", registryURL) + return nil } func runBuild(ctx context.Context, config *BuildConfig, logWriter io.Writer) (string, string, error) { @@ -468,7 +448,7 @@ func computeProvenance(config *BuildConfig) BuildProvenance { BaseImageDigest: config.BaseImageDigest, LockfileHashes: make(map[string]string), BuildkitVersion: getBuildkitVersion(), - ToolchainVersion: getToolchainVersion(config.Runtime), + ToolchainVersion: getToolchainVersion(), } // Hash lockfiles @@ -533,14 +513,8 @@ func getBuildkitVersion() string { return strings.TrimSpace(string(out)) } -func getToolchainVersion(runtime string) string { - switch { - case strings.HasPrefix(runtime, "nodejs"): - out, _ := exec.Command("node", "--version").Output() - return strings.TrimSpace(string(out)) - case strings.HasPrefix(runtime, "python"): - out, _ := exec.Command("python", "--version").Output() - return strings.TrimSpace(string(out)) - } - return "unknown" +func getToolchainVersion() string { + // Generic builder doesn't have runtime-specific toolchains + // The actual runtime version is determined by the user's Dockerfile + return "generic" } diff --git a/lib/builds/cache.go b/lib/builds/cache.go index c044f03c..e4842838 100644 --- a/lib/builds/cache.go +++ b/lib/builds/cache.go @@ -43,9 +43,8 @@ func (g *CacheKeyGenerator) GenerateCacheKey(tenantScope, runtime string, lockfi return nil, fmt.Errorf("tenant scope is required for caching") } - if !IsSupportedRuntime(runtime) { - return nil, fmt.Errorf("unsupported runtime: %s", runtime) - } + // Note: Runtime is no longer validated as the generic builder accepts any runtime. + // The runtime is still used as part of the cache key for separation. // Normalize tenant scope (alphanumeric + hyphen only) normalizedScope := normalizeCacheScope(tenantScope) diff --git a/lib/builds/cache_test.go b/lib/builds/cache_test.go index a776eeee..5bb4eca9 100644 --- a/lib/builds/cache_test.go +++ b/lib/builds/cache_test.go @@ -43,10 +43,13 @@ func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { wantErr: true, }, { - name: "invalid runtime", + name: "any runtime is accepted", tenantScope: "tenant", runtime: "ruby", - wantErr: true, + lockfileHashes: map[string]string{ + "Gemfile.lock": "abc123", + }, + wantPrefix: "localhost:8080/cache/tenant/ruby/", }, { name: "scope with special chars", diff --git a/lib/builds/errors.go b/lib/builds/errors.go index a5c1dfd0..6fab2e24 100644 --- a/lib/builds/errors.go +++ b/lib/builds/errors.go @@ -10,8 +10,13 @@ var ( ErrAlreadyExists = errors.New("build already exists") // ErrInvalidRuntime is returned when an unsupported runtime is specified + // Deprecated: Runtime validation is no longer performed. The generic builder + // accepts any Dockerfile. ErrInvalidRuntime = errors.New("invalid runtime") + // ErrDockerfileRequired is returned when no Dockerfile is provided + ErrDockerfileRequired = errors.New("dockerfile required: provide dockerfile parameter or include Dockerfile in source tarball") + // ErrBuildFailed is returned when a build fails ErrBuildFailed = errors.New("build failed") @@ -34,13 +39,12 @@ var ( ErrBuildInProgress = errors.New("build in progress") ) -// IsSupportedRuntime returns true if the runtime is supported +// IsSupportedRuntime returns true if the runtime is supported. +// Deprecated: This function always returns true. The generic builder system +// no longer validates runtimes - users provide their own Dockerfile. func IsSupportedRuntime(runtime string) bool { - switch runtime { - case RuntimeNodeJS20, RuntimePython312: - return true - default: - return false - } + // Always return true - the generic builder accepts any runtime value + // or no runtime at all. Kept for backward compatibility. + return true } diff --git a/lib/builds/images/README.md b/lib/builds/images/README.md index dad65ebf..5cb8efc2 100644 --- a/lib/builds/images/README.md +++ b/lib/builds/images/README.md @@ -1,226 +1,299 @@ -# Builder Images +# Generic Builder Image -Builder images run inside Hypeman microVMs to execute source-to-image builds using BuildKit. +The generic builder image runs inside Hypeman microVMs to execute source-to-image builds using BuildKit. It is runtime-agnostic - users provide their own Dockerfile which specifies the runtime. -## Available Images +## Architecture -| Image | Runtime | Use Case | -|-------|---------|----------| -| `nodejs20/` | Node.js 20.x | npm, yarn, pnpm projects | -| `python312/` | Python 3.12 | pip, poetry, pipenv projects | -| `base/` | None | Base BuildKit image (for custom runtimes) | +``` +┌─────────────────────────────────────────────────────────────┐ +│ Generic Builder Image (~50MB) │ +│ │ +│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │ +│ │ BuildKit │ │ builder- │ │ Minimal Alpine │ │ +│ │ (daemonless)│ │ agent │ │ (git, curl, fuse) │ │ +│ └─────────────┘ └─────────────┘ └─────────────────────┘ │ +└─────────────────────────────────────────────────────────────┘ + │ + ▼ + User's Dockerfile + │ + ▼ + ┌───────────────────────────────┐ + │ FROM node:20-alpine │ + │ FROM python:3.12-slim │ + │ FROM rust:1.75 │ + │ FROM golang:1.22 │ + │ ... any base image │ + └───────────────────────────────┘ +``` -## Creating a Builder Image +## Key Benefits -### Step 1: Create Dockerfile +- **One image to maintain** - No more runtime-specific builder images +- **Any Dockerfile works** - Node.js, Python, Rust, Go, Java, Ruby, etc. +- **Smaller footprint** - ~50MB vs 200MB+ for runtime-specific images +- **User-controlled versions** - Users specify their runtime version in their Dockerfile -Create a new directory under `images/` with a Dockerfile: +## Directory Structure -```dockerfile -# Use BuildKit rootless as base for build tools -FROM moby/buildkit:rootless AS buildkit - -# Use your runtime base image -FROM node:20-alpine +``` +images/ +└── generic/ + └── Dockerfile # The generic builder image +``` -# Install required dependencies -RUN apk add --no-cache \ - fuse-overlayfs \ - shadow \ - newuidmap \ - ca-certificates +## Building the Generic Builder Image -# Create non-root builder user -RUN adduser -D -u 1000 builder && \ - mkdir -p /home/builder/.local/share/buildkit && \ - chown -R builder:builder /home/builder +> **Important**: Hypeman uses `umoci` for OCI image manipulation, which requires images +> to have **OCI manifest format** (not Docker v2 format). You must use `docker buildx` +> with the `oci-mediatypes=true` option. -# Copy BuildKit binaries (these specific paths are required) -COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl -COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh -COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd -COPY --from=buildkit /usr/bin/buildkit-runc /usr/bin/runc +### Prerequisites -# Copy the builder agent (built during image build) -COPY builder-agent /usr/bin/builder-agent +1. **Docker Buildx** with a container builder: + ```bash + # Create a buildx builder (if you don't have one) + docker buildx create --name ocibuilder --use + ``` -# Set environment variables -ENV HOME=/home/builder -ENV XDG_RUNTIME_DIR=/home/builder/.local/share -ENV BUILDKITD_FLAGS="" +2. **Docker Hub login** (or your registry): + ```bash + docker login + ``` -# Run as builder user -USER builder -WORKDIR /home/builder +### 1. Build and Push with OCI Format -# The agent is the entrypoint -ENTRYPOINT ["/usr/bin/builder-agent"] +```bash +# From repository root +docker buildx build \ + --platform linux/amd64 \ + --output "type=registry,oci-mediatypes=true" \ + --tag hirokernel/builder-generic:latest \ + -f lib/builds/images/generic/Dockerfile \ + . ``` -### Step 2: Required Components - -Every builder image **must** include: +This command: +- Builds for `linux/amd64` platform +- Uses `oci-mediatypes=true` to create OCI manifests (required for Hypeman) +- Pushes directly to the registry -| Component | Path | Source | Purpose | -|-----------|------|--------|---------| -| `buildctl` | `/usr/bin/buildctl` | `moby/buildkit:rootless` | BuildKit CLI | -| `buildctl-daemonless.sh` | `/usr/bin/buildctl-daemonless.sh` | `moby/buildkit:rootless` | Runs buildkitd + buildctl together | -| `buildkitd` | `/usr/bin/buildkitd` | `moby/buildkit:rootless` | BuildKit daemon | -| `runc` | `/usr/bin/runc` | `moby/buildkit:rootless` (as `buildkit-runc`) | Container runtime | -| `builder-agent` | `/usr/bin/builder-agent` | Built from Go source | Hypeman orchestration agent | -| `fuse-overlayfs` | System package | apk/apt | Overlay filesystem for rootless builds | - -### Step 3: Build the Agent - -The builder agent must be compiled for the target architecture: +### 2. Verify the Manifest Format ```bash -# From repository root -cd lib/builds/builder_agent -GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o builder-agent . +# Should show "application/vnd.oci.image.index.v1+json" +docker manifest inspect hirokernel/builder-generic:latest | head -5 ``` -### Step 4: Build the Image (OCI Format) +Expected output: +```json +{ + "schemaVersion": 2, + "mediaType": "application/vnd.oci.image.index.v1+json", + ... +} +``` -**Important**: Hypeman uses `umoci` to extract images, which requires OCI format (not Docker v2 manifest). +### 3. Import into Hypeman ```bash -# From repository root +# Generate a token +TOKEN=$(make gen-jwt | tail -1) -# Build agent first -cd lib/builds/builder_agent -GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o builder-agent . -cd ../../.. +# Import the image +curl -X POST http://localhost:8083/images \ + -H "Authorization: Bearer $TOKEN" \ + -H "Content-Type: application/json" \ + -d '{"name": "hirokernel/builder-generic:latest"}' -# Build image with OCI output -docker buildx build --platform linux/amd64 \ - -t yourregistry/builder-nodejs20:latest \ - -f lib/builds/images/nodejs20/Dockerfile \ - --output type=oci,dest=/tmp/builder.tar \ - . +# Wait for import to complete +curl http://localhost:8083/images/docker.io%2Fhirokernel%2Fbuilder-generic:latest \ + -H "Authorization: Bearer $TOKEN" ``` -### Step 5: Push to Registry +### 4. Configure Hypeman -Use `crane` (from go-containerregistry) to push in OCI format: +Set the builder image in your `.env`: ```bash -# Extract the OCI tarball -mkdir -p /tmp/oci-builder -tar -xf /tmp/builder.tar -C /tmp/oci-builder - -# Push to registry -crane push /tmp/oci-builder yourregistry/builder-nodejs20:latest +BUILDER_IMAGE=hirokernel/builder-generic:latest ``` -### Step 6: Configure Hypeman +### Why OCI Format is Required -Set the builder image in your `.env`: +| Build Method | Manifest Type | Works with Hypeman? | +|--------------|---------------|---------------------| +| `docker build` | Docker v2 (`application/vnd.docker.distribution.manifest.v2+json`) | ❌ No | +| `docker buildx --output type=docker` | Docker v2 | ❌ No | +| `docker buildx --output type=registry,oci-mediatypes=true` | OCI (`application/vnd.oci.image.index.v1+json`) | ✅ Yes | -```bash -BUILDER_IMAGE=yourregistry/builder-nodejs20:latest -``` +Hypeman uses `umoci` to extract and convert OCI images to ext4 disk images for microVMs. +`umoci` strictly requires OCI-format manifests and cannot parse Docker v2 manifests. -## Testing Your Builder Image +### Building for Local Testing (without pushing) -### 1. Pull the Image into Hypeman +If you need to test locally before pushing: ```bash -TOKEN=$(make gen-jwt | tail -1) -curl -X POST http://localhost:8083/images \ - -H "Authorization: Bearer $TOKEN" \ - -H "Content-Type: application/json" \ - -d '{"name": "yourregistry/builder-nodejs20:latest"}' +# Build and load to local Docker (for testing only - won't work with Hypeman import) +docker build \ + -t hypeman/builder:local \ + -f lib/builds/images/generic/Dockerfile \ + . + +# Run locally to test +docker run --rm hypeman/builder:local --help ``` -### 2. Submit a Test Build +**Note**: Images built with `docker build` cannot be imported into Hypeman directly. +You must rebuild with `docker buildx --output type=registry,oci-mediatypes=true` +before deploying to Hypeman. + +## Usage + +### Submitting a Build + +Users must provide a Dockerfile either: +1. **In the source tarball** - Include a `Dockerfile` in the root of the source +2. **As a parameter** - Pass `dockerfile` content in the API request ```bash -# Create minimal test source -mkdir -p /tmp/test-app -echo '{"name": "test", "version": "1.0.0"}' > /tmp/test-app/package.json -echo '{"lockfileVersion": 3, "packages": {}}' > /tmp/test-app/package-lock.json -echo 'console.log("Hello from test build!");' > /tmp/test-app/index.js -tar -czf /tmp/source.tar.gz -C /tmp/test-app . - -# Submit build +# Option 1: Dockerfile in source tarball +tar -czf source.tar.gz Dockerfile package.json index.js + curl -X POST http://localhost:8083/builds \ -H "Authorization: Bearer $TOKEN" \ - -F "runtime=nodejs20" \ - -F "source=@/tmp/source.tar.gz" + -F "source=@source.tar.gz" + +# Option 2: Dockerfile as parameter +curl -X POST http://localhost:8083/builds \ + -H "Authorization: Bearer $TOKEN" \ + -F "source=@source.tar.gz" \ + -F "dockerfile=FROM node:20-alpine +WORKDIR /app +COPY . . +RUN npm ci +CMD [\"node\", \"index.js\"]" ``` -### 3. Check Build Status +### Example Dockerfiles -```bash -BUILD_ID="" -curl http://localhost:8083/builds/$BUILD_ID \ - -H "Authorization: Bearer $TOKEN" | jq +**Node.js:** +```dockerfile +FROM node:20-alpine +WORKDIR /app +COPY package*.json ./ +RUN npm ci +COPY . . +CMD ["node", "index.js"] ``` -### 4. Debug Failed Builds - -If the build fails, check the builder instance logs: +**Python:** +```dockerfile +FROM python:3.12-slim +WORKDIR /app +COPY requirements.txt ./ +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +CMD ["python", "main.py"] +``` -```bash -# Find the builder instance -curl http://localhost:8083/instances \ - -H "Authorization: Bearer $TOKEN" | jq '.[] | select(.name | startswith("builder-"))' +**Go:** +```dockerfile +FROM golang:1.22-alpine AS builder +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download +COPY . . +RUN CGO_ENABLED=0 go build -o main . + +FROM alpine:3.21 +COPY --from=builder /app/main /main +CMD ["/main"] +``` -# Get its logs -INSTANCE_ID="" -curl "http://localhost:8083/instances/$INSTANCE_ID/logs" \ - -H "Authorization: Bearer $TOKEN" +**Rust:** +```dockerfile +FROM rust:1.75 AS builder +WORKDIR /app +COPY Cargo.toml Cargo.lock ./ +COPY src ./src +RUN cargo build --release + +FROM debian:bookworm-slim +COPY --from=builder /app/target/release/myapp /myapp +CMD ["/myapp"] ``` -## Environment Variables +## Required Components -Builder images should configure these environment variables: +The generic builder image contains: + +| Component | Path | Purpose | +|-----------|------|---------| +| `buildctl` | `/usr/bin/buildctl` | BuildKit CLI | +| `buildctl-daemonless.sh` | `/usr/bin/buildctl-daemonless.sh` | Runs buildkitd + buildctl | +| `buildkitd` | `/usr/bin/buildkitd` | BuildKit daemon | +| `runc` | `/usr/bin/runc` | Container runtime | +| `builder-agent` | `/usr/bin/builder-agent` | Hypeman orchestration | +| `fuse-overlayfs` | System package | Rootless overlay filesystem | +| `git` | System package | Git operations (for go mod, etc.) | +| `curl` | System package | Network utilities | + +## Environment Variables | Variable | Value | Purpose | |----------|-------|---------| | `HOME` | `/home/builder` | User home directory | | `XDG_RUNTIME_DIR` | `/home/builder/.local/share` | Runtime directory for BuildKit | -| `BUILDKITD_FLAGS` | `""` (empty) | BuildKit daemon flags (cgroups are mounted in VM) | +| `BUILDKITD_FLAGS` | `""` (empty) | BuildKit daemon flags | ## MicroVM Runtime Environment -When the builder image runs inside a Hypeman microVM: +When the builder runs inside a Hypeman microVM: 1. **Volumes mounted**: - `/src` - Source code (read-write) - `/config/build.json` - Build configuration (read-only) -2. **Cgroups**: Mounted by init script at `/sys/fs/cgroup` (v2 preferred, v1 fallback) +2. **Cgroups**: Mounted at `/sys/fs/cgroup` 3. **Network**: Access to host registry via gateway IP `10.102.0.1` -4. **Registry**: HTTP (insecure) - agent adds `registry.insecure=true` flag +4. **Registry**: Uses HTTP (insecure) with `registry.insecure=true` ## Troubleshooting | Issue | Cause | Solution | |-------|-------|----------| -| `runc: not found` | Missing or wrong path | Copy `buildkit-runc` to `/usr/bin/runc` | -| `no cgroup mount found` | Cgroups not available | Ensure VM init script mounts cgroups | -| `fuse-overlayfs: not found` | Missing package | Add `fuse-overlayfs` to image | -| `permission denied` on buildkit | Wrong user/permissions | Run as non-root user with proper home dir | -| `can't enable NoProcessSandbox without Rootless` | Wrong BUILDKITD_FLAGS | Set `BUILDKITD_FLAGS=""` | - -## Adding a New Runtime - -To add support for a new runtime (e.g., Ruby, Go): - -1. Create `images/ruby32/Dockerfile` based on the template above -2. Add Dockerfile template in `templates/templates.go`: - ```go - var ruby32Template = `FROM {{.BaseImage}} - COPY . /app - WORKDIR /app - RUN bundle install - CMD ["ruby", "app.rb"] - ` - ``` -3. Register the generator in `templates/templates.go` -4. Build and push the builder image -5. Test with a sample project +| `manifest data is not v1.Manifest` | Image built with Docker v2 format | Rebuild with `docker buildx --output type=registry,oci-mediatypes=true` | +| Image import stuck on `pending`/`failed` | Manifest format incompatible | Check manifest format with `docker manifest inspect` | +| `Dockerfile required` | No Dockerfile in source or parameter | Include Dockerfile in tarball or pass as parameter | +| `401 Unauthorized` during push | Registry token issue | Check builder agent logs, verify token generation | +| `runc: not found` | BuildKit binaries missing | Rebuild the builder image | +| `no cgroup mount found` | Cgroups not available | Check VM init script | +| `fuse-overlayfs: not found` | Missing package | Rebuild image with fuse-overlayfs | +| `permission denied` | Wrong user/permissions | Ensure running as `builder` user | + +### Debugging Image Import Issues + +```bash +# Check image status +cat ~/hypeman_data_dir/images/docker.io/hirokernel/builder-generic/*/metadata.json | jq . + +# Check OCI cache for manifest format +cat ~/hypeman_data_dir/system/oci-cache/index.json | jq '.manifests[-1]' + +# Verify image on Docker Hub has OCI format +skopeo inspect --raw docker://hirokernel/builder-generic:latest | head -5 +``` + +If you see `application/vnd.docker.distribution.manifest.v2+json`, the image needs to be rebuilt with OCI format. + +## Migration from Runtime-Specific Images + +If you were using `nodejs20` or `python312` builder images: + +1. **Update your build requests** to include a Dockerfile +2. **The `runtime` parameter is deprecated** - you can still send it but it's ignored +3. **Configure `BUILDER_IMAGE`** to use the generic builder diff --git a/lib/builds/images/base/Dockerfile b/lib/builds/images/base/Dockerfile deleted file mode 100644 index 07c495f3..00000000 --- a/lib/builds/images/base/Dockerfile +++ /dev/null @@ -1,29 +0,0 @@ -# Base builder image with rootless BuildKit -# This serves as the foundation for all runtime-specific builder images - -FROM moby/buildkit:rootless - -# Switch to root temporarily to install additional packages -USER root - -# Install common build dependencies -RUN apk add --no-cache \ - ca-certificates \ - git \ - curl \ - jq \ - tar \ - gzip - -# Create directories for the builder agent -RUN mkdir -p /config /run/secrets - -# Switch back to unprivileged user -USER 1000 - -# Set buildkit flags for rootless operation -ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" - -# Default entrypoint is buildkitd, but we'll override for builder-agent -ENTRYPOINT ["/usr/bin/buildctl-daemonless.sh"] - diff --git a/lib/builds/images/nodejs20/Dockerfile b/lib/builds/images/generic/Dockerfile similarity index 58% rename from lib/builds/images/nodejs20/Dockerfile rename to lib/builds/images/generic/Dockerfile index aad38d0e..e1bc89c4 100644 --- a/lib/builds/images/nodejs20/Dockerfile +++ b/lib/builds/images/generic/Dockerfile @@ -1,5 +1,6 @@ -# Node.js 20 Builder Image -# Contains rootless BuildKit + Node.js toolchain + builder agent +# Generic Builder Image +# Contains rootless BuildKit + builder agent - runtime agnostic +# Users provide their own Dockerfile which specifies the runtime (node, python, etc.) FROM moby/buildkit:rootless AS buildkit @@ -18,10 +19,10 @@ COPY lib/builds/builder_agent/ ./lib/builds/builder_agent/ # Build the agent RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /builder-agent ./lib/builds/builder_agent -# Final builder image -FROM node:20-alpine +# Final builder image - minimal alpine base +FROM alpine:3.21 -# Copy BuildKit and runc from official image +# Copy BuildKit binaries from official image COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd @@ -30,37 +31,23 @@ COPY --from=buildkit /usr/bin/buildkit-runc /usr/bin/runc # Copy builder agent COPY --from=agent-builder /builder-agent /usr/bin/builder-agent -# Install additional dependencies +# Install minimal dependencies RUN apk add --no-cache \ ca-certificates \ git \ curl \ - jq \ - tar \ - gzip \ - shadow \ fuse-overlayfs -# Use existing node user (uid 1000) for rootless BuildKit -# Rename node -> builder for clarity and setup buildkit directories -RUN deluser --remove-home node 2>/dev/null || true && \ - adduser -D -u 1000 builder && \ - mkdir -p /home/builder/.local/share/buildkit && \ - chown -R builder:builder /home/builder - -# Create directories for build -RUN mkdir -p /config /run/secrets /src && \ - chown -R builder:builder /config /run/secrets /src - -# Enable corepack for pnpm/yarn support -RUN corepack enable +# Create unprivileged user for rootless BuildKit +RUN adduser -D -u 1000 builder && \ + mkdir -p /home/builder/.local/share/buildkit /config /run/secrets /src && \ + chown -R builder:builder /home/builder /config /run/secrets /src # Switch to unprivileged user USER builder WORKDIR /src # Set environment for buildkit in microVM -# Empty flags - use default buildkit behavior with cgroups ENV BUILDKITD_FLAGS="" ENV HOME=/home/builder ENV XDG_RUNTIME_DIR=/home/builder/.local/share @@ -68,3 +55,5 @@ ENV XDG_RUNTIME_DIR=/home/builder/.local/share # Run builder agent as entrypoint ENTRYPOINT ["/usr/bin/builder-agent"] + + diff --git a/lib/builds/images/python312/Dockerfile b/lib/builds/images/python312/Dockerfile deleted file mode 100644 index 8e3a9a31..00000000 --- a/lib/builds/images/python312/Dockerfile +++ /dev/null @@ -1,69 +0,0 @@ -# Python 3.12 Builder Image -# Contains rootless BuildKit + Python toolchain + builder agent - -FROM moby/buildkit:rootless AS buildkit - -# Build the builder-agent (multi-stage build from hypeman repo) -FROM golang:1.25-alpine AS agent-builder - -WORKDIR /app - -# Copy go.mod and go.sum first for better layer caching -COPY go.mod go.sum ./ -RUN go mod download - -# Copy only the builder_agent source -COPY lib/builds/builder_agent/ ./lib/builds/builder_agent/ - -# Build the agent -RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /builder-agent ./lib/builds/builder_agent - -# Final builder image -FROM python:3.12-slim - -# Copy BuildKit from official image -COPY --from=buildkit /usr/bin/buildctl /usr/bin/buildctl -COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonless.sh -COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd - -# Copy builder agent -COPY --from=agent-builder /builder-agent /usr/bin/builder-agent - -# Install additional dependencies -RUN apt-get update && apt-get install -y --no-install-recommends \ - ca-certificates \ - git \ - curl \ - jq \ - tar \ - gzip \ - fuse-overlayfs \ - && rm -rf /var/lib/apt/lists/* - -# Create unprivileged user for rootless BuildKit -RUN useradd -m -u 1000 builder && \ - mkdir -p /home/builder/.local/share/buildkit && \ - chown -R builder:builder /home/builder - -# Create directories for build -RUN mkdir -p /config /run/secrets /src && \ - chown -R builder:builder /config /run/secrets /src - -# Install common Python tools -RUN pip install --no-cache-dir \ - pip-tools \ - poetry \ - pipenv - -# Switch to unprivileged user -USER builder -WORKDIR /src - -# Set environment for rootless buildkit -ENV BUILDKITD_FLAGS="--oci-worker-no-process-sandbox" -ENV HOME=/home/builder -ENV XDG_RUNTIME_DIR=/home/builder/.local/share - -# Run builder agent as entrypoint -ENTRYPOINT ["/usr/bin/builder-agent"] - diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 0eaac3e7..e5d0f2fd 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -60,6 +60,10 @@ type Config struct { // DefaultTimeout is the default build timeout in seconds DefaultTimeout int + + // RegistrySecret is the secret used to sign registry access tokens + // This should be the same secret used by the registry middleware + RegistrySecret string } // DefaultConfig returns the default build manager configuration @@ -79,6 +83,7 @@ type manager struct { instanceManager instances.Manager volumeManager volumes.Manager secretProvider SecretProvider + tokenGenerator *RegistryTokenGenerator logger *slog.Logger metrics *Metrics createMu sync.Mutex @@ -105,6 +110,7 @@ func NewManager( instanceManager: instanceMgr, volumeManager: volumeMgr, secretProvider: secretProvider, + tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), logger: logger, } @@ -176,12 +182,29 @@ func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourc return nil, fmt.Errorf("store source: %w", err) } + // Generate scoped registry token for this build + // Token grants push access to the build output repo and cache repo + allowedRepos := []string{fmt.Sprintf("builds/%s", id)} + if req.CacheScope != "" { + allowedRepos = append(allowedRepos, fmt.Sprintf("cache/%s", req.CacheScope)) + } + tokenTTL := time.Duration(policy.TimeoutSeconds) * time.Second + if tokenTTL < 30*time.Minute { + tokenTTL = 30 * time.Minute // Minimum 30 minutes + } + registryToken, err := m.tokenGenerator.GeneratePushToken(id, allowedRepos, tokenTTL) + if err != nil { + deleteBuild(m.paths, id) + return nil, fmt.Errorf("generate registry token: %w", err) + } + // Write build config for the builder agent buildConfig := &BuildConfig{ JobID: id, Runtime: req.Runtime, BaseImageDigest: req.BaseImageDigest, RegistryURL: m.config.RegistryURL, + RegistryToken: registryToken, CacheScope: req.CacheScope, SourcePath: "/src", Dockerfile: req.Dockerfile, @@ -249,6 +272,13 @@ func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildReques return } + // Save build logs (regardless of success/failure) + if result.Logs != "" { + if err := appendLog(m.paths, id, []byte(result.Logs)); err != nil { + m.logger.Warn("failed to save build logs", "id", id, "error", err) + } + } + if !result.Success { m.logger.Error("build failed", "id", id, "error", result.Error, "duration", duration) m.updateBuildComplete(id, StatusFailed, nil, &result.Error, &result.Provenance, &durationMS) diff --git a/lib/builds/registry_token.go b/lib/builds/registry_token.go new file mode 100644 index 00000000..e9d1a32e --- /dev/null +++ b/lib/builds/registry_token.go @@ -0,0 +1,108 @@ +// Package builds implements registry token generation for secure builder VM authentication. +package builds + +import ( + "fmt" + "time" + + "github.com/golang-jwt/jwt/v5" +) + +// RegistryTokenClaims contains the claims for a scoped registry access token. +// These tokens are issued to builder VMs to grant limited push access to specific repositories. +type RegistryTokenClaims struct { + jwt.RegisteredClaims + + // BuildID is the build job identifier for audit purposes + BuildID string `json:"build_id"` + + // Repositories is the list of allowed repository paths (e.g., ["builds/abc123", "cache/tenant-x"]) + Repositories []string `json:"repos"` + + // Scope is the access scope: "push" for write access, "pull" for read-only + Scope string `json:"scope"` +} + +// RegistryTokenGenerator creates scoped registry access tokens +type RegistryTokenGenerator struct { + secret []byte +} + +// NewRegistryTokenGenerator creates a new token generator with the given secret +func NewRegistryTokenGenerator(secret string) *RegistryTokenGenerator { + return &RegistryTokenGenerator{ + secret: []byte(secret), + } +} + +// GeneratePushToken creates a short-lived token granting push access to specific repositories. +// The token expires after the specified duration (typically matching the build timeout). +func (g *RegistryTokenGenerator) GeneratePushToken(buildID string, repos []string, ttl time.Duration) (string, error) { + if buildID == "" { + return "", fmt.Errorf("build ID is required") + } + if len(repos) == 0 { + return "", fmt.Errorf("at least one repository is required") + } + + now := time.Now() + claims := RegistryTokenClaims{ + RegisteredClaims: jwt.RegisteredClaims{ + Subject: "builder-" + buildID, + IssuedAt: jwt.NewNumericDate(now), + ExpiresAt: jwt.NewNumericDate(now.Add(ttl)), + Issuer: "hypeman", + }, + BuildID: buildID, + Repositories: repos, + Scope: "push", + } + + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + return token.SignedString(g.secret) +} + +// ValidateToken parses and validates a registry token, returning the claims if valid. +func (g *RegistryTokenGenerator) ValidateToken(tokenString string) (*RegistryTokenClaims, error) { + token, err := jwt.ParseWithClaims(tokenString, &RegistryTokenClaims{}, func(token *jwt.Token) (interface{}, error) { + // Validate signing method + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + return g.secret, nil + }) + + if err != nil { + return nil, fmt.Errorf("parse token: %w", err) + } + + claims, ok := token.Claims.(*RegistryTokenClaims) + if !ok || !token.Valid { + return nil, fmt.Errorf("invalid token") + } + + return claims, nil +} + +// IsRepositoryAllowed checks if the given repository path is allowed by the token claims. +func (c *RegistryTokenClaims) IsRepositoryAllowed(repo string) bool { + for _, allowed := range c.Repositories { + if allowed == repo { + return true + } + } + return false +} + +// IsPushAllowed returns true if the token grants push (write) access. +func (c *RegistryTokenClaims) IsPushAllowed() bool { + return c.Scope == "push" +} + +// IsPullAllowed returns true if the token grants pull (read) access. +// Push tokens also implicitly grant pull access. +func (c *RegistryTokenClaims) IsPullAllowed() bool { + return c.Scope == "push" || c.Scope == "pull" +} + + diff --git a/lib/builds/registry_token_test.go b/lib/builds/registry_token_test.go new file mode 100644 index 00000000..3c376462 --- /dev/null +++ b/lib/builds/registry_token_test.go @@ -0,0 +1,113 @@ +package builds + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRegistryTokenGenerator_GeneratePushToken(t *testing.T) { + generator := NewRegistryTokenGenerator("test-secret-key") + + t.Run("valid token generation", func(t *testing.T) { + token, err := generator.GeneratePushToken("build-123", []string{"builds/build-123", "cache/tenant-x"}, 30*time.Minute) + require.NoError(t, err) + assert.NotEmpty(t, token) + + // Validate the token + claims, err := generator.ValidateToken(token) + require.NoError(t, err) + assert.Equal(t, "build-123", claims.BuildID) + assert.Equal(t, []string{"builds/build-123", "cache/tenant-x"}, claims.Repositories) + assert.Equal(t, "push", claims.Scope) + assert.Equal(t, "builder-build-123", claims.Subject) + assert.Equal(t, "hypeman", claims.Issuer) + }) + + t.Run("empty build ID", func(t *testing.T) { + _, err := generator.GeneratePushToken("", []string{"builds/build-123"}, 30*time.Minute) + require.Error(t, err) + assert.Contains(t, err.Error(), "build ID is required") + }) + + t.Run("empty repositories", func(t *testing.T) { + _, err := generator.GeneratePushToken("build-123", []string{}, 30*time.Minute) + require.Error(t, err) + assert.Contains(t, err.Error(), "at least one repository is required") + }) +} + +func TestRegistryTokenGenerator_ValidateToken(t *testing.T) { + generator := NewRegistryTokenGenerator("test-secret-key") + + t.Run("valid token", func(t *testing.T) { + token, err := generator.GeneratePushToken("build-abc", []string{"builds/build-abc"}, time.Hour) + require.NoError(t, err) + + claims, err := generator.ValidateToken(token) + require.NoError(t, err) + assert.Equal(t, "build-abc", claims.BuildID) + }) + + t.Run("expired token", func(t *testing.T) { + // Generate a token that expires immediately + token, err := generator.GeneratePushToken("build-expired", []string{"builds/build-expired"}, -time.Hour) + require.NoError(t, err) + + _, err = generator.ValidateToken(token) + require.Error(t, err) + assert.Contains(t, err.Error(), "token is expired") + }) + + t.Run("invalid signature", func(t *testing.T) { + // Generate with one secret + gen1 := NewRegistryTokenGenerator("secret-1") + token, err := gen1.GeneratePushToken("build-123", []string{"builds/build-123"}, time.Hour) + require.NoError(t, err) + + // Validate with different secret + gen2 := NewRegistryTokenGenerator("secret-2") + _, err = gen2.ValidateToken(token) + require.Error(t, err) + assert.Contains(t, err.Error(), "signature is invalid") + }) + + t.Run("malformed token", func(t *testing.T) { + _, err := generator.ValidateToken("not.a.valid.jwt.token") + require.Error(t, err) + }) +} + +func TestRegistryTokenClaims_IsRepositoryAllowed(t *testing.T) { + claims := &RegistryTokenClaims{ + Repositories: []string{"builds/abc123", "cache/tenant-x"}, + } + + t.Run("allowed repo", func(t *testing.T) { + assert.True(t, claims.IsRepositoryAllowed("builds/abc123")) + assert.True(t, claims.IsRepositoryAllowed("cache/tenant-x")) + }) + + t.Run("not allowed repo", func(t *testing.T) { + assert.False(t, claims.IsRepositoryAllowed("builds/other")) + assert.False(t, claims.IsRepositoryAllowed("cache/other-tenant")) + }) +} + +func TestRegistryTokenClaims_IsPushAllowed(t *testing.T) { + t.Run("push scope", func(t *testing.T) { + claims := &RegistryTokenClaims{Scope: "push"} + assert.True(t, claims.IsPushAllowed()) + assert.True(t, claims.IsPullAllowed()) // push implies pull + }) + + t.Run("pull scope", func(t *testing.T) { + claims := &RegistryTokenClaims{Scope: "pull"} + assert.False(t, claims.IsPushAllowed()) + assert.True(t, claims.IsPullAllowed()) + }) +} + + diff --git a/lib/builds/templates/templates.go b/lib/builds/templates/templates.go deleted file mode 100644 index 92611d85..00000000 --- a/lib/builds/templates/templates.go +++ /dev/null @@ -1,230 +0,0 @@ -// Package templates provides Dockerfile generation for different runtimes. -package templates - -import ( - "fmt" - "os" - "path/filepath" - "strings" -) - -// Generator generates Dockerfiles for a specific runtime -type Generator interface { - // Generate creates a Dockerfile for the given source directory - Generate(sourceDir string, baseImageDigest string) (string, error) - - // DetectLockfile returns the detected lockfile type and path - DetectLockfile(sourceDir string) (string, string) -} - -// GetGenerator returns a Generator for the given runtime -func GetGenerator(runtime string) (Generator, error) { - switch runtime { - case "nodejs20": - return &NodeJSGenerator{Version: "20"}, nil - case "python312": - return &PythonGenerator{Version: "3.12"}, nil - default: - return nil, fmt.Errorf("unsupported runtime: %s", runtime) - } -} - -// NodeJSGenerator generates Dockerfiles for Node.js applications -type NodeJSGenerator struct { - Version string -} - -// DetectLockfile detects which package manager lockfile is present -func (g *NodeJSGenerator) DetectLockfile(sourceDir string) (string, string) { - lockfiles := []struct { - name string - manager string - }{ - {"pnpm-lock.yaml", "pnpm"}, - {"yarn.lock", "yarn"}, - {"package-lock.json", "npm"}, - } - - for _, lf := range lockfiles { - path := filepath.Join(sourceDir, lf.name) - if _, err := os.Stat(path); err == nil { - return lf.manager, lf.name - } - } - - return "npm", "package-lock.json" -} - -// Generate creates a Dockerfile for a Node.js application -func (g *NodeJSGenerator) Generate(sourceDir string, baseImageDigest string) (string, error) { - manager, lockfile := g.DetectLockfile(sourceDir) - - // Determine base image - baseImage := baseImageDigest - if baseImage == "" { - baseImage = fmt.Sprintf("node:%s-alpine", g.Version) - } - - // Determine install command based on package manager - var installCmd string - switch manager { - case "pnpm": - installCmd = "corepack enable && pnpm install --frozen-lockfile" - case "yarn": - installCmd = "yarn install --frozen-lockfile" - default: - installCmd = "npm ci" - } - - // Check if package.json exists - if _, err := os.Stat(filepath.Join(sourceDir, "package.json")); err != nil { - return "", fmt.Errorf("package.json not found in source directory") - } - - // Detect entry point - entryPoint := detectNodeEntryPoint(sourceDir) - - dockerfile := fmt.Sprintf(`FROM %s - -WORKDIR /app - -# Copy dependency files first (cache layer) -COPY package.json %s ./ - -# Install dependencies (strict mode from lockfile) -RUN %s - -# Copy application source -COPY . . - -# Default command -CMD ["node", "%s"] -`, baseImage, lockfile, installCmd, entryPoint) - - return dockerfile, nil -} - -// detectNodeEntryPoint tries to detect the entry point for a Node.js app -func detectNodeEntryPoint(sourceDir string) string { - // Check common entry points - candidates := []string{"index.js", "src/index.js", "main.js", "app.js", "server.js"} - for _, candidate := range candidates { - if _, err := os.Stat(filepath.Join(sourceDir, candidate)); err == nil { - return candidate - } - } - return "index.js" -} - -// PythonGenerator generates Dockerfiles for Python applications -type PythonGenerator struct { - Version string -} - -// DetectLockfile detects which Python dependency file is present -func (g *PythonGenerator) DetectLockfile(sourceDir string) (string, string) { - lockfiles := []struct { - name string - manager string - }{ - {"poetry.lock", "poetry"}, - {"Pipfile.lock", "pipenv"}, - {"requirements.txt", "pip"}, - } - - for _, lf := range lockfiles { - path := filepath.Join(sourceDir, lf.name) - if _, err := os.Stat(path); err == nil { - return lf.manager, lf.name - } - } - - return "pip", "requirements.txt" -} - -// Generate creates a Dockerfile for a Python application -func (g *PythonGenerator) Generate(sourceDir string, baseImageDigest string) (string, error) { - manager, lockfile := g.DetectLockfile(sourceDir) - - // Determine base image - baseImage := baseImageDigest - if baseImage == "" { - baseImage = fmt.Sprintf("python:%s-slim", g.Version) - } - - var installCmd string - var copyFiles string - - switch manager { - case "poetry": - // Poetry requires pyproject.toml and poetry.lock - copyFiles = "pyproject.toml poetry.lock" - installCmd = `pip install poetry && \ - poetry config virtualenvs.create false && \ - poetry install --no-dev --no-interaction --no-ansi` - case "pipenv": - copyFiles = "Pipfile Pipfile.lock" - installCmd = `pip install pipenv && \ - pipenv install --system --deploy --ignore-pipfile` - default: - // Check if requirements.txt has hashes for strict mode - hasHashes := checkRequirementsHasHashes(sourceDir) - copyFiles = "requirements.txt" - if hasHashes { - // Strict mode: require hashes, prefer binary packages - installCmd = "pip install --require-hashes --only-binary :all: -r requirements.txt" - } else { - installCmd = "pip install --no-cache-dir -r requirements.txt" - } - } - - // Check if lockfile exists - if _, err := os.Stat(filepath.Join(sourceDir, lockfile)); err != nil { - return "", fmt.Errorf("%s not found in source directory", lockfile) - } - - // Detect entry point - entryPoint := detectPythonEntryPoint(sourceDir) - - dockerfile := fmt.Sprintf(`FROM %s - -WORKDIR /app - -# Copy dependency files first (cache layer) -COPY %s ./ - -# Install dependencies -RUN %s - -# Copy application source -COPY . . - -# Default command -CMD ["python", "%s"] -`, baseImage, copyFiles, installCmd, entryPoint) - - return dockerfile, nil -} - -// checkRequirementsHasHashes checks if requirements.txt contains hash pins -func checkRequirementsHasHashes(sourceDir string) bool { - reqPath := filepath.Join(sourceDir, "requirements.txt") - data, err := os.ReadFile(reqPath) - if err != nil { - return false - } - return strings.Contains(string(data), "--hash=") -} - -// detectPythonEntryPoint tries to detect the entry point for a Python app -func detectPythonEntryPoint(sourceDir string) string { - // Check common entry points - candidates := []string{"main.py", "app.py", "run.py", "server.py", "src/main.py"} - for _, candidate := range candidates { - if _, err := os.Stat(filepath.Join(sourceDir, candidate)); err == nil { - return candidate - } - } - return "main.py" -} - diff --git a/lib/builds/templates/templates_test.go b/lib/builds/templates/templates_test.go deleted file mode 100644 index 55472ec4..00000000 --- a/lib/builds/templates/templates_test.go +++ /dev/null @@ -1,180 +0,0 @@ -package templates - -import ( - "os" - "path/filepath" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestGetGenerator(t *testing.T) { - tests := []struct { - runtime string - wantErr bool - }{ - {"nodejs20", false}, - {"python312", false}, - {"ruby", true}, - {"java", true}, - {"", true}, - } - - for _, tt := range tests { - t.Run(tt.runtime, func(t *testing.T) { - gen, err := GetGenerator(tt.runtime) - if tt.wantErr { - assert.Error(t, err) - assert.Nil(t, gen) - } else { - assert.NoError(t, err) - assert.NotNil(t, gen) - } - }) - } -} - -func TestNodeJSGenerator_DetectLockfile(t *testing.T) { - // Create temp directory - tmpDir := t.TempDir() - - gen := &NodeJSGenerator{Version: "20"} - - // Default to npm when no lockfile - manager, lockfile := gen.DetectLockfile(tmpDir) - assert.Equal(t, "npm", manager) - assert.Equal(t, "package-lock.json", lockfile) - - // Detect pnpm - os.WriteFile(filepath.Join(tmpDir, "pnpm-lock.yaml"), []byte{}, 0644) - manager, lockfile = gen.DetectLockfile(tmpDir) - assert.Equal(t, "pnpm", manager) - assert.Equal(t, "pnpm-lock.yaml", lockfile) - - // Remove pnpm, add yarn - os.Remove(filepath.Join(tmpDir, "pnpm-lock.yaml")) - os.WriteFile(filepath.Join(tmpDir, "yarn.lock"), []byte{}, 0644) - manager, lockfile = gen.DetectLockfile(tmpDir) - assert.Equal(t, "yarn", manager) - assert.Equal(t, "yarn.lock", lockfile) -} - -func TestNodeJSGenerator_Generate(t *testing.T) { - tmpDir := t.TempDir() - - // Create package.json - err := os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte(`{"name": "test"}`), 0644) - require.NoError(t, err) - - // Create package-lock.json - err = os.WriteFile(filepath.Join(tmpDir, "package-lock.json"), []byte(`{}`), 0644) - require.NoError(t, err) - - // Create index.js - err = os.WriteFile(filepath.Join(tmpDir, "index.js"), []byte(`console.log("hello")`), 0644) - require.NoError(t, err) - - gen := &NodeJSGenerator{Version: "20"} - dockerfile, err := gen.Generate(tmpDir, "") - require.NoError(t, err) - - // Check Dockerfile contents - assert.Contains(t, dockerfile, "FROM node:20-alpine") - assert.Contains(t, dockerfile, "npm ci") - assert.Contains(t, dockerfile, "COPY package.json package-lock.json") - assert.Contains(t, dockerfile, "CMD [\"node\", \"index.js\"]") -} - -func TestNodeJSGenerator_GenerateWithCustomBase(t *testing.T) { - tmpDir := t.TempDir() - - os.WriteFile(filepath.Join(tmpDir, "package.json"), []byte(`{}`), 0644) - os.WriteFile(filepath.Join(tmpDir, "package-lock.json"), []byte(`{}`), 0644) - - gen := &NodeJSGenerator{Version: "20"} - dockerfile, err := gen.Generate(tmpDir, "node@sha256:abc123") - require.NoError(t, err) - - assert.Contains(t, dockerfile, "FROM node@sha256:abc123") -} - -func TestNodeJSGenerator_MissingPackageJson(t *testing.T) { - tmpDir := t.TempDir() - - gen := &NodeJSGenerator{Version: "20"} - _, err := gen.Generate(tmpDir, "") - assert.Error(t, err) - assert.Contains(t, err.Error(), "package.json not found") -} - -func TestPythonGenerator_DetectLockfile(t *testing.T) { - tmpDir := t.TempDir() - - gen := &PythonGenerator{Version: "3.12"} - - // Default to pip when no lockfile - manager, lockfile := gen.DetectLockfile(tmpDir) - assert.Equal(t, "pip", manager) - assert.Equal(t, "requirements.txt", lockfile) - - // Detect poetry - os.WriteFile(filepath.Join(tmpDir, "poetry.lock"), []byte{}, 0644) - manager, lockfile = gen.DetectLockfile(tmpDir) - assert.Equal(t, "poetry", manager) - assert.Equal(t, "poetry.lock", lockfile) - - // Remove poetry, add pipenv - os.Remove(filepath.Join(tmpDir, "poetry.lock")) - os.WriteFile(filepath.Join(tmpDir, "Pipfile.lock"), []byte{}, 0644) - manager, lockfile = gen.DetectLockfile(tmpDir) - assert.Equal(t, "pipenv", manager) - assert.Equal(t, "Pipfile.lock", lockfile) -} - -func TestPythonGenerator_Generate(t *testing.T) { - tmpDir := t.TempDir() - - // Create requirements.txt - err := os.WriteFile(filepath.Join(tmpDir, "requirements.txt"), []byte("flask==2.0.0"), 0644) - require.NoError(t, err) - - // Create main.py - err = os.WriteFile(filepath.Join(tmpDir, "main.py"), []byte(`print("hello")`), 0644) - require.NoError(t, err) - - gen := &PythonGenerator{Version: "3.12"} - dockerfile, err := gen.Generate(tmpDir, "") - require.NoError(t, err) - - assert.Contains(t, dockerfile, "FROM python:3.12-slim") - assert.Contains(t, dockerfile, "pip install --no-cache-dir -r requirements.txt") - assert.Contains(t, dockerfile, "COPY requirements.txt") - assert.Contains(t, dockerfile, "CMD [\"python\", \"main.py\"]") -} - -func TestPythonGenerator_GenerateWithHashes(t *testing.T) { - tmpDir := t.TempDir() - - // Create requirements.txt with hashes - err := os.WriteFile(filepath.Join(tmpDir, "requirements.txt"), []byte(`flask==2.0.0 --hash=sha256:abc123`), 0644) - require.NoError(t, err) - - gen := &PythonGenerator{Version: "3.12"} - dockerfile, err := gen.Generate(tmpDir, "") - require.NoError(t, err) - - // Should use strict mode with hashes - assert.Contains(t, dockerfile, "--require-hashes") - assert.Contains(t, dockerfile, "--only-binary") -} - -func TestPythonGenerator_MissingRequirements(t *testing.T) { - tmpDir := t.TempDir() - - gen := &PythonGenerator{Version: "3.12"} - _, err := gen.Generate(tmpDir, "") - assert.Error(t, err) - assert.Contains(t, err.Error(), "requirements.txt not found") -} - diff --git a/lib/builds/types.go b/lib/builds/types.go index 2fc47831..7ee5b821 100644 --- a/lib/builds/types.go +++ b/lib/builds/types.go @@ -14,10 +14,12 @@ const ( StatusCancelled = "cancelled" ) -// Runtime constants for supported build runtimes +// Runtime constants (deprecated - kept for backward compatibility) +// The generic builder system no longer requires runtime selection. +// Users provide their own Dockerfile which specifies the runtime. const ( - RuntimeNodeJS20 = "nodejs20" - RuntimePython312 = "python312" + RuntimeNodeJS20 = "nodejs20" // Deprecated + RuntimePython312 = "python312" // Deprecated ) // Build represents a source-to-image build job @@ -38,8 +40,13 @@ type Build struct { // CreateBuildRequest represents a request to create a new build type CreateBuildRequest struct { - // Runtime specifies the build runtime (e.g., nodejs20, python312) - Runtime string `json:"runtime"` + // Runtime is deprecated. Kept for backward compatibility but no longer required. + // The generic builder system accepts any Dockerfile. + Runtime string `json:"runtime,omitempty"` + + // Dockerfile content. Required if not included in the source tarball. + // The Dockerfile specifies the runtime (e.g., FROM node:20-alpine). + Dockerfile string `json:"dockerfile,omitempty"` // BaseImageDigest optionally pins the base image by digest for reproducibility BaseImageDigest string `json:"base_image_digest,omitempty"` @@ -53,9 +60,6 @@ type CreateBuildRequest struct { // CacheScope is the tenant-specific cache key prefix for isolation CacheScope string `json:"cache_scope,omitempty"` - // Dockerfile is an optional custom Dockerfile (if not provided, one is generated) - Dockerfile string `json:"dockerfile,omitempty"` - // BuildArgs are ARG values to pass to the Dockerfile BuildArgs map[string]string `json:"build_args,omitempty"` @@ -119,8 +123,11 @@ type BuildConfig struct { // JobID is the build job identifier JobID string `json:"job_id"` - // Runtime is the build runtime (nodejs20, python312) - Runtime string `json:"runtime"` + // Runtime is deprecated, kept for logging purposes only + Runtime string `json:"runtime,omitempty"` + + // Dockerfile content (if not provided in source tarball) + Dockerfile string `json:"dockerfile,omitempty"` // BaseImageDigest optionally pins the base image BaseImageDigest string `json:"base_image_digest,omitempty"` @@ -128,15 +135,16 @@ type BuildConfig struct { // RegistryURL is where to push the built image RegistryURL string `json:"registry_url"` + // RegistryToken is a short-lived JWT granting push access to specific repositories. + // The builder agent uses this token to authenticate with the registry. + RegistryToken string `json:"registry_token,omitempty"` + // CacheScope is the tenant-specific cache key prefix CacheScope string `json:"cache_scope,omitempty"` // SourcePath is the path to source in the guest (typically /src) SourcePath string `json:"source_path"` - // Dockerfile is an optional custom Dockerfile content - Dockerfile string `json:"dockerfile,omitempty"` - // BuildArgs are ARG values for the Dockerfile BuildArgs map[string]string `json:"build_args,omitempty"` diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 496b8217..d0158a12 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -2,8 +2,10 @@ package middleware import ( "context" + "encoding/base64" "fmt" "net/http" + "regexp" "strings" "github.com/getkin/kin-openapi/openapi3filter" @@ -15,6 +17,18 @@ type contextKey string const userIDKey contextKey = "user_id" +// registryPathPattern matches /v2/{repository}/... paths +var registryPathPattern = regexp.MustCompile(`^/v2/([^/]+(?:/[^/]+)?)/`) + +// RegistryTokenClaims contains the claims for a scoped registry access token. +// This mirrors the type in lib/builds/registry_token.go to avoid circular imports. +type RegistryTokenClaims struct { + jwt.RegisteredClaims + BuildID string `json:"build_id"` + Repositories []string `json:"repos"` + Scope string `json:"scope"` +} + // OapiAuthenticationFunc creates an AuthenticationFunc compatible with nethttp-middleware // that validates JWT bearer tokens for endpoints with security requirements. func OapiAuthenticationFunc(jwtSecret string) openapi3filter.AuthenticationFunc { @@ -108,6 +122,37 @@ func extractBearerToken(authHeader string) (string, error) { return parts[1], nil } +// extractTokenFromAuth extracts a JWT token from either Bearer or Basic auth headers. +// For Bearer: returns the token directly +// For Basic: decodes base64 and returns the username part (BuildKit sends JWT as username) +func extractTokenFromAuth(authHeader string) (string, string, error) { + parts := strings.SplitN(authHeader, " ", 2) + if len(parts) != 2 { + return "", "", fmt.Errorf("invalid authorization header format") + } + + scheme := strings.ToLower(parts[0]) + switch scheme { + case "bearer": + return parts[1], "bearer", nil + case "basic": + // Decode base64 + decoded, err := base64.StdEncoding.DecodeString(parts[1]) + if err != nil { + return "", "", fmt.Errorf("invalid basic auth encoding: %w", err) + } + // Split on colon to get username:password + credentials := strings.SplitN(string(decoded), ":", 2) + if len(credentials) == 0 { + return "", "", fmt.Errorf("invalid basic auth format") + } + // The JWT is the username part + return credentials[0], "basic", nil + default: + return "", "", fmt.Errorf("unsupported authorization scheme: %s", scheme) + } +} + // GetUserIDFromContext extracts the user ID from context func GetUserIDFromContext(ctx context.Context) string { if userID, ok := ctx.Value(userIDKey).(string); ok { @@ -116,8 +161,13 @@ func GetUserIDFromContext(ctx context.Context) string { return "" } +// isRegistryPath checks if the request is for the OCI registry endpoints (/v2/...) +func isRegistryPath(path string) bool { + return strings.HasPrefix(path, "/v2/") +} + // isInternalVMRequest checks if the request is from an internal VM network (10.102.x.x) -// This is used to allow builder VMs to push images without authentication +// This is used as a fallback for builder VMs that don't have token auth yet func isInternalVMRequest(r *http.Request) bool { // Get the real client IP (RealIP middleware sets X-Real-IP) ip := r.Header.Get("X-Real-IP") @@ -134,24 +184,128 @@ func isInternalVMRequest(r *http.Request) bool { return strings.HasPrefix(ip, "10.102.") } +// extractRepoFromPath extracts the repository name from a registry path. +// e.g., "/v2/builds/abc123/manifests/latest" -> "builds/abc123" +func extractRepoFromPath(path string) string { + matches := registryPathPattern.FindStringSubmatch(path) + if len(matches) >= 2 { + return matches[1] + } + return "" +} + +// isWriteOperation returns true if the HTTP method implies a write operation +func isWriteOperation(method string) bool { + return method == http.MethodPut || method == http.MethodPost || method == http.MethodPatch || method == http.MethodDelete +} + +// validateRegistryToken validates a registry-scoped JWT token and checks repository access. +// Returns the claims if valid, nil otherwise. +func validateRegistryToken(tokenString, jwtSecret, requestPath, method string) (*RegistryTokenClaims, error) { + token, err := jwt.ParseWithClaims(tokenString, &RegistryTokenClaims{}, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + return []byte(jwtSecret), nil + }) + + if err != nil { + return nil, fmt.Errorf("parse token: %w", err) + } + + claims, ok := token.Claims.(*RegistryTokenClaims) + if !ok || !token.Valid { + return nil, fmt.Errorf("invalid token") + } + + // Check if this is a registry token (has repos claim) + if len(claims.Repositories) == 0 { + return nil, fmt.Errorf("not a registry token") + } + + // Extract repository from request path + repo := extractRepoFromPath(requestPath) + if repo == "" { + // Allow /v2/ (base path check) without repo validation + if requestPath == "/v2/" || requestPath == "/v2" { + return claims, nil + } + return nil, fmt.Errorf("could not extract repository from path") + } + + // Check if the repository is allowed by the token + allowed := false + for _, allowedRepo := range claims.Repositories { + if allowedRepo == repo { + allowed = true + break + } + } + if !allowed { + return nil, fmt.Errorf("repository %s not allowed by token", repo) + } + + // Check scope for write operations + if isWriteOperation(method) && claims.Scope != "push" { + return nil, fmt.Errorf("token does not allow write operations") + } + + return claims, nil +} + // JwtAuth creates a chi middleware that validates JWT bearer tokens func JwtAuth(jwtSecret string) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log := logger.FromContext(r.Context()) - // Allow internal VM network (10.102.x.x) to bypass auth for registry pushes - // This enables builder VMs to push images without authentication - if isInternalVMRequest(r) { - log.DebugContext(r.Context(), "allowing internal VM request without auth", "remote_addr", r.RemoteAddr) - // Set a system user ID for internal requests - ctx := context.WithValue(r.Context(), userIDKey, "internal-builder") - next.ServeHTTP(w, r.WithContext(ctx)) + // Extract token from Authorization header + authHeader := r.Header.Get("Authorization") + + // For registry paths, handle specially to support both Bearer and Basic auth + if isRegistryPath(r.URL.Path) { + if authHeader != "" { + // Try to extract token (supports both Bearer and Basic auth) + token, authType, err := extractTokenFromAuth(authHeader) + if err == nil { + log.DebugContext(r.Context(), "extracted token for registry request", "auth_type", authType) + + // Try to validate as a registry-scoped token + registryClaims, err := validateRegistryToken(token, jwtSecret, r.URL.Path, r.Method) + if err == nil { + // Valid registry token - set build ID as user for audit trail + log.DebugContext(r.Context(), "registry token validated", + "build_id", registryClaims.BuildID, + "repos", registryClaims.Repositories, + "scope", registryClaims.Scope) + ctx := context.WithValue(r.Context(), userIDKey, "builder-"+registryClaims.BuildID) + next.ServeHTTP(w, r.WithContext(ctx)) + return + } + log.DebugContext(r.Context(), "registry token validation failed", "error", err) + } else { + log.DebugContext(r.Context(), "failed to extract token", "error", err) + } + } + + // Fallback: Allow internal VM network (10.102.x.x) for registry pushes + // This is a transitional fallback for older builder images without token auth + if isInternalVMRequest(r) { + log.DebugContext(r.Context(), "allowing internal VM request via IP fallback (deprecated)", + "remote_addr", r.RemoteAddr, + "path", r.URL.Path) + ctx := context.WithValue(r.Context(), userIDKey, "internal-builder-legacy") + next.ServeHTTP(w, r.WithContext(ctx)) + return + } + + // Registry auth failed + log.DebugContext(r.Context(), "registry request unauthorized", "remote_addr", r.RemoteAddr) + OapiErrorHandler(w, "registry authentication required", http.StatusUnauthorized) return } - // Extract token from Authorization header - authHeader := r.Header.Get("Authorization") + // For non-registry paths, require Bearer token if authHeader == "" { log.DebugContext(r.Context(), "missing authorization header") OapiErrorHandler(w, "authorization header required", http.StatusUnauthorized) @@ -166,7 +320,7 @@ func JwtAuth(jwtSecret string) func(http.Handler) http.Handler { return } - // Parse and validate JWT + // Parse and validate as regular user JWT claims := jwt.MapClaims{} parsedToken, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) { // Validate signing method diff --git a/lib/oapi/oapi.go b/lib/oapi/oapi.go index 7ab98508..b8902f02 100644 --- a/lib/oapi/oapi.go +++ b/lib/oapi/oapi.go @@ -70,12 +70,6 @@ const ( Unknown InstanceState = "Unknown" ) -// Defines values for CreateBuildMultipartBodyRuntime. -const ( - Nodejs20 CreateBuildMultipartBodyRuntime = "nodejs20" - Python312 CreateBuildMultipartBodyRuntime = "python312" -) - // Defines values for GetInstanceLogsParamsSource. const ( App GetInstanceLogsParamsSource = "app" @@ -143,8 +137,8 @@ type Build struct { // QueuePosition Position in build queue (only when status is queued) QueuePosition *int `json:"queue_position"` - // Runtime Build runtime - Runtime string `json:"runtime"` + // Runtime (Deprecated) Build runtime hint + Runtime *string `json:"runtime,omitempty"` // StartedAt Build start timestamp StartedAt *time.Time `json:"started_at"` @@ -547,22 +541,19 @@ type CreateBuildMultipartBody struct { // CacheScope Tenant-specific cache key prefix CacheScope *string `json:"cache_scope,omitempty"` - // Dockerfile Optional custom Dockerfile content + // Dockerfile Dockerfile content. Required if not included in the source tarball. Dockerfile *string `json:"dockerfile,omitempty"` - // Runtime Build runtime - Runtime CreateBuildMultipartBodyRuntime `json:"runtime"` + // Runtime (Deprecated) Build runtime hint. No longer required. + Runtime *string `json:"runtime,omitempty"` - // Source Source tarball (tar.gz) + // Source Source tarball (tar.gz) containing application code and optionally a Dockerfile Source openapi_types.File `json:"source"` // TimeoutSeconds Build timeout (default 600) TimeoutSeconds *int `json:"timeout_seconds,omitempty"` } -// CreateBuildMultipartBodyRuntime defines parameters for CreateBuild. -type CreateBuildMultipartBodyRuntime string - // GetBuildLogsParams defines parameters for GetBuildLogs. type GetBuildLogsParams struct { // Follow Continue streaming new lines after initial output @@ -9221,116 +9212,117 @@ func (sh *strictHandler) GetVolume(w http.ResponseWriter, r *http.Request, id st // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+w9CXMTO5p/RdU7U+Ps2I5jjgFPbW2FBHieJZBKIG9nXlgjd8u2iFpqJLUTQ+W/b+nq", - "U213gBgypOpVPZNW6/j03Vd/CUIWJ4wiKkUw+hKIcIFiqH/uSwnDxRkjaYxO0KcUCan+nHCWIC4x0oNi", - "llI5SaBcqH9FSIQcJxIzGoyCYygX4HKBOAJLPQsQC5aSCEwR0O+hKOgG6ArGCUHBKNiNqdyNoIRBN5Cr", - "RP1JSI7pPLjuBhzBiFGyMsvMYEpkMJpBIlC3suyRmhpAAdQrPf1ONt+UMYIgDa71jJ9SzFEUjP4oHuN9", - "NphNP6JQqsX3lxATOCXoEC1xiOpgCFPOEZWTiOMl4nVQHJjnZAWmLKURMONAh6aEADwDlFG0UwIGXeII", - "K0ioIWrpYCR5ijyQifSeJjjy3MDBGJjHYHwIOgt0VV5k+Lfpk6B5SgpjVJ/0tzSGtKeAq7bl5tdji3O/", - "euibGbM4TidzztKkPvP4zdHRO6AfAprGU8SLMz4ZZvNhKtEccTVhEuIJjCKOhPCf3z0s7m0wGAxGcDga", - "DPoD3y6XiEaMN4LUPPaDdG8QoTVTtgKpnb8G0tdn48PxPjhgPGEc6ndrK1UQuwie4rmKaFO+FR/+P0sx", - "iTxYz9TGJIomUNYPpV8CdgxmFEgcIyFhnATdYMZ4rF4KIihRTz1pg+ohR3DDcmpEq8XqSJ8amE5i0TS7", - "GwIwBTEmBAsUMhqJ4hqYyscPmw9TQF3EOfPwiufqzyBGQsA5Ah3FwBQXpUBIKFMBsAAziAmKdtqAzIfD", - "5jAf2RTgCFGJZ7hMacFUDejBabg3fOCl4hjO0STCcysTytMf6r8DNgNqHgn0aP9BFMqv2p1DL8nRrL7e", - "C81E9SIczRBHNPzm5RLOlohCapj9n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CN9miNlmd6HpNc42y", + "5pNSZvvGSrbcdPcOx/UT0sw/YuEF4n3MdgmecshXu3SO6dWQQImELK9mfduN69NzW7MwOldLv+HSKrqz", + "hluHsEvEQyWJCVIAEV0ljLEUXQCV+aWFGFDH7d9BCKnCrDm5GAeIRuASywWAul2ZAvGqBxPcw2aqQTeI", + "4dUrROfK/n38oIZHBcaO/dF7/5/uTzv/7YUkTwnygPGEpRLTOdCPwYxxIBdYgHwOWKJ441npqJsSraPE", + "mI7Na3vZTCDncOXfNTe5dbsnpBI+jdtnGMizvpGzUAWwVo8+cKD2P+j1vjx+t6tYMoFCyAVn6XxR3JU/", + "nDx4X6BFXeaXFqkk7PIbzsnndIk5ozGiEiwhxwp8JSPzS/D6zej55Pnrs2CoKBGlobXYjt+cvA2GwYPB", + "YBD4jooFkwlJ5xOBP6OSuyN48PJZUJ3IQTZ/EKOY8ZWmmO0DdBZl9jDHIyD4AoFz1d95oETY3suq4NrX", + "Q/nV8FbyZYPggCTBFDVKju7Pwu2XjF8QBqPe3ndmdoqk6ru+xNfmAQgZneG5szgN2yOALZsF3Qp7Iaoo", + "EpUAY5Trcve/L5BcIF7gMNel+pM56fTrwM2wQJGStl50ptVAzJaIE7jygHhv4EHx7xxLvaP2PRBhcQHU", + "yxsgrHozGH40qIN44EexZ1KeOT1TiLI81WYm2UT29o/sz/22fLUME6cY2intV6fzWnvElDqyxFymkIDD", + "43clkeN1kBnXq0fsGs9uUdTa/c/wAGXZn9L2qDE9az9sXfD6TxcjV5pPlw1uaJ+XI9NYw1RIFhd8HaBT", + "UUZxWW0t79iSkV4EJdQSoKWYMtOte/DilenKbEoTNCfzqceCUgjEFMzxHE5XsnzY7A3qW+8ntOvfR+om", + "77aBB4omknmctg4t45Gio2vbxsjXvvCJZJPlDHt6ziRVrn1jAcKKK92CVnXRS0JsXetdcLnASrYJ4Iig", + "RejZUVGJ6J/THlCTG4JRNkDWbdalOkS0paW76DBemATWRjmYrnYABGdHffA2m+1fBKBQ4iVy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EuDcHdnngdERCspBAZT2WG/loKynWXxdVkU18LEp", + "b+ImeRKtpMe61PLTclJ5a7n/6F/flH+ONitmholOdWP31uQmjisEQpaSiP5FgqniPKOqochqlALJ/D6A", + "ZtZ39IKyS1peuvFfKP79lCK+AmdHRyVvF0czmzneYuEsSRr3gSU32ob9DcfvxtkU0mK2kQpTlYQF+frd", + "E1+KprkL3xjUtTDRi7hrzsjQ3Wnz2yTRREOFDGB7B9NUgiwXTkHukLA00pkEfImFzsWUeIm0RnySUorp", + "XPWgz4xQPSErwM3f1798DBX83LuJ/tf6N04XqYzYJdXviEUqgfqXnrJaglWH1ndhkDwEr5l+x860q8R/", + "Ra8yzSGNpqt686oO1jHWulLPJeMo0oNZthyCFxkrZsxsmbcjkP1pJIQN8+kQ5o5R463f0u5W0A0s1YNu", + "YEgYdANHGfXTrFD/0pMPuoGdiDfIa+DZlN8TuzvAlfAeNne5bMoLKDQGHRQncuUczI57dm7GLgdZh774", + "6/c2CQZPv4dT8t1aL+S/ScZYUUK5QTbKptqeNpr+3pSM8aiq2xp7x14gL2urlSCykGuuLK67qG5ujKtn", + "zu02T6txwhtcTm/KYcs5x91FdLfTN5lwDUa2SScprKwwk+a9McfTN97kx8Jd4f9KklmNdLMfywhDkCDe", + "yyDh1FklQS851mEnSyBDWEWC/1I6hd/vtl5rPoJX2Qhan4UCVDJ/zTpyT4/O/d3pgxOXl4Fnrgs9jX5Z", + "vfarwO1LHDhU1TdjXc0DpwB5Gc/KnzUSrYm3KuDMx+iuL6ugRBcKU47l6lQdCPaiJYIc8YPUwFCfFHoR", + "+s/54NqXe32tE3RmntzRl+YSKzg4HmuUxJDCudqysyNA8AyFq5Ag64qrKRH6PsCbw3HPxBCc5a49OVhq", + "grh0y4Pjsc70spcIg0F/v69vdbAEUZjgYBg86O/pXDZFBr3EXR2i1T+tb1TxoT7JxpE9cZ+ZJoq0ImFU", + "GOLsDwYmD4dKK1phnoq1+1GY2LA5XrWobXMKm8v9dXW15t1ymoCd/nU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ugGKSf2myXD3V2i2i2YkPqLusH1++v/DwAA//8MW0xthacA", + "AA==", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/lib/providers/providers.go b/lib/providers/providers.go index 19c13feb..27573581 100644 --- a/lib/providers/providers.go +++ b/lib/providers/providers.go @@ -196,6 +196,7 @@ func ProvideBuildManager(p *paths.Paths, cfg *config.Config, instanceManager ins BuilderImage: cfg.BuilderImage, RegistryURL: cfg.RegistryURL, DefaultTimeout: cfg.BuildTimeout, + RegistrySecret: cfg.JwtSecret, // Use same secret for registry tokens } // Apply defaults if not set diff --git a/openapi.yaml b/openapi.yaml index 2222b084..4552baf2 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -662,22 +662,20 @@ components: CreateBuildRequest: type: object - required: [runtime] properties: runtime: type: string - enum: [nodejs20, python312] - description: Build runtime - example: nodejs20 + description: "(Deprecated) Build runtime hint. No longer required - the generic builder accepts any Dockerfile." + example: generic + dockerfile: + type: string + description: "Dockerfile content. Required if not included in the source tarball. The Dockerfile specifies the runtime (e.g., FROM node:20-alpine)." base_image_digest: type: string description: Optional pinned base image digest for reproducibility cache_scope: type: string description: Tenant-specific cache key prefix for isolation - dockerfile: - type: string - description: Optional custom Dockerfile content build_args: type: object additionalProperties: @@ -688,7 +686,7 @@ components: Build: type: object - required: [id, status, runtime, created_at] + required: [id, status, created_at] properties: id: type: string @@ -698,8 +696,8 @@ components: $ref: "#/components/schemas/BuildStatus" runtime: type: string - description: Build runtime - example: nodejs20 + description: "(Deprecated) Build runtime hint" + example: generic queue_position: type: integer description: Position in build queue (only when status is queued) @@ -1836,26 +1834,24 @@ paths: schema: type: object required: - - runtime - source properties: - runtime: - type: string - enum: [nodejs20, python312] - description: Build runtime source: type: string format: binary - description: Source tarball (tar.gz) + description: Source tarball (tar.gz) containing application code and optionally a Dockerfile + dockerfile: + type: string + description: Dockerfile content. Required if not included in the source tarball. + runtime: + type: string + description: "(Deprecated) Build runtime hint. No longer required." base_image_digest: type: string description: Optional pinned base image digest cache_scope: type: string description: Tenant-specific cache key prefix - dockerfile: - type: string - description: Optional custom Dockerfile content timeout_seconds: type: integer description: Build timeout (default 600) From 3492001c2e5c8e3fc282f86d34eced6eb76e6de5 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 09:55:07 -0500 Subject: [PATCH 10/42] docs(builds): update README with registry token auth and generic builder - Update architecture diagram to show JWT token auth instead of insecure - Add Registry Token System section documenting registry_token.go - Add Metrics section documenting metrics.go - Update cache example to remove outdated runtime references - Add Registry Authentication section explaining token-based auth - Update Security Model to include registry auth - Fix Build and Push section to use docker buildx with OCI format - Update E2E test example to use generic builder image - Update troubleshooting for 401 errors with token-based auth - Update config.json example to show registry_token and dockerfile fields - Remove references to deleted templates package --- lib/builds/README.md | 72 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/lib/builds/README.md b/lib/builds/README.md index e37dca72..cb0e4e7a 100644 --- a/lib/builds/README.md +++ b/lib/builds/README.md @@ -28,7 +28,7 @@ The build system provides source-to-image builds inside ephemeral Cloud Hypervis │ │ │ ││ │ │ ▼ ││ │ │ Push to Registry ││ -│ │ (HTTP, insecure) ││ +│ │ (JWT token auth) ││ │ └─────────────────────────────────────────────────────────────┘│ └─────────────────────────────────────────────────────────────────┘ │ @@ -106,11 +106,40 @@ Registry-based caching with tenant isolation: ```go gen := NewCacheKeyGenerator("localhost:8080") -key, _ := gen.GenerateCacheKey("my-tenant", "nodejs20", lockfileHashes) -// key.ImportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/nodejs20/abc123" -// key.ExportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/nodejs20/abc123,mode=max" +key, _ := gen.GenerateCacheKey("my-tenant", "myapp", lockfileHashes) +// key.ImportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/myapp/abc123" +// key.ExportCacheArg() → "type=registry,ref=localhost:8080/cache/my-tenant/myapp/abc123,mode=max" ``` +### Registry Token System (`registry_token.go`) + +JWT-based authentication for builder VMs to push images: + +```go +generator := NewRegistryTokenGenerator(jwtSecret) +token, _ := generator.GeneratePushToken(buildID, []string{"builds/abc123", "cache/tenant-x"}, 30*time.Minute) +// Token grants push access only to specified repositories +// Validated by middleware on /v2/* registry endpoints +``` + +| Field | Description | +|-------|-------------| +| `BuildID` | Build job identifier for audit | +| `Repositories` | Allowed repository paths | +| `Scope` | Access scope: `push` or `pull` | +| `ExpiresAt` | Token expiry (matches build timeout) | + +### Metrics (`metrics.go`) + +OpenTelemetry metrics for monitoring: + +| Metric | Type | Description | +|--------|------|-------------| +| `hypeman_build_duration_seconds` | Histogram | Build duration | +| `hypeman_builds_total` | Counter | Total builds by status/runtime | +| `hypeman_build_queue_length` | Gauge | Pending builds in queue | +| `hypeman_builds_active` | Gauge | Currently running builds | + ### Builder Agent (`builder_agent/main.go`) Guest binary that runs inside builder VMs: @@ -190,7 +219,14 @@ The `REGISTRY_URL` must be accessible from inside builder VMs. Since `localhost` REGISTRY_URL=10.102.0.1:8083 # Gateway IP accessible from VM network ``` -The middleware allows unauthenticated registry pushes from the VM network (10.102.x.x). +### Registry Authentication + +Builder VMs authenticate to the registry using short-lived JWT tokens: + +1. **Token Generation**: The build manager generates a scoped token for each build +2. **Token Scope**: Grants push access only to `builds/{build_id}` and `cache/{cache_scope}` +3. **Token TTL**: Matches build timeout (minimum 30 minutes) +4. **Authentication**: Builder agent sends token via Basic auth (`token:` format) ## Build Status Flow @@ -209,6 +245,7 @@ queued → building → pushing → ready 3. **Network Control**: `network_mode: isolated` or `egress` with optional domain allowlist 4. **Secret Handling**: Secrets fetched via vsock, never written to disk in guest 5. **Cache Isolation**: Per-tenant cache scopes prevent cross-tenant cache poisoning +6. **Registry Auth**: Short-lived JWT tokens scoped to specific repositories (builds/{id}, cache/{scope}) ## Builder Images @@ -233,15 +270,17 @@ Builder images must include: ### Build and Push +> **Important**: Images must be built with OCI manifest format for Hypeman. +> See [`images/README.md`](./images/README.md) for detailed build instructions. + ```bash -# Build the generic builder image -docker build \ - -t hypeman/builder:latest \ +# Build with OCI format (required for Hypeman) +docker buildx build \ + --platform linux/amd64 \ + --output "type=registry,oci-mediatypes=true" \ + --tag yourregistry/builder:latest \ -f lib/builds/images/generic/Dockerfile \ . - -# Push to your registry -docker push hypeman/builder:latest ``` ### Environment Variables @@ -312,7 +351,7 @@ go test ./lib/builds/... -v # Test specific components go test ./lib/builds/queue_test.go ./lib/builds/queue.go ./lib/builds/types.go -v go test ./lib/builds/cache_test.go ./lib/builds/cache.go ./lib/builds/types.go ./lib/builds/errors.go -v -go test ./lib/builds/templates/... -v +go test ./lib/builds/registry_token_test.go ./lib/builds/registry_token.go -v ``` ### E2E Testing @@ -328,7 +367,7 @@ go test ./lib/builds/templates/... -v curl -X POST http://localhost:8083/images \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ - -d '{"name": "hirokernel/builder-nodejs20:latest"}' + -d '{"name": "hirokernel/builder-generic:latest"}' ``` 3. **Create test source with Dockerfile**: @@ -382,7 +421,7 @@ go test ./lib/builds/templates/... -v | `no cgroup mount found` | Cgroups not mounted in VM | Update init script to mount cgroups | | `http: server gave HTTP response to HTTPS client` | BuildKit using HTTPS for HTTP registry | Add `registry.insecure=true` to output flags | | `connection refused` to localhost:8080 | Registry URL not accessible from VM | Use gateway IP (10.102.0.1) instead of localhost | -| `authorization header required` | Registry auth blocking VM push | Ensure auth bypass for 10.102.x.x IPs | +| `401 Unauthorized` | Registry auth issue | Check registry_token in config.json; verify middleware handles Basic auth | | `No space left on device` | Instance memory too small for image | Use at least 1GB RAM for Node.js images | | `can't enable NoProcessSandbox without Rootless` | Wrong BUILDKITD_FLAGS | Use empty flags or remove the flag | @@ -412,11 +451,14 @@ Expected format: ```json { "job_id": "abc123", - "runtime": "nodejs20", "registry_url": "10.102.0.1:8083", + "registry_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", "cache_scope": "my-tenant", "source_path": "/src", + "dockerfile": "FROM node:20-alpine\nWORKDIR /app\n...", "timeout_seconds": 300, "network_mode": "egress" } ``` + +Note: `registry_token` is a short-lived JWT granting push access to `builds/abc123` and `cache/my-tenant`. From de373f7ca7afcfc3ab21b7690bec47d9e06bb2e9 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 09:58:25 -0500 Subject: [PATCH 11/42] chore: remove trailing newlines --- lib/builds/errors.go | 1 - lib/builds/registry_token.go | 2 -- lib/builds/registry_token_test.go | 2 -- 3 files changed, 5 deletions(-) diff --git a/lib/builds/errors.go b/lib/builds/errors.go index 6fab2e24..da0dabc0 100644 --- a/lib/builds/errors.go +++ b/lib/builds/errors.go @@ -47,4 +47,3 @@ func IsSupportedRuntime(runtime string) bool { // or no runtime at all. Kept for backward compatibility. return true } - diff --git a/lib/builds/registry_token.go b/lib/builds/registry_token.go index e9d1a32e..6c8cf44b 100644 --- a/lib/builds/registry_token.go +++ b/lib/builds/registry_token.go @@ -104,5 +104,3 @@ func (c *RegistryTokenClaims) IsPushAllowed() bool { func (c *RegistryTokenClaims) IsPullAllowed() bool { return c.Scope == "push" || c.Scope == "pull" } - - diff --git a/lib/builds/registry_token_test.go b/lib/builds/registry_token_test.go index 3c376462..1231e07e 100644 --- a/lib/builds/registry_token_test.go +++ b/lib/builds/registry_token_test.go @@ -109,5 +109,3 @@ func TestRegistryTokenClaims_IsPushAllowed(t *testing.T) { assert.True(t, claims.IsPullAllowed()) }) } - - From bc517e1d1aea30e402fb8bf1eb5de3e9f4e16549 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 15:23:17 -0500 Subject: [PATCH 12/42] fix(images): support both Docker v2 and OCI v1 manifest formats Use go-containerregistry instead of umoci's casext for manifest parsing, which handles both Docker v2 and OCI v1 formats automatically. Changes: - extractOCIMetadata: Use layout.Path and Image.ConfigFile() from go-containerregistry which abstracts away manifest format differences - unpackLayers: Get manifest via go-containerregistry, then convert to OCI v1 format for umoci's layer unpacker - Add imageByAnnotation() helper to find images in OCI layout by tag - Add convertToOCIManifest() to convert go-containerregistry manifest to OCI v1.Manifest for umoci compatibility - Update documentation to remove OCI format requirement This allows users to build images with standard 'docker build' without needing 'docker buildx --output type=registry,oci-mediatypes=true'. --- lib/builds/README.md | 15 ++-- lib/builds/images/README.md | 79 +++++-------------- lib/images/oci.go | 152 +++++++++++++++++++++++------------- 3 files changed, 125 insertions(+), 121 deletions(-) diff --git a/lib/builds/README.md b/lib/builds/README.md index cb0e4e7a..4aaa7def 100644 --- a/lib/builds/README.md +++ b/lib/builds/README.md @@ -270,17 +270,16 @@ Builder images must include: ### Build and Push -> **Important**: Images must be built with OCI manifest format for Hypeman. -> See [`images/README.md`](./images/README.md) for detailed build instructions. +See [`images/README.md`](./images/README.md) for detailed build instructions. ```bash -# Build with OCI format (required for Hypeman) -docker buildx build \ - --platform linux/amd64 \ - --output "type=registry,oci-mediatypes=true" \ - --tag yourregistry/builder:latest \ +# Build and push the builder image +docker build \ + -t yourregistry/builder:latest \ -f lib/builds/images/generic/Dockerfile \ . + +docker push yourregistry/builder:latest ``` ### Environment Variables @@ -417,7 +416,7 @@ go test ./lib/builds/registry_token_test.go ./lib/builds/registry_token.go -v | Error | Cause | Solution | |-------|-------|----------| -| `image not found` | Builder image not in OCI format | Push with `crane` after `docker buildx --output type=oci` | +| `image not found` | Builder image not imported | Import image using `POST /images` endpoint | | `no cgroup mount found` | Cgroups not mounted in VM | Update init script to mount cgroups | | `http: server gave HTTP response to HTTPS client` | BuildKit using HTTPS for HTTP registry | Add `registry.insecure=true` to output flags | | `connection refused` to localhost:8080 | Registry URL not accessible from VM | Use gateway IP (10.102.0.1) instead of localhost | diff --git a/lib/builds/images/README.md b/lib/builds/images/README.md index 5cb8efc2..c9fc911d 100644 --- a/lib/builds/images/README.md +++ b/lib/builds/images/README.md @@ -44,57 +44,41 @@ images/ ## Building the Generic Builder Image -> **Important**: Hypeman uses `umoci` for OCI image manipulation, which requires images -> to have **OCI manifest format** (not Docker v2 format). You must use `docker buildx` -> with the `oci-mediatypes=true` option. +Hypeman supports both Docker v2 and OCI image formats. You can use standard `docker build` +or `docker buildx` - both work. ### Prerequisites -1. **Docker Buildx** with a container builder: - ```bash - # Create a buildx builder (if you don't have one) - docker buildx create --name ocibuilder --use - ``` - +1. **Docker** installed 2. **Docker Hub login** (or your registry): ```bash docker login ``` -### 1. Build and Push with OCI Format +### 1. Build and Push ```bash # From repository root -docker buildx build \ - --platform linux/amd64 \ - --output "type=registry,oci-mediatypes=true" \ - --tag hirokernel/builder-generic:latest \ +docker build \ + -t hirokernel/builder-generic:latest \ -f lib/builds/images/generic/Dockerfile \ . -``` -This command: -- Builds for `linux/amd64` platform -- Uses `oci-mediatypes=true` to create OCI manifests (required for Hypeman) -- Pushes directly to the registry +docker push hirokernel/builder-generic:latest +``` -### 2. Verify the Manifest Format +Or with buildx for multi-platform support: ```bash -# Should show "application/vnd.oci.image.index.v1+json" -docker manifest inspect hirokernel/builder-generic:latest | head -5 -``` - -Expected output: -```json -{ - "schemaVersion": 2, - "mediaType": "application/vnd.oci.image.index.v1+json", - ... -} +docker buildx build \ + --platform linux/amd64 \ + --push \ + --tag hirokernel/builder-generic:latest \ + -f lib/builds/images/generic/Dockerfile \ + . ``` -### 3. Import into Hypeman +### 2. Import into Hypeman ```bash # Generate a token @@ -111,7 +95,7 @@ curl http://localhost:8083/images/docker.io%2Fhirokernel%2Fbuilder-generic:lates -H "Authorization: Bearer $TOKEN" ``` -### 4. Configure Hypeman +### 3. Configure Hypeman Set the builder image in your `.env`: @@ -119,23 +103,10 @@ Set the builder image in your `.env`: BUILDER_IMAGE=hirokernel/builder-generic:latest ``` -### Why OCI Format is Required - -| Build Method | Manifest Type | Works with Hypeman? | -|--------------|---------------|---------------------| -| `docker build` | Docker v2 (`application/vnd.docker.distribution.manifest.v2+json`) | ❌ No | -| `docker buildx --output type=docker` | Docker v2 | ❌ No | -| `docker buildx --output type=registry,oci-mediatypes=true` | OCI (`application/vnd.oci.image.index.v1+json`) | ✅ Yes | - -Hypeman uses `umoci` to extract and convert OCI images to ext4 disk images for microVMs. -`umoci` strictly requires OCI-format manifests and cannot parse Docker v2 manifests. - ### Building for Local Testing (without pushing) -If you need to test locally before pushing: - ```bash -# Build and load to local Docker (for testing only - won't work with Hypeman import) +# Build locally docker build \ -t hypeman/builder:local \ -f lib/builds/images/generic/Dockerfile \ @@ -145,10 +116,6 @@ docker build \ docker run --rm hypeman/builder:local --help ``` -**Note**: Images built with `docker build` cannot be imported into Hypeman directly. -You must rebuild with `docker buildx --output type=registry,oci-mediatypes=true` -before deploying to Hypeman. - ## Usage ### Submitting a Build @@ -266,8 +233,7 @@ When the builder runs inside a Hypeman microVM: | Issue | Cause | Solution | |-------|-------|----------| -| `manifest data is not v1.Manifest` | Image built with Docker v2 format | Rebuild with `docker buildx --output type=registry,oci-mediatypes=true` | -| Image import stuck on `pending`/`failed` | Manifest format incompatible | Check manifest format with `docker manifest inspect` | +| Image import stuck on `pending`/`failed` | Network or registry issue | Check Hypeman logs, verify registry access | | `Dockerfile required` | No Dockerfile in source or parameter | Include Dockerfile in tarball or pass as parameter | | `401 Unauthorized` during push | Registry token issue | Check builder agent logs, verify token generation | | `runc: not found` | BuildKit binaries missing | Rebuild the builder image | @@ -281,15 +247,10 @@ When the builder runs inside a Hypeman microVM: # Check image status cat ~/hypeman_data_dir/images/docker.io/hirokernel/builder-generic/*/metadata.json | jq . -# Check OCI cache for manifest format +# Check OCI cache index cat ~/hypeman_data_dir/system/oci-cache/index.json | jq '.manifests[-1]' - -# Verify image on Docker Hub has OCI format -skopeo inspect --raw docker://hirokernel/builder-generic:latest | head -5 ``` -If you see `application/vnd.docker.distribution.manifest.v2+json`, the image needs to be rebuilt with OCI format. - ## Migration from Runtime-Specific Images If you were using `nodejs20` or `python312` builder images: diff --git a/lib/images/oci.go b/lib/images/oci.go index 959e6c79..f98b89f7 100644 --- a/lib/images/oci.go +++ b/lib/images/oci.go @@ -13,6 +13,8 @@ import ( "github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/layout" "github.com/google/go-containerregistry/pkg/v1/remote" + digest "github.com/opencontainers/go-digest" + "github.com/opencontainers/image-spec/specs-go" v1 "github.com/opencontainers/image-spec/specs-go/v1" rspec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/umoci/oci/cas/dir" @@ -205,61 +207,65 @@ func (c *ociClient) extractDigest(layoutTag string) (string, error) { return digest, nil } -// extractOCIMetadata reads metadata from OCI layout config.json -func (c *ociClient) extractOCIMetadata(layoutTag string) (*containerMetadata, error) { - // Open the shared OCI layout - casEngine, err := dir.Open(c.cacheDir) +// imageByAnnotation finds an image in the OCI layout by its annotation tag. +// This iterates through the index to find the image with matching +// "org.opencontainers.image.ref.name" annotation. +func imageByAnnotation(path layout.Path, layoutTag string) (gcr.Image, error) { + index, err := path.ImageIndex() if err != nil { - return nil, fmt.Errorf("open oci layout: %w", err) + return nil, fmt.Errorf("get image index: %w", err) } - defer casEngine.Close() - engine := casext.NewEngine(casEngine) - - // Resolve the layout tag in the shared layout - descriptorPaths, err := engine.ResolveReference(context.Background(), layoutTag) + indexManifest, err := index.IndexManifest() if err != nil { - return nil, fmt.Errorf("resolve reference: %w", err) + return nil, fmt.Errorf("get index manifest: %w", err) } - if len(descriptorPaths) == 0 { - return nil, fmt.Errorf("no image found in oci layout") + // Find the image with matching annotation + for _, desc := range indexManifest.Manifests { + if desc.Annotations != nil { + if refName, ok := desc.Annotations["org.opencontainers.image.ref.name"]; ok { + if refName == layoutTag { + return path.Image(desc.Digest) + } + } + } } - // Get the manifest - manifestBlob, err := engine.FromDescriptor(context.Background(), descriptorPaths[0].Descriptor()) - if err != nil { - return nil, fmt.Errorf("get manifest: %w", err) - } + return nil, fmt.Errorf("no image found with tag %s", layoutTag) +} - // casext automatically parses manifests, so Data is already a v1.Manifest - manifest, ok := manifestBlob.Data.(v1.Manifest) - if !ok { - return nil, fmt.Errorf("manifest data is not v1.Manifest (got %T)", manifestBlob.Data) +// extractOCIMetadata reads metadata from OCI layout config.json +// Uses go-containerregistry which handles both Docker v2 and OCI v1 manifests. +func (c *ociClient) extractOCIMetadata(layoutTag string) (*containerMetadata, error) { + // Open OCI layout using go-containerregistry (handles Docker v2 and OCI v1) + path, err := layout.FromPath(c.cacheDir) + if err != nil { + return nil, fmt.Errorf("open oci layout: %w", err) } - // Get the config blob - configBlob, err := engine.FromDescriptor(context.Background(), manifest.Config) + // Get the image by annotation tag from the layout + img, err := imageByAnnotation(path, layoutTag) if err != nil { - return nil, fmt.Errorf("get config: %w", err) + return nil, fmt.Errorf("find image by tag %s: %w", layoutTag, err) } - // casext automatically parses config, so Data is already a v1.Image - config, ok := configBlob.Data.(v1.Image) - if !ok { - return nil, fmt.Errorf("config data is not v1.Image (got %T)", configBlob.Data) + // Get config file (go-containerregistry handles manifest format automatically) + configFile, err := img.ConfigFile() + if err != nil { + return nil, fmt.Errorf("get config file: %w", err) } - // Extract metadata + // Extract metadata from config meta := &containerMetadata{ - Entrypoint: config.Config.Entrypoint, - Cmd: config.Config.Cmd, + Entrypoint: configFile.Config.Entrypoint, + Cmd: configFile.Config.Cmd, Env: make(map[string]string), - WorkingDir: config.Config.WorkingDir, + WorkingDir: configFile.Config.WorkingDir, } // Parse environment variables - for _, env := range config.Config.Env { + for _, env := range configFile.Config.Env { for i := 0; i < len(env); i++ { if env[i] == '=' { key := env[:i] @@ -274,37 +280,36 @@ func (c *ociClient) extractOCIMetadata(layoutTag string) (*containerMetadata, er } // unpackLayers unpacks all OCI layers to a target directory using umoci -func (c *ociClient) unpackLayers(ctx context.Context, imageRef, targetDir string) error { - // Open the shared OCI layout - casEngine, err := dir.Open(c.cacheDir) +// Uses go-containerregistry to get the manifest (handles both Docker v2 and OCI v1) +// then converts it to OCI v1 format for umoci's layer unpacker. +func (c *ociClient) unpackLayers(ctx context.Context, layoutTag, targetDir string) error { + // Open OCI layout using go-containerregistry (handles Docker v2 and OCI v1) + path, err := layout.FromPath(c.cacheDir) if err != nil { return fmt.Errorf("open oci layout: %w", err) } - defer casEngine.Close() - engine := casext.NewEngine(casEngine) - - // Resolve the image reference (tag) in the shared layout - descriptorPaths, err := engine.ResolveReference(context.Background(), imageRef) + // Get the image by annotation tag from the layout + img, err := imageByAnnotation(path, layoutTag) if err != nil { - return fmt.Errorf("resolve reference: %w", err) + return fmt.Errorf("find image by tag %s: %w", layoutTag, err) } - if len(descriptorPaths) == 0 { - return fmt.Errorf("no image found") - } - - // Get the manifest blob - manifestBlob, err := engine.FromDescriptor(context.Background(), descriptorPaths[0].Descriptor()) + // Get manifest from go-containerregistry + gcrManifest, err := img.Manifest() if err != nil { return fmt.Errorf("get manifest: %w", err) } - // casext automatically parses manifests - manifest, ok := manifestBlob.Data.(v1.Manifest) - if !ok { - return fmt.Errorf("manifest data is not v1.Manifest (got %T)", manifestBlob.Data) + // Convert go-containerregistry manifest to OCI v1.Manifest for umoci + ociManifest := convertToOCIManifest(gcrManifest) + + // Open the shared OCI layout with umoci for layer unpacking + casEngine, err := dir.Open(c.cacheDir) + if err != nil { + return fmt.Errorf("open oci layout for unpacking: %w", err) } + defer casEngine.Close() // Pre-create target directory (umoci needs it to exist) if err := os.MkdirAll(targetDir, 0755); err != nil { @@ -330,7 +335,7 @@ func (c *ociClient) unpackLayers(ctx context.Context, imageRef, targetDir string }, } - err = layer.UnpackRootfs(context.Background(), casEngine, targetDir, manifest, unpackOpts) + err = layer.UnpackRootfs(context.Background(), casEngine, targetDir, ociManifest, unpackOpts) if err != nil { return fmt.Errorf("unpack rootfs: %w", err) } @@ -338,6 +343,45 @@ func (c *ociClient) unpackLayers(ctx context.Context, imageRef, targetDir string return nil } +// convertToOCIManifest converts a go-containerregistry manifest to OCI v1.Manifest +// This allows us to use go-containerregistry (which handles both Docker v2 and OCI v1) +// for manifest parsing, while still using umoci for layer unpacking. +func convertToOCIManifest(gcrManifest *gcr.Manifest) v1.Manifest { + // Convert config descriptor + configDesc := v1.Descriptor{ + MediaType: string(gcrManifest.Config.MediaType), + Digest: gcrDigestToOCI(gcrManifest.Config.Digest), + Size: gcrManifest.Config.Size, + Annotations: gcrManifest.Config.Annotations, + } + + // Convert layer descriptors + layers := make([]v1.Descriptor, len(gcrManifest.Layers)) + for i, layer := range gcrManifest.Layers { + layers[i] = v1.Descriptor{ + MediaType: string(layer.MediaType), + Digest: gcrDigestToOCI(layer.Digest), + Size: layer.Size, + Annotations: layer.Annotations, + } + } + + return v1.Manifest{ + Versioned: specs.Versioned{ + SchemaVersion: int(gcrManifest.SchemaVersion), + }, + MediaType: string(gcrManifest.MediaType), + Config: configDesc, + Layers: layers, + Annotations: gcrManifest.Annotations, + } +} + +// gcrDigestToOCI converts a go-containerregistry digest to OCI digest +func gcrDigestToOCI(d gcr.Hash) digest.Digest { + return digest.NewDigestFromEncoded(digest.Algorithm(d.Algorithm), d.Hex) +} + type containerMetadata struct { Entrypoint []string Cmd []string From 95b0437ef58e219d66ffc3754473f9d5c641d396 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 15:27:59 -0500 Subject: [PATCH 13/42] fix: ensure volume cleanup succeeds after build timeout - Use context.Background() for volume deletion in defers instead of the build timeout context, matching the pattern used for instance cleanup - Fix error variable shadowing in setup config volume error path (copyErr) - Improve multipart form field parsing with proper error handling using io.ReadAll instead of bytes.Buffer with ignored errors --- cmd/api/api/builds.go | 57 ++++++++++++++++++++++++++++++------------- lib/builds/manager.go | 8 +++--- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go index d8c5d3f5..32e913e2 100644 --- a/cmd/api/api/builds.go +++ b/cmd/api/api/builds.go @@ -1,7 +1,6 @@ package api import ( - "bytes" "context" "errors" "io" @@ -65,25 +64,50 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe }, nil } case "runtime": - var buf bytes.Buffer - io.Copy(&buf, part) - runtime = buf.String() + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read runtime field", + }, nil + } + runtime = string(data) case "base_image_digest": - var buf bytes.Buffer - io.Copy(&buf, part) - baseImageDigest = buf.String() + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read base_image_digest field", + }, nil + } + baseImageDigest = string(data) case "cache_scope": - var buf bytes.Buffer - io.Copy(&buf, part) - cacheScope = buf.String() + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read cache_scope field", + }, nil + } + cacheScope = string(data) case "dockerfile": - var buf bytes.Buffer - io.Copy(&buf, part) - dockerfile = buf.String() + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read dockerfile field", + }, nil + } + dockerfile = string(data) case "timeout_seconds": - var buf bytes.Buffer - io.Copy(&buf, part) - if v, err := strconv.Atoi(buf.String()); err == nil { + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read timeout_seconds field", + }, nil + } + if v, err := strconv.Atoi(string(data)); err == nil { timeoutSeconds = v } } @@ -294,4 +318,3 @@ func (r *stringReaderImpl) Read(p []byte) (n int, err error) { r.i += n return n, nil } - diff --git a/lib/builds/manager.go b/lib/builds/manager.go index e5d0f2fd..c542b148 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -325,7 +325,7 @@ func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRe if err != nil { return nil, fmt.Errorf("create source volume: %w", err) } - defer m.volumeManager.DeleteVolume(ctx, sourceVolID) + defer m.volumeManager.DeleteVolume(context.Background(), sourceVolID) // Create config volume with build.json for the builder agent configVolID := fmt.Sprintf("build-config-%s", id) @@ -346,17 +346,17 @@ func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRe // by copying it to the expected location volPath := m.paths.VolumeData(configVolID) if copyErr := copyFile(configVolPath, volPath); copyErr != nil { - return nil, fmt.Errorf("setup config volume: %w", err) + return nil, fmt.Errorf("setup config volume: %w", copyErr) } } else { // Copy our config disk over the empty volume volPath := m.paths.VolumeData(configVolID) if err := copyFile(configVolPath, volPath); err != nil { - m.volumeManager.DeleteVolume(ctx, configVolID) + m.volumeManager.DeleteVolume(context.Background(), configVolID) return nil, fmt.Errorf("write config to volume: %w", err) } } - defer m.volumeManager.DeleteVolume(ctx, configVolID) + defer m.volumeManager.DeleteVolume(context.Background(), configVolID) // Create builder instance builderName := fmt.Sprintf("builder-%s", id) From a1b575004f4f457e76bc01e6f873d40721d0b6f1 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 16:09:27 -0500 Subject: [PATCH 14/42] cursor comment --- lib/builds/manager.go | 54 +++++++++++++++++++++++++++++++++++++++++++ lib/builds/storage.go | 42 +++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/lib/builds/manager.go b/lib/builds/manager.go index c542b148..963bdd94 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -665,6 +665,17 @@ func (m *manager) RecoverPendingBuilds() { // Re-enqueue the build if meta.Request != nil { + // Regenerate registry token since the original token may have expired + // during server downtime. Token TTL is minimum 30 minutes. + if err := m.refreshBuildToken(meta.ID, meta.Request); err != nil { + m.logger.Error("failed to refresh registry token for recovered build", + "id", meta.ID, "error", err) + // Mark the build as failed since we can't refresh the token + errMsg := fmt.Sprintf("failed to refresh registry token on recovery: %v", err) + m.updateBuildComplete(meta.ID, StatusFailed, nil, &errMsg, nil, nil) + continue + } + m.queue.Enqueue(meta.ID, *meta.Request, func() { policy := DefaultBuildPolicy() if meta.Request.BuildPolicy != nil { @@ -680,6 +691,49 @@ func (m *manager) RecoverPendingBuilds() { } } +// refreshBuildToken regenerates the registry token for a build and updates the config file +func (m *manager) refreshBuildToken(buildID string, req *CreateBuildRequest) error { + // Read existing build config + config, err := readBuildConfig(m.paths, buildID) + if err != nil { + return fmt.Errorf("read build config: %w", err) + } + + // Determine token TTL from build policy + policy := DefaultBuildPolicy() + if req.BuildPolicy != nil { + policy = *req.BuildPolicy + policy.ApplyDefaults() + } + tokenTTL := time.Duration(policy.TimeoutSeconds) * time.Second + if tokenTTL < 30*time.Minute { + tokenTTL = 30 * time.Minute // Minimum 30 minutes + } + + // Generate allowed repos list + allowedRepos := []string{fmt.Sprintf("builds/%s", buildID)} + if req.CacheScope != "" { + allowedRepos = append(allowedRepos, fmt.Sprintf("cache/%s", req.CacheScope)) + } + + // Generate fresh registry token + registryToken, err := m.tokenGenerator.GeneratePushToken(buildID, allowedRepos, tokenTTL) + if err != nil { + return fmt.Errorf("generate registry token: %w", err) + } + + // Update config with new token + config.RegistryToken = registryToken + + // Write updated config back to disk + if err := writeBuildConfig(m.paths, buildID, config); err != nil { + return fmt.Errorf("write build config: %w", err) + } + + m.logger.Debug("refreshed registry token for recovered build", "id", buildID) + return nil +} + // Helper functions func ensureDir(path string) error { diff --git a/lib/builds/storage.go b/lib/builds/storage.go index 2643b20e..1d56055a 100644 --- a/lib/builds/storage.go +++ b/lib/builds/storage.go @@ -12,19 +12,19 @@ import ( // buildMetadata is the internal representation stored on disk type buildMetadata struct { - ID string `json:"id"` - Status string `json:"status"` - Runtime string `json:"runtime"` + ID string `json:"id"` + Status string `json:"status"` + Runtime string `json:"runtime"` Request *CreateBuildRequest `json:"request,omitempty"` - ImageDigest *string `json:"image_digest,omitempty"` - ImageRef *string `json:"image_ref,omitempty"` - Error *string `json:"error,omitempty"` - Provenance *BuildProvenance `json:"provenance,omitempty"` - CreatedAt time.Time `json:"created_at"` - StartedAt *time.Time `json:"started_at,omitempty"` - CompletedAt *time.Time `json:"completed_at,omitempty"` - DurationMS *int64 `json:"duration_ms,omitempty"` - BuilderInstance *string `json:"builder_instance,omitempty"` // Instance ID of builder VM + ImageDigest *string `json:"image_digest,omitempty"` + ImageRef *string `json:"image_ref,omitempty"` + Error *string `json:"error,omitempty"` + Provenance *BuildProvenance `json:"provenance,omitempty"` + CreatedAt time.Time `json:"created_at"` + StartedAt *time.Time `json:"started_at,omitempty"` + CompletedAt *time.Time `json:"completed_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` + BuilderInstance *string `json:"builder_instance,omitempty"` // Instance ID of builder VM } // toBuild converts internal metadata to the public Build type @@ -225,3 +225,21 @@ func writeBuildConfig(p *paths.Paths, id string, config *BuildConfig) error { return nil } +// readBuildConfig reads the build config for a build +func readBuildConfig(p *paths.Paths, id string) (*BuildConfig, error) { + configPath := p.BuildConfig(id) + data, err := os.ReadFile(configPath) + if err != nil { + if os.IsNotExist(err) { + return nil, ErrNotFound + } + return nil, fmt.Errorf("read build config: %w", err) + } + + var config BuildConfig + if err := json.Unmarshal(data, &config); err != nil { + return nil, fmt.Errorf("unmarshal build config: %w", err) + } + + return &config, nil +} From e7b6a7f7bba55039b573f42e06a76529596aa04b Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Wed, 7 Jan 2026 16:47:16 -0500 Subject: [PATCH 15/42] fix: security and reliability improvements for build system - Reject registry tokens from API authentication (defense-in-depth) - Check for repos, scope, build_id claims - Reject tokens with builder- subject prefix - Add comprehensive test coverage - Fix indefinite blocking in waitForResult - Use goroutine + select for context-aware cancellation - Close connection to unblock decoder on timeout - Prevents resource leaks from unresponsive builders - Fix Makefile builder targets - Replace non-existent nodejs20/python312 targets - Single build-builder target for generic builder - build-builders as backwards-compatible alias --- Makefile | 15 +-- lib/builds/manager.go | 39 ++++-- lib/middleware/oapi_auth.go | 25 ++++ lib/middleware/oapi_auth_test.go | 204 +++++++++++++++++++++++++++++++ 4 files changed, 264 insertions(+), 19 deletions(-) create mode 100644 lib/middleware/oapi_auth_test.go diff --git a/Makefile b/Makefile index 2be7fc29..9ac489ad 100644 --- a/Makefile +++ b/Makefile @@ -206,17 +206,12 @@ test: ensure-ch-binaries ensure-caddy-binaries build-embedded gen-jwt: $(GODOTENV) @$(GODOTENV) -f .env go run ./cmd/gen-jwt -user-id $${USER_ID:-test-user} -# Build the nodejs20 builder image for builds -build-builder-nodejs20: - docker build -t hypeman/builder-nodejs20:latest -f lib/builds/images/nodejs20/Dockerfile . - docker tag hypeman/builder-nodejs20:latest hypeman/builder:latest +# Build the generic builder image for builds +build-builder: + docker build -t hypeman/builder:latest -f lib/builds/images/generic/Dockerfile . -# Build the python312 builder image for builds -build-builder-python312: - docker build -t hypeman/builder-python312:latest -f lib/builds/images/python312/Dockerfile . - -# Build all builder images -build-builders: build-builder-nodejs20 build-builder-python312 +# Alias for backwards compatibility +build-builders: build-builder # Run E2E build system test (requires server running: make dev) e2e-build-test: diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 963bdd94..4d6a3d44 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -459,17 +459,38 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( return nil, fmt.Errorf("send get_result request: %w", err) } - // Wait for response - var response VsockMessage - if err := decoder.Decode(&response); err != nil { - return nil, fmt.Errorf("read result: %w", err) - } + // Use a goroutine for decoding so we can respect context cancellation. + // json.Decoder.Decode() doesn't respect context, so we need to close the + // connection to unblock it when the context is cancelled. + type decodeResult struct { + response VsockMessage + err error + } + resultCh := make(chan decodeResult, 1) + + go func() { + var response VsockMessage + err := decoder.Decode(&response) + resultCh <- decodeResult{response: response, err: err} + }() - if response.Type != "build_result" || response.Result == nil { - return nil, fmt.Errorf("unexpected response type: %s", response.Type) + // Wait for either the result or context cancellation + select { + case <-ctx.Done(): + // Close the connection to unblock the decoder goroutine + conn.Close() + // Drain the result channel to avoid goroutine leak + <-resultCh + return nil, ctx.Err() + case dr := <-resultCh: + if dr.err != nil { + return nil, fmt.Errorf("read result: %w", dr.err) + } + if dr.response.Type != "build_result" || dr.response.Result == nil { + return nil, fmt.Errorf("unexpected response type: %s", dr.response.Type) + } + return dr.response.Result, nil } - - return response.Result, nil } // dialBuilderVsock connects to a builder VM's vsock socket using Cloud Hypervisor's handshake diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index d0158a12..40245f63 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -342,6 +342,31 @@ func JwtAuth(jwtSecret string) func(http.Handler) http.Handler { return } + // Reject registry tokens - they should not be used for API authentication. + // Registry tokens have specific claims that user tokens don't have. + // This provides defense-in-depth even though BuildKit isolates build containers. + if _, hasRepos := claims["repos"]; hasRepos { + log.DebugContext(r.Context(), "rejected registry token used for API auth") + OapiErrorHandler(w, "invalid token type", http.StatusUnauthorized) + return + } + if _, hasScope := claims["scope"]; hasScope { + log.DebugContext(r.Context(), "rejected registry token used for API auth") + OapiErrorHandler(w, "invalid token type", http.StatusUnauthorized) + return + } + if _, hasBuildID := claims["build_id"]; hasBuildID { + log.DebugContext(r.Context(), "rejected registry token used for API auth") + OapiErrorHandler(w, "invalid token type", http.StatusUnauthorized) + return + } + // Also reject tokens with "builder-" prefix in subject as an extra safeguard + if sub, ok := claims["sub"].(string); ok && strings.HasPrefix(sub, "builder-") { + log.DebugContext(r.Context(), "rejected builder token used for API auth", "sub", sub) + OapiErrorHandler(w, "invalid token type", http.StatusUnauthorized) + return + } + // Extract user ID from claims and add to context var userID string if sub, ok := claims["sub"].(string); ok { diff --git a/lib/middleware/oapi_auth_test.go b/lib/middleware/oapi_auth_test.go new file mode 100644 index 00000000..dbb5a266 --- /dev/null +++ b/lib/middleware/oapi_auth_test.go @@ -0,0 +1,204 @@ +package middleware + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/golang-jwt/jwt/v5" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const testJWTSecret = "test-secret-key-for-testing" + +// generateUserToken creates a valid user JWT token +func generateUserToken(t *testing.T, userID string) string { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": userID, + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + return tokenString +} + +// generateRegistryToken creates a registry token (like those given to builder VMs) +func generateRegistryToken(t *testing.T, buildID string) string { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "builder-" + buildID, + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + "iss": "hypeman", + "build_id": buildID, + "repos": []string{"builds/" + buildID}, + "scope": "push", + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + return tokenString +} + +func TestJwtAuth_RejectsRegistryTokens(t *testing.T) { + // Create a simple handler that returns 200 if auth passes + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + // Wrap with JwtAuth middleware + handler := JwtAuth(testJWTSecret)(nextHandler) + + t.Run("valid user token is accepted", func(t *testing.T) { + userToken := generateUserToken(t, "user-123") + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+userToken) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code, "user token should be accepted") + }) + + t.Run("registry token with repos claim is rejected", func(t *testing.T) { + registryToken := generateRegistryToken(t, "build-abc123") + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+registryToken) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code, "registry token should be rejected") + assert.Contains(t, rr.Body.String(), "invalid token type") + }) + + t.Run("token with only builder- prefix is rejected", func(t *testing.T) { + // A token that has builder- prefix but no other registry claims + // This could be crafted by an attacker who knows the pattern + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "builder-malicious-build", + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+tokenString) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code, "builder- prefix token should be rejected") + assert.Contains(t, rr.Body.String(), "invalid token type") + }) + + t.Run("token with scope claim is rejected", func(t *testing.T) { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "some-user", + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + "scope": "push", + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+tokenString) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code, "token with scope claim should be rejected") + }) + + t.Run("token with build_id claim is rejected", func(t *testing.T) { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "some-user", + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + "build_id": "some-build", + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+tokenString) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code, "token with build_id claim should be rejected") + }) +} + +func TestJwtAuth_RequiresAuthorization(t *testing.T) { + nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + handler := JwtAuth(testJWTSecret)(nextHandler) + + t.Run("missing authorization header is rejected", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code) + assert.Contains(t, rr.Body.String(), "authorization header required") + }) + + t.Run("invalid token format is rejected", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Basic abc123") + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code) + assert.Contains(t, rr.Body.String(), "invalid authorization header format") + }) + + t.Run("expired token is rejected", func(t *testing.T) { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "user-123", + "iat": time.Now().Add(-2 * time.Hour).Unix(), + "exp": time.Now().Add(-1 * time.Hour).Unix(), // Expired + }) + tokenString, err := token.SignedString([]byte(testJWTSecret)) + require.NoError(t, err) + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+tokenString) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code) + assert.Contains(t, rr.Body.String(), "invalid token") + }) + + t.Run("wrong secret is rejected", func(t *testing.T) { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "sub": "user-123", + "iat": time.Now().Unix(), + "exp": time.Now().Add(time.Hour).Unix(), + }) + tokenString, err := token.SignedString([]byte("wrong-secret")) + require.NoError(t, err) + + req := httptest.NewRequest(http.MethodGet, "/instances", nil) + req.Header.Set("Authorization", "Bearer "+tokenString) + + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusUnauthorized, rr.Code) + assert.Contains(t, rr.Body.String(), "invalid token") + }) +} + From 8bafdbeb933ccfc743bf57f314a98de6bf14a1ff Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 12:18:03 -0500 Subject: [PATCH 16/42] Remove deprecated runtime code and add security fixes Build System Cleanup: - Remove deprecated RuntimeNodeJS20/RuntimePython312 constants - Remove Runtime field from Build, CreateBuildRequest, BuildConfig - Remove ToolchainVersion from BuildProvenance - Update OpenAPI spec: remove runtime field, rename /builds/{id}/logs to /builds/{id}/events - Add typed BuildEvent schema for SSE streaming - Remove unused deref function from builds.go - Update documentation (PLAN.md, README.md) to reflect generic builder Security Fixes: - Fix IP spoofing vulnerability: isInternalVMRequest now only trusts r.RemoteAddr - Add registry token rejection to OapiAuthenticationFunc for defense-in-depth Testing: - Add comprehensive build manager unit tests with mocked dependencies - Enhance E2E test script to run VM with built image after successful build - Add --skip-run flag to E2E test for build-only testing - Fix test race conditions by testing storage/queue directly Documentation: - Add lib/builds/TODO.md tracking remaining issues - Mark completed security fixes and improvements --- cmd/api/api/builds.go | 65 +-- lib/builds/PLAN.md | 16 +- lib/builds/TODO.md | 120 +++++ lib/builds/builder_agent/main.go | 25 +- lib/builds/cache_test.go | 26 +- lib/builds/errors.go | 14 - lib/builds/images/README.md | 10 +- lib/builds/images/generic/Dockerfile | 4 +- lib/builds/manager.go | 15 +- lib/builds/manager_test.go | 696 +++++++++++++++++++++++++++ lib/builds/metrics.go | 3 +- lib/builds/queue_test.go | 2 +- lib/builds/storage.go | 2 - lib/builds/types.go | 40 +- lib/middleware/oapi_auth.go | 35 +- lib/oapi/oapi.go | 408 ++++++++-------- openapi.yaml | 61 +-- scripts/e2e-build-test.sh | 247 ++++++++-- 18 files changed, 1338 insertions(+), 451 deletions(-) create mode 100644 lib/builds/TODO.md create mode 100644 lib/builds/manager_test.go diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go index 32e913e2..bc3d8914 100644 --- a/cmd/api/api/builds.go +++ b/cmd/api/api/builds.go @@ -38,7 +38,6 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe // Parse multipart form fields var sourceData []byte - var runtime string var baseImageDigest, cacheScope, dockerfile string var timeoutSeconds int @@ -63,15 +62,6 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe Message: "failed to read source data", }, nil } - case "runtime": - data, err := io.ReadAll(part) - if err != nil { - return oapi.CreateBuild400JSONResponse{ - Code: "invalid_request", - Message: "failed to read runtime field", - }, nil - } - runtime = string(data) case "base_image_digest": data, err := io.ReadAll(part) if err != nil { @@ -114,12 +104,6 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe part.Close() } - // Note: runtime is deprecated and optional. The generic builder accepts any Dockerfile. - // If runtime is empty, we use "generic" as a placeholder for logging/caching purposes. - if runtime == "" { - runtime = "generic" - } - if len(sourceData) == 0 { return oapi.CreateBuild400JSONResponse{ Code: "invalid_request", @@ -132,7 +116,6 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe // Build domain request domainReq := builds.CreateBuildRequest{ - Runtime: runtime, BaseImageDigest: baseImageDigest, CacheScope: cacheScope, Dockerfile: dockerfile, @@ -148,12 +131,6 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe build, err := s.BuildManager.CreateBuild(ctx, domainReq, sourceData) if err != nil { switch { - case errors.Is(err, builds.ErrInvalidRuntime): - // Deprecated: Runtime validation no longer occurs, but kept for compatibility - return oapi.CreateBuild400JSONResponse{ - Code: "invalid_runtime", - Message: err.Error(), - }, nil case errors.Is(err, builds.ErrDockerfileRequired): return oapi.CreateBuild400JSONResponse{ Code: "dockerfile_required", @@ -227,29 +204,28 @@ func (s *ApiService) CancelBuild(ctx context.Context, request oapi.CancelBuildRe return oapi.CancelBuild204Response{}, nil } -// GetBuildLogs streams build logs -func (s *ApiService) GetBuildLogs(ctx context.Context, request oapi.GetBuildLogsRequestObject) (oapi.GetBuildLogsResponseObject, error) { +// GetBuildEvents streams build events +func (s *ApiService) GetBuildEvents(ctx context.Context, request oapi.GetBuildEventsRequestObject) (oapi.GetBuildEventsResponseObject, error) { log := logger.FromContext(ctx) logs, err := s.BuildManager.GetBuildLogs(ctx, request.Id) if err != nil { if errors.Is(err, builds.ErrNotFound) { - return oapi.GetBuildLogs404JSONResponse{ + return oapi.GetBuildEvents404JSONResponse{ Code: "not_found", Message: "build not found", }, nil } - log.ErrorContext(ctx, "failed to get build logs", "error", err, "id", request.Id) - return oapi.GetBuildLogs500JSONResponse{ + log.ErrorContext(ctx, "failed to get build events", "error", err, "id", request.Id) + return oapi.GetBuildEvents500JSONResponse{ Code: "internal_error", - Message: "failed to get build logs", + Message: "failed to get build events", }, nil } - // Return logs as SSE - // For simplicity, return all logs at once - // TODO: Implement proper SSE streaming with follow support - return oapi.GetBuildLogs200TexteventStreamResponse{ + // Return logs as SSE events + // TODO: Implement proper SSE streaming with follow support and typed events + return oapi.GetBuildEvents200TexteventStreamResponse{ Body: stringReader(string(logs)), ContentLength: int64(len(logs)), }, nil @@ -257,15 +233,9 @@ func (s *ApiService) GetBuildLogs(ctx context.Context, request oapi.GetBuildLogs // buildToOAPI converts a domain Build to OAPI Build func buildToOAPI(b *builds.Build) oapi.Build { - var runtimePtr *string - if b.Runtime != "" { - runtimePtr = &b.Runtime - } - oapiBuild := oapi.Build{ Id: b.ID, Status: oapi.BuildStatus(b.Status), - Runtime: runtimePtr, QueuePosition: b.QueuePosition, ImageDigest: b.ImageDigest, ImageRef: b.ImageRef, @@ -278,11 +248,10 @@ func buildToOAPI(b *builds.Build) oapi.Build { if b.Provenance != nil { oapiBuild.Provenance = &oapi.BuildProvenance{ - BaseImageDigest: &b.Provenance.BaseImageDigest, - SourceHash: &b.Provenance.SourceHash, - ToolchainVersion: &b.Provenance.ToolchainVersion, - BuildkitVersion: &b.Provenance.BuildkitVersion, - Timestamp: &b.Provenance.Timestamp, + BaseImageDigest: &b.Provenance.BaseImageDigest, + SourceHash: &b.Provenance.SourceHash, + BuildkitVersion: &b.Provenance.BuildkitVersion, + Timestamp: &b.Provenance.Timestamp, } if len(b.Provenance.LockfileHashes) > 0 { oapiBuild.Provenance.LockfileHashes = &b.Provenance.LockfileHashes @@ -292,14 +261,6 @@ func buildToOAPI(b *builds.Build) oapi.Build { return oapiBuild } -// deref safely dereferences a pointer, returning empty string if nil -func deref(s *string) string { - if s == nil { - return "" - } - return *s -} - // stringReader wraps a string as an io.Reader type stringReaderImpl struct { s string diff --git a/lib/builds/PLAN.md b/lib/builds/PLAN.md index 8fc622f0..f06a80e5 100644 --- a/lib/builds/PLAN.md +++ b/lib/builds/PLAN.md @@ -5,7 +5,7 @@ - ✅ Source-to-image builds in isolated microVMs - ✅ BuildKit-based builds with daemonless execution - ✅ Tenant-isolated registry caching -- ✅ Node.js 20 and Python 3.12 runtimes +- ✅ Generic builder (any Dockerfile/runtime supported) - ✅ Vsock communication for build results - ✅ Cgroup mounting for container runtime support @@ -59,22 +59,14 @@ Export to: - [ ] SBOM (Software Bill of Materials) generation during builds - [ ] Vulnerability scanning of built images before push -### Phase 3: Additional Runtimes +### Phase 3: Security Hardening -| Runtime | Package Managers | Priority | -|---------|-----------------|----------| -| Go 1.22+ | go mod | High | -| Ruby 3.3+ | bundler, gem | Medium | -| Rust | cargo | Medium | -| Java 21+ | Maven, Gradle | Medium | -| PHP 8.3+ | composer | Low | -| Custom Dockerfile | N/A | High | +The generic builder now supports any Dockerfile. Security improvements: -#### Custom Dockerfile Support -- [ ] Allow users to provide their own Dockerfile - [ ] Security review: sandbox custom Dockerfiles more strictly - [ ] Validate Dockerfile doesn't use dangerous instructions - [ ] Consider read-only base image allowlist +- [ ] Rate limiting for build submissions ### Phase 4: Performance & Observability diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md new file mode 100644 index 00000000..94df0cbf --- /dev/null +++ b/lib/builds/TODO.md @@ -0,0 +1,120 @@ +# Build System TODOs + +Outstanding issues and improvements for the build system. + +## ✅ High Priority - Security & Bugs (Completed) + +### 1. ~~IP Spoofing Vulnerability~~ ✅ FIXED + +**File:** `lib/middleware/oapi_auth.go` + +**Issue:** The `isInternalVMRequest` function was reading the `X-Real-IP` header directly from the client request. + +**Fix:** Changed to only use `r.RemoteAddr` as the authoritative source. Added security comment explaining why headers should not be trusted. + +--- + +### 2. ~~Registry Token Scope Leakage~~ ✅ FIXED + +**File:** `lib/middleware/oapi_auth.go` + +**Issue:** Registry tokens could potentially be used on non-registry endpoints. + +**Fix:** Both `JwtAuth` middleware and `OapiAuthenticationFunc` now reject tokens with registry-specific claims (`repos`, `scope`, `build_id`) when used for non-registry API authentication. + +--- + +### 3. ~~Missing Read Deadline on Vsock~~ ✅ ALREADY FIXED + +**File:** `lib/builds/manager.go` + +**Issue:** The `waitForResult` function blocked indefinitely on `decoder.Decode()`. + +**Status:** Already implemented with goroutine pattern + connection close on context cancellation (lines 455-486). + +--- + +## 🟡 Medium Priority - Implementation TODOs + +### 4. SSE Streaming Implementation + +**File:** `cmd/api/api/builds.go` (L227) + +```go +// TODO: Implement proper SSE streaming with follow support and typed events +``` + +**Description:** The `/builds/{id}/events` endpoint should stream typed events (`LogEvent`, `BuildStatusEvent`) with proper SSE formatting, heartbeat events, and `follow` query parameter support. + +--- + +### 5. Build Secrets + +**File:** `lib/builds/builder_agent/main.go` (L239) + +```go +// TODO: Implement bidirectional secret fetching +``` + +**Description:** Allow builds to securely fetch secrets (e.g., npm tokens, pip credentials) via the vsock channel during the build process. + +--- + +## 🟢 Low Priority - Improvements + +### 6. ~~E2E Test Enhancement~~ ✅ DONE + +**File:** `scripts/e2e-build-test.sh` + +**Status:** Enhanced to run a VM with the built image after successful build. The test now: +- Creates an instance from the built image +- Waits for the instance to start +- Executes a test command inside the instance +- Cleans up the instance +- Use `--skip-run` flag to skip the VM test + +### 7. ~~Build Manager Unit Tests~~ ✅ DONE + +**File:** `lib/builds/manager_test.go` + +**Status:** Added comprehensive unit tests with mocked dependencies: +- `TestCreateBuild_Success` - Happy path build creation +- `TestCreateBuild_WithBuildPolicy` - Build with custom policy +- `TestGetBuild_Found/NotFound` - Build retrieval +- `TestListBuilds_Empty/WithBuilds` - Listing builds +- `TestCancelBuild_*` - Cancel scenarios (queued, not found, completed) +- `TestGetBuildLogs_*` - Log retrieval +- `TestBuildQueue_ConcurrencyLimit` - Queue concurrency +- `TestUpdateStatus_*` - Status updates with errors +- `TestRegistryTokenGeneration` - Token generation verification +- `TestCreateBuild_MultipleConcurrent` - Concurrent build creation + +### 8. Guest Agent on Builder VMs + +**Suggestion:** Run the guest-agent on builder VMs to enable `exec` into failed builds for debugging. + +### 9. Builder Image Tooling + +**File:** `lib/builds/images/README.md` + +**Suggestion:** Create a script or tooling for building and publishing new builder images. + +--- + +## ✅ Completed + +- [x] Remove deprecated `RuntimeNodeJS20` and `RuntimePython312` constants +- [x] Remove `Runtime` field from API and storage +- [x] Remove `ToolchainVersion` from `BuildProvenance` +- [x] Update OpenAPI spec to remove runtime field +- [x] Rename `/builds/{id}/logs` to `/builds/{id}/events` with typed events +- [x] Remove unused `deref` function +- [x] Update documentation (README.md, PLAN.md) +- [x] Fix context leak in volume cleanup (use `context.Background()`) +- [x] Fix incorrect error wrapping in config volume setup +- [x] Fix IP spoofing vulnerability in `isInternalVMRequest` +- [x] Add registry token rejection to `OapiAuthenticationFunc` +- [x] Verify vsock read deadline handling (already fixed with goroutine pattern) +- [x] E2E test enhancement - run VM with built image +- [x] Build manager unit tests with mocked dependencies + diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index 5b4d59c1..e2dc3633 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -38,7 +38,6 @@ const ( // BuildConfig matches the BuildConfig type from lib/builds/types.go type BuildConfig struct { JobID string `json:"job_id"` - Runtime string `json:"runtime"` BaseImageDigest string `json:"base_image_digest,omitempty"` RegistryURL string `json:"registry_url"` RegistryToken string `json:"registry_token,omitempty"` @@ -69,12 +68,11 @@ type BuildResult struct { // BuildProvenance records build inputs type BuildProvenance struct { - BaseImageDigest string `json:"base_image_digest"` - SourceHash string `json:"source_hash"` - LockfileHashes map[string]string `json:"lockfile_hashes,omitempty"` - ToolchainVersion string `json:"toolchain_version,omitempty"` - BuildkitVersion string `json:"buildkit_version,omitempty"` - Timestamp time.Time `json:"timestamp"` + BaseImageDigest string `json:"base_image_digest"` + SourceHash string `json:"source_hash"` + LockfileHashes map[string]string `json:"lockfile_hashes,omitempty"` + BuildkitVersion string `json:"buildkit_version,omitempty"` + Timestamp time.Time `json:"timestamp"` } // VsockMessage is the envelope for vsock communication @@ -445,10 +443,9 @@ func extractDigest(metadataPath string) (string, error) { func computeProvenance(config *BuildConfig) BuildProvenance { prov := BuildProvenance{ - BaseImageDigest: config.BaseImageDigest, - LockfileHashes: make(map[string]string), - BuildkitVersion: getBuildkitVersion(), - ToolchainVersion: getToolchainVersion(), + BaseImageDigest: config.BaseImageDigest, + LockfileHashes: make(map[string]string), + BuildkitVersion: getBuildkitVersion(), } // Hash lockfiles @@ -512,9 +509,3 @@ func getBuildkitVersion() string { out, _ := cmd.Output() return strings.TrimSpace(string(out)) } - -func getToolchainVersion() string { - // Generic builder doesn't have runtime-specific toolchains - // The actual runtime version is determined by the user's Dockerfile - return "generic" -} diff --git a/lib/builds/cache_test.go b/lib/builds/cache_test.go index 5bb4eca9..8ca4340a 100644 --- a/lib/builds/cache_test.go +++ b/lib/builds/cache_test.go @@ -21,25 +21,25 @@ func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { { name: "valid nodejs build", tenantScope: "tenant-abc", - runtime: "nodejs20", + runtime: "nodejs", lockfileHashes: map[string]string{ "package-lock.json": "abc123", }, - wantPrefix: "localhost:8080/cache/tenant-abc/nodejs20/", + wantPrefix: "localhost:8080/cache/tenant-abc/nodejs/", }, { name: "valid python build", tenantScope: "my-team", - runtime: "python312", + runtime: "python", lockfileHashes: map[string]string{ "requirements.txt": "def456", }, - wantPrefix: "localhost:8080/cache/my-team/python312/", + wantPrefix: "localhost:8080/cache/my-team/python/", }, { name: "empty tenant scope", tenantScope: "", - runtime: "nodejs20", + runtime: "nodejs", wantErr: true, }, { @@ -54,11 +54,11 @@ func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { { name: "scope with special chars", tenantScope: "My Team!@#$%", - runtime: "nodejs20", + runtime: "nodejs", lockfileHashes: map[string]string{ "package-lock.json": "abc", }, - wantPrefix: "localhost:8080/cache/my-team/nodejs20/", + wantPrefix: "localhost:8080/cache/my-team/nodejs/", }, } @@ -79,17 +79,17 @@ func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { func TestCacheKey_Args(t *testing.T) { key := &CacheKey{ - Reference: "localhost:8080/cache/tenant/nodejs20/abc123", + Reference: "localhost:8080/cache/tenant/nodejs/abc123", TenantScope: "tenant", - Runtime: "nodejs20", + Runtime: "nodejs", LockfileHash: "abc123", } importArg := key.ImportCacheArg() - assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs20/abc123", importArg) + assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs/abc123", importArg) exportArg := key.ExportCacheArg() - assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs20/abc123,mode=max", exportArg) + assert.Equal(t, "type=registry,ref=localhost:8080/cache/tenant/nodejs/abc123,mode=max", exportArg) } func TestValidateCacheScope(t *testing.T) { @@ -169,7 +169,7 @@ func TestGetCacheKeyFromConfig(t *testing.T) { importArg, exportArg, err := GetCacheKeyFromConfig( "localhost:8080", "my-tenant", - "nodejs20", + "nodejs", map[string]string{"package-lock.json": "abc"}, ) require.NoError(t, err) @@ -182,7 +182,7 @@ func TestGetCacheKeyFromConfig(t *testing.T) { importArg, exportArg, err = GetCacheKeyFromConfig( "localhost:8080", "", // Empty = no caching - "nodejs20", + "nodejs", nil, ) require.NoError(t, err) diff --git a/lib/builds/errors.go b/lib/builds/errors.go index da0dabc0..2e8d888d 100644 --- a/lib/builds/errors.go +++ b/lib/builds/errors.go @@ -9,11 +9,6 @@ var ( // ErrAlreadyExists is returned when a build with the same ID already exists ErrAlreadyExists = errors.New("build already exists") - // ErrInvalidRuntime is returned when an unsupported runtime is specified - // Deprecated: Runtime validation is no longer performed. The generic builder - // accepts any Dockerfile. - ErrInvalidRuntime = errors.New("invalid runtime") - // ErrDockerfileRequired is returned when no Dockerfile is provided ErrDockerfileRequired = errors.New("dockerfile required: provide dockerfile parameter or include Dockerfile in source tarball") @@ -38,12 +33,3 @@ var ( // ErrBuildInProgress is returned when trying to cancel a build that's already complete ErrBuildInProgress = errors.New("build in progress") ) - -// IsSupportedRuntime returns true if the runtime is supported. -// Deprecated: This function always returns true. The generic builder system -// no longer validates runtimes - users provide their own Dockerfile. -func IsSupportedRuntime(runtime string) bool { - // Always return true - the generic builder accepts any runtime value - // or no runtime at all. Kept for backward compatibility. - return true -} diff --git a/lib/builds/images/README.md b/lib/builds/images/README.md index c9fc911d..93045573 100644 --- a/lib/builds/images/README.md +++ b/lib/builds/images/README.md @@ -251,10 +251,10 @@ cat ~/hypeman_data_dir/images/docker.io/hirokernel/builder-generic/*/metadata.js cat ~/hypeman_data_dir/system/oci-cache/index.json | jq '.manifests[-1]' ``` -## Migration from Runtime-Specific Images +## Using the Generic Builder -If you were using `nodejs20` or `python312` builder images: +The generic builder accepts any Dockerfile. To use it: -1. **Update your build requests** to include a Dockerfile -2. **The `runtime` parameter is deprecated** - you can still send it but it's ignored -3. **Configure `BUILDER_IMAGE`** to use the generic builder +1. **Include a Dockerfile** in your source tarball (or pass it via the `dockerfile` parameter) +2. **Your Dockerfile specifies the runtime** - e.g., `FROM node:20-alpine` or `FROM python:3.12-slim` +3. **Configure `BUILDER_IMAGE`** in your `.env` to point to the generic builder image diff --git a/lib/builds/images/generic/Dockerfile b/lib/builds/images/generic/Dockerfile index e1bc89c4..f2f40418 100644 --- a/lib/builds/images/generic/Dockerfile +++ b/lib/builds/images/generic/Dockerfile @@ -1,6 +1,6 @@ # Generic Builder Image -# Contains rootless BuildKit + builder agent - runtime agnostic -# Users provide their own Dockerfile which specifies the runtime (node, python, etc.) +# Contains rootless BuildKit + builder agent +# Builds any Dockerfile provided by the user FROM moby/buildkit:rootless AS buildkit diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 4d6a3d44..de3afbf7 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -140,12 +140,7 @@ func (m *manager) Start(ctx context.Context) error { // CreateBuild starts a new build job func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) { - m.logger.Info("creating build", "runtime", req.Runtime) - - // Validate runtime - if !IsSupportedRuntime(req.Runtime) { - return nil, fmt.Errorf("%w: %s", ErrInvalidRuntime, req.Runtime) - } + m.logger.Info("creating build") // Apply defaults to build policy policy := req.BuildPolicy @@ -166,7 +161,6 @@ func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourc meta := &buildMetadata{ ID: id, Status: StatusQueued, - Runtime: req.Runtime, Request: &req, CreatedAt: time.Now(), } @@ -201,7 +195,6 @@ func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourc // Write build config for the builder agent buildConfig := &BuildConfig{ JobID: id, - Runtime: req.Runtime, BaseImageDigest: req.BaseImageDigest, RegistryURL: m.config.RegistryURL, RegistryToken: registryToken, @@ -267,7 +260,7 @@ func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildReques errMsg := err.Error() m.updateBuildComplete(id, StatusFailed, nil, &errMsg, nil, &durationMS) if m.metrics != nil { - m.metrics.RecordBuild(ctx, "failed", req.Runtime, duration) + m.metrics.RecordBuild(ctx, "failed", duration) } return } @@ -283,7 +276,7 @@ func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildReques m.logger.Error("build failed", "id", id, "error", result.Error, "duration", duration) m.updateBuildComplete(id, StatusFailed, nil, &result.Error, &result.Provenance, &durationMS) if m.metrics != nil { - m.metrics.RecordBuild(ctx, "failed", req.Runtime, duration) + m.metrics.RecordBuild(ctx, "failed", duration) } return } @@ -299,7 +292,7 @@ func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildReques } if m.metrics != nil { - m.metrics.RecordBuild(ctx, "success", req.Runtime, duration) + m.metrics.RecordBuild(ctx, "success", duration) } } diff --git a/lib/builds/manager_test.go b/lib/builds/manager_test.go new file mode 100644 index 00000000..ef5a369c --- /dev/null +++ b/lib/builds/manager_test.go @@ -0,0 +1,696 @@ +package builds + +import ( + "context" + "encoding/json" + "io" + "log/slog" + "os" + "path/filepath" + "testing" + "time" + + "github.com/onkernel/hypeman/lib/instances" + "github.com/onkernel/hypeman/lib/paths" + "github.com/onkernel/hypeman/lib/resources" + "github.com/onkernel/hypeman/lib/volumes" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// mockInstanceManager implements instances.Manager for testing +type mockInstanceManager struct { + instances map[string]*instances.Instance + createFunc func(ctx context.Context, req instances.CreateInstanceRequest) (*instances.Instance, error) + getFunc func(ctx context.Context, id string) (*instances.Instance, error) + deleteFunc func(ctx context.Context, id string) error + stopFunc func(ctx context.Context, id string) (*instances.Instance, error) + createCallCount int + deleteCallCount int +} + +func newMockInstanceManager() *mockInstanceManager { + return &mockInstanceManager{ + instances: make(map[string]*instances.Instance), + } +} + +func (m *mockInstanceManager) ListInstances(ctx context.Context) ([]instances.Instance, error) { + var result []instances.Instance + for _, inst := range m.instances { + result = append(result, *inst) + } + return result, nil +} + +func (m *mockInstanceManager) CreateInstance(ctx context.Context, req instances.CreateInstanceRequest) (*instances.Instance, error) { + m.createCallCount++ + if m.createFunc != nil { + return m.createFunc(ctx, req) + } + inst := &instances.Instance{ + StoredMetadata: instances.StoredMetadata{ + Id: "inst-" + req.Name, + Name: req.Name, + }, + State: instances.StateRunning, + } + m.instances[inst.Id] = inst + return inst, nil +} + +func (m *mockInstanceManager) GetInstance(ctx context.Context, id string) (*instances.Instance, error) { + if m.getFunc != nil { + return m.getFunc(ctx, id) + } + if inst, ok := m.instances[id]; ok { + return inst, nil + } + return nil, instances.ErrNotFound +} + +func (m *mockInstanceManager) DeleteInstance(ctx context.Context, id string) error { + m.deleteCallCount++ + if m.deleteFunc != nil { + return m.deleteFunc(ctx, id) + } + delete(m.instances, id) + return nil +} + +func (m *mockInstanceManager) StandbyInstance(ctx context.Context, id string) (*instances.Instance, error) { + return nil, nil +} + +func (m *mockInstanceManager) RestoreInstance(ctx context.Context, id string) (*instances.Instance, error) { + return nil, nil +} + +func (m *mockInstanceManager) StopInstance(ctx context.Context, id string) (*instances.Instance, error) { + if m.stopFunc != nil { + return m.stopFunc(ctx, id) + } + if inst, ok := m.instances[id]; ok { + inst.State = instances.StateStopped + return inst, nil + } + return nil, instances.ErrNotFound +} + +func (m *mockInstanceManager) StartInstance(ctx context.Context, id string) (*instances.Instance, error) { + return nil, nil +} + +func (m *mockInstanceManager) StreamInstanceLogs(ctx context.Context, id string, tail int, follow bool, source instances.LogSource) (<-chan string, error) { + return nil, nil +} + +func (m *mockInstanceManager) RotateLogs(ctx context.Context, maxBytes int64, maxFiles int) error { + return nil +} + +func (m *mockInstanceManager) AttachVolume(ctx context.Context, id string, volumeId string, req instances.AttachVolumeRequest) (*instances.Instance, error) { + return nil, nil +} + +func (m *mockInstanceManager) DetachVolume(ctx context.Context, id string, volumeId string) (*instances.Instance, error) { + return nil, nil +} + +func (m *mockInstanceManager) ListInstanceAllocations(ctx context.Context) ([]resources.InstanceAllocation, error) { + return nil, nil +} + +// mockVolumeManager implements volumes.Manager for testing +type mockVolumeManager struct { + volumes map[string]*volumes.Volume + createFunc func(ctx context.Context, req volumes.CreateVolumeRequest) (*volumes.Volume, error) + createFromArchiveFunc func(ctx context.Context, req volumes.CreateVolumeFromArchiveRequest, archive io.Reader) (*volumes.Volume, error) + deleteFunc func(ctx context.Context, id string) error + createCallCount int + deleteCallCount int +} + +func newMockVolumeManager() *mockVolumeManager { + return &mockVolumeManager{ + volumes: make(map[string]*volumes.Volume), + } +} + +func (m *mockVolumeManager) ListVolumes(ctx context.Context) ([]volumes.Volume, error) { + var result []volumes.Volume + for _, vol := range m.volumes { + result = append(result, *vol) + } + return result, nil +} + +func (m *mockVolumeManager) CreateVolume(ctx context.Context, req volumes.CreateVolumeRequest) (*volumes.Volume, error) { + m.createCallCount++ + if m.createFunc != nil { + return m.createFunc(ctx, req) + } + vol := &volumes.Volume{ + Id: "vol-" + req.Name, + Name: req.Name, + } + m.volumes[vol.Id] = vol + return vol, nil +} + +func (m *mockVolumeManager) CreateVolumeFromArchive(ctx context.Context, req volumes.CreateVolumeFromArchiveRequest, archive io.Reader) (*volumes.Volume, error) { + m.createCallCount++ + if m.createFromArchiveFunc != nil { + return m.createFromArchiveFunc(ctx, req, archive) + } + vol := &volumes.Volume{ + Id: "vol-" + req.Name, + Name: req.Name, + } + m.volumes[vol.Id] = vol + return vol, nil +} + +func (m *mockVolumeManager) GetVolume(ctx context.Context, id string) (*volumes.Volume, error) { + if vol, ok := m.volumes[id]; ok { + return vol, nil + } + return nil, volumes.ErrNotFound +} + +func (m *mockVolumeManager) GetVolumeByName(ctx context.Context, name string) (*volumes.Volume, error) { + for _, vol := range m.volumes { + if vol.Name == name { + return vol, nil + } + } + return nil, volumes.ErrNotFound +} + +func (m *mockVolumeManager) DeleteVolume(ctx context.Context, id string) error { + m.deleteCallCount++ + if m.deleteFunc != nil { + return m.deleteFunc(ctx, id) + } + delete(m.volumes, id) + return nil +} + +func (m *mockVolumeManager) AttachVolume(ctx context.Context, id string, req volumes.AttachVolumeRequest) error { + return nil +} + +func (m *mockVolumeManager) DetachVolume(ctx context.Context, volumeID string, instanceID string) error { + return nil +} + +func (m *mockVolumeManager) GetVolumePath(id string) string { + return "/tmp/volumes/" + id +} + +func (m *mockVolumeManager) TotalVolumeBytes(ctx context.Context) (int64, error) { + return 0, nil +} + +// mockSecretProvider implements SecretProvider for testing +type mockSecretProvider struct{} + +func (m *mockSecretProvider) GetSecrets(ctx context.Context, secretIDs []string) (map[string]string, error) { + return make(map[string]string), nil +} + +// Test helper to create a manager with test paths and mocks +func setupTestManager(t *testing.T) (*manager, *mockInstanceManager, *mockVolumeManager, string) { + t.Helper() + + // Create temp directory for test data + tempDir, err := os.MkdirTemp("", "builds-test-*") + require.NoError(t, err) + + // Create paths + p := paths.New(tempDir) + + // Create necessary directories + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "builds"), 0755)) + + // Create mocks + instanceMgr := newMockInstanceManager() + volumeMgr := newMockVolumeManager() + secretProvider := &mockSecretProvider{} + + // Create config + config := Config{ + MaxConcurrentBuilds: 2, + BuilderImage: "test/builder:latest", + RegistryURL: "localhost:5000", + DefaultTimeout: 300, + RegistrySecret: "test-secret-key", + } + + // Create a discard logger for tests + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) + + // Create manager (without calling NewManager to avoid RecoverPendingBuilds) + mgr := &manager{ + config: config, + paths: p, + queue: NewBuildQueue(config.MaxConcurrentBuilds), + instanceManager: instanceMgr, + volumeManager: volumeMgr, + secretProvider: secretProvider, + tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), + logger: logger, + } + + return mgr, instanceMgr, volumeMgr, tempDir +} + +func TestCreateBuild_Success(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + req := CreateBuildRequest{ + CacheScope: "test-scope", + Dockerfile: "FROM alpine\nRUN echo hello", + } + sourceData := []byte("fake-tarball-data") + + build, err := mgr.CreateBuild(ctx, req, sourceData) + + require.NoError(t, err) + assert.NotEmpty(t, build.ID) + assert.Equal(t, StatusQueued, build.Status) + assert.NotNil(t, build.CreatedAt) + + // Verify source was stored + sourcePath := filepath.Join(tempDir, "builds", build.ID, "source", "source.tar.gz") + data, err := os.ReadFile(sourcePath) + require.NoError(t, err) + assert.Equal(t, sourceData, data) + + // Verify config was written + configPath := filepath.Join(tempDir, "builds", build.ID, "config.json") + _, err = os.Stat(configPath) + assert.NoError(t, err) + + // Verify metadata was written + metaPath := filepath.Join(tempDir, "builds", build.ID, "metadata.json") + _, err = os.Stat(metaPath) + assert.NoError(t, err) +} + +func TestCreateBuild_WithBuildPolicy(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + timeout := 600 + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + BuildPolicy: &BuildPolicy{ + TimeoutSeconds: timeout, + NetworkMode: "host", + }, + } + + build, err := mgr.CreateBuild(ctx, req, []byte("source")) + + require.NoError(t, err) + assert.NotEmpty(t, build.ID) +} + +func TestGetBuild_Found(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build first + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + } + created, err := mgr.CreateBuild(ctx, req, []byte("source")) + require.NoError(t, err) + + // Get the build + build, err := mgr.GetBuild(ctx, created.ID) + + require.NoError(t, err) + assert.Equal(t, created.ID, build.ID) + assert.Equal(t, StatusQueued, build.Status) +} + +func TestGetBuild_NotFound(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + _, err := mgr.GetBuild(ctx, "nonexistent-id") + + assert.Error(t, err) + assert.ErrorIs(t, err, ErrNotFound) +} + +func TestListBuilds_Empty(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + builds, err := mgr.ListBuilds(ctx) + + require.NoError(t, err) + assert.Empty(t, builds) +} + +func TestListBuilds_WithBuilds(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create multiple builds + for i := 0; i < 3; i++ { + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + } + _, err := mgr.CreateBuild(ctx, req, []byte("source")) + require.NoError(t, err) + } + + builds, err := mgr.ListBuilds(ctx) + + require.NoError(t, err) + assert.Len(t, builds, 3) +} + +func TestCancelBuild_QueuedBuild(t *testing.T) { + // Test the queue cancellation directly to avoid race conditions + queue := NewBuildQueue(1) // Only 1 concurrent + + started := make(chan struct{}) + + // Add a blocking build to fill the single slot + queue.Enqueue("build-1", CreateBuildRequest{}, func() { + started <- struct{}{} + select {} // Block forever + }) + + // Wait for first build to start + <-started + + // Add a second build - this one should be queued + queue.Enqueue("build-2", CreateBuildRequest{}, func() {}) + + // Verify it's pending + assert.Equal(t, 1, queue.PendingCount()) + + // Cancel the queued build + cancelled := queue.Cancel("build-2") + assert.True(t, cancelled) + + // Verify it's removed from pending + assert.Equal(t, 0, queue.PendingCount()) +} + +func TestCancelBuild_NotFound(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + err := mgr.CancelBuild(ctx, "nonexistent-id") + + assert.Error(t, err) + assert.ErrorIs(t, err, ErrNotFound) +} + +func TestCancelBuild_AlreadyCompleted(t *testing.T) { + // Test cancel rejection for completed builds by directly setting up metadata + tempDir, err := os.MkdirTemp("", "builds-test-*") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + p := paths.New(tempDir) + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "builds", "completed-build"), 0755)) + + // Create metadata with completed status + meta := &buildMetadata{ + ID: "completed-build", + Status: StatusReady, + CreatedAt: time.Now(), + } + require.NoError(t, writeMetadata(p, meta)) + + // Create manager + config := Config{ + MaxConcurrentBuilds: 2, + RegistrySecret: "test-secret", + } + mgr := &manager{ + config: config, + paths: p, + queue: NewBuildQueue(config.MaxConcurrentBuilds), + tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), + } + + // Try to cancel - should fail because it's already completed + err = mgr.CancelBuild(context.Background(), "completed-build") + + require.Error(t, err, "expected error when cancelling completed build") + assert.Contains(t, err.Error(), "already completed") +} + +func TestGetBuildLogs_Empty(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + } + build, err := mgr.CreateBuild(ctx, req, []byte("source")) + require.NoError(t, err) + + // Get logs (should be empty initially) + logs, err := mgr.GetBuildLogs(ctx, build.ID) + + require.NoError(t, err) + assert.Empty(t, logs) +} + +func TestGetBuildLogs_WithLogs(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + } + build, err := mgr.CreateBuild(ctx, req, []byte("source")) + require.NoError(t, err) + + // Append some logs + logData := []byte("Step 1: FROM alpine\nStep 2: RUN echo hello\n") + err = appendLog(mgr.paths, build.ID, logData) + require.NoError(t, err) + + // Get logs + logs, err := mgr.GetBuildLogs(ctx, build.ID) + + require.NoError(t, err) + assert.Equal(t, logData, logs) +} + +func TestGetBuildLogs_NotFound(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + _, err := mgr.GetBuildLogs(ctx, "nonexistent-id") + + assert.Error(t, err) +} + +func TestBuildQueue_ConcurrencyLimit(t *testing.T) { + // Test the queue directly rather than through the manager + // because the manager's runBuild goroutine completes quickly with mocks + queue := NewBuildQueue(2) // Max 2 concurrent + + started := make(chan string, 5) + + // Enqueue 5 builds with blocking start functions + for i := 0; i < 5; i++ { + id := string(rune('A' + i)) + queue.Enqueue(id, CreateBuildRequest{}, func() { + started <- id + // Block until test completes - simulates long-running build + select {} + }) + } + + // Give goroutines time to start + for i := 0; i < 2; i++ { + <-started + } + + // First 2 should be active, rest should be pending + active := queue.ActiveCount() + pending := queue.PendingCount() + assert.Equal(t, 2, active, "expected 2 active builds") + assert.Equal(t, 3, pending, "expected 3 pending builds") +} + +func TestUpdateStatus(t *testing.T) { + // Test the updateStatus function directly using storage functions + // This avoids race conditions with the build queue goroutines + tempDir, err := os.MkdirTemp("", "builds-test-*") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + p := paths.New(tempDir) + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "builds", "test-build-1"), 0755)) + + // Create initial metadata + meta := &buildMetadata{ + ID: "test-build-1", + Status: StatusQueued, + CreatedAt: time.Now(), + } + require.NoError(t, writeMetadata(p, meta)) + + // Update status + meta.Status = StatusBuilding + now := time.Now() + meta.StartedAt = &now + require.NoError(t, writeMetadata(p, meta)) + + // Read back and verify + readMeta, err := readMetadata(p, "test-build-1") + require.NoError(t, err) + assert.Equal(t, StatusBuilding, readMeta.Status) + assert.NotNil(t, readMeta.StartedAt) +} + +func TestUpdateStatus_WithError(t *testing.T) { + // Test status updates with error message directly + tempDir, err := os.MkdirTemp("", "builds-test-*") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + p := paths.New(tempDir) + require.NoError(t, os.MkdirAll(filepath.Join(tempDir, "builds", "test-build-1"), 0755)) + + // Create initial metadata + meta := &buildMetadata{ + ID: "test-build-1", + Status: StatusQueued, + CreatedAt: time.Now(), + } + require.NoError(t, writeMetadata(p, meta)) + + // Update status with error + errMsg := "build failed: out of memory" + meta.Status = StatusFailed + meta.Error = &errMsg + require.NoError(t, writeMetadata(p, meta)) + + // Read back and verify + readMeta, err := readMetadata(p, "test-build-1") + require.NoError(t, err) + assert.Equal(t, StatusFailed, readMeta.Status) + require.NotNil(t, readMeta.Error) + assert.Contains(t, *readMeta.Error, "out of memory") +} + +func TestRegistryTokenGeneration(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build with cache scope + req := CreateBuildRequest{ + CacheScope: "my-cache", + Dockerfile: "FROM alpine", + } + build, err := mgr.CreateBuild(ctx, req, []byte("source")) + require.NoError(t, err) + + // Read the build config and verify token was generated + configPath := filepath.Join(tempDir, "builds", build.ID, "config.json") + data, err := os.ReadFile(configPath) + require.NoError(t, err) + + var config BuildConfig + err = json.Unmarshal(data, &config) + require.NoError(t, err) + + assert.NotEmpty(t, config.RegistryToken) + assert.Equal(t, "localhost:5000", config.RegistryURL) +} + +func TestStart(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Start should succeed without error + err := mgr.Start(ctx) + + assert.NoError(t, err) +} + +func TestCreateBuild_MultipleConcurrent(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create builds in parallel + done := make(chan *Build, 5) + errs := make(chan error, 5) + + for i := 0; i < 5; i++ { + go func() { + req := CreateBuildRequest{ + Dockerfile: "FROM alpine", + } + build, err := mgr.CreateBuild(ctx, req, []byte("source")) + if err != nil { + errs <- err + } else { + done <- build + } + }() + } + + // Collect results + var builds []*Build + for i := 0; i < 5; i++ { + select { + case b := <-done: + builds = append(builds, b) + case err := <-errs: + t.Fatalf("unexpected error: %v", err) + } + } + + assert.Len(t, builds, 5) + + // Verify all IDs are unique + ids := make(map[string]bool) + for _, b := range builds { + assert.False(t, ids[b.ID], "duplicate build ID: %s", b.ID) + ids[b.ID] = true + } +} diff --git a/lib/builds/metrics.go b/lib/builds/metrics.go index f6c4227b..92d3c029 100644 --- a/lib/builds/metrics.go +++ b/lib/builds/metrics.go @@ -60,10 +60,9 @@ func NewMetrics(meter metric.Meter) (*Metrics, error) { } // RecordBuild records metrics for a completed build -func (m *Metrics) RecordBuild(ctx context.Context, status string, runtime string, duration time.Duration) { +func (m *Metrics) RecordBuild(ctx context.Context, status string, duration time.Duration) { attrs := []attribute.KeyValue{ attribute.String("status", status), - attribute.String("runtime", runtime), } m.buildDuration.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...)) diff --git a/lib/builds/queue_test.go b/lib/builds/queue_test.go index d5f3fd6e..5f5dd9af 100644 --- a/lib/builds/queue_test.go +++ b/lib/builds/queue_test.go @@ -16,7 +16,7 @@ func TestBuildQueue_EnqueueStartsImmediately(t *testing.T) { done := make(chan struct{}) // Enqueue first build - should start immediately - pos := queue.Enqueue("build-1", CreateBuildRequest{Runtime: "nodejs20"}, func() { + pos := queue.Enqueue("build-1", CreateBuildRequest{}, func() { started <- "build-1" <-done // Wait for signal }) diff --git a/lib/builds/storage.go b/lib/builds/storage.go index 1d56055a..777382bb 100644 --- a/lib/builds/storage.go +++ b/lib/builds/storage.go @@ -14,7 +14,6 @@ import ( type buildMetadata struct { ID string `json:"id"` Status string `json:"status"` - Runtime string `json:"runtime"` Request *CreateBuildRequest `json:"request,omitempty"` ImageDigest *string `json:"image_digest,omitempty"` ImageRef *string `json:"image_ref,omitempty"` @@ -32,7 +31,6 @@ func (m *buildMetadata) toBuild() *Build { return &Build{ ID: m.ID, Status: m.Status, - Runtime: m.Runtime, ImageDigest: m.ImageDigest, ImageRef: m.ImageRef, Error: m.Error, diff --git a/lib/builds/types.go b/lib/builds/types.go index 7ee5b821..310e7124 100644 --- a/lib/builds/types.go +++ b/lib/builds/types.go @@ -14,36 +14,23 @@ const ( StatusCancelled = "cancelled" ) -// Runtime constants (deprecated - kept for backward compatibility) -// The generic builder system no longer requires runtime selection. -// Users provide their own Dockerfile which specifies the runtime. -const ( - RuntimeNodeJS20 = "nodejs20" // Deprecated - RuntimePython312 = "python312" // Deprecated -) - // Build represents a source-to-image build job type Build struct { - ID string `json:"id"` - Status string `json:"status"` - Runtime string `json:"runtime"` - QueuePosition *int `json:"queue_position,omitempty"` - ImageDigest *string `json:"image_digest,omitempty"` - ImageRef *string `json:"image_ref,omitempty"` - Error *string `json:"error,omitempty"` + ID string `json:"id"` + Status string `json:"status"` + QueuePosition *int `json:"queue_position,omitempty"` + ImageDigest *string `json:"image_digest,omitempty"` + ImageRef *string `json:"image_ref,omitempty"` + Error *string `json:"error,omitempty"` Provenance *BuildProvenance `json:"provenance,omitempty"` - CreatedAt time.Time `json:"created_at"` - StartedAt *time.Time `json:"started_at,omitempty"` - CompletedAt *time.Time `json:"completed_at,omitempty"` - DurationMS *int64 `json:"duration_ms,omitempty"` + CreatedAt time.Time `json:"created_at"` + StartedAt *time.Time `json:"started_at,omitempty"` + CompletedAt *time.Time `json:"completed_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` } // CreateBuildRequest represents a request to create a new build type CreateBuildRequest struct { - // Runtime is deprecated. Kept for backward compatibility but no longer required. - // The generic builder system accepts any Dockerfile. - Runtime string `json:"runtime,omitempty"` - // Dockerfile content. Required if not included in the source tarball. // The Dockerfile specifies the runtime (e.g., FROM node:20-alpine). Dockerfile string `json:"dockerfile,omitempty"` @@ -107,9 +94,6 @@ type BuildProvenance struct { // LockfileHashes maps lockfile names to their SHA256 hashes LockfileHashes map[string]string `json:"lockfile_hashes,omitempty"` - // ToolchainVersion is the runtime version (e.g., "node v20.10.0") - ToolchainVersion string `json:"toolchain_version,omitempty"` - // BuildkitVersion is the BuildKit version used BuildkitVersion string `json:"buildkit_version,omitempty"` @@ -123,9 +107,6 @@ type BuildConfig struct { // JobID is the build job identifier JobID string `json:"job_id"` - // Runtime is deprecated, kept for logging purposes only - Runtime string `json:"runtime,omitempty"` - // Dockerfile content (if not provided in source tarball) Dockerfile string `json:"dockerfile,omitempty"` @@ -205,4 +186,3 @@ func (p *BuildPolicy) ApplyDefaults() { p.NetworkMode = defaults.NetworkMode } } - diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 40245f63..430e822a 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -79,6 +79,21 @@ func OapiAuthenticationFunc(jwtSecret string) openapi3filter.AuthenticationFunc return fmt.Errorf("invalid token") } + // Reject registry tokens - they should not be used for API authentication. + // Registry tokens have specific claims (repos, scope, build_id) that user tokens don't have. + if _, hasRepos := claims["repos"]; hasRepos { + log.DebugContext(ctx, "rejected registry token used for API auth") + return fmt.Errorf("invalid token type") + } + if _, hasScope := claims["scope"]; hasScope { + log.DebugContext(ctx, "rejected registry token used for API auth") + return fmt.Errorf("invalid token type") + } + if _, hasBuildID := claims["build_id"]; hasBuildID { + log.DebugContext(ctx, "rejected registry token used for API auth") + return fmt.Errorf("invalid token type") + } + // Extract user ID from claims and add to context var userID string if sub, ok := claims["sub"].(string); ok { @@ -167,17 +182,17 @@ func isRegistryPath(path string) bool { } // isInternalVMRequest checks if the request is from an internal VM network (10.102.x.x) -// This is used as a fallback for builder VMs that don't have token auth yet +// This is used as a fallback for builder VMs that don't have token auth yet. +// +// SECURITY: We only trust RemoteAddr, not X-Real-IP or X-Forwarded-For headers, +// as those can be spoofed by attackers to bypass authentication. func isInternalVMRequest(r *http.Request) bool { - // Get the real client IP (RealIP middleware sets X-Real-IP) - ip := r.Header.Get("X-Real-IP") - if ip == "" { - // Fall back to RemoteAddr - ip = r.RemoteAddr - // Remove port if present - if idx := strings.LastIndex(ip, ":"); idx != -1 { - ip = ip[:idx] - } + // Use only RemoteAddr - never trust client-supplied headers for auth decisions + ip := r.RemoteAddr + + // RemoteAddr is "IP:port" format, extract just the IP + if idx := strings.LastIndex(ip, ":"); idx != -1 { + ip = ip[:idx] } // Check if it's from the VM network (10.102.x.x) diff --git a/lib/oapi/oapi.go b/lib/oapi/oapi.go index e0e3b201..5154aa97 100644 --- a/lib/oapi/oapi.go +++ b/lib/oapi/oapi.go @@ -29,6 +29,13 @@ const ( BearerAuthScopes = "bearerAuth.Scopes" ) +// Defines values for BuildEventType. +const ( + Heartbeat BuildEventType = "heartbeat" + Log BuildEventType = "log" + Status BuildEventType = "status" +) + // Defines values for BuildStatus. const ( BuildStatusBuilding BuildStatus = "building" @@ -149,9 +156,6 @@ type Build struct { // QueuePosition Position in build queue (only when status is queued) QueuePosition *int `json:"queue_position"` - // Runtime (Deprecated) Build runtime hint - Runtime *string `json:"runtime,omitempty"` - // StartedAt Build start timestamp StartedAt *time.Time `json:"started_at"` @@ -159,6 +163,24 @@ type Build struct { Status BuildStatus `json:"status"` } +// BuildEvent defines model for BuildEvent. +type BuildEvent struct { + // Content Log line content (only for type=log) + Content *string `json:"content,omitempty"` + + // Status Build job status + Status *BuildStatus `json:"status,omitempty"` + + // Timestamp Event timestamp + Timestamp time.Time `json:"timestamp"` + + // Type Event type + Type BuildEventType `json:"type"` +} + +// BuildEventType Event type +type BuildEventType string + // BuildProvenance defines model for BuildProvenance. type BuildProvenance struct { // BaseImageDigest Pinned base image digest used @@ -175,9 +197,6 @@ type BuildProvenance struct { // Timestamp Build completion timestamp Timestamp *time.Time `json:"timestamp,omitempty"` - - // ToolchainVersion Runtime version (e.g., "node v20.10.0") - ToolchainVersion *string `json:"toolchain_version,omitempty"` } // BuildStatus Build job status @@ -686,9 +705,6 @@ type CreateBuildMultipartBody struct { // Dockerfile Dockerfile content. Required if not included in the source tarball. Dockerfile *string `json:"dockerfile,omitempty"` - // Runtime (Deprecated) Build runtime hint. No longer required. - Runtime *string `json:"runtime,omitempty"` - // Source Source tarball (tar.gz) containing application code and optionally a Dockerfile Source openapi_types.File `json:"source"` @@ -696,9 +712,9 @@ type CreateBuildMultipartBody struct { TimeoutSeconds *int `json:"timeout_seconds,omitempty"` } -// GetBuildLogsParams defines parameters for GetBuildLogs. -type GetBuildLogsParams struct { - // Follow Continue streaming new lines after initial output +// GetBuildEventsParams defines parameters for GetBuildEvents. +type GetBuildEventsParams struct { + // Follow Continue streaming new events after initial output Follow *bool `form:"follow,omitempty" json:"follow,omitempty"` } @@ -853,8 +869,8 @@ type ClientInterface interface { // GetBuild request GetBuild(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*http.Response, error) - // GetBuildLogs request - GetBuildLogs(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*http.Response, error) + // GetBuildEvents request + GetBuildEvents(ctx context.Context, id string, params *GetBuildEventsParams, reqEditors ...RequestEditorFn) (*http.Response, error) // ListDevices request ListDevices(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) @@ -1010,8 +1026,8 @@ func (c *Client) GetBuild(ctx context.Context, id string, reqEditors ...RequestE return c.Client.Do(req) } -func (c *Client) GetBuildLogs(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*http.Response, error) { - req, err := NewGetBuildLogsRequest(c.Server, id, params) +func (c *Client) GetBuildEvents(ctx context.Context, id string, params *GetBuildEventsParams, reqEditors ...RequestEditorFn) (*http.Response, error) { + req, err := NewGetBuildEventsRequest(c.Server, id, params) if err != nil { return nil, err } @@ -1590,8 +1606,8 @@ func NewGetBuildRequest(server string, id string) (*http.Request, error) { return req, nil } -// NewGetBuildLogsRequest generates requests for GetBuildLogs -func NewGetBuildLogsRequest(server string, id string, params *GetBuildLogsParams) (*http.Request, error) { +// NewGetBuildEventsRequest generates requests for GetBuildEvents +func NewGetBuildEventsRequest(server string, id string, params *GetBuildEventsParams) (*http.Request, error) { var err error var pathParam0 string @@ -1606,7 +1622,7 @@ func NewGetBuildLogsRequest(server string, id string, params *GetBuildLogsParams return nil, err } - operationPath := fmt.Sprintf("/builds/%s/logs", pathParam0) + operationPath := fmt.Sprintf("/builds/%s/events", pathParam0) if operationPath[0] == '/' { operationPath = "." + operationPath } @@ -2844,8 +2860,8 @@ type ClientWithResponsesInterface interface { // GetBuildWithResponse request GetBuildWithResponse(ctx context.Context, id string, reqEditors ...RequestEditorFn) (*GetBuildResponse, error) - // GetBuildLogsWithResponse request - GetBuildLogsWithResponse(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*GetBuildLogsResponse, error) + // GetBuildEventsWithResponse request + GetBuildEventsWithResponse(ctx context.Context, id string, params *GetBuildEventsParams, reqEditors ...RequestEditorFn) (*GetBuildEventsResponse, error) // ListDevicesWithResponse request ListDevicesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ListDevicesResponse, error) @@ -3050,7 +3066,7 @@ func (r GetBuildResponse) StatusCode() int { return 0 } -type GetBuildLogsResponse struct { +type GetBuildEventsResponse struct { Body []byte HTTPResponse *http.Response JSON404 *Error @@ -3058,7 +3074,7 @@ type GetBuildLogsResponse struct { } // Status returns HTTPResponse.Status -func (r GetBuildLogsResponse) Status() string { +func (r GetBuildEventsResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } @@ -3066,7 +3082,7 @@ func (r GetBuildLogsResponse) Status() string { } // StatusCode returns HTTPResponse.StatusCode -func (r GetBuildLogsResponse) StatusCode() int { +func (r GetBuildEventsResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } @@ -3864,13 +3880,13 @@ func (c *ClientWithResponses) GetBuildWithResponse(ctx context.Context, id strin return ParseGetBuildResponse(rsp) } -// GetBuildLogsWithResponse request returning *GetBuildLogsResponse -func (c *ClientWithResponses) GetBuildLogsWithResponse(ctx context.Context, id string, params *GetBuildLogsParams, reqEditors ...RequestEditorFn) (*GetBuildLogsResponse, error) { - rsp, err := c.GetBuildLogs(ctx, id, params, reqEditors...) +// GetBuildEventsWithResponse request returning *GetBuildEventsResponse +func (c *ClientWithResponses) GetBuildEventsWithResponse(ctx context.Context, id string, params *GetBuildEventsParams, reqEditors ...RequestEditorFn) (*GetBuildEventsResponse, error) { + rsp, err := c.GetBuildEvents(ctx, id, params, reqEditors...) if err != nil { return nil, err } - return ParseGetBuildLogsResponse(rsp) + return ParseGetBuildEventsResponse(rsp) } // ListDevicesWithResponse request returning *ListDevicesResponse @@ -4367,15 +4383,15 @@ func ParseGetBuildResponse(rsp *http.Response) (*GetBuildResponse, error) { return response, nil } -// ParseGetBuildLogsResponse parses an HTTP response from a GetBuildLogsWithResponse call -func ParseGetBuildLogsResponse(rsp *http.Response) (*GetBuildLogsResponse, error) { +// ParseGetBuildEventsResponse parses an HTTP response from a GetBuildEventsWithResponse call +func ParseGetBuildEventsResponse(rsp *http.Response) (*GetBuildEventsResponse, error) { bodyBytes, err := io.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } - response := &GetBuildLogsResponse{ + response := &GetBuildEventsResponse{ Body: bodyBytes, HTTPResponse: rsp, } @@ -5731,9 +5747,9 @@ type ServerInterface interface { // Get build details // (GET /builds/{id}) GetBuild(w http.ResponseWriter, r *http.Request, id string) - // Stream build logs (SSE) - // (GET /builds/{id}/logs) - GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) + // Stream build events (SSE) + // (GET /builds/{id}/events) + GetBuildEvents(w http.ResponseWriter, r *http.Request, id string, params GetBuildEventsParams) // List registered devices // (GET /devices) ListDevices(w http.ResponseWriter, r *http.Request) @@ -5857,9 +5873,9 @@ func (_ Unimplemented) GetBuild(w http.ResponseWriter, r *http.Request, id strin w.WriteHeader(http.StatusNotImplemented) } -// Stream build logs (SSE) -// (GET /builds/{id}/logs) -func (_ Unimplemented) GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) { +// Stream build events (SSE) +// (GET /builds/{id}/events) +func (_ Unimplemented) GetBuildEvents(w http.ResponseWriter, r *http.Request, id string, params GetBuildEventsParams) { w.WriteHeader(http.StatusNotImplemented) } @@ -6160,8 +6176,8 @@ func (siw *ServerInterfaceWrapper) GetBuild(w http.ResponseWriter, r *http.Reque handler.ServeHTTP(w, r) } -// GetBuildLogs operation middleware -func (siw *ServerInterfaceWrapper) GetBuildLogs(w http.ResponseWriter, r *http.Request) { +// GetBuildEvents operation middleware +func (siw *ServerInterfaceWrapper) GetBuildEvents(w http.ResponseWriter, r *http.Request) { var err error @@ -6181,7 +6197,7 @@ func (siw *ServerInterfaceWrapper) GetBuildLogs(w http.ResponseWriter, r *http.R r = r.WithContext(ctx) // Parameter object where we will unmarshal all parameters from the context - var params GetBuildLogsParams + var params GetBuildEventsParams // ------------- Optional query parameter "follow" ------------- @@ -6192,7 +6208,7 @@ func (siw *ServerInterfaceWrapper) GetBuildLogs(w http.ResponseWriter, r *http.R } handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - siw.Handler.GetBuildLogs(w, r, id, params) + siw.Handler.GetBuildEvents(w, r, id, params) })) for _, middleware := range siw.HandlerMiddlewares { @@ -7211,7 +7227,7 @@ func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handl r.Get(options.BaseURL+"/builds/{id}", wrapper.GetBuild) }) r.Group(func(r chi.Router) { - r.Get(options.BaseURL+"/builds/{id}/logs", wrapper.GetBuildLogs) + r.Get(options.BaseURL+"/builds/{id}/events", wrapper.GetBuildEvents) }) r.Group(func(r chi.Router) { r.Get(options.BaseURL+"/devices", wrapper.ListDevices) @@ -7466,21 +7482,21 @@ func (response GetBuild500JSONResponse) VisitGetBuildResponse(w http.ResponseWri return json.NewEncoder(w).Encode(response) } -type GetBuildLogsRequestObject struct { +type GetBuildEventsRequestObject struct { Id string `json:"id"` - Params GetBuildLogsParams + Params GetBuildEventsParams } -type GetBuildLogsResponseObject interface { - VisitGetBuildLogsResponse(w http.ResponseWriter) error +type GetBuildEventsResponseObject interface { + VisitGetBuildEventsResponse(w http.ResponseWriter) error } -type GetBuildLogs200TexteventStreamResponse struct { +type GetBuildEvents200TexteventStreamResponse struct { Body io.Reader ContentLength int64 } -func (response GetBuildLogs200TexteventStreamResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { +func (response GetBuildEvents200TexteventStreamResponse) VisitGetBuildEventsResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "text/event-stream") if response.ContentLength != 0 { w.Header().Set("Content-Length", fmt.Sprint(response.ContentLength)) @@ -7494,18 +7510,18 @@ func (response GetBuildLogs200TexteventStreamResponse) VisitGetBuildLogsResponse return err } -type GetBuildLogs404JSONResponse Error +type GetBuildEvents404JSONResponse Error -func (response GetBuildLogs404JSONResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { +func (response GetBuildEvents404JSONResponse) VisitGetBuildEventsResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(404) return json.NewEncoder(w).Encode(response) } -type GetBuildLogs500JSONResponse Error +type GetBuildEvents500JSONResponse Error -func (response GetBuildLogs500JSONResponse) VisitGetBuildLogsResponse(w http.ResponseWriter) error { +func (response GetBuildEvents500JSONResponse) VisitGetBuildEventsResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(500) @@ -8767,9 +8783,9 @@ type StrictServerInterface interface { // Get build details // (GET /builds/{id}) GetBuild(ctx context.Context, request GetBuildRequestObject) (GetBuildResponseObject, error) - // Stream build logs (SSE) - // (GET /builds/{id}/logs) - GetBuildLogs(ctx context.Context, request GetBuildLogsRequestObject) (GetBuildLogsResponseObject, error) + // Stream build events (SSE) + // (GET /builds/{id}/events) + GetBuildEvents(ctx context.Context, request GetBuildEventsRequestObject) (GetBuildEventsResponseObject, error) // List registered devices // (GET /devices) ListDevices(ctx context.Context, request ListDevicesRequestObject) (ListDevicesResponseObject, error) @@ -9001,26 +9017,26 @@ func (sh *strictHandler) GetBuild(w http.ResponseWriter, r *http.Request, id str } } -// GetBuildLogs operation middleware -func (sh *strictHandler) GetBuildLogs(w http.ResponseWriter, r *http.Request, id string, params GetBuildLogsParams) { - var request GetBuildLogsRequestObject +// GetBuildEvents operation middleware +func (sh *strictHandler) GetBuildEvents(w http.ResponseWriter, r *http.Request, id string, params GetBuildEventsParams) { + var request GetBuildEventsRequestObject request.Id = id request.Params = params handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) { - return sh.ssi.GetBuildLogs(ctx, request.(GetBuildLogsRequestObject)) + return sh.ssi.GetBuildEvents(ctx, request.(GetBuildEventsRequestObject)) } for _, middleware := range sh.middlewares { - handler = middleware(handler, "GetBuildLogs") + handler = middleware(handler, "GetBuildEvents") } response, err := handler(r.Context(), w, r, request) if err != nil { sh.options.ResponseErrorHandlerFunc(w, r, err) - } else if validResponse, ok := response.(GetBuildLogsResponseObject); ok { - if err := validResponse.VisitGetBuildLogsResponse(w); err != nil { + } else if validResponse, ok := response.(GetBuildEventsResponseObject); ok { + if err := validResponse.VisitGetBuildEventsResponse(w); err != nil { sh.options.ResponseErrorHandlerFunc(w, r, err) } } else if response != nil { @@ -9868,139 +9884,141 @@ func (sh *strictHandler) GetVolume(w http.ResponseWriter, r *http.Request, id st // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+x96XIbudXoq6D6JhUy4SZK9shMpW7Jku1hrmWrJFtzk5E/GuwGSYy6gTaApsRx6W8e", - "II+YJ/kKW69osiVLtBU7laqh1djOwcHZcfDZ82kUU4KI4N7os8f9BYqg+nkgBPQX5zRMInS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wSlVFZRqCZmmkMqOvJEjVxsMpCt+KLl4N16", - "Tab5TO+1qfSFtPBUoNx+2pxb9jYdyzmoijzMGgwGsrE7hR1yba42musuOEW2EGApRRXrgk7mzg/INQYt", - "FMViZZOkrE3fvp0Rf5AO6KSNew5rDZ7dR2LN+7WZNP8lV+byfhM7yUaPSWVPa8PXbu3xqByT0GaSuTJQ", - "9KGXEqG5WFO3bF21Sl02UtlAJnVknpRzXW9RobLO6s1Oji1IZktUbjLmagLF+j5NDrLcSur3RjvNvrCc", - "J+a2jucdUWYsks25GNpFI23CbvlOidLCrhhWJo5BkEasREFqtVZN4/W+/GN4nc6gDEjIQenqs4YjVxbk", - "1XN1xf3U3i3AMzuEWkb5EvvzL6tzaqmquhnrCp9at6zz4Bn+s4aj1Z2tEnFmc3TW11aVrAv5CcNidSYF", - "gok4IsgQO0g0GSpJoYBQf84mV/lINzfKapw5lMdXupIdODgZKyqJIIFzuWXnxyDEM+Sv/BCZdJKKa1OV", - "p3h7OO7qPDgbcVXxPywUQux904OTsbrqZiqJeYPesKdKo9AYERhjb+Tt9nbUZT6JBgViX6UZq5/GNyPP", - "oZJk48BI3Oe6iUQtjynhGjnDwUDfJSHCsFaYXSfq/8a100GL18Y6mq7wWXWiV7IkrCZgln/T8fYGO7da", - "z8YbQK5p3xOYiAVl+Heklvnklki406Rjoo1cW+gFmYYZzXqjX4vU+uuHmw8djydRBKWKqNGV4SqmvE6F", - "QRxAQNCVyT//jU574EybCOo6UFY7WVvwKJAsCQIBWW/+O4DMX+AluiCGE+vbXJCpZLsISA6sU52KZKan", - "1ruvjzDi4jkNViXspsP15XBKGyki+NblEtPSBHFN3UQXd9Q3ILlPnVc/EYFEZBfq9NXHS7QCMUMzfO1M", - "V1JJG24H8FH6DRhMFHm7VHcx8cMkyARgsbBhz53bfLcinT3whoKQkjli6fU15wR1Nu5ZYW2gpSmnbXPr", - "JRfMHSJNdZAEgJqdClcAggwped15iglkq7o6jjQRE1spt+bqgWmW5cU+HQzam32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6bI4yEfMyiRQW72XDbajitjZQRvHY5w6w0QdJQ19DHHc0NSBEy79VPRCGV+YU6c/XxCi1A", + "wtEE33jTlXTSht8BfJx9cwUwy7xdqbuYhiSNcgFYLjzovQbTZIKel7qCjtnYLZf6rphUgcYNUUAaAWYR", + "SRYAghzmomo7xhTyRVMZRJbKkavD23AzwDbL01YfDgZbq12ldqketl9qqITjpxqX2721A26ZW/2AF0oe", + "q+NE7bWPyLC1DXCYxzBy2Yg/WOkKVmp1wAKT1P2tIN3+iKNPhnwJMmHaCqfTlTEdp0sghzGSiAs9r48s", + "To6VCav+7QIb2mYzFlGZeLsF9FQVo7c1wt5vLDmaFe/UtLC/AfrT8+Z3OvW8jzY1LySmokhWBv1ekaPe", + "LEeIXb8W9wzJb4HiBptipe7q+Vek3/tCP8+QVQxzpFW42TaaO2+cP3wrOYKxsKOYxkonPNcw9c4RlUAX", + "uxZ9+1+nrugklXeETd8NgUEhsaW+hbF7cl+aEooWl7qTuSSX9bN3R8MZpFNlgBv5+efvf7hyxX/+/oct", + "V/zn73/o475ti+/r4bJC2++G4N8IJT1I8By5xQi1BDRHfAH2BrZknP7kuYkqLuklfYVkyqnIUhnUujRO", + "zID6igDV68E0RUKZ8AjGutTQxMbYjanuUZXdWTao3OiJ7tYsBruCwgKUVHQ0oAM2mGKJIQEslaYqgIZD", + "Z6zlgJg1B8XJq16Hmh9qNX+R6EYa6u0ZANdkMKZQvefcmdrtZkzQOT9/stUHT2A4Mys3eRT/On/5AuTD", + "AKNz9n/wpNU8yXCUMkPRWDa8qVDBt9FncWzbbMJpYWu1reG14LooF1IGnFvMD7W7hQfDjzfnzfC5FI5d", + "7aVmn8Lnr9dXx76VTXl7++xor45zW1gsR9nXsCZBx9aEye7slaqXfS2i3wgDLhS9y7gwYOam4MYsnCNG", + "JwSHEvQcLLa2eWb1lAnkvrCDVxZqAN26JvqqZ16YvSgqtkt5NI1Co/I83GakR/VNujXESLaqQpm5H5Jk", + "FekcYxEy1bdALb0QJoV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F+hkN1fe0VJq+MIDrmXDUS1iiWUdkruxYHBv1qF70vqGufaYf3V16bFV16K5bKr8AS3k/", + "ymsEWR16Wwte4dbiyw3RquK7792PrBZ/mArJYjfuyTHowFSy3hRRhdy87H3C2RxH1VfQvpEnj07hDY7T", + "OHv389lj/YoiN6ke+j1dnWjkaArdhAhFQmd+bK35PFL9ZSS7F59X8/72mJjjpo065VfMmc6LE6otVjqm", + "I3LJGCCQT9HWd3Mz0Z61/GLiyXHlWuI9zPaeO+rL9YyW+d3tTNqWluZd5HZn7o7NZnZffDtWWKF+2z28", + "XjjP1MymlPJviwQHmxMJm04lv7jHXjtlbc0raDMDqBF9BPOchZCACM0RYYl+ANC0DbpByol9zmy4va3M", + "NKIMueHB4GAQfHr76f8FAAD//5h+GpZkxAAA", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/openapi.yaml b/openapi.yaml index db30348d..eebb68f6 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -723,9 +723,6 @@ components: additionalProperties: type: string description: Map of lockfile names to SHA256 hashes - toolchain_version: - type: string - description: Runtime version (e.g., "node v20.10.0") buildkit_version: type: string description: BuildKit version used @@ -734,29 +731,24 @@ components: format: date-time description: Build completion timestamp - CreateBuildRequest: + BuildEvent: type: object + required: [type, timestamp] properties: - runtime: - type: string - description: "(Deprecated) Build runtime hint. No longer required - the generic builder accepts any Dockerfile." - example: generic - dockerfile: + type: type: string - description: "Dockerfile content. Required if not included in the source tarball. The Dockerfile specifies the runtime (e.g., FROM node:20-alpine)." - base_image_digest: + enum: [log, status, heartbeat] + description: Event type + timestamp: type: string - description: Optional pinned base image digest for reproducibility - cache_scope: + format: date-time + description: Event timestamp + content: type: string - description: Tenant-specific cache key prefix for isolation - build_args: - type: object - additionalProperties: - type: string - description: Build arguments to pass to Dockerfile - build_policy: - $ref: "#/components/schemas/BuildPolicy" + description: Log line content (only for type=log) + status: + $ref: "#/components/schemas/BuildStatus" + description: New build status (only for type=status) Build: type: object @@ -768,10 +760,6 @@ components: example: "build-abc123" status: $ref: "#/components/schemas/BuildStatus" - runtime: - type: string - description: "(Deprecated) Build runtime hint" - example: generic queue_position: type: integer description: Position in build queue (only when status is queued) @@ -2114,9 +2102,6 @@ paths: dockerfile: type: string description: Dockerfile content. Required if not included in the source tarball. - runtime: - type: string - description: "(Deprecated) Build runtime hint. No longer required." base_image_digest: type: string description: Optional pinned base image digest @@ -2218,13 +2203,17 @@ paths: schema: $ref: "#/components/schemas/Error" - /builds/{id}/logs: + /builds/{id}/events: get: - summary: Stream build logs (SSE) + summary: Stream build events (SSE) description: | - Streams build logs as Server-Sent Events. - Returns existing logs, then continues streaming new lines if follow=true. - operationId: getBuildLogs + Streams build events as Server-Sent Events. Events include: + - `log`: Build log lines with timestamp and content + - `status`: Build status changes (queued→building→pushing→ready/failed) + - `heartbeat`: Keep-alive events sent every 30s to prevent connection timeouts + + Returns existing logs as events, then continues streaming if follow=true. + operationId: getBuildEvents security: - bearerAuth: [] parameters: @@ -2240,14 +2229,14 @@ paths: schema: type: boolean default: false - description: Continue streaming new lines after initial output + description: Continue streaming new events after initial output responses: 200: - description: Log stream (SSE) + description: Event stream (SSE). Each event is a JSON BuildEvent object. content: text/event-stream: schema: - type: string + $ref: "#/components/schemas/BuildEvent" 404: description: Build not found content: diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index 143d000c..4c6e5db5 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -1,12 +1,15 @@ #!/bin/bash # E2E Build System Test -# Usage: ./scripts/e2e-build-test.sh +# Usage: ./scripts/e2e-build-test.sh [--skip-run] # # Prerequisites: # - API server running (make dev) # - Generic builder image imported into Hypeman registry # - .env file configured # +# Options: +# --skip-run Skip running the built image (only test build) +# # Environment variables: # API_URL - API endpoint (default: http://localhost:8083) # BUILDER_IMAGE - Builder image to check (default: hypeman/builder:latest) @@ -17,6 +20,21 @@ set -e API_URL="${API_URL:-http://localhost:8083}" TIMEOUT_POLLS=60 POLL_INTERVAL=5 +SKIP_RUN=false + +# Parse arguments +while [[ $# -gt 0 ]]; do + case $1 in + --skip-run) + SKIP_RUN=true + shift + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done # Colors RED='\033[0;31m' @@ -149,57 +167,128 @@ submit_build() { echo "$BUILD_ID" } -# Poll for build completion -wait_for_build() { +# Get build logs +get_logs() { local token="$1" local build_id="$2" - log "Waiting for build to complete..." + log "Fetching build logs..." + curl -s "$API_URL/builds/$build_id/logs" \ + -H "Authorization: Bearer $token" +} + +# Create and run an instance from the built image +run_built_image() { + local token="$1" + local image_ref="$2" - for i in $(seq 1 $TIMEOUT_POLLS); do - RESPONSE=$(curl -s "$API_URL/builds/$build_id" \ + log "Creating instance from built image..." + log " Image: $image_ref" + + # Create instance + RESPONSE=$(curl -s -X POST "$API_URL/instances" \ + -H "Authorization: Bearer $token" \ + -H "Content-Type: application/json" \ + -d "{ + \"image\": \"$image_ref\", + \"name\": \"e2e-test-instance\", + \"vcpus\": 1, + \"memory\": \"256M\" + }") + + INSTANCE_ID=$(echo "$RESPONSE" | jq -r '.id // empty') + + if [ -z "$INSTANCE_ID" ]; then + error "Failed to create instance" + echo "$RESPONSE" | jq . + return 1 + fi + + log "Instance created: $INSTANCE_ID" + + # Wait for instance to be running + log "Waiting for instance to start..." + for i in $(seq 1 30); do + RESPONSE=$(curl -s "$API_URL/instances/$INSTANCE_ID" \ -H "Authorization: Bearer $token") - STATUS=$(echo "$RESPONSE" | jq -r '.status') + STATE=$(echo "$RESPONSE" | jq -r '.state') - case "$STATUS" in - "ready") - log "✅ Build succeeded!" - echo "$RESPONSE" | jq . - return 0 + case "$STATE" in + "running") + log "✓ Instance is running" + break ;; - "failed") - error "❌ Build failed!" + "stopped"|"shutdown"|"failed") + error "Instance failed to start (state: $STATE)" echo "$RESPONSE" | jq . + cleanup_instance "$token" "$INSTANCE_ID" return 1 ;; - "cancelled") - warn "Build was cancelled" - return 1 - ;; - "queued"|"building"|"pushing") - echo -ne "\r Status: $STATUS (poll $i/$TIMEOUT_POLLS)..." - ;; *) - warn "Unknown status: $STATUS" + echo -ne "\r State: $STATE (poll $i/30)..." ;; esac - sleep $POLL_INTERVAL + sleep 2 done + echo "" - error "Build timed out after $((TIMEOUT_POLLS * POLL_INTERVAL)) seconds" - return 1 + if [ "$STATE" != "running" ]; then + error "Instance did not start in time" + cleanup_instance "$token" "$INSTANCE_ID" + return 1 + fi + + # Give the container a moment to run + sleep 2 + + # Try to exec into the instance and run a simple command + log "Executing test command in instance..." + EXEC_RESPONSE=$(curl -s -X POST "$API_URL/instances/$INSTANCE_ID/exec" \ + -H "Authorization: Bearer $token" \ + -H "Content-Type: application/json" \ + -d '{ + "command": ["node", "-e", "console.log(\"E2E VM test passed!\")"], + "timeout_seconds": 30 + }') + + EXEC_EXIT_CODE=$(echo "$EXEC_RESPONSE" | jq -r '.exit_code // -1') + EXEC_STDOUT=$(echo "$EXEC_RESPONSE" | jq -r '.stdout // ""') + + if [ "$EXEC_EXIT_CODE" = "0" ]; then + log "✅ Instance exec succeeded!" + log " Output: $EXEC_STDOUT" + else + warn "Instance exec returned exit code: $EXEC_EXIT_CODE" + echo "$EXEC_RESPONSE" | jq . + fi + + # Cleanup + cleanup_instance "$token" "$INSTANCE_ID" + + return 0 } -# Get build logs -get_logs() { +# Cleanup instance +cleanup_instance() { local token="$1" - local build_id="$2" + local instance_id="$2" - log "Fetching build logs..." - curl -s "$API_URL/builds/$build_id/logs" \ - -H "Authorization: Bearer $token" + log "Cleaning up instance: $instance_id" + + # Stop the instance + curl -s -X POST "$API_URL/instances/$instance_id/stop" \ + -H "Authorization: Bearer $token" > /dev/null 2>&1 || true + + # Wait a bit for it to stop + sleep 2 + + # Delete the instance + curl -s -X DELETE "$API_URL/instances/$instance_id" \ + -H "Authorization: Bearer $token" > /dev/null 2>&1 || true + + log "✓ Instance cleaned up" } # Main @@ -232,29 +321,89 @@ main() { BUILD_ID=$(submit_build "$TOKEN" "$SOURCE") echo "" - # Wait for completion - if wait_for_build "$TOKEN" "$BUILD_ID"; then - echo "" - log "=== Build Logs ===" - get_logs "$TOKEN" "$BUILD_ID" - echo "" - log "=== E2E Test PASSED ===" + # Wait for completion and capture the response + BUILD_RESPONSE="" + log "Waiting for build to complete..." + + for i in $(seq 1 $TIMEOUT_POLLS); do + BUILD_RESPONSE=$(curl -s "$API_URL/builds/$BUILD_ID" \ + -H "Authorization: Bearer $TOKEN") - # Cleanup - rm -f "$SOURCE" - exit 0 - else - echo "" - log "=== Build Logs ===" - get_logs "$TOKEN" "$BUILD_ID" - echo "" - error "=== E2E Test FAILED ===" + STATUS=$(echo "$BUILD_RESPONSE" | jq -r '.status') + + case "$STATUS" in + "ready") + log "✅ Build succeeded!" + echo "$BUILD_RESPONSE" | jq . + break + ;; + "failed") + error "❌ Build failed!" + echo "$BUILD_RESPONSE" | jq . + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" + error "=== E2E Test FAILED ===" + rm -f "$SOURCE" + exit 1 + ;; + "cancelled") + warn "Build was cancelled" + rm -f "$SOURCE" + exit 1 + ;; + "queued"|"building"|"pushing") + echo -ne "\r Status: $STATUS (poll $i/$TIMEOUT_POLLS)..." + ;; + *) + warn "Unknown status: $STATUS" + ;; + esac - # Cleanup + sleep $POLL_INTERVAL + done + echo "" + + if [ "$STATUS" != "ready" ]; then + error "Build timed out after $((TIMEOUT_POLLS * POLL_INTERVAL)) seconds" rm -f "$SOURCE" exit 1 fi + + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" + + # Run the built image (unless skipped) + if [ "$SKIP_RUN" = "false" ]; then + IMAGE_REF=$(echo "$BUILD_RESPONSE" | jq -r '.image_ref // empty') + + if [ -n "$IMAGE_REF" ]; then + echo "" + log "=== Running Built Image ===" + if run_built_image "$TOKEN" "$IMAGE_REF"; then + log "✅ VM run test passed!" + else + error "❌ VM run test failed!" + rm -f "$SOURCE" + exit 1 + fi + else + warn "No image_ref in build response, skipping VM test" + fi + else + log "Skipping VM run test (--skip-run)" + fi + + echo "" + log "=== E2E Test PASSED ===" + + # Cleanup + rm -f "$SOURCE" + exit 0 } -main "$@" +main From 31bdde60951cfdd7726bca032f26712c414645d7 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 13:30:07 -0500 Subject: [PATCH 17/42] Fix E2E test to use /events endpoint instead of /logs --- scripts/e2e-build-test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index 4c6e5db5..2b634e27 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -167,13 +167,13 @@ submit_build() { echo "$BUILD_ID" } -# Get build logs +# Get build events/logs get_logs() { local token="$1" local build_id="$2" - log "Fetching build logs..." - curl -s "$API_URL/builds/$build_id/logs" \ + log "Fetching build events..." + curl -s "$API_URL/builds/$build_id/events" \ -H "Authorization: Bearer $token" } From 57c444211b8ad4f1a6f3530cbd891357dc592b5d Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 14:37:31 -0500 Subject: [PATCH 18/42] Fix E2E test and add OCI media types to builder output - Added oci-mediatypes=true to BuildKit output in builder agent This ensures built images use OCI format which is required for Hypeman's image conversion (umoci expects OCI, not Docker format) - Improved E2E script image import status checking - Use exact name matching instead of build ID filtering - Better error messages when import fails with media type hint - Export imported image name for use in instance creation - The VM run test requires the updated builder image to be deployed Use --skip-run flag until the builder image is published --- lib/builds/builder_agent/main.go | 2 +- scripts/e2e-build-test.sh | 103 ++++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index e2dc3633..2659fdc0 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -377,7 +377,7 @@ func runBuild(ctx context.Context, config *BuildConfig, logWriter io.Writer) (st "--frontend", "dockerfile.v0", "--local", "context=" + config.SourcePath, "--local", "dockerfile=" + config.SourcePath, - "--output", fmt.Sprintf("type=image,name=%s,push=true,registry.insecure=true", outputRef), + "--output", fmt.Sprintf("type=image,name=%s,push=true,registry.insecure=true,oci-mediatypes=true", outputRef), "--metadata-file", "/tmp/build-metadata.json", } diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index 2b634e27..b0ee6caf 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -177,15 +177,116 @@ get_logs() { -H "Authorization: Bearer $token" } +# Import an image into Hypeman's image store +import_image() { + local token="$1" + local image_ref="$2" + + log "Importing image into Hypeman..." + log " Image: $image_ref" + + # Request image import + RESPONSE=$(curl -s -X POST "$API_URL/images" \ + -H "Authorization: Bearer $token" \ + -H "Content-Type: application/json" \ + -d "{\"name\": \"$image_ref\"}") + + IMAGE_NAME=$(echo "$RESPONSE" | jq -r '.name // empty') + IMAGE_STATUS=$(echo "$RESPONSE" | jq -r '.status // empty') + + if [ -z "$IMAGE_NAME" ]; then + error "Failed to import image" + echo "$RESPONSE" | jq . + return 1 + fi + + log "Image import started: $IMAGE_NAME (status: $IMAGE_STATUS)" + + # Extract the build ID for filtering (last part of the path before the tag) + # e.g., "10.102.0.1:8083/builds/abc123:latest" -> "abc123" + BUILD_ID=$(echo "$IMAGE_NAME" | sed -E 's|.*/([^/:]+)(:[^/]*)?$|\1|') + + # Wait for image to be ready + # Look specifically for the image with matching name (not just build ID, since there may be docker.io versions) + log "Waiting for image conversion..." + for i in $(seq 1 60); do + # Query the list endpoint and filter by exact name prefix + RESPONSE=$(curl -s "$API_URL/images" \ + -H "Authorization: Bearer $token" | \ + jq --arg name "$IMAGE_NAME" '[.[] | select(.name == $name)] | .[0] // empty') + + if [ -z "$RESPONSE" ] || [ "$RESPONSE" = "null" ]; then + echo -ne "\r Waiting for image... (poll $i/60)..." + sleep 2 + continue + fi + + STATUS=$(echo "$RESPONSE" | jq -r '.status // empty') + IMAGE_ERROR=$(echo "$RESPONSE" | jq -r '.error // empty') + FOUND_NAME=$(echo "$RESPONSE" | jq -r '.name // empty') + + case "$STATUS" in + "ready") + log "✓ Image is ready: $FOUND_NAME" + # Export the actual image name for use in instance creation + echo "$FOUND_NAME" + return 0 + ;; + "failed") + error "Image import failed: $IMAGE_ERROR" + if echo "$IMAGE_ERROR" | grep -q "mediatype"; then + error " Hint: The builder may be pushing Docker-format images instead of OCI format." + error " Ensure the builder image has been updated with oci-mediatypes=true" + fi + return 1 + ;; + "pending"|"pulling"|"converting") + echo -ne "\r Status: $STATUS (poll $i/60)..." + ;; + *) + warn "Unknown status: $STATUS" + ;; + esac + + sleep 2 + done + echo "" + + error "Image import timed out" + return 1 +} + # Create and run an instance from the built image run_built_image() { local token="$1" local image_ref="$2" - log "Creating instance from built image..." + log "Running built image as VM..." log " Image: $image_ref" + # First, import the image into Hypeman's image store + IMPORTED_NAME=$(import_image "$token" "$image_ref") + if [ $? -ne 0 ]; then + error "Failed to import image" + error "" + error " This typically happens when the builder outputs Docker-format images" + error " instead of OCI format. The builder agent needs oci-mediatypes=true" + error " in the BuildKit output configuration." + error "" + error " To fix: rebuild the builder image and deploy it:" + error " make build-builder" + error " docker push /builder:latest" + return 1 + fi + + # Use the imported image name (may differ from the original reference) + if [ -n "$IMPORTED_NAME" ]; then + log "Using imported image: $IMPORTED_NAME" + image_ref="$IMPORTED_NAME" + fi + # Create instance + log "Creating instance..." RESPONSE=$(curl -s -X POST "$API_URL/instances" \ -H "Authorization: Bearer $token" \ -H "Content-Type: application/json" \ From f03c3cac3decf10bc406fe05dd14edadd40e6437 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 15:16:59 -0500 Subject: [PATCH 19/42] Fix E2E script output handling for image import --- scripts/e2e-build-test.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index b0ee6caf..0ca8fc4e 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -216,7 +216,7 @@ import_image() { jq --arg name "$IMAGE_NAME" '[.[] | select(.name == $name)] | .[0] // empty') if [ -z "$RESPONSE" ] || [ "$RESPONSE" = "null" ]; then - echo -ne "\r Waiting for image... (poll $i/60)..." + echo -ne "\r Waiting for image... (poll $i/60)..." >&2 sleep 2 continue fi @@ -227,12 +227,14 @@ import_image() { case "$STATUS" in "ready") + echo "" >&2 # Clear the progress line log "✓ Image is ready: $FOUND_NAME" - # Export the actual image name for use in instance creation + # Export the actual image name for use in instance creation (to stdout) echo "$FOUND_NAME" return 0 ;; "failed") + echo "" >&2 # Clear the progress line error "Image import failed: $IMAGE_ERROR" if echo "$IMAGE_ERROR" | grep -q "mediatype"; then error " Hint: The builder may be pushing Docker-format images instead of OCI format." @@ -241,7 +243,7 @@ import_image() { return 1 ;; "pending"|"pulling"|"converting") - echo -ne "\r Status: $STATUS (poll $i/60)..." + echo -ne "\r Status: $STATUS (poll $i/60)..." >&2 ;; *) warn "Unknown status: $STATUS" From 5e40a645a8387103835534a2d9373ec4de009c75 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 15:35:56 -0500 Subject: [PATCH 20/42] feat(builds): implement SSE streaming for build events - Add BuildEvent type with log, status, and heartbeat event types - Add StreamBuildEvents method to Manager interface - Implement status subscription system for real-time status updates - Implement log streaming using tail -f for follow mode - Add heartbeat events every 30 seconds in follow mode - Update GetBuildEvents API handler with proper SSE response - Add unit tests for StreamBuildEvents (5 test cases) - Update TODO.md to mark SSE streaming as completed --- cmd/api/api/builds.go | 72 +++++++----- lib/builds/TODO.md | 17 +-- lib/builds/manager.go | 234 +++++++++++++++++++++++++++++++++++-- lib/builds/manager_test.go | 209 +++++++++++++++++++++++++++++++-- lib/builds/types.go | 22 ++++ 5 files changed, 503 insertions(+), 51 deletions(-) diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go index bc3d8914..81b546cd 100644 --- a/cmd/api/api/builds.go +++ b/cmd/api/api/builds.go @@ -2,8 +2,11 @@ package api import ( "context" + "encoding/json" "errors" + "fmt" "io" + "net/http" "strconv" "github.com/onkernel/hypeman/lib/builds" @@ -204,11 +207,19 @@ func (s *ApiService) CancelBuild(ctx context.Context, request oapi.CancelBuildRe return oapi.CancelBuild204Response{}, nil } -// GetBuildEvents streams build events +// GetBuildEvents streams build events via SSE +// With follow=false (default), streams existing logs then closes +// With follow=true, continues streaming until build completes func (s *ApiService) GetBuildEvents(ctx context.Context, request oapi.GetBuildEventsRequestObject) (oapi.GetBuildEventsResponseObject, error) { log := logger.FromContext(ctx) - logs, err := s.BuildManager.GetBuildLogs(ctx, request.Id) + // Parse follow parameter (default false) + follow := false + if request.Params.Follow != nil { + follow = *request.Params.Follow + } + + eventChan, err := s.BuildManager.StreamBuildEvents(ctx, request.Id, follow) if err != nil { if errors.Is(err, builds.ErrNotFound) { return oapi.GetBuildEvents404JSONResponse{ @@ -216,19 +227,42 @@ func (s *ApiService) GetBuildEvents(ctx context.Context, request oapi.GetBuildEv Message: "build not found", }, nil } - log.ErrorContext(ctx, "failed to get build events", "error", err, "id", request.Id) + log.ErrorContext(ctx, "failed to stream build events", "error", err, "id", request.Id) return oapi.GetBuildEvents500JSONResponse{ Code: "internal_error", - Message: "failed to get build events", + Message: "failed to stream build events", }, nil } - // Return logs as SSE events - // TODO: Implement proper SSE streaming with follow support and typed events - return oapi.GetBuildEvents200TexteventStreamResponse{ - Body: stringReader(string(logs)), - ContentLength: int64(len(logs)), - }, nil + return buildEventsStreamResponse{eventChan: eventChan}, nil +} + +// buildEventsStreamResponse implements oapi.GetBuildEventsResponseObject with proper SSE streaming +type buildEventsStreamResponse struct { + eventChan <-chan builds.BuildEvent +} + +func (r buildEventsStreamResponse) VisitGetBuildEventsResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") // Disable nginx buffering + w.WriteHeader(200) + + flusher, ok := w.(http.Flusher) + if !ok { + return fmt.Errorf("streaming not supported") + } + + for event := range r.eventChan { + jsonEvent, err := json.Marshal(event) + if err != nil { + continue + } + fmt.Fprintf(w, "data: %s\n\n", jsonEvent) + flusher.Flush() + } + return nil } // buildToOAPI converts a domain Build to OAPI Build @@ -261,21 +295,3 @@ func buildToOAPI(b *builds.Build) oapi.Build { return oapiBuild } -// stringReader wraps a string as an io.Reader -type stringReaderImpl struct { - s string - i int -} - -func stringReader(s string) io.Reader { - return &stringReaderImpl{s: s} -} - -func (r *stringReaderImpl) Read(p []byte) (n int, err error) { - if r.i >= len(r.s) { - return 0, io.EOF - } - n = copy(p, r.s[r.i:]) - r.i += n - return n, nil -} diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index 94df0cbf..62ef8efa 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -36,15 +36,17 @@ Outstanding issues and improvements for the build system. ## 🟡 Medium Priority - Implementation TODOs -### 4. SSE Streaming Implementation +### 4. ~~SSE Streaming Implementation~~ ✅ DONE -**File:** `cmd/api/api/builds.go` (L227) +**Files:** `cmd/api/api/builds.go`, `lib/builds/manager.go`, `lib/builds/types.go` -```go -// TODO: Implement proper SSE streaming with follow support and typed events -``` - -**Description:** The `/builds/{id}/events` endpoint should stream typed events (`LogEvent`, `BuildStatusEvent`) with proper SSE formatting, heartbeat events, and `follow` query parameter support. +**Status:** Implemented proper SSE streaming with: +- `BuildEvent` type with `log`, `status`, and `heartbeat` event types +- `StreamBuildEvents` method in Manager with real-time log tailing via `tail -f` +- Status subscription system for broadcasting status changes to SSE clients +- Heartbeat events every 30 seconds in follow mode +- `follow` query parameter support +- Unit tests for all streaming scenarios --- @@ -117,4 +119,5 @@ Outstanding issues and improvements for the build system. - [x] Verify vsock read deadline handling (already fixed with goroutine pattern) - [x] E2E test enhancement - run VM with built image - [x] Build manager unit tests with mocked dependencies +- [x] SSE streaming implementation with typed events, follow mode, and heartbeats diff --git a/lib/builds/manager.go b/lib/builds/manager.go index de3afbf7..0fa6e647 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -8,6 +8,7 @@ import ( "log/slog" "net" "os" + "os/exec" "path/filepath" "strings" "sync" @@ -42,6 +43,11 @@ type Manager interface { // GetBuildLogs returns the logs for a build GetBuildLogs(ctx context.Context, id string) ([]byte, error) + // StreamBuildEvents streams build events (logs, status changes, heartbeats) + // With follow=false, returns existing logs then closes + // With follow=true, continues streaming until build completes or context cancels + StreamBuildEvents(ctx context.Context, id string, follow bool) (<-chan BuildEvent, error) + // RecoverPendingBuilds recovers builds that were interrupted on restart RecoverPendingBuilds() } @@ -87,6 +93,10 @@ type manager struct { logger *slog.Logger metrics *Metrics createMu sync.Mutex + + // Status subscription system for SSE streaming + statusSubscribers map[string][]chan BuildEvent + subscriberMu sync.RWMutex } // NewManager creates a new build manager @@ -104,14 +114,15 @@ func NewManager( } m := &manager{ - config: config, - paths: p, - queue: NewBuildQueue(config.MaxConcurrentBuilds), - instanceManager: instanceMgr, - volumeManager: volumeMgr, - secretProvider: secretProvider, - tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), - logger: logger, + config: config, + paths: p, + queue: NewBuildQueue(config.MaxConcurrentBuilds), + instanceManager: instanceMgr, + volumeManager: volumeMgr, + secretProvider: secretProvider, + tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), + logger: logger, + statusSubscribers: make(map[string][]chan BuildEvent), } // Initialize metrics if meter is provided @@ -563,6 +574,9 @@ func (m *manager) updateStatus(id string, status string, err error) { if writeErr := writeMetadata(m.paths, meta); writeErr != nil { m.logger.Error("write metadata for status update", "id", id, "error", writeErr) } + + // Notify subscribers of status change + m.notifyStatusChange(id, status) } // updateBuildComplete updates the build with final results @@ -585,6 +599,55 @@ func (m *manager) updateBuildComplete(id string, status string, digest *string, if writeErr := writeMetadata(m.paths, meta); writeErr != nil { m.logger.Error("write metadata for completion", "id", id, "error", writeErr) } + + // Notify subscribers of status change + m.notifyStatusChange(id, status) +} + +// subscribeToStatus adds a subscriber channel for status updates on a build +func (m *manager) subscribeToStatus(buildID string, ch chan BuildEvent) { + m.subscriberMu.Lock() + defer m.subscriberMu.Unlock() + m.statusSubscribers[buildID] = append(m.statusSubscribers[buildID], ch) +} + +// unsubscribeFromStatus removes a subscriber channel +func (m *manager) unsubscribeFromStatus(buildID string, ch chan BuildEvent) { + m.subscriberMu.Lock() + defer m.subscriberMu.Unlock() + + subscribers := m.statusSubscribers[buildID] + for i, sub := range subscribers { + if sub == ch { + m.statusSubscribers[buildID] = append(subscribers[:i], subscribers[i+1:]...) + break + } + } + + // Clean up empty subscriber lists + if len(m.statusSubscribers[buildID]) == 0 { + delete(m.statusSubscribers, buildID) + } +} + +// notifyStatusChange broadcasts a status change to all subscribers +func (m *manager) notifyStatusChange(buildID string, status string) { + m.subscriberMu.RLock() + defer m.subscriberMu.RUnlock() + + event := BuildEvent{ + Type: EventTypeStatus, + Timestamp: time.Now(), + Status: status, + } + + for _, ch := range m.statusSubscribers[buildID] { + // Non-blocking send - drop if channel is full + select { + case ch <- event: + default: + } + } } // GetBuild returns a build by ID @@ -666,6 +729,161 @@ func (m *manager) GetBuildLogs(ctx context.Context, id string) ([]byte, error) { return readLog(m.paths, id) } +// StreamBuildEvents streams build events (logs, status changes, heartbeats) +func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) (<-chan BuildEvent, error) { + meta, err := readMetadata(m.paths, id) + if err != nil { + return nil, err + } + + // Create output channel + out := make(chan BuildEvent, 100) + + // Check if build is already complete + isComplete := meta.Status == StatusReady || meta.Status == StatusFailed || meta.Status == StatusCancelled + + go func() { + defer close(out) + + // Create a channel for status updates + statusChan := make(chan BuildEvent, 10) + if follow && !isComplete { + m.subscribeToStatus(id, statusChan) + defer m.unsubscribeFromStatus(id, statusChan) + } + + // Stream existing logs using tail + logPath := m.paths.BuildLog(id) + + // Check if log file exists + if _, err := os.Stat(logPath); os.IsNotExist(err) { + // No logs yet - if not following, just return + if !follow || isComplete { + return + } + // Wait for log file to appear, or for build to complete + for { + select { + case <-ctx.Done(): + return + case event := <-statusChan: + select { + case out <- event: + case <-ctx.Done(): + return + } + // Check if build completed + if event.Status == StatusReady || event.Status == StatusFailed || event.Status == StatusCancelled { + return + } + case <-time.After(500 * time.Millisecond): + if _, err := os.Stat(logPath); err == nil { + break // Log file appeared + } + continue + } + break + } + } + + // Build tail command args + args := []string{"-n", "+1"} // Start from beginning + if follow && !isComplete { + args = append(args, "-f") + } + args = append(args, logPath) + + cmd := exec.CommandContext(ctx, "tail", args...) + stdout, err := cmd.StdoutPipe() + if err != nil { + m.logger.Error("create stdout pipe for build logs", "id", id, "error", err) + return + } + + if err := cmd.Start(); err != nil { + m.logger.Error("start tail for build logs", "id", id, "error", err) + return + } + + // Goroutine to read log lines + logLines := make(chan string, 100) + go func() { + defer close(logLines) + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + select { + case logLines <- scanner.Text(): + case <-ctx.Done(): + return + } + } + }() + + // Heartbeat ticker (30 seconds) + heartbeatTicker := time.NewTicker(30 * time.Second) + defer heartbeatTicker.Stop() + + // Main event loop + for { + select { + case <-ctx.Done(): + cmd.Process.Kill() + return + + case line, ok := <-logLines: + if !ok { + // Log stream ended - wait for tail to exit + cmd.Wait() + return + } + event := BuildEvent{ + Type: EventTypeLog, + Timestamp: time.Now(), + Content: line, + } + select { + case out <- event: + case <-ctx.Done(): + cmd.Process.Kill() + return + } + + case event := <-statusChan: + select { + case out <- event: + case <-ctx.Done(): + cmd.Process.Kill() + return + } + // Check if build completed + if event.Status == StatusReady || event.Status == StatusFailed || event.Status == StatusCancelled { + // Give a moment for final logs to come through + time.Sleep(100 * time.Millisecond) + cmd.Process.Kill() + return + } + + case <-heartbeatTicker.C: + if !follow { + continue + } + event := BuildEvent{ + Type: EventTypeHeartbeat, + Timestamp: time.Now(), + } + select { + case out <- event: + case <-ctx.Done(): + cmd.Process.Kill() + return + } + } + } + }() + + return out, nil +} + // RecoverPendingBuilds recovers builds that were interrupted on restart func (m *manager) RecoverPendingBuilds() { pending, err := listPendingBuilds(m.paths) diff --git a/lib/builds/manager_test.go b/lib/builds/manager_test.go index ef5a369c..c53ac984 100644 --- a/lib/builds/manager_test.go +++ b/lib/builds/manager_test.go @@ -252,14 +252,15 @@ func setupTestManager(t *testing.T) (*manager, *mockInstanceManager, *mockVolume // Create manager (without calling NewManager to avoid RecoverPendingBuilds) mgr := &manager{ - config: config, - paths: p, - queue: NewBuildQueue(config.MaxConcurrentBuilds), - instanceManager: instanceMgr, - volumeManager: volumeMgr, - secretProvider: secretProvider, - tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), - logger: logger, + config: config, + paths: p, + queue: NewBuildQueue(config.MaxConcurrentBuilds), + instanceManager: instanceMgr, + volumeManager: volumeMgr, + secretProvider: secretProvider, + tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret), + logger: logger, + statusSubscribers: make(map[string][]chan BuildEvent), } return mgr, instanceMgr, volumeMgr, tempDir @@ -694,3 +695,195 @@ func TestCreateBuild_MultipleConcurrent(t *testing.T) { ids[b.ID] = true } } + +func TestStreamBuildEvents_NotFound(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + _, err := mgr.StreamBuildEvents(ctx, "nonexistent-id", false) + assert.Error(t, err) +} + +func TestStreamBuildEvents_ExistingLogs(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build + req := CreateBuildRequest{Dockerfile: "FROM alpine"} + sourceData := []byte("fake-tarball-data") + build, err := mgr.CreateBuild(ctx, req, sourceData) + require.NoError(t, err) + + // Write some logs directly + logDir := filepath.Join(tempDir, "builds", build.ID, "logs") + require.NoError(t, os.MkdirAll(logDir, 0755)) + logPath := filepath.Join(logDir, "build.log") + require.NoError(t, os.WriteFile(logPath, []byte("line1\nline2\nline3\n"), 0644)) + + // Stream events without follow + eventChan, err := mgr.StreamBuildEvents(ctx, build.ID, false) + require.NoError(t, err) + + // Collect events + var events []BuildEvent + for event := range eventChan { + events = append(events, event) + } + + // Should have 3 log events + assert.Len(t, events, 3) + for _, event := range events { + assert.Equal(t, EventTypeLog, event.Type) + } + assert.Equal(t, "line1", events[0].Content) + assert.Equal(t, "line2", events[1].Content) + assert.Equal(t, "line3", events[2].Content) +} + +func TestStreamBuildEvents_NoLogs(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx := context.Background() + + // Create a build + req := CreateBuildRequest{Dockerfile: "FROM alpine"} + sourceData := []byte("fake-tarball-data") + build, err := mgr.CreateBuild(ctx, req, sourceData) + require.NoError(t, err) + + // Stream events without follow (no logs exist) + eventChan, err := mgr.StreamBuildEvents(ctx, build.ID, false) + require.NoError(t, err) + + // Should close immediately with no events + var events []BuildEvent + for event := range eventChan { + events = append(events, event) + } + assert.Empty(t, events) +} + +func TestStreamBuildEvents_WithStatusUpdate(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // Create a build + req := CreateBuildRequest{Dockerfile: "FROM alpine"} + sourceData := []byte("fake-tarball-data") + build, err := mgr.CreateBuild(ctx, req, sourceData) + require.NoError(t, err) + + // Write some initial logs + logDir := filepath.Join(tempDir, "builds", build.ID, "logs") + require.NoError(t, os.MkdirAll(logDir, 0755)) + logPath := filepath.Join(logDir, "build.log") + require.NoError(t, os.WriteFile(logPath, []byte("initial log\n"), 0644)) + + // Stream events with follow + eventChan, err := mgr.StreamBuildEvents(ctx, build.ID, true) + require.NoError(t, err) + + // Read events until we see the initial log + var foundInitialLog bool + timeout := time.After(2 * time.Second) +eventLoop: + for !foundInitialLog { + select { + case event := <-eventChan: + if event.Type == EventTypeLog && event.Content == "initial log" { + foundInitialLog = true + break eventLoop + } + // Skip status events from queue (e.g. "building") + case <-timeout: + t.Fatal("timeout waiting for initial log event") + } + } + + // Trigger a status update to "ready" (should cause stream to close) + mgr.updateStatus(build.ID, StatusReady, nil) + + // Should receive "ready" status event and channel should close + var readyReceived bool + timeout = time.After(2 * time.Second) + for !readyReceived { + select { + case event, ok := <-eventChan: + if !ok { + // Channel closed, this is fine after status update + return + } + if event.Type == EventTypeStatus && event.Status == StatusReady { + readyReceived = true + } + case <-timeout: + t.Fatal("timeout waiting for ready status event") + } + } +} + +func TestStreamBuildEvents_ContextCancellation(t *testing.T) { + mgr, _, _, tempDir := setupTestManager(t) + defer os.RemoveAll(tempDir) + + ctx, cancel := context.WithCancel(context.Background()) + + // Create a build + req := CreateBuildRequest{Dockerfile: "FROM alpine"} + sourceData := []byte("fake-tarball-data") + build, err := mgr.CreateBuild(ctx, req, sourceData) + require.NoError(t, err) + + // Write some logs + logDir := filepath.Join(tempDir, "builds", build.ID, "logs") + require.NoError(t, os.MkdirAll(logDir, 0755)) + logPath := filepath.Join(logDir, "build.log") + require.NoError(t, os.WriteFile(logPath, []byte("log line\n"), 0644)) + + // Stream events with follow + eventChan, err := mgr.StreamBuildEvents(ctx, build.ID, true) + require.NoError(t, err) + + // Read events until we see the log line + var foundLogLine bool + timeout := time.After(2 * time.Second) +eventLoop: + for !foundLogLine { + select { + case event := <-eventChan: + if event.Type == EventTypeLog && event.Content == "log line" { + foundLogLine = true + break eventLoop + } + // Skip status events from queue (e.g. "building") + case <-timeout: + t.Fatal("timeout waiting for log event") + } + } + + // Cancel the context + cancel() + + // Channel should close + timeout = time.After(2 * time.Second) + for { + select { + case _, ok := <-eventChan: + if !ok { + // Channel closed as expected + return + } + // May get more events before close, drain them + case <-timeout: + t.Fatal("timeout waiting for channel to close after cancel") + } + } +} diff --git a/lib/builds/types.go b/lib/builds/types.go index 310e7124..a2832a9f 100644 --- a/lib/builds/types.go +++ b/lib/builds/types.go @@ -160,6 +160,28 @@ type BuildResult struct { DurationMS int64 `json:"duration_ms"` } +// BuildEvent represents a typed SSE event for build streaming +type BuildEvent struct { + // Type is one of "log", "status", or "heartbeat" + Type string `json:"type"` + + // Timestamp is when the event occurred + Timestamp time.Time `json:"timestamp"` + + // Content is the log line content (only for type="log") + Content string `json:"content,omitempty"` + + // Status is the new build status (only for type="status") + Status string `json:"status,omitempty"` +} + +// BuildEvent type constants +const ( + EventTypeLog = "log" + EventTypeStatus = "status" + EventTypeHeartbeat = "heartbeat" +) + // DefaultBuildPolicy returns the default build policy func DefaultBuildPolicy() BuildPolicy { return BuildPolicy{ From efbe9ddcdb8195083db81aad9d92728432e95d46 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 15:53:54 -0500 Subject: [PATCH 21/42] feat(builds): implement build secrets via vsock - Add SecretIDs field to VsockMessage for secrets request - Add SecretsVsockPort constant (5002) for future extensibility - Update waitForResult to handle get_secrets requests from agent - Implement host_ready message to trigger secrets exchange - Builder agent requests secrets on host_ready, waits for response - Write secrets to /run/secrets/{id} for BuildKit consumption - Add FileSecretProvider for reading secrets from filesystem - Path traversal protection in FileSecretProvider - Unit tests for FileSecretProvider (8 test cases) - Update TODO.md to mark build secrets as completed --- lib/builds/TODO.md | 17 ++-- lib/builds/builder_agent/main.go | 128 +++++++++++++++++++++--- lib/builds/file_secret_provider.go | 65 ++++++++++++ lib/builds/file_secret_provider_test.go | 102 +++++++++++++++++++ lib/builds/manager.go | 87 ++++++++++------ lib/builds/vsock_handler.go | 12 ++- 6 files changed, 356 insertions(+), 55 deletions(-) create mode 100644 lib/builds/file_secret_provider.go create mode 100644 lib/builds/file_secret_provider_test.go diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index 62ef8efa..be099c19 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -50,15 +50,17 @@ Outstanding issues and improvements for the build system. --- -### 5. Build Secrets +### 5. ~~Build Secrets~~ ✅ DONE -**File:** `lib/builds/builder_agent/main.go` (L239) +**Files:** `lib/builds/manager.go`, `lib/builds/builder_agent/main.go`, `lib/builds/file_secret_provider.go` -```go -// TODO: Implement bidirectional secret fetching -``` - -**Description:** Allow builds to securely fetch secrets (e.g., npm tokens, pip credentials) via the vsock channel during the build process. +**Status:** Implemented secure secret injection via vsock: +- Host sends `host_ready` message when connected to builder agent +- Agent requests secrets with `get_secrets` message containing secret IDs +- Host responds with `secrets_response` containing secret values from `SecretProvider` +- Agent writes secrets to `/run/secrets/{id}` for BuildKit consumption +- `FileSecretProvider` reads secrets from a configurable directory +- Unit tests for `FileSecretProvider` with path traversal protection --- @@ -120,4 +122,5 @@ Outstanding issues and improvements for the build system. - [x] E2E test enhancement - run VM with built image - [x] Build manager unit tests with mocked dependencies - [x] SSE streaming implementation with typed events, follow mode, and heartbeats +- [x] Build secrets via vsock with FileSecretProvider diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index 2659fdc0..01f17d2b 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -77,10 +77,11 @@ type BuildProvenance struct { // VsockMessage is the envelope for vsock communication type VsockMessage struct { - Type string `json:"type"` - Result *BuildResult `json:"result,omitempty"` - Log string `json:"log,omitempty"` - Secrets map[string]string `json:"secrets,omitempty"` // For secrets response from host + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` + SecretIDs []string `json:"secret_ids,omitempty"` // For secrets request to host + Secrets map[string]string `json:"secrets,omitempty"` // For secrets response from host } // Global state for the result to send when host connects @@ -88,6 +89,12 @@ var ( buildResult *BuildResult buildResultLock sync.Mutex buildDone = make(chan struct{}) + + // Secrets coordination + buildConfig *BuildConfig + buildConfigLock sync.Mutex + secretsReady = make(chan struct{}) + secretsOnce sync.Once ) func main() { @@ -151,6 +158,17 @@ func handleHostConnection(conn net.Conn) { } switch msg.Type { + case "host_ready": + // Host is ready to handle requests + // Request secrets if we have any configured + if err := handleSecretsRequest(encoder, decoder); err != nil { + log.Printf("Failed to fetch secrets: %v", err) + } + // Signal that secrets are ready (even if failed, build can proceed) + secretsOnce.Do(func() { + close(secretsReady) + }) + case "get_result": // Host is asking for the build result // Wait for build to complete if not done yet @@ -178,17 +196,80 @@ func handleHostConnection(conn net.Conn) { encoder.Encode(VsockMessage{Type: "status", Log: "building"}) } - case "secrets_response": - // Host is sending secrets we requested - // This is handled inline during secret fetching - log.Printf("Received secrets response") - default: log.Printf("Unknown message type: %s", msg.Type) } } } +// handleSecretsRequest requests secrets from the host and writes them to /run/secrets/ +func handleSecretsRequest(encoder *json.Encoder, decoder *json.Decoder) error { + // Wait for config to be loaded + var config *BuildConfig + for i := 0; i < 30; i++ { + buildConfigLock.Lock() + config = buildConfig + buildConfigLock.Unlock() + if config != nil { + break + } + time.Sleep(100 * time.Millisecond) + } + + if config == nil { + log.Printf("Config not loaded yet, skipping secrets") + return nil + } + + if len(config.Secrets) == 0 { + log.Printf("No secrets configured") + return nil + } + + // Extract secret IDs + secretIDs := make([]string, len(config.Secrets)) + for i, s := range config.Secrets { + secretIDs[i] = s.ID + } + + log.Printf("Requesting secrets: %v", secretIDs) + + // Send get_secrets request + req := VsockMessage{ + Type: "get_secrets", + SecretIDs: secretIDs, + } + if err := encoder.Encode(req); err != nil { + return fmt.Errorf("send get_secrets: %w", err) + } + + // Wait for secrets_response + var resp VsockMessage + if err := decoder.Decode(&resp); err != nil { + return fmt.Errorf("receive secrets_response: %w", err) + } + + if resp.Type != "secrets_response" { + return fmt.Errorf("unexpected response type: %s", resp.Type) + } + + // Write secrets to /run/secrets/ + if err := os.MkdirAll("/run/secrets", 0700); err != nil { + return fmt.Errorf("create secrets dir: %w", err) + } + + for id, value := range resp.Secrets { + secretPath := fmt.Sprintf("/run/secrets/%s", id) + if err := os.WriteFile(secretPath, []byte(value), 0600); err != nil { + return fmt.Errorf("write secret %s: %w", id, err) + } + log.Printf("Wrote secret: %s", id) + } + + log.Printf("Received %d secrets", len(resp.Secrets)) + return nil +} + // runBuildProcess runs the actual build and stores the result func runBuildProcess() { start := time.Now() @@ -214,6 +295,11 @@ func runBuildProcess() { } log.Printf("Job: %s", config.JobID) + // Store config globally so handleHostConnection can access it for secrets + buildConfigLock.Lock() + buildConfig = config + buildConfigLock.Unlock() + // Setup registry authentication before running the build if err := setupRegistryAuth(config.RegistryURL, config.RegistryToken); err != nil { setResult(BuildResult{ @@ -233,11 +319,27 @@ func runBuildProcess() { defer cancel() } - // Note: Secret fetching would need the host connection - // For now, we skip secrets if they require host communication - // TODO: Implement bidirectional secret fetching + // Wait for secrets if any are configured if len(config.Secrets) > 0 { - log.Printf("Warning: Secrets requested but vsock secret fetching not yet implemented in new model") + log.Printf("Waiting for secrets from host...") + select { + case <-secretsReady: + log.Printf("Secrets ready, proceeding with build") + case <-time.After(30 * time.Second): + log.Printf("Warning: Timeout waiting for secrets, proceeding anyway") + // Signal secrets ready to avoid blocking other goroutines + secretsOnce.Do(func() { + close(secretsReady) + }) + case <-ctx.Done(): + setResult(BuildResult{ + Success: false, + Error: "build timeout while waiting for secrets", + Logs: logs.String(), + DurationMS: time.Since(start).Milliseconds(), + }) + return + } } // Ensure Dockerfile exists (either in source or provided via config) diff --git a/lib/builds/file_secret_provider.go b/lib/builds/file_secret_provider.go new file mode 100644 index 00000000..8262e0a7 --- /dev/null +++ b/lib/builds/file_secret_provider.go @@ -0,0 +1,65 @@ +package builds + +import ( + "context" + "fmt" + "os" + "path/filepath" + "strings" +) + +// FileSecretProvider reads secrets from files in a directory. +// Each secret is stored as a file named by its ID, with the secret value as the file content. +// Example: /etc/hypeman/secrets/npm_token contains the npm token value. +type FileSecretProvider struct { + secretsDir string +} + +// NewFileSecretProvider creates a new file-based secret provider. +// secretsDir is the directory containing secret files (e.g., /etc/hypeman/secrets/). +func NewFileSecretProvider(secretsDir string) *FileSecretProvider { + return &FileSecretProvider{ + secretsDir: secretsDir, + } +} + +// GetSecrets returns the values for the given secret IDs by reading files from the secrets directory. +// Missing secrets are silently skipped (not an error). +// Returns an error only if a secret file exists but cannot be read. +func (p *FileSecretProvider) GetSecrets(ctx context.Context, secretIDs []string) (map[string]string, error) { + result := make(map[string]string) + + for _, id := range secretIDs { + // Validate secret ID to prevent path traversal + if strings.Contains(id, "/") || strings.Contains(id, "\\") || id == ".." || id == "." { + continue // Skip invalid IDs + } + + path := filepath.Join(p.secretsDir, id) + + // Check context before each file read + select { + case <-ctx.Done(): + return result, ctx.Err() + default: + } + + data, err := os.ReadFile(path) + if err != nil { + if os.IsNotExist(err) { + // Secret doesn't exist - skip it (not an error) + continue + } + return nil, fmt.Errorf("read secret %s: %w", id, err) + } + + // Trim whitespace (especially trailing newlines) + result[id] = strings.TrimSpace(string(data)) + } + + return result, nil +} + +// Ensure FileSecretProvider implements SecretProvider +var _ SecretProvider = (*FileSecretProvider)(nil) + diff --git a/lib/builds/file_secret_provider_test.go b/lib/builds/file_secret_provider_test.go new file mode 100644 index 00000000..5ad93f71 --- /dev/null +++ b/lib/builds/file_secret_provider_test.go @@ -0,0 +1,102 @@ +package builds + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFileSecretProvider_GetSecrets(t *testing.T) { + // Create temp directory with test secrets + tempDir, err := os.MkdirTemp("", "secrets-test-*") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + // Write test secrets + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "npm_token"), []byte("npm-secret-value\n"), 0600)) + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "github_token"), []byte("github-secret-value"), 0600)) + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "with_whitespace"), []byte(" trimmed \n"), 0600)) + + provider := NewFileSecretProvider(tempDir) + ctx := context.Background() + + t.Run("fetch existing secrets", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"npm_token", "github_token"}) + require.NoError(t, err) + assert.Len(t, secrets, 2) + assert.Equal(t, "npm-secret-value", secrets["npm_token"]) + assert.Equal(t, "github-secret-value", secrets["github_token"]) + }) + + t.Run("missing secrets are skipped", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"npm_token", "nonexistent"}) + require.NoError(t, err) + assert.Len(t, secrets, 1) + assert.Equal(t, "npm-secret-value", secrets["npm_token"]) + }) + + t.Run("all missing secrets returns empty map", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"missing1", "missing2"}) + require.NoError(t, err) + assert.Empty(t, secrets) + }) + + t.Run("whitespace is trimmed", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"with_whitespace"}) + require.NoError(t, err) + assert.Equal(t, "trimmed", secrets["with_whitespace"]) + }) + + t.Run("path traversal is blocked", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"../etc/passwd", "../../root/.ssh/id_rsa"}) + require.NoError(t, err) + assert.Empty(t, secrets) + }) + + t.Run("special characters in ID are blocked", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{"foo/bar", "baz\\qux", "..", "."}) + require.NoError(t, err) + assert.Empty(t, secrets) + }) + + t.Run("empty request returns empty map", func(t *testing.T) { + secrets, err := provider.GetSecrets(ctx, []string{}) + require.NoError(t, err) + assert.Empty(t, secrets) + }) +} + +func TestFileSecretProvider_ContextCancellation(t *testing.T) { + tempDir, err := os.MkdirTemp("", "secrets-test-*") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + // Write many secrets + for i := 0; i < 10; i++ { + require.NoError(t, os.WriteFile(filepath.Join(tempDir, "secret"+string(rune('A'+i))), []byte("value"), 0600)) + } + + provider := NewFileSecretProvider(tempDir) + + // Cancel context immediately + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + secrets, err := provider.GetSecrets(ctx, []string{"secretA", "secretB", "secretC"}) + // May return partial results or context error + assert.True(t, err == context.Canceled || len(secrets) <= 3) +} + +func TestNoOpSecretProvider(t *testing.T) { + provider := &NoOpSecretProvider{} + ctx := context.Background() + + secrets, err := provider.GetSecrets(ctx, []string{"any", "secret", "ids"}) + require.NoError(t, err) + assert.Empty(t, secrets) +} + diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 0fa6e647..8dfc66aa 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -454,46 +454,71 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( m.logger.Info("connected to builder agent", "instance", inst.Id) - // Send request for result encoder := json.NewEncoder(conn) decoder := json.NewDecoder(conn) - // Request the build result (this will block until build completes) - if err := encoder.Encode(VsockMessage{Type: "get_result"}); err != nil { - return nil, fmt.Errorf("send get_result request: %w", err) + // Tell the agent we're ready - it may request secrets + if err := encoder.Encode(VsockMessage{Type: "host_ready"}); err != nil { + return nil, fmt.Errorf("send host_ready: %w", err) } - // Use a goroutine for decoding so we can respect context cancellation. - // json.Decoder.Decode() doesn't respect context, so we need to close the - // connection to unblock it when the context is cancelled. - type decodeResult struct { - response VsockMessage - err error - } - resultCh := make(chan decodeResult, 1) + // Handle messages from agent until we get the build result + for { + // Use a goroutine for decoding so we can respect context cancellation. + type decodeResult struct { + response VsockMessage + err error + } + resultCh := make(chan decodeResult, 1) - go func() { - var response VsockMessage - err := decoder.Decode(&response) - resultCh <- decodeResult{response: response, err: err} - }() + go func() { + var response VsockMessage + err := decoder.Decode(&response) + resultCh <- decodeResult{response: response, err: err} + }() - // Wait for either the result or context cancellation - select { - case <-ctx.Done(): - // Close the connection to unblock the decoder goroutine - conn.Close() - // Drain the result channel to avoid goroutine leak - <-resultCh - return nil, ctx.Err() - case dr := <-resultCh: - if dr.err != nil { - return nil, fmt.Errorf("read result: %w", dr.err) + // Wait for either a message or context cancellation + var dr decodeResult + select { + case <-ctx.Done(): + conn.Close() + <-resultCh + return nil, ctx.Err() + case dr = <-resultCh: + if dr.err != nil { + return nil, fmt.Errorf("read message: %w", dr.err) + } } - if dr.response.Type != "build_result" || dr.response.Result == nil { - return nil, fmt.Errorf("unexpected response type: %s", dr.response.Type) + + // Handle message based on type + switch dr.response.Type { + case "get_secrets": + // Agent is requesting secrets + m.logger.Debug("agent requesting secrets", "instance", inst.Id, "secret_ids", dr.response.SecretIDs) + + // Fetch secrets from provider + secrets, err := m.secretProvider.GetSecrets(ctx, dr.response.SecretIDs) + if err != nil { + m.logger.Error("failed to fetch secrets", "error", err) + secrets = make(map[string]string) + } + + // Send secrets response + if err := encoder.Encode(VsockMessage{Type: "secrets_response", Secrets: secrets}); err != nil { + return nil, fmt.Errorf("send secrets response: %w", err) + } + m.logger.Debug("sent secrets to agent", "count", len(secrets)) + + case "build_result": + // Build completed + if dr.response.Result == nil { + return nil, fmt.Errorf("received build_result with nil result") + } + return dr.response.Result, nil + + default: + m.logger.Warn("unexpected message type from agent", "type", dr.response.Type) } - return dr.response.Result, nil } } diff --git a/lib/builds/vsock_handler.go b/lib/builds/vsock_handler.go index bfe25de9..f5aebfcd 100644 --- a/lib/builds/vsock_handler.go +++ b/lib/builds/vsock_handler.go @@ -7,14 +7,18 @@ import ( const ( // BuildAgentVsockPort is the port the builder agent listens on inside the guest BuildAgentVsockPort = 5001 + + // SecretsVsockPort is the port the host listens on for secret requests from builder agents + SecretsVsockPort = 5002 ) // VsockMessage is the envelope for vsock communication with builder agents type VsockMessage struct { - Type string `json:"type"` - Result *BuildResult `json:"result,omitempty"` - Log string `json:"log,omitempty"` - Secrets map[string]string `json:"secrets,omitempty"` // For secrets response + Type string `json:"type"` + Result *BuildResult `json:"result,omitempty"` + Log string `json:"log,omitempty"` + SecretIDs []string `json:"secret_ids,omitempty"` // For secrets request + Secrets map[string]string `json:"secrets,omitempty"` // For secrets response } // SecretsRequest is sent by the builder agent to fetch secrets From 21e7e948695af38508f7548df4a78602ea0ecbe7 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 16:01:18 -0500 Subject: [PATCH 22/42] feat(config): add BUILD_SECRETS_DIR configuration - Add BuildSecretsDir to Config struct - Load from BUILD_SECRETS_DIR environment variable - Update ProvideBuildManager to use FileSecretProvider when configured - Log when build secrets are enabled --- cmd/api/config/config.go | 2 ++ lib/providers/providers.go | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/api/config/config.go b/cmd/api/config/config.go index 7e54091e..d56c8024 100644 --- a/cmd/api/config/config.go +++ b/cmd/api/config/config.go @@ -108,6 +108,7 @@ type Config struct { BuilderImage string // OCI image for builder VMs RegistryURL string // URL of registry for built images BuildTimeout int // Default build timeout in seconds + BuildSecretsDir string // Directory containing build secrets (optional) // Hypervisor configuration DefaultHypervisor string // Default hypervisor type: "cloud-hypervisor" or "qemu" @@ -196,6 +197,7 @@ func Load() *Config { BuilderImage: getEnv("BUILDER_IMAGE", "hypeman/builder:latest"), RegistryURL: getEnv("REGISTRY_URL", "localhost:8080"), BuildTimeout: getEnvInt("BUILD_TIMEOUT", 600), + BuildSecretsDir: getEnv("BUILD_SECRETS_DIR", ""), // Optional: path to directory with build secrets // Hypervisor configuration DefaultHypervisor: getEnv("DEFAULT_HYPERVISOR", "cloud-hypervisor"), diff --git a/lib/providers/providers.go b/lib/providers/providers.go index 88ec6167..bede8650 100644 --- a/lib/providers/providers.go +++ b/lib/providers/providers.go @@ -238,6 +238,13 @@ func ProvideBuildManager(p *paths.Paths, cfg *config.Config, instanceManager ins buildConfig.DefaultTimeout = 600 } + // Configure secret provider + var secretProvider builds.SecretProvider + if cfg.BuildSecretsDir != "" { + secretProvider = builds.NewFileSecretProvider(cfg.BuildSecretsDir) + log.Info("build secrets enabled", "dir", cfg.BuildSecretsDir) + } + meter := otel.GetMeterProvider().Meter("hypeman") - return builds.NewManager(p, buildConfig, instanceManager, volumeManager, nil, log, meter) + return builds.NewManager(p, buildConfig, instanceManager, volumeManager, secretProvider, log, meter) } From b9c5567236e7d011e17c928c2ea0031f64c83151 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 16:43:47 -0500 Subject: [PATCH 23/42] fix(builds): fix vsock protocol deadlock and add secrets API support - Fix protocol deadlock: agent now proactively sends build_result when complete instead of waiting for host to request it (was causing builds to hang forever) - Add secrets field to /builds POST API endpoint - Add INFO logging for vsock communication debugging - Regenerate oapi.go with secrets field support The build agent would receive host_ready, handle secrets, and then loop waiting for more messages. But it never sent anything back, and the host was also waiting. Now the agent spawns a goroutine after host_ready to wait for build completion and send the result automatically. --- cmd/api/api/builds.go | 16 ++ lib/builds/builder_agent/main.go | 14 ++ lib/builds/manager.go | 7 +- lib/oapi/oapi.go | 277 ++++++++++++++++--------------- openapi.yaml | 6 + 5 files changed, 183 insertions(+), 137 deletions(-) diff --git a/cmd/api/api/builds.go b/cmd/api/api/builds.go index 81b546cd..deaf6ec1 100644 --- a/cmd/api/api/builds.go +++ b/cmd/api/api/builds.go @@ -43,6 +43,7 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe var sourceData []byte var baseImageDigest, cacheScope, dockerfile string var timeoutSeconds int + var secrets []builds.SecretRef for { part, err := request.Body.NextPart() @@ -103,6 +104,20 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe if v, err := strconv.Atoi(string(data)); err == nil { timeoutSeconds = v } + case "secrets": + data, err := io.ReadAll(part) + if err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "failed to read secrets field", + }, nil + } + if err := json.Unmarshal(data, &secrets); err != nil { + return oapi.CreateBuild400JSONResponse{ + Code: "invalid_request", + Message: "secrets must be a JSON array of {\"id\": \"...\", \"env_var\": \"...\"} objects", + }, nil + } } part.Close() } @@ -122,6 +137,7 @@ func (s *ApiService) CreateBuild(ctx context.Context, request oapi.CreateBuildRe BaseImageDigest: baseImageDigest, CacheScope: cacheScope, Dockerfile: dockerfile, + Secrets: secrets, } // Apply timeout if provided diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index 01f17d2b..b589ba05 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -169,6 +169,20 @@ func handleHostConnection(conn net.Conn) { close(secretsReady) }) + // Wait for build to complete and send result to host + go func() { + <-buildDone + + buildResultLock.Lock() + result := buildResult + buildResultLock.Unlock() + + log.Printf("Build completed, sending result to host") + if err := encoder.Encode(VsockMessage{Type: "build_result", Result: result}); err != nil { + log.Printf("Failed to send build result: %v", err) + } + }() + case "get_result": // Host is asking for the build result // Wait for build to complete if not done yet diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 8dfc66aa..5f949807 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -458,9 +458,11 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( decoder := json.NewDecoder(conn) // Tell the agent we're ready - it may request secrets + m.logger.Info("sending host_ready to agent", "instance", inst.Id) if err := encoder.Encode(VsockMessage{Type: "host_ready"}); err != nil { return nil, fmt.Errorf("send host_ready: %w", err) } + m.logger.Info("host_ready sent, waiting for agent messages", "instance", inst.Id) // Handle messages from agent until we get the build result for { @@ -491,10 +493,11 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( } // Handle message based on type + m.logger.Info("received message from agent", "type", dr.response.Type, "instance", inst.Id) switch dr.response.Type { case "get_secrets": // Agent is requesting secrets - m.logger.Debug("agent requesting secrets", "instance", inst.Id, "secret_ids", dr.response.SecretIDs) + m.logger.Info("agent requesting secrets", "instance", inst.Id, "secret_ids", dr.response.SecretIDs) // Fetch secrets from provider secrets, err := m.secretProvider.GetSecrets(ctx, dr.response.SecretIDs) @@ -507,7 +510,7 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( if err := encoder.Encode(VsockMessage{Type: "secrets_response", Secrets: secrets}); err != nil { return nil, fmt.Errorf("send secrets response: %w", err) } - m.logger.Debug("sent secrets to agent", "count", len(secrets)) + m.logger.Info("sent secrets to agent", "count", len(secrets), "instance", inst.Id) case "build_result": // Build completed diff --git a/lib/oapi/oapi.go b/lib/oapi/oapi.go index 5154aa97..4171b96e 100644 --- a/lib/oapi/oapi.go +++ b/lib/oapi/oapi.go @@ -705,6 +705,11 @@ type CreateBuildMultipartBody struct { // Dockerfile Dockerfile content. Required if not included in the source tarball. Dockerfile *string `json:"dockerfile,omitempty"` + // Secrets JSON array of secret references to inject during build. + // Each object has "id" (required) for use with --mount=type=secret,id=... + // Example: [{"id": "npm_token"}, {"id": "github_token"}] + Secrets *string `json:"secrets,omitempty"` + // Source Source tarball (tar.gz) containing application code and optionally a Dockerfile Source openapi_types.File `json:"source"` @@ -9884,141 +9889,143 @@ func (sh *strictHandler) GetVolume(w http.ResponseWriter, r *http.Request, id st // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/+x9e3MTOfboV1H1/W2ts2s7zgMmeGvrVkiAyS6BFIHM3Z1wjdwt25qopUZSOzEU/84H", - "mI84n+SWXv1U221IDLmwtVVj0nocHR2dt44+BiGLE0YRlSIYfgxEOEMx1D8PpYTh7IKRNEav0PsUCan+", - "nHCWIC4x0o1illI5SqCcqX9FSIQcJxIzGgyDMyhn4HqGOAJzPQoQM5aSCIwR0P1QFHQDdAPjhKBgGGzH", - "VG5HUMKgG8hFov4kJMd0GnzqBhzBiFGyMNNMYEpkMJxAIlC3Mu2pGhpAAVSXnu6TjTdmjCBIg096xPcp", - "5igKhr8Wl/E2a8zGv6FQqskP5xATOCboGM1xiOpoCFPOEZWjiOM54nVUHJnvZAHGLKURMO1Ah6aEADwB", - "lFG0VUIGneMIK0yoJmrqYCh5ijyYiTRMIxx5duDoBJjP4OQYdGbopjzJ7k/jg6B5SApjVB/05zSGtKeQ", - "q8By4+u2xbGf7/tGxiyO09GUszSpj3zy8vT0DdAfAU3jMeLFEQ92s/EwlWiKuBowCfEIRhFHQvjX7z4W", - "YRsMBoMh3B0OBv2BD8o5ohHjjSg1n/0o3RlEaMmQrVBqx6+h9MXFyfHJIThiPGEc6r61mSqEXURPcV1F", - "sinvio/+H6eYRB6qZwowiaIRlPVF6U7AtsGMAoljJCSMk6AbTBiPVacgghL11Jc2pB5yBFdMp1q0mqxO", - "9KnB6SgWTaO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BaOe2nVXpRRZJYkvVJilfIXDw+zC4u", - "eFw0SVqHc64EqrvvUI7A7vtWq70sy0KK2VCFuKLTSl3uVRmvhRyoVilXTlh7swozmdgQZlpSGcMN61f0", - "T4quyKpVPI/9uS9KVW7C1qlRpOv4KnnuHhw8erS3/+DRbivUWGsnM5cbnGFNJrODYFugsHJHqLxjuw8G", - "+n9rAWUMZj9IDUZzGaDSfZ/PBujTkuOTX5Ov5OBn52NJfah8J7kdrrSV+wetsAVdwSqPses+aY2icI2z", - "gyYTpBW1kcFbLwemEuRpBUMIExhiufDEQeG19nuDrElh9IftUiYrwHpQascGcCKV9j9HXKTjPMmu4yYH", - "fwPak1ShhYPW+aQiHY/0CB6nW3VW3c4GiqKKCZIbPSwdk4J72maKZ0UkfH7U6wyZ4BqKkm2ofocSRd3C", - "Nd2qE8G0aF+FxNF6Vogkd4/6Usn9RUeK21/Zzm5QlCY5OVcxvkyMNR9BJZV1FKqNmeaRip48USsX2wyU", - "F41RcvDzeo3GxUzvpan0pbTwTKCsP23BLbtOx2oOqiYPC4PFQD52t7RDvs01RnPTBafY1TGspKhiU4/K", - "3vkBhcagg+JELlySlLPpt9Yz4g+zAb20ccthrcGj20isebM0k+b/kytzRb+Jm2Slx6S2p43ha7/2eFyN", - "SRgzyV4ZKPvQK4nQQi4pu7as2KapeqltIJs6Mk2rua5rFNhssnrzk+PqqbkKm6uMuYZAsblPU1hZAZLm", - "vTFOsy+sRoqFK0P6mSizFsnqXAzjolE2Ya96p0RrYdccaxPHIsggVqEgs1rrpvFyX/4pvMlm0AYkFKBy", - "9dmso1AW5NljfcX9lbtbgCduCA1G9RL74y8r0+qoqr4Zy+q2Ores9+BZ/rOEozWdrQpx5nN0l5eGVawL", - "hSnHcnGuBIKNOCLIET9MDRlqSaEXof+cT67zkT590lbjxKM8PkMUcRyCw7MTTSUxpHCqtuziFBA8QeEi", - "JMimk9Rcm7o8xcujk57Jg3MRVx3/w1IjxN03PTw70VfdbJW1YNDf7evSKCxBFC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F/FxX465oTS3Eskqpi0v", - "vrmy1KB1b3DoB4Bct+zxmm7xRSJTnS8zUPPa+dnjJ/1Lmr324qoDgqOzN137wmxXv2FrRrBvjPSB/1Ee", - "ASBH7mWeSyoZCCEJUwIlAtnrNOZFKdEQ1n1VeLHpzs5bPolno7NneUT2bMt9sjH8NKF3r/gujKa4wvOs", - "jbml9qXWjWSWWmG2Rl6pW8GPFLwWWaUFZLWpQm+a98F5miSMSwHkNdPPMwody9dFFscsWgxB1o8C8xKQ", - "ZXH2CRdbjh1F+hkN1fe0VJq+MIDrmXDUS1iiWUdkruxYHBv1qF70vqGufaYf3V16bFV16K5bKr8AS3k/", - "ymsEWR16Wwte4dbiyw3RquK7792PrBZ/mArJYjfuyTHowFSy3hRRhdy87H3C2RxH1VfQvpEnj07hDY7T", - "OHv389lj/YoiN6ke+j1dnWjkaArdhAhFQmd+bK35PFL9ZSS7F59X8/72mJjjpo065VfMmc6LE6otVjqm", - "I3LJGCCQT9HWd3Mz0Z61/GLiyXHlWuI9zPaeO+rL9YyW+d3tTNqWluZd5HZn7o7NZnZffDtWWKF+2z28", - "XjjP1MymlPJviwQHmxMJm04lv7jHXjtlbc0raDMDqBF9BPOchZCACM0RYYl+ANC0DbpByol9zmy4va3M", - "NKIMueHB4GAQfHr76f8FAAD//5h+GpZkxAAA", + "H4sIAAAAAAAC/+x97XITubboq6j6nl3HOdt2nA+Y4FNTt0ICTPYhkCKQffeecI3cLduadEs9ktqJofg7", + "DzCPOE9yS0tSf1ltd4AYcmHXrhqT1ufS0vpeSx+CkCcpZ4QpGQw/BDKckQTDz0OlcDi74HGWkFfk94xI", + "pf+cCp4SoSiBRgnPmBqlWM30vyIiQ0FTRTkLhsEZVjN0PSOCoDmMguSMZ3GExgRBPxIF3YDc4CSNSTAM", + "thOmtiOscNAN1CLVf5JKUDYNPnYDQXDEWbww00xwFqtgOMGxJN3atKd6aIQl0l160Ccfb8x5TDALPsKI", + "v2dUkCgY/lrextu8MR//RkKlJz+cYx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xFrGdEiuOEcxFlOy", + "9d1kJtq7ViQmnhzX0hLvYbT33GFfIWe0jO9up9K21DTvIrY7N3dsNrL74tvRwkr12+5heuE8FzObQsq/", + "LRQcbI4lbDqU/OIeW+20tjWvgc0MoEf0IcxzHuIYRWROYp7Ci4embdANMhHb99uG29taTYu1Ijc8GBwM", + "go9vP/6/AAAA///Bm92RVcUAAA==", } // GetSwagger returns the content of the embedded swagger specification file diff --git a/openapi.yaml b/openapi.yaml index eebb68f6..bc20590f 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -2111,6 +2111,12 @@ paths: timeout_seconds: type: integer description: Build timeout (default 600) + secrets: + type: string + description: | + JSON array of secret references to inject during build. + Each object has "id" (required) for use with --mount=type=secret,id=... + Example: [{"id": "npm_token"}, {"id": "github_token"}] responses: 202: description: Build created and queued From 1789dcbf4678a2a0ef5f1f7f915566fea6ff151f Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 16:44:07 -0500 Subject: [PATCH 24/42] docs: update TODO with vsock protocol fix details --- lib/builds/TODO.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index be099c19..2c06d0d9 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -52,7 +52,7 @@ Outstanding issues and improvements for the build system. ### 5. ~~Build Secrets~~ ✅ DONE -**Files:** `lib/builds/manager.go`, `lib/builds/builder_agent/main.go`, `lib/builds/file_secret_provider.go` +**Files:** `lib/builds/manager.go`, `lib/builds/builder_agent/main.go`, `lib/builds/file_secret_provider.go`, `cmd/api/api/builds.go` **Status:** Implemented secure secret injection via vsock: - Host sends `host_ready` message when connected to builder agent @@ -61,6 +61,9 @@ Outstanding issues and improvements for the build system. - Agent writes secrets to `/run/secrets/{id}` for BuildKit consumption - `FileSecretProvider` reads secrets from a configurable directory - Unit tests for `FileSecretProvider` with path traversal protection +- **Fixed vsock protocol deadlock** - agent now proactively sends `build_result` when complete +- Added `secrets` field to POST `/builds` API endpoint (JSON array of `{"id": "..."}` objects) +- E2E tested: builds complete successfully and logs stream via SSE --- From f95b1ebe6ebfbff3478fc7f43f60758828adc5db Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 16:50:23 -0500 Subject: [PATCH 25/42] docs: document cgroup requirement for BuildKit secrets The secrets API flow is fully implemented and working: - Host receives secrets from API - Host sends secrets to builder agent via vsock - Agent writes secrets to /run/secrets/ - BuildKit receives --secret flags However, BuildKit's runc requires cgroup mounts when --secret flags are present, which the current microVM doesn't have. This is an infrastructure issue to be fixed separately. --- lib/builds/TODO.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index 2c06d0d9..a76a1a2d 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -96,11 +96,26 @@ Outstanding issues and improvements for the build system. - `TestRegistryTokenGeneration` - Token generation verification - `TestCreateBuild_MultipleConcurrent` - Concurrent build creation -### 8. Guest Agent on Builder VMs +### 8. Enable cgroups for BuildKit Secrets + +**Issue:** When `--secret` flags are passed to BuildKit, runc requires cgroup mounts that aren't present in the microVM. + +**Error:** `runc run failed: no cgroup mount found in mountinfo` + +**Status:** The secrets API flow works correctly (host → vsock → agent → BuildKit flags), but BuildKit execution fails due to missing cgroups. + +**Potential Fixes:** +- Enable cgroup support in the microVM kernel/rootfs +- Use BuildKit in rootless mode without cgroup dependency +- Configure BuildKit to use a different runc execution mode + +**Workaround:** Builds without secrets work fine. The secrets code is ready once cgroups are enabled. + +### 9. Guest Agent on Builder VMs **Suggestion:** Run the guest-agent on builder VMs to enable `exec` into failed builds for debugging. -### 9. Builder Image Tooling +### 10. Builder Image Tooling **File:** `lib/builds/images/README.md` From cf94ff0fd32ae2cb1c38086819a6d1f0b76a5ff2 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 16:56:09 -0500 Subject: [PATCH 26/42] docs: add detailed cgroup analysis for BuildKit secrets Documents the root cause (missing /sys/fs/cgroup mount in VM init), two proposed solutions (Option A: all VMs, Option B: builder-only), and security analysis for team discussion. --- lib/builds/TODO.md | 77 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index a76a1a2d..d4fac17f 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -104,13 +104,80 @@ Outstanding issues and improvements for the build system. **Status:** The secrets API flow works correctly (host → vsock → agent → BuildKit flags), but BuildKit execution fails due to missing cgroups. -**Potential Fixes:** -- Enable cgroup support in the microVM kernel/rootfs -- Use BuildKit in rootless mode without cgroup dependency -- Configure BuildKit to use a different runc execution mode - **Workaround:** Builds without secrets work fine. The secrets code is ready once cgroups are enabled. +#### Root Cause + +The VM init (`lib/system/init/mount.go`) mounts `/proc`, `/sys`, `/dev`, `/dev/pts`, `/dev/shm` but does NOT mount `/sys/fs/cgroup`. When BuildKit receives `--secret` flags, it uses runc which requires cgroups even for rootless execution. + +#### Proposed Solutions + +**Option A: Add cgroup mount to VM init (all VMs)** + +File: `lib/system/init/mount.go` + +```go +// In mountEssentials(), add: +if err := os.MkdirAll("/sys/fs/cgroup", 0755); err != nil { + return fmt.Errorf("mkdir /sys/fs/cgroup: %w", err) +} +if err := syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, ""); err != nil { + log.Info("mount", "cgroup2 failed (non-fatal)") +} + +// In bindMountsToNewRoot(), add to mounts slice: +{"/sys/fs/cgroup", newroot + "/sys/fs/cgroup"}, +``` + +Pros: +- Enables cgroups for all VM workloads +- Happens early in boot before user processes +- Properly bind-mounts to new root + +Cons: +- All VMs get cgroup access (larger attack surface, though mitigated by VM isolation) + +**Option B: Add cgroup mount in builder-agent only** + +File: `lib/builds/builder_agent/main.go` + +```go +func mountCgroups() error { + if err := os.MkdirAll("/sys/fs/cgroup", 0755); err != nil { + return err + } + return syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, "") +} +``` + +Pros: +- Only affects builder VMs +- Minimal scope + +Cons: +- Late in boot (after chroot) +- May not work if /sys is read-only in newroot + +#### Security Analysis + +| Concern | Risk Level | Mitigation | +|---------|------------|------------| +| Container escape via cgroup | Very Low | VM hypervisor isolation + cgroup v2 (no release_agent) | +| Resource manipulation | Low | VM has hypervisor-level resource limits | +| Attack surface for user VMs | Medium | Consider making cgroups opt-in or read-only | + +**Recommendation:** Option A with cgroup v2 is safe because: +1. VMs are already isolated by Cloud Hypervisor (hardware boundary) +2. Builder VMs are ephemeral (destroyed after each build) +3. Builder runs as unprivileged user (uid 1000) +4. Cgroup v2 has better security than v1 (no release_agent escape vector) + +#### After Implementation + +1. Rebuild init binary: `make init` +2. Rebuild initrd: `make initrd` +3. Test builds with secrets + ### 9. Guest Agent on Builder VMs **Suggestion:** Run the guest-agent on builder VMs to enable `exec` into failed builds for debugging. From 0a7e0471c86f70b5b806eb9e8a041c9321ba5536 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:01:50 -0500 Subject: [PATCH 27/42] feat(builds): add guest-agent to builder VMs for exec debugging - Update builder Dockerfile to build and include guest-agent binary - Only copy proto files (not client.go) to avoid host-side dependencies - Start guest-agent in builder-agent main() before build starts - Guest-agent listens on vsock port 2222 for exec requests Note: Testing blocked by cgroup issue (builds fail before we can exec). Once cgroups are enabled, exec into builder VMs will work. --- lib/builds/TODO.md | 15 ++++++++++-- lib/builds/builder_agent/main.go | 35 ++++++++++++++++++++++++++++ lib/builds/images/generic/Dockerfile | 17 ++++++++++---- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index d4fac17f..c58baf17 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -178,9 +178,20 @@ Cons: 2. Rebuild initrd: `make initrd` 3. Test builds with secrets -### 9. Guest Agent on Builder VMs +### 9. ~~Guest Agent on Builder VMs~~ ✅ DONE -**Suggestion:** Run the guest-agent on builder VMs to enable `exec` into failed builds for debugging. +**Files:** `lib/builds/images/generic/Dockerfile`, `lib/builds/builder_agent/main.go` + +**Status:** Implemented guest-agent support in builder VMs: +- Builder Dockerfile now builds and includes `/usr/bin/guest-agent` +- Builder-agent starts guest-agent at boot (before build starts) +- Guest-agent listens on vsock port 2222 for exec requests + +**Limitation:** Currently can't test exec because: +1. Builder instances are deleted immediately after build completion (success or failure) +2. Builds fail due to cgroup issue (prevents long-running builds to exec into) + +**Future Enhancement:** Add `KeepFailedBuilders` option to keep failed build instances running for debugging. ### 10. Builder Image Tooling diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index b589ba05..9875f3a7 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -100,6 +100,9 @@ var ( func main() { log.Println("=== Builder Agent Starting ===") + // Start guest-agent for exec/debugging support (runs in background) + startGuestAgent() + // Start vsock listener first (so host can connect as soon as VM is ready) listener, err := startVsockListener() if err != nil { @@ -139,6 +142,38 @@ func startVsockListener() (*vsock.Listener, error) { return nil, fmt.Errorf("failed to listen on vsock port %d after retries: %v", vsockPort, err) } +// startGuestAgent starts the guest-agent binary for exec/debugging support. +// The guest-agent listens on vsock port 2222 and provides exec capability +// so operators can debug failed builds. +func startGuestAgent() { + guestAgentPath := "/usr/bin/guest-agent" + + // Check if guest-agent exists + if _, err := os.Stat(guestAgentPath); os.IsNotExist(err) { + log.Printf("guest-agent not found at %s (exec disabled)", guestAgentPath) + return + } + + // Start guest-agent in background + cmd := exec.Command(guestAgentPath) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Start(); err != nil { + log.Printf("Failed to start guest-agent: %v", err) + return + } + + log.Printf("Started guest-agent (PID %d) for exec support", cmd.Process.Pid) + + // Let the process run in background - don't wait for it + go func() { + if err := cmd.Wait(); err != nil { + log.Printf("guest-agent exited: %v", err) + } + }() +} + // handleHostConnection handles a connection from the host func handleHostConnection(conn net.Conn) { defer conn.Close() diff --git a/lib/builds/images/generic/Dockerfile b/lib/builds/images/generic/Dockerfile index f2f40418..83a080f9 100644 --- a/lib/builds/images/generic/Dockerfile +++ b/lib/builds/images/generic/Dockerfile @@ -1,10 +1,10 @@ # Generic Builder Image -# Contains rootless BuildKit + builder agent +# Contains rootless BuildKit + builder agent + guest-agent for debugging # Builds any Dockerfile provided by the user FROM moby/buildkit:rootless AS buildkit -# Build the builder-agent (multi-stage build from hypeman repo) +# Build the builder-agent and guest-agent (multi-stage build from hypeman repo) FROM golang:1.25-alpine AS agent-builder WORKDIR /app @@ -13,12 +13,18 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download -# Copy only the builder_agent source +# Copy the builder_agent and guest_agent sources COPY lib/builds/builder_agent/ ./lib/builds/builder_agent/ +COPY lib/system/guest_agent/ ./lib/system/guest_agent/ +# Only copy proto files for guest-agent (not client.go which has host-side deps) +COPY lib/guest/guest.pb.go lib/guest/guest_grpc.pb.go ./lib/guest/ -# Build the agent +# Build the builder-agent RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /builder-agent ./lib/builds/builder_agent +# Build the guest-agent (for exec into builder VMs) +RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /guest-agent ./lib/system/guest_agent + # Final builder image - minimal alpine base FROM alpine:3.21 @@ -28,8 +34,9 @@ COPY --from=buildkit /usr/bin/buildctl-daemonless.sh /usr/bin/buildctl-daemonles COPY --from=buildkit /usr/bin/buildkitd /usr/bin/buildkitd COPY --from=buildkit /usr/bin/buildkit-runc /usr/bin/runc -# Copy builder agent +# Copy builder agent and guest agent COPY --from=agent-builder /builder-agent /usr/bin/builder-agent +COPY --from=agent-builder /guest-agent /usr/bin/guest-agent # Install minimal dependencies RUN apk add --no-cache \ From 3a89b1b356b4c13f92c45ac7ffd7b18fd6221082 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:09:51 -0500 Subject: [PATCH 28/42] fix(e2e): fix state comparison and image name matching in E2E test - Convert state to lowercase for comparison (API returns 'Running' not 'running') - Use build ID matching for imported images (API normalizes registry names) E2E test now passes for full build + VM run flow. --- scripts/e2e-build-test.sh | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index 0ca8fc4e..cdef866c 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -207,13 +207,14 @@ import_image() { BUILD_ID=$(echo "$IMAGE_NAME" | sed -E 's|.*/([^/:]+)(:[^/]*)?$|\1|') # Wait for image to be ready - # Look specifically for the image with matching name (not just build ID, since there may be docker.io versions) + # The API may normalize image names (e.g., 10.102.0.1:8083/builds/xxx -> docker.io/builds/xxx) + # So we need to check for both the original name and the normalized version log "Waiting for image conversion..." for i in $(seq 1 60); do - # Query the list endpoint and filter by exact name prefix + # Query the list endpoint and filter by build ID (works regardless of registry prefix) RESPONSE=$(curl -s "$API_URL/images" \ -H "Authorization: Bearer $token" | \ - jq --arg name "$IMAGE_NAME" '[.[] | select(.name == $name)] | .[0] // empty') + jq --arg buildid "$BUILD_ID" '[.[] | select(.name | contains($buildid))] | .[0] // empty') if [ -z "$RESPONSE" ] || [ "$RESPONSE" = "null" ]; then echo -ne "\r Waiting for image... (poll $i/60)..." >&2 @@ -317,7 +318,10 @@ run_built_image() { STATE=$(echo "$RESPONSE" | jq -r '.state') - case "$STATE" in + # Convert state to lowercase for comparison (API may return "Running" or "running") + STATE_LOWER=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') + + case "$STATE_LOWER" in "running") log "✓ Instance is running" break @@ -337,8 +341,8 @@ run_built_image() { done echo "" - if [ "$STATE" != "running" ]; then - error "Instance did not start in time" + if [ "$STATE_LOWER" != "running" ]; then + error "Instance did not start in time (final state: $STATE)" cleanup_instance "$token" "$INSTANCE_ID" return 1 fi From 1db9fece4696350ceee91f7cf2d99c8f61e41af3 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:14:56 -0500 Subject: [PATCH 29/42] fix(registry): preserve registry host in image names when triggering conversion Previously, when the builder pushed to 10.102.0.1:8083/builds/xxx, the registry would extract only the path (/builds/xxx) and normalize it to docker.io/builds/xxx. This was confusing because: - docker.io implies Docker Hub, but these are local builds - Could conflict with real Docker Hub images - Lost the original registry URL The fix includes the request's Host header when building the full repository path, so images are now stored as 10.102.0.1:8083/builds/xxx as expected. --- lib/registry/registry.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/registry/registry.go b/lib/registry/registry.go index 9f43309b..d3ef7002 100644 --- a/lib/registry/registry.go +++ b/lib/registry/registry.go @@ -67,9 +67,17 @@ func (r *Registry) Handler() http.Handler { if req.Method == http.MethodPut { matches := manifestPutPattern.FindStringSubmatch(req.URL.Path) if matches != nil { - repo := matches[1] + pathRepo := matches[1] reference := matches[2] + // Include the host to form the full repository path + // This preserves the registry host (e.g., "10.102.0.1:8083/builds/xxx") + // instead of normalizing to docker.io + fullRepo := pathRepo + if req.Host != "" { + fullRepo = req.Host + "/" + pathRepo + } + body, err := io.ReadAll(req.Body) req.Body.Close() if err != nil { @@ -94,7 +102,7 @@ func (r *Registry) Handler() http.Handler { r.handler.ServeHTTP(wrapper, req) if wrapper.statusCode == http.StatusCreated { - go r.triggerConversion(repo, reference, digest) + go r.triggerConversion(fullRepo, reference, digest) } return } From 2ab35a2c568843ac50e3fd82684f2e2662db47ab Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:18:17 -0500 Subject: [PATCH 30/42] docs: clean up TODO.md - remove completed tasks Removed completed items: - IP spoofing vulnerability fix - Registry token scope leakage fix - Vsock read deadline handling - SSE streaming implementation - Build secrets via vsock - E2E test enhancement - Build manager unit tests - Guest agent on builder VMs - Runtime/toolchain cleanup Remaining tasks: - Enable cgroups for BuildKit secrets (blocked on team discussion) - Builder image tooling - Keep failed builders for debugging --- lib/builds/TODO.md | 133 +++------------------------------------------ 1 file changed, 8 insertions(+), 125 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index c58baf17..55e96204 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -2,101 +2,11 @@ Outstanding issues and improvements for the build system. -## ✅ High Priority - Security & Bugs (Completed) - -### 1. ~~IP Spoofing Vulnerability~~ ✅ FIXED - -**File:** `lib/middleware/oapi_auth.go` - -**Issue:** The `isInternalVMRequest` function was reading the `X-Real-IP` header directly from the client request. - -**Fix:** Changed to only use `r.RemoteAddr` as the authoritative source. Added security comment explaining why headers should not be trusted. - ---- - -### 2. ~~Registry Token Scope Leakage~~ ✅ FIXED - -**File:** `lib/middleware/oapi_auth.go` - -**Issue:** Registry tokens could potentially be used on non-registry endpoints. - -**Fix:** Both `JwtAuth` middleware and `OapiAuthenticationFunc` now reject tokens with registry-specific claims (`repos`, `scope`, `build_id`) when used for non-registry API authentication. - ---- - -### 3. ~~Missing Read Deadline on Vsock~~ ✅ ALREADY FIXED - -**File:** `lib/builds/manager.go` - -**Issue:** The `waitForResult` function blocked indefinitely on `decoder.Decode()`. - -**Status:** Already implemented with goroutine pattern + connection close on context cancellation (lines 455-486). - --- -## 🟡 Medium Priority - Implementation TODOs - -### 4. ~~SSE Streaming Implementation~~ ✅ DONE +## 🟡 Medium Priority -**Files:** `cmd/api/api/builds.go`, `lib/builds/manager.go`, `lib/builds/types.go` - -**Status:** Implemented proper SSE streaming with: -- `BuildEvent` type with `log`, `status`, and `heartbeat` event types -- `StreamBuildEvents` method in Manager with real-time log tailing via `tail -f` -- Status subscription system for broadcasting status changes to SSE clients -- Heartbeat events every 30 seconds in follow mode -- `follow` query parameter support -- Unit tests for all streaming scenarios - ---- - -### 5. ~~Build Secrets~~ ✅ DONE - -**Files:** `lib/builds/manager.go`, `lib/builds/builder_agent/main.go`, `lib/builds/file_secret_provider.go`, `cmd/api/api/builds.go` - -**Status:** Implemented secure secret injection via vsock: -- Host sends `host_ready` message when connected to builder agent -- Agent requests secrets with `get_secrets` message containing secret IDs -- Host responds with `secrets_response` containing secret values from `SecretProvider` -- Agent writes secrets to `/run/secrets/{id}` for BuildKit consumption -- `FileSecretProvider` reads secrets from a configurable directory -- Unit tests for `FileSecretProvider` with path traversal protection -- **Fixed vsock protocol deadlock** - agent now proactively sends `build_result` when complete -- Added `secrets` field to POST `/builds` API endpoint (JSON array of `{"id": "..."}` objects) -- E2E tested: builds complete successfully and logs stream via SSE - ---- - -## 🟢 Low Priority - Improvements - -### 6. ~~E2E Test Enhancement~~ ✅ DONE - -**File:** `scripts/e2e-build-test.sh` - -**Status:** Enhanced to run a VM with the built image after successful build. The test now: -- Creates an instance from the built image -- Waits for the instance to start -- Executes a test command inside the instance -- Cleans up the instance -- Use `--skip-run` flag to skip the VM test - -### 7. ~~Build Manager Unit Tests~~ ✅ DONE - -**File:** `lib/builds/manager_test.go` - -**Status:** Added comprehensive unit tests with mocked dependencies: -- `TestCreateBuild_Success` - Happy path build creation -- `TestCreateBuild_WithBuildPolicy` - Build with custom policy -- `TestGetBuild_Found/NotFound` - Build retrieval -- `TestListBuilds_Empty/WithBuilds` - Listing builds -- `TestCancelBuild_*` - Cancel scenarios (queued, not found, completed) -- `TestGetBuildLogs_*` - Log retrieval -- `TestBuildQueue_ConcurrencyLimit` - Queue concurrency -- `TestUpdateStatus_*` - Status updates with errors -- `TestRegistryTokenGeneration` - Token generation verification -- `TestCreateBuild_MultipleConcurrent` - Concurrent build creation - -### 8. Enable cgroups for BuildKit Secrets +### 1. Enable cgroups for BuildKit Secrets **Issue:** When `--secret` flags are passed to BuildKit, runc requires cgroup mounts that aren't present in the microVM. @@ -178,45 +88,18 @@ Cons: 2. Rebuild initrd: `make initrd` 3. Test builds with secrets -### 9. ~~Guest Agent on Builder VMs~~ ✅ DONE - -**Files:** `lib/builds/images/generic/Dockerfile`, `lib/builds/builder_agent/main.go` - -**Status:** Implemented guest-agent support in builder VMs: -- Builder Dockerfile now builds and includes `/usr/bin/guest-agent` -- Builder-agent starts guest-agent at boot (before build starts) -- Guest-agent listens on vsock port 2222 for exec requests - -**Limitation:** Currently can't test exec because: -1. Builder instances are deleted immediately after build completion (success or failure) -2. Builds fail due to cgroup issue (prevents long-running builds to exec into) +--- -**Future Enhancement:** Add `KeepFailedBuilders` option to keep failed build instances running for debugging. +## 🟢 Low Priority -### 10. Builder Image Tooling +### 2. Builder Image Tooling **File:** `lib/builds/images/README.md` **Suggestion:** Create a script or tooling for building and publishing new builder images. ---- +### 3. Keep Failed Builders for Debugging -## ✅ Completed - -- [x] Remove deprecated `RuntimeNodeJS20` and `RuntimePython312` constants -- [x] Remove `Runtime` field from API and storage -- [x] Remove `ToolchainVersion` from `BuildProvenance` -- [x] Update OpenAPI spec to remove runtime field -- [x] Rename `/builds/{id}/logs` to `/builds/{id}/events` with typed events -- [x] Remove unused `deref` function -- [x] Update documentation (README.md, PLAN.md) -- [x] Fix context leak in volume cleanup (use `context.Background()`) -- [x] Fix incorrect error wrapping in config volume setup -- [x] Fix IP spoofing vulnerability in `isInternalVMRequest` -- [x] Add registry token rejection to `OapiAuthenticationFunc` -- [x] Verify vsock read deadline handling (already fixed with goroutine pattern) -- [x] E2E test enhancement - run VM with built image -- [x] Build manager unit tests with mocked dependencies -- [x] SSE streaming implementation with typed events, follow mode, and heartbeats -- [x] Build secrets via vsock with FileSecretProvider +**Suggestion:** Add `KeepFailedBuilders` option to keep failed build instances running for debugging via exec. +Currently, builder instances are deleted immediately after build completion (success or failure), making it impossible to debug failed builds interactively. From 39e2e5b96a562ad617bb376f53aa8b84c4f6b47c Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:18:57 -0500 Subject: [PATCH 31/42] docs: remove 'Keep Failed Builders' from TODO and delete PLAN.md --- lib/builds/PLAN.md | 174 --------------------------------------------- lib/builds/TODO.md | 6 -- 2 files changed, 180 deletions(-) delete mode 100644 lib/builds/PLAN.md diff --git a/lib/builds/PLAN.md b/lib/builds/PLAN.md deleted file mode 100644 index f06a80e5..00000000 --- a/lib/builds/PLAN.md +++ /dev/null @@ -1,174 +0,0 @@ -# Build System Roadmap - -## Current State (v0.1) - -- ✅ Source-to-image builds in isolated microVMs -- ✅ BuildKit-based builds with daemonless execution -- ✅ Tenant-isolated registry caching -- ✅ Generic builder (any Dockerfile/runtime supported) -- ✅ Vsock communication for build results -- ✅ Cgroup mounting for container runtime support - -## Planned Improvements - -### Phase 1: Cache Optimization - -**Goal**: Reduce build times by sharing common base layers across tenants. - -#### Multi-tier Cache Strategy - -``` -Import order (first match wins): -1. shared/{runtime}/base ← Pre-warmed with OS + runtime layers (read-only) -2. {tenant}/{runtime}/{hash} ← Tenant-specific dependency layers - -Export to: -→ {tenant}/{runtime}/{hash} ← Only tenant-specific layers -``` - -#### Benefits -- **Fast builds**: Common layers (apt packages, Node.js binary, etc.) are shared -- **Tenant isolation**: Application dependencies remain isolated -- **No cross-tenant poisoning**: Tenants can only write to their own scope -- **Controlled shared cache**: Only operators can update the shared base cache - -#### Implementation Tasks -- [ ] Update `cache.go` with `ImportCacheArgs() []string` returning multiple args -- [ ] Update `builder_agent/main.go` to handle multiple `--import-cache` flags -- [ ] Add CLI/API endpoint for pre-warming shared cache -- [ ] Create cron job or webhook to refresh shared cache on base image updates -- [ ] Document cache warming process in README - -### Phase 2: Security Hardening - -#### Secret Management -- [ ] Implement vsock-based secret injection (secrets never written to disk) -- [ ] Add secret scoping per build (which secrets a build can access) -- [ ] Audit logging for secret access during builds -- [ ] Integration with external secret managers (Vault, AWS Secrets Manager) - -#### Network Policy -- [ ] Implement domain allowlist for `egress` mode -- [ ] Add `isolated` mode (no network access during build phase) -- [ ] Rate limiting on registry pushes to prevent abuse -- [ ] DNS filtering for allowed domains - -#### Build Provenance & Supply Chain Security -- [ ] Sign build provenance with Sigstore/cosign -- [ ] SLSA Level 2 compliance (authenticated build process) -- [ ] SBOM (Software Bill of Materials) generation during builds -- [ ] Vulnerability scanning of built images before push - -### Phase 3: Security Hardening - -The generic builder now supports any Dockerfile. Security improvements: - -- [ ] Security review: sandbox custom Dockerfiles more strictly -- [ ] Validate Dockerfile doesn't use dangerous instructions -- [ ] Consider read-only base image allowlist -- [ ] Rate limiting for build submissions - -### Phase 4: Performance & Observability - -#### Metrics (Prometheus) -- [ ] `hypeman_build_duration_seconds` - histogram by runtime, status -- [ ] `hypeman_build_cache_hits_total` - counter for cache hits/misses -- [ ] `hypeman_build_queue_wait_seconds` - time spent in queue -- [ ] `hypeman_build_vm_boot_seconds` - microVM boot time -- [ ] `hypeman_build_push_duration_seconds` - registry push time - -#### Logging Improvements -- [ ] Structured JSON logs from builder agent -- [ ] Log streaming during build (not just after completion) -- [ ] Build log retention policy - -#### Distributed Builds -- [ ] Build worker pool across multiple hosts -- [ ] Load balancing for build queue (consistent hashing by tenant?) -- [ ] Horizontal scaling of build capacity -- [ ] Worker health checks and automatic failover - -## Security Model - -### Threat Model - -| Threat | Mitigation | Status | -|--------|------------|--------| -| Container escape to host | MicroVM isolation (separate kernel) | ✅ Implemented | -| Cross-tenant cache poisoning | Tenant-scoped cache paths | ✅ Implemented | -| Host kernel exploit | Separate kernel per VM | ✅ Implemented | -| Malicious dependency exfiltration | Network isolation (egress control) | 🔄 Partial | -| Secret theft during build | Vsock-only secret injection | 📋 Planned | -| Registry credential theft | Per-build short-lived tokens | 📋 Planned | -| Resource exhaustion (DoS) | VM resource limits | ✅ Implemented | -| Build log information leak | Tenant-scoped log access | ✅ Implemented | - -### Security Boundaries - -``` -┌─────────────────────────────────────────────────────────────┐ -│ Host System │ -│ ┌─────────────────────────────────────────────────────────┐│ -│ │ Hypeman API ││ -│ │ - JWT authentication ││ -│ │ - Tenant isolation at API level ││ -│ └─────────────────────────────────────────────────────────┘│ -│ │ │ -│ ┌───────────────────────────┼───────────────────────────┐ │ -│ │ MicroVM Boundary (Cloud Hypervisor) │ │ -│ │ ┌─────────────────────────────────────────────────┐ │ │ -│ │ │ Builder VM │ │ │ -│ │ │ - Separate kernel │ │ │ -│ │ │ - Ephemeral (destroyed after build) │ │ │ -│ │ │ - Limited network (egress only to registry) │ │ │ -│ │ │ - No access to other tenants' data │ │ │ -│ │ │ ┌─────────────────────────────────────────┐ │ │ │ -│ │ │ │ BuildKit (rootless) │ │ │ │ -│ │ │ │ - User namespace isolation │ │ │ │ -│ │ │ │ - No real root privileges │ │ │ │ -│ │ │ └─────────────────────────────────────────┘ │ │ │ -│ │ └─────────────────────────────────────────────────┘ │ │ -│ └────────────────────────────────────────────────────────┘ │ -└─────────────────────────────────────────────────────────────┘ -``` - -### Not Protected (By Design) - -These are inherent to the build process and cannot be fully mitigated: - -1. **Malicious code execution during package install** - `npm install` and `pip install` execute arbitrary code by design -2. **Supply chain attacks on upstream packages** - Typosquatting, compromised maintainers, etc. -3. **Tenant poisoning their own cache** - A tenant can push malicious layers to their own cache scope -4. **Information leakage via build output** - Malicious deps can encode secrets in build artifacts - -## Open Questions - -1. **Custom Dockerfiles**: Should we support user-provided Dockerfiles? - - Pro: Flexibility for advanced users - - Con: Larger attack surface, harder to secure - - Possible middle ground: Allowlist of base images - -2. **Cache TTL Policy**: How long should tenant caches be retained? - - Options: 7 days, 30 days, size-based eviction, never (until explicit delete) - - Consider: Storage costs vs build speed - -3. **Build Artifact Signing**: Required for all builds or opt-in? - - Required: Better security posture, SLSA compliance - - Opt-in: Less friction for getting started - -4. **Multi-arch Builds**: Worth the complexity? - - Use case: Deploy same image to ARM and x86 - - Complexity: Requires QEMU or cross-compilation support - -5. **Build Concurrency Limits**: Per-tenant or global? - - Per-tenant: Fair sharing, prevents noisy neighbor - - Global: Simpler, but one tenant could starve others - -## References - -- [BuildKit GitHub](https://github.com/moby/buildkit) -- [Rootless Containers](https://rootlesscontaine.rs/) -- [SLSA Framework](https://slsa.dev/) -- [Sigstore](https://www.sigstore.dev/) -- [Cloud Hypervisor](https://github.com/cloud-hypervisor/cloud-hypervisor) - diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index 55e96204..f0d504e5 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -97,9 +97,3 @@ Cons: **File:** `lib/builds/images/README.md` **Suggestion:** Create a script or tooling for building and publishing new builder images. - -### 3. Keep Failed Builders for Debugging - -**Suggestion:** Add `KeepFailedBuilders` option to keep failed build instances running for debugging via exec. - -Currently, builder instances are deleted immediately after build completion (success or failure), making it impossible to debug failed builds interactively. From 99a38c4cae857555676b676357f416e730eb404c Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 17:38:35 -0500 Subject: [PATCH 32/42] fix(tests): update registry tests to use full host in image names After the registry fix to preserve host in image names, tests need to include the serverHost when looking up images. Also fixes TestRegistryPushAndConvert timeout issue. Note: Some tests may still fail due to Docker Hub rate limiting. --- cmd/api/api/registry_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/api/api/registry_test.go b/cmd/api/api/registry_test.go index 8605b238..e88978ef 100644 --- a/cmd/api/api/registry_test.go +++ b/cmd/api/api/registry_test.go @@ -73,7 +73,8 @@ func TestRegistryPushAndConvert(t *testing.T) { t.Log("Push successful!") // Wait for image to be converted - imageName := "test/alpine@" + digest.String() + // Include serverHost since our registry now stores images with the full host + imageName := serverHost + "/test/alpine@" + digest.String() imgResp := waitForImageReady(t, svc, imageName, 60*time.Second) assert.NotNil(t, imgResp.SizeBytes, "ready image should have size") } @@ -124,7 +125,8 @@ func TestRegistryPushAndCreateInstance(t *testing.T) { require.NoError(t, err) // Wait for image to be ready - imageName := "test/alpine@" + digest.String() + // Include serverHost since our registry now stores images with the full host + imageName := serverHost + "/test/alpine@" + digest.String() waitForImageReady(t, svc, imageName, 60*time.Second) // Create instance with pushed image @@ -362,7 +364,8 @@ func TestRegistryTagPush(t *testing.T) { t.Log("Push successful!") // The image should be registered with the computed digest, not the tag - imageName := "tag-test/alpine@" + digest.String() + // Include serverHost since our registry now stores images with the full host + imageName := serverHost + "/tag-test/alpine@" + digest.String() waitForImageReady(t, svc, imageName, 60*time.Second) // Verify image appears in ListImages (GET /images) @@ -415,7 +418,8 @@ func TestRegistryDockerV2ManifestConversion(t *testing.T) { // Wait for image to be converted // The server converts Docker v2 to OCI format internally, resulting in a different digest - imgResp := waitForImageReady(t, svc, "dockerv2-test/alpine:v1", 60*time.Second) + // Include serverHost since our registry now stores images with the full host + imgResp := waitForImageReady(t, svc, serverHost+"/dockerv2-test/alpine:v1", 60*time.Second) assert.NotNil(t, imgResp.SizeBytes, "ready image should have size") assert.NotEmpty(t, imgResp.Digest, "image should have digest") } From 983de98ce69c4e17a8bbaddf8b7ade0441fe58a1 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 19:15:14 -0500 Subject: [PATCH 33/42] feat(init): add cgroup2 mount for BuildKit/runc support Mount cgroup2 filesystem at /sys/fs/cgroup during VM init and bind-mount it to the new root. This enables runc (used by BuildKit) to work properly for builds with secrets. Security notes: - cgroup v2 has no release_agent escape vector (unlike v1) - VMs are already isolated by Cloud Hypervisor (hardware boundary) - This is non-fatal if the kernel doesn't support cgroup2 To activate, rebuild initrd: make init && make initrd --- lib/builds/TODO.md | 87 +++---------------------- lib/builds/file_secret_provider.go | 1 + lib/builds/file_secret_provider_test.go | 1 + lib/system/init/mount.go | 17 ++++- 4 files changed, 28 insertions(+), 78 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index f0d504e5..e2a94b3c 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -4,89 +4,22 @@ Outstanding issues and improvements for the build system. --- -## 🟡 Medium Priority +## ✅ Completed ### 1. Enable cgroups for BuildKit Secrets -**Issue:** When `--secret` flags are passed to BuildKit, runc requires cgroup mounts that aren't present in the microVM. +**Status:** ✅ Implemented (Option A) -**Error:** `runc run failed: no cgroup mount found in mountinfo` +**Changes:** Added cgroup2 mount to `lib/system/init/mount.go`: +- `mountEssentials()` now mounts `/sys/fs/cgroup` with cgroup2 filesystem +- `bindMountsToNewRoot()` now bind-mounts cgroups to the new root -**Status:** The secrets API flow works correctly (host → vsock → agent → BuildKit flags), but BuildKit execution fails due to missing cgroups. - -**Workaround:** Builds without secrets work fine. The secrets code is ready once cgroups are enabled. - -#### Root Cause - -The VM init (`lib/system/init/mount.go`) mounts `/proc`, `/sys`, `/dev`, `/dev/pts`, `/dev/shm` but does NOT mount `/sys/fs/cgroup`. When BuildKit receives `--secret` flags, it uses runc which requires cgroups even for rootless execution. - -#### Proposed Solutions - -**Option A: Add cgroup mount to VM init (all VMs)** - -File: `lib/system/init/mount.go` - -```go -// In mountEssentials(), add: -if err := os.MkdirAll("/sys/fs/cgroup", 0755); err != nil { - return fmt.Errorf("mkdir /sys/fs/cgroup: %w", err) -} -if err := syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, ""); err != nil { - log.Info("mount", "cgroup2 failed (non-fatal)") -} - -// In bindMountsToNewRoot(), add to mounts slice: -{"/sys/fs/cgroup", newroot + "/sys/fs/cgroup"}, +**To activate:** Rebuild the embedded binaries, then start the API server: +```bash +make build-embedded # Rebuilds lib/system/init/init +make dev # Or: make build && ./bin/hypeman ``` - -Pros: -- Enables cgroups for all VM workloads -- Happens early in boot before user processes -- Properly bind-mounts to new root - -Cons: -- All VMs get cgroup access (larger attack surface, though mitigated by VM isolation) - -**Option B: Add cgroup mount in builder-agent only** - -File: `lib/builds/builder_agent/main.go` - -```go -func mountCgroups() error { - if err := os.MkdirAll("/sys/fs/cgroup", 0755); err != nil { - return err - } - return syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, "") -} -``` - -Pros: -- Only affects builder VMs -- Minimal scope - -Cons: -- Late in boot (after chroot) -- May not work if /sys is read-only in newroot - -#### Security Analysis - -| Concern | Risk Level | Mitigation | -|---------|------------|------------| -| Container escape via cgroup | Very Low | VM hypervisor isolation + cgroup v2 (no release_agent) | -| Resource manipulation | Low | VM has hypervisor-level resource limits | -| Attack surface for user VMs | Medium | Consider making cgroups opt-in or read-only | - -**Recommendation:** Option A with cgroup v2 is safe because: -1. VMs are already isolated by Cloud Hypervisor (hardware boundary) -2. Builder VMs are ephemeral (destroyed after each build) -3. Builder runs as unprivileged user (uid 1000) -4. Cgroup v2 has better security than v1 (no release_agent escape vector) - -#### After Implementation - -1. Rebuild init binary: `make init` -2. Rebuild initrd: `make initrd` -3. Test builds with secrets +The initrd is automatically rebuilt on first VM start when it detects the embedded binaries have changed. --- diff --git a/lib/builds/file_secret_provider.go b/lib/builds/file_secret_provider.go index 8262e0a7..382ba7cf 100644 --- a/lib/builds/file_secret_provider.go +++ b/lib/builds/file_secret_provider.go @@ -63,3 +63,4 @@ func (p *FileSecretProvider) GetSecrets(ctx context.Context, secretIDs []string) // Ensure FileSecretProvider implements SecretProvider var _ SecretProvider = (*FileSecretProvider)(nil) + diff --git a/lib/builds/file_secret_provider_test.go b/lib/builds/file_secret_provider_test.go index 5ad93f71..784ed96d 100644 --- a/lib/builds/file_secret_provider_test.go +++ b/lib/builds/file_secret_provider_test.go @@ -100,3 +100,4 @@ func TestNoOpSecretProvider(t *testing.T) { assert.Empty(t, secrets) } + diff --git a/lib/system/init/mount.go b/lib/system/init/mount.go index 3dcee32a..fa1fc89d 100644 --- a/lib/system/init/mount.go +++ b/lib/system/init/mount.go @@ -14,6 +14,7 @@ import ( // This function mounts: // - /dev/pts (pseudo-terminals) // - /dev/shm (shared memory) +// - /sys/fs/cgroup (cgroup2 for container runtimes like runc) func mountEssentials(log *Logger) error { // Create mount points for pts and shm (proc/sys/dev already exist from wrapper) for _, dir := range []string{"/dev/pts", "/dev/shm"} { @@ -32,6 +33,19 @@ func mountEssentials(log *Logger) error { return fmt.Errorf("chmod /dev/shm: %w", err) } + // Mount cgroup2 for container runtimes (runc/BuildKit require cgroups) + // This is safe because VMs are already isolated by the hypervisor, and + // cgroup v2 has better security than v1 (no release_agent escape vector) + if err := os.MkdirAll("/sys/fs/cgroup", 0755); err != nil { + return fmt.Errorf("mkdir /sys/fs/cgroup: %w", err) + } + if err := syscall.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", 0, ""); err != nil { + // Non-fatal: some kernels may not have cgroup2 support + log.Info("mount", "cgroup2 mount failed (non-fatal): "+err.Error()) + } else { + log.Info("mount", "mounted cgroup2") + } + log.Info("mount", "mounted devpts/shm") // Set up serial console now that /dev is mounted @@ -99,7 +113,7 @@ func bindMountsToNewRoot(log *Logger) error { newroot := "/overlay/newroot" // Create mount points in new root - for _, dir := range []string{"proc", "sys", "dev", "dev/pts"} { + for _, dir := range []string{"proc", "sys", "sys/fs/cgroup", "dev", "dev/pts"} { if err := os.MkdirAll(newroot+"/"+dir, 0755); err != nil { return fmt.Errorf("mkdir %s: %w", dir, err) } @@ -109,6 +123,7 @@ func bindMountsToNewRoot(log *Logger) error { mounts := []struct{ src, dst string }{ {"/proc", newroot + "/proc"}, {"/sys", newroot + "/sys"}, + {"/sys/fs/cgroup", newroot + "/sys/fs/cgroup"}, {"/dev", newroot + "/dev"}, {"/dev/pts", newroot + "/dev/pts"}, } From 21f82d4020568a7e1d3111137b9d39418772a087 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 19:40:48 -0500 Subject: [PATCH 34/42] docs: update TODO.md with verified cgroup2 implementation --- lib/builds/TODO.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index e2a94b3c..d2e4d962 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -8,18 +8,13 @@ Outstanding issues and improvements for the build system. ### 1. Enable cgroups for BuildKit Secrets -**Status:** ✅ Implemented (Option A) +**Status:** ✅ Implemented and tested **Changes:** Added cgroup2 mount to `lib/system/init/mount.go`: - `mountEssentials()` now mounts `/sys/fs/cgroup` with cgroup2 filesystem - `bindMountsToNewRoot()` now bind-mounts cgroups to the new root -**To activate:** Rebuild the embedded binaries, then start the API server: -```bash -make build-embedded # Rebuilds lib/system/init/init -make dev # Or: make build && ./bin/hypeman -``` -The initrd is automatically rebuilt on first VM start when it detects the embedded binaries have changed. +**Verified:** E2E build test passes (`./scripts/e2e-build-test.sh`) --- From 61f505143f2d4694e06f034e2e1290fbd2d03537 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 19:41:34 -0500 Subject: [PATCH 35/42] chore: clean up TODO after cgroup2 implementation verified --- lib/builds/TODO.md | 16 +-------------- lib/builds/manager.go | 40 ++++++++++++++++++------------------- lib/builds/types.go | 22 -------------------- lib/middleware/oapi_auth.go | 4 ++-- scripts/e2e-build-test.sh | 22 ++++++++++---------- 5 files changed, 34 insertions(+), 70 deletions(-) diff --git a/lib/builds/TODO.md b/lib/builds/TODO.md index d2e4d962..9cced6c5 100644 --- a/lib/builds/TODO.md +++ b/lib/builds/TODO.md @@ -4,23 +4,9 @@ Outstanding issues and improvements for the build system. --- -## ✅ Completed - -### 1. Enable cgroups for BuildKit Secrets - -**Status:** ✅ Implemented and tested - -**Changes:** Added cgroup2 mount to `lib/system/init/mount.go`: -- `mountEssentials()` now mounts `/sys/fs/cgroup` with cgroup2 filesystem -- `bindMountsToNewRoot()` now bind-mounts cgroups to the new root - -**Verified:** E2E build test passes (`./scripts/e2e-build-test.sh`) - ---- - ## 🟢 Low Priority -### 2. Builder Image Tooling +### Builder Image Tooling **File:** `lib/builds/images/README.md` diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 5f949807..82cbee4c 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -466,30 +466,30 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( // Handle messages from agent until we get the build result for { - // Use a goroutine for decoding so we can respect context cancellation. - type decodeResult struct { - response VsockMessage - err error - } - resultCh := make(chan decodeResult, 1) + // Use a goroutine for decoding so we can respect context cancellation. + type decodeResult struct { + response VsockMessage + err error + } + resultCh := make(chan decodeResult, 1) - go func() { - var response VsockMessage - err := decoder.Decode(&response) - resultCh <- decodeResult{response: response, err: err} - }() + go func() { + var response VsockMessage + err := decoder.Decode(&response) + resultCh <- decodeResult{response: response, err: err} + }() // Wait for either a message or context cancellation var dr decodeResult - select { - case <-ctx.Done(): - conn.Close() - <-resultCh - return nil, ctx.Err() + select { + case <-ctx.Done(): + conn.Close() + <-resultCh + return nil, ctx.Err() case dr = <-resultCh: - if dr.err != nil { + if dr.err != nil { return nil, fmt.Errorf("read message: %w", dr.err) - } + } } // Handle message based on type @@ -516,8 +516,8 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( // Build completed if dr.response.Result == nil { return nil, fmt.Errorf("received build_result with nil result") - } - return dr.response.Result, nil + } + return dr.response.Result, nil default: m.logger.Warn("unexpected message type from agent", "type", dr.response.Type) diff --git a/lib/builds/types.go b/lib/builds/types.go index a2832a9f..310e7124 100644 --- a/lib/builds/types.go +++ b/lib/builds/types.go @@ -160,28 +160,6 @@ type BuildResult struct { DurationMS int64 `json:"duration_ms"` } -// BuildEvent represents a typed SSE event for build streaming -type BuildEvent struct { - // Type is one of "log", "status", or "heartbeat" - Type string `json:"type"` - - // Timestamp is when the event occurred - Timestamp time.Time `json:"timestamp"` - - // Content is the log line content (only for type="log") - Content string `json:"content,omitempty"` - - // Status is the new build status (only for type="status") - Status string `json:"status,omitempty"` -} - -// BuildEvent type constants -const ( - EventTypeLog = "log" - EventTypeStatus = "status" - EventTypeHeartbeat = "heartbeat" -) - // DefaultBuildPolicy returns the default build policy func DefaultBuildPolicy() BuildPolicy { return BuildPolicy{ diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 430e822a..2c79714e 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -191,8 +191,8 @@ func isInternalVMRequest(r *http.Request) bool { ip := r.RemoteAddr // RemoteAddr is "IP:port" format, extract just the IP - if idx := strings.LastIndex(ip, ":"); idx != -1 { - ip = ip[:idx] + if idx := strings.LastIndex(ip, ":"); idx != -1 { + ip = ip[:idx] } // Check if it's from the VM network (10.102.x.x) diff --git a/scripts/e2e-build-test.sh b/scripts/e2e-build-test.sh index cdef866c..f454063b 100755 --- a/scripts/e2e-build-test.sh +++ b/scripts/e2e-build-test.sh @@ -447,10 +447,10 @@ main() { "failed") error "❌ Build failed!" echo "$BUILD_RESPONSE" | jq . - echo "" - log "=== Build Logs ===" - get_logs "$TOKEN" "$BUILD_ID" - echo "" + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" error "=== E2E Test FAILED ===" rm -f "$SOURCE" exit 1 @@ -478,10 +478,10 @@ main() { exit 1 fi - echo "" - log "=== Build Logs ===" - get_logs "$TOKEN" "$BUILD_ID" - echo "" + echo "" + log "=== Build Logs ===" + get_logs "$TOKEN" "$BUILD_ID" + echo "" # Run the built image (unless skipped) if [ "$SKIP_RUN" = "false" ]; then @@ -494,9 +494,9 @@ main() { log "✅ VM run test passed!" else error "❌ VM run test failed!" - rm -f "$SOURCE" - exit 1 - fi + rm -f "$SOURCE" + exit 1 + fi else warn "No image_ref in build response, skipping VM test" fi From 6456b2b3020bf483941aef0bbd3e5a6a0220b5a3 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 19:52:32 -0500 Subject: [PATCH 36/42] fix(builds): restore missing BuildEvent type definition The BuildEvent type and event type constants were accidentally removed from types.go, causing build failures in lib/providers. --- lib/builds/types.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/builds/types.go b/lib/builds/types.go index 310e7124..08b05881 100644 --- a/lib/builds/types.go +++ b/lib/builds/types.go @@ -139,6 +139,28 @@ type BuildConfig struct { NetworkMode string `json:"network_mode"` } +// BuildEvent represents a typed SSE event for build streaming +type BuildEvent struct { + // Type is one of "log", "status", or "heartbeat" + Type string `json:"type"` + + // Timestamp is when the event occurred + Timestamp time.Time `json:"timestamp"` + + // Content is the log line content (only for type="log") + Content string `json:"content,omitempty"` + + // Status is the new build status (only for type="status") + Status string `json:"status,omitempty"` +} + +// BuildEvent type constants +const ( + EventTypeLog = "log" + EventTypeStatus = "status" + EventTypeHeartbeat = "heartbeat" +) + // BuildResult is returned by the builder agent after a build completes type BuildResult struct { // Success indicates whether the build succeeded From f09e53d98df8c83c01249996dce23dbbc18ab71e Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 20:09:04 -0500 Subject: [PATCH 37/42] test: add SKIP_DOCKER_HUB_TESTS env var to skip rate-limited tests When SKIP_DOCKER_HUB_TESTS=1 is set, tests that require pulling images from Docker Hub are skipped. This allows CI to run without being blocked by Docker Hub rate limiting. Affected tests: - lib/images: TestCreateImage*, TestListImages, TestDeleteImage, TestLayerCaching - lib/instances: TestBasicEndToEnd, TestStandbyAndRestore, TestExecConcurrent, TestCreateInstanceWithNetwork, TestQEMU*, TestVolume*, TestOverlayDisk*, TestAggregateLimits_EnforcedAtRuntime - lib/system: TestEnsureSystemFiles - cmd/api/api: TestCreateImage*, TestCreateInstance*, TestInstanceLifecycle, TestRegistry*, TestCp*, TestExec* - integration: TestSystemdMode --- cmd/api/api/cp_test.go | 2 ++ cmd/api/api/exec_test.go | 2 ++ cmd/api/api/images_test.go | 3 +++ cmd/api/api/instances_test.go | 2 ++ cmd/api/api/registry_test.go | 16 ++++++++++++++++ integration/systemd_test.go | 9 +++++++++ lib/images/manager_test.go | 15 +++++++++++++++ lib/instances/exec_test.go | 1 + lib/instances/manager_test.go | 10 ++++++++++ lib/instances/network_test.go | 1 + lib/instances/qemu_test.go | 2 ++ lib/instances/resource_limits_test.go | 1 + lib/instances/volumes_test.go | 3 +++ lib/system/manager_test.go | 10 ++++++++++ 14 files changed, 77 insertions(+) diff --git a/cmd/api/api/cp_test.go b/cmd/api/api/cp_test.go index 98acf5eb..75cb4af5 100644 --- a/cmd/api/api/cp_test.go +++ b/cmd/api/api/cp_test.go @@ -17,6 +17,7 @@ import ( ) func TestCpToAndFromInstance(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -156,6 +157,7 @@ func TestCpToAndFromInstance(t *testing.T) { } func TestCpDirectoryToInstance(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/cmd/api/api/exec_test.go b/cmd/api/api/exec_test.go index ef474a04..cb3faea0 100644 --- a/cmd/api/api/exec_test.go +++ b/cmd/api/api/exec_test.go @@ -18,6 +18,7 @@ import ( ) func TestExecInstanceNonTTY(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -158,6 +159,7 @@ func TestExecInstanceNonTTY(t *testing.T) { // 2. guest-agent must keep running even after the main app exits // 3. The VM must not kernel panic when the entrypoint exits func TestExecWithDebianMinimal(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/cmd/api/api/images_test.go b/cmd/api/api/images_test.go index 9d5f0590..0a9b0e0c 100644 --- a/cmd/api/api/images_test.go +++ b/cmd/api/api/images_test.go @@ -33,6 +33,7 @@ func TestGetImage_NotFound(t *testing.T) { } func TestCreateImage_Async(t *testing.T) { + skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() @@ -125,6 +126,7 @@ func TestCreateImage_Async(t *testing.T) { } func TestCreateImage_InvalidTag(t *testing.T) { + skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() @@ -171,6 +173,7 @@ func TestCreateImage_InvalidName(t *testing.T) { } func TestCreateImage_Idempotent(t *testing.T) { + skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() diff --git a/cmd/api/api/instances_test.go b/cmd/api/api/instances_test.go index ffe45a0d..1560dcac 100644 --- a/cmd/api/api/instances_test.go +++ b/cmd/api/api/instances_test.go @@ -33,6 +33,7 @@ func TestGetInstance_NotFound(t *testing.T) { } func TestCreateInstance_ParsesHumanReadableSizes(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -129,6 +130,7 @@ func TestCreateInstance_InvalidSizeFormat(t *testing.T) { } func TestInstanceLifecycle_StopStart(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Skip("/dev/kvm not available - skipping lifecycle test") diff --git a/cmd/api/api/registry_test.go b/cmd/api/api/registry_test.go index e88978ef..7969ef62 100644 --- a/cmd/api/api/registry_test.go +++ b/cmd/api/api/registry_test.go @@ -25,6 +25,16 @@ import ( "github.com/stretchr/testify/require" ) +// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. +// This is used to skip tests that require pulling from Docker Hub in CI +// environments where rate limiting may be an issue. +func skipIfNoDockerHub(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { + t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") + } +} + // setupRegistryTest creates a test service with a mounted OCI registry server. // Returns the service (for API calls) and the server host (for building push URLs). func setupRegistryTest(t *testing.T) (*ApiService, string) { @@ -47,6 +57,7 @@ func setupRegistryTest(t *testing.T) (*ApiService, string) { } func TestRegistryPushAndConvert(t *testing.T) { + skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull a small image from Docker Hub to push to our registry @@ -92,6 +103,7 @@ func TestRegistryVersionCheck(t *testing.T) { } func TestRegistryPushAndCreateInstance(t *testing.T) { + skipIfNoDockerHub(t) // This is a full e2e test that requires KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Skip("/dev/kvm not available - skipping VM creation test") @@ -174,6 +186,7 @@ func TestRegistryPushAndCreateInstance(t *testing.T) { // TestRegistryLayerCaching verifies that pushing the same image twice // reuses cached layers and doesn't re-upload them. func TestRegistryLayerCaching(t *testing.T) { + skipIfNoDockerHub(t) _, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub @@ -257,6 +270,7 @@ func TestRegistryLayerCaching(t *testing.T) { // TestRegistrySharedLayerCaching verifies that pushing different images // that share layers reuses the cached shared layers. func TestRegistrySharedLayerCaching(t *testing.T) { + skipIfNoDockerHub(t) _, serverHost := setupRegistryTest(t) // Pull alpine image (this will be our base) @@ -338,6 +352,7 @@ func TestRegistrySharedLayerCaching(t *testing.T) { // TestRegistryTagPush verifies that pushing with a tag reference (not digest) // correctly triggers conversion. The server computes the digest from the manifest. func TestRegistryTagPush(t *testing.T) { + skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub @@ -391,6 +406,7 @@ func TestRegistryTagPush(t *testing.T) { // Docker v2 manifest (as returned by local Docker daemon) is correctly converted // to OCI format and the image conversion succeeds. func TestRegistryDockerV2ManifestConversion(t *testing.T) { + skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub (OCI format) diff --git a/integration/systemd_test.go b/integration/systemd_test.go index 16ba8638..1991642b 100644 --- a/integration/systemd_test.go +++ b/integration/systemd_test.go @@ -22,6 +22,14 @@ import ( "github.com/stretchr/testify/require" ) +// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. +func skipIfNoDockerHub(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { + t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") + } +} + // TestSystemdMode verifies that hypeman correctly detects and runs // systemd-based images with systemd as PID 1. // @@ -31,6 +39,7 @@ import ( // - Starts systemd as PID 1 // - Injects and starts the hypeman-agent.service func TestSystemdMode(t *testing.T) { + skipIfNoDockerHub(t) if testing.Short() { t.Skip("skipping integration test in short mode") } diff --git a/lib/images/manager_test.go b/lib/images/manager_test.go index 406566ef..d0d62c05 100644 --- a/lib/images/manager_test.go +++ b/lib/images/manager_test.go @@ -12,7 +12,16 @@ import ( "github.com/stretchr/testify/require" ) +// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. +func skipIfNoDockerHub(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { + t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") + } +} + func TestCreateImage(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -83,6 +92,7 @@ func TestCreateImage(t *testing.T) { } func TestCreateImageDifferentTag(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -105,6 +115,7 @@ func TestCreateImageDifferentTag(t *testing.T) { } func TestCreateImageDuplicate(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -134,6 +145,7 @@ func TestCreateImageDuplicate(t *testing.T) { } func TestListImages(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -163,6 +175,7 @@ func TestListImages(t *testing.T) { } func TestGetImage(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -198,6 +211,7 @@ func TestGetImageNotFound(t *testing.T) { } func TestDeleteImage(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -270,6 +284,7 @@ func TestNormalizedRefParsing(t *testing.T) { } func TestLayerCaching(t *testing.T) { + skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) diff --git a/lib/instances/exec_test.go b/lib/instances/exec_test.go index 1efb471e..d8ccbcd7 100644 --- a/lib/instances/exec_test.go +++ b/lib/instances/exec_test.go @@ -35,6 +35,7 @@ func waitForExecAgent(ctx context.Context, mgr *manager, instanceID string, time // TestExecConcurrent tests concurrent exec commands from multiple goroutines. // This validates that the exec infrastructure handles concurrent access correctly. func TestExecConcurrent(t *testing.T) { + skipIfNoDockerHub(t) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available") } diff --git a/lib/instances/manager_test.go b/lib/instances/manager_test.go index ccd89adc..58294830 100644 --- a/lib/instances/manager_test.go +++ b/lib/instances/manager_test.go @@ -31,6 +31,14 @@ import ( "github.com/stretchr/testify/require" ) +// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. +func skipIfNoDockerHub(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { + t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") + } +} + // setupTestManager creates a manager and registers cleanup for any orphaned processes func setupTestManager(t *testing.T) (*manager, string) { tmpDir := t.TempDir() @@ -174,6 +182,7 @@ func cleanupOrphanedProcesses(t *testing.T, mgr *manager) { } func TestBasicEndToEnd(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access (don't skip, fail informatively) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -825,6 +834,7 @@ func TestStorageOperations(t *testing.T) { } func TestStandbyAndRestore(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access (don't skip, fail informatively) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/lib/instances/network_test.go b/lib/instances/network_test.go index 0ad25494..39271746 100644 --- a/lib/instances/network_test.go +++ b/lib/instances/network_test.go @@ -19,6 +19,7 @@ import ( // TestCreateInstanceWithNetwork tests instance creation with network allocation // and verifies network connectivity persists after standby/restore func TestCreateInstanceWithNetwork(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access requireKVMAccess(t) diff --git a/lib/instances/qemu_test.go b/lib/instances/qemu_test.go index 31e3045e..cabc6fa6 100644 --- a/lib/instances/qemu_test.go +++ b/lib/instances/qemu_test.go @@ -161,6 +161,7 @@ func (r *qemuInstanceResolver) ResolveInstance(ctx context.Context, nameOrID str // It tests: create, get, list, logs, network, ingress, volumes, exec, and delete. // It does NOT test: snapshot/standby, hot memory resize (not supported by QEMU in first pass). func TestQEMUBasicEndToEnd(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -540,6 +541,7 @@ func TestQEMUBasicEndToEnd(t *testing.T) { // TestQEMUStandbyAndRestore tests the standby/restore cycle with QEMU. // This tests QEMU's migrate-to-file snapshot mechanism. func TestQEMUStandbyAndRestore(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/lib/instances/resource_limits_test.go b/lib/instances/resource_limits_test.go index 1895d87e..50287709 100644 --- a/lib/instances/resource_limits_test.go +++ b/lib/instances/resource_limits_test.go @@ -227,6 +227,7 @@ func TestAggregateUsage_StructValues(t *testing.T) { // aggregate resource limits are enforced when creating VMs. // It creates one VM, then tries to create another that would exceed the total limit. func TestAggregateLimits_EnforcedAtRuntime(t *testing.T) { + skipIfNoDockerHub(t) // Skip in short mode - this is an integration test if testing.Short() { t.Skip("skipping integration test in short mode") diff --git a/lib/instances/volumes_test.go b/lib/instances/volumes_test.go index 6be9e24f..05fe5ea7 100644 --- a/lib/instances/volumes_test.go +++ b/lib/instances/volumes_test.go @@ -40,6 +40,7 @@ func execWithRetry(ctx context.Context, inst *Instance, command []string) (strin // 3. Attached read-only to multiple instances simultaneously // 4. Data persists and is readable from all instances func TestVolumeMultiAttachReadOnly(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group") @@ -222,6 +223,7 @@ func TestVolumeMultiAttachReadOnly(t *testing.T) { // TestOverlayDiskCleanupOnDelete verifies that vol-overlays/ directory is removed // when an instance with overlay volumes is deleted. func TestOverlayDiskCleanupOnDelete(t *testing.T) { + skipIfNoDockerHub(t) // Skip in short mode - this is an integration test if testing.Short() { t.Skip("skipping integration test in short mode") @@ -332,6 +334,7 @@ func createTestTarGz(t *testing.T, files map[string][]byte) *bytes.Buffer { // TestVolumeFromArchive tests that a volume can be created from a tar.gz archive // and the files are accessible when mounted to an instance func TestVolumeFromArchive(t *testing.T) { + skipIfNoDockerHub(t) // Require KVM if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group") diff --git a/lib/system/manager_test.go b/lib/system/manager_test.go index ea91ae6b..fa5a929f 100644 --- a/lib/system/manager_test.go +++ b/lib/system/manager_test.go @@ -2,6 +2,7 @@ package system import ( "context" + "os" "testing" "github.com/onkernel/hypeman/lib/paths" @@ -9,6 +10,14 @@ import ( "github.com/stretchr/testify/require" ) +// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. +func skipIfNoDockerHub(t *testing.T) { + t.Helper() + if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { + t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") + } +} + func TestGetDefaultKernelVersion(t *testing.T) { tmpDir := t.TempDir() mgr := NewManager(paths.New(tmpDir)) @@ -29,6 +38,7 @@ func TestGetKernelPath(t *testing.T) { } func TestEnsureSystemFiles(t *testing.T) { + skipIfNoDockerHub(t) // This test requires network access and takes a while // Skip by default, run explicitly with: go test -run TestEnsureSystemFiles if testing.Short() { From 1967d4938841757f37dbe1e472aeb4d637b96ed6 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 21:08:10 -0500 Subject: [PATCH 38/42] Revert "test: add SKIP_DOCKER_HUB_TESTS env var to skip rate-limited tests" This reverts commit f09e53d98df8c83c01249996dce23dbbc18ab71e. --- cmd/api/api/cp_test.go | 2 -- cmd/api/api/exec_test.go | 2 -- cmd/api/api/images_test.go | 3 --- cmd/api/api/instances_test.go | 2 -- cmd/api/api/registry_test.go | 16 ---------------- integration/systemd_test.go | 9 --------- lib/images/manager_test.go | 15 --------------- lib/instances/exec_test.go | 1 - lib/instances/manager_test.go | 10 ---------- lib/instances/network_test.go | 1 - lib/instances/qemu_test.go | 2 -- lib/instances/resource_limits_test.go | 1 - lib/instances/volumes_test.go | 3 --- lib/system/manager_test.go | 10 ---------- 14 files changed, 77 deletions(-) diff --git a/cmd/api/api/cp_test.go b/cmd/api/api/cp_test.go index 75cb4af5..98acf5eb 100644 --- a/cmd/api/api/cp_test.go +++ b/cmd/api/api/cp_test.go @@ -17,7 +17,6 @@ import ( ) func TestCpToAndFromInstance(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -157,7 +156,6 @@ func TestCpToAndFromInstance(t *testing.T) { } func TestCpDirectoryToInstance(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/cmd/api/api/exec_test.go b/cmd/api/api/exec_test.go index cb3faea0..ef474a04 100644 --- a/cmd/api/api/exec_test.go +++ b/cmd/api/api/exec_test.go @@ -18,7 +18,6 @@ import ( ) func TestExecInstanceNonTTY(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -159,7 +158,6 @@ func TestExecInstanceNonTTY(t *testing.T) { // 2. guest-agent must keep running even after the main app exits // 3. The VM must not kernel panic when the entrypoint exits func TestExecWithDebianMinimal(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/cmd/api/api/images_test.go b/cmd/api/api/images_test.go index 0a9b0e0c..9d5f0590 100644 --- a/cmd/api/api/images_test.go +++ b/cmd/api/api/images_test.go @@ -33,7 +33,6 @@ func TestGetImage_NotFound(t *testing.T) { } func TestCreateImage_Async(t *testing.T) { - skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() @@ -126,7 +125,6 @@ func TestCreateImage_Async(t *testing.T) { } func TestCreateImage_InvalidTag(t *testing.T) { - skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() @@ -173,7 +171,6 @@ func TestCreateImage_InvalidName(t *testing.T) { } func TestCreateImage_Idempotent(t *testing.T) { - skipIfNoDockerHub(t) svc := newTestService(t) ctx := ctx() diff --git a/cmd/api/api/instances_test.go b/cmd/api/api/instances_test.go index 1560dcac..ffe45a0d 100644 --- a/cmd/api/api/instances_test.go +++ b/cmd/api/api/instances_test.go @@ -33,7 +33,6 @@ func TestGetInstance_NotFound(t *testing.T) { } func TestCreateInstance_ParsesHumanReadableSizes(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -130,7 +129,6 @@ func TestCreateInstance_InvalidSizeFormat(t *testing.T) { } func TestInstanceLifecycle_StopStart(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access for VM creation if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Skip("/dev/kvm not available - skipping lifecycle test") diff --git a/cmd/api/api/registry_test.go b/cmd/api/api/registry_test.go index 7969ef62..e88978ef 100644 --- a/cmd/api/api/registry_test.go +++ b/cmd/api/api/registry_test.go @@ -25,16 +25,6 @@ import ( "github.com/stretchr/testify/require" ) -// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. -// This is used to skip tests that require pulling from Docker Hub in CI -// environments where rate limiting may be an issue. -func skipIfNoDockerHub(t *testing.T) { - t.Helper() - if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { - t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") - } -} - // setupRegistryTest creates a test service with a mounted OCI registry server. // Returns the service (for API calls) and the server host (for building push URLs). func setupRegistryTest(t *testing.T) (*ApiService, string) { @@ -57,7 +47,6 @@ func setupRegistryTest(t *testing.T) (*ApiService, string) { } func TestRegistryPushAndConvert(t *testing.T) { - skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull a small image from Docker Hub to push to our registry @@ -103,7 +92,6 @@ func TestRegistryVersionCheck(t *testing.T) { } func TestRegistryPushAndCreateInstance(t *testing.T) { - skipIfNoDockerHub(t) // This is a full e2e test that requires KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Skip("/dev/kvm not available - skipping VM creation test") @@ -186,7 +174,6 @@ func TestRegistryPushAndCreateInstance(t *testing.T) { // TestRegistryLayerCaching verifies that pushing the same image twice // reuses cached layers and doesn't re-upload them. func TestRegistryLayerCaching(t *testing.T) { - skipIfNoDockerHub(t) _, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub @@ -270,7 +257,6 @@ func TestRegistryLayerCaching(t *testing.T) { // TestRegistrySharedLayerCaching verifies that pushing different images // that share layers reuses the cached shared layers. func TestRegistrySharedLayerCaching(t *testing.T) { - skipIfNoDockerHub(t) _, serverHost := setupRegistryTest(t) // Pull alpine image (this will be our base) @@ -352,7 +338,6 @@ func TestRegistrySharedLayerCaching(t *testing.T) { // TestRegistryTagPush verifies that pushing with a tag reference (not digest) // correctly triggers conversion. The server computes the digest from the manifest. func TestRegistryTagPush(t *testing.T) { - skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub @@ -406,7 +391,6 @@ func TestRegistryTagPush(t *testing.T) { // Docker v2 manifest (as returned by local Docker daemon) is correctly converted // to OCI format and the image conversion succeeds. func TestRegistryDockerV2ManifestConversion(t *testing.T) { - skipIfNoDockerHub(t) svc, serverHost := setupRegistryTest(t) // Pull alpine image from Docker Hub (OCI format) diff --git a/integration/systemd_test.go b/integration/systemd_test.go index 1991642b..16ba8638 100644 --- a/integration/systemd_test.go +++ b/integration/systemd_test.go @@ -22,14 +22,6 @@ import ( "github.com/stretchr/testify/require" ) -// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. -func skipIfNoDockerHub(t *testing.T) { - t.Helper() - if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { - t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") - } -} - // TestSystemdMode verifies that hypeman correctly detects and runs // systemd-based images with systemd as PID 1. // @@ -39,7 +31,6 @@ func skipIfNoDockerHub(t *testing.T) { // - Starts systemd as PID 1 // - Injects and starts the hypeman-agent.service func TestSystemdMode(t *testing.T) { - skipIfNoDockerHub(t) if testing.Short() { t.Skip("skipping integration test in short mode") } diff --git a/lib/images/manager_test.go b/lib/images/manager_test.go index d0d62c05..406566ef 100644 --- a/lib/images/manager_test.go +++ b/lib/images/manager_test.go @@ -12,16 +12,7 @@ import ( "github.com/stretchr/testify/require" ) -// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. -func skipIfNoDockerHub(t *testing.T) { - t.Helper() - if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { - t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") - } -} - func TestCreateImage(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -92,7 +83,6 @@ func TestCreateImage(t *testing.T) { } func TestCreateImageDifferentTag(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -115,7 +105,6 @@ func TestCreateImageDifferentTag(t *testing.T) { } func TestCreateImageDuplicate(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -145,7 +134,6 @@ func TestCreateImageDuplicate(t *testing.T) { } func TestListImages(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -175,7 +163,6 @@ func TestListImages(t *testing.T) { } func TestGetImage(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -211,7 +198,6 @@ func TestGetImageNotFound(t *testing.T) { } func TestDeleteImage(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) @@ -284,7 +270,6 @@ func TestNormalizedRefParsing(t *testing.T) { } func TestLayerCaching(t *testing.T) { - skipIfNoDockerHub(t) dataDir := t.TempDir() mgr, err := NewManager(paths.New(dataDir), 1, nil) require.NoError(t, err) diff --git a/lib/instances/exec_test.go b/lib/instances/exec_test.go index d8ccbcd7..1efb471e 100644 --- a/lib/instances/exec_test.go +++ b/lib/instances/exec_test.go @@ -35,7 +35,6 @@ func waitForExecAgent(ctx context.Context, mgr *manager, instanceID string, time // TestExecConcurrent tests concurrent exec commands from multiple goroutines. // This validates that the exec infrastructure handles concurrent access correctly. func TestExecConcurrent(t *testing.T) { - skipIfNoDockerHub(t) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available") } diff --git a/lib/instances/manager_test.go b/lib/instances/manager_test.go index 58294830..ccd89adc 100644 --- a/lib/instances/manager_test.go +++ b/lib/instances/manager_test.go @@ -31,14 +31,6 @@ import ( "github.com/stretchr/testify/require" ) -// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. -func skipIfNoDockerHub(t *testing.T) { - t.Helper() - if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { - t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") - } -} - // setupTestManager creates a manager and registers cleanup for any orphaned processes func setupTestManager(t *testing.T) (*manager, string) { tmpDir := t.TempDir() @@ -182,7 +174,6 @@ func cleanupOrphanedProcesses(t *testing.T, mgr *manager) { } func TestBasicEndToEnd(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access (don't skip, fail informatively) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -834,7 +825,6 @@ func TestStorageOperations(t *testing.T) { } func TestStandbyAndRestore(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access (don't skip, fail informatively) if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/lib/instances/network_test.go b/lib/instances/network_test.go index 39271746..0ad25494 100644 --- a/lib/instances/network_test.go +++ b/lib/instances/network_test.go @@ -19,7 +19,6 @@ import ( // TestCreateInstanceWithNetwork tests instance creation with network allocation // and verifies network connectivity persists after standby/restore func TestCreateInstanceWithNetwork(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access requireKVMAccess(t) diff --git a/lib/instances/qemu_test.go b/lib/instances/qemu_test.go index cabc6fa6..31e3045e 100644 --- a/lib/instances/qemu_test.go +++ b/lib/instances/qemu_test.go @@ -161,7 +161,6 @@ func (r *qemuInstanceResolver) ResolveInstance(ctx context.Context, nameOrID str // It tests: create, get, list, logs, network, ingress, volumes, exec, and delete. // It does NOT test: snapshot/standby, hot memory resize (not supported by QEMU in first pass). func TestQEMUBasicEndToEnd(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") @@ -541,7 +540,6 @@ func TestQEMUBasicEndToEnd(t *testing.T) { // TestQEMUStandbyAndRestore tests the standby/restore cycle with QEMU. // This tests QEMU's migrate-to-file snapshot mechanism. func TestQEMUStandbyAndRestore(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM access if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group (sudo usermod -aG kvm $USER)") diff --git a/lib/instances/resource_limits_test.go b/lib/instances/resource_limits_test.go index 50287709..1895d87e 100644 --- a/lib/instances/resource_limits_test.go +++ b/lib/instances/resource_limits_test.go @@ -227,7 +227,6 @@ func TestAggregateUsage_StructValues(t *testing.T) { // aggregate resource limits are enforced when creating VMs. // It creates one VM, then tries to create another that would exceed the total limit. func TestAggregateLimits_EnforcedAtRuntime(t *testing.T) { - skipIfNoDockerHub(t) // Skip in short mode - this is an integration test if testing.Short() { t.Skip("skipping integration test in short mode") diff --git a/lib/instances/volumes_test.go b/lib/instances/volumes_test.go index 05fe5ea7..6be9e24f 100644 --- a/lib/instances/volumes_test.go +++ b/lib/instances/volumes_test.go @@ -40,7 +40,6 @@ func execWithRetry(ctx context.Context, inst *Instance, command []string) (strin // 3. Attached read-only to multiple instances simultaneously // 4. Data persists and is readable from all instances func TestVolumeMultiAttachReadOnly(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group") @@ -223,7 +222,6 @@ func TestVolumeMultiAttachReadOnly(t *testing.T) { // TestOverlayDiskCleanupOnDelete verifies that vol-overlays/ directory is removed // when an instance with overlay volumes is deleted. func TestOverlayDiskCleanupOnDelete(t *testing.T) { - skipIfNoDockerHub(t) // Skip in short mode - this is an integration test if testing.Short() { t.Skip("skipping integration test in short mode") @@ -334,7 +332,6 @@ func createTestTarGz(t *testing.T, files map[string][]byte) *bytes.Buffer { // TestVolumeFromArchive tests that a volume can be created from a tar.gz archive // and the files are accessible when mounted to an instance func TestVolumeFromArchive(t *testing.T) { - skipIfNoDockerHub(t) // Require KVM if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) { t.Fatal("/dev/kvm not available - ensure KVM is enabled and user is in 'kvm' group") diff --git a/lib/system/manager_test.go b/lib/system/manager_test.go index fa5a929f..ea91ae6b 100644 --- a/lib/system/manager_test.go +++ b/lib/system/manager_test.go @@ -2,7 +2,6 @@ package system import ( "context" - "os" "testing" "github.com/onkernel/hypeman/lib/paths" @@ -10,14 +9,6 @@ import ( "github.com/stretchr/testify/require" ) -// skipIfNoDockerHub skips the test if SKIP_DOCKER_HUB_TESTS is set. -func skipIfNoDockerHub(t *testing.T) { - t.Helper() - if os.Getenv("SKIP_DOCKER_HUB_TESTS") != "" { - t.Skip("Skipping test that requires Docker Hub (SKIP_DOCKER_HUB_TESTS is set)") - } -} - func TestGetDefaultKernelVersion(t *testing.T) { tmpDir := t.TempDir() mgr := NewManager(paths.New(tmpDir)) @@ -38,7 +29,6 @@ func TestGetKernelPath(t *testing.T) { } func TestEnsureSystemFiles(t *testing.T) { - skipIfNoDockerHub(t) // This test requires network access and takes a while // Skip by default, run explicitly with: go test -run TestEnsureSystemFiles if testing.Short() { From 520cd0d21ef071728969f0617e4a2bf004034ec5 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 21:11:58 -0500 Subject: [PATCH 39/42] fix: address cursor bot review comments from PR #53 - Fix closure capturing loop variable in recoverPendingBuilds (add meta := meta shadow) - Fix premature loop exit in StreamBuildEvents when receiving non-terminal status events - Fix race condition where cancelled builds could be overwritten to 'failed' status - Fix nil panic when secretProvider is not configured (use NoOpSecretProvider fallback) --- lib/builds/manager.go | 51 +++++++++++++++++++++++--------------- lib/providers/providers.go | 4 ++- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 82cbee4c..49aac6e0 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -466,30 +466,30 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( // Handle messages from agent until we get the build result for { - // Use a goroutine for decoding so we can respect context cancellation. - type decodeResult struct { - response VsockMessage - err error - } - resultCh := make(chan decodeResult, 1) + // Use a goroutine for decoding so we can respect context cancellation. + type decodeResult struct { + response VsockMessage + err error + } + resultCh := make(chan decodeResult, 1) - go func() { - var response VsockMessage - err := decoder.Decode(&response) - resultCh <- decodeResult{response: response, err: err} - }() + go func() { + var response VsockMessage + err := decoder.Decode(&response) + resultCh <- decodeResult{response: response, err: err} + }() // Wait for either a message or context cancellation var dr decodeResult - select { - case <-ctx.Done(): - conn.Close() - <-resultCh - return nil, ctx.Err() + select { + case <-ctx.Done(): + conn.Close() + <-resultCh + return nil, ctx.Err() case dr = <-resultCh: - if dr.err != nil { + if dr.err != nil { return nil, fmt.Errorf("read message: %w", dr.err) - } + } } // Handle message based on type @@ -516,8 +516,8 @@ func (m *manager) waitForResult(ctx context.Context, inst *instances.Instance) ( // Build completed if dr.response.Result == nil { return nil, fmt.Errorf("received build_result with nil result") - } - return dr.response.Result, nil + } + return dr.response.Result, nil default: m.logger.Warn("unexpected message type from agent", "type", dr.response.Type) @@ -615,6 +615,14 @@ func (m *manager) updateBuildComplete(id string, status string, digest *string, return } + // Don't overwrite terminal states - this prevents race conditions where + // a cancelled build's runBuild goroutine later fails and tries to set "failed" + if meta.Status == StatusCancelled || meta.Status == StatusReady || meta.Status == StatusFailed { + m.logger.Debug("skipping status update for already-terminal build", + "id", id, "current_status", meta.Status, "attempted_status", status) + return + } + meta.Status = status meta.ImageDigest = digest meta.Error = errMsg @@ -804,6 +812,8 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) if event.Status == StatusReady || event.Status == StatusFailed || event.Status == StatusCancelled { return } + // Non-terminal status event - keep waiting for log file + continue case <-time.After(500 * time.Millisecond): if _, err := os.Stat(logPath); err == nil { break // Log file appeared @@ -921,6 +931,7 @@ func (m *manager) RecoverPendingBuilds() { } for _, meta := range pending { + meta := meta // Shadow loop variable for closure capture m.logger.Info("recovering build", "id", meta.ID, "status", meta.Status) // Re-enqueue the build diff --git a/lib/providers/providers.go b/lib/providers/providers.go index bede8650..c2a46f7a 100644 --- a/lib/providers/providers.go +++ b/lib/providers/providers.go @@ -238,11 +238,13 @@ func ProvideBuildManager(p *paths.Paths, cfg *config.Config, instanceManager ins buildConfig.DefaultTimeout = 600 } - // Configure secret provider + // Configure secret provider (use NoOpSecretProvider as fallback to avoid nil panics) var secretProvider builds.SecretProvider if cfg.BuildSecretsDir != "" { secretProvider = builds.NewFileSecretProvider(cfg.BuildSecretsDir) log.Info("build secrets enabled", "dir", cfg.BuildSecretsDir) + } else { + secretProvider = &builds.NoOpSecretProvider{} } meter := otel.GetMeterProvider().Meter("hypeman") From 2bded30ec6c99a2805917c0fbfc9eeb208c0e492 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 21:21:04 -0500 Subject: [PATCH 40/42] fix: make TestCreateImage_Idempotent resilient to timing variations The second CreateImage call can return either 'pending' (still processing) or 'ready' (already completed) depending on CI speed and caching. The key idempotency invariant is that the digest is the same, not the status. --- cmd/api/api/images_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/api/api/images_test.go b/cmd/api/api/images_test.go index 9d5f0590..fc2e0180 100644 --- a/cmd/api/api/images_test.go +++ b/cmd/api/api/images_test.go @@ -225,9 +225,17 @@ func TestCreateImage_Idempotent(t *testing.T) { t.Fatal("Build failed - this is the root cause of test failures") } - require.Equal(t, oapi.ImageStatus(images.StatusPending), img2.Status) - require.NotNil(t, img2.QueuePosition, "should have queue position") - require.Equal(t, 1, *img2.QueuePosition, "should still be at position 1") + // Status can be "pending" (still processing) or "ready" (already completed in fast CI) + // The key idempotency invariant is that the digest is the same (verified above) + require.Contains(t, []oapi.ImageStatus{ + oapi.ImageStatus(images.StatusPending), + oapi.ImageStatus(images.StatusReady), + }, img2.Status, "status should be pending or ready") + + // If still pending, should have queue position + if img2.Status == oapi.ImageStatus(images.StatusPending) { + require.NotNil(t, img2.QueuePosition, "should have queue position when pending") + } // Construct digest reference: repository@digest // Extract repository from imageName (strip tag part) From 8c803c1277d43bb0db7da1edc3278c136574dd71 Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 21:31:13 -0500 Subject: [PATCH 41/42] moar --- lib/builds/builder_agent/main.go | 21 ++++++++++++--- lib/builds/cache.go | 18 ++++++++----- lib/builds/cache_test.go | 45 ++++++++++++++++++++++++++++++-- lib/middleware/oapi_auth.go | 4 +-- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/lib/builds/builder_agent/main.go b/lib/builds/builder_agent/main.go index 9875f3a7..102fa877 100644 --- a/lib/builds/builder_agent/main.go +++ b/lib/builds/builder_agent/main.go @@ -95,6 +95,10 @@ var ( buildConfigLock sync.Mutex secretsReady = make(chan struct{}) secretsOnce sync.Once + + // Encoder lock protects concurrent access to json.Encoder + // (the goroutine sending build_result and the main loop handling get_status) + encoderLock sync.Mutex ) func main() { @@ -213,7 +217,10 @@ func handleHostConnection(conn net.Conn) { buildResultLock.Unlock() log.Printf("Build completed, sending result to host") - if err := encoder.Encode(VsockMessage{Type: "build_result", Result: result}); err != nil { + encoderLock.Lock() + err := encoder.Encode(VsockMessage{Type: "build_result", Result: result}) + encoderLock.Unlock() + if err != nil { log.Printf("Failed to send build result: %v", err) } }() @@ -231,19 +238,24 @@ func handleHostConnection(conn net.Conn) { Type: "build_result", Result: result, } - if err := encoder.Encode(response); err != nil { + encoderLock.Lock() + err := encoder.Encode(response) + encoderLock.Unlock() + if err != nil { log.Printf("Failed to send result: %v", err) } return // Close connection after sending result case "get_status": // Host is checking if build is still running + encoderLock.Lock() select { case <-buildDone: encoder.Encode(VsockMessage{Type: "status", Log: "completed"}) default: encoder.Encode(VsockMessage{Type: "status", Log: "building"}) } + encoderLock.Unlock() default: log.Printf("Unknown message type: %s", msg.Type) @@ -288,7 +300,10 @@ func handleSecretsRequest(encoder *json.Encoder, decoder *json.Decoder) error { Type: "get_secrets", SecretIDs: secretIDs, } - if err := encoder.Encode(req); err != nil { + encoderLock.Lock() + err := encoder.Encode(req) + encoderLock.Unlock() + if err != nil { return fmt.Errorf("send get_secrets: %w", err) } diff --git a/lib/builds/cache.go b/lib/builds/cache.go index e4842838..ff3e26a8 100644 --- a/lib/builds/cache.go +++ b/lib/builds/cache.go @@ -24,15 +24,16 @@ type CacheKey struct { Reference string // Components - TenantScope string - Runtime string + TenantScope string + Runtime string LockfileHash string } // GenerateCacheKey generates a cache key for a build. // // Cache key structure: -// {registry}/cache/{tenant_scope}/{runtime}/{lockfile_hash} +// +// {registry}/cache/{tenant_scope}/{runtime}/{lockfile_hash} // // This structure provides: // - Tenant isolation: each tenant's cache is isolated by scope @@ -123,14 +124,18 @@ func normalizeCacheScope(scope string) string { return normalized } -// computeCombinedHash computes a combined hash from multiple lockfile hashes +// computeCombinedHash computes a combined hash from multiple lockfile hashes. +// Returns a 64-character hex string (sha256), even for empty input. func computeCombinedHash(lockfileHashes map[string]string) string { + h := sha256.New() + if len(lockfileHashes) == 0 { - return "empty" + // Hash "empty" to get a consistent 64-char hex string + h.Write([]byte("empty")) + return hex.EncodeToString(h.Sum(nil)) } // Sort keys for determinism - h := sha256.New() for _, name := range sortedKeys(lockfileHashes) { h.Write([]byte(name)) h.Write([]byte(":")) @@ -172,4 +177,3 @@ func GetCacheKeyFromConfig(registryURL, cacheScope, runtime string, lockfileHash return key.ImportCacheArg(), key.ExportCacheArg(), nil } - diff --git a/lib/builds/cache_test.go b/lib/builds/cache_test.go index 8ca4340a..d51fb7cf 100644 --- a/lib/builds/cache_test.go +++ b/lib/builds/cache_test.go @@ -60,6 +60,20 @@ func TestCacheKeyGenerator_GenerateCacheKey(t *testing.T) { }, wantPrefix: "localhost:8080/cache/my-team/nodejs/", }, + { + name: "empty lockfileHashes does not panic", + tenantScope: "tenant-abc", + runtime: "nodejs", + lockfileHashes: map[string]string{}, + wantPrefix: "localhost:8080/cache/tenant-abc/nodejs/", + }, + { + name: "nil lockfileHashes does not panic", + tenantScope: "tenant-abc", + runtime: "python", + lockfileHashes: nil, + wantPrefix: "localhost:8080/cache/tenant-abc/python/", + }, } for _, tt := range tests { @@ -159,9 +173,14 @@ func TestComputeCombinedHash(t *testing.T) { }) assert.NotEqual(t, hash1, hash3) - // Empty map should return "empty" + // Empty map should return a valid hash (64 hex chars), not a short string emptyHash := computeCombinedHash(map[string]string{}) - assert.Equal(t, "empty", emptyHash) + assert.Len(t, emptyHash, 64, "empty hash should be 64 hex characters (sha256)") + + // Nil map should also return a valid hash + nilHash := computeCombinedHash(nil) + assert.Len(t, nilHash, 64, "nil hash should be 64 hex characters (sha256)") + assert.Equal(t, emptyHash, nilHash, "empty and nil should produce same hash") } func TestGetCacheKeyFromConfig(t *testing.T) { @@ -188,4 +207,26 @@ func TestGetCacheKeyFromConfig(t *testing.T) { require.NoError(t, err) assert.Empty(t, importArg) assert.Empty(t, exportArg) + + // With cache scope but empty lockfileHashes - should not panic (regression test) + importArg, exportArg, err = GetCacheKeyFromConfig( + "localhost:8080", + "my-tenant", + "nodejs", + map[string]string{}, // Empty lockfileHashes + ) + require.NoError(t, err) + assert.NotEmpty(t, importArg, "should generate cache args even with empty lockfileHashes") + assert.NotEmpty(t, exportArg) + + // With cache scope but nil lockfileHashes - should not panic (regression test) + importArg, exportArg, err = GetCacheKeyFromConfig( + "localhost:8080", + "my-tenant", + "python", + nil, // nil lockfileHashes + ) + require.NoError(t, err) + assert.NotEmpty(t, importArg, "should generate cache args even with nil lockfileHashes") + assert.NotEmpty(t, exportArg) } diff --git a/lib/middleware/oapi_auth.go b/lib/middleware/oapi_auth.go index 2c79714e..430e822a 100644 --- a/lib/middleware/oapi_auth.go +++ b/lib/middleware/oapi_auth.go @@ -191,8 +191,8 @@ func isInternalVMRequest(r *http.Request) bool { ip := r.RemoteAddr // RemoteAddr is "IP:port" format, extract just the IP - if idx := strings.LastIndex(ip, ":"); idx != -1 { - ip = ip[:idx] + if idx := strings.LastIndex(ip, ":"); idx != -1 { + ip = ip[:idx] } // Check if it's from the VM network (10.102.x.x) From 528cf8dba85f064390fee1d6dc761bef09b335ad Mon Sep 17 00:00:00 2001 From: Hiro Tamada Date: Thu, 8 Jan 2026 22:21:43 -0500 Subject: [PATCH 42/42] moar --- lib/builds/manager.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/builds/manager.go b/lib/builds/manager.go index 49aac6e0..1bfc40b1 100644 --- a/lib/builds/manager.go +++ b/lib/builds/manager.go @@ -843,6 +843,14 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) return } + // Ensure tail process is cleaned up on all exit paths to avoid zombie processes. + // Kill() is safe to call even if the process has already exited. + // Wait() reaps the process to prevent zombies. + defer func() { + cmd.Process.Kill() + cmd.Wait() + }() + // Goroutine to read log lines logLines := make(chan string, 100) go func() { @@ -865,13 +873,11 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) for { select { case <-ctx.Done(): - cmd.Process.Kill() return case line, ok := <-logLines: if !ok { - // Log stream ended - wait for tail to exit - cmd.Wait() + // Log stream ended return } event := BuildEvent{ @@ -882,7 +888,6 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) select { case out <- event: case <-ctx.Done(): - cmd.Process.Kill() return } @@ -890,14 +895,12 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) select { case out <- event: case <-ctx.Done(): - cmd.Process.Kill() return } // Check if build completed if event.Status == StatusReady || event.Status == StatusFailed || event.Status == StatusCancelled { // Give a moment for final logs to come through time.Sleep(100 * time.Millisecond) - cmd.Process.Kill() return } @@ -912,7 +915,6 @@ func (m *manager) StreamBuildEvents(ctx context.Context, id string, follow bool) select { case out <- event: case <-ctx.Done(): - cmd.Process.Kill() return } }