mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:47:33 +00:00
feat: Iam projection (#3074)
* feat: implement projection for iam and clean up code * feat: add migration * fix: remove unused tests * fix: handler
This commit is contained in:
122
internal/query/projection/iam.go
Normal file
122
internal/query/projection/iam.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
"github.com/caos/zitadel/internal/eventstore/handler"
|
||||
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
|
||||
"github.com/caos/zitadel/internal/repository/iam"
|
||||
)
|
||||
|
||||
type IAMProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
IAMProjectionTable = "zitadel.projections.iam"
|
||||
)
|
||||
|
||||
func NewIAMProjection(ctx context.Context, config crdb.StatementHandlerConfig) *IAMProjection {
|
||||
p := &IAMProjection{}
|
||||
config.ProjectionName = IAMProjectionTable
|
||||
config.Reducers = p.reducers()
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *IAMProjection) reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: iam.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
{
|
||||
Event: iam.GlobalOrgSetEventType,
|
||||
Reduce: p.reduceGlobalOrgSet,
|
||||
},
|
||||
{
|
||||
Event: iam.ProjectSetEventType,
|
||||
Reduce: p.reduceIAMProjectSet,
|
||||
},
|
||||
{
|
||||
Event: iam.SetupStartedEventType,
|
||||
Reduce: p.reduceSetupEvent,
|
||||
},
|
||||
{
|
||||
Event: iam.SetupDoneEventType,
|
||||
Reduce: p.reduceSetupEvent,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type IAMColumn string
|
||||
|
||||
const (
|
||||
IAMColumnID = "id"
|
||||
IAMColumnChangeDate = "change_date"
|
||||
IAMColumnGlobalOrgID = "global_org_id"
|
||||
IAMColumnProjectID = "iam_project_id"
|
||||
IAMColumnSequence = "sequence"
|
||||
IAMColumnSetUpStarted = "setup_started"
|
||||
IAMColumnSetUpDone = "setup_done"
|
||||
)
|
||||
|
||||
func (p *IAMProjection) reduceGlobalOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.GlobalOrgSetEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-3n89fs", "seq", event.Sequence(), "expectedType", iam.GlobalOrgSetEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-2n9f2", "reduce.wrong.event.type")
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
handler.NewCol(IAMColumnGlobalOrgID, e.OrgID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *IAMProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.ProjectSetEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-2j9fw", "seq", event.Sequence(), "expectedType", iam.ProjectSetEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-30o0e", "reduce.wrong.event.type")
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
handler.NewCol(IAMColumnProjectID, e.ProjectID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *IAMProjection) reduceSetupEvent(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SetupStepEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-39fjw", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.SetupDoneEventType, iam.SetupStartedEventType}).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-d9nfw", "reduce.wrong.event.type")
|
||||
}
|
||||
columns := []handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
}
|
||||
if e.EventType == iam.SetupStartedEventType {
|
||||
columns = append(columns, handler.NewCol(IAMColumnSetUpStarted, e.Step))
|
||||
} else {
|
||||
columns = append(columns, handler.NewCol(IAMColumnSetUpDone, e.Step))
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
columns,
|
||||
), nil
|
||||
}
|
158
internal/query/projection/iam_test.go
Normal file
158
internal/query/projection/iam_test.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
"github.com/caos/zitadel/internal/eventstore/handler"
|
||||
"github.com/caos/zitadel/internal/eventstore/repository"
|
||||
"github.com/caos/zitadel/internal/repository/iam"
|
||||
)
|
||||
|
||||
func TestIAMProjection_reduces(t *testing.T) {
|
||||
type args struct {
|
||||
event func(t *testing.T) eventstore.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
reduce func(event eventstore.Event) (*handler.Statement, error)
|
||||
want wantReduce
|
||||
}{
|
||||
{
|
||||
name: "reduceGlobalOrgSet",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
repository.EventType(iam.GlobalOrgSetEventType),
|
||||
iam.AggregateType,
|
||||
[]byte(`{"globalOrgId": "orgid"}`),
|
||||
), iam.GlobalOrgSetMapper),
|
||||
},
|
||||
reduce: (&IAMProjection{}).reduceGlobalOrgSet,
|
||||
want: wantReduce{
|
||||
projection: IAMProjectionTable,
|
||||
aggregateType: eventstore.AggregateType("iam"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPSERT INTO zitadel.projections.iam (id, change_date, sequence, global_org_id) VALUES ($1, $2, $3, $4)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
"orgid",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceGlobalOrgSet",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
repository.EventType(iam.ProjectSetEventType),
|
||||
iam.AggregateType,
|
||||
[]byte(`{"iamProjectId": "project-id"}`),
|
||||
), iam.ProjectSetMapper),
|
||||
},
|
||||
reduce: (&IAMProjection{}).reduceIAMProjectSet,
|
||||
want: wantReduce{
|
||||
projection: IAMProjectionTable,
|
||||
aggregateType: eventstore.AggregateType("iam"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPSERT INTO zitadel.projections.iam (id, change_date, sequence, iam_project_id) VALUES ($1, $2, $3, $4)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
"project-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceSetupStarted",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
repository.EventType(iam.SetupStartedEventType),
|
||||
iam.AggregateType,
|
||||
[]byte(`{"Step": 1}`),
|
||||
), iam.SetupStepMapper),
|
||||
},
|
||||
reduce: (&IAMProjection{}).reduceSetupEvent,
|
||||
want: wantReduce{
|
||||
projection: IAMProjectionTable,
|
||||
aggregateType: eventstore.AggregateType("iam"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPSERT INTO zitadel.projections.iam (id, change_date, sequence, setup_started) VALUES ($1, $2, $3, $4)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
domain.Step1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceSetupDone",
|
||||
args: args{
|
||||
event: getEvent(testEvent(
|
||||
repository.EventType(iam.SetupDoneEventType),
|
||||
iam.AggregateType,
|
||||
[]byte(`{"Step": 1}`),
|
||||
), iam.SetupStepMapper),
|
||||
},
|
||||
reduce: (&IAMProjection{}).reduceSetupEvent,
|
||||
want: wantReduce{
|
||||
projection: IAMProjectionTable,
|
||||
aggregateType: eventstore.AggregateType("iam"),
|
||||
sequence: 15,
|
||||
previousSequence: 10,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "UPSERT INTO zitadel.projections.iam (id, change_date, sequence, setup_done) VALUES ($1, $2, $3, $4)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
domain.Step1,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
event := baseEvent(t)
|
||||
got, err := tt.reduce(event)
|
||||
if _, ok := err.(errors.InvalidArgument); !ok {
|
||||
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
||||
}
|
||||
|
||||
event = tt.args.event(t)
|
||||
got, err = tt.reduce(event)
|
||||
assertReduce(t, got, err, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
@@ -67,6 +67,7 @@ func Start(ctx context.Context, sqlClient *sql.DB, es *eventstore.Eventstore, co
|
||||
NewUserGrantProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_grants"]))
|
||||
NewUserMetadataProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_metadata"]))
|
||||
NewUserAuthMethodProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_auth_method"]))
|
||||
NewIAMProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["iam"]))
|
||||
_, err := NewKeyProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["keys"]), defaults.KeyConfig, keyChan)
|
||||
|
||||
return err
|
||||
|
Reference in New Issue
Block a user