Packages are accepted once by the instance. Installations configure them at the instance, an organisation or a person; bindings and credentials resolve the person's, then their organisation's, then the instance's. Decisions, approval requests and held chat actions record the installation and scope that acted. The dashboard lists packages separately from installations, shows every installation's scope, and creates, edits and disables installations at their scope.
196 lines
8.6 KiB
Go
196 lines
8.6 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpapi"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// extensionKinds lists the API path and names for each kind of accepted
|
|
// package. The group comes from the contract's entry in store.Contracts.
|
|
func extensionKinds() []extensionKind {
|
|
return []extensionKind{
|
|
{store.ContractConnector, "/api/connectors", "connector", "connector", "connectors", "Connectors"},
|
|
{store.ContractSecretEngine, "/api/secret-engines", "secret-engine", "secret engine", "secret engines", "Secret engines"},
|
|
{store.ContractGuard, "/api/guards", "guard", "guard", "guards", "Guards"},
|
|
{store.ContractMCP, "/api/mcp-servers", "mcp-server", "MCP server", "MCP servers", "MCP servers"},
|
|
}
|
|
}
|
|
|
|
type extensionKind struct {
|
|
contract, path, noun, singular, plural, tag string
|
|
}
|
|
|
|
// ExtensionKind is one kind of accepted package and the group it belongs to.
|
|
type ExtensionKind struct {
|
|
Kind string `json:"kind" doc:"The API path segment that lists this kind"`
|
|
Contract string `json:"contract" doc:"The package contract this kind accepts"`
|
|
Label string `json:"label"`
|
|
Singular string `json:"singular"`
|
|
Group string `json:"group" enum:"platform,integration,agent"`
|
|
}
|
|
|
|
func listExtensionKinds(context.Context, *struct{}) (*httpapi.Reply[[]ExtensionKind], error) {
|
|
kinds := extensionKinds()
|
|
out := make([]ExtensionKind, 0, len(kinds))
|
|
for _, k := range kinds {
|
|
out = append(out, ExtensionKind{
|
|
Kind: strings.TrimPrefix(k.path, "/api/"), Contract: k.contract, Label: k.tag, Singular: k.singular,
|
|
Group: string(store.GroupOf(k.contract)),
|
|
})
|
|
}
|
|
return httpapi.OK(out), nil
|
|
}
|
|
|
|
// Register adds the API for packages, installations and credentials. Accepting
|
|
// or removing a package is for an instance administrator; installations and
|
|
// credentials are checked against the scope that owns them in each handler.
|
|
func (m *Module) Register(api huma.API) {
|
|
read := httpapi.Authorized(governance.ActionRead, governance.KindConnector)
|
|
httpapi.Register(api, read, huma.Operation{
|
|
OperationID: "list-extension-kinds", Method: http.MethodGet, Path: "/api/extension-kinds",
|
|
Summary: "List the kinds of extension and the group each belongs to", Tags: []string{"Connectors"},
|
|
}, listExtensionKinds)
|
|
for _, k := range extensionKinds() {
|
|
tags := []string{k.tag}
|
|
httpapi.Register(api, read, huma.Operation{
|
|
OperationID: "list-" + k.noun + "s", Method: http.MethodGet, Path: k.path,
|
|
Summary: "List the " + k.plural + " the caller can use", Tags: tags,
|
|
}, m.list(k.contract))
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionDelete, governance.KindConnector), huma.Operation{
|
|
OperationID: "remove-" + k.noun, Method: http.MethodDelete, Path: k.path + "/{name}",
|
|
Summary: "Remove an accepted package", Tags: tags,
|
|
}, m.remove(k.contract))
|
|
if k.contract == store.ContractMCP {
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindMCPServer), huma.Operation{
|
|
OperationID: "accept-mcp-server", Method: http.MethodPut, Path: k.path + "/{name}",
|
|
Summary: "Accept an MCP server and the tools it may offer", Tags: tags,
|
|
}, m.acceptServer)
|
|
continue
|
|
}
|
|
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindConnector), huma.Operation{
|
|
OperationID: "accept-" + k.noun, Method: http.MethodPut, Path: k.path + "/{name}",
|
|
Summary: "Accept a package and start it", Tags: tags,
|
|
}, m.accept(k.contract))
|
|
}
|
|
m.registerInstallations(api)
|
|
tags := []string{"Credentials"}
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionRead, governance.KindSecret), huma.Operation{
|
|
OperationID: "list-credentials", Method: http.MethodGet, Path: "/api/credentials",
|
|
Summary: "List the credentials the caller can see, never their values", Tags: tags,
|
|
}, m.listCredentials)
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionRead, governance.KindSecret), huma.Operation{
|
|
OperationID: "get-credential", Method: http.MethodGet, Path: "/api/credentials/{name}",
|
|
Summary: "Get the credential a name resolves to for the caller, never its values", Tags: tags,
|
|
}, m.getCredential)
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionUpdate, governance.KindSecret), huma.Operation{
|
|
OperationID: "put-credential", Method: http.MethodPut, Path: "/api/credentials/{name}",
|
|
Summary: "Store a credential sealed at a scope and restart what uses it", Tags: tags,
|
|
}, m.putCredential)
|
|
httpapi.Register(api, httpapi.Authorized(governance.ActionRead, governance.KindSecret), huma.Operation{
|
|
OperationID: "list-credential-kinds", Method: http.MethodGet, Path: "/api/credential-kinds",
|
|
Summary: "List credential kinds and the fields each takes", Tags: tags,
|
|
}, listCredentialKinds)
|
|
m.registerCatalogue(api)
|
|
m.registerExtensions(api)
|
|
}
|
|
|
|
type acceptRequest struct {
|
|
Path string `json:"path" doc:"The extension binary on the host"`
|
|
SHA256 string `json:"sha256" doc:"The binary's digest; it runs only while the digest matches"`
|
|
Egress []string `json:"egress,omitzero" doc:"Every host the extension may reach"`
|
|
Env []string `json:"env,omitzero" doc:"Host environment variables the extension may see"`
|
|
Settings map[string]string `json:"settings,omitzero"`
|
|
// Credentials maps each credential slot the package declares to a
|
|
// credential by name.
|
|
Credentials map[string]string `json:"credentials,omitzero"`
|
|
}
|
|
|
|
func (m *Module) accept(contract string) func(context.Context, *packageInput) (*httpapi.Reply[packageState], error) {
|
|
return func(ctx context.Context, in *packageInput) (*httpapi.Reply[packageState], error) {
|
|
return m.acceptAs(ctx, in, contract)
|
|
}
|
|
}
|
|
|
|
type packageInput struct {
|
|
Name string `path:"name" doc:"Lowercase letters, digits and hyphens"`
|
|
Body acceptRequest
|
|
}
|
|
|
|
type nameInput struct {
|
|
Name string `path:"name"`
|
|
}
|
|
|
|
// packageState is an accepted package's name and state after a change.
|
|
type packageState struct {
|
|
Name string `json:"name"`
|
|
State string `json:"state" enum:"running,removed"`
|
|
}
|
|
|
|
// acceptAs accepts a package by path for the instance and creates the
|
|
// instance installation of the same name, so the extension runs for every
|
|
// organisation.
|
|
func (m *Module) acceptAs(ctx context.Context, in *packageInput, contract string) (*httpapi.Reply[packageState], error) {
|
|
name, req := in.Name, in.Body
|
|
if !validName.MatchString(name) || req.Path == "" || req.SHA256 == "" {
|
|
return nil, huma.NewError(http.StatusBadRequest, "a connector needs a lowercase name, a path and a sha256")
|
|
}
|
|
settings, err := json.Marshal(req.Settings)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
slots, err := json.Marshal(req.Credentials, json.FormatNilMapAsNull(false))
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusBadRequest, err.Error())
|
|
}
|
|
a := store.Installed{
|
|
ID: uuid.New(), Scope: store.ScopeInstance, Name: name, PackageName: name, Contract: contract,
|
|
Path: req.Path, SHA256: req.SHA256,
|
|
Egress: req.Egress, Env: req.Env, Settings: settings, Credentials: slots,
|
|
// Only an MCP server declares its tools at acceptance; the column
|
|
// holds a list either way.
|
|
Tools: jsontext.Value(`[]`), Enabled: true,
|
|
}
|
|
previous, found, err := m.instanceRow(ctx, name)
|
|
if err != nil {
|
|
return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the installations")
|
|
}
|
|
if found {
|
|
if len(previous.Manifest) > 0 {
|
|
return nil, huma.NewError(http.StatusConflict, name+" is installed from the catalogue; change it through its extension page")
|
|
}
|
|
a.ID, a.PackageID = previous.ID, previous.PackageID
|
|
}
|
|
if err := m.commit(context.WithoutCancel(ctx), a, found, previous, m.persistInstance(ctx)); err != nil {
|
|
return nil, acceptError(err)
|
|
}
|
|
if found && (previous.Path != a.Path || previous.SHA256 != a.SHA256) {
|
|
m.relaunchPackage(context.WithoutCancel(ctx), a)
|
|
}
|
|
return httpapi.OK(packageState{Name: name, State: "running"}), nil
|
|
}
|
|
|
|
// acceptError maps an error returned by commit to an HTTP error.
|
|
func acceptError(err error) error {
|
|
if errors.As(err, new(persistError)) {
|
|
return huma.NewError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return huma.NewError(http.StatusUnprocessableEntity, err.Error())
|
|
}
|
|
|
|
// errNoPackage is the refusal to install a package the instance hasn't accepted.
|
|
func errNoPackage(name string) error {
|
|
return huma.NewError(http.StatusNotFound, fmt.Sprintf("the instance hasn't accepted a package named %s", name))
|
|
}
|