Skip to content

Commit

Permalink
Ensure sessions are removed when ER/T is deleted. Fixes #2625
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Jan 7, 2025
1 parent bb12859 commit f839c86
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
29 changes: 20 additions & 9 deletions controller/db/api_session_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func newApiSessionStore(stores *stores) *apiSessionStoreImpl {
store.baseStore = newBaseStore[*ApiSession](stores, store)
stores.EventualEventer.AddEventualListener(EventualEventApiSessionDelete, store.onEventualDelete)
store.InitImpl(store)
store.AddEntityConstraint(store)
return store
}

Expand Down Expand Up @@ -141,6 +142,7 @@ func (store *apiSessionStoreImpl) GetEventsEmitter() events.EventEmmiter {
func (store *apiSessionStoreImpl) onEventualDelete(db boltz.Db, name string, apiSessionId []byte) {
idCollector := &sessionIdCollector{}
indexPath := []string{RootBucket, boltz.IndexesBucket, EntityTypeApiSessions, EntityTypeSessions}

err := db.View(func(tx *bbolt.Tx) error {
path := append(indexPath, string(apiSessionId))
if bucket := boltz.Path(tx, path...); bucket != nil {
Expand Down Expand Up @@ -224,24 +226,33 @@ func (store *apiSessionStoreImpl) Update(ctx boltz.MutateContext, entity *ApiSes
return err
}

func (store *apiSessionStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error {
func (store *apiSessionStoreImpl) ProcessPreCommit(state *boltz.EntityChangeState[*ApiSession]) error {
if state.ChangeType == boltz.EntityDeleted {
return store.handleDeleteCleanup(state.Ctx, state.EntityId)
}
return nil
}

func (store *apiSessionStoreImpl) ProcessPostCommit(_ *boltz.EntityChangeState[*ApiSession]) {
/* does nothing */
}

func (store *apiSessionStoreImpl) handleDeleteCleanup(ctx boltz.MutateContext, id string) error {
for _, apiSessionCertId := range store.GetRelatedEntitiesIdList(ctx.Tx(), id, EntityTypeApiSessionCertificates) {
if err := store.stores.apiSessionCertificate.DeleteById(ctx, apiSessionCertId); err != nil {
return err
}
}

err := store.baseStore.DeleteById(ctx, id)

if err == nil {
if bboltEventualEventer, ok := store.baseStore.stores.EventualEventer.(*EventualEventerBbolt); ok {
bboltEventualEventer.AddEventualEventWithCtx(ctx, EventualEventApiSessionDelete, []byte(id))
} else {
store.baseStore.stores.EventualEventer.AddEventualEvent(EventualEventApiSessionDelete, []byte(id))
if bboltEventualEventer, ok := store.baseStore.stores.EventualEventer.(*EventualEventerBbolt); ok {
if err := bboltEventualEventer.AddEventualEventWithCtx(ctx, EventualEventApiSessionDelete, []byte(id)); err != nil {
return err
}
} else {
store.baseStore.stores.EventualEventer.AddEventualEvent(EventualEventApiSessionDelete, []byte(id))
}

return err
return nil
}

func (store *apiSessionStoreImpl) GetTokenIndex() boltz.ReadIndex {
Expand Down
24 changes: 11 additions & 13 deletions controller/db/eventual_eventer.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,8 @@ func (a *EventualEventerBbolt) initOutstandingEventCount() {
}
}

func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext, eventType string, data []byte) {
func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext, eventType string, data []byte) error {
newId := cuid.New()
total := atomic.AddInt64(a.outstandingEvents, 1)
a.Emit(EventualEventAddedName, &EventualEventAdded{
Id: newId,
Total: total,
})

event := &EventualEvent{
BaseExtEntity: boltz.BaseExtEntity{
Expand All @@ -357,18 +352,21 @@ func (a *EventualEventerBbolt) AddEventualEventWithCtx(ctx boltz.MutateContext,
}

if err != nil {
total := atomic.AddInt64(a.outstandingEvents, -1)
a.Emit(EventualEventRemovedName, &EventualEventRemoved{
Id: newId,
Total: total,
})
pfxlog.Logger().WithError(err).Error("error adding event for EventualEventerBbolt")
return
return err
}

total := atomic.AddInt64(a.outstandingEvents, 1)
a.Emit(EventualEventAddedName, &EventualEventAdded{
Id: newId,
Total: total,
})

return nil
}

func (a *EventualEventerBbolt) AddEventualEvent(eventType string, data []byte) {
a.AddEventualEventWithCtx(nil, eventType, data)
_ = a.AddEventualEventWithCtx(nil, eventType, data)
}

func (a *EventualEventerBbolt) AddEventualListener(eventType string, listener EventListenerFunc) {
Expand Down

0 comments on commit f839c86

Please sign in to comment.