diff --git a/internal/secret/engine.go b/internal/secret/engine.go index 8324170ab..9146f3e14 100644 --- a/internal/secret/engine.go +++ b/internal/secret/engine.go @@ -22,6 +22,17 @@ import ( "github.com/samcharles93/archie-core/internal/yaegiutil" ) +// safeResolve calls e.Resolve(key) with panic recovery. An interpreted +// (Yaegi-loaded) engine is untrusted relative to the daemon's own +// logic: a defect in the engine itself must degrade the credential it +// was resolving, not crash the process that's resolving it -- e.g. at +// boot, in internal/app/archied/provider_secrets.go. +func safeResolve(e Engine, key string) (string, error) { + return yaegiutil.Safe(e.Name(), func() (string, error) { + return e.Resolve(key) + }) +} + // Engine resolves secret references into their plaintext values. // Implementations must be safe for concurrent use. type Engine interface { @@ -87,7 +98,7 @@ func (r *Registry) Getenv(key string) string { } r.mu.RUnlock() for _, engine := range engines { - value, err := engine.Resolve(key) + value, err := safeResolve(engine, key) value = strings.TrimSpace(value) if err != nil || value == "" { continue @@ -111,7 +122,7 @@ func (r *Registry) Resolve(ref SecretRef) (string, error) { if !ok { return "", fmt.Errorf("secret engine %q not registered", ref.Engine) } - return e.Resolve(ref.Key) + return safeResolve(e, ref.Key) } // LoadDir discovers and evaluates .go files in dir. Each file must export diff --git a/internal/secret/secret_test.go b/internal/secret/secret_test.go index 43e2398a9..37e5ac39b 100644 --- a/internal/secret/secret_test.go +++ b/internal/secret/secret_test.go @@ -167,3 +167,44 @@ func TestNewRegistryHasEnv(t *testing.T) { t.Error("NewRegistry must pre-register env engine") } } + +// panickyEngine simulates a loaded interpreted engine (see +// TestLoadDirEngineWithPanicInResolve) whose Resolve panics on every +// call -- a defect in the engine itself, not something the daemon +// can validate ahead of time. +type panickyEngine struct{ name string } + +func (e panickyEngine) Name() string { return e.name } +func (e panickyEngine) Version() string { return "1.0.0" } +func (e panickyEngine) Resolve(key string) (string, error) { + panic("boom") +} + +// TestRegistryResolveRecoversPanickingEngine pins the degrade-not-crash +// contract at the one production caller that matters: a provider +// credential resolved through a panicking secret engine (e.g. a broken +// interpreted sops/Vault plugin) must surface as an error, not take the +// whole daemon down during boot (internal/app/archied/provider_secrets.go +// calls Registry.Resolve directly with no recovery of its own). +func TestRegistryResolveRecoversPanickingEngine(t *testing.T) { + r := NewRegistry() + r.Register(panickyEngine{name: "panicky"}) + + _, err := r.Resolve(SecretRef{Engine: "panicky", Key: "k"}) + if err == nil { + t.Fatal("Resolve() with a panicking engine returned no error; want a recovered error") + } +} + +// TestRegistryGetenvRecoversPanickingEngine is the Getenv-path sibling: +// a panicking non-env engine consulted as a Getenv fallback must not +// crash the caller either. +func TestRegistryGetenvRecoversPanickingEngine(t *testing.T) { + t.Setenv("ARCHIE_TEST_PANICKY_KEY", "") + r := NewRegistry() + r.Register(panickyEngine{name: "panicky"}) + + if got := r.Getenv("ARCHIE_TEST_PANICKY_KEY"); got != "" { + t.Fatalf("Getenv() = %q, want empty string when the only fallback engine panics", got) + } +}