test(database): exercise #440 stdout-leak guard + SQL backfill for #351 admin group (closes #545, #546)#579
Conversation
The #440 stdout-leak fix routed the per-user admin messages to the stdlib logger (stderr) but left the group_ids backfill line in assignAdminGroupAndWarn on fmt.Printf, which writes to stdout. The existing unit regression test could not catch it because it uses an unreachable pool, so the backfill branch never runs. - migrate.go: switch the "Backfilled ..." line from fmt.Printf to log.Printf so every admin-activity message in the file stays on the stderr-bound logger. - Add integration regression test TestAssignAdminGroup_BackfillLogsToStderr_NotStdout that seeds a drifted admin against a real container, runs the real ensureAdminUser path, and asserts the backfill message lands on stderr and never on stdout. Verified to fail when the line is reverted to fmt.Printf. Refs #545
… + test Issue #351 acceptance criterion 2 asked for a migration-level idempotent backfill. PR #533 backfilled only in Go (assignAdminGroupAndWarn), which fires only when RunMigrations gets a non-empty admin email. A DB restored from a backup, or migrated without ADMIN_EMAIL set, never re-applied the backfill to pre-existing drifted admin rows. Migration 000024's backfill is one-shot at its version and does not re-run on an already-migrated DB. - Add migration 000053_backfill_admin_group_ids: the same idempotent backfill as 000024 (DISTINCT unnest dedupe, EXISTS guard, only touches empty group_ids so operator customisation is preserved), applied at migrate time regardless of how the deployment invokes migrations. Down is a documented no-op (additive backfill has no safe reverse). - Add integration test TestMigration_BackfillAdminGroupIDs covering the restore / no-admin-email path: it runs migrations with NO admin email so the Go backfill cannot fire, proving the SQL migration repairs a drifted admin row, and asserts idempotency on re-apply. Verified to fail when 000053 is neutered. The #351 group-assignment invariant already runs in default CI: ci.yml's integration-tests job runs `go test -tags=integration ./...` against a postgres service and is required by the ci-success gate, so TestEnsureAdminUser_GroupAssignment is exercised on every PR. Refs #546
📝 WalkthroughWalkthroughThis PR adds an idempotent Postgres migration (000053) to backfill the Administrators group UUID into ChangesAdmin Group ID Backfill and Logging
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/database/postgres/migrations/migrate_security_integration_test.go (1)
19-52: ⚡ Quick winConsider consolidating duplicate capture helpers.
The
captureStdoutIntegrationandcaptureLogOutputIntegrationfunctions are duplicates ofcaptureStdoutandcaptureLogOutputfrommigrate_security_test.go(shown in relevant snippets). Since both files are inpackage migrations_testandmigrate_security_test.gohas no build tag, those helpers should be available to this integration test file when running with-tags=integration.Consider removing the duplicates here and reusing the originals, or moving all capture helpers to a shared location like
helpers_test.go(similar togetMigrationsPath()).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/database/postgres/migrations/migrate_security_integration_test.go` around lines 19 - 52, Duplicate test helpers captureStdoutIntegration and captureLogOutputIntegration should be removed and the tests should reuse the existing captureStdout and captureLogOutput helpers; either delete these two functions from migrate_security_integration_test.go and import/use the originals in migrate_security_test.go (they live in the same package migrations_test and are available under -tags=integration), or move the shared helpers into a new helpers_test.go so both files reference the same captureStdout and captureLogOutput symbols. Ensure references in migrate_security_integration_test.go are updated to call captureStdout and captureLogOutput (or the new shared names) and remove the duplicated implementations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/database/postgres/migrations/migrate_security_integration_test.go`:
- Around line 19-52: Duplicate test helpers captureStdoutIntegration and
captureLogOutputIntegration should be removed and the tests should reuse the
existing captureStdout and captureLogOutput helpers; either delete these two
functions from migrate_security_integration_test.go and import/use the originals
in migrate_security_test.go (they live in the same package migrations_test and
are available under -tags=integration), or move the shared helpers into a new
helpers_test.go so both files reference the same captureStdout and
captureLogOutput symbols. Ensure references in
migrate_security_integration_test.go are updated to call captureStdout and
captureLogOutput (or the new shared names) and remove the duplicated
implementations.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 329edcfb-2237-4ab3-91ed-8a6de8d7b27d
📒 Files selected for processing (5)
internal/database/postgres/migrations/000053_backfill_admin_group_ids.down.sqlinternal/database/postgres/migrations/000053_backfill_admin_group_ids.up.sqlinternal/database/postgres/migrations/backfill_admin_group_ids_test.gointernal/database/postgres/migrations/migrate.gointernal/database/postgres/migrations/migrate_security_integration_test.go
Summary
Hardens two database regression tests that did not actually exercise the code they claimed to guard, plus a SQL-level backfill for the #351 admin-group assignment.
Fixes
internal/database/postgres/migrations/migrate.go: the sec: plaintext admin password echo'd to stdout during DB migration startup #440 plaintext-password stdout-leak guard now routes the admin group-backfill log to stderr and the regression test (migrate_security_integration_test.go) exercises the real migrate path, asserting the plaintext password never reaches stdout. Previously the test re-typed the log calls inline, so a revert would not have failed it.000053_backfill_admin_group_ids) so the bootstrap-admin-to-Administrators-group assignment from fix(auth): bootstrap admin must be auto-assigned to the Administrators group (ensureAdminUser gap) #351 runs as a real migration (not only Go runtime logic behind an integration build tag), withbackfill_admin_group_ids_test.gocovering it.Migration
New migration
000053_backfill_admin_group_ids(up + down). Verified000053is free on base (highest prior is000052from the MFA PR).Test plan
Closes #545, #546.
Summary by CodeRabbit
Bug Fixes
Chores