Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

5.5 KB
Raw Blame
// Copyright 2026 BoxLite AI
// SPDX-License-Identifier: AGPL-3.0

package proxy

import (
	"context"
	"fmt"
	"log/slog"
	"net"
	"net/http"
	"os"
	"time"

	apiclient "github.com/boxlite-ai/boxlite/libs/api-client-go"
	"github.com/boxlite-ai/image-service/cmd/registry-proxy/config"
	"github.com/boxlite-ai/image-service/internal"
	"github.com/boxlite-ai/image-service/internal/oci"
	"github.com/boxlite-ai/image-service/internal/secrets"
	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

// ServiceName names this process to the telemetry pipeline and to the traces
// it emits.
const ServiceName = "boxlite-registry-proxy"

// HealthPath is what the platform probes to decide the process is up. It is
// deliberately outside /v2/, which belongs to the distribution protocol.
const HealthPath = "/health"

// readHeaderTimeout bounds how long a connection may hold the server without
// stating what it wants. The response has no such bound here: a blob is
// arbitrarily large, so how long it may take is left to the caller and, when
// deployed, to the platform's request timeout.
const readHeaderTimeout = 10 * time.Second

// Start runs the registry proxy until ctx is cancelled, then drains in-flight
// pulls within the configured shutdown timeout.
func Start(ctx context.Context, cfg *config.Config, api *apiclient.APIClient) error {
	// Before the listener, so a store that cannot be opened is a failure to
	// start rather than a failure on the first private pull.
	reader, err := secrets.NewReader(ctx, cfg.SecretStore, cfg.SecretDir)
	if err != nil {
		return fmt.Errorf("open the secret store: %w", err)
	}

	address := net.JoinHostPort(os.Getenv("BOXLITE_LISTEN_HOST"), fmt.Sprint(cfg.Port))
	listener, err := net.Listen("tcp", address)
	if err != nil {
		return fmt.Errorf("listen on %s: %w", address, err)
	}
	slog.Info("Registry proxy is running", "port", cfg.Port, "version", internal.Version)

	return serve(ctx, listener, NewRouter(cfg, api, reader), time.Duration(cfg.ShutdownTimeoutSec)*time.Second)
}

// serve answers on listener until ctx is cancelled, then stops accepting and
// gives the pulls already in flight up to drainTimeout to finish.
//
// Draining rather than closing is the whole point: a blob is one long response,
// so cutting the listener at shutdown truncates an image mid-layer and the
// puller sees a corrupt digest rather than a retryable failure.
func serve(ctx context.Context, listener net.Listener, handler http.Handler, drainTimeout time.Duration) error {
	server := &http.Server{Handler: handler, ReadHeaderTimeout: readHeaderTimeout, Protocols: acceptedProtocols()}

	serveErr := make(chan error, 1)
	go func() { serveErr <- server.Serve(listener) }()

	select {
	case err := <-serveErr:
		return err
	case <-ctx.Done():
		slog.Info("Draining in-flight pulls", "timeout", drainTimeout)
		drainCtx, cancel := context.WithTimeout(context.Background(), drainTimeout)
		defer cancel()
		return server.Shutdown(drainCtx)
	}
}

// acceptedProtocols is HTTP/1 and HTTP/2 in the clear, on one port.
//
// HTTP/2 because Cloud Run caps an HTTP/1 response at 32 MiB unless it is
// chunked, and a blob relayed with the upstream's Content-Length is not: a
// layer past that size would be cut off in production while every test here
// passed. The cap is on HTTP/1 alone, and Cloud Run speaks HTTP/2 to a
// container that accepts it without TLS — its own frontend terminates TLS.
//
// HTTP/1 as well, on the same port, because that is what a developer's curl
// and the local stack speak. Dropping the Content-Length instead would have
// forced chunking, but it would also have altered the headers this proxy
// promises to relay untouched.
func acceptedProtocols() *http.Protocols {
	protocols := new(http.Protocols)
	protocols.SetHTTP1(true)
	protocols.SetUnencryptedHTTP2(true)
	return protocols
}

// NewRouter builds the registry proxy's HTTP surface. It is separate from Start
// so the surface can be exercised without binding a port. A nil reader
// permits only allowlisted anonymous pulls.
func NewRouter(cfg *config.Config, api *apiclient.APIClient, reader secrets.Reader) *gin.Engine {
	gin.SetMode(gin.ReleaseMode)

	router := gin.New()
	router.Use(gin.Recovery())
	if cfg.OtelTracingEnabled && cfg.OtelEndpoint != "" {
		router.Use(otelgin.Middleware(ServiceName))
	}

	router.GET(HealthPath, func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"status": "ok", "version": internal.Version})
	})

	// One client for pulls, redirects and token exchanges alike: they share the
	// address rule and the connection pool, and a token endpoint is as much an
	// upstream as the registry that named it.
	upstream := oci.NewClient(newUpstreamClient(routable, cfg.UpstreamTimeout))
	proxy := &registryProxy{
		upstream:  upstream,
		runners:   newRunnerAuthenticator(api, cfg.CredentialTTL, cfg.RejectionTTL),
		allowlist: newUpstreamAllowlist(cfg.UpstreamHosts),
		limits:    newPullLimiter(cfg.PullsPerSecond, cfg.PullBurst, cfg.TrackedMeters),
		tokens:    newTokenBroker(upstream),
		// The same lifetime as a verified runner: both are answers the control
		// plane gave, and both are how long a change there takes to reach here.
		credentials: newCredentialFinder(api, reader, cfg.CredentialTTL),
	}
	// GET and HEAD share one handler: the distribution protocol answers both on
	// the manifest endpoint, and a HEAD is a GET whose body nobody reads.
	router.GET(oci.PathPrefix+"*path", proxy.handle)
	router.HEAD(oci.PathPrefix+"*path", proxy.handle)
	return router
}