From c087445d510dbb6b8ae7b79f76aadf6ff52f55aa Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 30 Jun 2016 23:15:47 +0200 Subject: [PATCH 1/3] feat(cli): add db command to cli. --- pkg/cmd/grafana-cli/commands/commands.go | 60 ++++++++++++++++--- .../commands/reset_password_command.go | 40 +++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 pkg/cmd/grafana-cli/commands/reset_password_command.go diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 2099a576647..b03e77e6cbf 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -1,14 +1,45 @@ package commands import ( + "flag" "os" "github.com/codegangsta/cli" "github.com/fatih/color" "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" + "github.com/grafana/grafana/pkg/services/sqlstore" + "github.com/grafana/grafana/pkg/setting" ) -func runCommand(command func(commandLine CommandLine) error) func(context *cli.Context) { +var configFile = flag.String("config", "", "path to config file") +var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory") + +func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) { + return func(context *cli.Context) { + + flag.Parse() + setting.NewConfigContext(&setting.CommandLineArgs{ + Config: *configFile, + HomePath: *homePath, + Args: flag.Args(), + }) + + sqlstore.NewEngine() + + cmd := &contextCommandLine{context} + if err := command(cmd); err != nil { + logger.Errorf("\n%s: ", color.RedString("Error")) + logger.Errorf("%s\n\n", err) + + cmd.ShowHelp() + os.Exit(1) + } else { + logger.Info("\n\n") + } + } +} + +func runPluginCommand(command func(commandLine CommandLine) error) func(context *cli.Context) { return func(context *cli.Context) { cmd := &contextCommandLine{context} @@ -28,34 +59,42 @@ var pluginCommands = []cli.Command{ { Name: "install", Usage: "install ", - Action: runCommand(installCommand), + Action: runPluginCommand(installCommand), }, { Name: "list-remote", Usage: "list remote available plugins", - Action: runCommand(listremoteCommand), + Action: runPluginCommand(listremoteCommand), }, { Name: "list-versions", Usage: "list-versions ", - Action: runCommand(listversionsCommand), + Action: runPluginCommand(listversionsCommand), }, { Name: "update", Usage: "update ", Aliases: []string{"upgrade"}, - Action: runCommand(upgradeCommand), + Action: runPluginCommand(upgradeCommand), }, { Name: "update-all", Aliases: []string{"upgrade-all"}, Usage: "update all your installed plugins", - Action: runCommand(upgradeAllCommand), + Action: runPluginCommand(upgradeAllCommand), }, { Name: "ls", Usage: "list all installed plugins", - Action: runCommand(lsCommand), + Action: runPluginCommand(lsCommand), }, { Name: "uninstall", Aliases: []string{"remove"}, Usage: "uninstall ", - Action: runCommand(removeCommand), + Action: runPluginCommand(removeCommand), + }, +} + +var userCommands = []cli.Command{ + { + Name: "reset-admin", + Usage: "reset-admin ", + Action: runDbCommand(resetPasswordCommand), }, } @@ -65,4 +104,9 @@ var Commands = []cli.Command{ Usage: "Manage plugins for grafana", Subcommands: pluginCommands, }, + { + Name: "user", + Usage: "", + Subcommands: userCommands, + }, } diff --git a/pkg/cmd/grafana-cli/commands/reset_password_command.go b/pkg/cmd/grafana-cli/commands/reset_password_command.go new file mode 100644 index 00000000000..dac43aa7aba --- /dev/null +++ b/pkg/cmd/grafana-cli/commands/reset_password_command.go @@ -0,0 +1,40 @@ +package commands + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/util" +) + +func resetPasswordCommand(c CommandLine) error { + newPassword := c.Args().First() + + if len(newPassword) < 4 { + return fmt.Errorf("New password too short") + } + + userQuery := models.GetUserByIdQuery{Id: 1} + + if err := bus.Dispatch(&userQuery); err != nil { + return fmt.Errorf("Could not read user from database. Error: %v", err) + } + + passwordHashed := util.EncodePassword(newPassword, userQuery.Result.Salt) + + cmd := models.ChangeUserPasswordCommand{ + UserId: 1, + NewPassword: passwordHashed, + } + + if err := bus.Dispatch(&cmd); err != nil { + return fmt.Errorf("Failed to update user password") + } + + logger.Infof("Admin password changed successfully %s", color.GreenString("✔")) + + return nil +} From 5d821d349271774521e2b242bb2dd405127c1d12 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 24 Aug 2016 20:27:41 +0200 Subject: [PATCH 2/3] style(cli): minor fix --- pkg/cmd/grafana-cli/commands/reset_password_command.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/reset_password_command.go b/pkg/cmd/grafana-cli/commands/reset_password_command.go index dac43aa7aba..febabd2055b 100644 --- a/pkg/cmd/grafana-cli/commands/reset_password_command.go +++ b/pkg/cmd/grafana-cli/commands/reset_password_command.go @@ -10,6 +10,8 @@ import ( "github.com/grafana/grafana/pkg/util" ) +const AdminUserId = 1 + func resetPasswordCommand(c CommandLine) error { newPassword := c.Args().First() @@ -17,7 +19,7 @@ func resetPasswordCommand(c CommandLine) error { return fmt.Errorf("New password too short") } - userQuery := models.GetUserByIdQuery{Id: 1} + userQuery := models.GetUserByIdQuery{Id: AdminUserId} if err := bus.Dispatch(&userQuery); err != nil { return fmt.Errorf("Could not read user from database. Error: %v", err) @@ -26,7 +28,7 @@ func resetPasswordCommand(c CommandLine) error { passwordHashed := util.EncodePassword(newPassword, userQuery.Result.Salt) cmd := models.ChangeUserPasswordCommand{ - UserId: 1, + UserId: AdminUserId, NewPassword: passwordHashed, } From 96e8ecfa7b27dd5e50fdf8294564b581daa15843 Mon Sep 17 00:00:00 2001 From: bergquist Date: Fri, 9 Dec 2016 15:25:02 +0100 Subject: [PATCH 3/3] feat(cli): adds command to reset admin password closes #5479 --- build.go | 4 ++++ pkg/api/user.go | 5 +++-- pkg/cmd/grafana-cli/commands/commands.go | 12 ++++++------ .../grafana-cli/commands/reset_password_command.go | 6 ++++-- pkg/models/user.go | 6 ++++++ 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/build.go b/build.go index de828d3ef91..faa014d7619 100644 --- a/build.go +++ b/build.go @@ -73,6 +73,10 @@ func main() { case "setup": setup() + case "build-cli": + clean() + build("grafana-cli", "./pkg/cmd/grafana-cli", []string{}) + case "build": clean() for _, binary := range binaries { diff --git a/pkg/api/user.go b/pkg/api/user.go index 715103aacbd..c3e354f5095 100644 --- a/pkg/api/user.go +++ b/pkg/api/user.go @@ -157,8 +157,9 @@ func ChangeUserPassword(c *middleware.Context, cmd m.ChangeUserPasswordCommand) return ApiError(401, "Invalid old password", nil) } - if len(cmd.NewPassword) < 4 { - return ApiError(400, "New password too short", nil) + password := m.Password(cmd.NewPassword) + if password.IsWeak() { + return ApiError(400, "New password is too short", nil) } cmd.UserId = c.UserId diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 7dff8e936b8..8b2ecfcf7f5 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -90,10 +90,10 @@ var pluginCommands = []cli.Command{ }, } -var userCommands = []cli.Command{ +var adminCommands = []cli.Command{ { - Name: "reset-admin", - Usage: "reset-admin ", + Name: "reset-admin-password", + Usage: "reset-admin-password ", Action: runDbCommand(resetPasswordCommand), }, } @@ -105,8 +105,8 @@ var Commands = []cli.Command{ Subcommands: pluginCommands, }, { - Name: "user", - Usage: "", - Subcommands: userCommands, + Name: "admin", + Usage: "Grafana admin commands", + Subcommands: adminCommands, }, } diff --git a/pkg/cmd/grafana-cli/commands/reset_password_command.go b/pkg/cmd/grafana-cli/commands/reset_password_command.go index febabd2055b..af2b8b3f89a 100644 --- a/pkg/cmd/grafana-cli/commands/reset_password_command.go +++ b/pkg/cmd/grafana-cli/commands/reset_password_command.go @@ -15,8 +15,9 @@ const AdminUserId = 1 func resetPasswordCommand(c CommandLine) error { newPassword := c.Args().First() - if len(newPassword) < 4 { - return fmt.Errorf("New password too short") + password := models.Password(newPassword) + if password.IsWeak() { + return fmt.Errorf("New password is too short") } userQuery := models.GetUserByIdQuery{Id: AdminUserId} @@ -36,6 +37,7 @@ func resetPasswordCommand(c CommandLine) error { return fmt.Errorf("Failed to update user password") } + logger.Infof("\n") logger.Infof("Admin password changed successfully %s", color.GreenString("✔")) return nil diff --git a/pkg/models/user.go b/pkg/models/user.go index 8bfad7c2fb4..e14f4486ba3 100644 --- a/pkg/models/user.go +++ b/pkg/models/user.go @@ -10,6 +10,12 @@ var ( ErrUserNotFound = errors.New("User not found") ) +type Password string + +func (p Password) IsWeak() bool { + return len(p) <= 4 +} + type User struct { Id int64 Version int