mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-23 13:28:45 +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:
+5
-172
@@ -1,7 +1,6 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
||||
"github.com/caos/zitadel/internal/iam/model"
|
||||
@@ -9,177 +8,11 @@ import (
|
||||
|
||||
func readModelToIAM(readModel *ReadModel) *model.IAM {
|
||||
return &model.IAM{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.ReadModel),
|
||||
GlobalOrgID: readModel.GlobalOrgID,
|
||||
IAMProjectID: readModel.ProjectID,
|
||||
SetUpDone: readModel.SetUpDone,
|
||||
SetUpStarted: readModel.SetUpStarted,
|
||||
Members: readModelToMembers(&readModel.Members),
|
||||
DefaultLabelPolicy: readModelToLabelPolicy(&readModel.DefaultLabelPolicy),
|
||||
DefaultLoginPolicy: readModelToLoginPolicy(&readModel.DefaultLoginPolicy),
|
||||
DefaultOrgIAMPolicy: readModelToOrgIAMPolicy(&readModel.DefaultOrgIAMPolicy),
|
||||
DefaultPasswordAgePolicy: readModelToPasswordAgePolicy(&readModel.DefaultPasswordAgePolicy),
|
||||
DefaultPasswordComplexityPolicy: readModelToPasswordComplexityPolicy(&readModel.DefaultPasswordComplexityPolicy),
|
||||
DefaultLockoutPolicy: readModelToPasswordLockoutPolicy(&readModel.DefaultPasswordLockoutPolicy),
|
||||
IDPs: readModelToIDPConfigs(&readModel.IDPs),
|
||||
}
|
||||
}
|
||||
|
||||
func readModelToIDPConfigView(rm *IAMIDPConfigReadModel) *domain.IDPConfigView {
|
||||
converted := &domain.IDPConfigView{
|
||||
AggregateID: rm.AggregateID,
|
||||
ChangeDate: rm.ChangeDate,
|
||||
CreationDate: rm.CreationDate,
|
||||
IDPConfigID: rm.ConfigID,
|
||||
IDPProviderType: rm.ProviderType,
|
||||
IsOIDC: rm.OIDCConfig != nil,
|
||||
Name: rm.Name,
|
||||
Sequence: rm.ProcessedSequence,
|
||||
State: rm.State,
|
||||
StylingType: rm.StylingType,
|
||||
}
|
||||
if rm.OIDCConfig != nil {
|
||||
converted.OIDCClientID = rm.OIDCConfig.ClientID
|
||||
converted.OIDCClientSecret = rm.OIDCConfig.ClientSecret
|
||||
converted.OIDCIDPDisplayNameMapping = rm.OIDCConfig.IDPDisplayNameMapping
|
||||
converted.OIDCIssuer = rm.OIDCConfig.Issuer
|
||||
converted.OIDCScopes = rm.OIDCConfig.Scopes
|
||||
converted.OIDCUsernameMapping = rm.OIDCConfig.UserNameMapping
|
||||
converted.OAuthAuthorizationEndpoint = rm.OIDCConfig.AuthorizationEndpoint
|
||||
converted.OAuthTokenEndpoint = rm.OIDCConfig.TokenEndpoint
|
||||
}
|
||||
if rm.JWTConfig != nil {
|
||||
converted.JWTEndpoint = rm.JWTConfig.JWTEndpoint
|
||||
converted.JWTIssuer = rm.JWTConfig.Issuer
|
||||
converted.JWTKeysEndpoint = rm.JWTConfig.KeysEndpoint
|
||||
}
|
||||
return converted
|
||||
}
|
||||
|
||||
func readModelToMember(readModel *MemberReadModel) *model.IAMMember {
|
||||
return &model.IAMMember{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.ReadModel),
|
||||
Roles: readModel.Roles,
|
||||
UserID: readModel.UserID,
|
||||
}
|
||||
}
|
||||
|
||||
func readModelToMembers(readModel *IAMMembersReadModel) []*model.IAMMember {
|
||||
members := make([]*model.IAMMember, len(readModel.Members))
|
||||
|
||||
for i, member := range readModel.Members {
|
||||
members[i] = &model.IAMMember{
|
||||
ObjectRoot: readModelToObjectRoot(member.ReadModel),
|
||||
Roles: member.Roles,
|
||||
UserID: member.UserID,
|
||||
}
|
||||
}
|
||||
|
||||
return members
|
||||
}
|
||||
|
||||
func readModelToLabelPolicy(readModel *IAMLabelPolicyReadModel) *model.LabelPolicy {
|
||||
return &model.LabelPolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.LabelPolicyReadModel.ReadModel),
|
||||
PrimaryColor: readModel.PrimaryColor,
|
||||
BackgroundColor: readModel.BackgroundColor,
|
||||
WarnColor: readModel.WarnColor,
|
||||
FontColor: readModel.FontColor,
|
||||
PrimaryColorDark: readModel.PrimaryColorDark,
|
||||
BackgroundColorDark: readModel.BackgroundColorDark,
|
||||
WarnColorDark: readModel.WarnColorDark,
|
||||
FontColorDark: readModel.FontColorDark,
|
||||
Default: true,
|
||||
}
|
||||
}
|
||||
|
||||
func readModelToLoginPolicy(readModel *IAMLoginPolicyReadModel) *model.LoginPolicy {
|
||||
return &model.LoginPolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.LoginPolicyReadModel.ReadModel),
|
||||
AllowExternalIdp: readModel.AllowExternalIDP,
|
||||
AllowRegister: readModel.AllowRegister,
|
||||
AllowUsernamePassword: readModel.AllowUserNamePassword,
|
||||
Default: true,
|
||||
}
|
||||
}
|
||||
func readModelToOrgIAMPolicy(readModel *IAMOrgIAMPolicyReadModel) *model.OrgIAMPolicy {
|
||||
return &model.OrgIAMPolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.OrgIAMPolicyReadModel.ReadModel),
|
||||
UserLoginMustBeDomain: readModel.UserLoginMustBeDomain,
|
||||
Default: true,
|
||||
}
|
||||
}
|
||||
func readModelToPasswordAgePolicy(readModel *IAMPasswordAgePolicyReadModel) *model.PasswordAgePolicy {
|
||||
return &model.PasswordAgePolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.PasswordAgePolicyReadModel.ReadModel),
|
||||
ExpireWarnDays: readModel.ExpireWarnDays,
|
||||
MaxAgeDays: readModel.MaxAgeDays,
|
||||
}
|
||||
}
|
||||
func readModelToPasswordComplexityPolicy(readModel *IAMPasswordComplexityPolicyReadModel) *model.PasswordComplexityPolicy {
|
||||
return &model.PasswordComplexityPolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.PasswordComplexityPolicyReadModel.ReadModel),
|
||||
HasLowercase: readModel.HasLowercase,
|
||||
HasNumber: readModel.HasNumber,
|
||||
HasSymbol: readModel.HasSymbol,
|
||||
HasUppercase: readModel.HasUpperCase,
|
||||
MinLength: readModel.MinLength,
|
||||
}
|
||||
}
|
||||
func readModelToPasswordLockoutPolicy(readModel *IAMLockoutPolicyReadModel) *model.LockoutPolicy {
|
||||
return &model.LockoutPolicy{
|
||||
ObjectRoot: readModelToObjectRoot(readModel.LockoutPolicyReadModel.ReadModel),
|
||||
MaxPasswordAttempts: readModel.MaxAttempts,
|
||||
ShowLockOutFailures: readModel.ShowLockOutFailures,
|
||||
}
|
||||
}
|
||||
|
||||
func readModelToIDPConfigs(rm *IAMIDPConfigsReadModel) []*model.IDPConfig {
|
||||
configs := make([]*model.IDPConfig, len(rm.Configs))
|
||||
for i, config := range rm.Configs {
|
||||
configs[i] = readModelToIDPConfig(&IAMIDPConfigReadModel{IDPConfigReadModel: *config})
|
||||
}
|
||||
return configs
|
||||
}
|
||||
|
||||
func readModelToIDPConfig(rm *IAMIDPConfigReadModel) *model.IDPConfig {
|
||||
config := &model.IDPConfig{
|
||||
ObjectRoot: readModelToObjectRoot(rm.ReadModel),
|
||||
IDPConfigID: rm.ConfigID,
|
||||
Name: rm.Name,
|
||||
State: model.IDPConfigState(rm.State),
|
||||
StylingType: model.IDPStylingType(rm.StylingType),
|
||||
}
|
||||
if rm.OIDCConfig != nil {
|
||||
config.OIDCConfig = readModelToIDPOIDCConfig(rm.OIDCConfig)
|
||||
}
|
||||
if rm.JWTConfig != nil {
|
||||
config.JWTIDPConfig = readModelToIDPJWTConfig(rm.JWTConfig)
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
func readModelToIDPOIDCConfig(rm *OIDCConfigReadModel) *model.OIDCIDPConfig {
|
||||
return &model.OIDCIDPConfig{
|
||||
ObjectRoot: readModelToObjectRoot(rm.ReadModel),
|
||||
ClientID: rm.ClientID,
|
||||
ClientSecret: rm.ClientSecret,
|
||||
ClientSecretString: string(rm.ClientSecret.Crypted),
|
||||
IDPConfigID: rm.IDPConfigID,
|
||||
IDPDisplayNameMapping: model.OIDCMappingField(rm.IDPDisplayNameMapping),
|
||||
Issuer: rm.Issuer,
|
||||
Scopes: rm.Scopes,
|
||||
UsernameMapping: model.OIDCMappingField(rm.UserNameMapping),
|
||||
}
|
||||
}
|
||||
|
||||
func readModelToIDPJWTConfig(rm *JWTConfigReadModel) *model.JWTIDPConfig {
|
||||
return &model.JWTIDPConfig{
|
||||
ObjectRoot: readModelToObjectRoot(rm.ReadModel),
|
||||
IDPConfigID: rm.IDPConfigID,
|
||||
JWTEndpoint: rm.JWTEndpoint,
|
||||
Issuer: rm.Issuer,
|
||||
KeysEndpoint: rm.KeysEndpoint,
|
||||
ObjectRoot: readModelToObjectRoot(readModel.ReadModel),
|
||||
GlobalOrgID: readModel.GlobalOrgID,
|
||||
IAMProjectID: readModel.ProjectID,
|
||||
SetUpDone: readModel.SetUpDone,
|
||||
SetUpStarted: readModel.SetUpStarted,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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