Files
kandev/apps/backend/internal/plugins/store/fs_store.go
T
João Salavisa fae2303db3 feat(plugins): per-plugin settings pages driven by manifest config_schema (#1761)
* feat(plugins): per-plugin settings pages driven by manifest config_schema

Settings > Plugins rows now open /settings/plugins/:id — a detail page
showing the manifest plus a settings form generated from the plugin
author's config_schema. Secret fields (secret: true / format: password)
are masked on the operator API and kept on masked re-submits; a running
plugin restarts on save and reads its config (cleartext secrets included)
through the new Host GetConfig gRPC RPC.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): write plugin config file owner-only (0600)

Operator config can now carry cleartext secret values (config_schema
fields marked secret), so <id>.config.yml must not be world-readable —
match the 0600 mode the record file already uses.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(plugins): vault-backed plugin secrets — Get/Set/DeleteSecret RPCs and encrypted config secrets

Plugins get vault-backed secret primitives on the Host API
(GetSecret/SetSecret/DeleteSecret, capability-gated, namespaced to
plugin:<id>㊙️<key> so a plugin can only touch its own entries;
purged on uninstall). Secret config_schema fields now live in kandev's
AES-256-GCM vault too — <id>.config.yml persists only a vault reference,
and the Host GetConfig RPC resolves it back to cleartext for the plugin.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(plugins): serve PR screenshots from the media/pr-1761-screenshots ref instead of the repo

The PR body now points at raw URLs pinned to an unmerged media ref, so
the binaries never land in main (PRs are squash-merged; only the branch
tip tree merges).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): address review feedback on settings validation and save flow

- integer form fields reject non-integral input instead of parseInt-truncating
- enum selections keep their schema-declared types (numeric enums round-trip)
- backend enum validation normalizes int-vs-float64 before comparison
- maskSecrets masks any non-zero secret value regardless of runtime type
- save flow splits PATCH from refetch: a refetch failure no longer reports a
  successful save as an error, and typed secrets are masked on screen
- optional enum fields gain a 'Not set' option so they can be cleared
- resolveConfigSecrets copies before mutating; restart-failure SetStatus
  errors are logged; SDK test fake returns a non-nil empty config map
- add missing tests: usePluginConfigForm hook, getPluginConfig client,
  integer/enum regression cases

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): harden secret config invariants from review

- reject non-string values on declared secret fields at validation time,
  closing the path where a numeric/boolean secret bypassed the vault and
  persisted cleartext in the config file
- delete removed secret fields' vault entries only AFTER the config commit
  succeeds, so a failed SetConfig never leaves the still-current config
  referencing a deleted vault entry
- uninstall fails closed: the plugin's vault namespace is purged before any
  destructive step, and a cleanup failure aborts the uninstall (retryable)
  instead of letting a reinstall inherit stale secrets

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): failure-atomic config secrets, quiesced uninstall purge, ctx propagation

- UpdateConfig is now failure-atomic for secret overwrites: storeConfigSecrets
  snapshots each vault entry before writing and returns a rollback that
  restores prior values (or deletes newly-created ones). A vault.Set failure
  mid-loop or a failed SetConfig commit rolls back, so a request reported as
  failed never changes the value a still-current config ref resolves to.
- Uninstall stops the plugin process BEFORE purging its vault namespace, so
  the plugin can no longer race the cleanup with a SetSecret between ListIDs
  and the deletes; the purge stays fail-closed and retryable.
- Thread the request context through Uninstall -> deletePluginSecrets instead
  of context.Background(), per the pass-context-through-chains convention.
- Consolidate the 'secret not found:' prefix check into isSecretNotFound
  (host.go + service rollback share one definition).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): detach rollback context, fail closed without a vault, reconcile uninstall status

- vault rollback closures now run on context.WithoutCancel(ctx): a request
  cancelled mid-save (browser closed) can no longer abort the rollback and
  leave the vault holding new values while the unchanged config still points
  at them (Greptile P1).
