2022-02-09 15:01:19 +01:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-04 17:12:20 +01:00
|
|
|
"context"
|
2022-08-31 09:52:43 +02:00
|
|
|
"embed"
|
2022-02-09 15:01:19 +01:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-02-11 11:02:47 +01:00
|
|
|
"github.com/spf13/viper"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-11 11:02:47 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-02-11 11:02:47 +01:00
|
|
|
)
|
|
|
|
|
|
2022-08-31 09:52:43 +02:00
|
|
|
var (
|
2025-04-02 16:53:06 +02:00
|
|
|
//go:embed sql/*.sql
|
2022-08-31 09:52:43 +02:00
|
|
|
stmts embed.FS
|
|
|
|
|
|
|
|
|
|
createUserStmt string
|
|
|
|
|
grantStmt string
|
|
|
|
|
databaseStmt string
|
|
|
|
|
createEventstoreStmt string
|
|
|
|
|
createProjectionsStmt string
|
|
|
|
|
createSystemStmt string
|
|
|
|
|
createEncryptionKeysStmt string
|
|
|
|
|
createEventsStmt string
|
|
|
|
|
createUniqueConstraints string
|
|
|
|
|
|
|
|
|
|
roleAlreadyExistsCode = "42710"
|
|
|
|
|
dbAlreadyExistsCode = "42P04"
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-09 15:01:19 +01:00
|
|
|
func New() *cobra.Command {
|
2022-02-11 11:02:47 +01:00
|
|
|
cmd := &cobra.Command{
|
2022-02-09 15:01:19 +01:00
|
|
|
Use: "init",
|
|
|
|
|
Short: "initialize ZITADEL instance",
|
2022-02-11 11:52:50 +01:00
|
|
|
Long: `Sets up the minimum requirements to start ZITADEL.
|
|
|
|
|
|
2023-02-15 02:52:11 +01:00
|
|
|
Prerequisites:
|
2025-04-02 16:53:06 +02:00
|
|
|
- PostgreSql database
|
2022-02-11 11:52:50 +01:00
|
|
|
|
2022-05-24 15:57:57 +01:00
|
|
|
The user provided by flags needs privileges to
|
2022-02-11 11:52:50 +01:00
|
|
|
- create the database if it does not exist
|
|
|
|
|
- see other users and create a new one if the user does not exist
|
|
|
|
|
- grant all rights of the ZITADEL database to the user created if not yet set
|
|
|
|
|
`,
|
2022-03-28 10:05:09 +02:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
config := MustNewConfig(viper.GetViper())
|
2022-02-16 13:30:49 +01:00
|
|
|
|
2024-01-04 17:12:20 +01:00
|
|
|
InitAll(cmd.Context(), config)
|
2022-02-09 15:01:19 +01:00
|
|
|
},
|
|
|
|
|
}
|
2022-02-11 11:02:47 +01:00
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
cmd.AddCommand(newZitadel(), newDatabase(), newUser(), newGrant())
|
2022-02-11 11:02:47 +01:00
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 17:12:20 +01:00
|
|
|
func InitAll(ctx context.Context, config *Config) {
|
2024-12-04 14:51:40 +01:00
|
|
|
err := initialise(ctx, config.Database,
|
2022-07-28 16:25:42 +02:00
|
|
|
VerifyUser(config.Database.Username(), config.Database.Password()),
|
2023-02-27 22:36:43 +01:00
|
|
|
VerifyDatabase(config.Database.DatabaseName()),
|
|
|
|
|
VerifyGrant(config.Database.DatabaseName(), config.Database.Username()),
|
2022-03-28 10:05:09 +02:00
|
|
|
)
|
|
|
|
|
logging.OnError(err).Fatal("unable to initialize the database")
|
|
|
|
|
|
2024-01-04 17:12:20 +01:00
|
|
|
err = verifyZitadel(ctx, config.Database)
|
2022-03-28 10:05:09 +02:00
|
|
|
logging.OnError(err).Fatal("unable to initialize ZITADEL")
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 14:51:40 +01:00
|
|
|
func initialise(ctx context.Context, config database.Config, steps ...func(context.Context, *database.DB) error) error {
|
2022-02-11 11:02:47 +01:00
|
|
|
logging.Info("initialization started")
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
err := ReadStmts()
|
2022-02-11 14:07:32 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-08-31 09:52:43 +02:00
|
|
|
|
2025-01-16 12:07:18 +01:00
|
|
|
db, err := database.Connect(config, true)
|
2022-03-15 07:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-08-31 09:52:43 +02:00
|
|
|
defer db.Close()
|
|
|
|
|
|
2024-12-04 14:51:40 +01:00
|
|
|
return Init(ctx, db, steps...)
|
2022-03-15 07:19:02 +01:00
|
|
|
}
|
2022-02-11 14:07:32 +01:00
|
|
|
|
2024-12-04 14:51:40 +01:00
|
|
|
func Init(ctx context.Context, db *database.DB, steps ...func(context.Context, *database.DB) error) error {
|
2022-02-11 14:07:32 +01:00
|
|
|
for _, step := range steps {
|
2024-12-04 14:51:40 +01:00
|
|
|
if err := step(ctx, db); err != nil {
|
2022-02-11 14:07:32 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-31 09:52:43 +02:00
|
|
|
|
2022-03-15 07:19:02 +01:00
|
|
|
return nil
|
2022-02-09 15:01:19 +01:00
|
|
|
}
|
2022-08-31 09:52:43 +02:00
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
func ReadStmts() (err error) {
|
|
|
|
|
createUserStmt, err = readStmt("01_user")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
databaseStmt, err = readStmt("02_database")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
grantStmt, err = readStmt("03_grant_user")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createEventstoreStmt, err = readStmt("04_eventstore")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createProjectionsStmt, err = readStmt("05_projections")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createSystemStmt, err = readStmt("06_system")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createEncryptionKeysStmt, err = readStmt("07_encryption_keys_table")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createEventsStmt, err = readStmt("08_events_table")
|
2022-08-31 09:52:43 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
createUniqueConstraints, err = readStmt("10_unique_constraints_table")
|
2024-05-27 11:03:34 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 09:52:43 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 16:53:06 +02:00
|
|
|
func readStmt(step string) (string, error) {
|
|
|
|
|
stmt, err := stmts.ReadFile("sql/" + step + ".sql")
|
2022-08-31 09:52:43 +02:00
|
|
|
return string(stmt), err
|
|
|
|
|
}
|