2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-06 09:47:55 +00:00
|
|
|
"github.com/caos/logging"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-06 09:47:55 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate := IAMAggregateFromWriteModel(&iamWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, iam_repo.NewSetupStepStartedEvent(ctx, aggregate, step))
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-Grgh1", "Setup start failed")
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(iamWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-02-18 13:48:27 +00:00
|
|
|
func (r *CommandSide) setup(ctx context.Context, step Step, iamAggregateProvider func(*IAMWriteModel) ([]eventstore.EventPusher, 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")
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
events, err := iamAggregateProvider(iam)
|
2021-01-06 09:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&iam.WriteModel)
|
|
|
|
events = append(events, iam_repo.NewSetupStepDoneEvent(ctx, iamAgg, step.Step()))
|
2021-01-06 09:47:55 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
_, err = r.eventstore.PushEvents(ctx, events...)
|
2021-01-06 09:47:55 +00:00
|
|
|
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
|
|
|
|
}
|