Files
archie/internal/app/archied/model_egress.go
T
sam 4d4f30b719 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

108 lines
3.6 KiB
Go

package archied
import (
"context"
"fmt"
"maps"
"slices"
"strings"
"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/secret"
"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. A provider's key is the one its api_key_ref resolves to, carried by
// the session, or the org's credential binding named after the provider for a
// provider that only declares an env var.
type modelEgress struct {
proxy *egress.Proxy
addr string
caFile string
config *config.Holder
secrets *secret.Registry
}
func (m modelEgress) Open(token, org string, providers map[string]config.Provider) (daemon.ModelSession, error) {
// Each keyed provider's credential variable: the operator's, or the name
// derived for a provider that carries only a secret reference.
keyed := make(map[string]string, len(providers))
for name, p := range providers {
if keyEnv := p.KeyEnv(name); keyEnv != "" {
keyed[name] = keyEnv
}
}
names := slices.Sorted(maps.Keys(keyed))
if len(names) == 0 {
return daemon.ModelSession{}, nil
}
bound := m.config.Get().Containers.BoundCredentials(org, names, names)
secrets := make(map[string]string, len(keyed))
var (
subs []egress.Substitution
hosts []string
env []string
)
for _, name := range names {
p := providers[name]
host, err := egress.ProviderHost(p.Class, p.BaseURL)
if err != nil {
return daemon.ModelSession{}, fmt.Errorf("provider %q: %w", name, err)
}
key, err := m.providerKey(name, p, org, bound)
if err != nil {
return daemon.ModelSession{}, err
}
if key != "" {
secrets[name] = key
}
subs = append(subs, egress.Substitution{Service: name, Host: host})
hosts = append(hosts, host)
env = append(env, keyed[name]+"="+egress.SentinelFor(name))
}
session, err := m.proxy.Register(egress.SessionOptions{
Token: token, Org: org, Substitutions: subs, Secrets: secrets,
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, Shared: true}},
}, nil
}
// providerKey is the key this session carries for p: its secret reference
// resolved. A provider with no reference keeps the org's binding path, which
// the proxy resolves when a request arrives, so its key stays only there.
func (m modelEgress) providerKey(name string, p config.Provider, org string, bound map[string]config.CredentialBinding) (string, error) {
if p.APIKey == (config.SecretRef{}) {
if _, ok := bound[name]; !ok {
return "", fmt.Errorf("provider %q has no credential binding named %q in org %s", name, name, org)
}
return "", nil
}
if m.secrets == nil {
return "", fmt.Errorf("provider %q: no secret engine is available to resolve its api_key_ref", name)
}
value, err := m.secrets.Resolve(p.APIKey)
if err != nil {
return "", fmt.Errorf("provider %q: resolve api_key_ref: %w", name, err)
}
if value = strings.TrimSpace(value); value == "" {
return "", fmt.Errorf("provider %q: api_key_ref resolved empty", name)
}
return value, nil
}
func (m modelEgress) Close(ctx context.Context, token string) { m.proxy.Revoke(ctx, token) }