125 lines
4.3 KiB
Go
125 lines
4.3 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"filippo.io/age"
|
|
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/seal"
|
|
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
|
)
|
|
|
|
// fakeRegistry is an in-memory registry.
|
|
type fakeRegistry struct {
|
|
accepted map[string]store.Accepted
|
|
creds []store.Credential
|
|
}
|
|
|
|
func (f *fakeRegistry) List(context.Context, string) ([]store.Accepted, error) {
|
|
var out []store.Accepted
|
|
for _, a := range f.accepted {
|
|
out = append(out, a)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) Upsert(context.Context, store.Accepted) error { return nil }
|
|
|
|
func (f *fakeRegistry) Delete(_ context.Context, _, name string) (bool, error) {
|
|
_, ok := f.accepted[name]
|
|
delete(f.accepted, name)
|
|
return ok, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) Credential(context.Context, string, string) (store.Credential, bool, error) {
|
|
return store.Credential{}, false, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) Credentials(context.Context, string) ([]store.Credential, error) {
|
|
return f.creds, nil
|
|
}
|
|
|
|
func (f *fakeRegistry) PutCredential(context.Context, store.Credential) error { return 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{orgID: "org", sealer: sealer, log: log, host: NewHost(log), failed: map[string]string{}}
|
|
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(m.credentialLabel(name, "bearer"), plain)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return store.Credential{OrgID: "org", 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},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
reg := &fakeRegistry{
|
|
accepted: map[string]store.Accepted{
|
|
"vault": {Name: "vault", Contract: store.ContractSecretEngine},
|
|
"idle-engine": {Name: "idle-engine", Contract: store.ContractSecretEngine},
|
|
"chat": {Name: "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[tt.target] = "boom"
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodDelete, "/", nil)
|
|
req.SetPathValue("name", tt.target)
|
|
rec := httptest.NewRecorder()
|
|
m.removeAs(rec, req, tt.contract)
|
|
if rec.Code != tt.wantStatus {
|
|
t.Fatalf("status = %d, want %d (%s)", rec.Code, tt.wantStatus, rec.Body)
|
|
}
|
|
if !strings.Contains(rec.Body.String(), tt.wantBody) {
|
|
t.Errorf("body %q lacks %q", rec.Body, tt.wantBody)
|
|
}
|
|
if _, kept := reg.accepted[tt.target]; kept != tt.wantKept {
|
|
t.Errorf("package kept = %v, want %v", kept, tt.wantKept)
|
|
}
|
|
if _, stillFailed := m.failed[tt.target]; tt.wantStatus == http.StatusOK && stillFailed {
|
|
t.Error("readiness still reports the removed package")
|
|
}
|
|
})
|
|
}
|
|
}
|