package archied import ( "context" "encoding/base64" "net" "net/http" "net/http/httptest" "strings" "testing" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/egress" "github.com/samcharles93/archie-core/internal/secret" ) // unboundResolver refuses every credential a session does not carry itself. type unboundResolver struct{} func (unboundResolver) Resolve(context.Context, string, string) (string, error) { return "", egress.ErrUnbound } // A provider credential recorded as a secret reference needs no credential // binding: modelEgress resolves it for the session while the container gets // only the sentinel. A provider that names only an api_key_env keeps resolving // through its org binding, and one with no binding is still refused. func TestModelEgressResolvesProviderSecretRefs(t *testing.T) { const realKey = "resolved-provider-key" t.Setenv("PROVIDER_KEY", realKey) cases := []struct { name string provider config.Provider bindings []config.CredentialBinding wantErr string wantEnv string }{ { name: "a secret reference needs no binding", provider: config.Provider{Class: "openai", APIKey: config.SecretRef{Engine: "env", Key: "PROVIDER_KEY"}}, wantEnv: config.ProviderKeyEnvName("used"), }, { name: "an env-only provider keeps its binding", provider: config.Provider{Class: "openai", APIKeyEnv: "PROVIDER_ENV_KEY"}, bindings: []config.CredentialBinding{{Service: "used", Org: "org-test", Secret: config.SecretRef{Engine: "env", Key: "PROVIDER_KEY"}}}, wantEnv: "PROVIDER_ENV_KEY", }, { name: "an env-only provider without a binding is refused", provider: config.Provider{Class: "openai", APIKeyEnv: "PROVIDER_ENV_KEY"}, wantErr: "no credential binding", }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { proxy := egress.NewProxy(nil, egress.ProxyOptions{ Resolver: unboundResolver{}, }) m := modelEgress{ proxy: proxy, secrets: secret.NewRegistry(), config: config.NewHolder(config.Config{Containers: config.ContainerConfig{Credentials: tc.bindings}}), } session, err := m.Open("run-credential", "org-test", map[string]config.Provider{"used": tc.provider}) if tc.wantErr != "" { if err == nil || !strings.Contains(err.Error(), tc.wantErr) { t.Fatalf("err = %v, want one naming %q", err, tc.wantErr) } return } if err != nil { t.Fatalf("open model egress: %v", err) } env := strings.Join(session.Env, "\n") if want := tc.wantEnv + "=" + egress.SentinelFor("used"); !strings.Contains(env, want) { t.Fatalf("session env %q does not carry %q", env, want) } if strings.Contains(env, realKey) { t.Fatalf("the provider key reached the container: %q", env) } }) } } // The key a provider's api_key_ref resolves to lives in the session, never in // the container: a request carrying the sentinel reaches the provider's host // with the real key, and the environment the container holds has only the // placeholder. func TestModelEgressSubstitutesTheProviderKey(t *testing.T) { t.Setenv("PROVIDER_KEY", "resolved-provider-key") var seen *http.Request upstream := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { seen = r })) defer upstream.Close() // The resolver answers nothing: a session key is what the substitution uses. proxy := egress.NewProxy(nil, egress.ProxyOptions{ Resolver: unboundResolver{}, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) { return (&net.Dialer{}).DialContext(ctx, "tcp", upstream.Listener.Addr().String()) }, }) m := modelEgress{proxy: proxy, secrets: secret.NewRegistry(), config: config.NewHolder(config.Config{})} providers := config.Config{ Models: map[string]string{"default": "alpha/model-one"}, Providers: map[string]config.Provider{ "alpha": {Class: "openai", BaseURL: "http://api.alpha.example", APIKey: config.SecretRef{Engine: "env", Key: "PROVIDER_KEY"}}, }, }.ModelProviders() session, err := m.Open("run-credential", "org-test", providers) if err != nil { t.Fatalf("open model egress: %v", err) } if env := strings.Join(session.Env, "\n"); !strings.Contains(env, egress.SentinelFor("alpha")) { t.Fatalf("session env %q does not place the sentinel", env) } req := httptest.NewRequestWithContext(context.Background(), http.MethodPost, "http://api.alpha.example/v1/chat", nil) req.Header.Set("Authorization", "Bearer "+egress.SentinelFor("alpha")) req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("archie:run-credential"))) rec := httptest.NewRecorder() proxy.ServeHTTP(rec, req) if rec.Code != http.StatusOK || seen == nil { t.Fatalf("request not forwarded: %d %q", rec.Code, rec.Body.String()) } if got := seen.Header.Get("Authorization"); got != "Bearer resolved-provider-key" { t.Fatalf("upstream saw %q, want the resolved key", got) } }