mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-06 08:52:19 +00:00
# Which Problems Are Solved
The event execution system currently uses a projection handler that
subscribes to and processes all events for all instances. This creates a
high static cost because the system over-fetches event data, handling
many events that are not needed by most instances. This inefficiency is
also reflected in high "rows returned" metrics in the database.
# How the Problems Are Solved
Eliminate the use of a project handler. Instead, events for which
"execution targets" are defined, are directly pushed to the queue by the
eventstore. A Router is populated in the Instance object in the authz
middleware.
- By joining the execution targets to the instance, no additional
queries are needed anymore.
- As part of the instance object, execution targets are now cached as
well.
- Events are queued within the same transaction, giving transactional
guarantees on delivery.
- Uses the "insert many fast` variant of River. Multiple jobs are queued
in a single round-trip to the database.
- Fix compatibility with PostgreSQL 15
# Additional Changes
- The signing key was stored as plain-text in the river job payload in
the DB. This violated our [Secrets
Storage](https://zitadel.com/docs/concepts/architecture/secrets#secrets-storage)
principle. This change removed the field and only uses the encrypted
version of the signing key.
- Fixed the target ordering from descending to ascending.
- Some minor linter warnings on the use of `io.WriteString()`.
# Additional Context
- Introduced in https://github.com/zitadel/zitadel/pull/9249
- Closes https://github.com/zitadel/zitadel/issues/10553
- Closes https://github.com/zitadel/zitadel/issues/9832
- Closes https://github.com/zitadel/zitadel/issues/10372
- Closes https://github.com/zitadel/zitadel/issues/10492
---------
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
(cherry picked from commit a9ebc06c77)
217 lines
6.0 KiB
Go
217 lines
6.0 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
target_domain "github.com/zitadel/zitadel/internal/execution/target"
|
|
"github.com/zitadel/zitadel/internal/repository/target"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type AddTarget struct {
|
|
models.ObjectRoot
|
|
|
|
Name string
|
|
TargetType target_domain.TargetType
|
|
Endpoint string
|
|
Timeout time.Duration
|
|
InterruptOnError bool
|
|
|
|
SigningKey string
|
|
}
|
|
|
|
func (a *AddTarget) IsValid() error {
|
|
if a.Name == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-ddqbm9us5p", "Errors.Target.Invalid")
|
|
}
|
|
if a.Timeout == 0 {
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-39f35d8uri", "Errors.Target.NoTimeout")
|
|
}
|
|
_, err := url.Parse(a.Endpoint)
|
|
if err != nil || a.Endpoint == "" {
|
|
return zerrors.ThrowInvalidArgument(err, "COMMAND-1r2k6qo6wg", "Errors.Target.InvalidURL")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Commands) AddTarget(ctx context.Context, add *AddTarget, resourceOwner string) (_ time.Time, err error) {
|
|
if resourceOwner == "" {
|
|
return time.Time{}, zerrors.ThrowInvalidArgument(nil, "COMMAND-brml926e2d", "Errors.IDMissing")
|
|
}
|
|
|
|
if err := add.IsValid(); err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
if add.AggregateID == "" {
|
|
add.AggregateID, err = c.idGenerator.Next()
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
}
|
|
wm, err := c.getTargetWriteModelByID(ctx, add.AggregateID, resourceOwner)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
if wm.State.Exists() {
|
|
return time.Time{}, zerrors.ThrowAlreadyExists(nil, "INSTANCE-9axkz0jvzm", "Errors.Target.AlreadyExists")
|
|
}
|
|
code, err := c.newSigningKey(ctx, c.eventstore.Filter, c.targetEncryption) //nolint
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
add.SigningKey = code.PlainCode()
|
|
pushedEvents, err := c.eventstore.Push(ctx, target.NewAddedEvent(
|
|
ctx,
|
|
TargetAggregateFromWriteModel(&wm.WriteModel),
|
|
add.Name,
|
|
add.TargetType,
|
|
add.Endpoint,
|
|
add.Timeout,
|
|
add.InterruptOnError,
|
|
code.Crypted,
|
|
))
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
if err := AppendAndReduce(wm, pushedEvents...); err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
return wm.ChangeDate, nil
|
|
}
|
|
|
|
type ChangeTarget struct {
|
|
models.ObjectRoot
|
|
|
|
Name *string
|
|
TargetType *target_domain.TargetType
|
|
Endpoint *string
|
|
Timeout *time.Duration
|
|
InterruptOnError *bool
|
|
|
|
ExpirationSigningKey bool
|
|
SigningKey *string
|
|
}
|
|
|
|
func (a *ChangeTarget) IsValid() error {
|
|
if a.AggregateID == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-1l6ympeagp", "Errors.IDMissing")
|
|
}
|
|
if a.Name != nil && *a.Name == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-d1wx4lm0zr", "Errors.Target.Invalid")
|
|
}
|
|
if a.Timeout != nil && *a.Timeout == 0 {
|
|
return zerrors.ThrowInvalidArgument(nil, "COMMAND-08b39vdi57", "Errors.Target.NoTimeout")
|
|
}
|
|
if a.Endpoint != nil {
|
|
_, err := url.Parse(*a.Endpoint)
|
|
if err != nil || *a.Endpoint == "" {
|
|
return zerrors.ThrowInvalidArgument(err, "COMMAND-jsbaera7b6", "Errors.Target.InvalidURL")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Commands) ChangeTarget(ctx context.Context, change *ChangeTarget, resourceOwner string) (time.Time, error) {
|
|
if resourceOwner == "" {
|
|
return time.Time{}, zerrors.ThrowInvalidArgument(nil, "COMMAND-zqibgg0wwh", "Errors.IDMissing")
|
|
}
|
|
if err := change.IsValid(); err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
existing, err := c.getTargetWriteModelByID(ctx, change.AggregateID, resourceOwner)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
if !existing.State.Exists() {
|
|
return time.Time{}, zerrors.ThrowNotFound(nil, "COMMAND-xj14f2cccn", "Errors.Target.NotFound")
|
|
}
|
|
|
|
var changedSigningKey *crypto.CryptoValue
|
|
if change.ExpirationSigningKey {
|
|
code, err := c.newSigningKey(ctx, c.eventstore.Filter, c.targetEncryption) //nolint
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
changedSigningKey = code.Crypted
|
|
change.SigningKey = &code.Plain
|
|
}
|
|
|
|
changedEvent := existing.NewChangedEvent(
|
|
ctx,
|
|
TargetAggregateFromWriteModel(&existing.WriteModel),
|
|
change.Name,
|
|
change.TargetType,
|
|
change.Endpoint,
|
|
change.Timeout,
|
|
change.InterruptOnError,
|
|
changedSigningKey,
|
|
)
|
|
if changedEvent == nil {
|
|
return existing.WriteModel.ChangeDate, nil
|
|
}
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
err = AppendAndReduce(existing, pushedEvents...)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
return existing.WriteModel.ChangeDate, nil
|
|
}
|
|
|
|
func (c *Commands) DeleteTarget(ctx context.Context, id, resourceOwner string) (time.Time, error) {
|
|
if id == "" || resourceOwner == "" {
|
|
return time.Time{}, zerrors.ThrowInvalidArgument(nil, "COMMAND-obqos2l3no", "Errors.IDMissing")
|
|
}
|
|
|
|
existing, err := c.getTargetWriteModelByID(ctx, id, resourceOwner)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
if !existing.State.Exists() {
|
|
return existing.WriteModel.ChangeDate, nil
|
|
}
|
|
|
|
if err := c.pushAppendAndReduce(ctx,
|
|
existing,
|
|
target.NewRemovedEvent(ctx,
|
|
TargetAggregateFromWriteModel(&existing.WriteModel),
|
|
existing.Name,
|
|
),
|
|
); err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
return existing.WriteModel.ChangeDate, nil
|
|
}
|
|
|
|
func (c *Commands) existsTargetsByIDs(ctx context.Context, ids []string, resourceOwner string) bool {
|
|
wm := NewTargetsExistsWriteModel(ids, resourceOwner)
|
|
err := c.eventstore.FilterToQueryReducer(ctx, wm)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return wm.AllExists()
|
|
}
|
|
|
|
func (c *Commands) getTargetWriteModelByID(ctx context.Context, id string, resourceOwner string) (*TargetWriteModel, error) {
|
|
wm := NewTargetWriteModel(id, resourceOwner)
|
|
err := c.eventstore.FilterToQueryReducer(ctx, wm)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return wm, nil
|
|
}
|
|
|
|
func (c *Commands) newSigningKey(ctx context.Context, filter preparation.FilterToQueryReducer, alg crypto.EncryptionAlgorithm) (*EncryptedCode, error) {
|
|
return c.newEncryptedCodeWithDefault(ctx, filter, domain.SecretGeneratorTypeSigningKey, alg, c.defaultSecretGenerators.SigningKey)
|
|
}
|