mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-28 23:18:54 +00:00
fix: init sub commands (#3218)
* fix(init): add sub commands * fix(init): admin user in config, test(init): verify functions * refactor: config, remove second commands * refactor: init steps * chore: fix link in readme * chore: numerate sql files * Update cmd/admin/initialise/sql/README.md Co-authored-by: Livio Amstutz <[email protected]> * Update cmd/admin/initialise/sql/README.md Co-authored-by: Livio Amstutz <[email protected]> * fix(init): remove unused index * user * fix database username in defaults.yaml Co-authored-by: Livio Amstutz <[email protected]>
This commit is contained in:
@@ -2,6 +2,7 @@ package initialise
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/database"
|
||||
@@ -9,6 +10,12 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
searchUser = "SELECT username FROM [show roles] WHERE username = $1"
|
||||
//go:embed sql/01_user.sql
|
||||
createUserStmt string
|
||||
)
|
||||
|
||||
func newUser() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "user",
|
||||
@@ -24,31 +31,21 @@ The user provided by flags needs priviledge to
|
||||
- grant all rights of the ZITADEL database to the user created if not yet set
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
config := new(Config)
|
||||
if err := viper.Unmarshal(config); err != nil {
|
||||
config := Config{}
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
return err
|
||||
}
|
||||
return initialise(config, verifyUser)
|
||||
return initialise(config, verifyUser(config.Database))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func verifyUser(db *sql.DB, config database.Config) error {
|
||||
logging.Info("verify user")
|
||||
exists, err := existsUser(db, config)
|
||||
if exists || err != nil {
|
||||
return err
|
||||
func verifyUser(config database.Config) func(*sql.DB) error {
|
||||
return func(db *sql.DB) error {
|
||||
logging.WithFields("username", config.Username).Info("verify user")
|
||||
return verify(db,
|
||||
exists(searchUser, config.Username),
|
||||
exec(createUserStmt, config.Username, &sql.NullString{String: config.Password, Valid: config.Password != ""}),
|
||||
)
|
||||
}
|
||||
return createUser(db, config)
|
||||
}
|
||||
|
||||
func existsUser(db *sql.DB, config database.Config) (exists bool, err error) {
|
||||
row := db.QueryRow("SELECT EXISTS(SELECT username FROM [show roles] WHERE username = $1)", config.User)
|
||||
err = row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
func createUser(db *sql.DB, config database.Config) error {
|
||||
_, err := db.Exec("CREATE USER $1 WITH PASSWORD $2", config.User, &sql.NullString{String: config.Password, Valid: config.Password != ""})
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user