diff --git a/internal/app/archie/operations.go b/internal/app/archie/operations.go index 0055f8501..b443b15e3 100644 --- a/internal/app/archie/operations.go +++ b/internal/app/archie/operations.go @@ -19,6 +19,10 @@ import ( "github.com/samcharles93/archie-core/modules/runs" ) +// catalogueUpdateInterval is how often an extension that follows the catalogue +// checks it for a new version. +const catalogueUpdateInterval = time.Hour + // operations is everything the gate holds back: the connectors, the access // chain administrative writes are checked against, the workflow that ships with // the binary, and the loops that do work. Run wires it at boot and Start brings @@ -80,6 +84,9 @@ func (o *operations) Start(ctx context.Context) error { // Nothing below can fail, so a retry cannot launch a connector or a loop twice. o.checks.Register("connectors", o.conns.Ready) + // An extension that follows the catalogue checks it for a new version. With + // no catalogue source set it does nothing. + o.workers.Go(func() { o.conns.WatchUpdates(ctx, catalogueUpdateInterval) }) // Administrative writes, and every API request, are checked against the same // live policy chain as chat and background work. The webhook intake applies // each source's delivery policy from it. diff --git a/internal/app/archie/run.go b/internal/app/archie/run.go index 8ff88d1c5..09e6852c4 100644 --- a/internal/app/archie/run.go +++ b/internal/app/archie/run.go @@ -12,6 +12,7 @@ import ( "net" "net/http" "os" + "path/filepath" "strconv" "time" @@ -155,7 +156,7 @@ func wire(log *slog.Logger, configPath string, cfg Config, db *sqlx.DB) (*binary conns, err := connectors.NewModule(db, log.With("module", "connectors"), orgID, connectors.Sealing{ Identity: cfg.AgeIdentity, Backups: cfg.AgeBackupRecipients, - }) + }, filepath.Join(cfg.StateDir, "extensions")) if err != nil { return nil, err } diff --git a/internal/platform/pg/migrations/00054_connectors_catalogue.sql b/internal/platform/pg/migrations/00054_connectors_catalogue.sql new file mode 100644 index 000000000..f9c00bc3d --- /dev/null +++ b/internal/platform/pg/migrations/00054_connectors_catalogue.sql @@ -0,0 +1,24 @@ +-- +goose Up +-- An extension installed from the catalogue is a row like any accepted one: its +-- path and digest are the live version and its egress, env and credentials are +-- what the operator accepted. package keeps the rest of its lifecycle: the +-- grants each version declared, a pending update, the previous version and the +-- update policy. It is null for an extension accepted by path. +ALTER TABLE connectors.accepted + ADD COLUMN enabled boolean NOT NULL DEFAULT true, + ADD COLUMN package jsonb; + +-- Where an organisation's catalogue is read from, and the key that signs it. +-- newest_issued_at is the newest catalogue accepted from it, so an older one +-- replayed by the source or the network is refused. +CREATE TABLE connectors.catalogue_source ( + org_id text PRIMARY KEY, + location text NOT NULL, + public_key text NOT NULL, + newest_issued_at timestamptz, + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- +goose Down +DROP TABLE connectors.catalogue_source; +ALTER TABLE connectors.accepted DROP COLUMN package, DROP COLUMN enabled; diff --git a/modules/connectors/catalogue.go b/modules/connectors/catalogue.go new file mode 100644 index 000000000..169a8478a --- /dev/null +++ b/modules/connectors/catalogue.go @@ -0,0 +1,217 @@ +package connectors + +import ( + "context" + "errors" + "fmt" + "net/http" + "slices" + "time" + + "github.com/danielgtaylor/huma/v2" + + "github.com/samcharles93/archie-core/internal/platform/httpapi" + "github.com/samcharles93/archie-core/modules/connectors/internal/catalogue" + "github.com/samcharles93/archie-core/modules/connectors/internal/store" + "github.com/samcharles93/archie-core/modules/governance" +) + +// errNoSource means no catalogue source is set. +var errNoSource = errors.New("no catalogue source is set") + +// sourceStore is where the catalogue source and its freshness mark are kept. +type sourceStore interface { + CatalogueSource(ctx context.Context, orgID string) (store.Source, bool, error) + PutCatalogueSource(ctx context.Context, src store.Source) error + AdvanceCatalogue(ctx context.Context, orgID string, issuedAt time.Time) (bool, error) +} + +// catalogueContracts are the contracts a catalogue entry may serve. An MCP +// server is configuration, not a package. +var catalogueContracts = []string{store.ContractConnector, store.ContractSecretEngine, store.ContractGuard} + +// CatalogueSource is where the catalogue is read from and the key that signs it. +type CatalogueSource struct { + Location string `json:"location" doc:"An https URL or an absolute path to the catalogue; its signature is beside it with .sig added"` + PublicKey string `json:"public_key" doc:"The base64 Ed25519 public key the catalogue is signed with"` + // Newest is the issue time of the newest catalogue accepted from it. + Newest *time.Time `json:"newest,omitempty" readOnly:"true"` +} + +// SourceView is the catalogue source as the dashboard reads it. +type SourceView struct { + Configured bool `json:"configured"` + Source *CatalogueSource `json:"source,omitempty"` +} + +func (m *Module) getCatalogueSource(ctx context.Context, _ *struct{}) (*httpapi.Reply[SourceView], error) { + src, ok, err := m.sources.CatalogueSource(ctx, m.orgID) + if err != nil { + m.log.ErrorContext(ctx, "read the catalogue source", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the catalogue source") + } + if !ok { + return httpapi.OK(SourceView{}), nil + } + view := CatalogueSource{Location: src.Location, PublicKey: src.PublicKey} + if src.Newest.Valid { + view.Newest = &src.Newest.Time + } + return httpapi.OK(SourceView{Configured: true, Source: &view}), nil +} + +func (m *Module) putCatalogueSource(ctx context.Context, in *struct{ Body CatalogueSource }) (*httpapi.Reply[SourceView], error) { + req := in.Body + if err := catalogue.CheckLocation(req.Location); err != nil { + return nil, huma.NewError(http.StatusBadRequest, err.Error()) + } + if _, err := catalogue.ParseKey(req.PublicKey); err != nil { + return nil, huma.NewError(http.StatusBadRequest, err.Error()) + } + if err := m.sources.PutCatalogueSource(ctx, store.Source{OrgID: m.orgID, Location: req.Location, PublicKey: req.PublicKey}); err != nil { + m.log.ErrorContext(ctx, "save the catalogue source", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the catalogue source") + } + m.log.InfoContext(ctx, "catalogue source set", "audit", true, "action", "catalogue.source") + return m.getCatalogueSource(ctx, nil) +} + +// readCatalogue reads the catalogue from the source and verifies it: the +// signature, then freshness, so an older catalogue is refused only once it is +// known to be genuine. +func (m *Module) readCatalogue(ctx context.Context) (catalogue.Document, string, error) { + src, ok, err := m.sources.CatalogueSource(ctx, m.orgID) + if err != nil { + return catalogue.Document{}, "", err + } + if !ok { + return catalogue.Document{}, "", errNoSource + } + key, err := catalogue.ParseKey(src.PublicKey) + if err != nil { + return catalogue.Document{}, "", err + } + body, sig, err := m.fetcher.Read(ctx, src.Location) + if err != nil { + return catalogue.Document{}, "", err + } + doc, err := catalogue.Verify(body, sig, key) + if err != nil { + return catalogue.Document{}, "", err + } + if fresh, err := m.sources.AdvanceCatalogue(ctx, m.orgID, doc.IssuedAt); err != nil { + return catalogue.Document{}, "", err + } else if !fresh { + return catalogue.Document{}, "", catalogue.ErrStale + } + return doc, src.Location, nil +} + +// CatalogueEntry is one extension the catalogue offers and whether it is +// installed. +type CatalogueEntry struct { + Name string `json:"name"` + DisplayName string `json:"display_name"` + Description string `json:"description"` + Contract string `json:"contract"` + Group string `json:"group" enum:"platform,integration,agent"` + Version string `json:"version"` + Digest string `json:"digest"` + Grants catalogue.Grants `json:"grants"` + Installed bool `json:"installed"` + // InstalledVersion and UpdateAvailable describe an installed entry: the + // catalogue now pins a different digest than the one installed. + InstalledVersion string `json:"installed_version,omitempty"` + UpdateAvailable bool `json:"update_available"` +} + +func (m *Module) listCatalogue(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]CatalogueEntry], error) { + doc, _, err := m.readCatalogue(ctx) + if err != nil { + return nil, catalogueError(err) + } + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + m.log.ErrorContext(ctx, "list accepted packages", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages") + } + out := make([]CatalogueEntry, 0, len(doc.Extensions)) + for _, e := range doc.Extensions { + entry := CatalogueEntry{ + Name: e.Name, DisplayName: e.DisplayName, Description: e.Description, Contract: e.Contract, + Group: string(store.GroupOf(e.Contract)), Version: e.Version, Digest: e.Digest, Grants: e.Grants, + } + if i := slices.IndexFunc(accepted, func(a store.Accepted) bool { return a.Name == e.Name }); i >= 0 { + entry.Installed = true + if rec, ok, err := packageOf(accepted[i]); err == nil && ok { + entry.InstalledVersion = rec.State.Current.Version + entry.UpdateAvailable = rec.State.Current.Digest != e.Digest + } + } + out = append(out, entry) + } + return httpapi.OK(out), nil +} + +// catalogueError reports why the catalogue couldn't be used. A catalogue that +// can't be fetched or verified is a bad gateway, never an empty list. +func catalogueError(err error) error { + switch { + case errors.Is(err, errNoSource): + return huma.NewError(http.StatusNotFound, err.Error()) + case errors.Is(err, catalogue.ErrBadSignature), errors.Is(err, catalogue.ErrStale): + return huma.NewError(http.StatusBadGateway, err.Error()) + default: + return huma.NewError(http.StatusBadGateway, fmt.Sprintf("catalogue unavailable: %v", err)) + } +} + +func (m *Module) registerCatalogue(api huma.API) { + tags := []string{"Extension catalogue"} + read := httpapi.AdminAuthorized(governance.ActionRead, governance.KindConnector) + write := httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindConnector) + httpapi.Register(api, read, huma.Operation{ + OperationID: "get-catalogue-source", Method: http.MethodGet, Path: "/api/extensions/catalogue-source", + Summary: "Get where the extension catalogue is read from", Tags: tags, + }, m.getCatalogueSource) + httpapi.Register(api, write, huma.Operation{ + OperationID: "put-catalogue-source", Method: http.MethodPut, Path: "/api/extensions/catalogue-source", + Summary: "Set where the extension catalogue is read from and the key that signs it", Tags: tags, + }, m.putCatalogueSource) + httpapi.Register(api, read, huma.Operation{ + OperationID: "list-catalogue", Method: http.MethodGet, Path: "/api/extensions/catalogue", + Summary: "List the extensions the signed catalogue offers", Tags: tags, + }, m.listCatalogue) + httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionCreate, governance.KindConnector), huma.Operation{ + OperationID: "install-extension", Method: http.MethodPost, Path: "/api/extensions/catalogue/{name}/install", + Summary: "Fetch an extension at the digest the catalogue pins and install it, not yet accepted", Tags: tags, + }, m.install) + httpapi.Register(api, write, huma.Operation{ + OperationID: "accept-extension-grants", Method: http.MethodPost, Path: "/api/extensions/{name}/accept", + Summary: "Accept the grants an installed extension may use, and its settings", Tags: tags, + }, m.acceptGrants) + httpapi.Register(api, write, huma.Operation{ + OperationID: "update-extension", Method: http.MethodPost, Path: "/api/extensions/{name}/update", + Summary: "Move an extension to the digest the catalogue now pins", Tags: tags, + }, m.updateExtension) + httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionApprove, governance.KindConnector), huma.Operation{ + OperationID: "approve-extension-update", Method: http.MethodPost, Path: "/api/extensions/{name}/approve-update", + Summary: "Apply the pending update and accept the grants it declares", Tags: tags, + }, m.approveUpdate) + httpapi.Register(api, write, huma.Operation{ + OperationID: "rollback-extension", Method: http.MethodPost, Path: "/api/extensions/{name}/rollback", + Summary: "Restore the previous version with the grants accepted for it", Tags: tags, + }, m.rollback) + httpapi.Register(api, write, huma.Operation{ + OperationID: "put-extension-update-policy", Method: http.MethodPut, Path: "/api/extensions/{name}/update-policy", + Summary: "Set whether an extension follows the catalogue on its own", Tags: tags, + }, m.setPolicy) + httpapi.Register(api, write, huma.Operation{ + OperationID: "put-extension-enabled", Method: http.MethodPut, Path: "/api/extensions/{name}/enabled", + Summary: "Start or stop an accepted extension", Tags: tags, + }, m.setEnabled) + httpapi.Register(api, write, huma.Operation{ + OperationID: "put-extension-settings", Method: http.MethodPut, Path: "/api/extensions/{name}/settings", + Summary: "Save an installed extension's settings and restart it", Tags: tags, + }, m.putExtensionSettings) +} diff --git a/modules/connectors/extensions_api.go b/modules/connectors/extensions_api.go new file mode 100644 index 000000000..f00cd2169 --- /dev/null +++ b/modules/connectors/extensions_api.go @@ -0,0 +1,75 @@ +package connectors + +import ( + "context" + "fmt" + "net/http" + "slices" + + "github.com/danielgtaylor/huma/v2" + + "github.com/samcharles93/archie-core/internal/platform/httpapi" + "github.com/samcharles93/archie-core/modules/connectors/internal/store" + "github.com/samcharles93/archie-core/modules/governance" +) + +// listExtensions returns every accepted package, whatever its contract: the +// extensions page shows one list rather than one per kind. +func (m *Module) listExtensions(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]ExtensionView], error) { + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + m.log.ErrorContext(ctx, "list accepted packages", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages") + } + views := make([]ExtensionView, 0, len(accepted)) + for _, a := range accepted { + view, err := m.viewOf(a) + if err != nil { + m.log.ErrorContext(ctx, "read accepted package", "package", a.Name, "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read "+a.Name) + } + views = append(views, view) + } + return httpapi.OK(views), nil +} + +// removeExtension removes an accepted package without naming its contract +// first. The contract is read from the row and handed to the same removal the +// per-kind routes use. +func (m *Module) removeExtension(ctx context.Context, in *nameInput) (*httpapi.Reply[packageState], error) { + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + m.log.ErrorContext(ctx, "list accepted packages", "error", err) + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages") + } + i := slices.IndexFunc(accepted, func(a store.Accepted) bool { return a.Name == in.Name }) + if i < 0 { + return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("no accepted package named %s", in.Name)) + } + _, managed, err := packageOf(accepted[i]) + if err != nil { + return nil, huma.NewError(http.StatusInternalServerError, err.Error()) + } + reply, err := m.removeAs(ctx, in, accepted[i].Contract) + if err != nil { + return nil, err + } + if managed { + if err := m.fetcher.Forget(in.Name); err != nil { + m.log.WarnContext(ctx, "keep extension artifacts", "package", in.Name, "error", err) + } + } + return reply, nil +} + +func (m *Module) registerExtensions(api huma.API) { + tags := []string{"Extensions"} + httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindConnector), huma.Operation{ + OperationID: "list-extensions", Method: http.MethodGet, Path: "/api/extensions", + Summary: "List every accepted package, whatever its contract", Tags: tags, + }, m.listExtensions) + httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionDelete, governance.KindConnector), huma.Operation{ + OperationID: "remove-extension", Method: http.MethodDelete, Path: "/api/extensions/{name}", + Summary: "Remove an accepted package", Tags: tags, + }, m.removeExtension) +} diff --git a/modules/connectors/internal/catalogue/catalogue_test.go b/modules/connectors/internal/catalogue/catalogue_test.go new file mode 100644 index 000000000..9eac83053 --- /dev/null +++ b/modules/connectors/internal/catalogue/catalogue_test.go @@ -0,0 +1,199 @@ +package catalogue + +import ( + "context" + "crypto/ed25519" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + "os" + "path/filepath" + "testing" +) + +// TestFetchRefusesAnArtifactWhoseDigestDoesNotMatch guards the install +// boundary: a catalogue pins a digest, and bytes that don't hash to it are +// never kept. +func TestFetchRefusesAnArtifactWhoseDigestDoesNotMatch(t *testing.T) { + body := []byte("an extension binary") + sum := sha256.Sum256(body) + good := hex.EncodeToString(sum[:]) + bad := "0000000000000000000000000000000000000000000000000000000000000000" + + cases := []struct { + name string + digest string + wantErr error + }{ + {"a matching digest is kept", "sha256:" + good, nil}, + {"a different digest is refused", bad, ErrDigestMismatch}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + dir := t.TempDir() + artifact := filepath.Join(dir, "artifact") + if err := os.WriteFile(artifact, body, 0o600); err != nil { + t.Fatal(err) + } + f := NewFetcher(filepath.Join(dir, "kept")) + got, err := f.Fetch(context.Background(), "example", artifact, c.digest) + if !errors.Is(err, c.wantErr) { + t.Fatalf("Fetch() error = %v, want %v", err, c.wantErr) + } + if c.wantErr != nil { + if got != "" { + t.Fatalf("Fetch() kept %q for a mismatched artifact", got) + } + return + } + raw, err := os.ReadFile(got) //nolint:gosec // the path is the fetcher's own output under t.TempDir + if err != nil { + t.Fatal(err) + } + if string(raw) != string(body) { + t.Fatalf("Fetch() kept %q, want %q", raw, body) + } + }) + } +} + +// TestAcceptRefusesGrantsTheExtensionNeverDeclared guards the grant boundary: +// an operator may narrow what an extension declared, never widen it. +func TestAcceptRefusesGrantsTheExtensionNeverDeclared(t *testing.T) { + declared := Grants{Egress: []string{"api.example.com"}, Env: []string{"HOME"}, Credentials: []string{"token"}} + state := Install(Pin{Version: "1.0.0", Digest: "d1", Declared: declared}) + + cases := []struct { + name string + grants Grants + wantErr error + }{ + {"declared grants are accepted", declared, nil}, + {"fewer than declared are accepted", Grants{Egress: []string{"api.example.com"}}, nil}, + {"an undeclared host is refused", Grants{Egress: []string{"elsewhere.example.com"}}, ErrBeyondDeclared}, + {"an undeclared credential is refused", Grants{Credentials: []string{"other"}}, ErrBeyondDeclared}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + if _, err := state.Accept(c.grants); !errors.Is(err, c.wantErr) { + t.Fatalf("Accept() error = %v, want %v", err, c.wantErr) + } + }) + } +} + +// TestAnUpdateThatAsksForMoreWaitsForApproval guards the update boundary: a +// pin that widens the grants changes nothing that runs until an operator +// approves it, and the approval accepts what that pin declares. +func TestAnUpdateThatAsksForMoreWaitsForApproval(t *testing.T) { + first := Pin{Version: "1.0.0", Digest: "d1", Declared: Grants{Credentials: []string{"token"}}} + state, err := Install(first).Accept(Grants{Credentials: []string{"token"}}) + if err != nil { + t.Fatal(err) + } + wider := Pin{Version: "1.1.0", Digest: "d2", Declared: Grants{Credentials: []string{"token"}, Env: []string{"HOME"}}} + + offered, applied := state.Offer(wider) + if applied { + t.Fatal("a wider update applied without approval") + } + if offered.Pending == nil || offered.Pending.Digest != "d2" { + t.Fatalf("Offer() pending = %+v, want d2", offered.Pending) + } + if offered.Current.Digest != "d1" || offered.Accepted == nil || len(offered.Accepted.Env) != 0 { + t.Fatalf("Offer() changed what runs: current %q accepted %+v", offered.Current.Digest, offered.Accepted) + } + + approved, err := offered.Approve() + if err != nil { + t.Fatal(err) + } + if approved.Current.Digest != "d2" || approved.Pending != nil { + t.Fatalf("Approve() current = %q pending = %+v", approved.Current.Digest, approved.Pending) + } + if approved.Accepted == nil || len(approved.Accepted.Env) != 1 || approved.Accepted.Env[0] != "HOME" { + t.Fatalf("Approve() accepted = %+v, want the pin's declared grants", approved.Accepted) + } +} + +// TestRollbackRestoresThePreviousGrants guards the rollback boundary: it +// restores the version and the grants accepted for that version, never the +// grants of the version it replaces. +func TestRollbackRestoresThePreviousGrants(t *testing.T) { + first := Pin{Version: "1.0.0", Digest: "d1", Declared: Grants{Credentials: []string{"token"}}} + state, err := Install(first).Accept(Grants{Credentials: []string{"token"}}) + if err != nil { + t.Fatal(err) + } + second := Pin{Version: "1.1.0", Digest: "d2", Declared: Grants{Credentials: []string{"token"}, Env: []string{"HOME"}}} + state, applied := state.Offer(second) + if applied { + t.Fatal("a wider update applied without approval") + } + state, err = state.Approve() + if err != nil { + t.Fatal(err) + } + if state.Accepted == nil || len(state.Accepted.Env) != 1 { + t.Fatalf("Approve() accepted = %+v, want HOME", state.Accepted) + } + + rolled, err := state.Rollback() + if err != nil { + t.Fatal(err) + } + if rolled.Current.Digest != "d1" { + t.Fatalf("Rollback() current = %q, want d1", rolled.Current.Digest) + } + if rolled.Accepted == nil || len(rolled.Accepted.Env) != 0 || len(rolled.Accepted.Credentials) != 1 { + t.Fatalf("Rollback() accepted = %+v, want the grants accepted for d1", rolled.Accepted) + } +} + +// TestVerifyRefusesAForgedOrMalformedCatalogue guards the trust boundary: the +// catalogue is read only when the configured key signs it, and an unsigned +// ordering or a duplicate entry is refused even then. +func TestVerifyRefusesAForgedOrMalformedCatalogue(t *testing.T) { + pub, priv, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + digest := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + entry := `{"name":"example","contract":"connector","artifact":"example","digest":"` + digest + `"}` + signed := func(body string) []byte { + return []byte(base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(body)))) + } + good := `{"issued_at":"2026-11-22T00:00:00Z","extensions":[` + entry + `]}` + unsigned := `{"extensions":[` + entry + `]}` + duplicate := `{"issued_at":"2026-11-22T00:00:00Z","extensions":[` + entry + `,` + entry + `]}` + _, other, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + + cases := []struct { + name string + body string + sig []byte + ok bool + }{ + {"a signed catalogue is read", good, signed(good), true}, + {"bytes changed after signing are refused", good + " ", signed(good), false}, + {"another key's signature is refused", good, []byte(base64.StdEncoding.EncodeToString(ed25519.Sign(other, []byte(good)))), false}, + {"a catalogue with no issue time is refused", unsigned, signed(unsigned), false}, + {"a entry listed twice is refused", duplicate, signed(duplicate), false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + _, err := Verify([]byte(c.body), c.sig, pub) + if c.ok && err != nil { + t.Fatalf("Verify() error = %v, want none", err) + } + if !c.ok && err == nil { + t.Fatal("Verify() accepted a catalogue it must refuse") + } + }) + } +} diff --git a/modules/connectors/internal/catalogue/document.go b/modules/connectors/internal/catalogue/document.go new file mode 100644 index 000000000..1eb16f22d --- /dev/null +++ b/modules/connectors/internal/catalogue/document.go @@ -0,0 +1,134 @@ +package catalogue + +import ( + "crypto/ed25519" + "encoding/base64" + "encoding/hex" + json "encoding/json/v2" + "errors" + "fmt" + "regexp" + "strings" + "time" +) + +// Entry is one extension a catalogue offers: what it is, the artifact to +// fetch and the digest that artifact must have, and the grants it asks for. +type Entry struct { + Name string `json:"name"` + DisplayName string `json:"display_name"` + Description string `json:"description"` + // Contract is the extension contract the binary serves: connector, + // secretengine or guard. + Contract string `json:"contract"` + Version string `json:"version"` + // Artifact is where the binary is fetched from: an https URL, or a path on + // this host. A relative reference is read from beside the catalogue. + Artifact string `json:"artifact"` + // Digest is the hex SHA-256 of the binary, optionally prefixed "sha256:". + Digest string `json:"digest"` + // Grants is what the extension asks to use. + Grants Grants `json:"grants"` +} + +// Document is a signed list of extensions. +type Document struct { + // IssuedAt orders catalogues: one issued before the newest accepted is + // refused, so a replayed older catalogue can't pin outdated digests. + IssuedAt time.Time `json:"issued_at"` + Extensions []Entry `json:"extensions"` +} + +// Entry returns the named extension. +func (d Document) Entry(name string) (Entry, bool) { + for _, e := range d.Extensions { + if e.Name == name { + return e, true + } + } + return Entry{}, false +} + +var ( + entryName = regexp.MustCompile(`^[a-z][a-z0-9-]{0,62}$`) + hexDigest = regexp.MustCompile(`^[a-f0-9]{64}$`) + + // ErrBadSignature means the catalogue was not signed by the configured key. + ErrBadSignature = errors.New("catalogue signature does not verify") + // ErrStale means the catalogue is older than the newest one accepted. + ErrStale = errors.New("catalogue is older than the newest accepted catalogue") +) + +// NormalDigest returns the bare lower-case hex of a digest, or an error when it +// is not a SHA-256. +func NormalDigest(digest string) (string, error) { + d := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(digest), "sha256:")) + if !hexDigest.MatchString(d) { + return "", errors.New("digest must be a hex SHA-256") + } + return d, nil +} + +// Normalised checks an entry and returns it with its digest and grants in +// their stored form. +func (e Entry) Normalised() (Entry, error) { + d, err := NormalDigest(e.Digest) + switch { + case !entryName.MatchString(e.Name): + return Entry{}, fmt.Errorf("catalogue entry %q: the name is lowercase letters, digits and hyphens", e.Name) + case err != nil: + return Entry{}, fmt.Errorf("catalogue entry %q: %w", e.Name, err) + case e.Artifact == "": + return Entry{}, fmt.Errorf("catalogue entry %q: an artifact is required", e.Name) + case e.Contract == "": + return Entry{}, fmt.Errorf("catalogue entry %q: a contract is required", e.Name) + } + e.Digest = d + e.Grants = e.Grants.Normalised() + return e, nil +} + +// Verify checks the signature over body and decodes it. The signature is the +// base64 Ed25519 signature of exactly body's bytes. A catalogue without an +// issue time is refused: it could not be ordered against a replay. +func Verify(body, signature []byte, key ed25519.PublicKey) (Document, error) { + if len(key) != ed25519.PublicKeySize { + return Document{}, errors.New("no valid signing key") + } + sig, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(signature))) + if err != nil || !ed25519.Verify(key, body, sig) { + return Document{}, ErrBadSignature + } + var doc Document + if err := json.Unmarshal(body, &doc, json.RejectUnknownMembers(true)); err != nil { + return Document{}, fmt.Errorf("catalogue: %w", err) + } + if doc.IssuedAt.IsZero() { + return Document{}, errors.New("catalogue has no issue time") + } + seen := make(map[string]bool, len(doc.Extensions)) + for i, e := range doc.Extensions { + n, err := e.Normalised() + if err != nil { + return Document{}, err + } + if seen[n.Name] { + return Document{}, fmt.Errorf("catalogue entry %q is listed twice", n.Name) + } + seen[n.Name] = true + doc.Extensions[i] = n + } + return doc, nil +} + +// ParseKey decodes a base64 Ed25519 public key. +func ParseKey(s string) (ed25519.PublicKey, error) { + raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(s)) + if err != nil || len(raw) != ed25519.PublicKeySize { + return nil, errors.New("the key is a base64 Ed25519 public key") + } + return ed25519.PublicKey(raw), nil +} + +// HexDigest is the hex SHA-256 of a binary as a catalogue states it. +func HexDigest(sum []byte) string { return hex.EncodeToString(sum) } diff --git a/modules/connectors/internal/catalogue/fetch.go b/modules/connectors/internal/catalogue/fetch.go new file mode 100644 index 000000000..832d96c75 --- /dev/null +++ b/modules/connectors/internal/catalogue/fetch.go @@ -0,0 +1,206 @@ +package catalogue + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "net" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "strings" + "time" +) + +// ErrDigestMismatch means an artifact is not the binary the catalogue pins. +var ErrDigestMismatch = errors.New("the artifact does not match its digest") + +const ( + // maxArtifact bounds a binary fetch. + maxArtifact = 512 << 20 + // maxDocument bounds a catalogue and its signature. + maxDocument = 1 << 20 +) + +// Fetcher reads catalogues and artifacts from an https URL or from a path on +// this host. +type Fetcher struct { + // Dir is where verified artifacts are kept, one directory per digest. + Dir string + Client *http.Client +} + +// NewFetcher returns a Fetcher that keeps artifacts under dir. +func NewFetcher(dir string) *Fetcher { + return &Fetcher{Dir: dir, Client: &http.Client{Timeout: 5 * time.Minute}} +} + +// CheckLocation refuses a location that is neither an https URL, a loopback +// http URL nor an absolute path. +func CheckLocation(loc string) error { + if strings.HasPrefix(loc, "/") { + return nil + } + u, err := url.Parse(loc) + if err != nil || u.Host == "" { + return errors.New("the location is an https URL or an absolute path") + } + if u.Scheme == "https" || (u.Scheme == "http" && loopback(u.Hostname())) { + return nil + } + return errors.New("the location is an https URL or an absolute path") +} + +func loopback(host string) bool { + if host == "localhost" { + return true + } + ip := net.ParseIP(host) + return ip != nil && ip.IsLoopback() +} + +// Resolve returns ref as an absolute location: a relative reference is read +// from beside base. +func Resolve(base, ref string) (string, error) { + if strings.HasPrefix(ref, "/") { + return ref, nil + } + if u, err := url.Parse(ref); err == nil && u.Scheme != "" { + return ref, nil + } + if strings.HasPrefix(base, "/") { + return path.Join(path.Dir(base), ref), nil + } + b, err := url.Parse(base) + if err != nil { + return "", err + } + r, err := b.Parse(ref) + if err != nil { + return "", err + } + return r.String(), nil +} + +func (f *Fetcher) open(ctx context.Context, loc string) (io.ReadCloser, error) { + if err := CheckLocation(loc); err != nil { + return nil, fmt.Errorf("%q: %w", loc, err) + } + if strings.HasPrefix(loc, "/") { + return os.Open(filepath.Clean(loc)) + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, loc, nil) + if err != nil { + return nil, err + } + resp, err := f.Client.Do(req) //nolint:gosec // the location is the operator's catalogue source or an artifact a signed catalogue names + if err != nil { + return nil, fmt.Errorf("fetch %s: %w", loc, err) + } + if resp.StatusCode != http.StatusOK { + _ = resp.Body.Close() + return nil, fmt.Errorf("fetch %s: status %d", loc, resp.StatusCode) + } + return resp.Body, nil +} + +// Read returns a catalogue and its detached signature, which sits beside it +// at the same location with ".sig" added. +func (f *Fetcher) Read(ctx context.Context, loc string) (body, signature []byte, err error) { + if body, err = f.readAll(ctx, loc); err != nil { + return nil, nil, err + } + if signature, err = f.readAll(ctx, loc+".sig"); err != nil { + return nil, nil, err + } + return body, signature, nil +} + +func (f *Fetcher) readAll(ctx context.Context, loc string) ([]byte, error) { + r, err := f.open(ctx, loc) + if err != nil { + return nil, err + } + defer func() { _ = r.Close() }() + data, err := io.ReadAll(io.LimitReader(r, maxDocument+1)) + if err != nil { + return nil, err + } + if len(data) > maxDocument { + return nil, fmt.Errorf("%s exceeds the size limit", loc) + } + return data, nil +} + +// Fetch reads the artifact at loc and keeps it as an executable under Dir, +// named for the extension and its digest. Nothing is kept unless the bytes hash +// to digest, a hex SHA-256. It returns the path of the kept binary. +func (f *Fetcher) Fetch(ctx context.Context, name, loc, digest string) (string, error) { + want, err := NormalDigest(digest) + if err != nil { + return "", err + } + dir := filepath.Join(f.Dir, name, want) + final := filepath.Join(dir, name) + if got, err := fileDigest(final); err == nil && got == want { + return final, nil + } + src, err := f.open(ctx, loc) + if err != nil { + return "", err + } + defer func() { _ = src.Close() }() + if err := os.MkdirAll(dir, 0o700); err != nil { + return "", err + } + tmp, err := os.CreateTemp(dir, "fetch-*") + if err != nil { + return "", err + } + defer func() { _ = os.Remove(tmp.Name()) }() + h := sha256.New() + n, err := io.Copy(io.MultiWriter(tmp, h), io.LimitReader(src, maxArtifact+1)) + if err != nil { + _ = tmp.Close() + return "", err + } + if err := tmp.Close(); err != nil { + return "", err + } + if n > maxArtifact { + return "", fmt.Errorf("%s exceeds the size limit", loc) + } + if hex.EncodeToString(h.Sum(nil)) != want { + return "", ErrDigestMismatch + } + if err := os.Chmod(tmp.Name(), 0o700); err != nil { //nolint:gosec // the verified binary must be executable + return "", err + } + if err := os.Rename(tmp.Name(), final); err != nil { + return "", err + } + return final, nil +} + +// Forget deletes every artifact kept for name. +func (f *Fetcher) Forget(name string) error { + return os.RemoveAll(filepath.Join(f.Dir, name)) +} + +func fileDigest(p string) (string, error) { + file, err := os.Open(filepath.Clean(p)) + if err != nil { + return "", err + } + defer func() { _ = file.Close() }() + h := sha256.New() + if _, err := io.Copy(h, file); err != nil { + return "", err + } + return hex.EncodeToString(h.Sum(nil)), nil +} diff --git a/modules/connectors/internal/catalogue/grants.go b/modules/connectors/internal/catalogue/grants.go new file mode 100644 index 000000000..3fd7a797e --- /dev/null +++ b/modules/connectors/internal/catalogue/grants.go @@ -0,0 +1,92 @@ +// Package catalogue holds the rules of installing an extension from a +// catalogue: what a catalogue says, what an operator accepted, and how an +// update or a rollback moves between the two. It does no I/O beyond fetching +// an artifact, so the rules that keep an extension within its grants are +// checked without a database or a running process. +package catalogue + +import ( + "slices" + "strings" +) + +// Grants is what an extension may use: the hosts it may reach, the host +// environment variables it may see and the credential slots it is configured +// with. A catalogue entry declares them and an operator accepts them. +type Grants struct { + // Egress is every host the extension may reach. An empty list means the + // extension is not confined and uses the host's network. + Egress []string `json:"egress"` + Env []string `json:"env"` + Credentials []string `json:"credentials"` +} + +// Normalised returns g with every list present, trimmed, sorted and without +// duplicates. +func (g Grants) Normalised() Grants { + return Grants{Egress: tidy(g.Egress), Env: tidy(g.Env), Credentials: tidy(g.Credentials)} +} + +func tidy(in []string) []string { + out := make([]string, 0, len(in)) + for _, s := range in { + if s = strings.TrimSpace(s); s != "" { + out = append(out, s) + } + } + slices.Sort(out) + return slices.Compact(out) +} + +// Unrestricted reports whether the extension reaches the whole network. +func (g Grants) Unrestricted() bool { return len(g.Egress) == 0 } + +// Covers reports whether every grant in want is one g holds. An extension +// that reaches the whole network holds more than any list of hosts, so it is +// covered only by g that also reaches the whole network. +func (g Grants) Covers(want Grants) bool { + if want.Unrestricted() && !g.Unrestricted() { + return false + } + if !want.Unrestricted() && !g.Unrestricted() && !subset(want.Egress, g.Egress) { + return false + } + return subset(want.Env, g.Env) && subset(want.Credentials, g.Credentials) +} + +func subset(want, have []string) bool { + for _, s := range want { + if !slices.Contains(have, s) { + return false + } + } + return true +} + +// Added returns what want asks for beyond g. Reaching the whole network is +// reported as the host "*". +func (g Grants) Added(want Grants) Grants { + out := Grants{ + Env: missing(want.Env, g.Env), + Credentials: missing(want.Credentials, g.Credentials), + } + switch { + case want.Unrestricted() && !g.Unrestricted(): + out.Egress = []string{"*"} + case want.Unrestricted(), g.Unrestricted(): + out.Egress = []string{} + default: + out.Egress = missing(want.Egress, g.Egress) + } + return out +} + +func missing(want, have []string) []string { + out := []string{} + for _, s := range want { + if !slices.Contains(have, s) { + out = append(out, s) + } + } + return out +} diff --git a/modules/connectors/internal/catalogue/lifecycle.go b/modules/connectors/internal/catalogue/lifecycle.go new file mode 100644 index 000000000..ce6b527bb --- /dev/null +++ b/modules/connectors/internal/catalogue/lifecycle.go @@ -0,0 +1,127 @@ +package catalogue + +import ( + "errors" + "fmt" +) + +// Update policies. Manual is the default; an auto package follows the +// catalogue, with an update that asks for more still waiting for approval. +const ( + PolicyManual = "manual" + PolicyAuto = "auto" +) + +var ( + // ErrBeyondDeclared means an operator accepted grants the extension never + // declared. + ErrBeyondDeclared = errors.New("the extension doesn't declare those grants") + // ErrNoPending means there is no update waiting for approval. + ErrNoPending = errors.New("the extension has no update waiting for approval") + // ErrNoPrevious means there is no earlier version to roll back to. + ErrNoPrevious = errors.New("the extension has no previous version") + // ErrInvalidPolicy means the update policy is neither manual nor auto. + ErrInvalidPolicy = errors.New("the update policy is manual or auto") +) + +// Pin is one version of an extension: a binary on this host with the digest +// it was verified against and the grants it declared. +type Pin struct { + Version string `json:"version"` + Digest string `json:"digest"` + Path string `json:"path"` + Declared Grants `json:"declared"` +} + +// State is an installed extension's place in its lifecycle. The live pin is +// Current, and the grants it runs with are Accepted, never what any pin +// declares: a pin gains no grant until an operator accepts it. +type State struct { + Policy string `json:"policy"` + Current Pin `json:"current"` + // Accepted is what the operator granted Current. Nil until they have. + Accepted *Grants `json:"accepted"` + // Pending is an update that asks for more than Accepted. Current and + // Accepted stay as they were until it is approved. + Pending *Pin `json:"pending,omitzero"` + // Previous is the pin an update, approval or rollback replaced, with the + // grants accepted against it, so a rollback restores both. + Previous *Pin `json:"previous,omitzero"` + PreviousAccepted *Grants `json:"previous_accepted,omitzero"` +} + +// Install starts the lifecycle of pin: nothing is accepted yet. +func Install(pin Pin) State { + return State{Policy: PolicyManual, Current: pin} +} + +// Accept records the operator's grants for the current pin. They may be fewer +// than the pin declares and never more. +func (s State) Accept(grants Grants) (State, error) { + grants = grants.Normalised() + if !s.Current.Declared.Covers(grants) { + return State{}, ErrBeyondDeclared + } + s.Accepted = &grants + return s, nil +} + +// Widens reports whether next asks for more than the operator accepted. An +// extension never accepted holds nothing, so any pin is wider. +func (s State) Widens(next Pin) bool { + if s.Accepted == nil { + return true + } + return !s.Accepted.Covers(next.Declared) +} + +// Offer moves to next, a version the catalogue lists. One that asks for no +// more than was accepted applies at once, keeping the accepted grants. One that +// asks for more waits as pending. applied is false when nothing took effect +// yet. +func (s State) Offer(next Pin) (_ State, applied bool) { + if next.Digest == s.Current.Digest || (s.Pending != nil && s.Pending.Digest == next.Digest) { + return s, false + } + if s.Widens(next) { + s.Pending = &next + return s, false + } + return s.swap(next, s.Accepted), true +} + +// Approve applies the pending update and accepts the grants it declares. +func (s State) Approve() (State, error) { + if s.Pending == nil { + return State{}, ErrNoPending + } + declared := s.Pending.Declared + return s.swap(*s.Pending, &declared), nil +} + +// Rollback restores the previous pin with the grants accepted against it, never +// the grants of the pin it replaces. The replaced pin becomes the previous one. +func (s State) Rollback() (State, error) { + if s.Previous == nil { + return State{}, ErrNoPrevious + } + return s.swap(*s.Previous, s.PreviousAccepted), nil +} + +// swap makes next the live pin with accepted as its grants, and keeps the pin +// it replaces for a rollback. +func (s State) swap(next Pin, accepted *Grants) State { + replaced, held := s.Current, s.Accepted + s.Previous, s.PreviousAccepted = &replaced, held + s.Current, s.Accepted, s.Pending = next, accepted, nil + return s +} + +// SetPolicy changes whether the extension follows the catalogue on its own. +func (s State) SetPolicy(policy string) (State, error) { + if policy != PolicyManual && policy != PolicyAuto { + return State{}, fmt.Errorf("%w, not %q", ErrInvalidPolicy, policy) + } + s.Policy = policy + return s, nil +} diff --git a/modules/connectors/internal/store/store.go b/modules/connectors/internal/store/store.go index 76f0b49b2..35860a286 100644 --- a/modules/connectors/internal/store/store.go +++ b/modules/connectors/internal/store/store.go @@ -10,10 +10,65 @@ import ( json "encoding/json/v2" "errors" "fmt" + "time" "github.com/jmoiron/sqlx" ) +// Source is where an organisation's catalogue is read from. +type Source struct { + OrgID string `db:"org_id"` + Location string `db:"location"` + PublicKey string `db:"public_key"` + // Newest is the issue time of the newest catalogue accepted from it. + Newest sql.NullTime `db:"newest_issued_at"` +} + +// CatalogueSource returns the organisation's catalogue source; ok is false when +// none is set. +func (s *Store) CatalogueSource(ctx context.Context, orgID string) (Source, bool, error) { + var out Source + err := s.db.GetContext(ctx, &out, ` + SELECT org_id, location, public_key, newest_issued_at FROM connectors.catalogue_source WHERE org_id = $1`, orgID) + if errors.Is(err, sql.ErrNoRows) { + return Source{}, false, nil + } + if err != nil { + return Source{}, false, fmt.Errorf("read catalogue source: %w", err) + } + return out, true, nil +} + +// PutCatalogueSource records the source. A different source starts a new +// timeline, so the newest issue time is cleared when the location or key +// changes. +func (s *Store) PutCatalogueSource(ctx context.Context, src Source) error { + if _, err := s.db.ExecContext(ctx, ` + INSERT INTO connectors.catalogue_source (org_id, location, public_key) VALUES ($1, $2, $3) + ON CONFLICT (org_id) DO UPDATE + SET newest_issued_at = CASE WHEN connectors.catalogue_source.location = EXCLUDED.location + AND connectors.catalogue_source.public_key = EXCLUDED.public_key + THEN connectors.catalogue_source.newest_issued_at END, + location = EXCLUDED.location, public_key = EXCLUDED.public_key, updated_at = now()`, + src.OrgID, src.Location, src.PublicKey); err != nil { + return fmt.Errorf("put catalogue source: %w", err) + } + return nil +} + +// AdvanceCatalogue records a catalogue issued at issuedAt as accepted and +// reports false when a newer one was accepted already. It never lowers the mark. +func (s *Store) AdvanceCatalogue(ctx context.Context, orgID string, issuedAt time.Time) (bool, error) { + res, err := s.db.ExecContext(ctx, ` + UPDATE connectors.catalogue_source SET newest_issued_at = $2 + WHERE org_id = $1 AND (newest_issued_at IS NULL OR newest_issued_at <= $2)`, orgID, issuedAt) + if err != nil { + return false, fmt.Errorf("record catalogue freshness: %w", err) + } + n, err := res.RowsAffected() + return n > 0, err +} + // Accepted is one package an operator accepted, with the authority it // declared. type Accepted struct { @@ -45,6 +100,13 @@ type Accepted struct { // []byte because database/sql scans NULL into []byte but not into a named // byte slice such as jsontext.Value. Capabilities []byte `db:"capabilities"` + // Enabled says whether the package runs. A package that is not enabled is + // accepted and kept but never launched. + Enabled bool `db:"enabled"` + // Package is the lifecycle of a package installed from the catalogue, as a + // JSON object. It is nil for a package accepted by path. A []byte for the + // same reason as Capabilities. + Package []byte `db:"package"` } // Contracts an accepted package can serve. @@ -113,7 +175,7 @@ func New(db *sqlx.DB) *Store { return &Store{db: db} } func (s *Store) List(ctx context.Context, orgID string) ([]Accepted, error) { var out []Accepted if err := s.db.SelectContext(ctx, &out, ` - SELECT org_id, name, path, sha256, egress, env, settings, contract, credentials, args, url, tools, effects, capabilities + SELECT org_id, name, path, sha256, egress, env, settings, contract, credentials, args, url, tools, effects, capabilities, enabled, package FROM connectors.accepted WHERE org_id = $1 ORDER BY name`, orgID); err != nil { return nil, fmt.Errorf("list connectors: %w", err) } @@ -126,13 +188,14 @@ func (s *Store) Upsert(ctx context.Context, a Accepted) error { return fmt.Errorf("accept %q: unknown contract %q", a.Name, a.Contract) } if _, err := s.db.NamedExecContext(ctx, ` - INSERT INTO connectors.accepted (org_id, name, path, sha256, egress, env, settings, contract, credentials, args, url, tools, effects) - VALUES (:org_id, :name, :path, :sha256, :egress, :env, :settings, :contract, :credentials, :args, :url, :tools, :effects) + INSERT INTO connectors.accepted (org_id, name, path, sha256, egress, env, settings, contract, credentials, args, url, tools, effects, enabled, package) + VALUES (:org_id, :name, :path, :sha256, :egress, :env, :settings, :contract, :credentials, :args, :url, :tools, :effects, :enabled, :package) ON CONFLICT (org_id, name) DO UPDATE SET path = EXCLUDED.path, sha256 = EXCLUDED.sha256, egress = EXCLUDED.egress, env = EXCLUDED.env, settings = EXCLUDED.settings, contract = EXCLUDED.contract, credentials = EXCLUDED.credentials, args = EXCLUDED.args, url = EXCLUDED.url, - tools = EXCLUDED.tools, effects = EXCLUDED.effects, updated_at = now()`, a); err != nil { + tools = EXCLUDED.tools, effects = EXCLUDED.effects, enabled = EXCLUDED.enabled, + package = EXCLUDED.package, updated_at = now()`, a); err != nil { return fmt.Errorf("accept connector: %w", err) } return nil diff --git a/modules/connectors/list.go b/modules/connectors/list.go index b880f2a87..fe67b9220 100644 --- a/modules/connectors/list.go +++ b/modules/connectors/list.go @@ -10,6 +10,7 @@ import ( "github.com/danielgtaylor/huma/v2" "github.com/samcharles93/archie-core/internal/platform/httpapi" + "github.com/samcharles93/archie-core/modules/connectors/internal/catalogue" "github.com/samcharles93/archie-core/modules/connectors/internal/store" ) @@ -40,6 +41,21 @@ type ExtensionView struct { // Capabilities is what a connector declared when it last loaded. It is // absent for other packages and for a connector that has never loaded. Capabilities *Capabilities `json:"capabilities,omitempty"` + // Enabled says whether an accepted package runs. A package accepted by path + // runs while its digest matches; one installed from the catalogue runs once + // its grants are accepted and it is enabled. + Enabled bool `json:"enabled"` + // Managed says the package was installed from the catalogue. + Managed bool `json:"managed"` + DisplayName string `json:"display_name,omitempty"` + Description string `json:"description,omitempty"` + Declared *catalogue.Grants `json:"declared,omitempty"` + Accepted *catalogue.Grants `json:"accepted,omitempty"` + // Pending is an update waiting for approval, with what it adds to the + // accepted grants. + Pending *PendingView `json:"pending,omitempty"` + CanRollback bool `json:"can_rollback"` + UpdatePolicy string `json:"update_policy,omitempty"` } // list returns the accepted packages of one contract, in name order. @@ -96,6 +112,17 @@ func (m *Module) viewOf(a store.Accepted) (ExtensionView, error) { } state, reason, version, schema := m.stateOf(a) view.State, view.Error, view.Version, view.SettingsSchema = state, reason, version, schema + view.Enabled = a.Enabled + if rec, ok, err := packageOf(a); err != nil { + return ExtensionView{}, err + } else if ok { + managed := managedView(rec) + view.Managed = true + view.DisplayName, view.Description = managed.DisplayName, managed.Description + view.Declared, view.Accepted = &managed.Declared, managed.Accepted + view.Pending, view.CanRollback, view.UpdatePolicy = managed.Pending, managed.CanRollback, managed.UpdatePolicy + view.Version = rec.State.Current.Version + } return view, nil } diff --git a/modules/connectors/mcpaccept.go b/modules/connectors/mcpaccept.go index 406ed777a..08b66107c 100644 --- a/modules/connectors/mcpaccept.go +++ b/modules/connectors/mcpaccept.go @@ -209,6 +209,7 @@ func (r mcpRequest) accepted(orgID, name string) (store.Accepted, error) { OrgID: orgID, Name: name, Contract: store.ContractMCP, Path: r.Path, SHA256: r.SHA256, URL: r.URL, Args: r.Args, Env: r.Env, Egress: r.Egress, Settings: settings, Credentials: slots, Tools: tools, Effects: effects, + Enabled: true, }, nil } diff --git a/modules/connectors/module.go b/modules/connectors/module.go index e520c2be1..dd6be66c3 100644 --- a/modules/connectors/module.go +++ b/modules/connectors/module.go @@ -22,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/platform/httpapi" "github.com/samcharles93/archie-core/internal/platform/seal" + "github.com/samcharles93/archie-core/modules/connectors/internal/catalogue" "github.com/samcharles93/archie-core/modules/connectors/internal/store" "github.com/samcharles93/archie-core/modules/governance" ) @@ -38,6 +39,11 @@ type Module struct { store registry sealer *seal.Sealer log *slog.Logger + // sources and fetcher read the catalogue and its artifacts, and pkgMu + // serialises changes to an installed package's lifecycle. + sources sourceStore + fetcher *catalogue.Fetcher + pkgMu sync.Mutex mu sync.RWMutex running map[string]*Connector @@ -89,13 +95,17 @@ type Sealing struct { } // NewModule returns the connectors module for orgID. -func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, sealing Sealing) (*Module, error) { +// +// Artifacts fetched from the catalogue are kept under artifactDir. +func NewModule(db *sqlx.DB, log *slog.Logger, orgID string, sealing Sealing, artifactDir string) (*Module, error) { sealer, err := seal.New(sealing.Identity, sealing.Backups) if err != nil { return nil, err } + st := store.New(db) return &Module{ - orgID: orgID, host: NewHost(log), store: store.New(db), sealer: sealer, log: log, + orgID: orgID, host: NewHost(log), store: st, sealer: sealer, log: log, + sources: st, fetcher: catalogue.NewFetcher(artifactDir), running: map[string]*Connector{}, engines: map[string]*Engine{}, guards: map[string]*Guard{}, failed: map[string]string{}, servers: map[string]*Server{}, stops: map[string]context.CancelFunc{}, }, nil @@ -380,6 +390,8 @@ func (m *Module) Register(api huma.API) { OperationID: "list-credential-kinds", Method: http.MethodGet, Path: "/api/credential-kinds", Summary: "List credential kinds and the fields each takes", Tags: tags, }, listCredentialKinds) + m.registerCatalogue(api) + m.registerExtensions(api) } type acceptRequest struct { @@ -432,7 +444,12 @@ func (m *Module) acceptAs(ctx context.Context, in *packageInput, contract string Egress: req.Egress, Env: req.Env, Settings: settings, Contract: contract, Credentials: slots, // Only an MCP server declares its tools at acceptance; the column // holds a list either way. - Tools: jsontext.Value(`[]`), + Tools: jsontext.Value(`[]`), Enabled: true, + } + if managed, err := m.managedBy(ctx, name); err != nil { + return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the accepted packages") + } else if managed { + return nil, huma.NewError(http.StatusConflict, name+" is installed from the catalogue; change it through its extension page") } if err := m.store.Upsert(ctx, a); err != nil { return nil, huma.NewError(http.StatusInternalServerError, err.Error()) @@ -459,6 +476,10 @@ func (m *Module) acceptAs(ctx context.Context, in *packageInput, contract string // connector, a secret engine and an MCP server never share a name. func (m *Module) launch(ctx context.Context, a store.Accepted) error { m.releaseAs(a.Name, a.Contract) + if !a.Enabled { + m.markStopped(a.Name) + return nil + } if a.Contract == store.ContractMCP { return m.startServer(ctx, a) } @@ -494,16 +515,7 @@ func (m *Module) launch(ctx context.Context, a store.Accepted) error { return nil } if a.Contract == store.ContractGuard { - g, err := m.host.StartGuard(ctx, spec) - if err != nil { - return m.fail(ctx, a.Name, err) - } - m.mu.Lock() - m.guards[a.Name] = g - delete(m.failed, a.Name) - m.mu.Unlock() - m.log.InfoContext(ctx, "guard started", "guard", a.Name) - return nil + return m.startGuard(ctx, a, spec) } c, err := m.host.Start(ctx, spec) if err != nil { @@ -525,6 +537,20 @@ func (m *Module) launch(ctx context.Context, a store.Accepted) error { return nil } +// startGuard launches a guard and records it as running. +func (m *Module) startGuard(ctx context.Context, a store.Accepted, spec Spec) error { + g, err := m.host.StartGuard(ctx, spec) + if err != nil { + return m.fail(ctx, a.Name, err) + } + m.mu.Lock() + m.guards[a.Name] = g + delete(m.failed, a.Name) + m.mu.Unlock() + m.log.InfoContext(ctx, "guard started", "guard", a.Name) + return nil +} + // checkDeclarations refuses declarations core can't work with before they are // stored: a reserved delivery whose schema rejects what core sends, or saved // configuration that the new declarations break. The connector's previous @@ -630,6 +656,13 @@ func (m *Module) releaseAs(name, contract string) { m.contracts[name] = contract } +// markStopped forgets a name's failure now that it is not meant to run. +func (m *Module) markStopped(name string) { + m.mu.Lock() + defer m.mu.Unlock() + delete(m.failed, name) +} + func (m *Module) fail(ctx context.Context, name string, err error) error { m.mu.Lock() delete(m.running, name) diff --git a/modules/connectors/packages.go b/modules/connectors/packages.go new file mode 100644 index 000000000..1c8cdf0cb --- /dev/null +++ b/modules/connectors/packages.go @@ -0,0 +1,495 @@ +package connectors + +import ( + "bytes" + "context" + json "encoding/json/v2" + "errors" + "fmt" + "maps" + "net/http" + "slices" + "time" + + "github.com/danielgtaylor/huma/v2" + + "github.com/samcharles93/archie-core/internal/platform/httpapi" + "github.com/samcharles93/archie-core/modules/connectors/internal/catalogue" + "github.com/samcharles93/archie-core/modules/connectors/internal/store" +) + +// packageRecord is the lifecycle of an extension installed from the catalogue, +// as it is stored on its accepted row. +type packageRecord struct { + DisplayName string `json:"display_name"` + Description string `json:"description"` + State catalogue.State `json:"state"` +} + +// packageOf reads a row's lifecycle; ok is false for a package accepted by +// path. +func packageOf(a store.Accepted) (rec packageRecord, ok bool, err error) { + if len(a.Package) == 0 { + return packageRecord{}, false, nil + } + if err := json.Unmarshal(a.Package, &rec); err != nil { + return packageRecord{}, false, fmt.Errorf("package %q: %w", a.Name, err) + } + return rec, true, nil +} + +// rowOf derives the row a lifecycle stores: the live pin is what runs and the +// accepted grants are what it runs with. Nothing is enabled before an +// operator has accepted grants, and a slot the grants don't name is dropped. +func rowOf(base store.Accepted, rec packageRecord, enabled bool) (store.Accepted, error) { + raw, err := json.Marshal(rec) + if err != nil { + return store.Accepted{}, err + } + slots, err := decodeSlots(base.Credentials) + if err != nil { + return store.Accepted{}, err + } + granted := catalogue.Grants{} + if g := rec.State.Accepted; g != nil { + granted = *g + } + maps.DeleteFunc(slots, func(slot, _ string) bool { return !slices.Contains(granted.Credentials, slot) }) + base.Credentials, err = json.Marshal(slots, json.FormatNilMapAsNull(false)) + if err != nil { + return store.Accepted{}, err + } + base.Path, base.SHA256 = rec.State.Current.Path, rec.State.Current.Digest + base.Egress, base.Env = granted.Egress, granted.Env + base.Package = raw + base.Enabled = enabled && rec.State.Accepted != nil + return base, nil +} + +// restarts reports whether a change to the row needs the process restarted. +func restarts(old, next store.Accepted) bool { + return old.Path != next.Path || old.SHA256 != next.SHA256 || old.Enabled != next.Enabled || + !slices.Equal(old.Egress, next.Egress) || !slices.Equal(old.Env, next.Env) || + !bytes.Equal(old.Credentials, next.Credentials) || !bytes.Equal(old.Settings, next.Settings) +} + +// managedBy reports whether name is installed from the catalogue. +func (m *Module) managedBy(ctx context.Context, name string) (bool, error) { + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + return false, err + } + i := slices.IndexFunc(accepted, func(a store.Accepted) bool { return a.Name == name }) + return i >= 0 && len(accepted[i].Package) > 0, nil +} + +var errNotManaged = errors.New("not installed from the catalogue") + +// find returns the installed package name. +func (m *Module) find(ctx context.Context, name string) (store.Accepted, packageRecord, error) { + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + return store.Accepted{}, packageRecord{}, err + } + i := slices.IndexFunc(accepted, func(a store.Accepted) bool { return a.Name == name }) + if i < 0 { + return store.Accepted{}, packageRecord{}, fmt.Errorf("no extension named %s: %w", name, errNotManaged) + } + rec, ok, err := packageOf(accepted[i]) + if err != nil { + return store.Accepted{}, packageRecord{}, err + } + if !ok { + return store.Accepted{}, packageRecord{}, fmt.Errorf("%s is accepted by path: %w", name, errNotManaged) + } + return accepted[i], rec, nil +} + +// lifecycleError maps what a lifecycle change returns to an HTTP status. +func (m *Module) lifecycleError(ctx context.Context, err error) error { + var status *huma.ErrorModel + switch { + case errors.As(err, &status): + return err + case errors.Is(err, errNotManaged): + return huma.NewError(http.StatusNotFound, err.Error()) + case errors.Is(err, catalogue.ErrBeyondDeclared), errors.Is(err, catalogue.ErrDigestMismatch): + return huma.NewError(http.StatusUnprocessableEntity, err.Error()) + case errors.Is(err, catalogue.ErrNoPending), errors.Is(err, catalogue.ErrNoPrevious): + return huma.NewError(http.StatusConflict, err.Error()) + case errors.Is(err, catalogue.ErrInvalidPolicy): + return huma.NewError(http.StatusBadRequest, err.Error()) + case errors.Is(err, errNoSource), errors.Is(err, catalogue.ErrBadSignature), errors.Is(err, catalogue.ErrStale): + return catalogueError(err) + } + m.log.ErrorContext(ctx, "extension lifecycle", "error", err) + return huma.NewError(http.StatusInternalServerError, "couldn't change the extension") +} + +// replace stores next and restarts what changed. When the new row won't start +// or its settings are refused, the old row is put back and restarted, so a +// failed change leaves the extension as it was. +func (m *Module) replace(ctx context.Context, old, next store.Accepted) error { + if err := m.store.Upsert(ctx, next); err != nil { + return err + } + if !restarts(old, next) { + return nil + } + bg := context.WithoutCancel(ctx) + err := m.launch(bg, next) + if err == nil && next.Enabled { + err = m.checkAccepted(next) + } + if err == nil { + return nil + } + if restoreErr := m.store.Upsert(bg, old); restoreErr != nil { + return errors.Join(err, restoreErr) + } + _ = m.launch(bg, old) + return huma.NewError(http.StatusUnprocessableEntity, err.Error()) +} + +// extensionName is the path of an operation on one installed extension. +type extensionName struct { + Name string `path:"name"` +} + +// Lifecycle is the result of a change to an installed extension. +type Lifecycle struct { + Name string `json:"name"` + Version string `json:"version"` + Digest string `json:"digest"` + // Pending is set when an update asks for more than was accepted and waits + // for approval. + Pending bool `json:"pending"` + Enabled bool `json:"enabled"` +} + +func lifecycleOf(a store.Accepted, rec packageRecord) *httpapi.Reply[Lifecycle] { + return httpapi.OK(Lifecycle{ + Name: a.Name, Version: rec.State.Current.Version, Digest: rec.State.Current.Digest, + Pending: rec.State.Pending != nil, Enabled: a.Enabled, + }) +} + +// change applies one lifecycle step to an installed extension. +func (m *Module) change(ctx context.Context, name, action string, step func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error)) (*httpapi.Reply[Lifecycle], error) { + m.pkgMu.Lock() + defer m.pkgMu.Unlock() + old, rec, err := m.find(ctx, name) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + next, nextRec, err := step(old, rec) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + if err := m.replace(ctx, old, next); err != nil { + return nil, m.lifecycleError(ctx, err) + } + m.log.InfoContext(ctx, "extension changed", "audit", true, "action", "extension."+action, "package", name, + "digest", nextRec.State.Current.Digest) + return lifecycleOf(next, nextRec), nil +} + +func (m *Module) install(ctx context.Context, in *extensionName) (*httpapi.Reply[Lifecycle], error) { + m.pkgMu.Lock() + defer m.pkgMu.Unlock() + doc, location, err := m.readCatalogue(ctx) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + entry, ok := doc.Entry(in.Name) + if !ok { + return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("the catalogue doesn't list %s", in.Name)) + } + if !slices.Contains(catalogueContracts, entry.Contract) { + return nil, huma.NewError(http.StatusUnprocessableEntity, fmt.Sprintf("%s serves contract %q, which a catalogue doesn't install", entry.Name, entry.Contract)) + } + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + if slices.ContainsFunc(accepted, func(a store.Accepted) bool { return a.Name == entry.Name }) { + return nil, huma.NewError(http.StatusConflict, entry.Name+" is already installed") + } + pin, err := m.fetchPin(ctx, location, entry) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + rec := packageRecord{DisplayName: entry.DisplayName, Description: entry.Description, State: catalogue.Install(pin)} + row, err := rowOf(store.Accepted{ + OrgID: m.orgID, Name: entry.Name, Contract: entry.Contract, + Settings: []byte(`{}`), Credentials: []byte(`{}`), Tools: []byte(`[]`), + }, rec, false) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + if err := m.store.Upsert(ctx, row); err != nil { + return nil, m.lifecycleError(ctx, err) + } + m.log.InfoContext(ctx, "extension installed", "audit", true, "action", "extension.install", "package", entry.Name, "digest", pin.Digest) + return lifecycleOf(row, rec), nil +} + +// fetchPin downloads the artifact an entry names and keeps it only if it +// hashes to the digest the entry pins. +func (m *Module) fetchPin(ctx context.Context, location string, entry catalogue.Entry) (catalogue.Pin, error) { + ref, err := catalogue.Resolve(location, entry.Artifact) + if err != nil { + return catalogue.Pin{}, err + } + path, err := m.fetcher.Fetch(ctx, entry.Name, ref, entry.Digest) + if err != nil { + return catalogue.Pin{}, fmt.Errorf("%s: %w", entry.Name, err) + } + return catalogue.Pin{Version: entry.Version, Digest: entry.Digest, Path: path, Declared: entry.Grants}, nil +} + +// grantsRequest is what an operator accepts for an installed extension. +type grantsRequest struct { + Egress []string `json:"egress,omitzero" doc:"Every host the extension may reach; empty leaves it unconfined, which only an extension that declares no hosts may be given"` + Env []string `json:"env,omitzero" doc:"Host environment variables the extension may see"` + // Credentials maps each credential slot to a credential by name. + Credentials map[string]string `json:"credentials,omitzero"` + Settings map[string]string `json:"settings,omitzero"` +} + +// acceptGrants records what an operator grants an installed extension. A +// request with no body accepts exactly what the extension declared; a body may +// narrow those grants and names the credential each slot resolves through. +type acceptGrantsInput struct { + Name string `path:"name"` + Body *grantsRequest +} + +func (m *Module) acceptGrants(ctx context.Context, in *acceptGrantsInput) (*httpapi.Reply[Lifecycle], error) { + return m.change(ctx, in.Name, "accept", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + grants := rec.State.Current.Declared + if in.Body != nil { + grants = catalogue.Grants{Egress: in.Body.Egress, Env: in.Body.Env, Credentials: slices.Collect(maps.Keys(in.Body.Credentials))} + settings, err := json.Marshal(in.Body.Settings, json.FormatNilMapAsNull(false)) + if err != nil { + return store.Accepted{}, rec, err + } + slots, err := json.Marshal(in.Body.Credentials, json.FormatNilMapAsNull(false)) + if err != nil { + return store.Accepted{}, rec, err + } + a.Settings, a.Credentials = settings, slots + } + state, err := rec.State.Accept(grants) + if err != nil { + return store.Accepted{}, rec, err + } + rec.State = state + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +// putExtensionSettings changes an installed extension's settings and restarts +// it with them. A setting whose name ends in _ref names a secret engine and +// key; its value is never stored here. +type extensionSettingsInput struct { + Name string `path:"name"` + + Body struct { + Settings map[string]string `json:"settings,omitzero"` + } +} + +func (m *Module) putExtensionSettings(ctx context.Context, in *extensionSettingsInput) (*httpapi.Reply[Lifecycle], error) { + settings, err := json.Marshal(in.Body.Settings, json.FormatNilMapAsNull(false)) + if err != nil { + return nil, huma.NewError(http.StatusBadRequest, err.Error()) + } + return m.change(ctx, in.Name, "settings", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + a.Settings = settings + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +func (m *Module) updateExtension(ctx context.Context, in *extensionName) (*httpapi.Reply[Lifecycle], error) { + doc, location, err := m.readCatalogue(ctx) + if err != nil { + return nil, m.lifecycleError(ctx, err) + } + return m.updateFrom(ctx, doc, location, in.Name) +} + +// updateFrom moves name to the digest doc pins for it. An update that asks for +// no more than was accepted applies; one that asks for more waits for approval +// and changes nothing that runs. +func (m *Module) updateFrom(ctx context.Context, doc catalogue.Document, location, name string) (*httpapi.Reply[Lifecycle], error) { + entry, ok := doc.Entry(name) + if !ok { + return nil, huma.NewError(http.StatusNotFound, fmt.Sprintf("the catalogue doesn't list %s", name)) + } + return m.change(ctx, name, "update", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + if entry.Digest == rec.State.Current.Digest || (rec.State.Pending != nil && rec.State.Pending.Digest == entry.Digest) { + return a, rec, nil + } + pin, err := m.fetchPin(ctx, location, entry) + if err != nil { + return store.Accepted{}, rec, err + } + rec.State, _ = rec.State.Offer(pin) + rec.DisplayName, rec.Description = entry.DisplayName, entry.Description + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +func (m *Module) approveUpdate(ctx context.Context, in *extensionName) (*httpapi.Reply[Lifecycle], error) { + return m.change(ctx, in.Name, "approve_update", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + state, err := rec.State.Approve() + if err != nil { + return store.Accepted{}, rec, err + } + rec.State = state + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +func (m *Module) rollback(ctx context.Context, in *extensionName) (*httpapi.Reply[Lifecycle], error) { + return m.change(ctx, in.Name, "rollback", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + state, err := rec.State.Rollback() + if err != nil { + return store.Accepted{}, rec, err + } + rec.State = state + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +type extensionPolicyInput struct { + Name string `path:"name"` + + Body struct { + Policy string `json:"policy" enum:"manual,auto"` + } +} + +func (m *Module) setPolicy(ctx context.Context, in *extensionPolicyInput) (*httpapi.Reply[Lifecycle], error) { + return m.change(ctx, in.Name, "update_policy", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + state, err := rec.State.SetPolicy(in.Body.Policy) + if err != nil { + return store.Accepted{}, rec, err + } + rec.State = state + row, err := rowOf(a, rec, a.Enabled) + return row, rec, err + }) +} + +type extensionEnabledInput struct { + Name string `path:"name"` + + Body struct { + Enabled bool `json:"enabled"` + } +} + +func (m *Module) setEnabled(ctx context.Context, in *extensionEnabledInput) (*httpapi.Reply[Lifecycle], error) { + enabled := in.Body.Enabled + return m.change(ctx, in.Name, "enable", func(a store.Accepted, rec packageRecord) (store.Accepted, packageRecord, error) { + if enabled && rec.State.Accepted == nil { + return store.Accepted{}, rec, huma.NewError(http.StatusConflict, "accept the extension's grants before enabling it") + } + row, err := rowOf(a, rec, enabled) + return row, rec, err + }) +} + +// UpdateAuto updates every extension whose policy is auto to the digest the +// catalogue pins. One that fails doesn't stop the rest. It does nothing when no +// catalogue source is set. +func (m *Module) UpdateAuto(ctx context.Context) error { + accepted, err := m.store.List(ctx, m.orgID) + if err != nil { + return err + } + var auto []string + for _, a := range accepted { + if rec, ok, err := packageOf(a); err == nil && ok && rec.State.Policy == catalogue.PolicyAuto { + auto = append(auto, a.Name) + } + } + if len(auto) == 0 { + return nil + } + doc, location, err := m.readCatalogue(ctx) + if err != nil { + return err + } + var errs []error + for _, name := range auto { + if _, err := m.updateFrom(ctx, doc, location, name); err != nil { + errs = append(errs, fmt.Errorf("update %s: %w", name, err)) + } + } + return errors.Join(errs...) +} + +// WatchUpdates runs UpdateAuto every interval until ctx ends. +func (m *Module) WatchUpdates(ctx context.Context, interval time.Duration) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + if err := m.UpdateAuto(ctx); err != nil { + m.log.WarnContext(ctx, "update extensions from the catalogue", "error", err) + } + } + } +} + +// ManagedView is what the dashboard shows of an extension installed from the +// catalogue: what each version declared next to what the operator accepted. +type ManagedView struct { + DisplayName string `json:"display_name"` + Description string `json:"description"` + Declared catalogue.Grants `json:"declared"` + // Accepted is nil until the operator accepts grants. + Accepted *catalogue.Grants `json:"accepted"` + // Pending is an update waiting for approval, with what it adds to the + // accepted grants. + Pending *PendingView `json:"pending"` + CanRollback bool `json:"can_rollback"` + UpdatePolicy string `json:"update_policy" enum:"manual,auto"` +} + +// PendingView is an update that asks for more than was accepted. +type PendingView struct { + Version string `json:"version"` + Digest string `json:"digest"` + Added catalogue.Grants `json:"added"` +} + +func managedView(rec packageRecord) *ManagedView { + s := rec.State + view := &ManagedView{ + DisplayName: rec.DisplayName, Description: rec.Description, + Declared: s.Current.Declared.Normalised(), CanRollback: s.Previous != nil, UpdatePolicy: s.Policy, + } + var held catalogue.Grants + if s.Accepted != nil { + held = s.Accepted.Normalised() + view.Accepted = &held + } + if s.Pending != nil { + view.Pending = &PendingView{ + Version: s.Pending.Version, Digest: s.Pending.Digest, Added: held.Added(s.Pending.Declared.Normalised()), + } + } + return view +} diff --git a/ui/src/settings/AcceptedExtensionsPage.vue b/ui/src/settings/AcceptedExtensionsPage.vue deleted file mode 100644 index dfe9ec36e..000000000 --- a/ui/src/settings/AcceptedExtensionsPage.vue +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - router.replace({ query: { kind: String(k) } })"> - - - {{ group.label }} - - {{ k.label }} - - - - - - - - diff --git a/ui/src/settings/AgentModelsPage.vue b/ui/src/settings/AgentModelsPage.vue deleted file mode 100644 index 18feac6c0..000000000 --- a/ui/src/settings/AgentModelsPage.vue +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - {{ error }} - Retry - - Loading models… - - Set the default alias. - No models - - - {{ alias }} - - - - - - - No key - {{ c.name }} - - - - Test - Save - - - - {{ tested[alias] }} - {{ rowError[alias] }} - - - - Add alias - - - -
{{ error }}
Loading models…
Set the default alias.
{{ tested[alias] }}
{{ rowError[alias] }}