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