package store import ( "context" "database/sql" "errors" "time" ) // Plugin is one installed third-party plugin. The signed manifest is kept so // the panel can re-verify it after a restart or an upgrade. type Plugin struct { Slug string `json:"slug"` Name string `json:"name"` Version string `json:"version"` Status string `json:"status"` // installed|disabled|failed|uninstalling Runtime string `json:"runtime"` Manifest string `json:"manifest"` PlatformSig string `json:"platform_sig"` Pubkey string `json:"pubkey"` SHA256 string `json:"sha256"` AutoUpdate bool `json:"auto_update"` LastError string `json:"last_error"` InstalledAt int64 `json:"installed_at"` UpdatedAt int64 `json:"updated_at"` } const pluginColumns = `slug, name, version, status, runtime, manifest, platform_sig, pubkey, sha256, auto_update, last_error, installed_at, updated_at` func scanPlugin(row interface{ Scan(...any) error }) (*Plugin, error) { var p Plugin var auto int err := row.Scan(&p.Slug, &p.Name, &p.Version, &p.Status, &p.Runtime, &p.Manifest, &p.PlatformSig, &p.Pubkey, &p.SHA256, &auto, &p.LastError, &p.InstalledAt, &p.UpdatedAt) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } if err != nil { return nil, err } p.AutoUpdate = auto == 1 return &p, nil } // Plugins lists installed plugins ordered by installation time. func (s *Store) Plugins(ctx context.Context) ([]*Plugin, error) { rows, err := s.db.QueryContext(ctx, `SELECT `+pluginColumns+` FROM plugins ORDER BY installed_at DESC`) if err != nil { return nil, err } defer rows.Close() out := []*Plugin{} for rows.Next() { p, err := scanPlugin(rows) if err != nil { return nil, err } out = append(out, p) } return out, rows.Err() } // PluginBySlug loads one installed plugin. func (s *Store) PluginBySlug(ctx context.Context, slug string) (*Plugin, error) { return scanPlugin(s.db.QueryRowContext(ctx, `SELECT `+pluginColumns+` FROM plugins WHERE slug = ?`, slug)) } // SavePlugin inserts or replaces the install record. func (s *Store) SavePlugin(ctx context.Context, p *Plugin) error { now := time.Now().Unix() if p.InstalledAt == 0 { p.InstalledAt = now } p.UpdatedAt = now auto := 0 if p.AutoUpdate { auto = 1 } _, err := s.db.ExecContext(ctx, `INSERT INTO plugins(slug, name, version, status, runtime, manifest, platform_sig, pubkey, sha256, auto_update, last_error, installed_at, updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(slug) DO UPDATE SET name=excluded.name, version=excluded.version, status=excluded.status, runtime=excluded.runtime, manifest=excluded.manifest, platform_sig=excluded.platform_sig, pubkey=excluded.pubkey, sha256=excluded.sha256, auto_update=excluded.auto_update, last_error=excluded.last_error, updated_at=excluded.updated_at`, p.Slug, p.Name, p.Version, p.Status, p.Runtime, p.Manifest, p.PlatformSig, p.Pubkey, p.SHA256, auto, p.LastError, p.InstalledAt, p.UpdatedAt) return err } // SetPluginStatus updates the lifecycle status and error text. func (s *Store) SetPluginStatus(ctx context.Context, slug, status, lastError string) error { _, err := s.db.ExecContext(ctx, `UPDATE plugins SET status = ?, last_error = ?, updated_at = ? WHERE slug = ?`, status, lastError, time.Now().Unix(), slug) return err } // SetPluginAutoUpdate toggles background upgrades for one plugin. func (s *Store) SetPluginAutoUpdate(ctx context.Context, slug string, on bool) error { v := 0 if on { v = 1 } _, err := s.db.ExecContext(ctx, `UPDATE plugins SET auto_update = ?, updated_at = ? WHERE slug = ?`, v, time.Now().Unix(), slug) return err } // DeletePlugin removes the install record. func (s *Store) DeletePlugin(ctx context.Context, slug string) error { _, err := s.db.ExecContext(ctx, `DELETE FROM plugins WHERE slug = ?`, slug) return err } // PluginSetting reads one value for a plugin. func (s *Store) PluginSetting(ctx context.Context, slug, key string) string { var v string err := s.db.QueryRowContext(ctx, `SELECT value FROM plugin_settings WHERE slug = ? AND key = ?`, slug, key).Scan(&v) if err != nil { return "" } return v } // SetPluginSetting writes one value for a plugin. func (s *Store) SetPluginSetting(ctx context.Context, slug, key, value string) error { _, err := s.db.ExecContext(ctx, `INSERT INTO plugin_settings(slug, key, value, updated_at) VALUES(?,?,?,?) ON CONFLICT(slug, key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at`, slug, key, value, time.Now().Unix()) return err } // PluginSettings returns every setting of a plugin. func (s *Store) PluginSettings(ctx context.Context, slug string) (map[string]string, error) { rows, err := s.db.QueryContext(ctx, `SELECT key, value FROM plugin_settings WHERE slug = ?`, slug) if err != nil { return nil, err } defer rows.Close() out := map[string]string{} for rows.Next() { var k, v string if err := rows.Scan(&k, &v); err != nil { return nil, err } out[k] = v } return out, rows.Err() } // DeletePluginSettings removes every setting of a plugin. func (s *Store) DeletePluginSettings(ctx context.Context, slug string) error { _, err := s.db.ExecContext(ctx, `DELETE FROM plugin_settings WHERE slug = ?`, slug) return err } // PluginEvent is one lifecycle record shown in the UI. type PluginEvent struct { ID int64 `json:"id"` Slug string `json:"slug"` Action string `json:"action"` Version string `json:"version"` Status string `json:"status"` Message string `json:"message"` CreatedAt int64 `json:"created_at"` } // AddPluginEvent appends a lifecycle record. func (s *Store) AddPluginEvent(ctx context.Context, e PluginEvent) error { _, err := s.db.ExecContext(ctx, `INSERT INTO plugin_events(slug, action, version, status, message, created_at) VALUES(?,?,?,?,?,?)`, e.Slug, e.Action, e.Version, e.Status, e.Message, time.Now().Unix()) return err } // PluginEvents returns the newest lifecycle records. func (s *Store) PluginEvents(ctx context.Context, slug string, limit int) ([]*PluginEvent, error) { if limit <= 0 || limit > 200 { limit = 50 } query := `SELECT id, slug, action, version, status, message, created_at FROM plugin_events` args := []any{} if slug != "" { query += ` WHERE slug = ?` args = append(args, slug) } query += ` ORDER BY id DESC LIMIT ?` args = append(args, limit) rows, err := s.db.QueryContext(ctx, query, args...) if err != nil { return nil, err } defer rows.Close() out := []*PluginEvent{} for rows.Next() { var e PluginEvent if err := rows.Scan(&e.ID, &e.Slug, &e.Action, &e.Version, &e.Status, &e.Message, &e.CreatedAt); err != nil { return nil, err } out = append(out, &e) } return out, rows.Err() } // Revocation caches a withdrawal pushed by the cloud. type Revocation struct { Slug string `json:"slug"` Version string `json:"version"` Severity string `json:"severity"` Action string `json:"action"` Reason string `json:"reason"` } // UpsertRevocation records one withdrawal. func (s *Store) UpsertRevocation(ctx context.Context, rv Revocation) error { _, err := s.db.ExecContext(ctx, `INSERT INTO plugin_revocations(slug, version, severity, action, reason, fetched_at) VALUES(?,?,?,?,?,?) ON CONFLICT(slug, version) DO UPDATE SET severity=excluded.severity, action=excluded.action, reason=excluded.reason, fetched_at=excluded.fetched_at`, rv.Slug, rv.Version, rv.Severity, rv.Action, rv.Reason, time.Now().Unix()) return err } // Revocations returns every cached withdrawal. func (s *Store) Revocations(ctx context.Context) ([]*Revocation, error) { rows, err := s.db.QueryContext(ctx, `SELECT slug, version, severity, action, reason FROM plugin_revocations`) if err != nil { return nil, err } defer rows.Close() out := []*Revocation{} for rows.Next() { var rv Revocation if err := rows.Scan(&rv.Slug, &rv.Version, &rv.Severity, &rv.Action, &rv.Reason); err != nil { return nil, err } out = append(out, &rv) } return out, rows.Err() } // RevocationFor reports whether a version is blocked or warned. func (s *Store) RevocationFor(ctx context.Context, slug, version string) (*Revocation, error) { var rv Revocation err := s.db.QueryRowContext(ctx, `SELECT slug, version, severity, action, reason FROM plugin_revocations WHERE slug = ? AND (version = ? OR version = '') ORDER BY version DESC LIMIT 1`, slug, version).Scan(&rv.Slug, &rv.Version, &rv.Severity, &rv.Action, &rv.Reason) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } if err != nil { return nil, err } return &rv, nil } // LastRevocationID returns the highest cached revocation row id, used as the // incremental cursor when polling the cloud. func (s *Store) LastRevocationID(ctx context.Context) int64 { var id int64 _ = s.db.QueryRowContext(ctx, `SELECT COALESCE(MAX(rowid),0) FROM plugin_revocations`).Scan(&id) return id }