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

@@ -38,6 +38,7 @@ var (
_ pact.HasMailTemplates = (*Plugin)(nil)
_ pact.HasLang = (*Plugin)(nil)
_ pact.HasCommands = (*Plugin)(nil)
_ pact.HasSchedule = (*Plugin)(nil)
_ surf.BucketProvider = (*Plugin)(nil)
_ bouncer.UserProvider = classes.GormUsers{}
)
@@ -53,8 +54,7 @@ var langFS embed.FS
// Plugin is the golem15.user plugin.
type Plugin struct {
app *backpack.App
sweepStop chan struct{}
app *backpack.App
}
func (p *Plugin) ID() string { return "golem15.user" }
@@ -79,19 +79,19 @@ func (p *Plugin) Boot(app *backpack.App) error {
if _, err := classes.MailFallbackLocale(app); err != nil {
return err
}
// Serve publishes the database after Boot, so the GORM hooks, the
// blacklist store and its sweep are installed on lagoon.Publish, or at
// once when the database is already published.
if _, err := classes.BlacklistSweepInterval(app); err != nil {
return err
}
// Serve publishes the database after Boot, so the GORM hooks and the
// blacklist store are installed on lagoon.Publish, or at once when the
// database is already published. Expired blacklist rows are deleted by
// the scheduled user:jwt-blacklist-sweep command (Schedule).
if err := lagoon.OnDatabase(app, func(sqlDB *sql.DB, gdb *gorm.DB) error {
if err := classes.RegisterHooks(gdb); err != nil {
return err
}
store := bouncer.NewPostgresBlacklist(sqlDB, "jwt_blacklist")
if err := app.Publish[bouncer.BlacklistStore](store); err != nil {
return err
}
p.startBlacklistSweep(store)
return nil
store := bouncer.NewPostgresBlacklist(sqlDB, classes.JWTBlacklistTable)
return app.Publish[bouncer.BlacklistStore](store)
}); err != nil {
return err
}
@@ -221,37 +221,16 @@ func (p *Plugin) bootBackendLinks(app *backpack.App) error {
return nil
}
func (p *Plugin) startBlacklistSweep(bl bouncer.BlacklistStore) {
interval := 10 * time.Minute
if p.app != nil && p.app.Config != nil {
raw := p.app.Config.String("golem15.user.jwt.blacklist_sweep_interval")
if raw != "" {
parsed, err := time.ParseDuration(raw)
if err != nil || parsed <= 0 {
return
}
interval = parsed
}
// Schedule returns the scheduled commands: user:jwt-blacklist-sweep every
// golem15.user.jwt.blacklist_sweep_interval (10m by default), none when the
// interval is 0. p.app is nil before Register, which yields the default. A
// bad interval schedules nothing; Boot reports it.
func (p *Plugin) Schedule() []pact.ScheduledCommand {
interval, err := classes.BlacklistSweepInterval(p.app)
if err != nil || interval == 0 {
return nil
}
if p.sweepStop != nil {
return
}
p.sweepStop = make(chan struct{})
stop := p.sweepStop
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := bl.Sweep(context.Background(), time.Now()); err != nil {
pluginLog.Error("golem15.user: jwt blacklist sweep", "err", err)
}
case <-stop:
return
}
}
}()
return []pact.ScheduledCommand{{Command: "user:jwt-blacklist-sweep", Cadence: pact.Every(interval)}}
}
func (p *Plugin) Buckets() map[string]surf.Bucket {
@@ -312,8 +291,14 @@ func (p *Plugin) MailLayouts() map[string]string {
}
}
// Commands returns the console commands, among them
// user:jwt-blacklist-sweep, which Schedule runs.
func (p *Plugin) Commands() []bonfire.Command {
return append([]bonfire.Command{console.RequirePasswordChange(p.withDB), console.BackfillHandles(p.app)}, console.APITokenCommands(p.app, p.withDB)...)
return append([]bonfire.Command{
console.RequirePasswordChange(p.withDB),
console.BackfillHandles(p.app),
console.JWTBlacklistSweep(p.app),
}, console.APITokenCommands(p.app, p.withDB)...)
}
// withDB runs fn with the application's database: the one the serve command