A test run used to suppress only forge and push writes, which named one destination among many. Deliveries now have one seam: TaskContext.Deliveries records each as "would ..." and performs none, and a recording egress session does the same for a Kit run's HTTP writes and central tool calls, answering them synthetically. Provider hosts stay live, since model traffic is how a test run reads. Native containers have no proxy (archie-core-n8d3).
112 lines
3.9 KiB
Go
112 lines
3.9 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))
|
|
}
|
|
// A model session never records, whatever its task is: this is the one
|
|
// session that must carry provider traffic for real, and it carries nothing
|
|
// else because its network policy allows only provider hosts. A test run's
|
|
// recordings happen on the Kit session that carries the rest of the run.
|
|
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) }
|