2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-06 10:47:55 +01:00
|
|
|
"github.com/caos/logging"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-06 10:47:55 +01:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 14:52:13 +01:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
type Step interface {
|
|
|
|
Step() domain.Step
|
2021-02-24 11:17:39 +01:00
|
|
|
execute(context.Context, *Commands) error
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
SetupUser = "SETUP"
|
|
|
|
)
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) ExecuteSetupSteps(ctx context.Context, steps []Step) error {
|
|
|
|
iam, err := c.GetIAM(ctx)
|
2021-01-06 10:47:55 +01: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 12:59:51 +01:00
|
|
|
iam = &domain.IAM{ObjectRoot: models.ObjectRoot{}}
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
ctx = setSetUpContextData(ctx)
|
2021-01-06 10:47:55 +01:00
|
|
|
|
|
|
|
for _, step := range steps {
|
2021-02-24 11:17:39 +01:00
|
|
|
iam, err = c.StartSetup(ctx, step.Step())
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
err = step.execute(ctx, c)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func setSetUpContextData(ctx context.Context) context.Context {
|
|
|
|
return authz.SetCtxData(ctx, authz.CtxData{UserID: SetupUser})
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) StartSetup(ctx context.Context, step domain.Step) (*domain.IAM, error) {
|
2021-03-30 10:59:16 +02:00
|
|
|
logging.Log("COMMA-3M9fs").Infof("start setup step %v", step)
|
2021-02-24 11:17:39 +01:00
|
|
|
iamWriteModel, err := c.getIAMWriteModel(ctx)
|
2021-01-04 14:52:13 +01: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 14:48:27 +01:00
|
|
|
aggregate := IAMAggregateFromWriteModel(&iamWriteModel.WriteModel)
|
2021-02-24 11:17:39 +01:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, iam_repo.NewSetupStepStartedEvent(ctx, aggregate, step))
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-Grgh1", "Setup start failed")
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
err = AppendAndReduce(iamWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-12 12:59:51 +01:00
|
|
|
logging.LogWithFields("SETUP-fhh21", "step", step).Info("setup step started")
|
2021-01-04 14:52:13 +01:00
|
|
|
return writeModelToIAM(iamWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) setup(ctx context.Context, step Step, iamAggregateProvider func(*IAMWriteModel) ([]eventstore.EventPusher, error)) error {
|
|
|
|
iam, err := c.getIAMWriteModel(ctx)
|
2021-01-06 10:47:55 +01: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 14:48:27 +01:00
|
|
|
events, err := iamAggregateProvider(iam)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&iam.WriteModel)
|
|
|
|
events = append(events, iam_repo.NewSetupStepDoneEvent(ctx, iamAgg, step.Step()))
|
2021-01-06 10:47:55 +01:00
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, events...)
|
2021-04-01 10:14:07 +02:00
|
|
|
if caos_errs.IsErrorAlreadyExists(err) {
|
|
|
|
logging.LogWithFields("SETUP-4M9gsf", "step", step.Step()).WithError(err).Info("setup step already done")
|
|
|
|
}
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
2021-01-21 10:49:38 +01:00
|
|
|
return caos_errs.ThrowPreconditionFailedf(nil, "EVENT-dbG31", "Setup %v failed", step.Step())
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
2021-01-12 12:59:51 +01:00
|
|
|
logging.LogWithFields("SETUP-Sg1t1", "step", step.Step()).Info("setup step done")
|
2021-01-06 10:47:55 +01:00
|
|
|
return nil
|
|
|
|
}
|