A source now declares how its deliveries prove themselves: token (the default, unchanged), hmac (HMAC-SHA256 over the raw body, per-source header and prefix, secret sealed at rest and write-only) or none. The events module verifies before the connector parses, answers 401 and logs the refusal. The digest comparison is ported from internal/webhookguard/hmac.go and the header-borne signature from internal/infrastructure/captureintake/receiver.go; the old copies stay until their service goes. The seal package moves to internal/platform/seal so events can seal the secret.
140 lines
4.6 KiB
Go
140 lines
4.6 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"filippo.io/age"
|
|
"github.com/danielgtaylor/huma/v2"
|
|
|
|
"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 {
|
|
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, _, name string) (store.Credential, bool, error) {
|
|
for _, c := range f.creds {
|
|
if c.Name == name {
|
|
return c, true, nil
|
|
}
|
|
}
|
|
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"
|
|
_, 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.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")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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()
|
|
}
|