'zrok admin create account'

This commit is contained in:
Michael Quigley 2024-01-30 12:59:56 -05:00
parent 00d46be77a
commit cfe13bd085
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
9 changed files with 75 additions and 9 deletions

View File

@ -0,0 +1,66 @@
package main
import (
"github.com/openziti/zrok/controller"
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/store"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
adminCreateCmd.AddCommand(newAdminCreateAccount().cmd)
}
type adminCreateAccount struct {
cmd *cobra.Command
}
func newAdminCreateAccount() *adminCreateAccount {
cmd := &cobra.Command{
Use: "account <configPath}> <email> <password>",
Short: "Pre-populate an account in the database; returns an enable token for the account",
Args: cobra.ExactArgs(3),
}
command := &adminCreateAccount{cmd: cmd}
cmd.Run = command.run
return command
}
func (cmd *adminCreateAccount) run(_ *cobra.Command, args []string) {
cfg, err := config.LoadConfig(args[0])
if err != nil {
panic(err)
}
str, err := store.Open(cfg.Store)
if err != nil {
panic(err)
}
token, err := controller.CreateToken()
if err != nil {
panic(err)
}
hpwd, err := controller.HashPassword(args[2])
if err != nil {
panic(err)
}
trx, err := str.Begin()
if err != nil {
panic(err)
}
defer func() {
if err := trx.Commit(); err != nil {
panic(err)
}
}()
a := &store.Account{
Email: args[1],
Salt: hpwd.Salt,
Password: hpwd.Password,
Token: token,
}
if _, err := str.CreateAccount(a, trx); err != nil {
panic(err)
}
logrus.Infof("account token = %v", token)
}

View File

@ -62,7 +62,7 @@ func (h *accessHandler) Handle(params share.AccessParams, principal *rest_model_
return share.NewAccessNotFound()
}
feToken, err := createToken()
feToken, err := CreateToken()
if err != nil {
logrus.Error(err)
return share.NewAccessInternalServerError()

View File

@ -50,7 +50,7 @@ func (h *createFrontendHandler) Handle(params admin.CreateFrontendParams, princi
}
defer func() { _ = tx.Rollback() }()
feToken, err := createToken()
feToken, err := CreateToken()
if err != nil {
logrus.Errorf("error creating frontend token: %v", err)
return admin.NewCreateFrontendInternalServerError()

View File

@ -55,7 +55,7 @@ func (h *inviteHandler) Handle(params account.InviteParams) middleware.Responder
logrus.Infof("using invite token '%v' to process invite request for '%v'", inviteToken.Token, params.Body.Email)
}
token, err = createToken()
token, err = CreateToken()
if err != nil {
logrus.Error(err)
return account.NewInviteInternalServerError()

View File

@ -24,7 +24,7 @@ func salt() string {
return base64.StdEncoding.EncodeToString(buf)
}
func hashPassword(password string) (*hashedPassword, error) {
func HashPassword(password string) (*hashedPassword, error) {
return rehashPassword(password, salt())
}

View File

@ -38,7 +38,7 @@ func (h *registerHandler) Handle(params account.RegisterParams) middleware.Respo
return account.NewRegisterNotFound()
}
token, err := createToken()
token, err := CreateToken()
if err != nil {
logrus.Errorf("error creating token for request '%v' (%v): %v", params.Body.Token, ar.Email, err)
return account.NewRegisterInternalServerError()
@ -49,7 +49,7 @@ func (h *registerHandler) Handle(params account.RegisterParams) middleware.Respo
return account.NewRegisterUnprocessableEntity().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
hpwd, err := hashPassword(params.Body.Password)
hpwd, err := HashPassword(params.Body.Password)
if err != nil {
logrus.Errorf("error hashing password for request '%v' (%v): %v", params.Body.Token, ar.Email, err)
return account.NewRegisterInternalServerError()

View File

@ -53,7 +53,7 @@ func (handler *resetPasswordHandler) Handle(params account.ResetPasswordParams)
return account.NewResetPasswordUnprocessableEntity().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
hpwd, err := hashPassword(params.Body.Password)
hpwd, err := HashPassword(params.Body.Password)
if err != nil {
logrus.Errorf("error hashing password for '%v' (%v): %v", params.Body.Token, a.Email, err)
return account.NewResetPasswordRequestInternalServerError()

View File

@ -34,7 +34,7 @@ func (handler *resetPasswordRequestHandler) Handle(params account.ResetPasswordR
}
defer func() { _ = tx.Rollback() }()
token, err = createToken()
token, err = CreateToken()
if err != nil {
logrus.Errorf("error creating token for '%v': %v", params.Body.EmailAddress, err)
return account.NewResetPasswordRequestInternalServerError()

View File

@ -65,7 +65,7 @@ func createShareToken() (string, error) {
return gen(), nil
}
func createToken() (string, error) {
func CreateToken() (string, error) {
gen, err := nanoid.CustomASCII("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 12)
if err != nil {
return "", err