- storeConfigSecrets fails closed (errSecretVaultRequired) when a plugin
  declares secret config fields but no vault is wired, instead of silently
  persisting the secret in cleartext.
- vaultRestoreFunc distinguishes not-found from a genuine Reveal error: a
  backend read failure aborts the update before writing rather than risk a
  rollback that deletes a real secret; rollback now returns errors, joined
  into UpdateConfig's failure so an inconsistent vault is surfaced, not just
  logged.
- Uninstall reconciles lifecycle status when a secret-purge failure aborts
  after the process was stopped: a previously-running plugin transitions to
  error and notifies observers instead of being reported active with no
  process running.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): detach removed-secret cleanup ctx and clear stale save dirty flag

- cleanupRemovedConfigSecrets now runs on context.WithoutCancel(ctx), matching
  the rollback path, so a client disconnect right after the config commit can
  no longer cancel the delete and orphan now-unreferenced vault entries.
- tests: assert GetConfig succeeds before treating an empty map as proof that
  nothing persisted (a swallowed read error would be a false pass).
- web: on a post-save refetch failure, rebase initialValues onto the masked
  snapshot so the saved form no longer reads as dirty.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): fail tests on discarded GetConfig errors, drop setState-in-updater

- config_test: capture and fatal on GetConfig errors at the four remaining
  positive-assertion sites (a nil from a failed read would panic on the next
  map access instead of giving a clear failure), matching the earlier fix.
- web hook: compute the masked post-save snapshot from the current values and
  apply it to setValues + setInitialValues directly, instead of calling
  setInitialValues inside a setValues functional updater (a React anti-pattern
  that runs twice under StrictMode).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(plugins): skip validation of a secret field's own vault ref

- validateConfigSchema takes the plugin id and skips a secret field whose
  value is that field's config vault ref (an internal storage marker,
  already validated when first set). Without this, a field declaring both
  secret: true and enum: [...] 400s on every save that keeps the mask,
  because the enum check saw the vault ref instead of a user value. Freshly
  entered secret values are still validated; a foreign plugin's ref is not
  skipped. Covered by unit + service-level regression tests.
- surface mid-loop vault rollback failures in storeConfigSecrets via a
  warning instead of discarding them (double-fault observability).
- web: integer inputs get step=1 so browsers flag non-integral input in
  place (serializeConfigValues remains the backstop).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* test(plugins): split config secret tests into config_secrets_test.go

config_test.go had grown past the 800-line backend file-length limit
(revive), failing CI lint. Move the vault-backed config and plugin-scoped
secret tests (plus their fake-vault/store helpers) into a sibling file;
no test logic changes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 22:13:58 +01:00

298 lines
9.4 KiB
Go

