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.
109 lines
3.9 KiB
Go
109 lines
3.9 KiB
Go
package config
|
|
|
|
import "testing"
|
|
|
|
// A task's run carries only the providers its model aliases reach, and every
|
|
// credential-bearing provider names the variable a run reads its credential
|
|
// from: the operator's api_key_env, or a name derived from the provider so a
|
|
// container that holds only a sentinel still knows where to put it.
|
|
func TestModelProviders(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ref := SecretRef{Engine: "env", Key: "PROVIDER_KEY"}
|
|
cases := []struct {
|
|
name string
|
|
models map[string]string
|
|
providers map[string]Provider
|
|
want []string
|
|
wantEnv map[string]string // "" means no credential variable
|
|
}{
|
|
{
|
|
name: "an alias's provider keeps the operator's variable",
|
|
models: map[string]string{"default": "alpha/model-one", "fast": "beta/model-two"},
|
|
providers: map[string]Provider{"alpha": {Class: "openai", APIKeyEnv: "ALPHA_KEY"}, "beta": {Class: "gemini", APIKeyEnv: "BETA_KEY"}},
|
|
want: []string{"alpha", "beta"},
|
|
wantEnv: map[string]string{"alpha": "ALPHA_KEY", "beta": "BETA_KEY"},
|
|
},
|
|
{
|
|
name: "a provider no alias names is left out",
|
|
models: map[string]string{"default": "alpha/model-one"},
|
|
providers: map[string]Provider{"alpha": {Class: "openai", APIKeyEnv: "ALPHA_KEY"}, "unused": {Class: "gemini", APIKeyEnv: "UNUSED_KEY"}},
|
|
want: []string{"alpha"},
|
|
wantEnv: map[string]string{"alpha": "ALPHA_KEY"},
|
|
},
|
|
{
|
|
name: "a secret reference names a derived variable",
|
|
models: map[string]string{"default": "alpha/model-one", "fast": "beta/model-two"},
|
|
providers: map[string]Provider{"alpha": {Class: "openai", APIKey: ref}, "beta": {Class: "deepseek", APIKey: ref}},
|
|
want: []string{"alpha", "beta"},
|
|
wantEnv: map[string]string{"alpha": "<derived>", "beta": "<derived>"},
|
|
},
|
|
{
|
|
name: "a provider with no credential carries no variable",
|
|
models: map[string]string{"default": "alpha/model-one"},
|
|
providers: map[string]Provider{"alpha": {Class: "openai", BaseURL: "http://models.internal:11434"}},
|
|
want: []string{"alpha"},
|
|
wantEnv: map[string]string{"alpha": ""},
|
|
},
|
|
{
|
|
name: "an alias naming no configured provider carries nothing",
|
|
models: map[string]string{"default": "missing/model-one", "fast": "no-slash"},
|
|
providers: map[string]Provider{"alpha": {Class: "openai", APIKeyEnv: "ALPHA_KEY"}},
|
|
want: nil,
|
|
},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got := Config{Models: tc.models, Providers: tc.providers}.ModelProviders()
|
|
if len(got) != len(tc.want) {
|
|
t.Fatalf("providers = %v, want %v", keys(got), tc.want)
|
|
}
|
|
for _, name := range tc.want {
|
|
provider, ok := got[name]
|
|
if !ok {
|
|
t.Fatalf("provider %q missing from %v", name, keys(got))
|
|
}
|
|
wantEnv := tc.wantEnv[name]
|
|
switch wantEnv {
|
|
case "<derived>":
|
|
if provider.APIKeyEnv == "" {
|
|
t.Fatalf("provider %q with a secret reference has no credential variable", name)
|
|
}
|
|
default:
|
|
if provider.APIKeyEnv != wantEnv {
|
|
t.Fatalf("provider %q variable = %q, want %q", name, provider.APIKeyEnv, wantEnv)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The derived name belongs to the provider it was derived for: two providers
|
|
// never share one, and the same provider always gets the same name, so the
|
|
// daemon and the container agree without the key travelling.
|
|
func TestProviderKeyEnvNameIsPerProvider(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
alpha, beta := ProviderKeyEnvName("alpha"), ProviderKeyEnvName("beta")
|
|
if alpha == "" {
|
|
t.Fatal("ProviderKeyEnvName(alpha) is empty")
|
|
}
|
|
if alpha != ProviderKeyEnvName("alpha") {
|
|
t.Fatal("ProviderKeyEnvName is not stable for one provider")
|
|
}
|
|
if alpha == beta {
|
|
t.Fatalf("both providers share the variable %q", alpha)
|
|
}
|
|
}
|
|
|
|
func keys(providers map[string]Provider) []string {
|
|
out := make([]string, 0, len(providers))
|
|
for name := range providers {
|
|
out = append(out, name)
|
|
}
|
|
return out
|
|
}
|