fix(32.2-05): run admin:reset-password in a lagoon transaction
The reset fired BackendAccessRevokedEvent inside a plain GORM transaction, where lagoon.AfterCommit refuses to run, so a listener's after-commit work (sm-oauthserver-plugin's revoked MCPSubjectChanged events that end MCP sessions) never ran for this D-13 site. TestBackendAccessRevokedEvent now asserts the after-commit callback runs once for a committed reset and not for a vetoed one.
This commit is contained in:
@@ -13,25 +13,32 @@ import (
|
|||||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||||
"git.golem15.com/golem15/summercms/modules/bonfire"
|
"git.golem15.com/golem15/summercms/modules/bonfire"
|
||||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||||
|
"git.golem15.com/golem15/summercms/modules/lagoon"
|
||||||
"git.golem15.com/golem15/summercms/modules/pact"
|
"git.golem15.com/golem15/summercms/modules/pact"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// accessEvents records the in-transaction BackendAccessRevokedEvents and
|
// accessEvents records the in-transaction BackendAccessRevokedEvents and
|
||||||
// the after-commit pact.MCPSubjectChanged events of one app; while fail is
|
// the after-commit pact.MCPSubjectChanged events of one app, and counts the
|
||||||
// set the first listener answers an error, so the save rolls back.
|
// after-commit callbacks the revoked listener registers on the event's Tx
|
||||||
|
// that ran (as a listener that fires session events after the commit
|
||||||
|
// does); while fail is set the first listener answers an error, so the
|
||||||
|
// save rolls back.
|
||||||
type accessEvents struct {
|
type accessEvents struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
revoked []cabana.BackendAccessRevokedEvent
|
revoked []cabana.BackendAccessRevokedEvent
|
||||||
subjects []pact.MCPSubjectChanged
|
subjects []pact.MCPSubjectChanged
|
||||||
fail atomic.Bool
|
fail atomic.Bool
|
||||||
|
committed atomic.Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func listenAccessEvents(app *backpack.App) *accessEvents {
|
func listenAccessEvents(app *backpack.App) *accessEvents {
|
||||||
rec := &accessEvents{}
|
rec := &accessEvents{}
|
||||||
app.Events.Listen("test-revoked", func(_ context.Context, ev *cabana.BackendAccessRevokedEvent) error {
|
app.Events.Listen("test-revoked", func(ctx context.Context, ev *cabana.BackendAccessRevokedEvent) error {
|
||||||
rec.mu.Lock()
|
rec.mu.Lock()
|
||||||
rec.revoked = append(rec.revoked, *ev)
|
rec.revoked = append(rec.revoked, *ev)
|
||||||
rec.mu.Unlock()
|
rec.mu.Unlock()
|
||||||
|
lagoon.AfterCommit(ctx, ev.Tx, func(context.Context, *gorm.DB) { rec.committed.Add(1) })
|
||||||
if rec.fail.Load() {
|
if rec.fail.Load() {
|
||||||
return errors.New("listener refused")
|
return errors.New("listener refused")
|
||||||
}
|
}
|
||||||
@@ -136,6 +143,7 @@ func TestBackendAccessRevokedEvent(t *testing.T) {
|
|||||||
t.Run("admin:reset-password", func(t *testing.T) {
|
t.Run("admin:reset-password", func(t *testing.T) {
|
||||||
target, _ := env.admin(t, "access-reset", false, "")
|
target, _ := env.admin(t, "access-reset", false, "")
|
||||||
rec.take()
|
rec.take()
|
||||||
|
before := rec.committed.Load()
|
||||||
reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password")
|
reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password")
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := reset.Run(context.Background(), flagInput{
|
if err := reset.Run(context.Background(), flagInput{
|
||||||
@@ -145,12 +153,18 @@ func TestBackendAccessRevokedEvent(t *testing.T) {
|
|||||||
t.Fatalf("admin:reset-password: %v", err)
|
t.Fatalf("admin:reset-password: %v", err)
|
||||||
}
|
}
|
||||||
expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID)
|
expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID)
|
||||||
|
// The command commits through lagoon, so after-commit work a
|
||||||
|
// listener registers on the event's Tx runs, once.
|
||||||
|
if got := rec.committed.Load() - before; got != 1 {
|
||||||
|
t.Fatalf("after-commit callbacks run by the reset = %d, want 1", got)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("admin:reset-password rolls back on a listener error", func(t *testing.T) {
|
t.Run("admin:reset-password rolls back on a listener error", func(t *testing.T) {
|
||||||
target, _ := env.admin(t, "access-reset-veto", false, "")
|
target, _ := env.admin(t, "access-reset-veto", false, "")
|
||||||
before := env.storedUser(t, target.ID).Password
|
before := env.storedUser(t, target.ID).Password
|
||||||
rec.take()
|
rec.take()
|
||||||
|
committed := rec.committed.Load()
|
||||||
rec.fail.Store(true)
|
rec.fail.Store(true)
|
||||||
reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password")
|
reset := commandByName(t, cabana.RuntimeCommands(env.app), "admin:reset-password")
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@@ -165,6 +179,9 @@ func TestBackendAccessRevokedEvent(t *testing.T) {
|
|||||||
if env.storedUser(t, target.ID).Password != before {
|
if env.storedUser(t, target.ID).Password != before {
|
||||||
t.Fatal("a vetoed reset wrote the new hash")
|
t.Fatal("a vetoed reset wrote the new hash")
|
||||||
}
|
}
|
||||||
|
if rec.committed.Load() != committed {
|
||||||
|
t.Fatal("after-commit work ran for a rolled-back reset")
|
||||||
|
}
|
||||||
expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID)
|
expectRevoked(t, cabana.BackendAccessPasswordReset, target.ID)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -138,7 +138,12 @@ func adminResetPassword(ctx context.Context, app *backpack.App, in bonfire.Input
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return withAdminDB(ctx, app, func(gdb *gorm.DB) error {
|
return withAdminDB(ctx, app, func(gdb *gorm.DB) error {
|
||||||
return gdb.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
// A lagoon transaction, so the after-commit work a
|
||||||
|
// BackendAccessRevokedEvent listener registers on tx (for example
|
||||||
|
// the revoked events that end MCP sessions) runs once the reset
|
||||||
|
// commits; inside a plain GORM transaction lagoon.AfterCommit
|
||||||
|
// refuses to run it.
|
||||||
|
return lagoon.Transaction(ctx, gdb, func(ctx context.Context, tx *gorm.DB) error {
|
||||||
var users []BackendUser
|
var users []BackendUser
|
||||||
err := tx.Where("login = ? OR lower(email) = ?", identifier, strings.ToLower(identifier)).Find(&users).Error
|
err := tx.Where("login = ? OR lower(email) = ?", identifier, strings.ToLower(identifier)).Find(&users).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user