mirror of
https://github.com/mudler/LocalAI.git
synced 2026-11-04 11:33:22 -05:00
* feat(addons): add the add-on runtime, navigation and first-party add-ons Add the add-on runtime to LocalAI: manifests, the host operation broker, admin gating by account role, the add-on UI bundle builder and the SDK for add-on pages. Move the navigation to Chat, Models, Add-ons and Settings, with pinned add-ons in the sidebar and an Add-ons drawer. Ship the first-party add-ons (voice, face, fine-tuning, quantization) as packages for linux/amd64, linux/arm64 and darwin/arm64. Intel macOS (darwin/amd64) is enabled in the platform files. Squashed from the review branch. The tree is unchanged; the pre-squash history is kept on a local backup branch. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(gosec): tighten file modes and annotate false positives Write job state, configs and imported models with mode 0600, clean the paths that are read from disk, and mark the route tables, the header name and the retry jitter as reviewed false positives. Assisted-by: Claude Code:claude-sonnet-5-5 gosec Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): keep API keys off recovery mutations and same-site cookies POST /api/addons/dispatch forwarded the recovery and update mutations to the owner for any administrator key, which skipped the browser-session rule of their own routes. Refuse those actions there. Also apply the same-origin rule to the legacy token cookie, not only the session cookie, because the global CSRF check lets same-site requests through. Do not fail the start of LocalAI when an add-on library cannot be refreshed; log it and keep the installed add-ons up. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): refuse a reinstall that would migrate retained data unbacked An upgrade takes a verified backup before the new package migrates the data. A reinstall over retained data has no running generation to stop, so the new package would migrate it without one. Refuse a reinstall whose data format differs from the retained one. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): release the manager lock while a release is fetched Submit held the state lock across the catalog lookup and the package download, so the apps list and status reads stalled for the whole install. Serialize Submit and Reconcile with their own lock and drop the state lock around the fetch only. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): kill an add-on only after three missed health probes One slow 100 ms probe killed a busy add-on and dropped the work it was serving. Require three misses in a row. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): move settings under the operate hub The Operate hub already has Settings as a tab, so the sidebar no longer shows a separate Settings entry. The /app/settings route stays, and the hub tab still reaches it. The navigation test now expects Chat, Models and Add-ons as the primary items. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): accept same-host browser requests that carry no fetch metadata Browsers do not send Sec-Fetch-* headers over plain HTTP on an address that is not localhost, so every add-on request from a LAN address was refused as cross-site. Keep refusing a Sec-Fetch-Site value other than same-origin. When the header is absent, accept the request only if its Origin, or failing that its Referer, names the same host and port as the request. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): show the server's reason when an add-on request is refused A 403 or 400 answer showed a generic message and hid the reason in the response body, so a refusal by the browser guard could not be told from a missing permission. Show error.message from the body as the server sent it, and keep the generic text when the body has none. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): run the add-on owner in the normal binary The owner was only enabled when an embedding host set the add-on config, so a normal run answered every add-on request as unavailable. Add --addons-enabled (default true) and --addons-ownership-mode (default named-user, or local-owner; anything else stops startup). The node is its own owner: state lives under the data path, and the carrier uses loopback keys made at each start and kept in memory. --addons-enabled=false turns the owner off completely. Named-user mode needs the account database. With authentication off the owner stays off and a warning says how to use local-owner. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): keep the loopback owner address stable across restarts The single-node loopback transport took a new ephemeral port at each start. The owner stores its address in its identity file and refuses to start when the address differs, so every start after the first failed with "invalid addon owner configuration". Choose the port once, keep it in the add-on state root (loopback-port, mode 0600) and reuse it. If the port is taken at a later start, stop with an error that names the port and --addons-enabled=false, because another port would break the identity check. The keys and certificate stay in memory. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): let add-on pages keep the host modules they were written against A page written for the web UI imports React, the router, i18next and a few shared components. The add-on builder refuses a bundle that holds a host file, so moving such a page into an add-on meant rewriting it. Add a compat mode to the shared builder. It answers those imports from a directory of stand-ins that read the SDK, bundles the dynamic import of the page code into the one output file, and collects the style sheets the page imports. A relative import that is not in the add-on's own source resolves to the file at the same path under the compat directory. Widen the SDK for these pages, without a version change: more host components (spinner, modal, lightbox, model picker) and hooks (models, resources, voice profiles, operations, MCP client), a style on icons, the icon names the pages draw, sdk.navigation.openApp to open a page of the app (only addresses under /app, and the way back as the only state) and sdk.portal. The shell passes the query string to the page. The stand-ins cover the router (the tab in the path, the model in the query), the translator over the host locale files, the core API calls through sdk.core.fetch, and byte copies of the pure helpers, with tests that keep the copies equal to the originals. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): ship Studio and Talk as add-ons instead of built-in pages Studio and Talk were compiled into the web bundle and always listed in the Add-ons drawer. They are add-ons now, like Voice, Face, fine-tuning and quantization: a person installs and removes them, the drawer lists them only while they are installed, and they are packaged the same way. Move the pages, their components, hooks and helpers into addons/studio and addons/talk. Each has a manifest, a UI bundle, a release platform matrix and a catalog template, and is built by the compat mode of the shared builder. The core API stays where it is: the pages call it with the signed-in person's session. The history keeps its browser storage keys. The unit tests of the moved helpers move with them. In the core, drop the two pages, the Create group of the sidebar (Chat joins Home and Models in the old layout), the built-in catalog entries (the catalog is empty) and the talk locale namespace. The old addresses (/app/studio, /app/talk and the generator links) redirect to the add-on pages, with the model as ?model=. The Home /studio command is offered only while the Studio add-on is installed and opens it. The release workflow and its tests now list seven add-ons. Assisted-by: Claude Code:claude-sonnet-5-5 go node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): run the Studio and Talk specs against the add-on bundles The pages are add-ons now, so a spec that draws one installs the real built bundle first: the apps list names it and its module and style sheet are served from the build output, the way the add-on owner serves them. studio-addon.js holds that helper, and the Studio and Talk fixtures call it. Update the specs for the new addresses (/app/addons/studio/studio, the model as ?model=), the session permission the add-on route needs, the legacy redirects, the Home /studio command that appears only while the add-on is installed, and the sidebar without a Create group. Add specs for the add-ons themselves: every tab draws with no script error, the shell titles the page once, the old addresses say so when the add-on is not installed, a button of the page reaches a core page with the way back, and the drawer lists both only after they are installed. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): centre the icon of a built-in tile in its icon box The drawer drew the icon of a built-in tile (Studio's palette was one) in a bare 40px box. Only the placeholder box was laid out as a centred grid with the 20px size, so the real icon sat in the top-left corner at the size of the page text. Give the built-in tile the same classes as the placeholder box, and check in a browser that the icon is centred. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(addons): describe Studio and Talk as add-ons Replace the built-in list (now empty) with the Studio and Talk add-on sections, the new targets of the old links, the Home command, and the seven packaged add-ons. Describe the SDK additions the pages use: components, hooks, icons, openApp and portal. Say on the Studio page that it is installed from the drawer. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): click the shell title in the Studio slash-key spec The add-on shell titles the page, and the Studio overview no longer draws a second title, but the generator tabs keep theirs, so a bare h1 locator matched two elements. Click the first, which is the shell's. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): read the loopback port file through os.Root The persisted port file was read with a path built from the add-on root, which gosec reports as a file inclusion through a variable. Open it through os.Root so the name cannot leave the root, even by a symbolic link, and require a regular file with no group or other access. A looser file now fails closed instead of being trusted. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): expect Settings under the Operate hub, not the sidebar Settings moved out of the sidebar into the Operate hub. The navigation spec still expected a sidebar link. Assert that the sidebar has no Settings entry, and that the Operate hub opens Settings as a tab. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): drop Settings from the 404 places Settings is a tab of the Operate hub and not a destination of its own, so the 404 page does not list it. Assert that it is absent. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): drive the media history and TTS specs through the Studio add-on The image, video, sound and speech generators are pages of the Studio add-on now, and the old addresses redirect to it. Install the add-on with the Studio fixtures, open its pages and use its compose card, so the specs run against the page that exists. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): count the first-run steps, not every list item The recommended models are a list inside the second step, so counting all list items read 3 only while the models were still loading. Count the step elements, which does not depend on the load order. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): fail the node refresh by state, not by call count The spec served the list to the first request and failed the rest. The first request is not always the one from the page, so the page could get the failure first and show no list. Serve the list until the page shows it, then fail the refreshes. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): default to local-owner when auth is off A server without authentication has no accounts, so named-user left the add-on owner off. Resolve the unset mode in one place: local-owner when authentication is off, named-user when it is on. A flag or environment value still wins, and an unknown value is still refused. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): register an official add-on library by default Add gallery/addons/index.yaml, a YAML index of the four first-party add-ons (fine-tuning, quantization, voice, face) in the library format. The entries come from addonpkg catalog over locally built linux/amd64 packages, so the digests pin those images. On the first start, when the owner has no sources.json, register the raw GitHub URL of that index on master as the "official" library and write the file. The file is then the record: a library the admin removes stays removed, and an existing file is never changed. --addons-official-library=false skips the default. Assisted-by: Claude Code:claude-sonnet-5-5 go node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(addons): document the ownership default and the official library Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): list Studio and Talk in the first-party index Add the Studio and Talk entries to gallery/addons/index.yaml next to the four existing ones. They come from addonpkg catalog over locally built linux/amd64 packages, so the digests pin those images. Assisted-by: Claude Code:claude-sonnet-5-5 go node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): expect Studio and Talk in the official index The official index now lists six add-ons. Update the test that decodes it so it accepts Studio and Talk. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): add a real icon to each first-party add-on Each icon is a 256 by 256 PNG: a rounded square in one colour of the UI kit palette with a white outline glyph from the kit sprite. Fine-tuning uses sliders, quantization layers, voice waveform, face user, studio palette, talk phone and tables database. They were rendered with headless Chrome from the sprite paths. Assisted-by: Claude Code:claude-sonnet-5-5 chrome Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): ship a committed icon.png instead of the generated square The shared UI builder and the Tables builder copy ui/icon.png from the add-on source tree to the package unchanged when the file exists. They write the generated placeholder only when it does not. Add a fixture with an icon and a test for both cases. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): list the tables add-on in the first-party index Add the Tables entry to gallery/addons/index.yaml next to the six existing ones. It comes from addonpkg catalog over a locally built linux/amd64 package, so the digest pins that image. The official index test now expects seven add-ons. Assisted-by: Claude Code:claude-sonnet-5-5 go node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): make the Voice add-on one page with tabs The Voice add-on had four pages: recognition, library, create and speech, and the drawer listed two of them. It now has one page, "voice", and one drawer entry. Recognition, Library, Text to speech and From a recording are tabs of that page, drawn with the same tab strip the recognition page already used. The tab is the first path segment (voice/library), as in the Studio add-on, and the form that adds a voice is a view of the Library tab (voice/library/new). The old page names stay declared in the manifest. Each one redirects to its tab and keeps the query string, so saved links still open. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): send the old voice routes to the Voice tabs /app/voice, /app/voice-library and /app/voice-library/new now open the matching tab of the Voice add-on page while the add-on is installed and running, and the core pages otherwise. The moved-page helper compares the first segment of the target with the declared pages, so a target can name a tab. The docs and the navigation spec list the new addresses. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): cover the Voice tabs and the old page redirects The add-on unit tests render the tabs instead of the four pages. The Playwright spec checks the tab strip, the address of each tab, the missing Library tab for a member, and that the old page addresses of the add-on redirect to their tab with the query string kept. The migration table test follows the new targets. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(studio): keep the model menu inside a narrow screen The Home model picker sizes its menu from the viewport, and assumes the picker sits at the page gutter. In the Studio composer card the field is further in, so on a 390px screen the menu of the Sound expansion picker ended 7px past the right edge. Size the menu from the field it opens under instead. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): build the add-on bundles once for the whole run Each worker built the Studio and Talk bundles into the same dist directory, and the build empties that directory first. A worker that started, or restarted after a failure, removed the files another worker was serving, so a Studio spec saw "the style sheet of the add-on could not be loaded". Build once in a global setup before the workers start. A run without the setup still builds per worker. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): register the worker when the add-node spec says so The mocked roster added the worker on the third read of /api/nodes. The shell and the page both read it, and which one asks first depends on load, so the page could take the new worker as part of the roster it opened with and never show it as found. The worker now joins after the page shows it is listening. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): hold the gallery answer until the skeleton is seen The spec delayed the answer by a fixed 800 ms and then looked for the skeleton. The assertion polls at growing intervals, so on a slow machine it could step over that window. The answer now waits until the skeleton has been seen, and the spec then checks that the skeleton goes away. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): give the PDF tag the time the other PDF specs have pdf.js and its worker load on first use. The other specs in this file wait up to ten seconds for the result of that load. The first one waited five, and missed the tag on a busy machine. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): build the Face and Fine-tuning bundles in the global setup The navigation spec serves the built Fine-tuning bundle, but nothing built it, so two specs failed on a fresh checkout when the dist folder was missing. The spec also built the Face bundle in a hook that every worker runs, and a build rewrites the folder the other workers serve. The global setup now builds Studio, Talk, Face and Fine-tuning once, before the workers start. The spec builds the two bundles itself only when it runs without the global setup. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): fix the Home answers in the stale pin spec The spec asserts that the page shows no alert. The Home page raises its own alert when the model list fails or a staging operation has failed, and both answers come from the shared test server. A slow reply or a failed install left there counted as an alert about the pin. The spec now fixes both answers, so the alert count tells only about the pin. The assertions are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): read the server address in the add-node spec from the page The spec expected the register address to carry port 8089, so it failed on any other port. It now expects the origin the page was opened from. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): give the navigation routes spec its own catalog No page is built in today, so the catalog is empty and the three specs that expect the studio and talk entries failed on the list and on the pin seed. They passed only while Studio and Talk were built in. The spec now sets a catalog of two ungated entries for each example and restores the old one afterwards. The assertions are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(cli): report a failed write of the MLX hostfile The tunnel callback ignored the error of os.WriteFile and set the hostfile variable even when the file was not written. It now logs the error and leaves the variable alone. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): discard the close error of the held listener errcheck flags the deferred Close of the listener that keeps the port busy. The test has no use for that error, so the defer drops it explicitly. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): move the add-on tool tests to Ginkgo The fine-tuning, quantization and add-on host library tests used t.Errorf, t.Fatal and t.Run, which the forbidigo rule rejects. They now use Describe, It and Gomega, with a suite bootstrap in each package. The cases and the assertions are the same. The tables in the host client and the tool schema tests keep one entry per case. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): move the host operation tests to Ginkgo The hostops tests used the testing package calls that forbidigo rejects. They now run as Ginkgo specs under one suite bootstrap. Every check and every per-operation case is kept. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): move the pin store tests to Ginkgo The pin service, file store, sweep and shared store contract tests used the testing package calls that forbidigo rejects. The store contract is now a function that registers its specs for each store, so the file store, the memory-only store and the database store run the same cases. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): move the remaining add-on tests to Ginkgo The built-in catalog, tool arguments, loopback carrier, host operation contract, packaging, application, config, CLI and endpoint tests used the testing package calls that forbidigo rejects. They now run as Ginkgo specs. The endpoint tests also drop the unchecked Close errors that errcheck reports when the auth tag is on. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): wait for the targeted Reinstall button before the install click The "generic deletion blocks targeted install at confirmation" spec dispatches a popstate to set the target node, then clicks Reinstall with a script click. The click can run before the targeted page has rendered or while the button is still disabled, so no install request is sent and the poll on the install count times out. It failed 1 run in 10 with two workers and on every retry in CI. The spec now waits for the button to be enabled first. The assertions are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * chore(addons): refresh the voice package digest in the first-party index The Voice add-on became one page with tabs and got a new icon, so the digest in gallery/addons/index.yaml no longer matched the build. Render the entry again with addonpkg catalog over a locally built linux/amd64 package; the main page is now voice. The other entries are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 go node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): wait for the target node's Reinstall button after the popstate The "generic deletion blocks targeted install at confirmation" spec dispatches a popstate to set the target node, then waits for a button named Reinstall and clicks it. Until React renders the targeted page, the page still draws the generic Reinstall button. It is enabled and has the same name, so the wait passed on it, the click started a global install, and no node install request was sent. The poll on the install count timed out with the confirm dialog still open. With the CPU throttled to 8x, the spec failed on 20 of 20 runs, and the button it clicked had the title "Reinstall". The spec now waits for the button titled "Reinstall on <node>". With the throttle it passes 30 of 30 runs. The assertions are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): give the Studio hand-off spec a budget for the cold bundle load The "a result made through a hand-off records which result it came from" spec opens the Studio add-on straight from an address, twice. Each is a cold load: the page shows "Loading" until the add-on module has run, and the next assertion had the default five seconds. The load takes about two seconds on an idle run and seven to ten under CPU load, so the spec failed on a loaded machine with the hand-off note not yet drawn. With the CPU throttled to 6x and eight busy loops running, it failed on 21 of 30 runs. The first assertion after each load now waits up to 20 seconds, and the spec then passed 30 of 30 under the same load. The assertions are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): carry the identity page styles in the Face add-on The Face page is drawn with the shared identity views, but its style sheet held only the clip and error rules. The page, card, model list, known-faces table, enrol sheet and distance scale classes had no rules. They looked right only when the host had already loaded the style sheet of its own Faces page, which a visit through the add-on drawer does not do. Opened that way, the cards had no gap, the model list showed bullets, and the install buttons ran into the names. The Face sheet now holds the same identity rules as the core sheet, so the page looks the same whichever way it is opened. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): scope the narrow-screen identity rules of the Voice add-on The rules for narrow screens (single-column pairs, smaller tool padding, compact rank rows) were copied into the Voice style sheet without their media query, so they applied at every width. They now sit in a max-width 720px query, and the reduced-motion rule in its own query, as in the core sheet. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): drop the repeated title from the Voice and Face pages The add-on shell already titles the page, so the recognition pages showed the name twice: "Voice" above "Voices", "Face" above "Faces". Keep the supporting line and the model picker, as the Studio page does, and remove the unused title strings. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): replace the Pinned sidebar section with an Apps entry The sidebar no longer lists pinned add-ons. It shows one Apps entry that opens the add-ons drawer, with a Show all link below it that opens the same drawer. Pins are used from the drawer, where a pinned tile keeps its pin and move buttons. The server pins API is unchanged. Remove the PinnedNav component and its locale title, rename the items.addons label to Apps, and update the e2e specs and the add-ons documentation to match. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): assert the single shell title on the Voice and Face pages The add-on shell now shows the one title, "Voice" or "Face", and the page no longer repeats it. The tests still looked for the h1 "Voices" and "Faces". Assert that the add-on page has exactly one h1 and that it is the shell title. The core pages keep their own "Voices" and "Faces" titles. Assisted-by: Claude Code:claude-sonnet-5-5 playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): drop the repeated title from the Voice Library and Text to speech pages The add-on shell already titles the page "Voice", so these two tabs showed a second heading below it ("Speech voices", "Speak text"). Keep the supporting line, as the Studio and recognition pages do, and remove the unused title strings. The e2e spec now checks that each tab has exactly one h1, the shell title. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): list pinned apps under Apps in the sidebar A pinned app now shows as an indented child of the Apps entry, with its icon and title, in pin order. Show all stays the last child. The pins come from GET /api/addons/pins through the navigation context, and the apps from the add-on registry, which the drawer feeds on every list read, so the links follow the drawer. A pin without an installed app is not drawn, nothing extra shows when no app is pinned, and the children are hidden when the sidebar is collapsed to the rail. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(addons): say that pinned apps are listed under Apps in the sidebar The sidebar lists the pinned apps between Apps and Show all, in pin order. The pins section and the navigation section said that pins do not appear in the sidebar. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): remove the Show all link under Apps in the sidebar The link repeated the Apps entry, which opens the same drawer, and looked redundant when no app was pinned. Apps is now the only add-on entry; pinned apps still list under it in pin order. The link, its locale key and the style comment are gone, and the e2e spec and the add-ons docs now say that Apps is the only entry. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): show a first-run card on Apps when nothing is installed An administrator who opens Apps with no add-on installed now sees a short card with three steps: choose a library, browse and install, start the add-on. Its button opens the Browse tab. The card is hidden once an add-on is installed and for anyone who cannot install. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): hide the pinned apps in the icon rail When the window is narrower than 1024 px the sidebar shrinks to the icon rail by CSS alone, without the collapsed class. The pinned app links were still drawn there, with the indent meant for the full sidebar, so their icons sat to the right of the rail and were cut off. Hide the pinned app links in both rail states. The Apps entry keeps its centred icon. The e2e spec checks that the rail shows no pinned app and that every icon is centred at 1280 px collapsed and at 1000 px. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): stop the backend redirect test racing the installed list The test reads the redirected URL right after the path changes. The Backends page then loads the installed list and drops a selected backend that is not installed, so the backend parameter was sometimes already gone when the test read it. The mock server has no llama-cpp, and about 15 of 40 repeated runs failed on the backend assertion. The redirect itself was correct, including show_all. Serve the selected backend in the installed list, so the page keeps the selection and the assertions stay as strict as before. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): accept a same-host Referer or Origin for human confirmation Browsers send no Sec-Fetch-* headers over plain HTTP on an address that is not localhost, so creating a resource from a LAN address failed with "Creating a resource requires your own browser session". When Sec-Fetch-Site is present it must still be same-origin with an empty destination, as before. When it is absent, Origin or, failing that, Referer must name the request host and port. A destination or mode that is present must still be the expected one. This matches the rule in BrowserProtection. The credential, token cookie, API key and tool-context checks are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): keep hub entries off the Apps route in the sidebar The Build hub still owned /app/addons through its legacy Add-ons tab, so the sidebar lit both Apps and Build on the Apps page. A tab whose gate is closed now owns no route when the sidebar asks, so only the Apps entry is active there. The hubs keep their own routes, and the legacy navigation still gives /app/addons to the Build hub. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): label the pin control on app tiles and in the detail view The pin was a small icon-only button and hard to find. Each app tile now shows a button reading Pin, or Pinned with a filled pin, and the detail view has the same control in its header. Both toggle the same stored pin. The Apps grid lists pinned apps first, in pin order, then the others. The sidebar list and the server pins API are unchanged. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): check the Voice From a recording tab has one title The Library and Text to speech tabs already assert that the add-on shell title is the only h1. Do the same for the From a recording tab, so a heading added there later fails the navigation spec. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): stop the add-on shell drawing a title over add-on pages The app decides what its page shows. The shell drew the add-on name as a header above every add-on page, so an add-on could not choose its own title. The shell now draws no header on a live, loading or frozen add-on page; each add-on titles its own pages. The unavailable and missing-page states keep the name, because no add-on page is drawn there. The runtime and shell specs now check that the shell adds no page title. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): let each add-on draw its own page title The shell no longer titles add-on pages, so each add-on draws one h1 per page. Voice titles its tabs "Voices" (Recognition and From a recording), "Speech voices" (Library) and "Speak text" (Text to speech); "Add a speech voice" already had its own. Face draws "Faces", Studio "Studio" and Talk "Talk". Tables promotes the database list and database name from h2 to h1. Fine-tuning and Quantization already drew a title. The add-on unit tests and the navigation and Studio specs now assert exactly one h1 per page, with the page's own title. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): add a kind to library entries and read unknown kinds tolerantly A library entry now has an optional kind: addon (the default when it is absent), backend or model. Every index written before kinds existed stays valid and its entries are add-ons. A reader keeps the entries of the kinds it knows and skips the others without reading their fields, so a newer kind never makes an older host refuse the whole index. The refresh result and the library state report how many entries were skipped and of which kinds, and the refresh logs a warning. A kind that is not a plain kind name still makes the index malformed. Search lists add-ons unless the caller asks for a kind, and Resolve returns add-ons only, so an add-on install cannot be pointed at a backend image. A library policy may name the signer with an anchored identityRegex instead of an exact identity, so builds from master and from tags both verify. An add-on install refuses a policy that carries one. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(backends): install a backend from a library entry A library can now list backends. An entry of kind backend names the image of the backend gallery by digest, and POST /backends/library/install installs it on the local node. The gallery stays the source of truth and its install path is the one used: the library resolves the entry, takes the signature policy from the library, pins the image by digest and calls the same install as the gallery. Installing a name that came from a gallery, another library or by hand is refused and changes nothing. The signature policy of a backend library names the signer with an anchored identityRegex, so images signed by the merge workflow on master and on tags both verify. A pattern that is not anchored is refused. An install is bounded by a size limit per kind: add-ons keep 512 MiB and a backend image defaults to 8 GiB, set with --addons-backend-max-bytes. The limit reads the manifest after the signature check and before any layer is fetched. The metadata of an installed backend records the library it came from. scripts/backends/cmd/backendcatalog renders entries from backend/index.yaml and pins them against the registry. The pilot is the CPU build of local-store, in gallery/backends/index.yaml. The library is registered on the first start only with --addons-backend-library, which is off by default. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): show the kind of library entries and install backends from them The Browse tab lists the entries of every kind this host reads and marks each one as an add-on or a backend. A backend installs through the backend route and not through the add-on lifecycle: the confirmation says that the backend gallery is not changed and that a backend runs native code, and a refusal of the host, such as a name installed from another origin, is shown with its own reason. The details give the kind, the alias and the signer pattern. The library editor can name the signer with a pattern. Without this a save would drop the pattern of a backend library. The Libraries tab reports the entries a refresh skipped because their kind is not read. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * docs(addons): describe backends in the library Document the entry kinds, how a reader skips a kind it does not know, the backend entry format, the signer identity pattern, the install route, the size limit of each kind, the pilot library and the command that renders it. The backend page links to it. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(mcp): install a backend from a library entry The backend library route installs a backend from an add-on library entry, and the admin REST surface and the MCP catalog must stay in step. Add the install_library_backend tool with an in-process and an HTTP client, the route map entry, the safety-rule listing and a skill recipe. Listing library entries stays on the add-on routes, which the assistant does not expose, so no list tool is added. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): carry the add-on library keys in every locale The add-on library and backend library strings existed only in English, so the other locales had no value for them. Add the missing keys of the addons namespace to de, es, id, it, ko, pt-BR and zh-CN. The values are the English text until a translation is contributed. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Revert "feat(mcp): install a backend from a library entry" This reverts commit7493cff727. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Revert "docs(addons): describe backends in the library" This reverts commit57816ed7e6. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Revert "feat(ui): show the kind of library entries and install backends from them" This reverts commit49b2b151b7. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Revert "feat(backends): install a backend from a library entry" This reverts commit1d8d3e3421. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Revert "feat(addons): add a kind to library entries and read unknown kinds tolerantly" This reverts commit484813f856. Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(ui): drop the backend library keys from the locales Revert the part of39f77ae29that added strings for backend libraries: the kind labels, the backend install texts, the signer identity pattern and the skipped entries notice. The add-on library keys stay. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): add an add-ons control to the chat header Add-on tools were on or off only through the /addons command, with a toast and no state a person could see. The chat header now has an Add-ons button in the style of the MCP dropdown. It lists each running add-on that ships tools with a switch, and a badge shows how many are on. The chat keeps the ids that are on (addonToolIds) next to the older boolean, and both are saved with the chat. A chat that has only the boolean means every add-on. The request sends addon_tools alone when every add-on is on, and adds addon_tool_ids when only some are. /addons turns all on or all off and uses the same state. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): let a chat request name the add-ons whose tools it uses addon_tools=true still offers the tools of every add-on the caller may use. A new addon_tool_ids metadata key, a comma-separated list of add-on ids, narrows that to some add-ons. The list counts only with the opt-in, and a list that names nothing offers no tool rather than all of them. The executor drops the tools of add-ons that are not listed and refuses a call to one as an unknown tool, so a model cannot reach a tool the person turned off. Both keys are consumed so they do not reach the backend. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): cover the per-add-on tool list and the chat header control Go tests cover the list form (the list filters the tools offered and the calls allowed) and the unchanged boolean form. Unit tests cover the chat add-on rules. Playwright specs cover the header control: listing only add-ons that ship tools, toggling each one, the count, /addons in step with the switches, and the request metadata. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): check each add-on page has one title when not enabled The shell names an add-on only where the add-on page cannot mount (not enabled, failed, no UI, bundle not loaded, no such page). Once the page mounts it draws its own title, including when it says it is not enabled on this server, and the shell draws none. The shell code already works this way; this change states the rule beside the unavailable state and pins it in the specs. The shell spec now expects exactly one title in the unavailable states. The navigation spec checks the Face page without a face model (the add-on title only) and the Face page of a stopped add-on (the shell title only). Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(addons): upgrade add-ons on their own when the setting is on A new auto_upgrade_addons setting (flag LOCALAI_AUTO_UPGRADE_ADDONS, default off) lets the owner upgrade an installed add-on when its library lists a newer version. It follows the backend setting: a startup flag, a runtime setting, and the same schedule as the backend update check (a first pass shortly after start, then every six hours). Each pass fetches the library of every installed add-on, and for each available update builds the same pinned upgrade command an administrator sends and runs it through the same lifecycle path. The channel rules, the signer identity, the digest checks, the SDK compatibility check and the backup before the upgrade all apply unchanged. The only difference is the gate: the owner acts on its own setting, so no administrator is asked. A refused or failed upgrade changes nothing and is logged; a candidate that failed is not retried until the library lists another digest. With the setting off, nothing runs and upgrades stay manual. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(ui): add the Upgrade add-ons automatically toggle to Settings The toggle sits beside Upgrade backends automatically and is off by default. It saves the auto_upgrade_addons runtime setting. The English locale gets the label key, and the Settings test fixture lists the new field. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): cover the automatic upgrade setting and document it The setting defaults to off in the config, the runtime settings and the flag, and is read back from the saved settings file. With it on, a real owner upgrades an installed add-on through the manager; with it off, nothing changes, and a change of the setting at run time takes effect on the next pass. The manager specs show the upgrade goes through the same pinned path as the manual one, and that a corrupted package, a failed signature policy, an incompatible release, a missing update checker and a caller who is not an administrator each leave the installed version in place. The new specs are added to the lifecycle case inventory. The add-ons page describes the setting, its schedule and its default. Assisted-by: Claude Code:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactor(addons): name the library entry identifier id The library entry of an add-on used addonId while the shared envelope and the model entries use id. Release now has the field ID, written as id, and every writer emits it: the index encoder, the package workflow templates, the catalog generator and gallery/addons/index.yaml. The digests in the index did not change. DecodeIndex still reads addonId as a legacy alias for one release, so an existing library and a library served by an older tool keep working. An entry that holds both names is refused. The Apps UI reads id and falls back to addonId. Assisted-by: Claude Code:claude-sonnet-5-5 go node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(addons): use the id field in the auto-upgrade test release The auto-upgrade test arrived after the identifier rename and still named the release field AddonID, so the application tests did not build. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(addons): annotate the auto-upgrade actor label for gosec The actor label looks like a credential to gosec G101. It is a fixed name for upgrades the owner starts itself. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * fix(schema): persist the per-chat add-on tool fields The chat header persists addonTools and addonToolIds, but Conversation had no fields for them, so the server dropped them on save. Add both and update the generated swagger files by hand because swag is not installed here. Assisted-by: Claude Code:claude-sonnet-5-5 go Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): count the add-ons auto-upgrade setting in the settings groups The setting sits in the Backends and galleries group, once. The page now lists 54 settings plus the three branding images, so the spec expects 57. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): pause the fake clock ahead of now in the clear-all spec The fake clock runs from install, so pausing at the current time failed with 'Cannot fast-forward to the past' on a slow runner. The save itself is synchronous and was not lost. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * test(ui): hold the Studio capabilities reply longer in the skeleton spec 800 ms was shorter than a slow runner needs to render the page, so the skeleton was gone before the check. The product shows it. Assisted-by: Claude Code:claude-sonnet-5-5 node playwright Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(i18n): add the French add-ons locale Master added the fr locale after this branch forked. The add-ons unit test requires every locale to carry the add-ons namespace, so fr needs its own addons.json. Assisted-by: Claude Code:claude-sonnet-5-5 node Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io> Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
1447 lines
52 KiB
Go
1447 lines
52 KiB
Go
// SPDX-License-Identifier: MIT
|
|
|
|
// Package resources is the owner's UI-only resource management: it lists the
|
|
// resources a person may use, creates, renames and deletes them on the
|
|
// person's explicit action, manages whole-resource grants, and keeps which
|
|
// resource each conversation selected.
|
|
//
|
|
// Nothing here is a tool. A model, a proposal, a typed dispatch and an agent
|
|
// have no way to reach any of it: the gateway names these calls only for the
|
|
// browser routes, the declared-tool rule reads capabilities.tools while the
|
|
// delete tool is listed in capabilities.adminTools, and sharing and deletion
|
|
// have no tool definition. Every call goes through the same host policy as
|
|
// every other operation: authentication is revalidated, the role is checked
|
|
// at the owner, and the work is admitted per resource, so a revocation or a
|
|
// deletion waits for it and cancels it.
|
|
package resources
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
|
|
"github.com/mudler/LocalAI/core/services/addons/gateway"
|
|
"github.com/mudler/LocalAI/core/services/addons/policy"
|
|
addonruntime "github.com/mudler/LocalAI/core/services/addons/runtime"
|
|
"github.com/mudler/LocalAI/core/services/addons/toolargs"
|
|
"github.com/mudler/LocalAI/pkg/addons/contract"
|
|
)
|
|
|
|
// Bounds of the management calls.
|
|
const (
|
|
// MaxListed bounds one listing.
|
|
MaxListed = 100
|
|
// MaxNameRunes bounds a display name the host shows.
|
|
MaxNameRunes = 80
|
|
// MaxUserResults bounds the user picker.
|
|
MaxUserResults = 20
|
|
// MinQueryRunes and MaxQueryRunes bound the user picker query.
|
|
MinQueryRunes = 2
|
|
MaxQueryRunes = 64
|
|
// MaxGrants bounds the grants of one resource that are listed.
|
|
MaxGrants = 256
|
|
maxNameInput = 256
|
|
)
|
|
|
|
// Capabilities reports the capabilities of the installed generation of an
|
|
// add-on. The manager implements it.
|
|
type Capabilities interface {
|
|
DeclaredCapabilities(ctx context.Context, addonID string, generation uint64) (contract.Capabilities, error)
|
|
}
|
|
|
|
// UserDirectory answers the identity questions of sharing from the account
|
|
// store. It never returns more than an immutable ID and a display name.
|
|
type UserDirectory interface {
|
|
// Lookup returns the display names of the IDs it knows. Unknown IDs are
|
|
// absent from the map.
|
|
Lookup(ctx context.Context, ids []string) (map[string]string, error)
|
|
// Eligible says whether the ID is an active named account that may use
|
|
// add-ons, so a grant to it is meaningful.
|
|
Eligible(ctx context.Context, id string) (bool, error)
|
|
// Search finds eligible accounts by a prefix of the display name or of
|
|
// the address, at most limit of them.
|
|
Search(ctx context.Context, query string, limit int) ([]gateway.UserRef, error)
|
|
}
|
|
|
|
// Options are the owner services the management needs. Missing ones make the
|
|
// calls that need them unavailable.
|
|
type Options struct {
|
|
Policy *policy.Policy
|
|
Broker *policy.Broker
|
|
Runtime gateway.CurrentRuntime
|
|
Capabilities Capabilities
|
|
// Tools reports the declared model tools. The create step of
|
|
// the policy refuses a tool it does not list.
|
|
Tools gateway.ToolSource
|
|
Users UserDirectory
|
|
// Selections holds the conversation selections. Without it the selection
|
|
// calls are unavailable.
|
|
Selections *SelectionStore
|
|
// BackingConfirmed asks the add-on whether the backing of a resource
|
|
// exists. A deletion that finds it gone is finished without the delete
|
|
// tool, so a (false, nil) must be the explicit answer of the add-on's
|
|
// declared existence tool. An add-on that cannot give one answers
|
|
// policy.ErrBackingUnanswered (policy.BackingConfirmed does), and the
|
|
// deletion stays confirmed and is retried.
|
|
BackingConfirmed func(context.Context, policy.ResourceKey) (bool, error)
|
|
Limits contract.Limits
|
|
Now func() time.Time
|
|
// NameTimeout bounds the display-name read of one resource.
|
|
NameTimeout time.Duration
|
|
}
|
|
|
|
// Service implements gateway.ResourceManager.
|
|
type Service struct {
|
|
o Options
|
|
// binds remembers the request each UI apply operation was dispatched
|
|
// for, newest last and bounded. The add-on's own ledger stays the
|
|
// authority across restarts: it refuses the reuse with a 409.
|
|
bindMu sync.Mutex
|
|
binds map[string]binding
|
|
order []string
|
|
}
|
|
|
|
// binding is the request an operation ID was first dispatched for.
|
|
type binding struct{ tool, hash string }
|
|
|
|
// maxBindings bounds the remembered requests.
|
|
const maxBindings = 4096
|
|
|
|
// requestHash is the fingerprint of one UI change: the resource, the tool
|
|
// and the canonical arguments (decoded, keys sorted, numbers kept as
|
|
// written). The expected revision is not part of it: a repeat of a change
|
|
// reads a newer revision and is still the same change.
|
|
func requestHash(resourceID, tool string, args json.RawMessage) string {
|
|
var v any
|
|
d := json.NewDecoder(strings.NewReader(string(args)))
|
|
d.UseNumber()
|
|
if d.Decode(&v) != nil {
|
|
v = string(args)
|
|
}
|
|
canon, _ := json.Marshal(v)
|
|
sum := sha256.Sum256([]byte(resourceID + "\x00" + tool + "\x00" + string(canon)))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// bound reports the request recorded for a wire operation.
|
|
func (s *Service) bound(wire string) (binding, bool) {
|
|
s.bindMu.Lock()
|
|
defer s.bindMu.Unlock()
|
|
b, ok := s.binds[wire]
|
|
return b, ok
|
|
}
|
|
|
|
// unbind forgets a binding the add-on did not keep.
|
|
func (s *Service) unbind(wire string, b binding) {
|
|
s.bindMu.Lock()
|
|
defer s.bindMu.Unlock()
|
|
if have, ok := s.binds[wire]; ok && have == b {
|
|
delete(s.binds, wire)
|
|
for i, id := range s.order {
|
|
if id == wire {
|
|
s.order = append(s.order[:i], s.order[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// bind records the request of a wire operation. It reports false when the
|
|
// operation is already bound to another request.
|
|
func (s *Service) bind(wire string, b binding) bool {
|
|
s.bindMu.Lock()
|
|
defer s.bindMu.Unlock()
|
|
if have, ok := s.binds[wire]; ok {
|
|
return have.hash == b.hash
|
|
}
|
|
if s.binds == nil {
|
|
s.binds = map[string]binding{}
|
|
}
|
|
if len(s.order) >= maxBindings {
|
|
delete(s.binds, s.order[0])
|
|
s.order = s.order[1:]
|
|
}
|
|
s.binds[wire] = b
|
|
s.order = append(s.order, wire)
|
|
return true
|
|
}
|
|
|
|
var _ gateway.ResourceManager = (*Service)(nil)
|
|
|
|
// New builds the service.
|
|
func New(o Options) *Service {
|
|
if o.Limits == (contract.Limits{}) {
|
|
o.Limits = contract.DefaultLimits()
|
|
}
|
|
if o.Now == nil {
|
|
o.Now = time.Now
|
|
}
|
|
if o.NameTimeout <= 0 {
|
|
o.NameTimeout = 3 * time.Second
|
|
}
|
|
return &Service{o: o}
|
|
}
|
|
|
|
var (
|
|
sha256Hex = regexp.MustCompile(`^[0-9a-f]{64}$`)
|
|
identifier = regexp.MustCompile(`^[a-z][a-z0-9-]{0,63}$`)
|
|
// resumeActor is the identity of the owner's own settlement of a
|
|
// deletion after a crash. It carries no authority over any resource: the
|
|
// request is the one typed delete of a resource that is already
|
|
// confirmed as deleting.
|
|
resumeActor = contract.Actor{UserID: "owner-reconcile", CredentialID: "owner:reconcile", Mode: "local-owner"}
|
|
)
|
|
|
|
func (s *Service) ready() error {
|
|
if s.o.Policy == nil || s.o.Runtime == nil || s.o.Capabilities == nil {
|
|
return gateway.ErrUnavailable
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// unavailable wraps a failure of the add-on as the shared unavailable answer.
|
|
func unavailable(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, gateway.ErrUnavailable) {
|
|
return err
|
|
}
|
|
if errors.Is(err, contract.ErrRefused) {
|
|
return gateway.Refused(err)
|
|
}
|
|
return fmt.Errorf("%w: %w", gateway.ErrUnavailable, err)
|
|
}
|
|
|
|
// classify keeps a policy denial a denial and everything else unavailable.
|
|
func classify(err error) error {
|
|
switch {
|
|
case err == nil:
|
|
return nil
|
|
case errors.Is(err, policy.ErrQuota):
|
|
return gateway.ErrQuotaReached
|
|
case errors.Is(err, gateway.ErrUnavailable):
|
|
return err
|
|
case errors.Is(err, gateway.ErrForbidden):
|
|
// A typed denial keeps its type. Every other denial is the one plain
|
|
// denial, whichever layer made it, so that an unknown resource and a
|
|
// resource that is not the actor's cannot be told apart.
|
|
var blocked *gateway.OutcomeBlocked
|
|
for _, typed := range []error{gateway.ErrSharingDisabled, gateway.ErrStale, gateway.ErrOperationConflict, gateway.ErrInvalidRequest, gateway.ErrQuotaReached, gateway.ErrNotSelected} {
|
|
if errors.Is(err, typed) {
|
|
return err
|
|
}
|
|
}
|
|
if errors.As(err, &blocked) {
|
|
return err
|
|
}
|
|
return gateway.ErrForbidden
|
|
}
|
|
return unavailable(err)
|
|
}
|
|
|
|
// session resolves the add-on's current instance and capabilities. A caller
|
|
// that names a generation gets ErrStale when it is not the current one.
|
|
func (s *Service) session(ctx context.Context, addonID string, generation uint64) (addonruntime.Instance, contract.Capabilities, error) {
|
|
if err := s.ready(); err != nil {
|
|
return addonruntime.Instance{}, contract.Capabilities{}, err
|
|
}
|
|
inst, err := s.o.Runtime.Runtime(ctx, addonID)
|
|
if err != nil || inst.Client == nil || inst.Generation == 0 {
|
|
if ctx.Err() != nil {
|
|
return inst, contract.Capabilities{}, ctx.Err()
|
|
}
|
|
return inst, contract.Capabilities{}, unavailable(err)
|
|
}
|
|
if generation != 0 && generation != inst.Generation {
|
|
return inst, contract.Capabilities{}, gateway.ErrStale
|
|
}
|
|
caps, err := s.o.Capabilities.DeclaredCapabilities(ctx, addonID, inst.Generation)
|
|
if err != nil {
|
|
if gateway.StatusFor(err) == 403 {
|
|
return inst, caps, gateway.ErrForbidden
|
|
}
|
|
return inst, caps, unavailable(err)
|
|
}
|
|
return inst, caps, nil
|
|
}
|
|
|
|
func randomID(prefix string) string {
|
|
var raw [12]byte
|
|
_, _ = rand.Read(raw[:])
|
|
return prefix + hex.EncodeToString(raw[:])
|
|
}
|
|
|
|
// uiOperation derives the operation identifier the add-on sees from the
|
|
// person's own operation ID, scoped to the person and the kind. Two people
|
|
// that chose the same ID never share a receipt, and repeating one request is
|
|
// one operation.
|
|
func uiOperation(actor contract.Actor, kind, operationID string) string {
|
|
sum := sha256.Sum256([]byte(kind + "\x00" + actor.Mode + "\x00" + actor.UserID + "\x00" + operationID))
|
|
return "u" + hex.EncodeToString(sum[:])[:31]
|
|
}
|
|
|
|
// deleteOperation is the operation identifier of the one deletion of a
|
|
// resource, so a retry and a resume after a crash are the same operation.
|
|
func deleteOperation(resourceID string) string { return "del-" + resourceID }
|
|
|
|
// cleanName bounds and cleans a display name the add-on gave.
|
|
func cleanName(n string) string {
|
|
if !utf8.ValidString(n) {
|
|
return ""
|
|
}
|
|
var b strings.Builder
|
|
for _, r := range n {
|
|
if unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || r == '<' || r == '>' {
|
|
continue
|
|
}
|
|
b.WriteRune(r)
|
|
}
|
|
out := strings.TrimSpace(b.String())
|
|
if utf8.RuneCountInString(out) > MaxNameRunes {
|
|
out = string([]rune(out)[:MaxNameRunes])
|
|
}
|
|
return out
|
|
}
|
|
|
|
type inspected struct {
|
|
Name string
|
|
Revision contract.Revision
|
|
}
|
|
|
|
// inspect reads the display name and revision of a resource through the
|
|
// add-on's inspect tool, admitted for the actor like any read.
|
|
func (s *Service) inspect(ctx context.Context, actor contract.Actor, key policy.ResourceKey, inst addonruntime.Instance, tool string) (inspected, error) {
|
|
var out inspected
|
|
args, _ := json.Marshal(map[string]string{gateway.ToolKey: tool})
|
|
err := s.o.Policy.Admit(ctx, actor, key, policy.ActionRead, func(rctx context.Context) error {
|
|
cctx, cancel := context.WithTimeout(rctx, s.o.NameTimeout)
|
|
defer cancel()
|
|
env := contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: randomID("n"), Generation: inst.Generation, Action: "read", Arguments: args},
|
|
Authority: contract.Authority{Actor: actor, Scope: "read", ExpiresAt: s.o.Now().Add(s.o.Limits.ReadTimeout)},
|
|
}
|
|
res, e := inst.Client.Read(cctx, env)
|
|
if e != nil {
|
|
if rctx.Err() != nil {
|
|
return rctx.Err()
|
|
}
|
|
return unavailable(e)
|
|
}
|
|
var v struct {
|
|
Name string `json:"name"`
|
|
Revision contract.Revision `json:"revision"`
|
|
}
|
|
if json.Unmarshal(res.Payload, &v) != nil {
|
|
return unavailable(errors.New("inspect result is not an object"))
|
|
}
|
|
out = inspected{Name: cleanName(v.Name), Revision: v.Revision}
|
|
return nil
|
|
})
|
|
return out, err
|
|
}
|
|
|
|
// listWidth is how many name reads one listing runs at once: half of the
|
|
// add-on's concurrency budget, and at least one. The runtime refuses a call over
|
|
// the budget, so a listing that took all of it would make every other read of
|
|
// the person's page fail while it ran.
|
|
func (s *Service) listWidth() int {
|
|
n := int(s.o.Limits.Concurrency / 2)
|
|
if n < 1 {
|
|
return 1
|
|
}
|
|
return n
|
|
}
|
|
|
|
func operations(c contract.Capabilities) []string {
|
|
ops := []string{}
|
|
if r := c.Resources; r != nil {
|
|
if r.Create != "" {
|
|
ops = append(ops, "create")
|
|
}
|
|
if r.Rename != "" {
|
|
ops = append(ops, "rename")
|
|
}
|
|
if r.Delete != "" {
|
|
ops = append(ops, "delete")
|
|
}
|
|
}
|
|
return ops
|
|
}
|
|
|
|
// List lists the resources of an addon the actor may use, with the actor's
|
|
// own role. It shows nothing of anyone else's resources.
|
|
func (s *Service) List(ctx context.Context, actor contract.Actor, addonID string) (gateway.ResourceList, error) {
|
|
if !identifier.MatchString(addonID) {
|
|
return gateway.ResourceList{}, gateway.ErrInvalidRequest
|
|
}
|
|
inst, caps, err := s.session(ctx, addonID, 0)
|
|
if err != nil {
|
|
return gateway.ResourceList{}, err
|
|
}
|
|
views, err := s.o.Policy.Listing(ctx, actor, addonID)
|
|
if err != nil {
|
|
return gateway.ResourceList{}, classify(err)
|
|
}
|
|
out := gateway.ResourceList{AddonID: addonID, Generation: inst.Generation, Sharing: s.o.Policy.Mode() == policy.ModeNamedUser, Operations: operations(caps), Resources: []gateway.ResourceInfo{}}
|
|
if caps.Resources != nil {
|
|
out.Noun = caps.Resources.Noun
|
|
}
|
|
if len(views) > MaxListed {
|
|
views, out.Truncated = views[:MaxListed], true
|
|
}
|
|
infos := make([]gateway.ResourceInfo, len(views))
|
|
// The names are a convenience: the reads share one budget, and a resource
|
|
// whose name did not arrive in it is listed without one.
|
|
names, cancel := context.WithTimeout(ctx, 3*s.o.NameTimeout)
|
|
defer cancel()
|
|
var wg sync.WaitGroup
|
|
sem := make(chan struct{}, s.listWidth())
|
|
for i, v := range views {
|
|
infos[i] = gateway.ResourceInfo{ID: v.Key.ResourceID, AddonID: addonID, Role: string(v.Role)}
|
|
if v.Role == policy.RoleOwner {
|
|
n := v.Shared
|
|
infos[i].SharedWith = &n
|
|
}
|
|
if caps.Resources == nil || caps.Resources.Inspect == "" {
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
sem <- struct{}{}
|
|
go func() {
|
|
defer wg.Done()
|
|
defer func() { <-sem }()
|
|
r, e := s.inspect(names, actor, v.Key, inst, caps.Resources.Inspect)
|
|
if e != nil {
|
|
// The name is a convenience: the resource is still listed.
|
|
return
|
|
}
|
|
infos[i].Name = r.Name
|
|
if v.Role == policy.RoleOwner {
|
|
rev := r.Revision
|
|
infos[i].Revision = &rev
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if s.o.Users != nil {
|
|
var ids []string
|
|
seen := map[string]bool{}
|
|
for _, v := range views {
|
|
if v.Role != policy.RoleOwner && !seen[v.OwnerID] {
|
|
seen[v.OwnerID] = true
|
|
ids = append(ids, v.OwnerID)
|
|
}
|
|
}
|
|
if len(ids) > 0 {
|
|
if names, e := s.o.Users.Lookup(ctx, ids); e == nil {
|
|
for i, v := range views {
|
|
if v.Role != policy.RoleOwner {
|
|
infos[i].Owner = cleanName(names[v.OwnerID])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out.Resources = infos
|
|
return out, nil
|
|
}
|
|
|
|
// Create creates a resource through the add-on's create tool, as the person's
|
|
// own UI action. It reserves the resource for the person, runs the typed
|
|
// create through the same preview and apply steps every mutation uses, and
|
|
// activates it. Repeating the operation ID returns the same resource.
|
|
func (s *Service) Create(ctx context.Context, actor contract.Actor, r gateway.CreateResourceRequest) (gateway.ResourceInfo, error) {
|
|
if !identifier.MatchString(r.AddonID) || !identifier.MatchString(r.OperationID) || r.Generation == 0 {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
inst, caps, err := s.session(ctx, r.AddonID, r.Generation)
|
|
if err != nil {
|
|
return gateway.ResourceInfo{}, err
|
|
}
|
|
if caps.Resources == nil || caps.Resources.Create == "" {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
def, ok := definition(caps, caps.Resources.Create, contract.ToolKindCreate)
|
|
if !ok {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
resource, body, err := toolargs.BindDefinition(def, r.Arguments)
|
|
if err != nil || resource != "" {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
if s.o.Tools == nil {
|
|
return gateway.ResourceInfo{}, gateway.ErrUnavailable
|
|
}
|
|
sum := sha256.Sum256(append([]byte(r.AddonID+"\x00"), body...))
|
|
ref := hex.EncodeToString(sum[:])
|
|
idSum := sha256.Sum256([]byte("create\x00" + actor.Mode + "\x00" + actor.UserID + "\x00" + r.OperationID))
|
|
id := "r" + hex.EncodeToString(idSum[:])[:31]
|
|
step := policy.RuntimeCreate(s.o.Runtime, s.o.Tools, s.o.Limits, actor, body)
|
|
key, err := s.o.Policy.CreateWithID(ctx, actor, r.AddonID, id, ref, step)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, policy.ErrNotCreated):
|
|
// The add-on refused the request and made nothing.
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
case errors.Is(err, policy.ErrQuota):
|
|
return gateway.ResourceInfo{}, gateway.ErrQuotaReached
|
|
}
|
|
return gateway.ResourceInfo{}, classify(err)
|
|
}
|
|
info := gateway.ResourceInfo{ID: key.ResourceID, AddonID: key.AddonID, Role: string(policy.RoleOwner)}
|
|
n := 0
|
|
info.SharedWith = &n
|
|
if caps.Resources.Inspect != "" {
|
|
if v, e := s.inspect(ctx, actor, key, inst, caps.Resources.Inspect); e == nil {
|
|
info.Name = v.Name
|
|
rev := v.Revision
|
|
info.Revision = &rev
|
|
}
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
// definition finds the definition of a declared tool of one kind.
|
|
func definition(c contract.Capabilities, tool, kind string) (contract.ToolDefinition, bool) {
|
|
declared := false
|
|
for _, t := range c.Tools {
|
|
declared = declared || t == tool
|
|
}
|
|
if !declared {
|
|
return contract.ToolDefinition{}, false
|
|
}
|
|
for _, d := range c.ToolDefinitions {
|
|
if d.Name == tool && d.Kind == kind {
|
|
return d, true
|
|
}
|
|
}
|
|
return contract.ToolDefinition{}, false
|
|
}
|
|
|
|
func validOperation(id string) bool { return identifier.MatchString(id) }
|
|
|
|
// owned resolves the resource and checks that the actor holds the owner role,
|
|
// with the same denial for a resource that does not exist.
|
|
func (s *Service) owned(ctx context.Context, actor contract.Actor, resourceID string) (policy.ResourceKey, error) {
|
|
if s.o.Policy == nil {
|
|
return policy.ResourceKey{}, gateway.ErrUnavailable
|
|
}
|
|
// An unknown resource gets a key without an add-on, which the policy
|
|
// denies like a resource that is not the actor's.
|
|
key, _ := s.o.Policy.Key(resourceID)
|
|
key.ResourceID = resourceID
|
|
if err := s.o.Policy.Authorize(ctx, actor, key, policy.ActionShare); err != nil {
|
|
return policy.ResourceKey{}, classify(err)
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
func (s *Service) sharing() error {
|
|
if s.o.Policy == nil {
|
|
return gateway.ErrUnavailable
|
|
}
|
|
if s.o.Policy.Mode() != policy.ModeNamedUser {
|
|
return gateway.ErrSharingDisabled
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Grants lists the grants of a resource for its owner role.
|
|
func (s *Service) Grants(ctx context.Context, actor contract.Actor, resourceID string) (gateway.GrantList, error) {
|
|
if err := s.sharing(); err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
key, err := s.owned(ctx, actor, resourceID)
|
|
if err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
return s.grantList(ctx, actor, key)
|
|
}
|
|
|
|
func (s *Service) grantList(ctx context.Context, actor contract.Actor, key policy.ResourceKey) (gateway.GrantList, error) {
|
|
m, err := s.o.Policy.Grants(ctx, actor, key)
|
|
if err != nil {
|
|
return gateway.GrantList{}, classify(err)
|
|
}
|
|
ids := make([]string, 0, len(m))
|
|
for u := range m {
|
|
ids = append(ids, u)
|
|
}
|
|
sort.Strings(ids)
|
|
if len(ids) > MaxGrants {
|
|
ids = ids[:MaxGrants]
|
|
}
|
|
var names map[string]string
|
|
if s.o.Users != nil && len(ids) > 0 {
|
|
names, _ = s.o.Users.Lookup(ctx, ids)
|
|
}
|
|
out := gateway.GrantList{ResourceID: key.ResourceID, Grants: []gateway.Grant{}}
|
|
for _, u := range ids {
|
|
out.Grants = append(out.Grants, gateway.Grant{UserID: u, DisplayName: cleanName(names[u]), Role: string(m[u])})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// SetGrant gives a named user a role on the whole resource.
|
|
func (s *Service) SetGrant(ctx context.Context, actor contract.Actor, resourceID, userID, role string) (gateway.GrantList, error) {
|
|
if err := s.sharing(); err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
key, err := s.owned(ctx, actor, resourceID)
|
|
if err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
if role != string(policy.RoleViewer) && role != string(policy.RoleEditor) {
|
|
return gateway.GrantList{}, gateway.ErrInvalidRequest
|
|
}
|
|
if userID == "" || len(userID) > 256 || s.o.Users == nil {
|
|
return gateway.GrantList{}, gateway.ErrForbidden
|
|
}
|
|
ok, err := s.o.Users.Eligible(ctx, userID)
|
|
if err != nil {
|
|
return gateway.GrantList{}, classify(err)
|
|
}
|
|
if !ok {
|
|
return gateway.GrantList{}, gateway.ErrForbidden
|
|
}
|
|
if err = s.o.Policy.SetGrant(ctx, actor, key, userID, policy.Role(role)); err != nil {
|
|
return gateway.GrantList{}, classify(err)
|
|
}
|
|
return s.grantList(ctx, actor, key)
|
|
}
|
|
|
|
// RevokeGrant removes a user's grant. It returns only once the user's
|
|
// admitted work stopped and their pending proposals were invalidated.
|
|
func (s *Service) RevokeGrant(ctx context.Context, actor contract.Actor, resourceID, userID string) (gateway.GrantList, error) {
|
|
if err := s.sharing(); err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
key, err := s.owned(ctx, actor, resourceID)
|
|
if err != nil {
|
|
return gateway.GrantList{}, err
|
|
}
|
|
if userID == "" || len(userID) > 256 {
|
|
return gateway.GrantList{}, gateway.ErrInvalidRequest
|
|
}
|
|
if err = s.o.Policy.Revoke(ctx, actor, key, userID); err != nil {
|
|
return gateway.GrantList{}, classify(err)
|
|
}
|
|
// Selections of the revoked user are left alone on purpose: the reference
|
|
// is no authority, so it is denied at its next use like an unknown one.
|
|
return s.grantList(ctx, actor, key)
|
|
}
|
|
|
|
// SearchUsers is the user picker behind a resource's share dialog. It is
|
|
// reachable only by the resource's owner role, matches only a bounded prefix
|
|
// and returns nothing but an immutable ID and a display name.
|
|
func (s *Service) SearchUsers(ctx context.Context, actor contract.Actor, resourceID, query string, limit int) (gateway.UserList, error) {
|
|
if err := s.sharing(); err != nil {
|
|
return gateway.UserList{}, err
|
|
}
|
|
key, err := s.owned(ctx, actor, resourceID)
|
|
if err != nil {
|
|
return gateway.UserList{}, err
|
|
}
|
|
query = strings.TrimSpace(query)
|
|
if n := utf8.RuneCountInString(query); n < MinQueryRunes || n > MaxQueryRunes || strings.ContainsFunc(query, unicode.IsControl) {
|
|
return gateway.UserList{}, gateway.ErrInvalidRequest
|
|
}
|
|
if limit <= 0 || limit > MaxUserResults {
|
|
limit = MaxUserResults
|
|
}
|
|
if s.o.Users == nil {
|
|
return gateway.UserList{}, gateway.ErrUnavailable
|
|
}
|
|
owner, _ := s.o.Policy.OwnerOf(key)
|
|
found, err := s.o.Users.Search(ctx, query, limit+2)
|
|
if err != nil {
|
|
return gateway.UserList{}, classify(err)
|
|
}
|
|
out := gateway.UserList{Users: []gateway.UserRef{}}
|
|
for _, u := range found {
|
|
if u.ID == actor.UserID || u.ID == owner || u.ID == "" || len(out.Users) >= limit {
|
|
continue
|
|
}
|
|
out.Users = append(out.Users, gateway.UserRef{ID: u.ID, DisplayName: cleanName(u.DisplayName)})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Rename renames a resource through the add-on's rename tool, as the owner's
|
|
// own UI action. It needs no proposal: the owner's explicit request is the
|
|
// intent. It still runs under the resource's admission, behind unresolved
|
|
// outcomes, against the revision the owner saw, and leaves the add-on's
|
|
// receipt for the operation.
|
|
func (s *Service) Rename(ctx context.Context, actor contract.Actor, resourceID string, r gateway.RenameResourceRequest) (gateway.ResourceInfo, error) {
|
|
if !validOperation(r.OperationID) || r.Generation == 0 || r.Revision.Schema == 0 {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
name := strings.TrimSpace(r.Name)
|
|
if name == "" || name != r.Name || len(name) > maxNameInput {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
key, err := s.owned(ctx, actor, resourceID)
|
|
if err != nil {
|
|
return gateway.ResourceInfo{}, err
|
|
}
|
|
inst, caps, err := s.session(ctx, key.AddonID, r.Generation)
|
|
if err != nil {
|
|
return gateway.ResourceInfo{}, err
|
|
}
|
|
if caps.Resources == nil || caps.Resources.Rename == "" {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
def, ok := definition(caps, caps.Resources.Rename, contract.ToolKindSchema)
|
|
if !ok {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
asked, _ := json.Marshal(map[string]string{contract.ResourceArgument: key.ResourceID, "name": name})
|
|
res, body, err := toolargs.BindDefinition(def, asked)
|
|
if err != nil || res != key.ResourceID {
|
|
return gateway.ResourceInfo{}, gateway.ErrInvalidRequest
|
|
}
|
|
wire := uiOperation(actor, "rename", r.OperationID)
|
|
err = s.o.Policy.Admit(ctx, actor, key, policy.ActionSchema, func(rctx context.Context) error {
|
|
if s.o.Broker != nil {
|
|
if e := s.o.Broker.BlockedError(actor, key); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
return s.typedChange(rctx, actor, key, inst, string(policy.ActionSchema), wire, body, r.Revision, r.OperationID)
|
|
})
|
|
if err != nil {
|
|
return gateway.ResourceInfo{}, classify(err)
|
|
}
|
|
info := gateway.ResourceInfo{ID: key.ResourceID, AddonID: key.AddonID, Role: string(policy.RoleOwner)}
|
|
if caps.Resources.Inspect != "" {
|
|
if v, e := s.inspect(ctx, actor, key, inst, caps.Resources.Inspect); e == nil {
|
|
info.Name = v.Name
|
|
rev := v.Revision
|
|
info.Revision = &rev
|
|
}
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
// typedChange previews and applies one typed change against the revision the
|
|
// person saw. A moved revision is ErrStale and nothing is sent to apply. An
|
|
// apply whose outcome is not known is OutcomeUnknown with the person's
|
|
// operation ID, never a retry under a new one.
|
|
func (s *Service) typedChange(ctx context.Context, actor contract.Actor, key policy.ResourceKey, inst addonruntime.Instance, action, wire string, body json.RawMessage, seen contract.Revision, original string) error {
|
|
cur, err := s.o.Runtime.Runtime(ctx, key.AddonID)
|
|
if err != nil || cur.Client == nil {
|
|
return unavailable(err)
|
|
}
|
|
if cur.Generation != inst.Generation {
|
|
return gateway.ErrStale
|
|
}
|
|
env := func(scope string, d time.Duration) contract.Envelope {
|
|
return contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: wire, Generation: cur.Generation, Action: action, Arguments: body},
|
|
Authority: contract.Authority{Actor: actor, Scope: scope, ExpiresAt: s.o.Now().Add(d)},
|
|
}
|
|
}
|
|
preview, err := cur.Client.Preview(ctx, env("preview", s.o.Limits.PreviewTimeout))
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
if errors.Is(err, contract.ErrRefused) {
|
|
// The add-on's own refusal: deterministic, nothing applied.
|
|
return gateway.Refused(err)
|
|
}
|
|
return s.previewFailure(ctx, actor, key, cur, err)
|
|
}
|
|
if seen.Schema != 0 && preview.Revision != seen {
|
|
return gateway.ErrStale
|
|
}
|
|
receipt, err := cur.Client.Apply(ctx, env("apply", s.o.Limits.ApplyTimeout), preview.Revision)
|
|
if err != nil {
|
|
if errors.Is(err, addonruntime.ErrConflict) {
|
|
return gateway.ErrOperationConflict
|
|
}
|
|
if errors.Is(err, contract.ErrRefused) {
|
|
return gateway.Refused(err)
|
|
}
|
|
return &gateway.OutcomeUnknown{OperationID: original}
|
|
}
|
|
switch receipt.State {
|
|
case contract.ReceiptCommitted:
|
|
return nil
|
|
case contract.ReceiptRejected:
|
|
return gateway.ErrStale
|
|
}
|
|
return &gateway.OutcomeUnknown{OperationID: original}
|
|
}
|
|
|
|
// authorized resolves the resource and checks that the actor holds the role
|
|
// the action needs, with the same denial for a resource that does not exist.
|
|
func (s *Service) authorized(ctx context.Context, actor contract.Actor, resourceID string, action policy.Action) (policy.ResourceKey, error) {
|
|
if s.o.Policy == nil {
|
|
return policy.ResourceKey{}, gateway.ErrUnavailable
|
|
}
|
|
key, _ := s.o.Policy.Key(resourceID)
|
|
key.ResourceID = resourceID
|
|
if err := s.o.Policy.Authorize(ctx, actor, key, action); err != nil {
|
|
return policy.ResourceKey{}, classify(err)
|
|
}
|
|
return key, nil
|
|
}
|
|
|
|
// Apply changes the data or the schema of a resource through a write or
|
|
// schema tool the add-on declares, as the person's own UI action. It needs no
|
|
// proposal: the request in the person's own tab is the intent. It runs under
|
|
// the same rules as every change: the role of the action (editors write, only
|
|
// the owner changes the schema), the declared definition of the tool, the
|
|
// resource's admission, the revision the person read, and the add-on's
|
|
// receipt, which makes a repeat of the same operation one change.
|
|
func (s *Service) Apply(ctx context.Context, actor contract.Actor, resourceID string, r gateway.ApplyResourceRequest) (gateway.ApplyResult, error) {
|
|
if !validOperation(r.OperationID) || r.Generation == 0 || r.Revision.Schema == 0 || !identifier.MatchString(r.Tool) {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
// The lowest role of any change is the editor's. The role of this tool
|
|
// is checked again below, once its kind is known.
|
|
key, err := s.authorized(ctx, actor, resourceID, policy.ActionWrite)
|
|
if err != nil {
|
|
return gateway.ApplyResult{}, err
|
|
}
|
|
inst, caps, err := s.session(ctx, key.AddonID, r.Generation)
|
|
if err != nil {
|
|
return gateway.ApplyResult{}, err
|
|
}
|
|
kind := ""
|
|
for _, d := range caps.ToolDefinitions {
|
|
if d.Name == r.Tool {
|
|
kind = d.Kind
|
|
}
|
|
}
|
|
action := policy.ActionWrite
|
|
switch kind {
|
|
case contract.ToolKindWrite:
|
|
case contract.ToolKindSchema:
|
|
action = policy.ActionSchema
|
|
default:
|
|
// Reads, creations, admin tools and tools nobody declared are not
|
|
// changes a person applies here.
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
if rs := caps.Resources; rs != nil && (r.Tool == rs.Rename || r.Tool == rs.Create || r.Tool == rs.Delete) {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
def, ok := definition(caps, r.Tool, kind)
|
|
if !ok {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
if _, err = s.authorized(ctx, actor, resourceID, action); err != nil {
|
|
return gateway.ApplyResult{}, err
|
|
}
|
|
// The arguments are the tool's own and name the resource through the
|
|
// path only: a body that names another one is not a valid request.
|
|
var given map[string]json.RawMessage
|
|
if len(r.Arguments) == 0 || json.Unmarshal(r.Arguments, &given) != nil || given == nil {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
if _, named := given[contract.ResourceArgument]; named {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
if _, named := given[gateway.ToolKey]; named {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
asked := make(map[string]json.RawMessage, len(given)+1)
|
|
for k, v := range given {
|
|
asked[k] = v
|
|
}
|
|
asked[contract.ResourceArgument], _ = json.Marshal(key.ResourceID)
|
|
raw, _ := json.Marshal(asked)
|
|
res, body, err := toolargs.BindDefinition(def, raw)
|
|
if err != nil || res != key.ResourceID {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
wire := uiOperation(actor, "apply", r.OperationID)
|
|
want := binding{tool: r.Tool, hash: requestHash(key.ResourceID, r.Tool, raw)}
|
|
if have, ok := s.bound(wire); ok && have.hash != want.hash {
|
|
// The ID belongs to another request: refuse it before any call to
|
|
// the add-on, so it is never an unknown outcome.
|
|
return gateway.ApplyResult{}, gateway.ErrOperationConflict
|
|
}
|
|
var out gateway.ApplyResult
|
|
err = s.o.Policy.Admit(ctx, actor, key, action, func(rctx context.Context) error {
|
|
if s.o.Broker != nil {
|
|
if e := s.o.Broker.BlockedError(actor, key); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
var e error
|
|
out, e = s.typedApply(rctx, actor, key, inst, string(action), wire, body, r.Revision, r.OperationID, want)
|
|
return e
|
|
})
|
|
if err != nil {
|
|
return gateway.ApplyResult{}, classify(err)
|
|
}
|
|
out.ResourceID, out.OperationID, out.RequestHash = key.ResourceID, r.OperationID, want.hash
|
|
return out, nil
|
|
}
|
|
|
|
// typedApply is typedChange that also returns what the receipt reported.
|
|
func (s *Service) typedApply(ctx context.Context, actor contract.Actor, key policy.ResourceKey, inst addonruntime.Instance, action, wire string, body json.RawMessage, seen contract.Revision, original string, want binding) (gateway.ApplyResult, error) {
|
|
cur, err := s.o.Runtime.Runtime(ctx, key.AddonID)
|
|
if err != nil || cur.Client == nil {
|
|
return gateway.ApplyResult{}, unavailable(err)
|
|
}
|
|
if cur.Generation != inst.Generation {
|
|
return gateway.ApplyResult{}, gateway.ErrStale
|
|
}
|
|
env := func(scope string, d time.Duration) contract.Envelope {
|
|
return contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: wire, Generation: cur.Generation, Action: action, Arguments: body},
|
|
Authority: contract.Authority{Actor: actor, Scope: scope, ExpiresAt: s.o.Now().Add(d)},
|
|
}
|
|
}
|
|
preview, err := cur.Client.Preview(ctx, env("preview", s.o.Limits.PreviewTimeout))
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return gateway.ApplyResult{}, ctx.Err()
|
|
}
|
|
if errors.Is(err, contract.ErrRefused) {
|
|
return gateway.ApplyResult{}, gateway.Refused(err)
|
|
}
|
|
return gateway.ApplyResult{}, s.previewFailure(ctx, actor, key, cur, err)
|
|
}
|
|
if preview.Revision != seen {
|
|
// A repeat of a change that already committed previews as its own
|
|
// receipt's revision. It goes on to apply, where the add-on replays
|
|
// the receipt for the same request and refuses another one.
|
|
prior, e := cur.Client.Receipt(ctx, wire)
|
|
if e != nil || prior.State != contract.ReceiptCommitted || prior.Revision != preview.Revision {
|
|
return gateway.ApplyResult{}, gateway.ErrStale
|
|
}
|
|
}
|
|
if !s.bind(wire, want) {
|
|
return gateway.ApplyResult{}, gateway.ErrOperationConflict
|
|
}
|
|
receipt, err := cur.Client.Apply(ctx, env("apply", s.o.Limits.ApplyTimeout), preview.Revision)
|
|
if err != nil {
|
|
if errors.Is(err, addonruntime.ErrConflict) {
|
|
// The add-on's ledger holds the ID for another request and
|
|
// changed nothing.
|
|
return gateway.ApplyResult{}, gateway.ErrOperationConflict
|
|
}
|
|
if errors.Is(err, contract.ErrRefused) {
|
|
// The add-on refused the apply before it began, so nothing was
|
|
// applied and nothing was recorded for the ID: it is free again.
|
|
s.unbind(wire, want)
|
|
return gateway.ApplyResult{}, gateway.Refused(err)
|
|
}
|
|
return gateway.ApplyResult{}, &gateway.OutcomeUnknown{OperationID: original}
|
|
}
|
|
switch receipt.State {
|
|
case contract.ReceiptCommitted:
|
|
return gateway.ApplyResult{Revision: receipt.Revision, Result: receipt.Result.Payload}, nil
|
|
case contract.ReceiptRejected:
|
|
// Nothing was recorded for the ID, so it is free again.
|
|
s.unbind(wire, want)
|
|
// A rejection names why in the add-on's own words: a moved revision
|
|
// is the stale view, anything else is a request it cannot carry out.
|
|
var why struct {
|
|
Error string `json:"error"`
|
|
}
|
|
if json.Unmarshal(receipt.Result.Payload, &why) == nil && why.Error != "" && why.Error != "stale" {
|
|
return gateway.ApplyResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
return gateway.ApplyResult{}, gateway.ErrStale
|
|
}
|
|
return gateway.ApplyResult{}, &gateway.OutcomeUnknown{OperationID: original}
|
|
}
|
|
|
|
// previewFailure tells a refusal of the request from an outage: the runtime
|
|
// client reports any non-success the same way, so an add-on that still
|
|
// answers a read made the refusal.
|
|
func (s *Service) previewFailure(ctx context.Context, actor contract.Actor, key policy.ResourceKey, inst addonruntime.Instance, cause error) error {
|
|
args, _ := json.Marshal(map[string]string{gateway.ToolKey: "resource-exists"})
|
|
env := contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: randomID("p"), Generation: inst.Generation, Action: "read", Arguments: args},
|
|
Authority: contract.Authority{Actor: actor, Scope: "read", ExpiresAt: s.o.Now().Add(s.o.Limits.ReadTimeout)},
|
|
}
|
|
if _, e := inst.Client.Read(ctx, env); e == nil {
|
|
return gateway.ErrInvalidRequest
|
|
}
|
|
return unavailable(cause)
|
|
}
|
|
|
|
// Delete deletes a resource and its data, as the owner's explicit UI action.
|
|
//
|
|
// The order is the whole contract and every step is durable:
|
|
//
|
|
// 1. The resource becomes deleting. It is inaccessible to everyone and takes
|
|
// no new admission, and admitted work is cancelled and waited for.
|
|
// 2. With no work left, the revision the owner saw and the confirmation the
|
|
// owner typed are checked. A failure here returns the resource to use.
|
|
// 3. The deletion is marked confirmed, pending proposals are invalidated and
|
|
// the add-on is asked to delete under one operation identifier.
|
|
// 4. Only after the add-on confirmed it, the record and every grant go.
|
|
//
|
|
// A crash leaves a deleting record: an unconfirmed one is returned to use, a
|
|
// confirmed one is finished by Resume. The add-on is never uninstalled.
|
|
func (s *Service) Delete(ctx context.Context, actor contract.Actor, resourceID string, r gateway.DeleteResourceRequest) (gateway.DeleteResult, error) {
|
|
if !validOperation(r.OperationID) || r.Generation == 0 || r.Revision.Schema == 0 || r.Confirm == "" || len(r.Confirm) > maxNameInput {
|
|
return gateway.DeleteResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
key, ok := s.keyOf(resourceID)
|
|
if !ok {
|
|
// A deletion that finished after its answer was lost is the same
|
|
// success for the owner who asked, under the same operation.
|
|
if s.o.Policy != nil {
|
|
done, err := s.o.Policy.DeletedBy(ctx, actor, resourceID, r.OperationID)
|
|
if err != nil {
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
if done {
|
|
return gateway.DeleteResult{ResourceID: resourceID, Deleted: true}, nil
|
|
}
|
|
}
|
|
// A resource that is not there is denied like one that is not yours.
|
|
if _, err := s.owned(ctx, actor, resourceID); err != nil {
|
|
return gateway.DeleteResult{}, err
|
|
}
|
|
return gateway.DeleteResult{}, gateway.ErrForbidden
|
|
}
|
|
if s.deletingByOwner(key, actor) {
|
|
// A repeat of a deletion that is already confirmed finishes it.
|
|
release := s.o.Policy.Track(key)
|
|
defer release()
|
|
if err := s.o.Policy.BeginDelete(ctx, actor, key, r.OperationID); err != nil {
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
return s.finishDelete(ctx, actor, key, r.OperationID)
|
|
}
|
|
if _, err := s.owned(ctx, actor, resourceID); err != nil {
|
|
return gateway.DeleteResult{}, err
|
|
}
|
|
// Only the owner role gets here, so only it marks the resource as busy.
|
|
release := s.o.Policy.Track(key)
|
|
defer release()
|
|
inst, caps, err := s.session(ctx, key.AddonID, r.Generation)
|
|
if err != nil {
|
|
return gateway.DeleteResult{}, err
|
|
}
|
|
if caps.Resources == nil || caps.Resources.Delete == "" || caps.Resources.Inspect == "" || !contains(caps.AdminTools, caps.Resources.Delete) {
|
|
return gateway.DeleteResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
seen, err := s.inspect(ctx, actor, key, inst, caps.Resources.Inspect)
|
|
if err != nil {
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
if seen.Name == "" || r.Confirm != seen.Name {
|
|
return gateway.DeleteResult{}, gateway.ErrInvalidRequest
|
|
}
|
|
if s.o.Broker != nil {
|
|
if e := s.o.Broker.BlockedError(actor, key); e != nil {
|
|
return gateway.DeleteResult{}, classify(e)
|
|
}
|
|
}
|
|
// Past this point a failure before the confirmation returns the
|
|
// resource to use, even when the request was cancelled.
|
|
confirmed := false
|
|
abort := func() {
|
|
if !confirmed {
|
|
_ = s.o.Policy.AbortDelete(key)
|
|
}
|
|
}
|
|
if err = s.o.Policy.BeginDelete(ctx, actor, key, r.OperationID); err != nil {
|
|
abort()
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
defer abort()
|
|
// No admitted work is left and nothing new is admitted: this is the
|
|
// revision the deletion removes.
|
|
cur, err := s.revisionOf(ctx, actor, key, inst, caps.Resources.Inspect)
|
|
if err != nil {
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
if cur != r.Revision {
|
|
return gateway.DeleteResult{}, gateway.ErrStale
|
|
}
|
|
if err = s.o.Policy.ConfirmDelete(key); err != nil {
|
|
return gateway.DeleteResult{}, classify(err)
|
|
}
|
|
confirmed = true
|
|
return s.finishDelete(ctx, actor, key, r.OperationID)
|
|
}
|
|
|
|
func contains(list []string, s string) bool {
|
|
for _, x := range list {
|
|
if x == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *Service) keyOf(resourceID string) (policy.ResourceKey, bool) {
|
|
if s.o.Policy == nil {
|
|
return policy.ResourceKey{}, false
|
|
}
|
|
return s.o.Policy.Key(resourceID)
|
|
}
|
|
|
|
// deletingByOwner reports a confirmed deletion that this actor, as the owner
|
|
// role, asked for.
|
|
func (s *Service) deletingByOwner(key policy.ResourceKey, actor contract.Actor) bool {
|
|
return s.deleting(key, actor, true)
|
|
}
|
|
|
|
// deletingByActor reports a deletion in progress, confirmed or not, of a
|
|
// resource this actor owns.
|
|
func (s *Service) deletingByActor(key policy.ResourceKey, actor contract.Actor) bool {
|
|
return s.deleting(key, actor, false)
|
|
}
|
|
|
|
func (s *Service) deleting(key policy.ResourceKey, actor contract.Actor, confirmedOnly bool) bool {
|
|
for _, d := range s.o.Policy.Deleting() {
|
|
if d.Key == key && (d.Confirmed || !confirmedOnly) {
|
|
owner, ok := s.o.Policy.OwnerOf(key)
|
|
return ok && (s.o.Policy.Mode() == policy.ModeLocalOwner || owner == actor.UserID)
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// revisionOf reads the revision of a resource that is already out of use, as
|
|
// the owner's own request, directly from the add-on.
|
|
func (s *Service) revisionOf(ctx context.Context, actor contract.Actor, key policy.ResourceKey, inst addonruntime.Instance, tool string) (contract.Revision, error) {
|
|
args, _ := json.Marshal(map[string]string{gateway.ToolKey: tool})
|
|
cctx, cancel := context.WithTimeout(ctx, s.o.NameTimeout)
|
|
defer cancel()
|
|
env := contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: randomID("n"), Generation: inst.Generation, Action: "read", Arguments: args},
|
|
Authority: contract.Authority{Actor: actor, Scope: "read", ExpiresAt: s.o.Now().Add(s.o.Limits.ReadTimeout)},
|
|
}
|
|
res, err := inst.Client.Read(cctx, env)
|
|
if err != nil {
|
|
return contract.Revision{}, unavailable(err)
|
|
}
|
|
var v struct {
|
|
Revision contract.Revision `json:"revision"`
|
|
}
|
|
if json.Unmarshal(res.Payload, &v) != nil || v.Revision.Schema == 0 {
|
|
return contract.Revision{}, unavailable(errors.New("inspect result has no revision"))
|
|
}
|
|
return v.Revision, nil
|
|
}
|
|
|
|
// finishDelete runs the confirmed deletion to its end.
|
|
func (s *Service) finishDelete(ctx context.Context, actor contract.Actor, key policy.ResourceKey, original string) (gateway.DeleteResult, error) {
|
|
if s.o.Broker != nil {
|
|
if err := s.o.Broker.InvalidateResource(ctx, key); err != nil {
|
|
return gateway.DeleteResult{}, &gateway.OutcomeUnknown{OperationID: original}
|
|
}
|
|
}
|
|
if err := s.settleDeletion(ctx, actor, key, original); err != nil {
|
|
return gateway.DeleteResult{}, err
|
|
}
|
|
return gateway.DeleteResult{ResourceID: key.ResourceID, Deleted: true}, nil
|
|
}
|
|
|
|
// settleDeletion asks the add-on to delete the backing of a confirmed
|
|
// deletion under its one operation identifier, and removes the record and its
|
|
// grants only after the add-on confirmed. A backing that is already gone is
|
|
// the same outcome.
|
|
func (s *Service) settleDeletion(ctx context.Context, actor contract.Actor, key policy.ResourceKey, original string) error {
|
|
unknown := func() error { return &gateway.OutcomeUnknown{OperationID: original} }
|
|
inst, caps, err := s.session(ctx, key.AddonID, 0)
|
|
if err != nil {
|
|
return unknown()
|
|
}
|
|
if s.o.BackingConfirmed != nil {
|
|
found, e := s.o.BackingConfirmed(ctx, key)
|
|
if e != nil {
|
|
return unknown()
|
|
}
|
|
if !found {
|
|
return s.complete(key, unknown)
|
|
}
|
|
}
|
|
if caps.Resources == nil || caps.Resources.Delete == "" || !contains(caps.AdminTools, caps.Resources.Delete) {
|
|
return unknown()
|
|
}
|
|
body, _ := json.Marshal(map[string]string{gateway.ToolKey: caps.Resources.Delete})
|
|
wire := deleteOperation(key.ResourceID)
|
|
env := func(scope string, d time.Duration) contract.Envelope {
|
|
return contract.Envelope{
|
|
Request: contract.Request{AddonID: key.AddonID, ResourceID: key.ResourceID, OperationID: wire, Generation: inst.Generation, Action: string(policy.ActionDelete), Arguments: body},
|
|
Authority: contract.Authority{Actor: actor, Scope: scope, ExpiresAt: s.o.Now().Add(d)},
|
|
}
|
|
}
|
|
preview, err := inst.Client.Preview(ctx, env("preview", s.o.Limits.PreviewTimeout))
|
|
if err != nil {
|
|
// A preview deletes nothing; the deletion stays confirmed and is
|
|
// resumed.
|
|
return unknown()
|
|
}
|
|
receipt, err := inst.Client.Apply(ctx, env("apply", s.o.Limits.ApplyTimeout), preview.Revision)
|
|
if err != nil {
|
|
return unknown()
|
|
}
|
|
switch receipt.State {
|
|
case contract.ReceiptCommitted:
|
|
return s.complete(key, unknown)
|
|
case contract.ReceiptRejected:
|
|
// The add-on refused, so nothing was removed: the resource is back.
|
|
_ = s.o.Policy.AbortDelete(key)
|
|
return gateway.ErrStale
|
|
}
|
|
return unknown()
|
|
}
|
|
|
|
func (s *Service) complete(key policy.ResourceKey, unknown func() error) error {
|
|
// Selections that name the resource are left alone: the reference is
|
|
// denied like an unknown one, and the bounds and the expiry of the store
|
|
// remove it.
|
|
if err := s.o.Policy.FinishDelete(key); err != nil {
|
|
return unknown()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Resume settles the deletions a crash or an outage left: an unconfirmed one
|
|
// is returned to use, a confirmed one is finished. It is fail-soft and is
|
|
// repeated by the owner's reconcile loop while any deletion is left.
|
|
func (s *Service) Resume(ctx context.Context) {
|
|
if s.o.Policy == nil {
|
|
return
|
|
}
|
|
for _, d := range s.o.Policy.Deleting() {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
if s.o.Policy.Busy(d.Key) {
|
|
continue
|
|
}
|
|
if !d.Confirmed {
|
|
_ = s.o.Policy.AbortDelete(d.Key)
|
|
continue
|
|
}
|
|
release := s.o.Policy.Track(d.Key)
|
|
if s.o.Broker != nil {
|
|
_ = s.o.Broker.InvalidateResource(ctx, d.Key)
|
|
}
|
|
_ = s.settleDeletion(ctx, resumeActor, d.Key, d.Op)
|
|
release()
|
|
}
|
|
}
|
|
|
|
// Operation looks the receipt of a UI mutation up. It is read-only toward the
|
|
// add-on and available to the owner role only. kind is rename or delete.
|
|
func (s *Service) Operation(ctx context.Context, actor contract.Actor, resourceID, kind, operationID, hash string) (gateway.OperationStatus, error) {
|
|
if kind != "rename" && kind != "delete" && kind != "apply" || !validOperation(operationID) {
|
|
return gateway.OperationStatus{}, gateway.ErrInvalidRequest
|
|
}
|
|
if hash != "" && (kind != "apply" || !sha256Hex.MatchString(hash)) {
|
|
return gateway.OperationStatus{}, gateway.ErrInvalidRequest
|
|
}
|
|
if kind == "delete" && s.o.Policy != nil {
|
|
if k, known := s.o.Policy.Key(resourceID); !known {
|
|
// The record of a finished deletion is gone; its owner can still
|
|
// learn that it committed.
|
|
done, err := s.o.Policy.DeletedBy(ctx, actor, resourceID, operationID)
|
|
if err != nil {
|
|
return gateway.OperationStatus{}, classify(err)
|
|
}
|
|
if done {
|
|
return gateway.OperationStatus{OperationID: operationID, State: string(contract.ReceiptCommitted)}, nil
|
|
}
|
|
} else if s.deletingByActor(k, actor) {
|
|
return gateway.OperationStatus{OperationID: operationID, State: string(contract.ReceiptPending)}, nil
|
|
}
|
|
}
|
|
var key policy.ResourceKey
|
|
var err error
|
|
if kind == "apply" {
|
|
// Editors apply changes, so they look their own receipts up. The
|
|
// wire operation is derived from the actor: nobody reads another
|
|
// person's receipt.
|
|
key, err = s.authorized(ctx, actor, resourceID, policy.ActionWrite)
|
|
} else {
|
|
key, err = s.owned(ctx, actor, resourceID)
|
|
}
|
|
if err != nil {
|
|
return gateway.OperationStatus{}, err
|
|
}
|
|
wire := uiOperation(actor, kind, operationID)
|
|
if kind == "delete" {
|
|
wire = deleteOperation(key.ResourceID)
|
|
}
|
|
inst, _, err := s.session(ctx, key.AddonID, 0)
|
|
if err != nil {
|
|
return gateway.OperationStatus{}, err
|
|
}
|
|
have, known := s.bound(wire)
|
|
if kind == "apply" && hash != "" && (!known || have.hash != hash) {
|
|
if known {
|
|
// The ID was used for another request: its receipt is not an
|
|
// answer about this one.
|
|
return gateway.OperationStatus{}, gateway.ErrOperationConflict
|
|
}
|
|
// The host does not hold the request this ID was used for, so it
|
|
// cannot vouch that the receipt is the caller's. Repeating the
|
|
// change is safe: the add-on replays the same request and refuses
|
|
// another one.
|
|
return gateway.OperationStatus{OperationID: operationID, State: string(contract.ReceiptUnknown)}, nil
|
|
}
|
|
rcpt, err := inst.Client.Receipt(ctx, wire)
|
|
if err != nil {
|
|
return gateway.OperationStatus{}, unavailable(err)
|
|
}
|
|
st := gateway.OperationStatus{OperationID: operationID, State: string(rcpt.State)}
|
|
if kind == "apply" && known {
|
|
st.Tool, st.RequestHash = have.tool, have.hash
|
|
}
|
|
return st, nil
|
|
}
|
|
|
|
// conversation is the validated key of a selection.
|
|
func conversation(id string) bool { return toolargs.ValidConversationID(id) }
|
|
|
|
func (s *Service) selections() (*SelectionStore, error) {
|
|
if s.o.Selections == nil || s.o.Policy == nil {
|
|
return nil, gateway.ErrUnavailable
|
|
}
|
|
return s.o.Selections, nil
|
|
}
|
|
|
|
// authorizeSelected re-authorizes a stored reference at the owner: the actor
|
|
// must hold at least the viewer role now. A reference that fails reports the
|
|
// same denial as an unknown resource. It is kept: it is no authority, and the
|
|
// person may be granted the resource again.
|
|
func (s *Service) authorizeSelected(ctx context.Context, actor contract.Actor, addonID, resourceID string) (policy.ResourceKey, policy.Role, error) {
|
|
key, ok := s.o.Policy.Key(resourceID)
|
|
if ok && key.AddonID != addonID {
|
|
ok = false
|
|
}
|
|
if !ok {
|
|
key = policy.ResourceKey{AddonID: addonID, ResourceID: resourceID}
|
|
}
|
|
role, err := s.o.Policy.RoleOf(ctx, actor, key)
|
|
if err != nil {
|
|
return key, "", classify(err)
|
|
}
|
|
return key, role, nil
|
|
}
|
|
|
|
// Selection reads the selection of a conversation. A missing, revoked or
|
|
// deleted reference is the same answer: nothing is selected.
|
|
func (s *Service) Selection(ctx context.Context, actor contract.Actor, conv, addonID string) (gateway.Selection, error) {
|
|
st, err := s.selections()
|
|
if err != nil {
|
|
return gateway.Selection{}, err
|
|
}
|
|
if !conversation(conv) || !identifier.MatchString(addonID) {
|
|
return gateway.Selection{}, gateway.ErrInvalidRequest
|
|
}
|
|
id, ok, e := st.Get(actor.UserID, conv, addonID)
|
|
if e != nil {
|
|
return gateway.Selection{}, unavailable(e)
|
|
}
|
|
if !ok {
|
|
return gateway.Selection{}, gateway.ErrNotSelected
|
|
}
|
|
key, role, err := s.authorizeSelected(ctx, actor, addonID, id)
|
|
if err != nil {
|
|
if errors.Is(err, gateway.ErrForbidden) {
|
|
return gateway.Selection{}, gateway.ErrNotSelected
|
|
}
|
|
return gateway.Selection{}, err
|
|
}
|
|
return s.selectionView(ctx, actor, conv, key, role), nil
|
|
}
|
|
|
|
func (s *Service) selectionView(ctx context.Context, actor contract.Actor, conv string, key policy.ResourceKey, role policy.Role) gateway.Selection {
|
|
out := gateway.Selection{AddonID: key.AddonID, ConversationID: conv, ResourceID: key.ResourceID, Role: string(role)}
|
|
if inst, caps, err := s.session(ctx, key.AddonID, 0); err == nil && caps.Resources != nil && caps.Resources.Inspect != "" {
|
|
if v, e := s.inspect(ctx, actor, key, inst, caps.Resources.Inspect); e == nil {
|
|
out.Name = v.Name
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Select stores the selection of a conversation. The caller must hold at
|
|
// least the viewer role on the resource now, and the resource must belong to
|
|
// the add-on of the path.
|
|
func (s *Service) Select(ctx context.Context, actor contract.Actor, conv, addonID, resourceID string) (gateway.Selection, error) {
|
|
st, err := s.selections()
|
|
if err != nil {
|
|
return gateway.Selection{}, err
|
|
}
|
|
if !conversation(conv) || !identifier.MatchString(addonID) || !identifier.MatchString(resourceID) {
|
|
return gateway.Selection{}, gateway.ErrInvalidRequest
|
|
}
|
|
key, ok := s.o.Policy.Key(resourceID)
|
|
if !ok || key.AddonID != addonID {
|
|
// The same denial as a resource the person may not read.
|
|
if _, err = s.o.Policy.RoleOf(ctx, actor, policy.ResourceKey{AddonID: addonID, ResourceID: resourceID}); err != nil {
|
|
return gateway.Selection{}, classify(err)
|
|
}
|
|
return gateway.Selection{}, gateway.ErrForbidden
|
|
}
|
|
role, err := s.o.Policy.RoleOf(ctx, actor, key)
|
|
if err != nil {
|
|
return gateway.Selection{}, classify(err)
|
|
}
|
|
if e := st.Put(actor.UserID, conv, addonID, resourceID); e != nil {
|
|
return gateway.Selection{}, unavailable(e)
|
|
}
|
|
return s.selectionView(ctx, actor, conv, key, role), nil
|
|
}
|
|
|
|
// ClearSelection removes the selection of a conversation.
|
|
func (s *Service) ClearSelection(_ context.Context, actor contract.Actor, conv, addonID string) error {
|
|
st, err := s.selections()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !conversation(conv) || !identifier.MatchString(addonID) {
|
|
return gateway.ErrInvalidRequest
|
|
}
|
|
if e := st.Delete(actor.UserID, conv, addonID); e != nil {
|
|
return unavailable(e)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ResolveSelection is the tool path: the resource ID of the conversation's
|
|
// selection after the owner authorized it for the actor, or ErrNotSelected,
|
|
// or the denial of an unknown resource.
|
|
func (s *Service) ResolveSelection(ctx context.Context, actor contract.Actor, conv, addonID string) (string, error) {
|
|
st, err := s.selections()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !conversation(conv) || !identifier.MatchString(addonID) {
|
|
return "", gateway.ErrInvalidRequest
|
|
}
|
|
id, ok, e := st.Get(actor.UserID, conv, addonID)
|
|
if e != nil {
|
|
return "", unavailable(e)
|
|
}
|
|
if !ok {
|
|
return "", gateway.ErrNotSelected
|
|
}
|
|
if _, _, err = s.authorizeSelected(ctx, actor, addonID, id); err != nil {
|
|
return "", err
|
|
}
|
|
return id, nil
|
|
}
|