Files
archie/internal/app/archied/kit_launcher.go
T
sam 215c14a664 feat(kitrun): render the OAuth credential file with sentinels
A Kit's credential@1 oauth.credentialFile was never rendered: Assemble set
SBX_CRED_<SVC>_MODE=oauth but no file was written, so the harness CLI started
logged out and never reached the token endpoint the egress proxy intercepts.

Render it at launch, for each bound OAuth-managed service that declares one:
the Kit's structure with its access/refresh sentinels, and the stored token
set's expiry for {{.ExpiresAt}}. The renderer's only input about a token set
is that expiry -- the tokens never cross into kit, so a real token cannot be
written (docs/prds/external-agent-harness.md, Verification).

Expiry renders as epoch milliseconds, the unit both in-spec OAuth credential
files use (Claude Code's expiresAt, OpenCode's expires). A placeholder archie
does not render, or a token placeholder with no declared sentinel, refuses the
launch rather than leaving a literal or an empty value in the file.

kitrun reads the store only for a service the run credential carries, and
fails closed when a rendered credential file has no captured token set.
2026-09-30 07:36:57 +10:00

103 lines
3.7 KiB
Go

package archied
import (
"context"
"errors"
"log/slog"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/docker/sandbox-kit-spec/v3/fetch"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
)
const relayName = "archie-egress-relay"
// setupKitLauncher builds the egress path Kit tasks run on: the per-daemon
// CA, the proxy on the worker bridge's host gateway, and the relay. It
// degrades to no launcher, which parks Kit tasks, rather than failing boot:
// image profiles do not need it.
func (b *boot) setupKitLauncher(ctx context.Context) {
pool, log := b.containerPool, b.log
if pool == nil || pool.HostGateway() == "" {
log.Info("kit harness runs disabled: they need the managed container pool on a bridge with a host gateway")
return
}
exe, err := os.Executable()
if err != nil {
log.Warn("kit harness runs disabled", "err", err)
return
}
agentBinary := filepath.Join(filepath.Dir(exe), "archie-agent")
caDir := filepath.Join(b.cfg.StateDir, "egress")
ca, err := egress.LoadOrCreateCA(caDir)
if err != nil {
log.Warn("kit harness runs disabled: egress CA", "err", err)
return
}
fetcher, err := fetch.New(fetch.WithDockerCredentials())
if err != nil {
log.Warn("kit harness runs disabled: kit registry client", "err", err)
return
}
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(pool.HostGateway(), "0"))
if err != nil {
log.Warn("kit harness runs disabled: egress proxy listener", "err", err)
return
}
endpoints := kitrun.Endpoints{NATS: b.natsURL, StateStore: strings.TrimSpace(b.cfg.Services.Get(config.ServiceNameState).Target)}
cmd, err := kitrun.RelayCommand(ln.Addr().String(), endpoints)
if err != nil {
_ = ln.Close()
log.Warn("kit harness runs disabled: egress relay", "err", err)
return
}
// grants is the production Resolver: Launch/Release Grant/RevokeGrant it
// per run with exactly the declared-and-granted intersection
// (docs/prds/external-agent-harness.md, Credentials; archie-core-egkf.11).
grants := egress.NewGrantResolver()
// Without a harness secret store, Register refuses a Kit with a
// required OAuth credential.
oauthStore, _ := b.stateStore.(egress.OAuthStore)
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: grants, OAuthStore: oauthStore})
srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second}
go func() {
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error("egress proxy stopped", "err", err)
}
}()
networks := egress.NewNetworks(pool.Client(), egress.RelaySpec{
Name: relayName, Image: b.cfg.Containers.Image,
Entrypoint: []string{"archie-agent"}, Cmd: cmd, Network: pool.NetworkName(),
})
b.addCleanup(shutdownEgress(networks, srv, log))
b.kitLauncher = &kitrun.Launcher{
Pool: pool, Fetch: fetcher, Proxy: proxy, Networks: networks,
AgentBinary: agentBinary, CAFile: egress.CACertPath(caDir),
Config: b.cfgHolder, Secrets: b.secrets, Grants: grants, OAuth: oauthStore,
}
log.Info("kit harness runs enabled", "proxy", ln.Addr().String())
}
// shutdownEgress removes the relay and stops the proxy. It derives its own
// budget because the boot context is cancelled by the time shutdown runs.
//
//nolint:contextcheck
func shutdownEgress(networks *egress.Networks, srv *http.Server, log *slog.Logger) func() {
return func() {
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := networks.Close(stopCtx); err != nil {
log.Warn("egress relay removal failed", "err", err)
}
_ = srv.Close()
}
}