mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-24 08:41:33 +00:00
b5564572bc
This implementation increases parallel write capabilities of the eventstore. Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and [06](https://zitadel.com/docs/support/advisory/a10006). The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`. If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
)
|
|
|
|
const (
|
|
machineKeyEventPrefix = machineEventPrefix + "key."
|
|
MachineKeyAddedEventType = machineKeyEventPrefix + "added"
|
|
MachineKeyRemovedEventType = machineKeyEventPrefix + "removed"
|
|
)
|
|
|
|
type MachineKeyAddedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
KeyID string `json:"keyId,omitempty"`
|
|
KeyType domain.AuthNKeyType `json:"type,omitempty"`
|
|
ExpirationDate time.Time `json:"expirationDate,omitempty"`
|
|
PublicKey []byte `json:"publicKey,omitempty"`
|
|
}
|
|
|
|
func (e *MachineKeyAddedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineKeyAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewMachineKeyAddedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
keyID string,
|
|
keyType domain.AuthNKeyType,
|
|
expirationDate time.Time,
|
|
publicKey []byte,
|
|
) *MachineKeyAddedEvent {
|
|
return &MachineKeyAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineKeyAddedEventType,
|
|
),
|
|
KeyID: keyID,
|
|
KeyType: keyType,
|
|
ExpirationDate: expirationDate,
|
|
PublicKey: publicKey,
|
|
}
|
|
}
|
|
|
|
func MachineKeyAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineKeyAdded := &MachineKeyAddedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineKeyAdded)
|
|
if err != nil {
|
|
//first events had wrong payload.
|
|
// the keys were removed later, that's why we ignore them here.
|
|
if unwrapErr, ok := err.(*json.UnmarshalTypeError); ok && unwrapErr.Field == "publicKey" {
|
|
return machineKeyAdded, nil
|
|
}
|
|
return nil, errors.ThrowInternal(err, "USER-p0ovS", "unable to unmarshal machine key added")
|
|
}
|
|
|
|
return machineKeyAdded, nil
|
|
}
|
|
|
|
type MachineKeyRemovedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
KeyID string `json:"keyId,omitempty"`
|
|
}
|
|
|
|
func (e *MachineKeyRemovedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineKeyRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewMachineKeyRemovedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
keyID string,
|
|
) *MachineKeyRemovedEvent {
|
|
return &MachineKeyRemovedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineKeyRemovedEventType,
|
|
),
|
|
KeyID: keyID,
|
|
}
|
|
}
|
|
|
|
func MachineKeyRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineRemoved := &MachineKeyRemovedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineRemoved)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "USER-5Gm9s", "unable to unmarshal machine key removed")
|
|
}
|
|
|
|
return machineRemoved, nil
|
|
}
|