fix(quick-261119-kj8): sweep the JWT blacklist from a scheduled user:jwt-blacklist-sweep command

Boot started a ticker goroutine inside lagoon.OnDatabase that nothing
stopped, so every booting process (serve, each console command, each test
boot) leaked one and swept the blacklist once per process. The sweep is
now the console command user:jwt-blacklist-sweep, which the conga
scheduler runs on the job worker every
golem15.user.jwt.blacklist_sweep_interval (default 10m, 0 for none), once
per period across instances. An interval the scheduler cannot run
(unparsable, negative, under 1s, not dividing 24h) now fails boot.
This commit is contained in:
Jakub Zych
2026-11-19 15:06:29 +01:00
parent 6ec4c85c48
commit bc5f4f7260
9 changed files with 486 additions and 102 deletions

View File

@@ -506,47 +506,6 @@ func TestLoginRouteGroup(t *testing.T) {
}
}
func TestLoginBlacklistSweep(t *testing.T) {
gdb, sqlDB := sessionDB(t)
app := newSessionApp(t, gdb, sqlDB)
_, stop := bootPlugin(t, app, "50ms")
past := time.Now().Add(-time.Minute)
if err := gdb.Exec(`INSERT INTO jwt_blacklist (jti, expires_at, valid_until) VALUES (?, ?, ?)`, "sweep-me", past, past).Error; err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(2 * time.Second)
for {
var n int64
if err := gdb.Raw(`SELECT COUNT(*) FROM jwt_blacklist WHERE jti = ?`, "sweep-me").Scan(&n).Error; err != nil {
t.Fatal(err)
}
if n == 0 {
break
}
if time.Now().After(deadline) {
t.Fatal("expired jwt_blacklist row was not swept")
}
time.Sleep(20 * time.Millisecond)
}
stop()
time.Sleep(150 * time.Millisecond)
quiet := newSessionApp(t, gdb, sqlDB)
bootPlugin(t, quiet, "0s")
later := time.Now().Add(-time.Minute)
if err := gdb.Exec(`INSERT INTO jwt_blacklist (jti, expires_at, valid_until) VALUES (?, ?, ?)`, "sweep-keep", later, later).Error; err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
var n int64
if err := gdb.Raw(`SELECT COUNT(*) FROM jwt_blacklist WHERE jti = ?`, "sweep-keep").Scan(&n).Error; err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatal("non-positive sweep interval removed a row")
}
}
type captureMail struct {
got []string
}
@@ -707,7 +666,7 @@ func newSessionApp(t *testing.T, gdb *gorm.DB, sqlDB *sql.DB) *backpack.App {
return app
}
func bootPlugin(t *testing.T, app *backpack.App, sweep string) (*Plugin, func()) {
func bootPlugin(t *testing.T, app *backpack.App, sweep string) *Plugin {
t.Helper()
if sweep != "" {
if err := app.Config.Set("golem15.user.jwt.blacklist_sweep_interval", sweep); err != nil {
@@ -718,14 +677,7 @@ func bootPlugin(t *testing.T, app *backpack.App, sweep string) (*Plugin, func())
if err := p.Boot(app); err != nil {
t.Fatal(err)
}
stop := func() {}
if p.sweepStop != nil {
ch := p.sweepStop
var once sync.Once
stop = func() { once.Do(func() { close(ch) }) }
t.Cleanup(stop)
}
return p, stop
return p
}
func sessionConfig(t *testing.T) *compass.Config {