Skip to content

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

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

package proxy

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"slices"
	"time"

	"github.com/boxlite-ai/image-service/internal/oci"
)

// credentialedTokenLifetime caps how long a token bought with an
// organization's own login is reused.
//
// Version checks reject rotated credentials' cached tokens before reuse. This
// cap remains an additional bound: gcr.io states twelve hours; five minutes
// covers a manifest and every layer of one pull. Anonymous tokens are not capped.
const credentialedTokenLifetime = 5 * time.Minute

// ErrRealmRefused means a registry named a token endpoint this proxy will not
// send an organization's login to.
var ErrRealmRefused = errors.New("token endpoint refused for a login")

var errCustomRegistryRequiresBearer = errors.New("custom registry requires HTTPS and a Bearer token endpoint on the same host")

// tokenHostsBeyond names the hosts a registry's token endpoint may sit on
// other than the registry itself. Docker Hub's is `auth.docker.io`; ghcr.io,
// quay.io and gcr.io each issue tokens from their own host, as do gcr.io's
// regional hosts (us.gcr.io answers with realm https://us.gcr.io/v2/token).
//
// A login goes nowhere else. The challenge that names a token endpoint is the
// registry's own answer and arrives over verified TLS, but it is still text an
// upstream chose, and a password is not something to send wherever that text
// points. An anonymous exchange is not held to this: it carries nothing.
var tokenHostsBeyond = map[string][]string{"registry-1.docker.io": {"auth.docker.io"}}

// checkRealm refuses a token endpoint that is neither the registry's own host
// nor one it is known to use.
func checkRealm(challenge oci.Challenge, upstream oci.Upstream) error {
	realm, err := url.Parse(challenge.Parameters["realm"])
	if err != nil {
		return fmt.Errorf("%w: realm is not a URL: %w", ErrRealmRefused, err)
	}
	if realm.Host == upstream.Endpoint || slices.Contains(tokenHostsBeyond[upstream.Endpoint], realm.Host) {
		return nil
	}
	return fmt.Errorf("%w: %s named %s", ErrRealmRefused, upstream.Endpoint, realm.Host)
}

// tokenBroker holds the short-lived bearers upstream registries issue.
//
// Keyed by organization as well as upstream and scope, because an
// organization's own login buys some of them, and sharing one of those across
// organizations would hand out access nobody granted. Keyed by runner too, for
// the same reason one step down: a runner is given an organization's login
// only while it hosts a box of that organization, and a token that login
// bought for one runner would otherwise serve any other runner's pull of the
// same path without that question being asked.
type tokenBroker struct {
	client *oci.Client
	tokens *ttlCache[registryToken]
}

type registryToken struct {
	value         string
	secretVersion string
}

func newTokenBroker(client *oci.Client) *tokenBroker {
	return &tokenBroker{client: client, tokens: newTTLCache[registryToken](maxCacheEntries)}
}

// cached returns a bearer already held for this runner's pull, if any.
func (b *tokenBroker) cached(runnerID, org string, upstream oci.Upstream, secretVersion string) (string, bool) {
	token, held := b.tokens.get(tokenKey(runnerID, org, upstream))
	if !held || token.secretVersion != secretVersion {
		return "", false
	}
	return token.value, true
}

// acquire answers the challenge an upstream just made and remembers the result.
//
// With a login, the exchange presents it and the token it buys reaches what
// the organization can see. Without one it is anonymous, and reaches only what
// the upstream serves to anyone.
func (b *tokenBroker) acquire(
	ctx context.Context,
	runnerID string,
	org string,
	upstream oci.Upstream,
	header string,
	login *registryLogin,
) (string, error) {
	challenge, err := oci.ParseChallenge(header)
	if err != nil {
		return "", err
	}
	if challenge.Scheme != oci.SchemeBearer {
		if login != nil && isCustomRegistry(ctx) {
			return "", errCustomRegistryRequiresBearer
		}
		return "", nil
	}
	if login != nil {
		if err := checkRealm(challenge, upstream); err != nil {
			return "", err
		}
	}

	token, err := b.client.Exchange(ctx, challenge, oci.PullScope(upstream.Repository), basicAuth(login))
	if err != nil {
		return "", err
	}
	lifetime := token.Lifetime()
	if login != nil && lifetime > credentialedTokenLifetime {
		lifetime = credentialedTokenLifetime
	}
	cached := registryToken{value: token.Value}
	if login != nil {
		cached.secretVersion = login.secretVersion
	}
	b.tokens.put(tokenKey(runnerID, org, upstream), cached, lifetime)
	return token.Value, nil
}

// basicAuth is how a login is presented to a token endpoint, or nothing at all
// for an anonymous exchange.
func basicAuth(login *registryLogin) http.Header {
	if login == nil {
		return nil
	}
	request := http.Request{Header: http.Header{}}
	request.SetBasicAuth(login.username, login.password)
	return request.Header
}

// tokenKey is (runner, organization, upstream endpoint, repository). The
// repository is the scope: a token issued for one repository is refused for
// every other, so a key without it would hand out a bearer that cannot work.
func tokenKey(runnerID, org string, upstream oci.Upstream) string {
	return runnerID + "\x00" + org + "\x00" + upstream.Endpoint + "\x00" + upstream.Repository
}

// bearer returns the Authorization header value for a token, or nothing at all
// when there is no token: an empty header is how an anonymous pull is spelled.
func bearer(token string) http.Header {
	if token == "" {
		return nil
	}
	return http.Header{"Authorization": {"Bearer " + token}}
}