feat(connectors): an accepted package can be removed, and a secret engine credentials still use is refused (archie #2013)
This commit is contained in:
4 files changed
+242
-1
No files matched your search
@@ -86,6 +86,20 @@ func (s *Store) Upsert(ctx context.Context, a Accepted) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes an accepted package and reports whether there was one. Its
|
||||
// credential slots live on the row, so they go with it.
|
||||
func (s *Store) Delete(ctx context.Context, orgID, name string) (bool, error) {
|
||||
res, err := s.db.ExecContext(ctx, `DELETE FROM connectors.accepted WHERE org_id = $1 AND name = $2`, orgID, name)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("remove connector %q: %w", name, err)
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("remove connector %q: %w", name, err)
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
// Credential returns the named credential; ok is false when there is none.
|
||||
func (s *Store) Credential(ctx context.Context, orgID, name string) (Credential, bool, error) {
|
||||
var c Credential
|
||||
|
||||
@@ -32,7 +32,7 @@ var validName = regexp.MustCompile(`^[a-z][a-z0-9-]{0,62}$`)
|
||||
type Module struct {
|
||||
orgID string
|
||||
host *Host
|
||||
store *store.Store
|
||||
store registry
|
||||
sealer *seal.Sealer
|
||||
log *slog.Logger
|
||||
|
||||
@@ -46,6 +46,16 @@ type Module struct {
|
||||
failed map[string]string
|
||||
}
|
||||
|
||||
// registry is the module's persistence, so removal can be tested with a fake.
|
||||
type registry interface {
|
||||
List(ctx context.Context, orgID string) ([]store.Accepted, error)
|
||||
Upsert(ctx context.Context, a store.Accepted) error
|
||||
Delete(ctx context.Context, orgID, name string) (bool, error)
|
||||
Credential(ctx context.Context, orgID, name string) (store.Credential, bool, error)
|
||||
Credentials(ctx context.Context, orgID string) ([]store.Credential, error)
|
||||
PutCredential(ctx context.Context, c store.Credential) error
|
||||
}
|
||||
|
||||
// Sealing is the age identity credentials are sealed with, and the backup
|
||||
// recipients that can also open them.
|
||||
type Sealing struct {
|
||||
@@ -202,6 +212,9 @@ func (m *Module) Routes(mux *http.ServeMux, guard func(http.Handler) http.Handle
|
||||
mux.Handle("PUT /api/connectors/{name}", guard(m.accept(store.ContractConnector)))
|
||||
mux.Handle("PUT /api/secret-engines/{name}", guard(m.accept(store.ContractSecretEngine)))
|
||||
mux.Handle("PUT /api/mcp-servers/{name}", guard(http.HandlerFunc(m.acceptServer)))
|
||||
mux.Handle("DELETE /api/connectors/{name}", guard(m.remove(store.ContractConnector)))
|
||||
mux.Handle("DELETE /api/secret-engines/{name}", guard(m.remove(store.ContractSecretEngine)))
|
||||
mux.Handle("DELETE /api/mcp-servers/{name}", guard(m.remove(store.ContractMCP)))
|
||||
mux.Handle("PUT /api/credentials/{name}", guard(http.HandlerFunc(m.putCredential)))
|
||||
mux.Handle("GET /api/credentials", guard(http.HandlerFunc(m.listCredentials)))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package connectors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/platform/httpjson"
|
||||
"github.com/samcharles93/archie-core/modules/connectors/internal/store"
|
||||
)
|
||||
|
||||
func (m *Module) remove(contract string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) { m.removeAs(w, r, contract) }
|
||||
}
|
||||
|
||||
// removeAs retires an accepted package: its row (and so its credential slots)
|
||||
// is deleted and its process stopped. A name that isn't accepted under this
|
||||
// route's contract is a 404, which keeps a repeated removal harmless. A secret
|
||||
// engine a stored credential still resolves through is refused, since those
|
||||
// credentials would stop resolving.
|
||||
func (m *Module) removeAs(w http.ResponseWriter, r *http.Request, contract string) {
|
||||
ctx := r.Context()
|
||||
name := r.PathValue("name")
|
||||
accepted, err := m.store.List(ctx, m.orgID)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(ctx, "list accepted packages", "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't read the accepted packages")
|
||||
return
|
||||
}
|
||||
if !slices.ContainsFunc(accepted, func(a store.Accepted) bool { return a.Name == name && a.Contract == contract }) {
|
||||
httpjson.Error(w, http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
|
||||
return
|
||||
}
|
||||
if contract == store.ContractSecretEngine {
|
||||
users, err := m.engineUsers(ctx, name)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(ctx, "check secret engine use", "engine", name, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't check the credentials that use "+name)
|
||||
return
|
||||
}
|
||||
if len(users) > 0 {
|
||||
httpjson.Error(w, http.StatusConflict,
|
||||
fmt.Sprintf("secret engine %s is still used by credentials: %s", name, strings.Join(users, ", ")))
|
||||
return
|
||||
}
|
||||
}
|
||||
found, err := m.store.Delete(ctx, m.orgID, name)
|
||||
if err != nil {
|
||||
m.log.ErrorContext(ctx, "remove accepted package", "package", name, "error", err)
|
||||
httpjson.Error(w, http.StatusInternalServerError, "couldn't remove "+name)
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
httpjson.Error(w, http.StatusNotFound, fmt.Sprintf("no accepted %s named %s", contract, name))
|
||||
return
|
||||
}
|
||||
m.release(name)
|
||||
m.mu.Lock()
|
||||
delete(m.failed, name)
|
||||
m.mu.Unlock()
|
||||
// There is no audit trail module yet, so the removal is a structured log
|
||||
// line.
|
||||
m.log.InfoContext(ctx, "accepted package removed", "audit", true, "action", "package.remove", "contract", contract, "package", name)
|
||||
httpjson.Write(w, http.StatusOK, map[string]string{"name": name, "state": "removed"})
|
||||
}
|
||||
|
||||
// engineUsers names, in order, the credentials with a field resolved through
|
||||
// the secret engine.
|
||||
func (m *Module) engineUsers(ctx context.Context, engine string) ([]string, error) {
|
||||
creds, err := m.store.Credentials(ctx, m.orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var users []string
|
||||
for _, c := range creds {
|
||||
fields, err := m.openCredential(c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range fields {
|
||||
if f.Engine == engine {
|
||||
users = append(users, c.Name)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user