fix(controlplane): reject stored boot-derived provider keys
This commit is contained in:
1 parent
235b9c1a87
commit
093e677ad8
4 files changed
+83
No files matched your search
@@ -1,10 +1,20 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
@@ -24,3 +34,40 @@ func TestConfiguredSecretRegistryKeepsProviderReferences(t *testing.T) {
|
||||
t.Fatalf("provider rewritten: api_key=%+v api_key_env=%q", got.APIKey, got.APIKeyEnv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateStoreBootSeedsProviderSourceReference(t *testing.T) {
|
||||
url := pgtest.URL(t)
|
||||
store := pgstore.On(pgstore.PoolAt(t, url))
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
ref := secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_PROVIDER_SOURCE"}
|
||||
t.Setenv(ref.Key, "provider-secret")
|
||||
body := fmt.Sprintf("bot_user = 'archie'\ndatabase_url = %q\n[providers.openai]\nclass = 'openai'\napi_key = {engine = 'env', key = %q}\n", url, ref.Key)
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- RunStateStore(ctx, StateStoreOptions{Config: path, Listen: "127.0.0.1:0"}) }()
|
||||
deadline := time.After(10 * time.Second)
|
||||
for {
|
||||
resource, err := store.Resource(t.Context(), controlplane.ProviderSettingsKind)
|
||||
if err == nil {
|
||||
if strings.Contains(string(resource.Value), "ARCHIE_PROVIDER_") || !strings.Contains(string(resource.Value), ref.Key) {
|
||||
t.Fatalf("stored provider settings = %s, want source reference and no derived env name", resource.Value)
|
||||
}
|
||||
break
|
||||
}
|
||||
select {
|
||||
case bootErr := <-errCh:
|
||||
t.Fatalf("RunStateStore failed before provider import: %v", bootErr)
|
||||
case <-deadline:
|
||||
t.Fatalf("provider settings were not imported: %v", err)
|
||||
case <-time.After(25 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
if err := <-errCh; err != nil && !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("RunStateStore shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package controlplane
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
@@ -13,6 +14,8 @@ const (
|
||||
ModelRoleAssignmentsKind = "model-role-assignments"
|
||||
)
|
||||
|
||||
var bootDerivedProviderEnv = regexp.MustCompile(`^ARCHIE_PROVIDER_[0-9A-F]{16}_API_KEY$`)
|
||||
|
||||
type providerDocument struct {
|
||||
Class string `json:"class"`
|
||||
APIKeyEnv string `json:"api_key_env,omitempty"`
|
||||
@@ -39,6 +42,9 @@ func seedProviders(cfg config.Config) any {
|
||||
func validateProviders(input []byte) error {
|
||||
return validateAs(input, func(providers map[string]providerDocument) error {
|
||||
for name, provider := range providers {
|
||||
if err := rejectBootDerivedProviderEnv(name, provider); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(name) == "" || strings.TrimSpace(provider.Class) == "" {
|
||||
return fmt.Errorf("provider name and class are required")
|
||||
}
|
||||
@@ -53,6 +59,13 @@ func validateProviders(input []byte) error {
|
||||
})
|
||||
}
|
||||
|
||||
func rejectBootDerivedProviderEnv(name string, provider providerDocument) error {
|
||||
if bootDerivedProviderEnv.MatchString(provider.APIKeyEnv) && provider.APIKey == (config.SecretRef{}) {
|
||||
return fmt.Errorf("provider %q api_key_env %q is a boot-derived name; replace provider-settings with the operator's api_key_ref", name, provider.APIKeyEnv)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateModelRoles(input []byte) error {
|
||||
return validateAs(input, func(models map[string]string) error {
|
||||
for role, model := range models {
|
||||
|
||||
@@ -104,6 +104,9 @@ func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReade
|
||||
}
|
||||
out.Providers = make(map[string]config.Provider, len(providers))
|
||||
for name, provider := range providers {
|
||||
if err := rejectBootDerivedProviderEnv(name, provider); err != nil {
|
||||
return err
|
||||
}
|
||||
out.Providers[name] = config.Provider{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -86,6 +86,26 @@ func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *t
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeConfigRejectsStoredBootDerivedProviderName(t *testing.T) {
|
||||
stored := []byte(`{"openai":{"class":"openai","api_key_env":"ARCHIE_PROVIDER_6986D5A05E1DF674_API_KEY"}}`)
|
||||
if err := validateProviders(stored); err == nil {
|
||||
t.Fatal("replace accepted a boot-derived provider name")
|
||||
}
|
||||
_, _, err := runtimeConfigFrom(t.Context(), providerSettingsReader{value: stored}, config.Config{})
|
||||
if err == nil || !strings.Contains(err.Error(), "ARCHIE_PROVIDER_6986D5A05E1DF674_API_KEY") || !strings.Contains(err.Error(), "replace provider-settings") {
|
||||
t.Fatalf("RuntimeConfig error = %v, want the stored derived name and repair action", err)
|
||||
}
|
||||
}
|
||||
|
||||
type providerSettingsReader struct{ value []byte }
|
||||
|
||||
func (r providerSettingsReader) Query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) {
|
||||
if kind != ProviderSettingsKind {
|
||||
return 0, false, nil
|
||||
}
|
||||
return 2, true, decode(r.value)
|
||||
}
|
||||
|
||||
// TestRuntimeConfigLayersTheStoredSchedulingLabel: the label pairs with the
|
||||
// dispatch trigger and layers with it. A policy that carries one replaces the
|
||||
// file document's label, so the store owns the half of the pairing it judged
|
||||
|
||||
Reference in new issue
Block a user