2022-02-11 13:07:32 +00:00
|
|
|
package initialise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2022-02-16 12:30:49 +00:00
|
|
|
_ "embed"
|
2022-06-01 09:41:01 +00:00
|
|
|
"fmt"
|
2022-02-11 13:07:32 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-11 13:07:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newUser() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "user",
|
|
|
|
Short: "initialize only the database user",
|
|
|
|
Long: `Sets up the ZITADEL database user.
|
|
|
|
|
|
|
|
Prereqesits:
|
|
|
|
- cockroachdb
|
|
|
|
|
|
|
|
The user provided by flags needs priviledge to
|
|
|
|
- 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 08:05:09 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := MustNewConfig(viper.New())
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
err := initialise(config.Database, VerifyUser(config.Database.Username(), config.Database.Password()))
|
2022-03-28 08:05:09 +00:00
|
|
|
logging.OnError(err).Fatal("unable to init user")
|
2022-02-11 13:07:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 06:19:02 +00:00
|
|
|
func VerifyUser(username, password string) func(*sql.DB) error {
|
2022-02-16 12:30:49 +00:00
|
|
|
return func(db *sql.DB) error {
|
2022-03-15 06:19:02 +00:00
|
|
|
logging.WithFields("username", username).Info("verify user")
|
2022-08-31 07:52:43 +00:00
|
|
|
|
|
|
|
if password != "" {
|
|
|
|
createUserStmt += " WITH PASSWORD '" + password + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec(db, fmt.Sprintf(createUserStmt, username), []string{roleAlreadyExistsCode})
|
2022-02-11 13:07:32 +00:00
|
|
|
}
|
|
|
|
}
|