Files
archie/internal/infrastructure/egress/substitute.go
T
sam b4d8bb702e fix(egress): bind a task's model providers from their api_key_ref
A container task parked when a provider carried an api_key_env but no
credential binding named after it, even though the task's models never
reached that provider. Egress pre-bound every provider the instance
configures and ignored api_key_ref, so the credentials an operator stores in
provider-settings were never used.

A task's config now carries only the providers its model aliases resolve to,
each with the variable its credential is read from: the operator's
api_key_env, or a name derived from the provider when its credential is a
secret reference. modelEgress resolves that reference once for the session
and the proxy substitutes the session's key for the sentinel, so the
container still holds no key. A provider that only declares an env var keeps
resolving through its org credential binding.
2026-10-20 06:56:38 +11:00

98 lines
2.8 KiB
Go

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
}
key, err := p.keyFor(ctx, s, sub.service)
if err != nil {
return err
}
for _, values := range r.Header {
for i, v := range values {
values[i] = strings.ReplaceAll(v, sentinel, key)
}
}
r.URL.RawQuery = strings.ReplaceAll(r.URL.RawQuery, sentinel, key)
}
return nil
}
// keyFor is the key the run itself carries for service, or the one its
// credential binding resolves. A run-supplied key needs no binding; a service
// the session holds no key for keeps the binding's ErrUnbound semantics.
func (p *Proxy) keyFor(ctx context.Context, s *Session, service string) (string, error) {
if key, ok := s.secrets[service]; ok {
return key, nil
}
if p.resolver == nil {
return "", fmt.Errorf("credential %q has no resolver", service)
}
key, err := p.resolver.Resolve(ctx, s.credential(), service)
if errors.Is(err, ErrUnbound) {
return "", fmt.Errorf("credential %q is not bound for this run", service)
}
if err != nil {
return "", fmt.Errorf("resolve credential %q: %w", service, err)
}
return key, 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
}