|
| 1 | +package models_test |
| 2 | + |
| 3 | +// dbsafety_audit_sink_test.go — covers the *sql.DB-backed dbsafety audit sink |
| 4 | +// (truehomie-db hardening). Integration test — needs TEST_DATABASE_URL; skips |
| 5 | +// cleanly otherwise. |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "instant.dev/internal/models" |
| 17 | + "instant.dev/internal/providers/dbsafety" |
| 18 | + "instant.dev/internal/testhelpers" |
| 19 | +) |
| 20 | + |
| 21 | +// TestWireDBSafetyAuditSink_NilIsNoop asserts a nil db leaves the default |
| 22 | +// (structured-slog) sink in place rather than installing a panicking nil-db |
| 23 | +// sink. Pure — no DB needed. |
| 24 | +func TestWireDBSafetyAuditSink_NilIsNoop(t *testing.T) { |
| 25 | + dbsafety.SetAuditSink(nil) |
| 26 | + t.Cleanup(func() { dbsafety.SetAuditSink(nil) }) |
| 27 | + |
| 28 | + models.WireDBSafetyAuditSink(nil) // must NOT install a *sql.DB sink over a nil db |
| 29 | + |
| 30 | + // A guard against a dev host with valid names triggers exactly one emit; |
| 31 | + // with the default slog sink this must not panic. |
| 32 | + err := dbsafety.GuardDrop(context.Background(), dbsafety.DropParams{ |
| 33 | + Provider: "db.local", |
| 34 | + Env: dbsafety.EnvDevelopment, |
| 35 | + DSNHost: "postgres://u:p@localhost:5432/d", |
| 36 | + Token: "tok", |
| 37 | + DatabaseName: "db_tok", |
| 38 | + UserName: "usr_tok", |
| 39 | + }) |
| 40 | + require.NoError(t, err) |
| 41 | +} |
| 42 | + |
| 43 | +// TestDBSafetyAuditSink_EmitWritesRow drives the production sink end-to-end: a |
| 44 | +// sanctioned (dev-host, valid-name) GuardDrop emits a customer_db.direct_drop |
| 45 | +// audit_log row carrying the destroyed identifiers + DSN host. The emit fires |
| 46 | +// from a goroutine, so the row is polled for. |
| 47 | +func TestDBSafetyAuditSink_EmitWritesRow(t *testing.T) { |
| 48 | + db, clean := testhelpers.SetupTestDB(t) |
| 49 | + defer clean() |
| 50 | + |
| 51 | + models.WireDBSafetyAuditSink(db) |
| 52 | + t.Cleanup(func() { dbsafety.SetAuditSink(nil) }) |
| 53 | + |
| 54 | + const token = "auditseam-tok" |
| 55 | + err := dbsafety.GuardDrop(context.Background(), dbsafety.DropParams{ |
| 56 | + Provider: "db.local", |
| 57 | + Env: dbsafety.EnvDevelopment, |
| 58 | + DSNHost: "postgres://u:p@postgres-customers:5432/d", |
| 59 | + Token: token, |
| 60 | + DatabaseName: "db_" + token, |
| 61 | + UserName: "usr_" + token, |
| 62 | + }) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // Poll for the audit row (team_id is NULL — admin-only rows — so query the |
| 66 | + // kind directly rather than via ListAuditEventsByTeam, which filters team). |
| 67 | + deadline := time.Now().Add(3 * time.Second) |
| 68 | + var ( |
| 69 | + gotKind, gotActor, gotSummary, gotMeta string |
| 70 | + found bool |
| 71 | + ) |
| 72 | + for { |
| 73 | + row := db.QueryRowContext(context.Background(), ` |
| 74 | + SELECT kind, actor, summary, COALESCE(metadata::text, '') |
| 75 | + FROM audit_log |
| 76 | + WHERE kind = $1 |
| 77 | + AND metadata->>'token' = $2 |
| 78 | + ORDER BY created_at DESC |
| 79 | + LIMIT 1 |
| 80 | + `, dbsafety.AuditKindCustomerDBDirectDrop, token) |
| 81 | + if err := row.Scan(&gotKind, &gotActor, &gotSummary, &gotMeta); err == nil { |
| 82 | + found = true |
| 83 | + break |
| 84 | + } |
| 85 | + if time.Now().After(deadline) { |
| 86 | + break |
| 87 | + } |
| 88 | + time.Sleep(25 * time.Millisecond) |
| 89 | + } |
| 90 | + require.True(t, found, "a customer_db.direct_drop audit row must land after a sanctioned drop") |
| 91 | + |
| 92 | + assert.Equal(t, dbsafety.AuditKindCustomerDBDirectDrop, gotKind) |
| 93 | + assert.Equal(t, "system", gotActor) |
| 94 | + assert.Contains(t, gotSummary, "direct customer-DB drop") |
| 95 | + |
| 96 | + var meta map[string]string |
| 97 | + require.NoError(t, json.Unmarshal([]byte(gotMeta), &meta)) |
| 98 | + assert.Equal(t, "db.local", meta["provider"]) |
| 99 | + assert.Equal(t, "db_"+token, meta["database"]) |
| 100 | + assert.Equal(t, "usr_"+token, meta["user"]) |
| 101 | + assert.Equal(t, "postgres-customers", meta["dsn_host"], "DSN host (no credentials) must be recorded") |
| 102 | +} |
0 commit comments