egress: a table keyed by harness and provider
ProviderPolicy and Policy.Hash read one table of (harness, provider) rows instead of naming Claude and Codex (connectors spec 2.2). The provider's own CLI is its own harness; TensorX through Pi reaches api.tensorx.ai and nothing else. A Claude or Codex policy hashes exactly as before, so pinned manifests stay valid; a pair that is not a row is refused. A test checks each API-key provider's row is its upstream host.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+104
−11
internal/runner/egress/policy.go+46 −11internal/runner/egress/proxy_test.go+54 −0internal/runner/provider/types.go+4 −0
diff --git a/internal/runner/egress/policy.go b/internal/runner/egress/policy.go| index 8d576709..941813ad 100644 |
| --- a/internal/runner/egress/policy.go |
| +++ b/internal/runner/egress/policy.go |
| @@ -16,7 +16,42 @@ import ( |
| type Policy struct { |
| Provider string |
| - Hosts []string |
| + // Harness is set only when it is not the provider's own CLI, so the |
| + // hash of a Claude or Codex policy is what it always was. |
| + Harness string `json:",omitempty"` |
| + Hosts []string |
| +} |
| + |
| +// Route is one (harness, provider) pair the relay may carry. |
| +type Route struct{ Harness, Provider string } |
| + |
| +// routes is the egress table (connectors spec §2.2): for each harness |
| +// and provider, the only hosts its namespace reaches. The provider's own |
| +// CLI is its own harness. A metered connection reaches its provider's |
| +// API host and nothing else; a later connector adds a row here. |
| +var routes = map[Route][]string{ |
| + {"claude", "claude"}: {"api.anthropic.com"}, |
| + {"codex", "codex"}: {"chatgpt.com"}, |
| + {"pi", "tensorx"}: {"api.tensorx.ai"}, |
| +} |
| + |
| +// Routes lists the egress table, sorted, for the checks that compare it |
| +// with the providers' records. |
| +func Routes() map[Route][]string { |
| + out := map[Route][]string{} |
| + for r, hosts := range routes { |
| + out[r] = append([]string(nil), hosts...) |
| + } |
| + return out |
| +} |
| + |
| +func route(harness, provider string) (Route, []string, bool) { |
| + if harness == "" { |
| + harness = provider |
| + } |
| + r := Route{Harness: harness, Provider: provider} |
| + hosts, ok := routes[r] |
| + return r, hosts, ok |
| } |
| var ErrConflict = errors.New("egress_certified_auth_conflict") |
| @@ -29,15 +64,14 @@ func ProviderPolicy(c provider.NetworkContract, dedicated bool) (Policy, error) |
| if c.Conflict != "" { |
| return Policy{}, fmt.Errorf("%w: %s", ErrConflict, c.Conflict) |
| } |
| - p := Policy{Provider: c.Provider} |
| - switch c.Provider { |
| - case "claude": |
| - p.Hosts = []string{"api.anthropic.com"} |
| - case "codex": |
| - p.Hosts = []string{"chatgpt.com"} |
| - default: |
| + r, hosts, ok := route(c.Harness, c.Provider) |
| + if !ok { |
| return Policy{}, errPolicy |
| } |
| + p := Policy{Provider: c.Provider, Hosts: append([]string(nil), hosts...)} |
| + if r.Harness != r.Provider { |
| + p.Harness = r.Harness |
| + } |
| if c.AuthHost != "" { |
| return Policy{}, errPolicy |
| } |
| @@ -77,9 +111,10 @@ func hostname(s string) bool { |
| return true |
| } |
| -// Hash binds the exact host set and provider. The caller's slice is never retained. |
| +// Hash binds the exact host set, provider and harness, for a route in |
| +// the table. The caller's slice is never retained. |
| func (p Policy) Hash() (string, error) { |
| - if (p.Provider != "claude" && p.Provider != "codex") || len(p.Hosts) == 0 { |
| + if _, _, ok := route(p.Harness, p.Provider); !ok || len(p.Hosts) == 0 || p.Harness == p.Provider { |
| return "", errPolicy |
| } |
| hosts := append([]string(nil), p.Hosts...) |
| @@ -89,7 +124,7 @@ func (p Policy) Hash() (string, error) { |
| return "", errPolicy |
| } |
| } |
| - b, _ := json.Marshal(Policy{Provider: p.Provider, Hosts: hosts}) |
| + b, _ := json.Marshal(Policy{Provider: p.Provider, Harness: p.Harness, Hosts: hosts}) |
| hash := sha256.Sum256(b) |
| return hex.EncodeToString(hash[:]), nil |
| } |
diff --git a/internal/runner/egress/proxy_test.go b/internal/runner/egress/proxy_test.go| index 715efbb7..855ad647 100644 |
| --- a/internal/runner/egress/proxy_test.go |
| +++ b/internal/runner/egress/proxy_test.go |
| @@ -3,7 +3,9 @@ package egress |
| import ( |
| "bufio" |
| "context" |
| + "crypto/sha256" |
| "crypto/tls" |
| + "encoding/hex" |
| "errors" |
| "fmt" |
| "io" |
| @@ -11,6 +13,7 @@ import ( |
| "net/http" |
| "net/http/httptest" |
| "net/netip" |
| + "net/url" |
| "os" |
| "path/filepath" |
| "strings" |
| @@ -19,6 +22,7 @@ import ( |
| "time" |
| "amadan.net/oficina/equipo/internal/runner/provider" |
| + "amadan.net/oficina/equipo/internal/wire" |
| ) |
| func scratch(t *testing.T) string { |
| @@ -520,3 +524,53 @@ func TestEgressHeaderTimeout(t *testing.T) { |
| t.Fatal("incomplete header kept socket open") |
| } |
| } |
| + |
| +// TestEgressTableKeyedByHarnessAndProvider: the egress table (connectors |
| +// spec §2.2) gives a metered connection exactly its provider's host, and |
| +// nothing for a harness and provider that are not a row; the Claude and |
| +// Codex policies hash as they always did; and each API-key provider's |
| +// row is its upstream's host. |
| +func TestEgressTableKeyedByHarnessAndProvider(t *testing.T) { |
| + p, e := ProviderPolicy(provider.NetworkContract{Provider: "tensorx", Harness: "pi"}, true) |
| + if e != nil || p.Provider != "tensorx" || p.Harness != "pi" || len(p.Hosts) != 1 || p.Hosts[0] != "api.tensorx.ai" { |
| + t.Fatalf("tensorx through pi: %+v %v", p, e) |
| + } |
| + if _, e := p.Hash(); e != nil { |
| + t.Fatal(e) |
| + } |
| + for _, c := range []provider.NetworkContract{{Provider: "tensorx"}, {Provider: "tensorx", Harness: "claude"}, {Provider: "claude", Harness: "pi"}, {Provider: "codex", Harness: "pi"}, {Provider: "tensorx", Harness: "codex"}, {Provider: "tensorx", Harness: "pi", AuthHost: "auth.tensorx.ai"}} { |
| + if p, e := ProviderPolicy(c, true); e == nil { |
| + t.Fatalf("%+v admitted: %+v", c, p) |
| + } |
| + } |
| + for _, bad := range []Policy{{Provider: "tensorx", Hosts: []string{"api.tensorx.ai"}}, {Provider: "claude", Harness: "pi", Hosts: []string{"api.anthropic.com"}}, {Provider: "claude", Harness: "claude", Hosts: []string{"api.anthropic.com"}}} { |
| + if _, e := bad.Hash(); e == nil { |
| + t.Fatalf("hash of %+v", bad) |
| + } |
| + } |
| + // The Claude and Codex policies' hashes are those of the policy before |
| + // the table: the manifests that pin them stay valid. |
| + for name, host := range map[string]string{"claude": "api.anthropic.com", "codex": "chatgpt.com"} { |
| + p, _ := ProviderPolicy(provider.NetworkContract{Provider: name}, true) |
| + got, _ := p.Hash() |
| + sum := sha256.Sum256([]byte(`{"Provider":"` + name + `","Hosts":["` + host + `"]}`)) |
| + if got != hex.EncodeToString(sum[:]) || p.Harness != "" { |
| + t.Fatalf("%s policy hash changed: %s", name, got) |
| + } |
| + } |
| + a, _ := (Policy{Provider: "tensorx", Harness: "pi", Hosts: []string{"api.tensorx.ai"}}).Hash() |
| + b, _ := (Policy{Provider: "tensorx", Harness: "pi", Hosts: []string{"api.tensorx.ai", "other.example"}}).Hash() |
| + if a == "" || a == b { |
| + t.Fatal("hash does not bind the host set") |
| + } |
| + for name, kp := range wire.APIKeyProviders { |
| + u, err := url.Parse(kp.Upstream) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + hosts := Routes()[Route{Harness: kp.Harness, Provider: name}] |
| + if len(hosts) != 1 || hosts[0] != u.Hostname() || u.Scheme != "https" || kp.Endpoint != "https://"+u.Hostname()+":443" { |
| + t.Fatalf("%s: table %v, upstream %s, endpoint %s", name, hosts, kp.Upstream, kp.Endpoint) |
| + } |
| + } |
| +} |
diff --git a/internal/runner/provider/types.go b/internal/runner/provider/types.go| index c48fb26c..50afa2e0 100644 |
| --- a/internal/runner/provider/types.go |
| +++ b/internal/runner/provider/types.go |
| @@ -35,6 +35,10 @@ type NetworkContract struct { |
| Conflict string |
| Provider string |
| AuthHost string |
| + // Harness runs the provider's connection; "" is the provider's own |
| + // CLI (Claude, Codex). A metered connection names its harness (Pi |
| + // for TensorX), and the egress table is keyed by both. |
| + Harness string |
| } |
| type Identity struct { |