fix(eventstore): allow overwriting resource owner of events (#12261)

# Which Problems Are Solved

- The eventstore did not support intentionally overwriting the resource
owner when creating events for aggregates that may be reused across
owners.
- Resource owner handling was implicit and could not be controlled per
command/event type.
- We needed a safe way to distinguish between:
  - keeping the existing aggregate owner, and
  - explicitly setting a new owner for specific create-like events.

# How the Problems Are Solved

- Introduced a new eventstore command type with an explicit
enforce_owner flag.
- Updated eventstore.commands_to_events and eventstore.push so owner
assignment is now explicit:
  - if enforce_owner is true, the command owner is written
- if enforce_owner is false, the existing aggregate owner is retained
when present
- Added EnforceResourceOwnerCommand and wiring so command types can opt
in to enforced owner behavior.
- Wired the new behavior through the v3 eventstore push path, including
compatibility fallback for older command type mapping.
- Added migration/setup changes to register and use the new command type
and SQL functions.
- Added and updated tests for owner overwrite and aggregate ID reuse
scenarios.

# Additional Changes

- Added small migration/setup robustness improvements related to
eventstore setup ordering and helper reuse.
- Added focused test coverage for enforced owner behavior and
sequencing.
- Events that currently allow owner changes (implement
EnforceResourceOwner) are:
  - AddedEvent (action)
  - GroupAddedEvent
  - StartedEvent (idp intent)
  - ProjectAddedEvent
  - HumanAddedEvent
  - HumanRegisteredEvent
  - MachineAddedEvent
  - CreatedEvent (schema user)

# Additional Context

- Follow-up for eventstore owner-handling correctness in create flows
and aggregate ID reuse cases.
- No additional issue link was attached for this change.

---------

Co-authored-by: abhishek kumar gupta <[email protected]>
This commit is contained in:
Silvan
2026-06-15 11:24:37 +02:00
committed by GitHub
co-authored by abhishek kumar gupta
parent 4459a1380c
commit 6082e59d47
66 changed files with 1603 additions and 288 deletions
+75 -12
View File
@@ -87,7 +87,7 @@ func (wm *HumanWebAuthNWriteModel) AppendEvents(events ...eventstore.Event) {
if wm.WebauthNTokenID == e.WebAuthNTokenID {
wm.WriteModel.AppendEvents(&e.HumanWebAuthNRemovedEvent)
}
case *user.UserRemovedEvent:
default:
wm.WriteModel.AppendEvents(e)
}
}
@@ -104,7 +104,15 @@ func (wm *HumanWebAuthNWriteModel) Reduce() error {
wm.SignCount = e.SignCount
case *user.HumanWebAuthNRemovedEvent:
wm.State = domain.MFAStateRemoved
case *user.UserRemovedEvent:
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent, *user.UserRemovedEvent:
wm.Challenge = ""
wm.KeyID = nil
wm.PublicKey = nil
wm.AttestationType = ""
wm.AAGUID = nil
wm.SignCount = 0
wm.WebAuthNTokenName = ""
wm.RPID = ""
wm.State = domain.MFAStateRemoved
}
}
@@ -134,7 +142,11 @@ func (wm *HumanWebAuthNWriteModel) Query() *eventstore.SearchQueryBuilder {
AddQuery().
AggregateTypes(user.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(user.HumanU2FTokenAddedType,
EventTypes(
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
user.HumanU2FTokenAddedType,
user.HumanPasswordlessTokenAddedType,
user.HumanU2FTokenAddedType,
user.HumanPasswordlessTokenAddedType,
@@ -206,7 +218,11 @@ func (wm *HumanU2FTokensReadModel) Reduce() error {
wm.WebAuthNTokens[len(wm.WebAuthNTokens)-1] = nil
wm.WebAuthNTokens = wm.WebAuthNTokens[:len(wm.WebAuthNTokens)-1]
case *user.UserRemovedEvent:
wm.WebAuthNTokens = nil
wm.UserState = domain.UserStateDeleted
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent:
wm.WebAuthNTokens = nil
wm.UserState = domain.UserStateUnspecified
}
}
return wm.WriteModel.Reduce()
@@ -221,7 +237,12 @@ func (rm *HumanU2FTokensReadModel) Query() *eventstore.SearchQueryBuilder {
EventTypes(
user.HumanU2FTokenAddedType,
user.HumanU2FTokenVerifiedType,
user.HumanU2FTokenRemovedType).
user.HumanU2FTokenRemovedType,
user.UserRemovedType,
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
).
Builder()
}
@@ -293,7 +314,11 @@ func (wm *HumanPasswordlessTokensReadModel) Reduce() error {
wm.WebAuthNTokens[len(wm.WebAuthNTokens)-1] = nil
wm.WebAuthNTokens = wm.WebAuthNTokens[:len(wm.WebAuthNTokens)-1]
case *user.UserRemovedEvent:
wm.WebAuthNTokens = nil
wm.UserState = domain.UserStateDeleted
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent:
wm.WebAuthNTokens = nil
wm.UserState = domain.UserStateUnspecified
}
}
return wm.WriteModel.Reduce()
@@ -308,7 +333,12 @@ func (rm *HumanPasswordlessTokensReadModel) Query() *eventstore.SearchQueryBuild
EventTypes(
user.HumanPasswordlessTokenAddedType,
user.HumanPasswordlessTokenVerifiedType,
user.HumanPasswordlessTokenRemovedType).
user.HumanPasswordlessTokenRemovedType,
user.UserRemovedType,
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
).
Builder()
}
@@ -354,7 +384,7 @@ func (wm *HumanU2FLoginReadModel) AppendEvents(events ...eventstore.Event) {
continue
}
wm.WriteModel.AppendEvents(e)
case *user.UserRemovedEvent:
default:
wm.WriteModel.AppendEvents(e)
}
}
@@ -369,7 +399,15 @@ func (wm *HumanU2FLoginReadModel) Reduce() error {
wm.UserVerification = e.UserVerification
wm.State = domain.UserStateActive
case *user.UserRemovedEvent:
wm.Challenge = ""
wm.AllowedCredentialIDs = nil
wm.UserVerification = domain.UserVerificationRequirementUnspecified
wm.State = domain.UserStateDeleted
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent:
wm.Challenge = ""
wm.AllowedCredentialIDs = nil
wm.UserVerification = domain.UserVerificationRequirementUnspecified
wm.State = domain.UserStateUnspecified
}
}
return wm.WriteModel.Reduce()
@@ -383,7 +421,11 @@ func (rm *HumanU2FLoginReadModel) Query() *eventstore.SearchQueryBuilder {
AggregateIDs(rm.AggregateID).
EventTypes(
user.HumanU2FTokenBeginLoginType,
user.UserRemovedType).
user.UserRemovedType,
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
).
Builder()
}
@@ -415,7 +457,7 @@ func (wm *HumanPasswordlessLoginReadModel) AppendEvents(events ...eventstore.Eve
continue
}
wm.WriteModel.AppendEvents(e)
case *user.UserRemovedEvent:
default:
wm.WriteModel.AppendEvents(e)
}
}
@@ -430,7 +472,15 @@ func (wm *HumanPasswordlessLoginReadModel) Reduce() error {
wm.UserVerification = e.UserVerification
wm.State = domain.UserStateActive
case *user.UserRemovedEvent:
wm.Challenge = ""
wm.AllowedCredentialIDs = nil
wm.UserVerification = domain.UserVerificationRequirementUnspecified
wm.State = domain.UserStateDeleted
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent:
wm.Challenge = ""
wm.AllowedCredentialIDs = nil
wm.UserVerification = domain.UserVerificationRequirementUnspecified
wm.State = domain.UserStateUnspecified
}
}
return wm.WriteModel.Reduce()
@@ -444,7 +494,11 @@ func (rm *HumanPasswordlessLoginReadModel) Query() *eventstore.SearchQueryBuilde
AggregateIDs(rm.AggregateID).
EventTypes(
user.HumanPasswordlessTokenBeginLoginType,
user.UserRemovedType).
user.UserRemovedType,
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
).
Builder()
}
@@ -493,7 +547,7 @@ func (wm *HumanPasswordlessInitCodeWriteModel) AppendEvents(events ...eventstore
if wm.CodeID == e.ID {
wm.WriteModel.AppendEvents(e)
}
case *user.UserRemovedEvent:
default:
wm.WriteModel.AppendEvents(e)
}
}
@@ -512,7 +566,12 @@ func (wm *HumanPasswordlessInitCodeWriteModel) Reduce() error {
wm.appendCheckFailedEvent(e)
case *user.HumanPasswordlessInitCodeCheckSucceededEvent:
wm.State = domain.PasswordlessInitCodeStateRemoved
case *user.UserRemovedEvent:
case *user.HumanAddedEvent, *user.HumanRegisteredEvent, *user.MachineAddedEvent, *user.UserRemovedEvent:
wm.Attempts = 0
wm.CryptoCode = nil
wm.CodeCreationDate = time.Time{}
wm.CodeExpiration = 0
wm.State = domain.PasswordlessInitCodeStateUnspecified
wm.State = domain.PasswordlessInitCodeStateRemoved
}
}
@@ -554,6 +613,10 @@ func (wm *HumanPasswordlessInitCodeWriteModel) Query() *eventstore.SearchQueryBu
user.HumanPasswordlessInitCodeSentType,
user.HumanPasswordlessInitCodeCheckFailedType,
user.HumanPasswordlessInitCodeCheckSucceededType,
user.UserRemovedType).
user.UserRemovedType,
user.HumanAddedType,
user.HumanRegisteredType,
user.MachineAddedEventType,
).
Builder()
}