Files
coder/cli/resetpassword.go
T

99 lines
2.7 KiB
Go
Raw Normal View History

2022-05-12 12:32:56 -05:00
package cli
import (
"database/sql"
"fmt"
2022-05-12 12:32:56 -05:00
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/migrations"
"github.com/coder/coder/v2/coderd/userpassword"
2022-05-12 12:32:56 -05:00
)
2023-03-23 17:42:20 -05:00
func (*RootCmd) resetPassword() *clibase.Cmd {
2023-02-18 18:32:09 -06:00
var postgresURL string
2022-05-12 12:32:56 -05:00
2023-03-23 17:42:20 -05:00
root := &clibase.Cmd{
Use: "reset-password <username>",
Short: "Directly connect to the database to reset a user's password",
Middleware: clibase.RequireNArgs(1),
Handler: func(inv *clibase.Invocation) error {
username := inv.Args[0]
2022-05-12 12:32:56 -05:00
sqlDB, err := sql.Open("postgres", postgresURL)
if err != nil {
return xerrors.Errorf("dial postgres: %w", err)
}
defer sqlDB.Close()
err = sqlDB.Ping()
if err != nil {
return xerrors.Errorf("ping postgres: %w", err)
}
err = migrations.EnsureClean(sqlDB)
2022-05-12 12:32:56 -05:00
if err != nil {
return xerrors.Errorf("database needs migration: %w", err)
}
db := database.New(sqlDB)
2023-03-23 17:42:20 -05:00
user, err := db.GetUserByEmailOrUsername(inv.Context(), database.GetUserByEmailOrUsernameParams{
2022-05-12 12:32:56 -05:00
Username: username,
})
if err != nil {
return xerrors.Errorf("retrieving user: %w", err)
}
2023-03-23 17:42:20 -05:00
password, err := cliui.Prompt(inv, cliui.PromptOptions{
2023-06-07 00:22:58 -05:00
Text: "Enter new " + cliui.DefaultStyles.Field.Render("password") + ":",
Secret: true,
Validate: func(s string) error {
return userpassword.Validate(s)
},
2022-05-12 12:32:56 -05:00
})
if err != nil {
return xerrors.Errorf("password prompt: %w", err)
}
2023-03-23 17:42:20 -05:00
confirmedPassword, err := cliui.Prompt(inv, cliui.PromptOptions{
2023-06-07 00:22:58 -05:00
Text: "Confirm " + cliui.DefaultStyles.Field.Render("password") + ":",
2022-05-12 12:32:56 -05:00
Secret: true,
Validate: cliui.ValidateNotEmpty,
})
if err != nil {
return xerrors.Errorf("confirm password prompt: %w", err)
}
if password != confirmedPassword {
return xerrors.New("Passwords do not match")
}
hashedPassword, err := userpassword.Hash(password)
if err != nil {
return xerrors.Errorf("hash password: %w", err)
}
2023-03-23 17:42:20 -05:00
err = db.UpdateUserHashedPassword(inv.Context(), database.UpdateUserHashedPasswordParams{
2022-05-12 12:32:56 -05:00
ID: user.ID,
HashedPassword: []byte(hashedPassword),
})
if err != nil {
return xerrors.Errorf("updating password: %w", err)
}
2023-06-07 00:22:58 -05:00
_, _ = fmt.Fprintf(inv.Stdout, "\nPassword has been reset for user %s!\n", cliui.DefaultStyles.Keyword.Render(user.Username))
2022-05-12 12:32:56 -05:00
return nil
},
}
2023-03-23 17:42:20 -05:00
root.Options = clibase.OptionSet{
{
Flag: "postgres-url",
Description: "URL of a PostgreSQL database to connect to.",
Env: "CODER_PG_CONNECTION_URL",
Value: clibase.StringOf(&postgresURL),
},
}
2022-05-12 12:32:56 -05:00
return root
}