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:
52
classes/blacklist_sweep.go
Normal file
52
classes/blacklist_sweep.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
)
|
||||
|
||||
// JWTBlacklistTable is the table of migration
|
||||
// 202609220007_create_jwt_blacklist that the plugin's blacklist store uses.
|
||||
const JWTBlacklistTable = "jwt_blacklist"
|
||||
|
||||
// DefaultBlacklistSweepInterval is how often the scheduler runs
|
||||
// user:jwt-blacklist-sweep when golem15.user.jwt.blacklist_sweep_interval is
|
||||
// not set.
|
||||
const DefaultBlacklistSweepInterval = 10 * time.Minute
|
||||
|
||||
const blacklistSweepIntervalKey = "golem15.user.jwt.blacklist_sweep_interval"
|
||||
|
||||
// BlacklistSweepInterval reads golem15.user.jwt.blacklist_sweep_interval, a
|
||||
// Go duration (10m by default). 0 means no sweep is scheduled. Any other
|
||||
// value must be at least 1s and divide 24h evenly: the scheduler's Every
|
||||
// cadence is aligned to local midnight, so a period that does not divide the
|
||||
// day cannot be scheduled. A value that does not parse, is negative, is
|
||||
// shorter than a second or does not divide 24h is an error, so a typo fails
|
||||
// the boot instead of silently stopping the sweep.
|
||||
func BlacklistSweepInterval(app *backpack.App) (time.Duration, error) {
|
||||
if app == nil || app.Config == nil {
|
||||
return DefaultBlacklistSweepInterval, nil
|
||||
}
|
||||
raw := strings.TrimSpace(app.Config.String(blacklistSweepIntervalKey))
|
||||
if raw == "" {
|
||||
// A non-string value (an unquoted YAML 0) may read as "".
|
||||
if v, ok := app.Config.Lookup(blacklistSweepIntervalKey); ok && v != nil {
|
||||
raw = strings.TrimSpace(fmt.Sprint(v))
|
||||
}
|
||||
}
|
||||
if raw == "" {
|
||||
return DefaultBlacklistSweepInterval, nil
|
||||
}
|
||||
d, err := time.ParseDuration(raw)
|
||||
switch {
|
||||
case err != nil:
|
||||
case d == 0:
|
||||
return 0, nil
|
||||
case d >= time.Second && (24*time.Hour)%d == 0:
|
||||
return d, nil
|
||||
}
|
||||
return 0, fmt.Errorf("golem15.user: %s must be 0 (no sweep) or a duration of at least 1s that divides 24h evenly, such as 10m, got %q", blacklistSweepIntervalKey, raw)
|
||||
}
|
||||
59
classes/blacklist_sweep_test.go
Normal file
59
classes/blacklist_sweep_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package classes
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
)
|
||||
|
||||
// TestBlacklistSweepInterval reads golem15.user.jwt.blacklist_sweep_interval:
|
||||
// 10m by default, 0 for no sweep (string, duration or unquoted YAML int, and
|
||||
// from a SUMMER_ variable), and an error naming the key for a value the
|
||||
// scheduler cannot run.
|
||||
func TestBlacklistSweepInterval(t *testing.T) {
|
||||
const key = "golem15.user.jwt.blacklist_sweep_interval"
|
||||
if got, err := BlacklistSweepInterval(nil); got != DefaultBlacklistSweepInterval || err != nil {
|
||||
t.Errorf("nil app: %v %v", got, err)
|
||||
}
|
||||
if got, err := BlacklistSweepInterval(&backpack.App{}); got != DefaultBlacklistSweepInterval || err != nil {
|
||||
t.Errorf("nil config: %v %v", got, err)
|
||||
}
|
||||
if got, err := BlacklistSweepInterval(mailTestApp(t)); got != 10*time.Minute || err != nil {
|
||||
t.Errorf("unset: %v %v", got, err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
value any
|
||||
want time.Duration
|
||||
}{
|
||||
{"10m", 10 * time.Minute},
|
||||
{"1h", time.Hour},
|
||||
{"30s", 30 * time.Second},
|
||||
{"24h", 24 * time.Hour},
|
||||
{" 1m ", time.Minute},
|
||||
{"0", 0},
|
||||
{"0s", 0},
|
||||
{0, 0},
|
||||
} {
|
||||
app := mailTestApp(t)
|
||||
if err := app.Config.Set(key, tc.value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := BlacklistSweepInterval(app); got != tc.want || err != nil {
|
||||
t.Errorf("%#v: %v %v, want %v", tc.value, got, err, tc.want)
|
||||
}
|
||||
}
|
||||
if got, err := BlacklistSweepInterval(mailTestApp(t, "SUMMER_GOLEM15__USER__JWT__BLACKLIST_SWEEP_INTERVAL=0")); got != 0 || err != nil {
|
||||
t.Errorf("SUMMER_ 0: %v %v", got, err)
|
||||
}
|
||||
for _, bad := range []any{"soon", "3", "-1m", "500ms", "7m", "48h", -5} {
|
||||
app := mailTestApp(t)
|
||||
if err := app.Config.Set(key, bad); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := BlacklistSweepInterval(app); err == nil || got != 0 || !strings.Contains(err.Error(), key) {
|
||||
t.Errorf("%#v: %v %v, want an error naming %s", bad, got, err, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user