2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-01-06 09:47:55 +00:00
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-01-06 09:47:55 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
|
|
)
|
|
|
|
|
2021-01-06 09:47:55 +00:00
|
|
|
type Step interface {
|
|
|
|
Step() domain.Step
|
|
|
|
execute(context.Context, *CommandSide) error
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
SetupUser = "SETUP"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *CommandSide) ExecuteSetupSteps(ctx context.Context, steps []Step) error {
|
2021-01-12 11:59:51 +00:00
|
|
|
iam, err := r.GetIAM(ctx)
|
2021-01-06 09:47:55 +00:00
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iam != nil && (iam.SetUpDone == domain.StepCount-1 || iam.SetUpStarted != iam.SetUpDone) {
|
|
|
|
logging.Log("COMMA-dgd2z").Info("all steps done")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if iam == nil {
|
2021-01-12 11:59:51 +00:00
|
|
|
iam = &domain.IAM{ObjectRoot: models.ObjectRoot{}}
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
ctx = setSetUpContextData(ctx)
|
2021-01-06 09:47:55 +00:00
|
|
|
|
|
|
|
for _, step := range steps {
|
2021-01-12 11:59:51 +00:00
|
|
|
iam, err = r.StartSetup(ctx, step.Step())
|
2021-01-06 09:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = step.execute(ctx, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func setSetUpContextData(ctx context.Context) context.Context {
|
|
|
|
return authz.SetCtxData(ctx, authz.CtxData{UserID: SetupUser})
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func (r *CommandSide) StartSetup(ctx context.Context, step domain.Step) (*domain.IAM, error) {
|
|
|
|
iamWriteModel, err := r.getIAMWriteModel(ctx)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if iamWriteModel.SetUpStarted >= step || iamWriteModel.SetUpStarted != iamWriteModel.SetUpDone {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-9so34", "setup error")
|
|
|
|
}
|
|
|
|
aggregate := IAMAggregateFromWriteModel(&iamWriteModel.WriteModel).PushEvents(iam_repo.NewSetupStepStartedEvent(ctx, step))
|
|
|
|
err = r.eventstore.PushAggregate(ctx, iamWriteModel, aggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-Grgh1", "Setup start failed")
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
logging.LogWithFields("SETUP-fhh21", "step", step).Info("setup step started")
|
2021-01-04 13:52:13 +00:00
|
|
|
return writeModelToIAM(iamWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-01-06 09:47:55 +00:00
|
|
|
func (r *CommandSide) setup(ctx context.Context, step Step, iamAggregateProvider func(*IAMWriteModel) (*iam_repo.Aggregate, error)) error {
|
2021-01-12 11:59:51 +00:00
|
|
|
iam, err := r.getIAMWriteModel(ctx)
|
2021-01-06 09:47:55 +00:00
|
|
|
if err != nil && !caos_errs.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if iam.SetUpStarted != step.Step() && iam.SetUpDone+1 != step.Step() {
|
|
|
|
return caos_errs.ThrowPreconditionFailed(nil, "EVENT-Dge32", "wrong step")
|
|
|
|
}
|
|
|
|
iamAgg, err := iamAggregateProvider(iam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iamAgg.PushEvents(iam_repo.NewSetupStepDoneEvent(ctx, step.Step()))
|
|
|
|
|
|
|
|
_, err = r.eventstore.PushAggregates(ctx, iamAgg)
|
|
|
|
if err != nil {
|
2021-01-21 09:49:38 +00:00
|
|
|
return caos_errs.ThrowPreconditionFailedf(nil, "EVENT-dbG31", "Setup %v failed", step.Step())
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
logging.LogWithFields("SETUP-Sg1t1", "step", step.Step()).Info("setup step done")
|
2021-01-06 09:47:55 +00:00
|
|
|
return nil
|
|
|
|
}
|