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.
154 lines
5.7 KiB
Go
154 lines
5.7 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"filippo.io/age"
|
|
"github.com/danielgtaylor/huma/v2"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/seal"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
)
|
|
|
|
// fakeRegistry is an in-memory registry.
|
|
type fakeRegistry struct {
|
|
installed map[string]store.Installed
|
|
creds []store.Credential
|
|
}
|
|
|
|
func (f *fakeRegistry) List(context.Context) ([]store.Installed, error) {
|
|
var out []store.Installed
|
|
for _, a := range f.installed {
|
|
out = append(out, a)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) Overrides(context.Context) ([]store.Override, error) { return nil, nil }
|
|
|
|
func (f *fakeRegistry) Packages(context.Context) ([]store.Package, error) { return nil, nil }
|
|
|
|
func (f *fakeRegistry) DeletePackage(_ context.Context, name string) (bool, error) {
|
|
_, ok := f.installed[name]
|
|
delete(f.installed, name)
|
|
return ok, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) DeleteInstallation(context.Context, uuid.UUID) (bool, error) {
|
|
return false, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) Credentials(context.Context) ([]store.Credential, error) {
|
|
return f.creds, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) PutCredential(context.Context, store.Credential) error { return nil }
|
|
|
|
func (f *fakeRegistry) SetCapabilities(context.Context, uuid.UUID, jsontext.Value) error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeRegistry) SetOverride(context.Context, store.Override, bool, string) error { return nil }
|
|
|
|
func (f *fakeRegistry) SaveInstalled(_ context.Context, in store.Installed, _ string, _ jsontext.Value) (store.Installed, error) {
|
|
return in, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) SaveInstallation(_ context.Context, in store.Installed, _ jsontext.Value) (store.Installed, error) {
|
|
return in, nil
|
|
}
|
|
|
|
// TestRemovingAPackage guards the credential boundary of removal: a secret
|
|
// engine that a stored credential still resolves through is refused and named,
|
|
// so no credential is left unresolvable; anything else is removed once, and a
|
|
// second removal, or one on the wrong route, is a 404.
|
|
func TestRemovingAPackage(t *testing.T) {
|
|
id, err := age.GenerateX25519Identity()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sealer, err := seal.New(id.String(), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
log := slog.New(slog.DiscardHandler)
|
|
m := &Module{sealer: sealer, log: log, host: NewHost(log), failed: map[string]string{}, running: map[string]*Connector{}, engines: map[string]*Engine{}, guards: map[string]*Guard{}, servers: map[string]*Server{}, stops: map[string]context.CancelFunc{}}
|
|
credential := func(name string, fields map[string]Field) store.Credential {
|
|
plain, err := json.Marshal(fields)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sealed, err := sealer.Seal(credentialLabel(store.Owner{Scope: store.ScopeInstance}, name, "bearer"), plain)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return store.Credential{Scope: store.ScopeInstance, Name: name, Kind: "bearer", Sealed: sealed}
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
contract string
|
|
target string
|
|
wantStatus int
|
|
wantBody string
|
|
wantKept bool
|
|
}{
|
|
{name: "engine in use is refused and names its credentials", contract: store.ContractSecretEngine, target: "vault", wantStatus: 409, wantBody: "api-token, other-token", wantKept: true},
|
|
{name: "engine nobody uses is removed", contract: store.ContractSecretEngine, target: "idle-engine", wantStatus: 200},
|
|
{name: "connector is removed", contract: store.ContractConnector, target: "chat", wantStatus: 200},
|
|
{name: "missing package is a 404", contract: store.ContractConnector, target: "gone", wantStatus: 404},
|
|
{name: "wrong route is a 404 and keeps the package", contract: store.ContractMCP, target: "chat", wantStatus: 404, wantKept: true},
|
|
}
|
|
ids := map[string]uuid.UUID{"vault": uuid.New(), "idle-engine": uuid.New(), "chat": uuid.New(), "gone": uuid.New()}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
reg := &fakeRegistry{
|
|
installed: map[string]store.Installed{
|
|
"vault": {ID: ids["vault"], Scope: store.ScopeInstance, Name: "vault", PackageName: "vault", Contract: store.ContractSecretEngine},
|
|
"idle-engine": {ID: ids["idle-engine"], Scope: store.ScopeInstance, Name: "idle-engine", PackageName: "idle-engine", Contract: store.ContractSecretEngine},
|
|
"chat": {ID: ids["chat"], Scope: store.ScopeInstance, Name: "chat", PackageName: "chat", Contract: store.ContractConnector},
|
|
},
|
|
creds: []store.Credential{
|
|
credential("api-token", map[string]Field{"token": {Engine: "vault", Key: "k"}}),
|
|
credential("literal", map[string]Field{"token": {Value: "x"}}),
|
|
credential("other-token", map[string]Field{"token": {Engine: "vault", Key: "k2"}}),
|
|
},
|
|
}
|
|
m.store = reg
|
|
m.failed[ids[tt.target].String()] = "boom"
|
|
_, err := m.removeAs(t.Context(), &nameInput{Name: tt.target}, tt.contract)
|
|
status, body := statusOf(err)
|
|
if status != tt.wantStatus {
|
|
t.Fatalf("status = %d, want %d (%s)", status, tt.wantStatus, body)
|
|
}
|
|
if !strings.Contains(body, tt.wantBody) {
|
|
t.Errorf("body %q lacks %q", body, tt.wantBody)
|
|
}
|
|
if _, kept := reg.installed[tt.target]; kept != tt.wantKept {
|
|
t.Errorf("package kept = %v, want %v", kept, tt.wantKept)
|
|
}
|
|
if _, stillFailed := m.failed[ids[tt.target].String()]; tt.wantStatus == http.StatusOK && stillFailed {
|
|
t.Error("readiness still reports the removed package")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// statusOf is the status and message an operation's error answers with.
|
|
func statusOf(err error) (int, string) {
|
|
if err == nil {
|
|
return http.StatusOK, ""
|
|
}
|
|
if se, ok := errors.AsType[huma.StatusError](err); ok {
|
|
return se.GetStatus(), se.Error()
|
|
}
|
|
return http.StatusInternalServerError, err.Error()
|
|
}
|