Archie runs its own agent loop only. Removes Kit resolution and launch, the harness runner and adapters, the setup terminal and sandbox view, harness secrets and OAuth capture (table dropped in migration 0075), the task-tools contract that only Kit containers reached, and the sandbox-kit-spec dependency. Agent profiles carry image and tools only. The egress proxy keeps the model-session path; the sandbox relay code stays for #1867.
101 lines
3.5 KiB
Go
101 lines
3.5 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"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))
|
|
}
|
|
if _, err := m.proxy.Register(egress.SessionOptions{Token: token, Allow: hosts, Substitutions: subs, Secrets: secrets}); err != nil {
|
|
return daemon.ModelSession{}, err
|
|
}
|
|
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(_ context.Context, token string) { m.proxy.Revoke(token) }
|