fix(controlplane): keep an org's own provider credential reference

This commit is contained in:
sam committed 2026-10-22 17:31:54 +11:00
1 parent 11d8676024
commit 12cc06271d
2 files changed
+35 -20

No files matched your search

+13 -15
View File
@@ -125,19 +125,10 @@ func (s *Server) orgPolicy(ctx context.Context, orgID string) (*orgModelPolicy,
}
// instanceProviders is the providers the instance configured, which are the
// ones the system org's aliases name. The credential travels with the
// provider: the document holds it as api_key_ref, and without it every call
// that provider serves is unauthenticated.
// ones the system org's aliases name.
func (s *Server) instanceProviders(ctx context.Context) (map[string]config.Provider, error) {
var docs map[string]providerDocument
if err := s.readResource(ctx, storecontract.DefaultOrgID, ProviderSettingsKind, &docs); err != nil {
return nil, err
}
out := make(map[string]config.Provider, len(docs))
for name, doc := range docs {
out[name] = config.Provider{Class: doc.Class, BaseURL: doc.BaseURL, APIKeyEnv: doc.APIKeyEnv, APIKey: doc.APIKey}
}
return out, nil
out := map[string]config.Provider{}
return out, s.readProviders(ctx, storecontract.DefaultOrgID, out)
}
// orgProviders is orgID's own providers, empty unless its policy allows them.
@@ -146,14 +137,21 @@ func (s *Server) orgProviders(ctx context.Context, orgID string, policy *orgMode
if policy == nil || !policy.OwnProviders {
return out, nil
}
return out, s.readProviders(ctx, orgID, out)
}
// readProviders adds orgID's provider documents to out. The credential
// travels with the provider: without api_key_ref every call it serves is
// unauthenticated.
func (s *Server) readProviders(ctx context.Context, orgID string, out map[string]config.Provider) error {
var docs map[string]providerDocument
if err := s.readResource(ctx, orgID, ProviderSettingsKind, &docs); err != nil {
return nil, err
return err
}
for name, doc := range docs {
out[name] = config.Provider{Class: doc.Class, BaseURL: doc.BaseURL, APIKeyEnv: doc.APIKeyEnv}
out[name] = config.Provider{Class: doc.Class, BaseURL: doc.BaseURL, APIKeyEnv: doc.APIKeyEnv, APIKey: doc.APIKey}
}
return out, nil
return nil
}
func (s *Server) modelAliases(ctx context.Context, orgID string) (map[string]string, error) {
+22 -5
View File
@@ -51,7 +51,11 @@ func TestModelsForResolvesTheProvidersItsAliasesName(t *testing.T) {
"remote": {"class": "openai-compatible", "base_url": "https://models.example.com/v1", "api_key_ref": {"engine": "vault", "key": "providers/remote"}},
"local": {"class": "ollama", "api_key_env": "LOCAL_KEY"}
}`,
ModelAliasesKind: `{"default": "remote/large", "embedding": "local/embed"}`,
ModelAliasesKind: `{"default": "remote/large", "embedding": "local/embed"}`,
OrgModelPolicyKind: `{"beta": {"own_providers": true}}`,
}, "beta": {
ProviderSettingsKind: `{"own": {"class": "openai-compatible", "base_url": "https://own.example.com/v1", "api_key_ref": {"engine": "vault", "key": "providers/own"}}}`,
ModelAliasesKind: `{"default": "own/large"}`,
}}
tests := []struct {
name string
@@ -62,6 +66,7 @@ func TestModelsForResolvesTheProvidersItsAliasesName(t *testing.T) {
{name: "the system org holds the instance's aliases and providers", orgID: storecontract.DefaultOrgID, wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
{name: "no org named means the system org", orgID: "", wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
{name: "an org with no policy takes neither the instance's aliases nor its providers", orgID: "acme"},
{name: "an org allowed its own providers resolves them", orgID: "beta", wantAliases: []string{"default"}, wantProviders: []string{"own"}},
}
server := &Server{store: store}
for _, tt := range tests {
@@ -79,14 +84,26 @@ func TestModelsForResolvesTheProvidersItsAliasesName(t *testing.T) {
})
}
creds := []struct {
orgID, provider string
want config.SecretRef
}{
{orgID: storecontract.DefaultOrgID, provider: "remote", want: config.SecretRef{Engine: "vault", Key: "providers/remote"}},
{orgID: "beta", provider: "own", want: config.SecretRef{Engine: "vault", Key: "providers/own"}},
}
for _, c := range creds {
models, err := server.ModelsFor(t.Context(), c.orgID)
if err != nil {
t.Fatalf("models for %q: %v", c.orgID, err)
}
if got := models.Providers[c.provider].APIKey; got != c.want {
t.Fatalf("%s provider %s api_key_ref = %+v, want %+v (its only credential)", c.orgID, c.provider, got, c.want)
}
}
models, err := server.ModelsFor(t.Context(), storecontract.DefaultOrgID)
if err != nil {
t.Fatalf("models: %v", err)
}
want := config.Provider{Class: "openai-compatible", BaseURL: "https://models.example.com/v1", APIKey: config.SecretRef{Engine: "vault", Key: "providers/remote"}}
if got := models.Providers["remote"]; got != want {
t.Fatalf("remote provider = %+v, want %+v (the instance's api_key_ref is the only credential it has)", got, want)
}
if got := models.Providers["local"]; got.APIKeyEnv != "LOCAL_KEY" || got.Class != "ollama" {
t.Fatalf("local provider = %+v, want its class and api_key_env", got)
}