Files
千川白浪 d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

163 lines
4.9 KiB
Go

package pluginproto
import (
"encoding/json"
"errors"
"sort"
"strings"
"testing"
)
func TestEveryMethodIsFullyDescribed(t *testing.T) {
for method, spec := range HostMethods {
if !strings.HasPrefix(method, "host/") {
t.Errorf("%s is not a host/* method", method)
}
if spec.Status != StatusStable && spec.Status != StatusExperimental {
t.Errorf("%s has status %q", method, spec.Status)
}
if strings.TrimSpace(spec.Summary) == "" {
t.Errorf("%s has no summary", method)
}
if Feature(method) == "" || !strings.HasPrefix(Feature(method), "host.") {
t.Errorf("%s advertises feature %q", method, Feature(method))
}
for _, perm := range spec.Permissions {
if strings.TrimSpace(perm) == "" {
t.Errorf("%s lists an empty permission", method)
}
}
}
}
func TestFeaturesAreSortedUniqueAndStableOnly(t *testing.T) {
features := Features()
if !sort.StringsAreSorted(features) {
t.Fatalf("features are not sorted: %v", features)
}
seen := map[string]bool{}
for _, feature := range features {
if seen[feature] {
t.Fatalf("feature %q is advertised twice", feature)
}
seen[feature] = true
}
stable := map[string]bool{}
for method, spec := range HostMethods {
if spec.Status == StatusStable {
stable[Feature(method)] = true
}
}
if len(stable) != len(features) {
t.Fatalf("features = %v, want one per stable method (%d)", features, len(stable))
}
for feature := range stable {
if !seen[feature] {
t.Fatalf("stable feature %q is not advertised", feature)
}
}
}
func TestConsoleMethodsNeedTheWritePermission(t *testing.T) {
for _, method := range []string{
"host/console.screenshot", "host/console.pointer", "host/console.key",
"host/console.type", "host/console.typeSecret", "host/console.release",
} {
spec, ok := Lookup(method)
if !ok {
t.Fatalf("%s is missing from the catalog", method)
}
want := "vm:screen"
if method != "host/console.screenshot" {
want = "qmp:write"
}
if len(spec.Permissions) != 1 || spec.Permissions[0] != want {
t.Errorf("%s requires %v, want %s", method, spec.Permissions, want)
}
if !spec.StrictParams {
t.Errorf("%s does not decode its params strictly", method)
}
}
}
func TestVMAndConsoleMethodsDecodeStrictly(t *testing.T) {
// The driver-side methods the supervisor serves keep tolerant decoding; the
// VM and console methods the app serves are strict.
strict := []string{
"host/vm.list", "host/vm.get", "host/vm.start", "host/vm.shutdown", "host/vm.suspend",
"host/vm.resume", "host/vm.clone", "host/vm.delete", "host/vm.snapshot.list",
"host/vm.snapshot.create", "host/vm.snapshot.restore",
"host/console.screenshot", "host/console.pointer", "host/console.key",
"host/console.type", "host/console.typeSecret", "host/console.release",
}
for _, method := range strict {
spec, ok := Lookup(method)
if !ok {
t.Errorf("%s is missing from the catalog", method)
continue
}
if !spec.StrictParams {
t.Errorf("%s does not decode its params strictly", method)
}
}
}
func TestDecodeParamsTolerantKeepsUnknownFields(t *testing.T) {
out, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"vm":"a","nope":1}`), false)
if err != nil {
t.Fatalf("tolerant decode failed: %v", err)
}
if out.VM != "a" {
t.Fatalf("vm = %q", out.VM)
}
}
func TestDecodeParamsStrictRejectsUnknownFields(t *testing.T) {
_, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"vm":"a","nope":1}`), true)
var invalid *InvalidParamsError
if !errors.As(err, &invalid) {
t.Fatalf("err = %v, want InvalidParamsError", err)
}
if invalid.Method != "host/console.screenshot" {
t.Fatalf("method = %q", invalid.Method)
}
}
func TestDecodeParamsStrictRejectsTrailingData(t *testing.T) {
_, err := DecodeParams[KeyParams]("host/console.key", json.RawMessage(`{"key":"a"}{"key":"b"}`), true)
var invalid *InvalidParamsError
if !errors.As(err, &invalid) {
t.Fatalf("err = %v, want InvalidParamsError", err)
}
}
func TestDecodeParamsAcceptsMissingAndNullParams(t *testing.T) {
for _, raw := range []json.RawMessage{nil, json.RawMessage("null"), json.RawMessage(" ")} {
out, err := DecodeParams[VMParams]("host/vm.list", raw, true)
if err != nil {
t.Fatalf("decode %q failed: %v", raw, err)
}
if out != (VMParams{}) {
t.Fatalf("decode %q = %+v, want the zero value", raw, out)
}
}
}
func TestDecodeParamsReportsATypeErrorAsInvalidParams(t *testing.T) {
_, err := DecodeParams[ScreenshotParams]("host/console.screenshot", json.RawMessage(`{"max_width":"wide"}`), false)
var invalid *InvalidParamsError
if !errors.As(err, &invalid) {
t.Fatalf("err = %v, want InvalidParamsError", err)
}
}
func TestMethodsAreSorted(t *testing.T) {
methods := Methods()
if !sort.StringsAreSorted(methods) {
t.Fatalf("methods are not sorted: %v", methods)
}
if len(methods) != len(HostMethods) {
t.Fatalf("methods = %d, catalog = %d", len(methods), len(HostMethods))
}
}