fix(secret): recover a panicking secret engine instead of crashing

Registry.Resolve and Getenv called engine.Resolve directly with no panic
recovery, so a defective interpreted engine (sops/Vault plugin) took the
whole daemon down at boot when resolving a provider credential
(provider_secrets.go's only caller has no recovery of its own). Wrap both
call sites in yaegiutil.Safe, the same recovery every other Yaegi
extension surface in this repo already uses.
This commit is contained in:
sam committed 2026-09-15 04:38:51 +10:00
1 parent 97a9ed7ea5
commit bc430fa50d
2 files changed
+54 -2

No files matched your search

+13 -2
View File
@@ -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
+41
View File
@@ -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)
}
}