mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:37:31 +00:00
fix: multiple setup steps (#773)
* fix: multiple setup steps * fix: test set up started * fix: possible nil pointers in setup * fix: validate executed step
This commit is contained in:
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/caos/logging"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
@@ -12,10 +13,19 @@ const (
|
||||
IAMVersion = "v1"
|
||||
)
|
||||
|
||||
type Step int
|
||||
|
||||
const (
|
||||
Step1 = Step(model.Step1)
|
||||
//TODO: label policy
|
||||
// Step2 = Step(model.Step2)
|
||||
StepCount = Step(model.StepCount)
|
||||
)
|
||||
|
||||
type IAM struct {
|
||||
es_models.ObjectRoot
|
||||
SetUpStarted bool `json:"-"`
|
||||
SetUpDone bool `json:"-"`
|
||||
SetUpStarted Step `json:"-"`
|
||||
SetUpDone Step `json:"-"`
|
||||
GlobalOrgID string `json:"globalOrgId,omitempty"`
|
||||
IAMProjectID string `json:"iamProjectId,omitempty"`
|
||||
Members []*IAMMember `json:"-"`
|
||||
@@ -28,8 +38,8 @@ func IAMFromModel(iam *model.IAM) *IAM {
|
||||
idps := IDPConfigsFromModel(iam.IDPs)
|
||||
converted := &IAM{
|
||||
ObjectRoot: iam.ObjectRoot,
|
||||
SetUpStarted: iam.SetUpStarted,
|
||||
SetUpDone: iam.SetUpDone,
|
||||
SetUpStarted: Step(iam.SetUpStarted),
|
||||
SetUpDone: Step(iam.SetUpDone),
|
||||
GlobalOrgID: iam.GlobalOrgID,
|
||||
IAMProjectID: iam.IAMProjectID,
|
||||
Members: members,
|
||||
@@ -46,8 +56,8 @@ func IAMToModel(iam *IAM) *model.IAM {
|
||||
idps := IDPConfigsToModel(iam.IDPs)
|
||||
converted := &model.IAM{
|
||||
ObjectRoot: iam.ObjectRoot,
|
||||
SetUpStarted: iam.SetUpStarted,
|
||||
SetUpDone: iam.SetUpDone,
|
||||
SetUpStarted: model.Step(iam.SetUpStarted),
|
||||
SetUpDone: model.Step(iam.SetUpDone),
|
||||
GlobalOrgID: iam.GlobalOrgID,
|
||||
IAMProjectID: iam.IAMProjectID,
|
||||
Members: members,
|
||||
@@ -72,9 +82,27 @@ func (i *IAM) AppendEvent(event *es_models.Event) (err error) {
|
||||
i.ObjectRoot.AppendEvent(event)
|
||||
switch event.Type {
|
||||
case IAMSetupStarted:
|
||||
i.SetUpStarted = true
|
||||
if len(event.Data) == 0 {
|
||||
i.SetUpStarted = Step(model.Step1)
|
||||
return
|
||||
}
|
||||
step := new(struct{ Step Step })
|
||||
err = json.Unmarshal(event.Data, step)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.SetUpStarted = step.Step
|
||||
case IAMSetupDone:
|
||||
i.SetUpDone = true
|
||||
if len(event.Data) == 0 {
|
||||
i.SetUpDone = Step(model.Step1)
|
||||
return
|
||||
}
|
||||
step := new(struct{ Step Step })
|
||||
err = json.Unmarshal(event.Data, step)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
i.SetUpDone = step.Step
|
||||
case IAMProjectSet,
|
||||
GlobalOrgSet:
|
||||
err = i.SetData(event)
|
||||
|
@@ -2,8 +2,9 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
"testing"
|
||||
|
||||
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
||||
)
|
||||
|
||||
func mockIamData(iam *IAM) []byte {
|
||||
@@ -27,31 +28,31 @@ func TestProjectRoleAppendEvent(t *testing.T) {
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: IAMSetupStarted, ResourceOwner: "OrgID"},
|
||||
iam: &IAM{},
|
||||
},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1},
|
||||
},
|
||||
{
|
||||
name: "append set up done event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: IAMSetupDone, ResourceOwner: "OrgID"},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1},
|
||||
},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true, SetUpDone: true},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1, SetUpDone: Step1},
|
||||
},
|
||||
{
|
||||
name: "append globalorg event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: GlobalOrgSet, ResourceOwner: "OrgID", Data: mockIamData(&IAM{GlobalOrgID: "GlobalOrg"})},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1},
|
||||
},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true, GlobalOrgID: "GlobalOrg"},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1, GlobalOrgID: "GlobalOrg"},
|
||||
},
|
||||
{
|
||||
name: "append iamproject event",
|
||||
args: args{
|
||||
event: &es_models.Event{AggregateID: "AggregateID", Sequence: 1, Type: IAMProjectSet, ResourceOwner: "OrgID", Data: mockIamData(&IAM{IAMProjectID: "IamProject"})},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true},
|
||||
iam: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1},
|
||||
},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: true, IAMProjectID: "IamProject"},
|
||||
result: &IAM{ObjectRoot: es_models.ObjectRoot{AggregateID: "AggregateID"}, SetUpStarted: Step1, IAMProjectID: "IamProject"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
Reference in New Issue
Block a user