Merge crew/claude-997: OAuth token-endpoint interception and access-token injection in the egress proxy
This commit is contained in:
10 files changed
+658
-13
No files matched your search
@@ -142,7 +142,7 @@
|
||||
"url": "/dev/development/state-store/",
|
||||
"title": "State Store",
|
||||
"section": "Development",
|
||||
"body": "# State Store\n\nAuthority: `docs/prds/state-store-contract.md` and the State Store section of\n`CLAUDE.md`. All persistence is PostgreSQL behind the standalone State Store\nprocess; nothing else opens the database.\n\n## Adding a column\n\n1. Write the next migration in `internal/infrastructure/postgres/migrations/`\n (`00NN_name.sql`, goose Up and Down). Give the column a `NOT NULL DEFAULT`\n that is correct for existing rows.\n2. Update the queries in `internal/infrastructure/postgres/queries/` and\n regenerate with sqlc. A query that lists columns (`ListTaskSummaries`, the\n binding selects) does not pick the new one up; `SELECT *` queries do.\n3. Map it in the store (`taskFromRow`, `bindingValue` and friends). If the\n column holds encoded data, the package that owns the type owns the encoding\n (`task.EncodeInputs`), and the store and the wire both use it.\n4. Carry it over the wire (next section) if any process other than the State\n Store reads it.\n\n## Adding a field to a wire message\n\n1. Add it to `proto/state/v1/state.proto` with a new field number. Never reuse\n a number; retire one with `deprecated = true`.\n2. `task proto:generate`, then map it both ways in\n `internal/infrastructure/staterpc/values.go`.\n3. `TestTaskProtoCarriesEveryDomainField` fills every `task.Task` field and\n fails on one the mapping forgets. If it cannot fill the field's kind, extend\n it. Other messages have no such test, so add a round trip to\n `conformance_test.go`.\n\n## Adding an RPC or a store method\n\n1. Put the Go method on the narrow consumer interface in\n `internal/domain/storecontract` (keep each at eight methods or fewer) and add\n the matching RPC. A Go method without an RPC does not cross the process.\n2. Implement it in the Postgres store, in `staterpc.Client` (with its `var _`\n assertion) and in `staterpc.server`. A missing dependency answers\n `codes.Unavailable`, never a fabricated default.\n3. Wire the dependency in every composition that serves or consumes it:\n `stateStoreDeps` in `internal/app/archied/state_store.go`, the daemon's\n assertions in `bootstrap.go`, and `internal/app/archieui/compose.go` for the\n dashboard process.\n4. Task-scoped tokens may call only `Update`, `Transition` and `InsertEvent` on\n their own task (`staterpc.TaskGrants`). A new RPC is admin-only unless it is\n added there deliberately.\n5. Add it to `conformance_test.go`, which runs every surface over a real gRPC\n connection.\n\n## Adding an error sentinel\n\nAdd the sentinel to `storecontract`, a row to `wireErrors` in `values.go`, and\na case to `error_test.go`. The canonical message is part of the wire contract:\nchanging it breaks `errors.Is` on the client silently.\n\n## Harness OAuth secrets\n\n`HarnessSecretStore` stores token sets by org and credential service through\nadmin-only State Store RPCs. The EDA store seals them with the binding cipher's\nkeyring and a separate `HarnessSecretDomain`. OAuth writes require a configured\nbinding encryption key; the legacy binding plaintext fallback does not apply.\nThe proxy interception and setup terminal are separate harness slices.\n"
|
||||
"body": "# State Store\n\nAuthority: `docs/prds/state-store-contract.md` and the State Store section of\n`CLAUDE.md`. All persistence is PostgreSQL behind the standalone State Store\nprocess; nothing else opens the database.\n\n## Adding a column\n\n1. Write the next migration in `internal/infrastructure/postgres/migrations/`\n (`00NN_name.sql`, goose Up and Down). Give the column a `NOT NULL DEFAULT`\n that is correct for existing rows.\n2. Update the queries in `internal/infrastructure/postgres/queries/` and\n regenerate with sqlc. A query that lists columns (`ListTaskSummaries`, the\n binding selects) does not pick the new one up; `SELECT *` queries do.\n3. Map it in the store (`taskFromRow`, `bindingValue` and friends). If the\n column holds encoded data, the package that owns the type owns the encoding\n (`task.EncodeInputs`), and the store and the wire both use it.\n4. Carry it over the wire (next section) if any process other than the State\n Store reads it.\n\n## Adding a field to a wire message\n\n1. Add it to `proto/state/v1/state.proto` with a new field number. Never reuse\n a number; retire one with `deprecated = true`.\n2. `task proto:generate`, then map it both ways in\n `internal/infrastructure/staterpc/values.go`.\n3. `TestTaskProtoCarriesEveryDomainField` fills every `task.Task` field and\n fails on one the mapping forgets. If it cannot fill the field's kind, extend\n it. Other messages have no such test, so add a round trip to\n `conformance_test.go`.\n\n## Adding an RPC or a store method\n\n1. Put the Go method on the narrow consumer interface in\n `internal/domain/storecontract` (keep each at eight methods or fewer) and add\n the matching RPC. A Go method without an RPC does not cross the process.\n2. Implement it in the Postgres store, in `staterpc.Client` (with its `var _`\n assertion) and in `staterpc.server`. A missing dependency answers\n `codes.Unavailable`, never a fabricated default.\n3. Wire the dependency in every composition that serves or consumes it:\n `stateStoreDeps` in `internal/app/archied/state_store.go`, the daemon's\n assertions in `bootstrap.go`, and `internal/app/archieui/compose.go` for the\n dashboard process.\n4. Task-scoped tokens may call only `Update`, `Transition` and `InsertEvent` on\n their own task (`staterpc.TaskGrants`). A new RPC is admin-only unless it is\n added there deliberately.\n5. Add it to `conformance_test.go`, which runs every surface over a real gRPC\n connection.\n\n## Adding an error sentinel\n\nAdd the sentinel to `storecontract`, a row to `wireErrors` in `values.go`, and\na case to `error_test.go`. The canonical message is part of the wire contract:\nchanging it breaks `errors.Is` on the client silently.\n\n## Harness OAuth secrets\n\n`HarnessSecretStore` stores token sets by org and credential service through\nadmin-only State Store RPCs. The EDA store seals them with the binding cipher's\nkeyring and a separate `HarnessSecretDomain`. OAuth writes require a configured\nbinding encryption key; the legacy binding plaintext fallback does not apply.\nThe daemon's egress proxy (`internal/infrastructure/egress/oauth.go`) is the\nonly daemon consumer: for a run granted the service it swaps sentinels for the\nstored tokens and writes every refresh back. The setup terminal is a separate\nharness slice.\n"
|
||||
},
|
||||
{
|
||||
"url": "/dev/development/wiring/",
|
||||
|
||||
@@ -59,4 +59,7 @@ changing it breaks `errors.Is` on the client silently.
|
||||
admin-only State Store RPCs. The EDA store seals them with the binding cipher's
|
||||
keyring and a separate `HarnessSecretDomain`. OAuth writes require a configured
|
||||
binding encryption key; the legacy binding plaintext fallback does not apply.
|
||||
The proxy interception and setup terminal are separate harness slices.
|
||||
The daemon's egress proxy (`internal/infrastructure/egress/oauth.go`) is the
|
||||
only daemon consumer: for a run granted the service it swaps sentinels for the
|
||||
stored tokens and writes every refresh back. The setup terminal is a separate
|
||||
harness slice.
|
||||
@@ -63,7 +63,10 @@ func (b *boot) setupKitLauncher(ctx context.Context) {
|
||||
// per run with exactly the declared-and-granted intersection
|
||||
// (docs/prds/external-agent-harness.md, Credentials; archie-core-egkf.11).
|
||||
grants := egress.NewGrantResolver()
|
||||
proxy := egress.NewProxy(ca, egress.ProxyOptions{Resolver: grants})
|
||||
// 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})
|
||||
srv := &http.Server{Handler: proxy, ReadHeaderTimeout: 30 * time.Second}
|
||||
go func() {
|
||||
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
|
||||
@@ -898,7 +898,9 @@ type CredentialBinding struct {
|
||||
// however it is granted (docs/prds/orgs-and-access.md: "No record is
|
||||
// shared between orgs").
|
||||
Org string `toml:"org" yaml:"org"`
|
||||
// Secret is where the real value is resolved from.
|
||||
// Secret is where an API key's real value is resolved from. A service
|
||||
// the Kit declares OAuth-managed leaves it empty: its tokens are the
|
||||
// org's captured harness secret in the State Store.
|
||||
Secret SecretRef `toml:"secret" yaml:"secret"`
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
)
|
||||
|
||||
// maxTokenExchange bounds a token request or response body the proxy
|
||||
// buffers to rewrite; real ones are a few hundred bytes.
|
||||
const maxTokenExchange = 1 << 20
|
||||
|
||||
// OAuthStore persists one org's OAuth token set per credential binding
|
||||
// service (docs/prds/external-agent-harness.md, Credentials). It is
|
||||
// storecontract.HarnessSecretStore's method set, declared narrow here.
|
||||
type OAuthStore interface {
|
||||
GetHarnessSecret(ctx context.Context, org, service string) (harnesssecret.Secret, error)
|
||||
PutHarnessSecret(ctx context.Context, s harnesssecret.Secret) error
|
||||
}
|
||||
|
||||
// oauthRule is one compiled credential@1 OAuth credential: the token
|
||||
// endpoint the proxy intercepts, the resource hosts whose requests carry the
|
||||
// access token, the in-container sentinels and the provider's response
|
||||
// field names.
|
||||
type oauthRule struct {
|
||||
service string
|
||||
required bool
|
||||
runtime bool
|
||||
tokenHost pattern
|
||||
tokenPath string
|
||||
resourceHosts []pattern
|
||||
sentinels spec.Sentinels
|
||||
responseFields spec.ResponseFields
|
||||
}
|
||||
|
||||
// IsOAuthManaged reports whether the proxy holds c's tokens: an OAuth
|
||||
// credential with a token endpoint that is not a passthrough downgrade.
|
||||
func IsOAuthManaged(c spec.CredentialCapability) bool {
|
||||
return c.OAuth != nil && !c.OAuth.Passthrough && c.OAuth.TokenEndpoint != nil
|
||||
}
|
||||
|
||||
func compileOAuthRules(creds []spec.CredentialCapability) []oauthRule {
|
||||
var out []oauthRule
|
||||
for _, c := range creds {
|
||||
if !IsOAuthManaged(c) {
|
||||
continue
|
||||
}
|
||||
rule := oauthRule{
|
||||
service: c.Service, required: c.Required, runtime: c.Phase == "runtime",
|
||||
tokenHost: compilePattern(c.OAuth.TokenEndpoint.Host), tokenPath: c.OAuth.TokenEndpoint.Path,
|
||||
}
|
||||
for _, h := range c.OAuth.ResourceHosts {
|
||||
rule.resourceHosts = append(rule.resourceHosts, compilePattern(h))
|
||||
}
|
||||
if c.OAuth.Sentinels != nil {
|
||||
rule.sentinels = *c.OAuth.Sentinels
|
||||
}
|
||||
if c.OAuth.ResponseFields != nil {
|
||||
rule.responseFields = *c.OAuth.ResponseFields
|
||||
}
|
||||
out = append(out, rule)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// tokenEndpointRule returns the session's rule whose token endpoint r is a
|
||||
// POST to, for the request's current phase.
|
||||
func tokenEndpointRule(s *Session, r *http.Request, host string, port int) (oauthRule, bool) {
|
||||
if r.Method != http.MethodPost {
|
||||
return oauthRule{}, false
|
||||
}
|
||||
atRuntime := s.atRun.Load()
|
||||
for _, rule := range s.oauth {
|
||||
if rule.runtime != atRuntime || !rule.tokenHost.matches(normalizeHost(host), port) {
|
||||
continue
|
||||
}
|
||||
if rule.tokenPath != "" && r.URL.Path != rule.tokenPath {
|
||||
continue
|
||||
}
|
||||
return rule, true
|
||||
}
|
||||
return oauthRule{}, false
|
||||
}
|
||||
|
||||
// granted reports whether the run credential carries rule's service. The
|
||||
// org's stored token is read or written only for a granted run: a Kit
|
||||
// declaring a service does not by itself reach the org's secret. An optional
|
||||
// ungranted service is skipped; a required one is an error.
|
||||
func (p *Proxy) granted(ctx context.Context, s *Session, rule oauthRule) (bool, error) {
|
||||
var err error
|
||||
switch {
|
||||
case p.oauth == nil:
|
||||
err = errors.New("no harness secret store is configured")
|
||||
case p.resolver == nil:
|
||||
err = ErrUnbound
|
||||
default:
|
||||
_, err = p.resolver.Resolve(ctx, s.run, rule.service)
|
||||
}
|
||||
switch {
|
||||
case err == nil:
|
||||
return true, nil
|
||||
case errors.Is(err, ErrUnbound) && !rule.required:
|
||||
return false, nil
|
||||
case errors.Is(err, ErrUnbound):
|
||||
return false, fmt.Errorf("credential %q is required and not bound for this run", rule.service)
|
||||
default:
|
||||
return false, fmt.Errorf("credential %q: %w", rule.service, err)
|
||||
}
|
||||
}
|
||||
|
||||
// injectOAuth swaps the sentinel access token for the stored real one on a
|
||||
// request to one of a granted rule's resource hosts. A request not carrying
|
||||
// the sentinel never touches the store.
|
||||
func (p *Proxy) injectOAuth(ctx context.Context, s *Session, r *http.Request, host string, port int) error {
|
||||
atRuntime := s.atRun.Load()
|
||||
for _, rule := range s.oauth {
|
||||
if rule.runtime != atRuntime || rule.sentinels.AccessToken == "" ||
|
||||
!matchesAny(rule.resourceHosts, normalizeHost(host), port) || !headerCarries(r.Header, rule.sentinels.AccessToken) {
|
||||
continue
|
||||
}
|
||||
ok, err := p.granted(ctx, s, rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
current, err := p.oauth.GetHarnessSecret(ctx, s.org, rule.service)
|
||||
if err != nil {
|
||||
return fmt.Errorf("credential %q has no captured OAuth token: %w", rule.service, err)
|
||||
}
|
||||
for _, vs := range r.Header {
|
||||
for i, v := range vs {
|
||||
vs[i] = strings.ReplaceAll(v, rule.sentinels.AccessToken, current.AccessToken)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// interceptOAuth handles one request to a granted rule's token endpoint
|
||||
// whole. A refresh carrying the sentinel refresh token goes upstream with
|
||||
// the stored real one; a login code exchange carries none and passes as
|
||||
// sent. A successful response is captured to the store and reaches the
|
||||
// container with sentinels in place of the real tokens, so a refreshed
|
||||
// token is never returned to it.
|
||||
func (p *Proxy) interceptOAuth(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, rule oauthRule, scheme, target string) {
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, maxTokenExchange))
|
||||
if err != nil {
|
||||
http.Error(w, "read token request: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
var current harnesssecret.Secret
|
||||
if sentinel := rule.sentinels.RefreshToken; sentinel != "" && bytes.Contains(body, []byte(sentinel)) {
|
||||
current, err = p.oauth.GetHarnessSecret(ctx, s.org, rule.service)
|
||||
if err != nil {
|
||||
http.Error(w, "credential "+rule.service+" has no captured OAuth token (run the setup terminal first): "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
body = bytes.ReplaceAll(body, []byte(sentinel), []byte(current.RefreshToken))
|
||||
}
|
||||
out, err := http.NewRequestWithContext(ctx, http.MethodPost, scheme+"://"+target+r.URL.RequestURI(), bytes.NewReader(body))
|
||||
if err != nil {
|
||||
http.Error(w, "build upstream token request: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
out.Host = r.Host
|
||||
out.Header = r.Header.Clone()
|
||||
out.Header.Del("Proxy-Authorization")
|
||||
out.Header.Del("Proxy-Connection")
|
||||
// The response is parsed and rewritten here, so it must arrive as
|
||||
// plain JSON; the transport negotiates and decodes gzip on its own.
|
||||
out.Header.Del("Accept-Encoding")
|
||||
resp, err := p.transport.RoundTrip(out)
|
||||
if err != nil {
|
||||
http.Error(w, "upstream token endpoint "+target+": "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(io.LimitReader(resp.Body, maxTokenExchange))
|
||||
if err != nil {
|
||||
http.Error(w, "read token response: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
captured, err := rule.capture(s.org, respBody, current)
|
||||
if err == nil {
|
||||
err = p.oauth.PutHarnessSecret(ctx, captured)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "capture OAuth token: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
if respBody, err = rule.sentinelize(respBody); err != nil {
|
||||
http.Error(w, "rewrite token response: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
}
|
||||
for k, vs := range resp.Header {
|
||||
for _, v := range vs {
|
||||
w.Header().Add(k, v)
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(respBody)))
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
_, _ = w.Write(respBody)
|
||||
}
|
||||
|
||||
// responseFieldNames returns the provider's token-response field names,
|
||||
// defaulting to the OAuth 2.0 ones the rule does not override.
|
||||
func (rule oauthRule) responseFieldNames() (access, refresh, expiresIn string) {
|
||||
access, refresh, expiresIn = "access_token", "refresh_token", "expires_in"
|
||||
if rule.responseFields.AccessToken != "" {
|
||||
access = rule.responseFields.AccessToken
|
||||
}
|
||||
if rule.responseFields.RefreshToken != "" {
|
||||
refresh = rule.responseFields.RefreshToken
|
||||
}
|
||||
if rule.responseFields.ExpiresIn != "" {
|
||||
expiresIn = rule.responseFields.ExpiresIn
|
||||
}
|
||||
return access, refresh, expiresIn
|
||||
}
|
||||
|
||||
// capture parses a successful token response into the token set to store.
|
||||
// A provider that does not rotate refresh tokens omits the field, so the
|
||||
// previous refresh token is kept.
|
||||
func (rule oauthRule) capture(org string, body []byte, previous harnesssecret.Secret) (harnesssecret.Secret, error) {
|
||||
access, refresh, expiresIn := rule.responseFieldNames()
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal(body, &parsed); err != nil {
|
||||
return harnesssecret.Secret{}, fmt.Errorf("parse token response: %w", err)
|
||||
}
|
||||
secret := harnesssecret.Secret{Org: org, Service: rule.service, RefreshToken: previous.RefreshToken}
|
||||
secret.AccessToken, _ = parsed[access].(string)
|
||||
if v, ok := parsed[refresh].(string); ok && v != "" {
|
||||
secret.RefreshToken = v
|
||||
}
|
||||
secret.TokenType, _ = parsed["token_type"].(string)
|
||||
if v, ok := parsed[expiresIn].(float64); ok {
|
||||
secret.ExpiresAt = time.Now().Add(time.Duration(v) * time.Second)
|
||||
}
|
||||
if secret.AccessToken == "" {
|
||||
return harnesssecret.Secret{}, fmt.Errorf("token response has no %q field", access)
|
||||
}
|
||||
return secret, nil
|
||||
}
|
||||
|
||||
// sentinelize replaces a captured response's access and refresh tokens with
|
||||
// the Kit's sentinels. A token field with no declared sentinel cannot be
|
||||
// hidden, so it fails rather than hand the container a real token.
|
||||
func (rule oauthRule) sentinelize(body []byte) ([]byte, error) {
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal(body, &parsed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
access, refresh, _ := rule.responseFieldNames()
|
||||
for field, sentinel := range map[string]string{access: rule.sentinels.AccessToken, refresh: rule.sentinels.RefreshToken} {
|
||||
if _, ok := parsed[field]; !ok {
|
||||
continue
|
||||
}
|
||||
if sentinel == "" {
|
||||
return nil, fmt.Errorf("credential %q declares no sentinel for %q", rule.service, field)
|
||||
}
|
||||
parsed[field] = sentinel
|
||||
}
|
||||
return json.Marshal(parsed)
|
||||
}
|
||||
|
||||
func matchesAny(patterns []pattern, host string, port int) bool {
|
||||
for _, p := range patterns {
|
||||
if p.matches(host, port) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func headerCarries(h http.Header, value string) bool {
|
||||
for _, vs := range h {
|
||||
for _, v := range vs {
|
||||
if strings.Contains(v, value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/sandbox-kit-spec/v3/spec"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
type fakeOAuthStore struct {
|
||||
mu sync.Mutex
|
||||
secrets map[string]harnesssecret.Secret
|
||||
}
|
||||
|
||||
func newFakeOAuthStore() *fakeOAuthStore {
|
||||
return &fakeOAuthStore{secrets: map[string]harnesssecret.Secret{}}
|
||||
}
|
||||
|
||||
func (f *fakeOAuthStore) GetHarnessSecret(_ context.Context, org, service string) (harnesssecret.Secret, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
s, ok := f.secrets[org+"/"+service]
|
||||
if !ok {
|
||||
return harnesssecret.Secret{}, storecontract.ErrHarnessSecretNotFound
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (f *fakeOAuthStore) PutHarnessSecret(_ context.Context, s harnesssecret.Secret) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.secrets[s.Org+"/"+s.Service] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
// tokenEndpoint is the fake provider token endpoint: it records the body the
|
||||
// proxy sent upstream and answers with a configured response.
|
||||
type tokenEndpoint struct {
|
||||
mu sync.Mutex
|
||||
lastBody []byte
|
||||
hits int
|
||||
response string
|
||||
}
|
||||
|
||||
func (e *tokenEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
e.lastBody, e.hits = body, e.hits+1
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, e.response)
|
||||
}
|
||||
|
||||
func (e *tokenEndpoint) seen() (string, int) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
return string(e.lastBody), e.hits
|
||||
}
|
||||
|
||||
const (
|
||||
sentinelAccess = "sentinel-access"
|
||||
sentinelRefresh = "sentinel-refresh"
|
||||
)
|
||||
|
||||
func oauthCred(required bool) spec.CredentialCapability {
|
||||
return spec.CredentialCapability{
|
||||
Required: required,
|
||||
Service: "claude-code", Phase: "runtime",
|
||||
OAuth: &spec.OAuth{
|
||||
TokenEndpoint: &spec.TokenEndpoint{Host: "oauth.example.com", Path: "/token"},
|
||||
ResourceHosts: []string{"api.example.com"},
|
||||
Sentinels: &spec.Sentinels{AccessToken: sentinelAccess, RefreshToken: sentinelRefresh},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// oauthSession registers run-7 in org-1 with one OAuth credential, granted
|
||||
// to the run when grant is set.
|
||||
func (h *harness) oauthSession(t *testing.T, required, grant bool) *Session {
|
||||
t.Helper()
|
||||
if grant {
|
||||
h.bind("run-7/claude-code", "")
|
||||
}
|
||||
s, err := h.proxy.Register(SessionOptions{
|
||||
Run: "run-7", Org: "org-1",
|
||||
Network: &spec.PhasedNetwork{Runtime: &spec.NetworkRules{Allow: []string{"oauth.example.com:443", "api.example.com:443"}}},
|
||||
Credentials: []spec.CredentialCapability{oauthCred(required)},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.EnterRuntime()
|
||||
return s
|
||||
}
|
||||
|
||||
func postToken(t *testing.T, h *harness, s *Session, form string) (int, map[string]any) {
|
||||
t.Helper()
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "https://oauth.example.com/token", strings.NewReader(form))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := h.client(s.Token()).Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
var out map[string]any
|
||||
_ = json.Unmarshal(body, &out)
|
||||
return resp.StatusCode, out
|
||||
}
|
||||
|
||||
func TestOAuthRefreshUsesTheStoredTokenAndReturnsOnlySentinels(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
response string
|
||||
wantRefresh string
|
||||
}{
|
||||
{
|
||||
name: "rotating provider",
|
||||
response: `{"access_token":"new-access","refresh_token":"new-refresh","expires_in":3600,"token_type":"Bearer"}`,
|
||||
wantRefresh: "new-refresh",
|
||||
},
|
||||
{
|
||||
name: "provider keeps the refresh token",
|
||||
response: `{"access_token":"new-access","expires_in":3600}`,
|
||||
wantRefresh: "real-refresh",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
h.token.response = tt.response
|
||||
_ = h.oauth.PutHarnessSecret(t.Context(), harnesssecret.Secret{Org: "org-1", Service: "claude-code", AccessToken: "old-access", RefreshToken: "real-refresh"})
|
||||
s := h.oauthSession(t, true, true)
|
||||
|
||||
status, out := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
|
||||
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", status)
|
||||
}
|
||||
if sent, _ := h.token.seen(); sent != "grant_type=refresh_token&refresh_token=real-refresh" {
|
||||
t.Fatalf("upstream body = %q, want the stored refresh token in place of the sentinel", sent)
|
||||
}
|
||||
if out["access_token"] != sentinelAccess {
|
||||
t.Fatalf("container got access_token %v, want the sentinel", out["access_token"])
|
||||
}
|
||||
if _, rotated := out["refresh_token"]; rotated && out["refresh_token"] != sentinelRefresh {
|
||||
t.Fatalf("container got refresh_token %v, want the sentinel", out["refresh_token"])
|
||||
}
|
||||
stored, _ := h.oauth.GetHarnessSecret(t.Context(), "org-1", "claude-code")
|
||||
if stored.AccessToken != "new-access" || stored.RefreshToken != tt.wantRefresh || stored.ExpiresAt.IsZero() {
|
||||
t.Fatalf("stored = %+v, want access new-access, refresh %s and an expiry", stored, tt.wantRefresh)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthLoginExchangeCapturesWithNoStoredToken(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
h.token.response = `{"access_token":"first-access","refresh_token":"first-refresh"}`
|
||||
s := h.oauthSession(t, true, true)
|
||||
|
||||
status, out := postToken(t, h, s, "grant_type=authorization_code&code=abc")
|
||||
|
||||
if status != http.StatusOK || out["refresh_token"] != sentinelRefresh {
|
||||
t.Fatalf("status %d, body %v; want 200 with sentinels", status, out)
|
||||
}
|
||||
stored, err := h.oauth.GetHarnessSecret(t.Context(), "org-1", "claude-code")
|
||||
if err != nil || stored.RefreshToken != "first-refresh" {
|
||||
t.Fatalf("stored = %+v, %v; want the captured login", stored, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthRefreshWithNoStoredTokenNeverReachesUpstream(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
s := h.oauthSession(t, true, true)
|
||||
|
||||
status, _ := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
|
||||
|
||||
if status != http.StatusBadGateway {
|
||||
t.Fatalf("status = %d, want 502", status)
|
||||
}
|
||||
if _, hits := h.token.seen(); hits != 0 {
|
||||
t.Fatalf("upstream hit %d times, want 0", hits)
|
||||
}
|
||||
}
|
||||
|
||||
// An org's stored token is read and written only for a run whose credential
|
||||
// carries the service; declaring it in the Kit is not enough.
|
||||
func TestOAuthUngrantedRunNeitherReadsNorWritesTheOrgToken(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
required bool
|
||||
wantStatus int
|
||||
wantHits int
|
||||
}{
|
||||
{name: "required", required: true, wantStatus: http.StatusBadGateway, wantHits: 0},
|
||||
{name: "optional passes through untouched", required: false, wantStatus: http.StatusOK, wantHits: 1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
h.token.response = `{"access_token":"attacker-access","refresh_token":"attacker-refresh"}`
|
||||
seed := harnesssecret.Secret{Org: "org-1", Service: "claude-code", AccessToken: "real-access", RefreshToken: "real-refresh"}
|
||||
_ = h.oauth.PutHarnessSecret(t.Context(), seed)
|
||||
s := h.oauthSession(t, tt.required, false)
|
||||
|
||||
status, _ := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
|
||||
|
||||
sent, hits := h.token.seen()
|
||||
if status != tt.wantStatus || hits != tt.wantHits {
|
||||
t.Fatalf("status %d with %d upstream hits, want %d with %d", status, hits, tt.wantStatus, tt.wantHits)
|
||||
}
|
||||
if strings.Contains(sent, "real-refresh") {
|
||||
t.Fatalf("upstream saw the org's refresh token: %q", sent)
|
||||
}
|
||||
if stored, _ := h.oauth.GetHarnessSecret(t.Context(), "org-1", "claude-code"); stored != seed {
|
||||
t.Fatalf("stored = %+v, want it unchanged", stored)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOAuthResourceRequestCarriesTheStoredAccessToken(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
_ = h.oauth.PutHarnessSecret(t.Context(), harnesssecret.Secret{Org: "org-1", Service: "claude-code", AccessToken: "real-access"})
|
||||
s := h.oauthSession(t, true, true)
|
||||
|
||||
req, _ := http.NewRequestWithContext(t.Context(), http.MethodGet, "https://api.example.com/v1/messages", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+sentinelAccess)
|
||||
resp, err := h.client(s.Token()).Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
|
||||
if got := h.lastAuth.Load(); got != "Bearer real-access" {
|
||||
t.Fatalf("upstream Authorization = %v, want the stored access token", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRefusesARequiredOAuthCredentialWithNoStore(t *testing.T) {
|
||||
ca, err := LoadOrCreateCA(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := NewProxy(ca, ProxyOptions{})
|
||||
if _, err := p.Register(SessionOptions{Run: "run-7", Org: "org-1", Credentials: []spec.CredentialCapability{oauthCred(true)}}); err == nil {
|
||||
t.Fatal("Register() succeeded with a required OAuth credential and no store")
|
||||
}
|
||||
}
|
||||
|
||||
func newTokenEndpoint(t *testing.T) (*tokenEndpoint, *httptest.Server) {
|
||||
t.Helper()
|
||||
e := &tokenEndpoint{}
|
||||
srv := httptest.NewTLSServer(e)
|
||||
t.Cleanup(srv.Close)
|
||||
return e, srv
|
||||
}
|
||||
@@ -37,9 +37,11 @@ var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)}
|
||||
type Session struct {
|
||||
token string
|
||||
run string
|
||||
org string
|
||||
install rules
|
||||
runtime rules
|
||||
injections []injection
|
||||
oauth []oauthRule
|
||||
atRun atomic.Bool
|
||||
}
|
||||
|
||||
@@ -66,12 +68,17 @@ type ProxyOptions struct {
|
||||
// Resolver supplies credentials for injection. Without one, a request
|
||||
// that needs a required credential is refused.
|
||||
Resolver Resolver
|
||||
// OAuthStore holds the org token sets OAuth credentials use. Without
|
||||
// one, Register refuses a session with a required OAuth credential.
|
||||
OAuthStore OAuthStore
|
||||
}
|
||||
|
||||
// SessionOptions describe one container's run: the run credential it acts
|
||||
// under, and its Kit's network policy and credential requests.
|
||||
// under, the org whose OAuth token sets it uses, and its Kit's network
|
||||
// policy and credential requests.
|
||||
type SessionOptions struct {
|
||||
Run string
|
||||
Org string
|
||||
Network *spec.PhasedNetwork
|
||||
Credentials []spec.CredentialCapability
|
||||
}
|
||||
@@ -85,6 +92,7 @@ type Proxy struct {
|
||||
dial func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||
transport *http.Transport
|
||||
resolver Resolver
|
||||
oauth OAuthStore
|
||||
|
||||
mu sync.RWMutex
|
||||
sessions map[string]*Session
|
||||
@@ -93,7 +101,7 @@ type Proxy struct {
|
||||
type sessionKey struct{}
|
||||
|
||||
func NewProxy(ca *CA, opts ProxyOptions) *Proxy {
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, sessions: map[string]*Session{}}
|
||||
p := &Proxy{ca: ca, dial: opts.Dial, resolver: opts.Resolver, oauth: opts.OAuthStore, sessions: map[string]*Session{}}
|
||||
p.transport = &http.Transport{
|
||||
DialContext: p.dialUpstream,
|
||||
TLSClientConfig: &tls.Config{RootCAs: opts.UpstreamRoots, MinVersion: tls.VersionTLS12},
|
||||
@@ -110,7 +118,15 @@ func (p *Proxy) Register(opts SessionOptions) (*Session, error) {
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &Session{token: hex.EncodeToString(raw), run: opts.Run, injections: compileInjections(opts.Credentials)}
|
||||
s := &Session{
|
||||
token: hex.EncodeToString(raw), run: opts.Run, org: opts.Org,
|
||||
injections: compileInjections(opts.Credentials), oauth: compileOAuthRules(opts.Credentials),
|
||||
}
|
||||
for _, rule := range s.oauth {
|
||||
if rule.required && p.oauth == nil {
|
||||
return nil, fmt.Errorf("credential %q is OAuth-managed and no harness secret store is configured", rule.service)
|
||||
}
|
||||
}
|
||||
if opts.Network != nil {
|
||||
s.install, s.runtime = compileRules(opts.Network.Install), compileRules(opts.Network.Runtime)
|
||||
}
|
||||
@@ -233,9 +249,24 @@ func (p *Proxy) serveConnect(w http.ResponseWriter, r *http.Request, s *Session)
|
||||
|
||||
func (p *Proxy) forward(ctx context.Context, w http.ResponseWriter, r *http.Request, s *Session, scheme, host string, port int) {
|
||||
target := net.JoinHostPort(host, strconv.Itoa(port))
|
||||
if rule, ok := tokenEndpointRule(s, r, host, port); ok {
|
||||
granted, err := p.granted(ctx, s, rule)
|
||||
if err != nil {
|
||||
http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
if granted {
|
||||
p.interceptOAuth(ctx, w, r, s, rule, scheme, target)
|
||||
return
|
||||
}
|
||||
}
|
||||
// Injection happens before anything is sent, so a credential that
|
||||
// cannot be resolved never reaches upstream half-applied.
|
||||
if err := p.inject(ctx, s, r, host, port); err != nil {
|
||||
err := p.inject(ctx, s, r, host, port)
|
||||
if err == nil {
|
||||
err = p.injectOAuth(ctx, s, r, host, port)
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "egress to "+target+": "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -32,11 +32,13 @@ type harness struct {
|
||||
secrets map[string]string
|
||||
resolved []string
|
||||
resolveErr error
|
||||
oauth *fakeOAuthStore
|
||||
token *tokenEndpoint
|
||||
}
|
||||
|
||||
func newHarness(t *testing.T) *harness {
|
||||
t.Helper()
|
||||
h := &harness{secrets: map[string]string{}}
|
||||
h := &harness{secrets: map[string]string{}, oauth: newFakeOAuthStore()}
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h.hits.Add(1)
|
||||
h.lastAuth.Store(r.Header.Get("Authorization"))
|
||||
@@ -54,15 +56,19 @@ func newHarness(t *testing.T) *harness {
|
||||
}
|
||||
upstreamRoots := x509.NewCertPool()
|
||||
upstreamRoots.AddCert(h.upstream.Certificate())
|
||||
token, tokenSrv := newTokenEndpoint(t)
|
||||
h.token = token
|
||||
routes := map[string]string{
|
||||
"api.example.com:443": h.upstream.Listener.Addr().String(),
|
||||
"install.example.com:443": h.upstream.Listener.Addr().String(),
|
||||
"denied.example.com:443": h.upstream.Listener.Addr().String(),
|
||||
"plain.example.com:80": h.plain.Listener.Addr().String(),
|
||||
"oauth.example.com:443": tokenSrv.Listener.Addr().String(),
|
||||
}
|
||||
h.proxy = NewProxy(ca, ProxyOptions{
|
||||
UpstreamRoots: upstreamRoots,
|
||||
Resolver: ResolverFunc(h.resolve),
|
||||
OAuthStore: h.oauth,
|
||||
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
target, ok := routes[addr]
|
||||
if !ok {
|
||||
|
||||
@@ -137,7 +137,7 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
if l.Grants != nil {
|
||||
l.Grants.Grant(req.Execution, granted)
|
||||
}
|
||||
session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Network: network, Credentials: creds})
|
||||
session, err := l.Proxy.Register(egress.SessionOptions{Run: req.Execution, Org: req.Org, Network: network, Credentials: creds})
|
||||
if err != nil {
|
||||
if l.Grants != nil {
|
||||
l.Grants.RevokeGrant(req.Execution)
|
||||
@@ -185,10 +185,12 @@ func (l *Launcher) Launch(ctx context.Context, req Request) (*Run, error) {
|
||||
// renders sentinel-mode env vars from. A service failing any part of the
|
||||
// intersection, or whose binding's secret does not resolve, is simply
|
||||
// absent from both: it stays unbound, exactly as if credential@1 named a
|
||||
// service nobody configured.
|
||||
// service nobody configured. An OAuth-managed service is granted with no
|
||||
// value: its tokens are the org's State Store secret, which the proxy reads
|
||||
// only for a granted run.
|
||||
func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapability) (granted map[string]string, bound []string) {
|
||||
granted = map[string]string{}
|
||||
if l.Secrets == nil || l.Config == nil {
|
||||
if l.Config == nil {
|
||||
return granted, bound
|
||||
}
|
||||
current := l.Config.Get().Containers.Credentials
|
||||
@@ -196,11 +198,21 @@ func (l *Launcher) resolveCredentials(req Request, creds []spec.CredentialCapabi
|
||||
return granted, bound
|
||||
}
|
||||
declared := make([]string, len(creds))
|
||||
oauth := map[string]bool{}
|
||||
for i, c := range creds {
|
||||
declared[i] = c.Service
|
||||
oauth[c.Service] = egress.IsOAuthManaged(c)
|
||||
}
|
||||
bindings := config.ContainerConfig{Credentials: current}.BoundCredentials(req.Org, req.GrantedServices, declared)
|
||||
for service, binding := range bindings {
|
||||
if oauth[service] {
|
||||
granted[service] = ""
|
||||
bound = append(bound, service)
|
||||
continue
|
||||
}
|
||||
if l.Secrets == nil {
|
||||
continue
|
||||
}
|
||||
value, err := l.Secrets.Resolve(binding.Secret)
|
||||
if err != nil {
|
||||
continue
|
||||
|
||||
@@ -25,6 +25,12 @@ func declaredCred(service string) spec.CredentialCapability {
|
||||
return spec.CredentialCapability{Service: service}
|
||||
}
|
||||
|
||||
func declaredOAuth(service string) spec.CredentialCapability {
|
||||
c := declaredCred(service)
|
||||
c.OAuth = &spec.OAuth{TokenEndpoint: &spec.TokenEndpoint{Host: "auth.example.com"}}
|
||||
return c
|
||||
}
|
||||
|
||||
// TestResolveCredentialsReadsTheLiveConfig is the "reads current, not a boot
|
||||
// snapshot" requirement: two calls against the same Launcher, differing only
|
||||
// in what l.Config now holds, must produce different results. A Launcher
|
||||
@@ -65,6 +71,7 @@ func TestResolveCredentialsIsAnIntersection(t *testing.T) {
|
||||
Credentials: []config.CredentialBinding{
|
||||
{Service: "openai", Org: "acme", Secret: openaiRef},
|
||||
{Service: "other-org-svc", Org: "other-org", Secret: otherRef},
|
||||
{Service: "claude-code", Org: "acme"},
|
||||
},
|
||||
}}),
|
||||
Secrets: fakeSecretResolver{openaiRef: "sk-acme", otherRef: "sk-other"},
|
||||
@@ -95,6 +102,18 @@ func TestResolveCredentialsIsAnIntersection(t *testing.T) {
|
||||
creds: []spec.CredentialCapability{declaredCred("other-org-svc")},
|
||||
want: map[string]string{},
|
||||
},
|
||||
{
|
||||
name: "an OAuth service is granted without a config secret",
|
||||
req: Request{Org: "acme", GrantedServices: []string{"claude-code"}},
|
||||
creds: []spec.CredentialCapability{declaredOAuth("claude-code")},
|
||||
want: map[string]string{"claude-code": ""},
|
||||
},
|
||||
{
|
||||
name: "an ungranted OAuth service never resolves",
|
||||
req: Request{Org: "acme", GrantedServices: nil},
|
||||
creds: []spec.CredentialCapability{declaredOAuth("claude-code")},
|
||||
want: map[string]string{},
|
||||
},
|
||||
{
|
||||
name: "not declared by the Kit never resolves even though granted",
|
||||
req: Request{Org: "acme", GrantedServices: []string{"openai"}},
|
||||
@@ -109,7 +128,7 @@ func TestResolveCredentialsIsAnIntersection(t *testing.T) {
|
||||
t.Fatalf("resolveCredentials() = %v, want %v", granted, tt.want)
|
||||
}
|
||||
for service, value := range tt.want {
|
||||
if granted[service] != value {
|
||||
if got, ok := granted[service]; !ok || got != value {
|
||||
t.Fatalf("resolveCredentials()[%q] = %q, want %q", service, granted[service], value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user