feat(egress): the run credential is the proxy login; Kit secrets resolve per request from live grants

This commit is contained in:
sam committed 2026-10-04 19:35:26 +11:00
1 parent 4699280c9d
commit 23636d6120
11 files changed
+176 -134

No files matched your search

+5 -5
View File
@@ -59,13 +59,13 @@ func (b *boot) setupKitLauncher(ctx context.Context) {
log.Warn("kit harness runs disabled: egress relay", "err", err)
return
}
// grants is the production Resolver: Launch/Release Grant/RevokeGrant it
// per run with exactly the declared-and-granted intersection
grants := egress.NewGrantResolver()
// The proxy resolves each credential from the run credential the
// container logs in with, against the live grants and bindings.
resolver := runCredentialResolver{runs: b.stateStoreGrants.Client, config: b.cfgHolder, secrets: b.secrets}
// Without a harness secret store, Register refuses a Kit with a
// required OAuth credential.
oauthStore, _ := b.stateStore.(egress.OAuthStore)
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: grants, OAuthStore: oauthStore})
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: resolver, OAuthStore: oauthStore})
srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second}
go func() {
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
@@ -80,7 +80,7 @@ func (b *boot) setupKitLauncher(ctx context.Context) {
b.kitLauncher = &kitrun.Launcher{
Pool: pool, Fetch: fetcher, Proxy: proxy, Networks: networks,
AgentBinary: agentBinary, CAFile: egress.CACertPath(caDir),
Config: b.cfgHolder, Secrets: b.secrets, Grants: grants, OAuth: oauthStore,
Config: b.cfgHolder, Secrets: b.secrets, OAuth: oauthStore,
}
log.Info("kit harness runs enabled", "proxy", ln.Addr().String())
}
@@ -0,0 +1,41 @@
package archied
import (
"context"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
// runCredentialResolver is the egress proxy's Resolver: it resolves a Kit
// credential for the run a run credential names, when the request is made,
// from the live grants and bindings. A revoked credential, a grant removed
// mid-run or a binding in another org stops resolving at once.
type runCredentialResolver struct {
runs interface {
TaskForCredential(ctx context.Context, token string) (*workflow.Task, error)
}
config *config.Holder
secrets interface {
Resolve(config.SecretRef) (string, error)
}
}
func (r runCredentialResolver) Resolve(ctx context.Context, credential, service string) (string, error) {
task, err := r.runs.TaskForCredential(ctx, credential)
if err != nil {
return "", egress.ErrUnbound
}
cfg := r.config.Get()
org, granted := cfg.CredentialAccess(task.Identity)
binding, ok := cfg.Containers.BoundCredentials(org, granted, []string{service})[service]
if !ok {
return "", egress.ErrUnbound
}
if binding.Secret == (config.SecretRef{}) {
// An OAuth binding: the proxy supplies the org's token set itself.
return "", nil
}
return r.secrets.Resolve(binding.Secret)
}
@@ -0,0 +1,67 @@
package archied
import (
"context"
"errors"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
type runsByToken map[string]*workflow.Task
func (r runsByToken) TaskForCredential(_ context.Context, token string) (*workflow.Task, error) {
if task, ok := r[token]; ok {
return task, nil
}
return nil, errors.New("unknown")
}
type secretValues map[string]string
func (s secretValues) Resolve(ref config.SecretRef) (string, error) { return s[ref.Key], nil }
// TestEgressResolvesOnlyGrantedCredentials pins that the proxy hands a run
// only a secret its identity is granted and bound for in its own org.
func TestEgressResolvesOnlyGrantedCredentials(t *testing.T) {
own := []string{"anthropic"}
cfg := config.Config{
GrantedCredentials: []string{"github"},
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"}},
}},
}
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"},
}
tests := []struct {
name, credential, service, want string
}{
{"root run, granted", "root-run", "github", "gh-secret"},
{"root run, not granted", "root-run", "anthropic", ""},
{"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", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := r.Resolve(context.Background(), tt.credential, tt.service)
if tt.want == "" {
if !errors.Is(err, egress.ErrUnbound) {
t.Fatalf("got %q, %v; want ErrUnbound", got, err)
}
return
}
if err != nil || got != tt.want {
t.Fatalf("got %q, %v; want %q", got, err, tt.want)
}
})
}
}
+19
View File
@@ -807,6 +807,25 @@ func (c ContainerConfig) BoundCredentials(org string, granted, declared []string
return out
}
// CredentialAccess returns the org and granted credential services of the
// identity named name: the root's, each overridden by the identity's own when
// it sets one. An unknown name is the root.
func (c Config) CredentialAccess(name string) (org string, granted []string) {
org, granted = c.Org, c.GrantedCredentials
for _, id := range c.Identities {
if id.Name != name {
continue
}
if id.Org != "" {
org = id.Org
}
if id.GrantedCredentials != nil {
granted = *id.GrantedCredentials
}
}
return org, granted
}
// ValidateCredentialBindings rejects a binding with an empty service name or
// duplicated (service, org) pair: two bindings resolving the same Kit
// credential in the same org is ambiguous, not a fallback chain.
+1 -6
View File
@@ -2041,12 +2041,7 @@ func configForIdentity(root config.Config, identity config.IdentityConfig) confi
if identity.DiffCapLines != nil {
root.DiffCapLines = identity.DiffCapLines
}
if identity.Org != "" {
root.Org = identity.Org
}
if identity.GrantedCredentials != nil {
root.GrantedCredentials = *identity.GrantedCredentials
}
root.Org, root.GrantedCredentials = root.CredentialAccess(identity.Name)
root.Forge = identity.Forge
root.Dispatch = identity.Dispatch
root.Models = identity.Models
+1
View File
@@ -75,6 +75,7 @@ func (d *Daemon) runKitTask(ctx context.Context, task *workflow.Task, repo confi
GateRetries: iface.Needs().GateRetries,
Org: taskCfg.Org,
GrantedServices: taskCfg.GrantedCredentials,
RunCredential: credential,
})
if err != nil {
park("kit launch failed", err)
@@ -32,18 +32,18 @@ const (
// stops the request.
var ErrUnbound = errors.New("egress: credential not bound")
// Resolver supplies a credential's real value for one run. An implementation
// returns only secrets the run's credential carries, so what a Kit declares
// is always narrowed by what the org granted the run's identity.
// Resolver supplies a credential's real value for the run a run credential
// names. An implementation returns only secrets the run's identity is
// granted, so what a Kit declares is always narrowed by what the org granted.
type Resolver interface {
Resolve(ctx context.Context, run, service string) (string, error)
Resolve(ctx context.Context, credential, service string) (string, error)
}
// ResolverFunc adapts a function to Resolver.
type ResolverFunc func(ctx context.Context, run, service string) (string, error)
type ResolverFunc func(ctx context.Context, credential, service string) (string, error)
func (f ResolverFunc) Resolve(ctx context.Context, run, service string) (string, error) {
return f(ctx, run, service)
func (f ResolverFunc) Resolve(ctx context.Context, credential, service string) (string, error) {
return f(ctx, credential, service)
}
// injection is one compiled credential@1 apiKey inject rule.
@@ -103,7 +103,7 @@ func (p *Proxy) inject(ctx context.Context, s *Session, r *http.Request, host st
}
continue
}
secret, err := p.resolver.Resolve(ctx, s.run, rule.service)
secret, err := p.resolver.Resolve(ctx, s.token, rule.service)
switch {
case errors.Is(err, ErrUnbound) && !rule.required:
continue
-57
View File
@@ -1,57 +0,0 @@
package egress
import (
"context"
"sync"
)
// GrantResolver is the production Resolver: it holds, per active run, only the
// secrets a caller explicitly Grant-ed it -- the intersection a run credential
// carries. It never derives a secret from anything else, so a service nobody
// granted, or granted to a different run, always answers ErrUnbound.
type GrantResolver struct {
mu sync.RWMutex
grants map[string]map[string]string
}
// NewGrantResolver returns an empty resolver: no run may resolve anything
// until Grant is called for it.
func NewGrantResolver() *GrantResolver {
return &GrantResolver{grants: map[string]map[string]string{}}
}
// Grant records exactly what one run may resolve, replacing whatever it held
// before -- a repeated Grant for the same run (a retry, or a narrower set)
// never merges with the last, since a service the caller dropped must not
// keep resolving from a stale entry.
func (g *GrantResolver) Grant(run string, secrets map[string]string) {
g.mu.Lock()
defer g.mu.Unlock()
g.grants[run] = secrets
}
// RevokeGrant ends a run: nothing it was granted resolves after this call.
func (g *GrantResolver) RevokeGrant(run string) {
g.mu.Lock()
defer g.mu.Unlock()
delete(g.grants, run)
}
// Resolve implements Resolver. A run with no Grant, or a service not among
// what it was granted, answers ErrUnbound -- the only error ambiguous enough
// for an optional credential to pass over (see Resolver's doc).
func (g *GrantResolver) Resolve(_ context.Context, run, service string) (string, error) {
g.mu.RLock()
defer g.mu.RUnlock()
secrets, ok := g.grants[run]
if !ok {
return "", ErrUnbound
}
v, ok := secrets[service]
if !ok {
return "", ErrUnbound
}
return v, nil
}
var _ Resolver = (*GrantResolver)(nil)
+1 -1
View File
@@ -109,7 +109,7 @@ func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool,
case p.resolver == nil:
err = ErrUnbound
default:
_, err = p.resolver.Resolve(ctx, s.run, rule.service)
_, err = p.resolver.Resolve(ctx, s.token, rule.service)
}
switch {
case err == nil:
+8 -9
View File
@@ -2,11 +2,9 @@ package egress
import (
"context"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"net"
@@ -36,7 +34,6 @@ var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)}
// it to runtime before the workload's entrypoint starts.
type Session struct {
token string
run string
org string
install rules
runtime rules
@@ -45,7 +42,8 @@ type Session struct {
atRun atomic.Bool
}
// Token is the secret the container presents as its proxy password.
// Token is the secret the container presents as its proxy password: its run
// credential.
func (s *Session) Token() string { return s.token }
// EnterRuntime switches the session from install-phase to runtime-phase
@@ -77,7 +75,9 @@ type ProxyOptions struct {
// under, the org whose OAuth token sets it uses, and its Kit's network
// policy and credential requests.
type SessionOptions struct {
Run string
// Token is the run credential: the container's proxy password, and what
// the Resolver resolves the run from.
Token string
Org string
Network *spec.PhasedNetwork
// Credentials are what the Kit declares; Bound maps each service the run
@@ -118,12 +118,11 @@ func NewProxy(ca *CA, opts ProxyOptions) *Proxy {
// Register opens a session for one container. A nil network policy allows
// no egress in either phase.
func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
raw := make([]byte, 32)
if _, err := rand.Read(raw); err != nil {
return nil, err
if opts.Token == "" {
return nil, errors.New("egress session needs the run credential")
}
s := &Session{
token: hex.EncodeToString(raw), run: opts.Run, org: opts.Org,
token: opts.Token, org: opts.Org,
injections: compileInjections(opts.Credentials, opts.Bound), oauth: compileOAuthRules(opts.Credentials, opts.Bound),
}
for _, rule := range s.oauth {
+25 -48
View File
@@ -31,14 +31,6 @@ type SecretResolver interface {
Resolve(config.SecretRef) (string, error)
}
// Grants is what Launch and Release tell the egress proxy's resolver about a
// run's credentials: exactly the intersection Launch computed, and nothing
// once the run ends. *egress.GrantResolver satisfies it.
type Grants interface {
Grant(run string, secrets map[string]string)
RevokeGrant(run string)
}
// OAuthSecrets reads an org's stored OAuth token set for a bound service, so a
// Kit's credential file can render that set's expiry. It is the store's read
// half only: kitrun never writes a token set, and nothing but the expiry
@@ -62,11 +54,12 @@ type Launcher struct {
// Kit container: the worker's own binary and the egress CA.
AgentBinary string
CAFile string
// Config is read on every Launch. Nil Secrets or Grants leave every
// Config and Secrets decide, at launch, which declared credentials the run
// is bound for and of which kind. The values themselves are resolved by
// the proxy's Resolver per request. Nil Secrets leaves every API-key
// credential unbound.
Config *config.Holder
Secrets SecretResolver
Grants Grants
// OAuth reads the stored token set whose expiry a Kit's credential file
// renders. A nil OAuth refuses the launch of a Kit that renders one, the
// same way a nil OAuthStore makes the proxy refuse a required OAuth
@@ -96,6 +89,8 @@ type Request struct {
// other.
Org string
GrantedServices []string
// RunCredential is the run's credential: the container's proxy password.
RunCredential string
}
// Run is a started Kit task.
@@ -126,19 +121,13 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
if err != nil {
return nil, err
}
granted, kinds := l.resolveCredentials(req, k.creds)
kinds := l.boundKinds(req, k.creds)
facts, err := l.oauthFacts(ctx, req.Org, k.creds, kinds)
if err != nil {
return nil, err
}
if l.Grants != nil {
l.Grants.Grant(req.Execution, granted)
}
session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
session, err := l.Proxy.Register(egress.SessionOptions{Token: req.RunCredential, Org: req.Org, Network: k.network, Credentials: k.creds, Bound: kinds})
if err != nil {
if l.Grants != nil {
l.Grants.RevokeGrant(req.Execution)
}
return nil, err
}
run := &Run{network: "archie-kit-" + req.Execution, token: session.Token(), execution: req.Execution}
@@ -174,19 +163,18 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
return run, nil
}
// resolveCredentials resolves the run's credentials: services declared by
// the Kit, bound in config, and granted to the identity's org. granted maps
// run/service to value; kinds maps service to bound kind. A binding with a
// secret is an API key; one without is the org's OAuth token set.
func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapability) (granted map[string]string, kinds map[string]egress.CredentialKind) {
granted = map[string]string{}
kinds = map[string]egress.CredentialKind{}
// boundKinds is the kind of each declared credential the run is bound for:
// declared by the Kit, granted to the run's identity, bound in its org, and
// for an API key, resolvable now. The proxy resolves the value itself per
// request, through the same intersection.
func (l *Launcher) boundKinds(req Request, creds []spec.CredentialCapability) map[string]egress.CredentialKind {
kinds := map[string]egress.CredentialKind{}
if l.Config == nil {
return granted, kinds
return kinds
}
current := l.Config.Get().Containers.Credentials
if len(current) == 0 {
return granted, kinds
return kinds
}
declared := make([]string, len(creds))
oauth := map[string]bool{}
@@ -196,26 +184,19 @@ func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapabi
}
bindings := config.ContainerConfig{Credentials: current}.BoundCredentials(req.Org, req.GrantedServices, declared)
for service, binding := range bindings {
if binding.Secret == (config.SecretRef{}) {
switch {
case binding.Secret == (config.SecretRef{}):
// No secret: only OAuth gives the service a value.
if !oauth[service] {
continue
if oauth[service] {
kinds[service] = egress.CredentialOAuth
}
case l.Secrets != nil:
if _, err := l.Secrets.Resolve(binding.Secret); err == nil {
kinds[service] = egress.CredentialAPIKey
}
granted[service] = ""
kinds[service] = egress.CredentialOAuth
continue
}
if l.Secrets == nil {
continue
}
value, err := l.Secrets.Resolve(binding.Secret)
if err != nil {
continue
}
granted[service] = value
kinds[service] = egress.CredentialAPIKey
}
return granted, kinds
return kinds
}
// oauthFacts reads the stored scopes and expiry for each OAuth-bound
@@ -238,14 +219,10 @@ func (l *Launcher) oauthFacts(ctx context.Context, org string, creds []spec.Cred
return facts, nil
}
// release undoes what Launch already did for run: the egress grant, then the
// session. Call sites past this point pass the network/container errors
// release undoes what Launch already did for run: its proxy session. Call sites past this point pass the network/container errors
// through their own cleanup; release only ever needs to run once per Launch
// failure, so it takes no error to join.
func (l *Launcher) release(run *Run) {
if l.Grants != nil {
l.Grants.RevokeGrant(run.execution)
}
l.Proxy.Revoke(run.token)
}