Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure sessions are removed when ER/T is deleted. Fixes #2625 #2626

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading