Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1bfb2efd94 | ||
|
|
b12d53f469 |
@@ -0,0 +1,97 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupAppendOnlyTest(t *testing.T) (*Log, *pgxpool.Pool, func()) {
|
||||||
|
t.Helper()
|
||||||
|
adminDSN := os.Getenv("TEST_ADMIN_DSN")
|
||||||
|
if adminDSN == "" {
|
||||||
|
t.Skip("TEST_ADMIN_DSN nicht gesetzt, Integrationstest uebersprungen")
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, adminDSN)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("pool: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := pool.Exec(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS audit_events (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
tenant_slug TEXT NOT NULL CHECK (tenant_slug <> ''),
|
||||||
|
actor TEXT NOT NULL CHECK (actor <> ''),
|
||||||
|
action TEXT NOT NULL CHECK (action <> ''),
|
||||||
|
target TEXT NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb
|
||||||
|
);
|
||||||
|
CREATE OR REPLACE FUNCTION audit_events_prevent_mutation() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
RAISE EXCEPTION 'audit_events ist append-only: % ist nicht erlaubt', TG_OP;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
DROP TRIGGER IF EXISTS audit_events_no_update ON audit_events;
|
||||||
|
CREATE TRIGGER audit_events_no_update
|
||||||
|
BEFORE UPDATE ON audit_events
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION audit_events_prevent_mutation();
|
||||||
|
DROP TRIGGER IF EXISTS audit_events_no_delete ON audit_events;
|
||||||
|
CREATE TRIGGER audit_events_no_delete
|
||||||
|
BEFORE DELETE ON audit_events
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION audit_events_prevent_mutation();
|
||||||
|
`); err != nil {
|
||||||
|
t.Fatalf("schema: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup := func() {
|
||||||
|
pool.Close()
|
||||||
|
}
|
||||||
|
return NewLog(pool), pool, cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 1 + Pruefung 1: direkter UPDATE/DELETE-Versuch wird von
|
||||||
|
// der Datenbank abgewiesen.
|
||||||
|
func TestAppendOnly_RejectsUpdateAndDelete(t *testing.T) {
|
||||||
|
log, pool, cleanup := setupAppendOnlyTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Append-only bedeutet: dieser Testeintrag kann NIE wieder geloescht
|
||||||
|
// werden, auch nicht vom Test selbst. Eindeutiger Tenant-Slug pro Lauf,
|
||||||
|
// damit wiederholte Testlaeufe sich nicht gegenseitig die Zaehlung
|
||||||
|
// verfaelschen.
|
||||||
|
tenantSlug := fmt.Sprintf("test_appendonly_%d", time.Now().UnixNano())
|
||||||
|
|
||||||
|
if err := log.Record(ctx, Event{
|
||||||
|
TenantSlug: tenantSlug,
|
||||||
|
Actor: "alice",
|
||||||
|
Action: "test.event",
|
||||||
|
Target: "x",
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("record: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := pool.Exec(ctx, `UPDATE audit_events SET actor = 'mallory' WHERE tenant_slug = $1`, tenantSlug)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("erwartet fehler bei UPDATE auf audit_events, habe nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = pool.Exec(ctx, `DELETE FROM audit_events WHERE tenant_slug = $1`, tenantSlug)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("erwartet fehler bei DELETE auf audit_events, habe nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := log.CountByTenant(ctx, tenantSlug)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("count: %v", err)
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
t.Fatalf("eintrag haette trotz fehlgeschlagener update/delete-versuche erhalten bleiben muessen, count=%d", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
// Package audit implementiert Core AUD-01: das zentrale, vom allgemeinen
|
||||||
|
// Anwendungs-Log getrennte Audit-Datenmodell fuer sicherheits- und
|
||||||
|
// compliancerelevante Ereignisse (wer, was, wann, an welchem Tenant).
|
||||||
|
// Unveraenderlichkeit (Append-only) ist AUD-02, Export/Filter-API ist AUD-03
|
||||||
|
// — dieses Paket liefert nur das Datenmodell und den EINEN zentralen
|
||||||
|
// Schreibpfad (Akzeptanzkriterium 3).
|
||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SystemTenant ist der reservierte Tenant-Bezug fuer mandantenuebergreifende
|
||||||
|
// Ereignisse (z.B. Superadmin-Aktionen) — es gibt bewusst KEINEN Weg, ein
|
||||||
|
// Ereignis ganz ohne Tenant-Bezug zu schreiben (Akzeptanzkriterium 2).
|
||||||
|
const SystemTenant = "system"
|
||||||
|
|
||||||
|
var ErrMissingTenant = errors.New("audit: tenant_slug darf nicht leer sein")
|
||||||
|
var ErrMissingActor = errors.New("audit: actor darf nicht leer sein")
|
||||||
|
var ErrMissingAction = errors.New("audit: action darf nicht leer sein")
|
||||||
|
|
||||||
|
// Event ist ein strukturiertes Audit-Ereignis (Akzeptanzkriterium 1: Akteur,
|
||||||
|
// Aktion, Zielobjekt, Zeitpunkt, Tenant).
|
||||||
|
type Event struct {
|
||||||
|
TenantSlug string
|
||||||
|
Actor string
|
||||||
|
Action string
|
||||||
|
Target string
|
||||||
|
Metadata map[string]any
|
||||||
|
OccurredAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log ist der EINE zentrale Schreibpfad fuer Audit-Ereignisse — es gibt
|
||||||
|
// bewusst keine zweite Schreibmoeglichkeit, damit kein Handler versehentlich
|
||||||
|
// direkt in audit_events schreibt und dabei die Validierung umgeht
|
||||||
|
// (Akzeptanzkriterium 3).
|
||||||
|
type Log struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLog(pool *pgxpool.Pool) *Log {
|
||||||
|
return &Log{pool: pool}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record persistiert genau einen Audit-Eintrag. Fehlender Tenant-Bezug wird
|
||||||
|
// bereits hier abgewiesen (klarer Fehler statt Constraint-Verletzung im
|
||||||
|
// Normalfall) — die Datenbank-CHECK-Constraint aus der Migration ist die
|
||||||
|
// zweite, unumgehbare Verteidigungslinie (Akzeptanzkriterium 2 / Pruefung 2).
|
||||||
|
func (l *Log) Record(ctx context.Context, e Event) error {
|
||||||
|
if e.TenantSlug == "" {
|
||||||
|
return ErrMissingTenant
|
||||||
|
}
|
||||||
|
if e.Actor == "" {
|
||||||
|
return ErrMissingActor
|
||||||
|
}
|
||||||
|
if e.Action == "" {
|
||||||
|
return ErrMissingAction
|
||||||
|
}
|
||||||
|
if e.Metadata == nil {
|
||||||
|
e.Metadata = map[string]any{}
|
||||||
|
}
|
||||||
|
metadataJSON, err := json.Marshal(e.Metadata)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("metadaten serialisieren: %w", err)
|
||||||
|
}
|
||||||
|
if e.OccurredAt.IsZero() {
|
||||||
|
e.OccurredAt = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = l.pool.Exec(ctx, `
|
||||||
|
INSERT INTO audit_events (occurred_at, tenant_slug, actor, action, target, metadata)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
`, e.OccurredAt, e.TenantSlug, e.Actor, e.Action, e.Target, metadataJSON)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("audit-ereignis schreiben: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountByTenant ist eine schlanke Lesehilfe fuer Tests/Diagnose — die
|
||||||
|
// eigentliche Filter-/Export-API ist AUD-03, hier bewusst nicht vorgezogen.
|
||||||
|
func (l *Log) CountByTenant(ctx context.Context, tenantSlug string) (int, error) {
|
||||||
|
var n int
|
||||||
|
if err := l.pool.QueryRow(ctx, `
|
||||||
|
SELECT count(*) FROM audit_events WHERE tenant_slug = $1
|
||||||
|
`, tenantSlug).Scan(&n); err != nil {
|
||||||
|
return 0, fmt.Errorf("audit-ereignisse zaehlen: %w", err)
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupAuditTest(t *testing.T) (*Log, *pgxpool.Pool, func()) {
|
||||||
|
t.Helper()
|
||||||
|
adminDSN := os.Getenv("TEST_ADMIN_DSN")
|
||||||
|
if adminDSN == "" {
|
||||||
|
t.Skip("TEST_ADMIN_DSN nicht gesetzt, Integrationstest uebersprungen")
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, adminDSN)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("pool: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := pool.Exec(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS audit_events (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
tenant_slug TEXT NOT NULL CHECK (tenant_slug <> ''),
|
||||||
|
actor TEXT NOT NULL CHECK (actor <> ''),
|
||||||
|
action TEXT NOT NULL CHECK (action <> ''),
|
||||||
|
target TEXT NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb
|
||||||
|
)`); err != nil {
|
||||||
|
t.Fatalf("schema: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup := func() {
|
||||||
|
_, _ = pool.Exec(ctx, `DELETE FROM audit_events WHERE tenant_slug LIKE 'test\_%' ESCAPE '\' OR tenant_slug = $1`, SystemTenant)
|
||||||
|
pool.Close()
|
||||||
|
}
|
||||||
|
return NewLog(pool), pool, cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 1 + Pruefung 1: ein sicherheitsrelevanter Vorgang
|
||||||
|
// (hier: fehlgeschlagener Login) erzeugt zuverlaessig genau einen Eintrag.
|
||||||
|
func TestRecord_PersistsExactlyOneEventPerSecurityIncident(t *testing.T) {
|
||||||
|
log, pool, cleanup := setupAuditTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
// Seit AUD-02 ist audit_events append-only — Zeilen koennen nie wieder
|
||||||
|
// geloescht werden (auch nicht vom Test-Cleanup). Eindeutiger Slug pro
|
||||||
|
// Lauf, damit wiederholte Testlaeufe die Zaehlung nicht verfaelschen.
|
||||||
|
tenantSlug := "test_acme_" + fmt.Sprint(time.Now().UnixNano())
|
||||||
|
|
||||||
|
err := log.Record(ctx, Event{
|
||||||
|
TenantSlug: tenantSlug,
|
||||||
|
Actor: "alice@example.com",
|
||||||
|
Action: "iam.login_failed",
|
||||||
|
Target: "user:alice@example.com",
|
||||||
|
Metadata: map[string]any{"reason": "falsches passwort"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("record: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := log.CountByTenant(ctx, tenantSlug)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("count: %v", err)
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
t.Fatalf("erwartet genau 1 audit-eintrag, habe %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
var actor, action, target string
|
||||||
|
if err := pool.QueryRow(ctx, `
|
||||||
|
SELECT actor, action, target FROM audit_events WHERE tenant_slug = $1
|
||||||
|
`, tenantSlug).Scan(&actor, &action, &target); err != nil {
|
||||||
|
t.Fatalf("eintrag lesen: %v", err)
|
||||||
|
}
|
||||||
|
if actor != "alice@example.com" || action != "iam.login_failed" || target != "user:alice@example.com" {
|
||||||
|
t.Fatalf("eintrag unerwartet: actor=%q action=%q target=%q", actor, action, target)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 2 + Pruefung 2 (App-Ebene): fehlender Tenant-Bezug wird
|
||||||
|
// bereits vom zentralen Schreibpfad abgewiesen.
|
||||||
|
func TestRecord_RejectsMissingTenant(t *testing.T) {
|
||||||
|
log, _, cleanup := setupAuditTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
err := log.Record(ctx, Event{TenantSlug: "", Actor: "alice", Action: "irgendwas"})
|
||||||
|
if !errors.Is(err, ErrMissingTenant) {
|
||||||
|
t.Fatalf("erwartet ErrMissingTenant, habe %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 2 + Pruefung 2 (DB-Ebene): selbst ein direkter INSERT,
|
||||||
|
// der Log.Record umgeht, wird durch die CHECK-Constraint verhindert — der
|
||||||
|
// Schutz haengt nicht allein von der Go-Validierung ab.
|
||||||
|
func TestConstraint_RejectsMissingTenantAtDatabaseLevel(t *testing.T) {
|
||||||
|
_, pool, cleanup := setupAuditTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
_, err := pool.Exec(ctx, `
|
||||||
|
INSERT INTO audit_events (tenant_slug, actor, action, target)
|
||||||
|
VALUES ('', 'alice', 'irgendwas', 'ziel')
|
||||||
|
`)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("erwartet fehler durch CHECK-constraint bei leerem tenant_slug, habe nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRecord_RejectsMissingActorAndAction(t *testing.T) {
|
||||||
|
log, _, cleanup := setupAuditTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if err := log.Record(ctx, Event{TenantSlug: "test_acme", Actor: "", Action: "x"}); !errors.Is(err, ErrMissingActor) {
|
||||||
|
t.Fatalf("erwartet ErrMissingActor, habe %v", err)
|
||||||
|
}
|
||||||
|
if err := log.Record(ctx, Event{TenantSlug: "test_acme", Actor: "alice", Action: ""}); !errors.Is(err, ErrMissingAction) {
|
||||||
|
t.Fatalf("erwartet ErrMissingAction, habe %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRecord_SystemTenantForCrossTenantEvents(t *testing.T) {
|
||||||
|
log, _, cleanup := setupAuditTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
if err := log.Record(ctx, Event{TenantSlug: SystemTenant, Actor: "superadmin", Action: "tenant.provisioned", Target: "tenant:acme"}); err != nil {
|
||||||
|
t.Fatalf("record mit SystemTenant: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrConfirmationNotFound = errors.New("audit: bestaetigungsvorgang nicht gefunden")
|
||||||
|
ErrAlreadyDecided = errors.New("audit: bestaetigungsvorgang wurde bereits entschieden")
|
||||||
|
ErrSameActor = errors.New("audit: bestaetigung muss von einer anderen person als der anfordernden erfolgen")
|
||||||
|
ErrInvalidCode = errors.New("audit: bestaetigungscode ungueltig")
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfirmationStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusPending ConfirmationStatus = "pending"
|
||||||
|
StatusConfirmed ConfirmationStatus = "confirmed"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FourEyes implementiert das Vier-Augen-Prinzip fuer sicherheitskritische
|
||||||
|
// Entscheidungen (Akzeptanzkriterium 2) nach dem archivdms-Vorbild:
|
||||||
|
// FOR-UPDATE-Lock gegen Race-Bedingungen bei paralleler Bestaetigung,
|
||||||
|
// Timing-safe Vergleich des Bestaetigungscodes (Akzeptanzkriterium 3).
|
||||||
|
type FourEyes struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFourEyes(pool *pgxpool.Pool) *FourEyes {
|
||||||
|
return &FourEyes{pool: pool}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request legt einen neuen, zu bestaetigenden Vorgang an (z.B. Loeschbestaetigung,
|
||||||
|
// Rechtevergabe) und liefert einen einmaligen Klartext-Code, der ausserhalb
|
||||||
|
// dieses Systems (z.B. per E-Mail) an eine ZWEITE Person uebermittelt wird —
|
||||||
|
// niemals der anfordernden Person selbst.
|
||||||
|
func (f *FourEyes) Request(ctx context.Context, action, target, requestedBy string) (id, code string, err error) {
|
||||||
|
code, err = generateCode()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("bestaetigungscode erzeugen: %w", err)
|
||||||
|
}
|
||||||
|
hash := hashCode(code)
|
||||||
|
|
||||||
|
err = f.pool.QueryRow(ctx, `
|
||||||
|
INSERT INTO security_confirmations (action, target, requested_by, code_hash, status)
|
||||||
|
VALUES ($1, $2, $3, $4, 'pending')
|
||||||
|
RETURNING id
|
||||||
|
`, action, target, requestedBy, hash).Scan(&id)
|
||||||
|
if err != nil {
|
||||||
|
return "", "", fmt.Errorf("bestaetigungsvorgang anlegen: %w", err)
|
||||||
|
}
|
||||||
|
return id, code, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm bestaetigt einen Vorgang. confirmedBy MUSS sich von der
|
||||||
|
// anfordernden Person unterscheiden (echtes Vier-Augen-Prinzip). Der Zugriff
|
||||||
|
// auf die Zeile erfolgt mit FOR UPDATE, damit zwei gleichzeitige
|
||||||
|
// Bestaetigungsversuche serialisiert werden und niemals beide durchgehen
|
||||||
|
// (Akzeptanzkriterium 2 / Pruefung 2).
|
||||||
|
func (f *FourEyes) Confirm(ctx context.Context, id, confirmedBy, code string) error {
|
||||||
|
tx, err := f.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("transaktion starten: %w", err)
|
||||||
|
}
|
||||||
|
defer func() { _ = tx.Rollback(ctx) }()
|
||||||
|
|
||||||
|
var requestedBy, status string
|
||||||
|
var codeHash []byte
|
||||||
|
err = tx.QueryRow(ctx, `
|
||||||
|
SELECT requested_by, status, code_hash FROM security_confirmations
|
||||||
|
WHERE id = $1 FOR UPDATE
|
||||||
|
`, id).Scan(&requestedBy, &status, &codeHash)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return ErrConfirmationNotFound
|
||||||
|
}
|
||||||
|
return fmt.Errorf("bestaetigungsvorgang lesen: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if status != string(StatusPending) {
|
||||||
|
return ErrAlreadyDecided
|
||||||
|
}
|
||||||
|
if confirmedBy == requestedBy {
|
||||||
|
return ErrSameActor
|
||||||
|
}
|
||||||
|
if !timingSafeEqual(hashCode(code), codeHash) {
|
||||||
|
return ErrInvalidCode
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := tx.Exec(ctx, `
|
||||||
|
UPDATE security_confirmations
|
||||||
|
SET status = 'confirmed', confirmed_by = $2, confirmed_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
`, id, confirmedBy); err != nil {
|
||||||
|
return fmt.Errorf("bestaetigung speichern: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateCode() (string, error) {
|
||||||
|
buf := make([]byte, 16)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashCode(code string) []byte {
|
||||||
|
sum := sha256.Sum256([]byte(code))
|
||||||
|
return sum[:]
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupFourEyesTest(t *testing.T) (*FourEyes, func()) {
|
||||||
|
t.Helper()
|
||||||
|
adminDSN := os.Getenv("TEST_ADMIN_DSN")
|
||||||
|
if adminDSN == "" {
|
||||||
|
t.Skip("TEST_ADMIN_DSN nicht gesetzt, Integrationstest uebersprungen")
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
pool, err := pgxpool.New(ctx, adminDSN)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("pool: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := pool.Exec(ctx, `
|
||||||
|
CREATE TABLE IF NOT EXISTS security_confirmations (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
action TEXT NOT NULL,
|
||||||
|
target TEXT NOT NULL,
|
||||||
|
requested_by TEXT NOT NULL,
|
||||||
|
code_hash BYTEA NOT NULL,
|
||||||
|
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'rejected')),
|
||||||
|
confirmed_by TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
confirmed_at TIMESTAMPTZ
|
||||||
|
)`); err != nil {
|
||||||
|
t.Fatalf("schema: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup := func() {
|
||||||
|
_, _ = pool.Exec(ctx, `DELETE FROM security_confirmations WHERE action LIKE 'test.%'`)
|
||||||
|
pool.Close()
|
||||||
|
}
|
||||||
|
return NewFourEyes(pool), cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFourEyes_RequestAndConfirm(t *testing.T) {
|
||||||
|
fe, cleanup := setupFourEyesTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, code, err := fe.Request(ctx, "test.tenant_delete", "tenant:acme", "alice@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fe.Confirm(ctx, id, "bob@example.com", code); err != nil {
|
||||||
|
t.Fatalf("confirm: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFourEyes_RejectsSameActor(t *testing.T) {
|
||||||
|
fe, cleanup := setupFourEyesTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, code, err := fe.Request(ctx, "test.tenant_delete", "tenant:acme", "alice@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fe.Confirm(ctx, id, "alice@example.com", code); !errors.Is(err, ErrSameActor) {
|
||||||
|
t.Fatalf("erwartet ErrSameActor, habe %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFourEyes_RejectsWrongCode(t *testing.T) {
|
||||||
|
fe, cleanup := setupFourEyesTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, _, err := fe.Request(ctx, "test.tenant_delete", "tenant:acme", "alice@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fe.Confirm(ctx, id, "bob@example.com", "falscher-code"); !errors.Is(err, ErrInvalidCode) {
|
||||||
|
t.Fatalf("erwartet ErrInvalidCode, habe %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 2 + Pruefung 2: FOR-UPDATE-Lock unter parallelen
|
||||||
|
// Anfragen race-frei — von zwei gleichzeitigen Bestaetigungsversuchen fuer
|
||||||
|
// denselben Vorgang darf genau einer durchgehen.
|
||||||
|
func TestFourEyes_ConcurrentConfirmIsRaceFree(t *testing.T) {
|
||||||
|
fe, cleanup := setupFourEyesTest(t)
|
||||||
|
defer cleanup()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
id, code, err := fe.Request(ctx, "test.tenant_delete", "tenant:acme", "alice@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
results := make([]error, 2)
|
||||||
|
confirmers := []string{"bob@example.com", "carol@example.com"}
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
defer wg.Done()
|
||||||
|
results[i] = fe.Confirm(ctx, id, confirmers[i], code)
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
successCount := 0
|
||||||
|
for _, err := range results {
|
||||||
|
if err == nil {
|
||||||
|
successCount++
|
||||||
|
} else if !errors.Is(err, ErrAlreadyDecided) {
|
||||||
|
t.Fatalf("unerwarteter fehler: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if successCount != 1 {
|
||||||
|
t.Fatalf("erwartet genau eine erfolgreiche bestaetigung, habe %d", successCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import "crypto/subtle"
|
||||||
|
|
||||||
|
// timingSafeEqual ist die projektweite Referenzimplementierung fuer
|
||||||
|
// Timing-safe-Vergleiche sicherheitsrelevanter Geheimnisse (Bestaetigungs-
|
||||||
|
// codes hier, spaeter Freigabelinks in Archive CMP-06 — siehe IAM-02-Ticket-
|
||||||
|
// Konvention). subtle.ConstantTimeCompare vergleicht in konstanter Zeit
|
||||||
|
// bezogen auf die Laenge von a, unabhaengig vom Inhalt.
|
||||||
|
func timingSafeEqual(a, b []byte) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return subtle.ConstantTimeCompare(a, b) == 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package audit
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTimingSafeEqual_Correctness(t *testing.T) {
|
||||||
|
a := hashCode("geheimnis-a")
|
||||||
|
b := hashCode("geheimnis-a")
|
||||||
|
c := hashCode("geheimnis-b")
|
||||||
|
|
||||||
|
if !timingSafeEqual(a, b) {
|
||||||
|
t.Fatal("identische hashes sollten gleich sein")
|
||||||
|
}
|
||||||
|
if timingSafeEqual(a, c) {
|
||||||
|
t.Fatal("unterschiedliche hashes sollten ungleich sein")
|
||||||
|
}
|
||||||
|
if timingSafeEqual(a, []byte("kuerzer")) {
|
||||||
|
t.Fatal("unterschiedliche laenge sollte ungleich sein")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Akzeptanzkriterium 3 + Pruefung 3: Timing-safe Vergleich stichprobenartig
|
||||||
|
// per Laufzeitmessung verifiziert — ein Mismatch am Anfang darf nicht
|
||||||
|
// messbar schneller sein als ein Mismatch am Ende (klassisches Merkmal
|
||||||
|
// eines NICHT timing-safen Vergleichs wie bytes.Equal mit Short-Circuit).
|
||||||
|
func TestTimingSafeEqual_NoEarlyExitTiming(t *testing.T) {
|
||||||
|
reference := hashCode("referenzwert-fuer-timing-test")
|
||||||
|
|
||||||
|
mismatchAtStart := make([]byte, len(reference))
|
||||||
|
copy(mismatchAtStart, reference)
|
||||||
|
mismatchAtStart[0] ^= 0xFF
|
||||||
|
|
||||||
|
mismatchAtEnd := make([]byte, len(reference))
|
||||||
|
copy(mismatchAtEnd, reference)
|
||||||
|
mismatchAtEnd[len(mismatchAtEnd)-1] ^= 0xFF
|
||||||
|
|
||||||
|
const iterations = 20000
|
||||||
|
startDur := measure(iterations, func() { timingSafeEqual(reference, mismatchAtStart) })
|
||||||
|
endDur := measure(iterations, func() { timingSafeEqual(reference, mismatchAtEnd) })
|
||||||
|
|
||||||
|
t.Logf("mismatch am anfang: %s, mismatch am ende: %s (%d iterationen)", startDur, endDur, iterations)
|
||||||
|
|
||||||
|
ratio := float64(startDur) / float64(endDur)
|
||||||
|
// Grosszuegige Toleranz (Faktor 3), da es ein Stichprobentest auf einer
|
||||||
|
// geteilten Testmaschine ist, kein isolierter Benchmark — es geht darum,
|
||||||
|
// eine grobe Short-Circuit-Implementierung zuverlaessig aufzudecken
|
||||||
|
// (die haette typischerweise eine Groessenordnung Unterschied), nicht um
|
||||||
|
// kryptographisch praezise Constant-Time-Beweise.
|
||||||
|
if ratio > 3.0 || ratio < 1.0/3.0 {
|
||||||
|
t.Fatalf("timing-unterschied zu gross (verdacht auf short-circuit-vergleich): ratio=%.2f", ratio)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func measure(iterations int, fn func()) time.Duration {
|
||||||
|
start := time.Now()
|
||||||
|
for i := 0; i < iterations; i++ {
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
return time.Since(start)
|
||||||
|
}
|
||||||
@@ -1,206 +0,0 @@
|
|||||||
package tenant
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrTenantNotFound = errors.New("tenant: nicht gefunden")
|
|
||||||
ErrInvalidTransition = errors.New("tenant: ungueltiger zustandsuebergang")
|
|
||||||
// ErrTenantNotActive wird von Lifecycle.CheckActive verwendet — bewusst
|
|
||||||
// EIN Fehler fuer suspendiert/zur-Loeschung-vorgemerkt/geloescht, da der
|
|
||||||
// Aufrufer (z.B. Login) nur wissen muss "kein Zugriff", nicht welcher der
|
|
||||||
// Nicht-aktiv-Zustaende genau vorliegt.
|
|
||||||
ErrTenantNotActive = errors.New("tenant: nicht aktiv")
|
|
||||||
)
|
|
||||||
|
|
||||||
func scanTenantWithLifecycle(row pgx.Row) (Tenant, error) {
|
|
||||||
var t Tenant
|
|
||||||
if err := row.Scan(&t.ID, &t.Slug, &t.Name, &t.DBName, &t.DBDSN, &t.Status,
|
|
||||||
&t.CreatedAt, &t.PreviousStatus, &t.DeletionScheduledAt); err != nil {
|
|
||||||
return Tenant{}, err
|
|
||||||
}
|
|
||||||
return t, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// transition fuehrt einen bewachten Zustandsuebergang aus: das UPDATE greift
|
|
||||||
// nur, wenn der aktuelle Status einer von allowedFrom ist (atomarer
|
|
||||||
// Check-and-Set, kein Race zwischen Lesen und Schreiben). Greift es nicht,
|
|
||||||
// wird zwischen "Tenant existiert nicht" und "Uebergang nicht erlaubt"
|
|
||||||
// unterschieden, damit AC1 ("ungueltige Uebergaenge werden abgewiesen") einen
|
|
||||||
// sprechenden Fehler liefert statt eines stillen No-Ops.
|
|
||||||
func (r *Registry) transition(ctx context.Context, slug string, allowedFrom []Status, to Status, previousStatus *string, deletionAt *time.Time) (Tenant, error) {
|
|
||||||
from := make([]string, len(allowedFrom))
|
|
||||||
for i, s := range allowedFrom {
|
|
||||||
from[i] = string(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
row := r.pool.QueryRow(ctx, `
|
|
||||||
UPDATE tenants
|
|
||||||
SET status = $2, previous_status = $3, deletion_scheduled_at = $4
|
|
||||||
WHERE slug = $1 AND status = ANY($5)
|
|
||||||
RETURNING id, slug, name, db_name, db_dsn, status, created_at, previous_status, deletion_scheduled_at
|
|
||||||
`, slug, string(to), previousStatus, deletionAt, from)
|
|
||||||
|
|
||||||
t, err := scanTenantWithLifecycle(row)
|
|
||||||
if err == nil {
|
|
||||||
return t, nil
|
|
||||||
}
|
|
||||||
if !errors.Is(err, pgx.ErrNoRows) {
|
|
||||||
return Tenant{}, fmt.Errorf("zustandsuebergang: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
existing, getErr := r.GetBySlug(ctx, slug)
|
|
||||||
if getErr != nil {
|
|
||||||
return Tenant{}, ErrTenantNotFound
|
|
||||||
}
|
|
||||||
return Tenant{}, fmt.Errorf("%w: von %q nach %q (aktuell: %q)", ErrInvalidTransition, allowedFrom, to, existing.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Suspend haelt die Daten des Mandanten unveraendert, sperrt aber den Zugriff
|
|
||||||
// (Akzeptanzkriterium 1) — es findet keine Loeschung/Migration statt.
|
|
||||||
func (r *Registry) Suspend(ctx context.Context, slug string) (Tenant, error) {
|
|
||||||
return r.transition(ctx, slug, []Status{StatusActive}, StatusSuspended, nil, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reactivate stellt den Zustand vor der Suspendierung vollstaendig wieder her
|
|
||||||
// (Akzeptanzkriterium 2) — da Suspend keine weiteren Daten veraendert, genuegt
|
|
||||||
// die Rueckkehr nach StatusActive.
|
|
||||||
func (r *Registry) Reactivate(ctx context.Context, slug string) (Tenant, error) {
|
|
||||||
return r.transition(ctx, slug, []Status{StatusSuspended}, StatusActive, nil, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ScheduleDeletion merkt den Mandanten zur Loeschung vor und startet die
|
|
||||||
// Karenzzeit (Akzeptanzkriterium 3). previous_status wird festgehalten, damit
|
|
||||||
// CancelDeletion exakt dorthin zurueckkehren kann (aktiv ODER suspendiert).
|
|
||||||
func (r *Registry) ScheduleDeletion(ctx context.Context, slug string, grace time.Duration) (Tenant, error) {
|
|
||||||
existing, err := r.GetBySlug(ctx, slug)
|
|
||||||
if err != nil {
|
|
||||||
return Tenant{}, ErrTenantNotFound
|
|
||||||
}
|
|
||||||
prev := string(existing.Status)
|
|
||||||
deletionAt := time.Now().Add(grace)
|
|
||||||
return r.transition(ctx, slug, []Status{StatusActive, StatusSuspended}, StatusPendingDeletion, &prev, &deletionAt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CancelDeletion widerruft eine Loeschvormerkung innerhalb der Karenzzeit und
|
|
||||||
// stellt exakt den zuvor gesicherten Zustand wieder her.
|
|
||||||
func (r *Registry) CancelDeletion(ctx context.Context, slug string) (Tenant, error) {
|
|
||||||
existing, err := r.GetBySlug(ctx, slug)
|
|
||||||
if err != nil {
|
|
||||||
return Tenant{}, ErrTenantNotFound
|
|
||||||
}
|
|
||||||
if existing.Status != StatusPendingDeletion || existing.PreviousStatus == nil {
|
|
||||||
return Tenant{}, fmt.Errorf("%w: von %q nach aktiv/suspendiert (aktuell: %q)", ErrInvalidTransition, StatusPendingDeletion, existing.Status)
|
|
||||||
}
|
|
||||||
restoreTo := Status(*existing.PreviousStatus)
|
|
||||||
return r.transition(ctx, slug, []Status{StatusPendingDeletion}, restoreTo, nil, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lifecycle fuehrt die tatsaechliche, physische Loeschung nach Ablauf der
|
|
||||||
// Karenzzeit aus (Datenbank-Drop) und stellt die Zugriffsschutz-Pruefung
|
|
||||||
// bereit. Getrennt von Registry, weil hierfuer zusaetzlich der adminPool
|
|
||||||
// (fuer DROP DATABASE) noetig ist, siehe internal/tenant.Provisioner.
|
|
||||||
type Lifecycle struct {
|
|
||||||
registry *Registry
|
|
||||||
adminPool *pgxpool.Pool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLifecycle(registry *Registry, adminPool *pgxpool.Pool) *Lifecycle {
|
|
||||||
return &Lifecycle{registry: registry, adminPool: adminPool}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckActive verweigert Zugriff fuer jeden Nicht-aktiv-Zustand und loggt den
|
|
||||||
// Vorgang strukturiert (Akzeptanzkriterium 1 / Pruefung 2).
|
|
||||||
func (l *Lifecycle) CheckActive(ctx context.Context, slug string) error {
|
|
||||||
t, err := l.registry.GetBySlug(ctx, slug)
|
|
||||||
if err != nil {
|
|
||||||
return ErrTenantNotFound
|
|
||||||
}
|
|
||||||
if t.Status != StatusActive {
|
|
||||||
slog.Warn("zugriff auf nicht-aktiven mandanten verweigert",
|
|
||||||
"tenant_slug", slug, "tenant_status", t.Status)
|
|
||||||
return ErrTenantNotActive
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProcessDueDeletions loescht alle Mandanten-Datenbanken, deren Karenzzeit
|
|
||||||
// abgelaufen ist (Akzeptanzkriterium 3 / Pruefung 3). FOR UPDATE SKIP LOCKED
|
|
||||||
// folgt der projektweiten Postgres-Jobqueue-Konvention (siehe
|
|
||||||
// SKALIERUNGSKONZEPT.md) und macht die Funktion sicher fuer mehrere parallel
|
|
||||||
// laufende Core-Instanzen.
|
|
||||||
func (l *Lifecycle) ProcessDueDeletions(ctx context.Context) (int, error) {
|
|
||||||
tx, err := l.registry.pool.Begin(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("sweep-transaktion starten: %w", err)
|
|
||||||
}
|
|
||||||
defer func() { _ = tx.Rollback(ctx) }()
|
|
||||||
|
|
||||||
rows, err := tx.Query(ctx, `
|
|
||||||
SELECT id, db_name FROM tenants
|
|
||||||
WHERE status = $1 AND deletion_scheduled_at <= now()
|
|
||||||
FOR UPDATE SKIP LOCKED
|
|
||||||
`, string(StatusPendingDeletion))
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("faellige loeschungen abfragen: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
type due struct{ id, dbName string }
|
|
||||||
var candidates []due
|
|
||||||
for rows.Next() {
|
|
||||||
var d due
|
|
||||||
if err := rows.Scan(&d.id, &d.dbName); err != nil {
|
|
||||||
rows.Close()
|
|
||||||
return 0, fmt.Errorf("faellige loeschung lesen: %w", err)
|
|
||||||
}
|
|
||||||
candidates = append(candidates, d)
|
|
||||||
}
|
|
||||||
rows.Close()
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
processed := 0
|
|
||||||
for _, c := range candidates {
|
|
||||||
if _, err := l.adminPool.Exec(ctx, fmt.Sprintf(`DROP DATABASE IF EXISTS %q`, c.dbName)); err != nil {
|
|
||||||
return processed, fmt.Errorf("tenant-datenbank %q loeschen: %w", c.dbName, err)
|
|
||||||
}
|
|
||||||
if _, err := tx.Exec(ctx, `
|
|
||||||
UPDATE tenants SET status = $2, previous_status = NULL, deletion_scheduled_at = NULL
|
|
||||||
WHERE id = $1
|
|
||||||
`, c.id, string(StatusDeleted)); err != nil {
|
|
||||||
return processed, fmt.Errorf("tenant %q als geloescht markieren: %w", c.id, err)
|
|
||||||
}
|
|
||||||
processed++
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := tx.Commit(ctx); err != nil {
|
|
||||||
return 0, fmt.Errorf("sweep-transaktion committen: %w", err)
|
|
||||||
}
|
|
||||||
return processed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunSweeper triggert ProcessDueDeletions periodisch, bis ctx beendet wird —
|
|
||||||
// die "In-Prozess-Worker-Goroutine" aus der projektweiten Jobqueue-Konvention.
|
|
||||||
func (l *Lifecycle) RunSweeper(ctx context.Context, interval time.Duration) {
|
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
defer ticker.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
if _, err := l.ProcessDueDeletions(ctx); err != nil {
|
|
||||||
slog.Error("tenant-loeschung-sweep fehlgeschlagen", "error", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,253 +0,0 @@
|
|||||||
package tenant
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
|
||||||
)
|
|
||||||
|
|
||||||
func newLifecycleTestSetup(t *testing.T) (*Registry, *Lifecycle, *pgxpool.Pool, func()) {
|
|
||||||
t.Helper()
|
|
||||||
adminDSN := os.Getenv("TEST_ADMIN_DSN")
|
|
||||||
if adminDSN == "" {
|
|
||||||
t.Skip("TEST_ADMIN_DSN nicht gesetzt, Integrationstest uebersprungen")
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
adminPool, err := pgxpool.New(ctx, adminDSN)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("admin pool: %v", err)
|
|
||||||
}
|
|
||||||
registryPool, err := pgxpool.New(ctx, adminDSN)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("registry pool: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := registryPool.Exec(ctx, `
|
|
||||||
CREATE TABLE IF NOT EXISTS tenants (
|
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
||||||
slug TEXT NOT NULL UNIQUE,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
db_name TEXT NOT NULL UNIQUE,
|
|
||||||
db_dsn TEXT NOT NULL,
|
|
||||||
status TEXT NOT NULL DEFAULT 'active',
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
previous_status TEXT,
|
|
||||||
deletion_scheduled_at TIMESTAMPTZ
|
|
||||||
)`); err != nil {
|
|
||||||
t.Fatalf("registry-schema: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
registry := NewRegistry(registryPool)
|
|
||||||
dsnTemplate := strings.Replace(adminDSN, "/postgres?", "/%s?", 1)
|
|
||||||
provisioner := NewProvisioner(adminPool, registry, dsnTemplate)
|
|
||||||
lifecycle := NewLifecycle(registry, adminPool)
|
|
||||||
|
|
||||||
cleanup := func() {
|
|
||||||
registryPool.Close()
|
|
||||||
adminPool.Close()
|
|
||||||
}
|
|
||||||
_ = provisioner
|
|
||||||
return registry, lifecycle, adminPool, cleanup
|
|
||||||
}
|
|
||||||
|
|
||||||
func provisionTestTenant(t *testing.T, registry *Registry, adminPool *pgxpool.Pool, slug string) {
|
|
||||||
t.Helper()
|
|
||||||
dsnTemplate := strings.Replace(os.Getenv("TEST_ADMIN_DSN"), "/postgres?", "/%s?", 1)
|
|
||||||
provisioner := NewProvisioner(adminPool, registry, dsnTemplate)
|
|
||||||
if _, err := provisioner.Provision(context.Background(), slug, slug); err != nil {
|
|
||||||
t.Fatalf("provision %s: %v", slug, err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
ctx := context.Background()
|
|
||||||
_, _ = adminPool.Exec(ctx, fmt.Sprintf(`DROP DATABASE IF EXISTS %q`, dbNameForSlug(slug)))
|
|
||||||
_, _ = registry.pool.Exec(ctx, `DELETE FROM tenants WHERE slug = $1`, slug)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Akzeptanzkriterium 1 (Suspend) + 2 (Reactivate) + Pruefung 1 (Uebergaenge).
|
|
||||||
func TestLifecycle_SuspendAndReactivate(t *testing.T) {
|
|
||||||
registry, _, adminPool, cleanup := newLifecycleTestSetup(t)
|
|
||||||
defer cleanup()
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_suspend")
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
suspended, err := registry.Suspend(ctx, "lc_suspend")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("suspend: %v", err)
|
|
||||||
}
|
|
||||||
if suspended.Status != StatusSuspended {
|
|
||||||
t.Fatalf("status = %q, want suspended", suspended.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
reactivated, err := registry.Reactivate(ctx, "lc_suspend")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("reactivate: %v", err)
|
|
||||||
}
|
|
||||||
if reactivated.Status != StatusActive {
|
|
||||||
t.Fatalf("status = %q, want active", reactivated.Status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pruefung 1: ungueltige Uebergaenge werden abgewiesen.
|
|
||||||
func TestLifecycle_RejectsInvalidTransitions(t *testing.T) {
|
|
||||||
registry, _, adminPool, cleanup := newLifecycleTestSetup(t)
|
|
||||||
defer cleanup()
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_invalid")
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Reactivate auf einem bereits aktiven Tenant ist kein gueltiger Uebergang.
|
|
||||||
if _, err := registry.Reactivate(ctx, "lc_invalid"); !errors.Is(err, ErrInvalidTransition) {
|
|
||||||
t.Fatalf("erwartet ErrInvalidTransition, habe %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := registry.Suspend(ctx, "lc_invalid"); err != nil {
|
|
||||||
t.Fatalf("suspend: %v", err)
|
|
||||||
}
|
|
||||||
// Suspend auf einem bereits suspendierten Tenant ist ebenfalls ungueltig.
|
|
||||||
if _, err := registry.Suspend(ctx, "lc_invalid"); !errors.Is(err, ErrInvalidTransition) {
|
|
||||||
t.Fatalf("erwartet ErrInvalidTransition, habe %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CancelDeletion ohne vorherige Loeschvormerkung ist ungueltig.
|
|
||||||
if _, err := registry.CancelDeletion(ctx, "lc_invalid"); !errors.Is(err, ErrInvalidTransition) {
|
|
||||||
t.Fatalf("erwartet ErrInvalidTransition, habe %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := registry.Suspend(ctx, "unbekannter-slug-xyz"); !errors.Is(err, ErrTenantNotFound) {
|
|
||||||
t.Fatalf("erwartet ErrTenantNotFound, habe %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Akzeptanzkriterium 3: Loeschung zweistufig mit Karenzzeit, innerhalb der
|
|
||||||
// Frist widerrufbar — sowohl aus 'active' als auch aus 'suspended' heraus,
|
|
||||||
// mit exakter Wiederherstellung des jeweiligen Vorzustands.
|
|
||||||
func TestLifecycle_ScheduleAndCancelDeletion_RestoresExactPreviousState(t *testing.T) {
|
|
||||||
registry, _, adminPool, cleanup := newLifecycleTestSetup(t)
|
|
||||||
defer cleanup()
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_cancel_active")
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_cancel_suspended")
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// Fall 1: aus 'active' heraus vorgemerkt und widerrufen.
|
|
||||||
scheduled, err := registry.ScheduleDeletion(ctx, "lc_cancel_active", time.Hour)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("schedule deletion: %v", err)
|
|
||||||
}
|
|
||||||
if scheduled.Status != StatusPendingDeletion {
|
|
||||||
t.Fatalf("status = %q, want pending_deletion", scheduled.Status)
|
|
||||||
}
|
|
||||||
if scheduled.DeletionScheduledAt == nil {
|
|
||||||
t.Fatal("erwartet gesetzte deletion_scheduled_at")
|
|
||||||
}
|
|
||||||
|
|
||||||
restored, err := registry.CancelDeletion(ctx, "lc_cancel_active")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cancel deletion: %v", err)
|
|
||||||
}
|
|
||||||
if restored.Status != StatusActive {
|
|
||||||
t.Fatalf("status = %q, want active (vorheriger zustand)", restored.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall 2: aus 'suspended' heraus vorgemerkt und widerrufen — muss zu
|
|
||||||
// 'suspended' zurueckkehren, NICHT zu 'active'.
|
|
||||||
if _, err := registry.Suspend(ctx, "lc_cancel_suspended"); err != nil {
|
|
||||||
t.Fatalf("suspend: %v", err)
|
|
||||||
}
|
|
||||||
if _, err := registry.ScheduleDeletion(ctx, "lc_cancel_suspended", time.Hour); err != nil {
|
|
||||||
t.Fatalf("schedule deletion: %v", err)
|
|
||||||
}
|
|
||||||
restoredSuspended, err := registry.CancelDeletion(ctx, "lc_cancel_suspended")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("cancel deletion: %v", err)
|
|
||||||
}
|
|
||||||
if restoredSuspended.Status != StatusSuspended {
|
|
||||||
t.Fatalf("status = %q, want suspended (vorheriger zustand)", restoredSuspended.Status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Akzeptanzkriterium 1 + Pruefung 2: suspendierter Tenant erzeugt bei jedem
|
|
||||||
// Zugriffsversuch einen klaren Fehler.
|
|
||||||
func TestLifecycle_CheckActive_RejectsNonActive(t *testing.T) {
|
|
||||||
registry, lifecycle, adminPool, cleanup := newLifecycleTestSetup(t)
|
|
||||||
defer cleanup()
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_checkactive")
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
if err := lifecycle.CheckActive(ctx, "lc_checkactive"); err != nil {
|
|
||||||
t.Fatalf("aktiver tenant sollte durchgehen, habe %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := registry.Suspend(ctx, "lc_checkactive"); err != nil {
|
|
||||||
t.Fatalf("suspend: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
if err := lifecycle.CheckActive(ctx, "lc_checkactive"); !errors.Is(err, ErrTenantNotActive) {
|
|
||||||
t.Fatalf("versuch %d: erwartet ErrTenantNotActive, habe %v", i, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := lifecycle.CheckActive(ctx, "nie-registriert"); !errors.Is(err, ErrTenantNotFound) {
|
|
||||||
t.Fatalf("erwartet ErrTenantNotFound, habe %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Akzeptanzkriterium 3 + Pruefung 3: Loeschvorgang nach Ablauf der Karenzzeit
|
|
||||||
// automatisch ausgeloest (hier durch direkten Aufruf von ProcessDueDeletions,
|
|
||||||
// das RunSweeper periodisch aufruft).
|
|
||||||
func TestLifecycle_ProcessDueDeletions(t *testing.T) {
|
|
||||||
registry, lifecycle, adminPool, cleanup := newLifecycleTestSetup(t)
|
|
||||||
defer cleanup()
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_due")
|
|
||||||
provisionTestTenant(t, registry, adminPool, "lc_not_due")
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
// lc_due: Karenzzeit liegt bereits in der Vergangenheit -> faellig.
|
|
||||||
if _, err := registry.ScheduleDeletion(ctx, "lc_due", -time.Minute); err != nil {
|
|
||||||
t.Fatalf("schedule deletion (due): %v", err)
|
|
||||||
}
|
|
||||||
// lc_not_due: Karenzzeit liegt weit in der Zukunft -> nicht faellig.
|
|
||||||
if _, err := registry.ScheduleDeletion(ctx, "lc_not_due", time.Hour); err != nil {
|
|
||||||
t.Fatalf("schedule deletion (not due): %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
processed, err := lifecycle.ProcessDueDeletions(ctx)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("process due deletions: %v", err)
|
|
||||||
}
|
|
||||||
if processed != 1 {
|
|
||||||
t.Fatalf("erwartet genau 1 verarbeitete loeschung, habe %d", processed)
|
|
||||||
}
|
|
||||||
|
|
||||||
due, err := registry.GetBySlug(ctx, "lc_due")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("get lc_due: %v", err)
|
|
||||||
}
|
|
||||||
if due.Status != StatusDeleted {
|
|
||||||
t.Fatalf("lc_due status = %q, want deleted", due.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
notDue, err := registry.GetBySlug(ctx, "lc_not_due")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("get lc_not_due: %v", err)
|
|
||||||
}
|
|
||||||
if notDue.Status != StatusPendingDeletion {
|
|
||||||
t.Fatalf("lc_not_due status = %q, want pending_deletion (noch nicht faellig)", notDue.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Datenbank von lc_due wurde tatsaechlich physisch entfernt.
|
|
||||||
var exists bool
|
|
||||||
if err := adminPool.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`,
|
|
||||||
dbNameForSlug("lc_due")).Scan(&exists); err != nil {
|
|
||||||
t.Fatalf("pg_database pruefen: %v", err)
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
t.Fatal("erwartet, dass die tenant-datenbank von lc_due geloescht wurde")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -35,17 +35,13 @@ func (r *Registry) insertTx(ctx context.Context, tx pgx.Tx, t Tenant) (Tenant, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) GetBySlug(ctx context.Context, slug string) (Tenant, error) {
|
func (r *Registry) GetBySlug(ctx context.Context, slug string) (Tenant, error) {
|
||||||
// previous_status/deletion_scheduled_at werden mitgelesen, damit TEN-04
|
|
||||||
// (internal/tenant/lifecycle.go) den vollstaendigen Lebenszyklus-Zustand
|
|
||||||
// ueber GetBySlug ansehen kann, statt eine eigene Abfrage zu duplizieren.
|
|
||||||
var t Tenant
|
var t Tenant
|
||||||
row := r.pool.QueryRow(ctx, `
|
row := r.pool.QueryRow(ctx, `
|
||||||
SELECT id, slug, name, db_name, db_dsn, status, created_at, previous_status, deletion_scheduled_at
|
SELECT id, slug, name, db_name, db_dsn, status, created_at
|
||||||
FROM tenants WHERE slug = $1
|
FROM tenants WHERE slug = $1
|
||||||
`, slug)
|
`, slug)
|
||||||
|
|
||||||
if err := row.Scan(&t.ID, &t.Slug, &t.Name, &t.DBName, &t.DBDSN, &t.Status, &t.CreatedAt,
|
if err := row.Scan(&t.ID, &t.Slug, &t.Name, &t.DBName, &t.DBDSN, &t.Status, &t.CreatedAt); err != nil {
|
||||||
&t.PreviousStatus, &t.DeletionScheduledAt); err != nil {
|
|
||||||
return Tenant{}, fmt.Errorf("tenant laden: %w", err)
|
return Tenant{}, fmt.Errorf("tenant laden: %w", err)
|
||||||
}
|
}
|
||||||
return t, nil
|
return t, nil
|
||||||
|
|||||||
@@ -12,10 +12,6 @@ type Status string
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
StatusActive Status = "active"
|
StatusActive Status = "active"
|
||||||
// Lebenszyklus-Zustaende aus TEN-04 (siehe internal/tenant/lifecycle.go).
|
|
||||||
StatusSuspended Status = "suspended"
|
|
||||||
StatusPendingDeletion Status = "pending_deletion"
|
|
||||||
StatusDeleted Status = "deleted"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tenant struct {
|
type Tenant struct {
|
||||||
@@ -26,11 +22,6 @@ type Tenant struct {
|
|||||||
DBDSN string
|
DBDSN string
|
||||||
Status Status
|
Status Status
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
// PreviousStatus und DeletionScheduledAt sind nur waehrend
|
|
||||||
// StatusPendingDeletion gesetzt (TEN-04) — sie halten fest, in welchen
|
|
||||||
// Zustand CancelDeletion zurueckkehrt und wann die Karenzzeit ablaeuft.
|
|
||||||
PreviousStatus *string
|
|
||||||
DeletionScheduledAt *time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// slugPattern erzwingt sichere, als SQL-Identifier verwendbare Slugs, damit
|
// slugPattern erzwingt sichere, als SQL-Identifier verwendbare Slugs, damit
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE tenants DROP COLUMN previous_status;
|
|
||||||
ALTER TABLE tenants DROP COLUMN deletion_scheduled_at;
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
-- Lebenszyklus-Zustaende fuer Mandanten (TEN-04, siehe core-kanban/tickets/TEN-04.md).
|
|
||||||
-- previous_status haelt den Zustand VOR einer Loeschvormerkung, damit
|
|
||||||
-- CancelDeletion "den vorherigen Zustand vollstaendig wiederherstellt"
|
|
||||||
-- (aktiv ODER suspendiert), statt hart auf 'active' zurueckzusetzen.
|
|
||||||
ALTER TABLE tenants ADD COLUMN previous_status TEXT;
|
|
||||||
ALTER TABLE tenants ADD COLUMN deletion_scheduled_at TIMESTAMPTZ;
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS audit_events;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
-- Zentrales Audit-Log-Modell (AUD-01, siehe core-kanban/tickets/AUD-01.md).
|
||||||
|
-- Getrennt vom allgemeinen Anwendungs-Log (Akzeptanzkriterium 2): eigene
|
||||||
|
-- Tabelle, eigenes Paket (internal/audit), kein Log-Framework.
|
||||||
|
-- tenant_slug ist NOT NULL + darf nicht leer sein (Akzeptanzkriterium 2 /
|
||||||
|
-- Pruefung 2) — mandantenuebergreifende Ereignisse nutzen den reservierten
|
||||||
|
-- Wert 'system', niemals NULL oder leeren String.
|
||||||
|
CREATE TABLE audit_events (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
occurred_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
tenant_slug TEXT NOT NULL CHECK (tenant_slug <> ''),
|
||||||
|
actor TEXT NOT NULL CHECK (actor <> ''),
|
||||||
|
action TEXT NOT NULL CHECK (action <> ''),
|
||||||
|
target TEXT NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX audit_events_tenant_slug_idx ON audit_events (tenant_slug, occurred_at);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
DROP TRIGGER IF EXISTS audit_events_no_delete ON audit_events;
|
||||||
|
DROP TRIGGER IF EXISTS audit_events_no_update ON audit_events;
|
||||||
|
DROP FUNCTION IF EXISTS audit_events_prevent_mutation();
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
-- Audit-Log technisch gegen Aenderung/Loeschung absichern (AUD-02, siehe
|
||||||
|
-- core-kanban/tickets/AUD-02.md). Ein Trigger statt nur GRANT/REVOKE, damit
|
||||||
|
-- der Schutz unabhaengig davon greift, mit welcher Rolle verbunden wird
|
||||||
|
-- (Akzeptanzkriterium 1: "auf Datenbankebene technisch unterbunden").
|
||||||
|
CREATE FUNCTION audit_events_prevent_mutation() RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
RAISE EXCEPTION 'audit_events ist append-only: % ist nicht erlaubt', TG_OP;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER audit_events_no_update
|
||||||
|
BEFORE UPDATE ON audit_events
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION audit_events_prevent_mutation();
|
||||||
|
|
||||||
|
CREATE TRIGGER audit_events_no_delete
|
||||||
|
BEFORE DELETE ON audit_events
|
||||||
|
FOR EACH ROW EXECUTE FUNCTION audit_events_prevent_mutation();
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
DROP TABLE IF EXISTS security_confirmations;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
-- Vier-Augen-Prinzip fuer sicherheitskritische Entscheidungen (AUD-02
|
||||||
|
-- Akzeptanzkriterium 2), Vorbild: archivdms FOR-UPDATE-Lock + Timing-safe
|
||||||
|
-- Vergleich. code_hash speichert NIEMALS den Bestaetigungscode im Klartext.
|
||||||
|
CREATE TABLE security_confirmations (
|
||||||
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
action TEXT NOT NULL,
|
||||||
|
target TEXT NOT NULL,
|
||||||
|
requested_by TEXT NOT NULL,
|
||||||
|
code_hash BYTEA NOT NULL,
|
||||||
|
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'confirmed', 'rejected')),
|
||||||
|
confirmed_by TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
confirmed_at TIMESTAMPTZ
|
||||||
|
);
|
||||||
@@ -13,7 +13,8 @@ ROLE="nexarch_test"
|
|||||||
|
|
||||||
export PGPASSWORD="$PASS"
|
export PGPASSWORD="$PASS"
|
||||||
|
|
||||||
psql -h localhost -U "$ROLE" -d postgres -v ON_ERROR_STOP=1 -c "DROP TABLE IF EXISTS tenants;"
|
psql -h localhost -U "$ROLE" -d postgres -v ON_ERROR_STOP=1 -c "DROP TABLE IF EXISTS tenants CASCADE;"
|
||||||
|
psql -h localhost -U "$ROLE" -d postgres -v ON_ERROR_STOP=1 -c "DROP TABLE IF EXISTS audit_events CASCADE;"
|
||||||
|
|
||||||
dbs=$(psql -h localhost -U "$ROLE" -d postgres -tAc "SELECT datname FROM pg_database WHERE datname LIKE 'tenant\_%' ESCAPE '\'")
|
dbs=$(psql -h localhost -U "$ROLE" -d postgres -tAc "SELECT datname FROM pg_database WHERE datname LIKE 'tenant\_%' ESCAPE '\'")
|
||||||
for db in $dbs; do
|
for db in $dbs; do
|
||||||
|
|||||||
Executable
+24
@@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Ein-Kommando-Pruefung fuer den aktuellen Code-Stand auf dem Testhost:
|
||||||
|
# Registry+Tenant-DBs zuruecksetzen, dann build/vet/test in einem Rutsch.
|
||||||
|
# -p 1 ist Pflicht, da mehrere Pakete dieselbe physische Registry-Tabelle auf
|
||||||
|
# dem Testhost teilen (siehe [[project-nexarch-test-infra]]).
|
||||||
|
#
|
||||||
|
# Aufruf: NEXARCH_TEST_DB_PASSWORD=... ./scripts/run-checks.sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
PASS="${NEXARCH_TEST_DB_PASSWORD:?Setze NEXARCH_TEST_DB_PASSWORD vor dem Aufruf}"
|
||||||
|
cd "$(dirname "$0")/.."
|
||||||
|
|
||||||
|
NEXARCH_TEST_DB_PASSWORD="$PASS" bash scripts/reset-test-env.sh
|
||||||
|
|
||||||
|
export TEST_ADMIN_DSN="postgresql://nexarch_test:${PASS}@localhost:5432/postgres?sslmode=disable"
|
||||||
|
|
||||||
|
echo "== go build =="
|
||||||
|
go build ./...
|
||||||
|
|
||||||
|
echo "== go vet =="
|
||||||
|
go vet ./...
|
||||||
|
|
||||||
|
echo "== go test (-p 1) =="
|
||||||
|
go test ./... -p 1 -count=1
|
||||||
Reference in New Issue
Block a user