package store
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"github.com/kandev/kandev/internal/common/logger"
)
// idPattern mirrors internal/plugins/manifest's plugin id pattern:
// lowercase alphanumerics, dots, underscores, and hyphens, starting with a
// lowercase alphanumeric. manifest.Validate already enforces this shape
// upstream during install; this is the sink-level guard every FSStore
// method re-applies to id before building a filesystem path from it, so a
// malformed id (e.g. containing "..", "/", or "\") can never reach disk
// I/O regardless of how it got here.
var idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`)
// dotConfigSuffix is the suffix a plugin id must never end with: id+".yml"
// would then collide with the "<id>.config.yml" naming convention
// isRecordFile uses to distinguish a plugin's own record from another
// plugin's operator config, silently hiding the record from List() (see
// isRecordFile).
const dotConfigSuffix = ".config"
// safePluginID rejects any id that is not a single, clean path segment
// matching idPattern, or that ends in ".config" (see dotConfigSuffix). It is
// the guard called by every FSStore method before recordPath/configPath
// build a path from id.
func safePluginID(id string) error {
if !idPattern.MatchString(id) {
return fmt.Errorf("plugins: invalid plugin id %q", id)
}
if strings.HasSuffix(id, dotConfigSuffix) {
return fmt.Errorf("plugins: invalid plugin id %q: must not end in %q", id, dotConfigSuffix)
}
return nil
}
// FSStore persists plugin installation records under dir as "{id}.yml" (the
// Record) and "{id}.config.yml" (operator-editable config). FSStore
// implements Store.
type FSStore struct {
dir string
log *logger.Logger
// mu guards every disk write (Save, Delete, SetConfig) and read
// (GetConfig) that touches a plugin's record/config files, so
// concurrent callers (e.g. two operator UpdateConfig PATCH requests for
// the same id) can never interleave writes to the same path. Writers
// take Lock; GetConfig takes RLock since it only reads.
mu sync.RWMutex
}
// NewFSStore returns a store that persists records under dir.
func NewFSStore(dir string) *FSStore {
return &FSStore{dir: dir}
}
// SetLogger wires a logger List uses to warn about a corrupt/unreadable
// record file it has to skip. Optional: a nil (default, unset) logger makes
// List silently skip instead of warning.
func (s *FSStore) SetLogger(log *logger.Logger) {
s.log = log
}
// recordPath returns the on-disk path for a plugin's installation record.
// Every public method validates id with safePluginID before reaching here.
func (s *FSStore) recordPath(id string) string {
return filepath.Join(s.dir, id+".yml")
}
// configPath returns the on-disk path for a plugin's operator config.
func (s *FSStore) configPath(id string) string {
return filepath.Join(s.dir, id+".config.yml")
}
// writeRecord marshals record to YAML and writes it to disk, creating the
// store directory if needed. Used both to create a fresh record (Install)
// and to persist updates to an existing one (Save). The write is atomic
// (tmp file + rename within the same directory) so a crash mid-write can
// never leave a half-written "<id>.yml" for List()/Get() to trip over.
func (s *FSStore) writeRecord(record *Record) error {
if err := safePluginID(record.ID); err != nil {
return err
}
if err := os.MkdirAll(s.dir, 0o755); err != nil {
return fmt.Errorf("create plugin store dir: %w", err)
}
data, err := yaml.Marshal(record)
if err != nil {
return fmt.Errorf("marshal plugin record: %w", err)
}
if err := writeFileAtomic(s.dir, s.recordPath(record.ID), data, 0o600); err != nil {
return fmt.Errorf("write plugin record: %w", err)
}
return nil
}
// writeFileAtomic writes data to path via a temp file created in dir (the
// same filesystem/directory as path, so the final os.Rename is atomic) and
// an fsync-then-rename, so a reader (List/Get) never observes a
// partially-written file. The temp file is removed on any failure before
// the rename.
func writeFileAtomic(dir, path string, data []byte, mode os.FileMode) error {
tmp, err := os.CreateTemp(dir, ".tmp-*")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
tmpPath := tmp.Name()
succeeded := false
defer func() {
if !succeeded {
_ = os.Remove(tmpPath)
}
}()
if _, err := tmp.Write(data); err != nil {
_ = tmp.Close()
return fmt.Errorf("write temp file: %w", err)
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
return fmt.Errorf("sync temp file: %w", err)
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("close temp file: %w", err)
}
if err := os.Chmod(tmpPath, mode); err != nil {
return fmt.Errorf("chmod temp file: %w", err)
}
if err := os.Rename(tmpPath, path); err != nil {
return fmt.Errorf("rename temp file: %w", err)
}
succeeded = true
return nil
}
// Get loads the record for id from disk.
func (s *FSStore) Get(id string) (*Record, error) {
if err := safePluginID(id); err != nil {
return nil, err
}
data, err := os.ReadFile(s.recordPath(id))
if err != nil {
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("read plugin record: %w", err)
}
var record Record
if err := yaml.Unmarshal(data, &record); err != nil {
return nil, fmt.Errorf("unmarshal plugin record: %w", err)
}
return &record, nil
}
// List returns every installed plugin's record. A record file that fails to
// read or parse (corrupt, stray, or written by a future incompatible
// version) is skipped and warned about rather than failing List
// wholesale — the whole plugin subsystem's boot path depends on List()
// succeeding (Registry.Load), so one bad file must not disable every other
// installed plugin.
func (s *FSStore) List() ([]*Record, error) {
entries, err := os.ReadDir(s.dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("read plugin store dir: %w", err)
}
var records []*Record
for _, entry := range entries {
if entry.IsDir() || !isRecordFile(entry.Name()) {
continue
}
id := strings.TrimSuffix(entry.Name(), ".yml")
record, err := s.Get(id)
if err != nil {
s.warnSkippedRecord(id, err)
continue
}
records = append(records, record)
}
return records, nil
}
// warnSkippedRecord logs (if a logger is wired via SetLogger) that List
// skipped id's record file rather than failing outright.
func (s *FSStore) warnSkippedRecord(id string, err error) {
if s.log == nil {
return
}
s.log.Warn("plugins: skipping unreadable plugin record",
zap.String("plugin_id", id), zap.Error(err))
}
// isRecordFile reports whether name is a plugin record file ("{id}.yml"),
// excluding config files ("{id}.config.yml").
func isRecordFile(name string) bool {
return strings.HasSuffix(name, ".yml") && !strings.HasSuffix(name, ".config.yml")
}
// Save writes record to disk, creating it if it does not already exist or
// overwriting it (in full) if it does. Used both to persist a fresh install
// and to persist status/metadata updates to an existing record.
func (s *FSStore) Save(record *Record) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.writeRecord(record)
}
// Delete removes the record and its operator config for id.
func (s *FSStore) Delete(id string) error {
if err := safePluginID(id); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
if _, err := s.Get(id); err != nil {
return err
}
if err := os.Remove(s.recordPath(id)); err != nil {
return fmt.Errorf("delete plugin record: %w", err)
}
if err := os.Remove(s.configPath(id)); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("delete plugin config: %w", err)
}
return nil
}
// GetConfig returns the operator-editable config for id, or an empty map if
// no config has been set yet.
func (s *FSStore) GetConfig(id string) (map[string]any, error) {
if err := safePluginID(id); err != nil {
return nil, err
}
s.mu.RLock()
defer s.mu.RUnlock()
if _, err := s.Get(id); err != nil {
return nil, err
}
data, err := os.ReadFile(s.configPath(id))
if err != nil {
if os.IsNotExist(err) {
return map[string]any{}, nil
}
return nil, fmt.Errorf("read plugin config: %w", err)
}
var cfg map[string]any
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("unmarshal plugin config: %w", err)
}
if cfg == nil {
cfg = map[string]any{}
}
return cfg, nil
}
// SetConfig replaces the operator-editable config for id. The write is
// atomic (tmp file + rename within the store dir, via writeFileAtomic), the
// same guarantee writeRecord gives Save, and is additionally serialized by
// s.mu so two concurrent SetConfig calls for the same id (e.g. two operator
// UpdateConfig PATCH requests racing) can never interleave. Mode 0600, like
// the record file: secret config fields are normally stored as encrypted-
// vault references (internal/plugins/config.go), but the no-vault fallback
// stores cleartext, so the file must not be world-readable either way.
func (s *FSStore) SetConfig(id string, config map[string]any) error {
if err := safePluginID(id); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
if _, err := s.Get(id); err != nil {
return err
}
data, err := yaml.Marshal(config)
if err != nil {
return fmt.Errorf("marshal plugin config: %w", err)
}
if err := writeFileAtomic(s.dir, s.configPath(id), data, 0o600); err != nil {
return fmt.Errorf("write plugin config: %w", err)
}
return nil
}