fix(intake): security reports outlive their workspace
Purging an archived workspace deleted its intake reports with it: every intake table cascaded from tenants. Benjamin's decision Nr. 17 (docgen#6 17878, V-g): no automatic deletion of the reports when a workspace is deleted. The purge now refuses first, before any file or account is removed: 409 intake_retained while the workspace has reports. Migration 0030 makes the database refuse as well (intake_reports → tenants ON DELETE RESTRICT), and DeleteTenant maps that to ErrConflict. CountIntakeReports on both stores. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
This commit is contained in:
1 parent
25f46706a8
commit
34921bd44e
10 files changed
+101
-6
No files matched your search
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"strings"
|
||||
@@ -119,6 +120,15 @@ func (s *Server) adminPurgeTenant(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusConflict, "not_archived", "only an archived tenant can be deleted")
|
||||
return
|
||||
}
|
||||
// Security reports outlive their workspace (docgen#6 decision Nr. 17,
|
||||
// V-g): checked before anything is removed.
|
||||
if n, err := s.Store.CountIntakeReports(ctx, t.ID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "internal", err.Error())
|
||||
return
|
||||
} else if n > 0 {
|
||||
writeError(w, http.StatusConflict, "intake_retained", fmt.Sprintf("the workspace has %d security report(s); they are kept, so the workspace cannot be deleted", n))
|
||||
return
|
||||
}
|
||||
|
||||
docs, err := s.Store.ListDocuments(ctx, t.ID, false)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/keycloak"
|
||||
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
||||
@@ -132,3 +136,36 @@ func TestAdminPurgeTenant(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Security reports outlive their workspace (docgen#6 decision Nr. 17,
|
||||
// V-g): an archived workspace with reports is not purged, and nothing of it
|
||||
// is removed.
|
||||
func TestPurgeKeepsIntakeReports(t *testing.T) {
|
||||
eachStore(t, func(t *testing.T, h *testHarness) {
|
||||
tn := createWithAdmin(t, h, "reports-co", "it@reports.test")
|
||||
if _, _, err := h.store.CreateIntakeReport(context.Background(), store.IntakeCapture{TenantID: tn.ID, Channel: store.IntakeChannelPortal,
|
||||
ReporterSub: "u-1", About: store.IntakeAboutProduct, Danger: "no", Original: json.RawMessage(`{"what":"x"}`), At: time.Now().UTC()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp, _ := h.do("POST", "/v1/tenants/"+tn.ID+"/status", map[string]any{"status": "archived", "source": "test"}); resp.StatusCode != 200 {
|
||||
t.Fatalf("archive: %d", resp.StatusCode)
|
||||
}
|
||||
resp, raw := h.do("DELETE", "/v1/admin/tenants/"+tn.ID+"?confirm=reports-co", nil)
|
||||
if resp.StatusCode != 409 || !strings.Contains(string(raw), "intake_retained") {
|
||||
t.Fatalf("purge with reports: %d %s", resp.StatusCode, raw)
|
||||
}
|
||||
if len(h.kcMock.Purged) != 0 {
|
||||
t.Fatalf("sign-in removed although the purge was refused: %v", h.kcMock.Purged)
|
||||
}
|
||||
if resp, _ := h.do("GET", "/v1/tenants/"+tn.ID, nil); resp.StatusCode != 200 {
|
||||
t.Fatalf("tenant gone: %d", resp.StatusCode)
|
||||
}
|
||||
if n, err := h.store.CountIntakeReports(context.Background(), tn.ID); err != nil || n != 1 {
|
||||
t.Fatalf("reports kept: %d %v", n, err)
|
||||
}
|
||||
// The store refuses on its own too.
|
||||
if err := h.store.DeleteTenant(context.Background(), tn.ID); !errors.Is(err, store.ErrConflict) {
|
||||
t.Fatalf("store delete with reports: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -143,11 +143,15 @@ func TestIntakeStore(t *testing.T) {
|
||||
if l, err := h.store.ListIntakeReports(ctx, "not-a-uuid", "u-1"); err != nil || len(l) != 0 {
|
||||
t.Fatalf("list of a bad tenant: %v %v", l, err)
|
||||
}
|
||||
if err := h.store.DeleteTenant(ctx, h.tenant.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
// Security reports outlive their tenant (docgen#6 decision Nr. 17).
|
||||
if err := h.store.DeleteTenant(ctx, h.tenant.ID); !errors.Is(err, store.ErrConflict) {
|
||||
t.Fatalf("delete a tenant with reports: %v", err)
|
||||
}
|
||||
if p, err := h.store.PendingIntake(ctx, 0, 10); err != nil || len(p) != 0 {
|
||||
t.Fatalf("after the tenant is purged: %+v %v", p, err)
|
||||
if n, err := h.store.CountIntakeReports(ctx, h.tenant.ID); err != nil || n != 4 {
|
||||
t.Fatalf("reports kept: %d %v", n, err)
|
||||
}
|
||||
if n, err := h.store.CountIntakeReports(ctx, "not-a-uuid"); err != nil || n != 0 {
|
||||
t.Fatalf("count of a bad tenant: %d %v", n, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -601,6 +601,9 @@ func (m *Memory) DeleteTenant(_ context.Context, id string) error {
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
if slices.ContainsFunc(m.intakeReports, func(r IntakeReport) bool { return r.TenantID == id }) {
|
||||
return ErrConflict // security reports outlive the tenant (0030)
|
||||
}
|
||||
delete(m.tenants, id)
|
||||
delete(m.bySlug, t.Slug)
|
||||
delete(m.products, id)
|
||||
|
||||
@@ -80,6 +80,18 @@ func (m *Memory) ListIntakeReports(_ context.Context, tenantID, reporterSub stri
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) CountIntakeReports(_ context.Context, tenantID string) (int, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
n := 0
|
||||
for _, r := range m.intakeReports {
|
||||
if r.TenantID == tenantID {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (m *Memory) IntakeEntries(_ context.Context, tenantID, ref string) ([]IntakeEntry, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
@@ -734,9 +734,14 @@ func (p *Postgres) TenantActivity(ctx context.Context, tenantIDs []string, since
|
||||
}
|
||||
|
||||
// DeleteTenant implements Store. The foreign keys do the rest: every table
|
||||
// that belongs to a tenant cascades, audit_log keeps its rows (SET NULL).
|
||||
// that belongs to a tenant cascades, audit_log keeps its rows (SET NULL);
|
||||
// intake_reports refuses (RESTRICT, 0030): security reports outlive it.
|
||||
func (p *Postgres) DeleteTenant(ctx context.Context, id string) error {
|
||||
tag, err := p.pool.Exec(ctx, `DELETE FROM tenants WHERE id = $1`, id)
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == pgerrcode.ForeignKeyViolation {
|
||||
return ErrConflict
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -94,6 +94,15 @@ func (p *Postgres) GetIntakeReport(ctx context.Context, tenantID, ref string) (*
|
||||
return r, err
|
||||
}
|
||||
|
||||
func (p *Postgres) CountIntakeReports(ctx context.Context, tenantID string) (int, error) {
|
||||
var n int
|
||||
err := p.pool.QueryRow(ctx, `SELECT count(*) FROM intake_reports WHERE tenant_id = $1::uuid`, tenantID).Scan(&n)
|
||||
if isInvalidUUID(err) {
|
||||
return 0, nil
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (p *Postgres) ListIntakeReports(ctx context.Context, tenantID, reporterSub string) ([]IntakeReport, error) {
|
||||
rows, err := p.pool.Query(ctx, intakeReportSelect+`WHERE tenant_id = $1::uuid AND reporter_sub = $2 ORDER BY seq DESC`, tenantID, reporterSub)
|
||||
if err != nil {
|
||||
|
||||
@@ -171,7 +171,8 @@ type Store interface {
|
||||
// DeleteTenant removes the tenant and everything that belongs to it
|
||||
// (entitlements, keys, subscriptions, usage, licences, outbox, documents,
|
||||
// projects, managed services, mail). Audit events stay, with the tenant reference cleared.
|
||||
// ErrNotFound when there is no such tenant.
|
||||
// ErrNotFound when there is no such tenant; ErrConflict while it has
|
||||
// security reports, which outlive it (docgen#6 decision Nr. 17).
|
||||
DeleteTenant(ctx context.Context, id string) error
|
||||
PublishDocument(ctx context.Context, id, by string, at time.Time) (*Document, error)
|
||||
DeleteDocument(ctx context.Context, id string) error
|
||||
@@ -262,6 +263,9 @@ type Store interface {
|
||||
CreateIntakeReport(ctx context.Context, c IntakeCapture) (*IntakeReport, *IntakeEntry, error)
|
||||
GetIntakeReport(ctx context.Context, tenantID, reference string) (*IntakeReport, error)
|
||||
ListIntakeReports(ctx context.Context, tenantID, reporterSub string) ([]IntakeReport, error)
|
||||
// CountIntakeReports: how many security reports the tenant has. A tenant
|
||||
// with reports is never deleted (docgen#6 decision Nr. 17).
|
||||
CountIntakeReports(ctx context.Context, tenantID string) (int, error)
|
||||
IntakeEntries(ctx context.Context, tenantID, reference string) ([]IntakeEntry, error)
|
||||
AddIntakeSupplement(ctx context.Context, tenantID, reference, by string, content json.RawMessage, at time.Time) (*IntakeEntry, error)
|
||||
PendingIntake(ctx context.Context, after int64, limit int) ([]PendingIntake, error)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE intake_reports DROP CONSTRAINT intake_reports_tenant_id_fkey;
|
||||
ALTER TABLE intake_reports ADD CONSTRAINT intake_reports_tenant_id_fkey
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE;
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Security reports outlive their workspace (Benjamin's decision Nr. 17,
|
||||
-- docgen#6 17878, V-g: "kein automatisches Mitlöschen der Meldungen bei
|
||||
-- Löschung des Arbeitsbereichs"). A tenant with intake reports can no
|
||||
-- longer be deleted: the purge refuses first (409 intake_retained), and the
|
||||
-- database refuses as well, should anything else try.
|
||||
ALTER TABLE intake_reports DROP CONSTRAINT intake_reports_tenant_id_fkey;
|
||||
ALTER TABLE intake_reports ADD CONSTRAINT intake_reports_tenant_id_fkey
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE RESTRICT;
|
||||
Reference in new issue
Block a user