The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
// Copyright 2026 BoxLite AI
// SPDX-License-Identifier: AGPL-3.0
package proxy
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
"time"
"github.com/boxlite-ai/image-service/internal/oci"
"github.com/gin-gonic/gin"
)
// Realm names this proxy in the challenge it answers an unauthenticated caller
// with. The value is not a secret and not a URL; it is what a client shows a
// human when it asks which credential to use.
const Realm = "boxlite-registry-proxy"
// registryProxy serves the pull half of the distribution protocol on behalf of
// callers that do not hold the upstream's credentials.
type registryProxy struct {
upstream *oci.Client
runners *runnerAuthenticator
allowlist upstreamAllowlist
limits *pullLimiter
tokens *tokenBroker
credentials *credentialFinder
}
// handle serves every path under /v2/.
//
// The version check and the pulls share a route because gin's router refuses to
// hold /v2/ and /v2/*path at once — registering both panics on a catch-all
// conflict — so the one endpoint that is a fixed path is separated here.
func (p *registryProxy) handle(c *gin.Context) {
if c.Param("path") == "/" {
p.version(c)
return
}
p.pull(c)
}
// version answers the endpoint a client uses to discover what this registry
// requires of it.
//
// An unauthenticated caller is answered with a Basic challenge, and that answer
// is load-bearing rather than decorative. A client reads this response to decide
// whether to send a credential at all: answered 200 with no challenge, it
// concludes none is wanted and sends none, and every pull after it is refused
// for a reason the operator cannot see from the configuration.
func (p *registryProxy) version(c *gin.Context) {
if _, ok := p.authenticated(c); !ok {
return
}
c.Header("Docker-Distribution-Api-Version", "registry/2.0")
c.Status(http.StatusOK)
}
func (p *registryProxy) pull(c *gin.Context) {
route, err := ParseRoute(c.Request.URL.Path)
if err != nil {
p.refuseRoute(c, err)
return
}
caller, ok := p.authenticated(c)
if !ok {
return
}
if retryAfter, allowed := p.limits.allow(caller.runnerID, route.Org); !allowed {
c.Header("Retry-After", strconv.Itoa(int(retryAfter.Round(time.Second)/time.Second)+1))
// Which meter tripped is this proxy's business, not the caller's: it
// slows down either way, and naming the wrong one sends an operator
// looking in the wrong place.
refuse(c, http.StatusTooManyRequests, oci.CodeTooManyRequests, "too many pulls; slow down")
return
}
ctx := c.Request.Context()
if p.allowlist.permit(route.PublishedHost) != nil {
ctx = context.WithValue(ctx, customRegistryKey{}, true)
}
response, presentedLogin, err := p.forward(ctx, route, c.Request.Method, c.Request.Header, caller)
if err != nil {
p.refuseUpstream(c, route, presentedLogin, err)
return
}
defer response.Body.Close()
if response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden {
// The upstream refused us, not the caller. Passing its status through
// would tell the caller to re-authenticate to this proxy, which it has
// already done successfully, and it would loop.
refuse(c, http.StatusForbidden, oci.CodeDenied, upstreamRefusal(route, presentedLogin))
return
}
relay(c, response)
}
// upstreamRefusal says whose refusal it was. A login that was presented and
// refused is the organization's to fix, which is a different thing to tell
// someone than an image the upstream will not serve.
func upstreamRefusal(route Route, presentedLogin bool) string {
if presentedLogin {
return "upstream " + route.PublishedHost + " refused the organization's registry credential"
}
return "upstream " + route.PublishedHost + " refused the pull"
}
// forward issues the pull upstream, answering an authentication challenge once
// if the upstream makes one. It also reports whether that answer presented the
// organization's login.
//
// The current private credential version is checked before reusing a bearer.
// A matching token avoids another exchange and secret read; a rotated version
// needs the normal 401 challenge and a new exchange. Lookup failure is closed,
// even when a bearer is still warm.
func (p *registryProxy) forward(
ctx context.Context,
route Route,
method string,
inbound http.Header,
from caller,
) (*http.Response, bool, error) {
header := upstreamHeader(inbound)
credential, err := p.credentialForPull(ctx, from, route)
if err != nil {
return nil, false, err
}
token, _ := p.tokens.cached(from.runnerID, route.Org, route.Upstream, credential.secretVersion)
response, err := p.upstream.Pull(ctx, method, route.Upstream, route.Request, merge(header, bearer(token)))
if err != nil || response.StatusCode != http.StatusUnauthorized {
return response, false, err
}
challenge := response.Header.Get("Www-Authenticate")
response.Body.Close()
if challenge == "" {
return nil, false, errors.New("upstream refused the pull and named no way to authenticate")
}
login, err := p.credentials.loginFrom(ctx, credential)
if errors.Is(err, ErrCredentialUnavailable) {
// Rotation may have retired the selected version during the challenge round trip.
credential, err = p.credentialForPull(ctx, from, route)
if err != nil {
return nil, false, err
}
login, err = p.credentials.loginFrom(ctx, credential)
}
if err != nil {
return nil, false, err
}
token, err = p.tokens.acquire(ctx, from.runnerID, route.Org, route.Upstream, challenge, login)
if err != nil {
return nil, login != nil, err
}
response, err = p.upstream.Pull(ctx, method, route.Upstream, route.Request, merge(header, bearer(token)))
return response, login != nil, err
}
func (p *registryProxy) credentialForPull(ctx context.Context, from caller, route Route) (foundCredential, error) {
credential, err := p.credentials.lookup(ctx, from, route.Org, route.PublishedHost, route.Upstream.Repository)
if err != nil {
return foundCredential{}, err
}
if isCustomRegistry(ctx) && credential.none {
return foundCredential{}, fmt.Errorf("%w: a matching registry credential is required for %s", ErrHostRefused, route.PublishedHost)
}
return credential, nil
}
// relay copies the upstream answer to the caller.
//
// Untouched, and streamed. The digest a client verifies covers the manifest
// bytes as the upstream served them, so re-encoding here breaks every pull; and
// a blob is measured in gigabytes, so holding one to inspect it would put the
// whole image in this process's memory.
func relay(c *gin.Context, response *http.Response) {
for name, values := range response.Header {
if withheld(name) {
continue
}
for _, value := range values {
c.Writer.Header().Add(name, value)
}
}
c.Status(response.StatusCode)
if _, err := io.Copy(c.Writer, response.Body); err != nil {
// The status and headers are already on the wire, so nothing can be
// said to the caller; the truncated body is the signal. Recording it is
// what separates "the client hung up" from "the upstream did".
slog.WarnContext(c.Request.Context(), "Relay ended early", "error", err)
}
}
// withheld names the response headers that must not be copied onward.
func withheld(name string) bool {
switch http.CanonicalHeaderKey(name) {
// Hop-by-hop: they describe this connection, not the answer.
case "Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization",
"Te", "Trailer", "Transfer-Encoding", "Upgrade":
return true
// The upstream's own challenge. Relaying it would tell the caller to
// authenticate to a registry it holds no credential for, which is the whole
// reason this proxy exists.
case "Www-Authenticate":
return true
default:
return false
}
}
// upstreamHeader is the part of the caller's request that belongs to the pull
// rather than to the caller.
//
// Accept decides which manifest media type comes back, so dropping it turns an
// image index into a "manifest unknown" for clients that asked for one. Range
// is what a resumed blob download is made of. The caller's own Authorization is
// deliberately absent: it authenticates to this proxy and means nothing
// upstream.
func upstreamHeader(inbound http.Header) http.Header {
forwarded := http.Header{}
for _, name := range []string{"Accept", "Accept-Encoding", "Range", "If-None-Match"} {
for _, value := range inbound.Values(name) {
forwarded.Add(name, value)
}
}
return forwarded
}
func merge(base, extra http.Header) http.Header {
if len(extra) == 0 {
return base
}
merged := base.Clone()
if merged == nil {
merged = http.Header{}
}
for name, values := range extra {
for _, value := range values {
merged.Add(name, value)
}
}
return merged
}
// caller is a runner this proxy has verified, and the key it verified it by:
// the control plane answers only runners, so that key is also what a
// credential lookup is made with.
type caller struct {
runnerID string
apiKey string
}
// authenticated resolves the caller to a runner, answering the caller itself
// when it cannot.
func (p *registryProxy) authenticated(c *gin.Context) (caller, bool) {
_, apiKey, presented := c.Request.BasicAuth()
if !presented {
challenge(c)
return caller{}, false
}
runnerID, err := p.runners.authenticate(c.Request.Context(), apiKey)
switch {
case errors.Is(err, ErrUnauthenticated):
challenge(c)
return caller{}, false
case errors.Is(err, ErrAuthUnavailable):
// Not a refusal: the control plane could not be asked. Saying so beats
// a bare 500, which reads as a bug in this proxy, and beats admitting
// the caller, which would make the control plane's outage a way in.
slog.WarnContext(c.Request.Context(), "Cannot verify a caller", "error", err)
refuse(c, http.StatusServiceUnavailable, oci.CodeUnauthorized,
"cannot verify credentials right now; retry shortly")
return caller{}, false
case err != nil:
refuse(c, http.StatusInternalServerError, oci.CodeUnauthorized, "credential check failed")
return caller{}, false
}
return caller{runnerID: runnerID, apiKey: apiKey}, true
}
// challenge states what this proxy wants, which is Basic: a caller holds a
// runner API key, and there is no token endpoint here to trade it at.
func challenge(c *gin.Context) {
c.Header("Www-Authenticate", `Basic realm="`+Realm+`"`)
refuse(c, http.StatusUnauthorized, oci.CodeUnauthorized, "authentication required")
}
func (p *registryProxy) refuseRoute(c *gin.Context, err error) {
switch {
case errors.Is(err, oci.ErrNotPullPath):
refuse(c, http.StatusNotFound, oci.CodeUnsupported, "not a pull endpoint")
case errors.Is(err, ErrNotRoutable), errors.Is(err, oci.ErrInvalidName), errors.Is(err, oci.ErrInvalidHost):
refuse(c, http.StatusBadRequest, oci.CodeNameInvalid, err.Error())
default:
refuse(c, http.StatusBadRequest, oci.CodeManifestInvalid, err.Error())
}
}
func (p *registryProxy) refuseUpstream(c *gin.Context, route Route, presentedLogin bool, err error) {
if errors.Is(err, ErrHostRefused) {
refuse(c, http.StatusForbidden, oci.CodeDenied, err.Error())
return
}
slog.WarnContext(c.Request.Context(), "Upstream pull failed",
"upstream", route.Upstream.Endpoint, "repository", route.Upstream.Repository, "error", err)
if errors.Is(err, ErrCredentialUnavailable) {
// Not a refusal: the login could not be looked up or read. Pulling
// anonymously instead would turn this outage into a misleading denial.
refuse(c, http.StatusServiceUnavailable, oci.CodeUnauthorized,
"cannot read the registry credential for "+route.PublishedHost+" right now; retry shortly")
return
}
if errors.Is(err, ErrLoginNotPermitted) {
refuse(c, http.StatusForbidden, oci.CodeDenied,
"this runner hosts no box of the organization, so its registry credential is not used")
return
}
if errors.Is(err, ErrRealmRefused) {
refuse(c, http.StatusBadGateway, oci.CodeDenied,
"upstream "+route.PublishedHost+" named a token endpoint on another host; the organization's credential was not sent")
return
}
if errors.Is(err, errCustomRegistryRequiresBearer) {
refuse(c, http.StatusForbidden, oci.CodeDenied, route.PublishedHost+": "+err.Error())
return
}
if presentedLogin && errors.Is(err, oci.ErrCredentialRejected) {
refuse(c, http.StatusForbidden, oci.CodeDenied, upstreamRefusal(route, presentedLogin))
return
}
if errors.Is(err, ErrAddressRefused) {
refuse(c, http.StatusForbidden, oci.CodeDenied,
"upstream "+route.PublishedHost+" sent the pull to an address this proxy will not reach")
return
}
refuse(c, http.StatusBadGateway, oci.CodeUnsupported,
"upstream "+route.PublishedHost+" could not be reached")
}
// refuse answers with the document shape the specification gives refusals, so a
// client can act on the code rather than parse prose.
func refuse(c *gin.Context, status int, code oci.ErrorCode, message string) {
c.JSON(status, oci.Refusal(code, message))
}