2022-02-09 14:01:19 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
2022-03-23 08:02:39 +00:00
|
|
|
"context"
|
2023-03-01 00:11:23 +00:00
|
|
|
"embed"
|
2022-02-09 14:01:19 +00:00
|
|
|
_ "embed"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-03-23 08:02:39 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-11-04 09:21:58 +00:00
|
|
|
"github.com/zitadel/zitadel/cmd/build"
|
2022-06-27 10:32:34 +00:00
|
|
|
"github.com/zitadel/zitadel/cmd/key"
|
|
|
|
"github.com/zitadel/zitadel/cmd/tls"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/migration"
|
2022-11-04 09:21:58 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed steps.yaml
|
|
|
|
defaultSteps []byte
|
2022-04-25 15:05:20 +00:00
|
|
|
stepFiles []string
|
2022-02-09 14:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() *cobra.Command {
|
2022-04-25 15:05:20 +00:00
|
|
|
cmd := &cobra.Command{
|
2022-02-09 14:01:19 +00:00
|
|
|
Use: "setup",
|
|
|
|
Short: "setup ZITADEL instance",
|
|
|
|
Long: `sets up data to start ZITADEL.
|
|
|
|
Requirements:
|
|
|
|
- cockroachdb`,
|
2022-03-23 08:02:39 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-06-24 12:38:22 +00:00
|
|
|
err := tls.ModeFromFlag(cmd)
|
|
|
|
logging.OnError(err).Fatal("invalid tlsMode")
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
config := MustNewConfig(viper.GetViper())
|
|
|
|
steps := MustNewSteps(viper.New())
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
masterKey, err := key.MasterKey(cmd)
|
|
|
|
logging.OnError(err).Panic("No master key provided")
|
|
|
|
|
|
|
|
Setup(config, steps, masterKey)
|
2022-02-09 14:01:19 +00:00
|
|
|
},
|
|
|
|
}
|
2022-04-25 15:05:20 +00:00
|
|
|
|
2023-04-28 11:55:35 +00:00
|
|
|
cmd.AddCommand(NewCleanup())
|
|
|
|
|
2022-04-25 15:05:20 +00:00
|
|
|
Flags(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func Flags(cmd *cobra.Command) {
|
|
|
|
cmd.PersistentFlags().StringArrayVar(&stepFiles, "steps", nil, "paths to step files to overwrite default steps")
|
|
|
|
key.AddMasterKeyFlag(cmd)
|
2022-06-24 12:38:22 +00:00
|
|
|
tls.AddTLSModeFlag(cmd)
|
2022-02-09 14:01:19 +00:00
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func Setup(config *Config, steps *Steps, masterKey string) {
|
2022-11-04 09:21:58 +00:00
|
|
|
ctx := context.Background()
|
2022-07-20 09:20:49 +00:00
|
|
|
logging.Info("setup started")
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
dbClient, err := database.Connect(config.Database, false)
|
2022-03-23 08:02:39 +00:00
|
|
|
logging.OnError(err).Fatal("unable to connect to database")
|
|
|
|
|
2022-12-15 09:40:13 +00:00
|
|
|
eventstoreClient, err := eventstore.Start(&eventstore.Config{Client: dbClient})
|
2022-03-23 08:02:39 +00:00
|
|
|
logging.OnError(err).Fatal("unable to start eventstore")
|
2022-03-28 08:05:09 +00:00
|
|
|
migration.RegisterMappers(eventstoreClient)
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
steps.s1ProjectionTable = &ProjectionTable{dbClient: dbClient.DB}
|
|
|
|
steps.s2AssetsTable = &AssetTable{dbClient: dbClient.DB}
|
2022-04-13 05:42:48 +00:00
|
|
|
|
2022-07-27 08:22:20 +00:00
|
|
|
steps.FirstInstance.instanceSetup = config.DefaultInstance
|
|
|
|
steps.FirstInstance.userEncryptionKey = config.EncryptionKeys.User
|
|
|
|
steps.FirstInstance.smtpEncryptionKey = config.EncryptionKeys.SMTP
|
2023-06-15 06:16:39 +00:00
|
|
|
steps.FirstInstance.oidcEncryptionKey = config.EncryptionKeys.OIDC
|
2022-07-27 08:22:20 +00:00
|
|
|
steps.FirstInstance.masterKey = masterKey
|
2023-02-27 21:36:43 +00:00
|
|
|
steps.FirstInstance.db = dbClient.DB
|
2022-07-27 08:22:20 +00:00
|
|
|
steps.FirstInstance.es = eventstoreClient
|
|
|
|
steps.FirstInstance.defaults = config.SystemDefaults
|
|
|
|
steps.FirstInstance.zitadelRoles = config.InternalAuthZ.RolePermissionMappings
|
|
|
|
steps.FirstInstance.externalDomain = config.ExternalDomain
|
|
|
|
steps.FirstInstance.externalSecure = config.ExternalSecure
|
|
|
|
steps.FirstInstance.externalPort = config.ExternalPort
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2023-03-01 00:11:23 +00:00
|
|
|
steps.s4EventstoreIndexes = New04(dbClient)
|
2023-02-27 21:36:43 +00:00
|
|
|
steps.s5LastFailed = &LastFailed{dbClient: dbClient.DB}
|
|
|
|
steps.s6OwnerRemoveColumns = &OwnerRemoveColumns{dbClient: dbClient.DB}
|
|
|
|
steps.s7LogstoreTables = &LogstoreTables{dbClient: dbClient.DB, username: config.Database.Username(), dbType: config.Database.Type()}
|
2023-03-01 00:11:23 +00:00
|
|
|
steps.s8AuthTokens = &AuthTokenIndexes{dbClient: dbClient}
|
|
|
|
steps.s9EventstoreIndexes2 = New09(dbClient)
|
2023-04-25 08:12:53 +00:00
|
|
|
steps.CorrectCreationDate.dbClient = dbClient
|
2023-06-07 16:30:19 +00:00
|
|
|
steps.AddEventCreatedAt.dbClient = dbClient
|
|
|
|
steps.AddEventCreatedAt.step10 = steps.CorrectCreationDate
|
2022-09-02 14:05:13 +00:00
|
|
|
|
2022-11-04 09:21:58 +00:00
|
|
|
err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil)
|
|
|
|
logging.OnError(err).Fatal("unable to start projections")
|
|
|
|
|
2022-07-20 09:20:49 +00:00
|
|
|
repeatableSteps := []migration.RepeatableMigration{
|
|
|
|
&externalConfigChange{
|
|
|
|
es: eventstoreClient,
|
|
|
|
ExternalDomain: config.ExternalDomain,
|
|
|
|
ExternalPort: config.ExternalPort,
|
|
|
|
ExternalSecure: config.ExternalSecure,
|
|
|
|
},
|
2022-11-04 09:21:58 +00:00
|
|
|
&projectionTables{
|
|
|
|
es: eventstoreClient,
|
|
|
|
Version: build.Version(),
|
|
|
|
},
|
2022-07-20 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 10:37:39 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s1ProjectionTable)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 1")
|
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s2AssetsTable)
|
2022-04-28 08:30:41 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 2")
|
2022-07-27 08:22:20 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.FirstInstance)
|
2022-04-28 08:30:41 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 3")
|
2022-09-02 14:05:13 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s4EventstoreIndexes)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 4")
|
2022-11-18 12:49:38 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s5LastFailed)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 5")
|
2022-11-30 16:01:17 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s6OwnerRemoveColumns)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 6")
|
2023-02-15 01:52:11 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s7LogstoreTables)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 7")
|
2023-02-21 14:46:47 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s8AuthTokens)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 8")
|
2023-03-01 00:11:23 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.s9EventstoreIndexes2)
|
|
|
|
logging.OnError(err).Fatal("unable to migrate step 9")
|
2023-04-25 08:12:53 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.CorrectCreationDate)
|
2023-04-18 17:29:04 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 10")
|
2023-06-07 16:30:19 +00:00
|
|
|
err = migration.Migrate(ctx, eventstoreClient, steps.AddEventCreatedAt)
|
2023-05-11 08:04:35 +00:00
|
|
|
logging.OnError(err).Fatal("unable to migrate step 11")
|
2022-04-25 15:05:20 +00:00
|
|
|
|
2022-07-20 09:20:49 +00:00
|
|
|
for _, repeatableStep := range repeatableSteps {
|
|
|
|
err = migration.Migrate(ctx, eventstoreClient, repeatableStep)
|
|
|
|
logging.OnError(err).Fatalf("unable to migrate repeatable step: %s", repeatableStep.String())
|
2022-04-25 15:05:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-01 00:11:23 +00:00
|
|
|
|
|
|
|
func readStmt(fs embed.FS, folder, typ, filename string) (string, error) {
|
|
|
|
stmt, err := fs.ReadFile(folder + "/" + typ + "/" + filename)
|
|
|
|
return string(stmt), err
|
|
|
|
}
|