2016-06-30 16:15:47 -05:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-07-02 09:29:10 -05:00
|
|
|
"bufio"
|
2021-06-21 00:51:33 -05:00
|
|
|
"context"
|
2016-06-30 16:15:47 -05:00
|
|
|
"fmt"
|
2020-07-02 09:29:10 -05:00
|
|
|
"os"
|
2016-06-30 16:15:47 -05:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
2019-05-27 03:47:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
|
2016-06-30 16:15:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-05-27 03:47:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2016-06-30 16:15:47 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2019-10-23 03:40:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2016-06-30 16:15:47 -05:00
|
|
|
)
|
|
|
|
|
2016-08-24 13:27:41 -05:00
|
|
|
const AdminUserId = 1
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func resetPasswordCommand(c utils.CommandLine, sqlStore *sqlstore.SQLStore) error {
|
2020-07-02 09:29:10 -05:00
|
|
|
newPassword := ""
|
|
|
|
|
|
|
|
if c.Bool("password-from-stdin") {
|
|
|
|
logger.Infof("New Password: ")
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
if ok := scanner.Scan(); !ok {
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return fmt.Errorf("can't read password from stdin: %w", err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("can't read password from stdin")
|
|
|
|
}
|
|
|
|
newPassword = scanner.Text()
|
|
|
|
} else {
|
|
|
|
newPassword = c.Args().First()
|
|
|
|
}
|
2016-06-30 16:15:47 -05:00
|
|
|
|
2016-12-09 08:25:02 -06:00
|
|
|
password := models.Password(newPassword)
|
|
|
|
if password.IsWeak() {
|
2020-02-26 05:27:31 -06:00
|
|
|
return fmt.Errorf("new password is too short")
|
2016-06-30 16:15:47 -05:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:27:41 -05:00
|
|
|
userQuery := models.GetUserByIdQuery{Id: AdminUserId}
|
2016-06-30 16:15:47 -05:00
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(context.Background(), &userQuery); err != nil {
|
2020-02-26 05:27:31 -06:00
|
|
|
return fmt.Errorf("could not read user from database. Error: %v", err)
|
2016-06-30 16:15:47 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 03:40:12 -05:00
|
|
|
passwordHashed, err := util.EncodePassword(newPassword, userQuery.Result.Salt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-30 16:15:47 -05:00
|
|
|
|
|
|
|
cmd := models.ChangeUserPasswordCommand{
|
2016-08-24 13:27:41 -05:00
|
|
|
UserId: AdminUserId,
|
2016-06-30 16:15:47 -05:00
|
|
|
NewPassword: passwordHashed,
|
|
|
|
}
|
|
|
|
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(context.Background(), &cmd); err != nil {
|
2020-02-26 05:27:31 -06:00
|
|
|
return errutil.Wrapf(err, "failed to update user password")
|
2016-06-30 16:15:47 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 08:25:02 -06:00
|
|
|
logger.Infof("\n")
|
2016-06-30 16:15:47 -05:00
|
|
|
logger.Infof("Admin password changed successfully %s", color.GreenString("✔"))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|