Commit 96e8ecfa by bergquist

feat(cli): adds command to reset admin password

closes #5479
parent cd85e1f6
...@@ -73,6 +73,10 @@ func main() { ...@@ -73,6 +73,10 @@ func main() {
case "setup": case "setup":
setup() setup()
case "build-cli":
clean()
build("grafana-cli", "./pkg/cmd/grafana-cli", []string{})
case "build": case "build":
clean() clean()
for _, binary := range binaries { for _, binary := range binaries {
......
...@@ -157,8 +157,9 @@ func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) ...@@ -157,8 +157,9 @@ func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand)
return ApiError(401, "Invalid old password", nil) return ApiError(401, "Invalid old password", nil)
} }
if len(cmd.NewPassword) < 4 { password := m.Password(cmd.NewPassword)
return ApiError(400, "New password too short", nil) if password.IsWeak() {
return ApiError(400, "New password is too short", nil)
} }
cmd.UserId = c.UserId cmd.UserId = c.UserId
......
...@@ -90,10 +90,10 @@ var pluginCommands = []cli.Command{ ...@@ -90,10 +90,10 @@ var pluginCommands = []cli.Command{
}, },
} }
var userCommands = []cli.Command{ var adminCommands = []cli.Command{
{ {
Name: "reset-admin", Name: "reset-admin-password",
Usage: "reset-admin <new password>", Usage: "reset-admin-password <new password>",
Action: runDbCommand(resetPasswordCommand), Action: runDbCommand(resetPasswordCommand),
}, },
} }
...@@ -105,8 +105,8 @@ var Commands = []cli.Command{ ...@@ -105,8 +105,8 @@ var Commands = []cli.Command{
Subcommands: pluginCommands, Subcommands: pluginCommands,
}, },
{ {
Name: "user", Name: "admin",
Usage: "", Usage: "Grafana admin commands",
Subcommands: userCommands, Subcommands: adminCommands,
}, },
} }
...@@ -15,8 +15,9 @@ const AdminUserId = 1 ...@@ -15,8 +15,9 @@ const AdminUserId = 1
func resetPasswordCommand(c CommandLine) error { func resetPasswordCommand(c CommandLine) error {
newPassword := c.Args().First() newPassword := c.Args().First()
if len(newPassword) < 4 { password := models.Password(newPassword)
return fmt.Errorf("New password too short") if password.IsWeak() {
return fmt.Errorf("New password is too short")
} }
userQuery := models.GetUserByIdQuery{Id: AdminUserId} userQuery := models.GetUserByIdQuery{Id: AdminUserId}
...@@ -36,6 +37,7 @@ func resetPasswordCommand(c CommandLine) error { ...@@ -36,6 +37,7 @@ func resetPasswordCommand(c CommandLine) error {
return fmt.Errorf("Failed to update user password") return fmt.Errorf("Failed to update user password")
} }
logger.Infof("\n")
logger.Infof("Admin password changed successfully %s", color.GreenString("✔")) logger.Infof("Admin password changed successfully %s", color.GreenString("✔"))
return nil return nil
......
...@@ -10,6 +10,12 @@ var ( ...@@ -10,6 +10,12 @@ var (
ErrUserNotFound = errors.New("User not found") ErrUserNotFound = errors.New("User not found")
) )
type Password string
func (p Password) IsWeak() bool {
return len(p) <= 4
}
type User struct { type User struct {
Id int64 Id int64
Version int Version int
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment