A container task parked when a provider carried an api_key_env but no credential binding named after it, even though the task's models never reached that provider. Egress pre-bound every provider the instance configures and ignored api_key_ref, so the credentials an operator stores in provider-settings were never used. A task's config now carries only the providers its model aliases resolve to, each with the variable its credential is read from: the operator's api_key_env, or a name derived from the provider when its credential is a secret reference. modelEgress resolves that reference once for the session and the proxy substitutes the session's key for the sentinel, so the container still holds no key. A provider that only declares an env var keeps resolving through its org credential binding.
114 lines
3.7 KiB
Go
114 lines
3.7 KiB
Go
package egress
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// A session's own resolved keys are substituted before the binding resolver is
|
|
// consulted: a run that carries a provider credential needs no credential
|
|
// binding for it. A service the session holds no key for still resolves through
|
|
// its binding, and an unbound one still stops the request.
|
|
func TestSubstitutionPrefersSessionKeys(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
host string
|
|
secrets map[string]string
|
|
resolve func() (string, error)
|
|
wantAuth string
|
|
wantStatus int
|
|
wantResolve int
|
|
}{
|
|
{
|
|
name: "the session key replaces the sentinel on the service host",
|
|
host: "api.alpha.example", secrets: map[string]string{"alpha": "session-key"},
|
|
wantAuth: "Bearer session-key", wantStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "the session key never reaches another host",
|
|
host: "attacker.example", secrets: map[string]string{"alpha": "session-key"},
|
|
wantAuth: "Bearer " + SentinelFor("alpha"), wantStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "a service with no session key resolves from its binding",
|
|
host: "api.beta.example", secrets: map[string]string{"alpha": "session-key"},
|
|
resolve: func() (string, error) { return "bound-key", nil },
|
|
wantAuth: "Bearer bound-key", wantStatus: http.StatusOK, wantResolve: 1,
|
|
},
|
|
{
|
|
name: "an unbound service with no session key stops the request",
|
|
host: "api.beta.example",
|
|
resolve: func() (string, error) { return "", ErrUnbound },
|
|
wantStatus: http.StatusBadGateway, wantResolve: 1,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var seen *http.Request
|
|
upstream := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { seen = r }))
|
|
defer upstream.Close()
|
|
|
|
resolved := 0
|
|
proxy := NewProxy(nil, ProxyOptions{
|
|
Resolver: ResolverFunc(func(_ context.Context, credential, _ string) (string, error) {
|
|
if credential != "host-run-grant" {
|
|
t.Error("resolver received the proxy password")
|
|
return "", ErrUnbound
|
|
}
|
|
resolved++
|
|
if tc.resolve == nil {
|
|
return "", ErrUnbound
|
|
}
|
|
return tc.resolve()
|
|
}),
|
|
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String())
|
|
},
|
|
})
|
|
service := "alpha"
|
|
if tc.host == "api.beta.example" {
|
|
service = "beta"
|
|
}
|
|
session, err := proxy.Register(SessionOptions{
|
|
Token: "proxy-password", RunCredential: "host-run-grant",
|
|
Substitutions: []Substitution{{Service: "alpha", Host: "api.alpha.example"}, {Service: "beta", Host: "api.beta.example"}},
|
|
Secrets: tc.secrets,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("register session: %v", err)
|
|
}
|
|
|
|
req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "http://"+tc.host+"/v1/chat", nil)
|
|
req.Header.Set("Authorization", "Bearer "+SentinelFor(service))
|
|
rec := httptest.NewRecorder()
|
|
proxy.forward(context.Background(), rec, req, session, "http", tc.host, 80)
|
|
|
|
if resolved != tc.wantResolve {
|
|
t.Fatalf("resolver called %d times, want %d", resolved, tc.wantResolve)
|
|
}
|
|
if rec.Code != tc.wantStatus {
|
|
t.Fatalf("status = %d, want %d (%s)", rec.Code, tc.wantStatus, rec.Body.String())
|
|
}
|
|
if tc.wantAuth == "" {
|
|
if seen != nil {
|
|
t.Fatal("a refused request was forwarded upstream")
|
|
}
|
|
return
|
|
}
|
|
if seen == nil {
|
|
t.Fatalf("request not forwarded: %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
if got := seen.Header.Get("Authorization"); got != tc.wantAuth {
|
|
t.Fatalf("upstream saw %q, want %q", got, tc.wantAuth)
|
|
}
|
|
})
|
|
}
|
|
}
|