Skip to content

Commit

Permalink
feat(cmd): add database user password update
Browse files Browse the repository at this point in the history
  • Loading branch information
curzolapierre committed Feb 9, 2024
1 parent 6649668 commit e90e9ab
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ var (
&databaseListUsers,
&databaseDeleteUser,
&databaseCreateUser,
&databaseUpdateUserPassword,

// Maintenance
&databaseMaintenanceList,
Expand Down
37 changes: 37 additions & 0 deletions cmd/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@ Only available on ` + fmt.Sprintf("%s", dbUsers.SupportedAddons),
return nil
},
}
databaseUpdateUserPassword = cli.Command{
Name: "database-users-update-password",
Aliases: []string{"database-update-user-password"},
Category: "Addons",
ArgsUsage: "user",
Usage: "Update a database user's password",
Flags: []cli.Flag{
&appFlag,
&addonFlag,
},
Description: CommandDescription{
Description: `Update a non-protected database user's password.
Only available on ` + fmt.Sprintf("%s", dbUsers.SupportedAddons),
Examples: []string{
"scalingo --app myapp --addon addon-uuid database-users-update-password my_user",
},
}.Render(),

Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.ShowCommandHelp(c, "database-users-update-password")
}

currentApp := detect.CurrentApp(c)
utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs)
addonName := addonUUIDFromFlags(c, currentApp, true)

username := c.Args().First()

err := dbUsers.UpdateUser(c.Context, currentApp, addonName, username)
if err != nil {
errorQuit(c.Context, err)
}
return nil
},
}
)

func parseScheduleAtFlag(flag string) (int, *time.Location, error) {
Expand Down
70 changes: 70 additions & 0 deletions db/users/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package users

import (
"context"
"fmt"

"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
"github.com/Scalingo/go-scalingo/v6"
"github.com/Scalingo/go-utils/errors/v2"
"github.com/Scalingo/gopassword"
)

func UpdateUser(ctx context.Context, app, addonUUID, username string) error {
isSupported, err := doesDatabaseHandleUserManagement(ctx, app, addonUUID)
if err != nil {
return errors.Wrap(ctx, err, "get user management information")
}

if !isSupported {
io.Error(ErrDatabaseNotSupportUserManagement)
return nil
}

if usernameValidation, ok := isUsernameValid(username); !ok {
io.Error(usernameValidation)
return nil
}

password, confirmedPassword, err := askForPassword(ctx)
if err != nil {
return errors.Wrap(ctx, err, "ask for password")
}

passwordValidation, ok := isPasswordValid(password, confirmedPassword)
if !ok && password != "" {
io.Error(passwordValidation)
return nil
}

isPasswordGenerated := false
if password == "" {
isPasswordGenerated = true
password = gopassword.Generate(64)
confirmedPassword = password
}

c, err := config.ScalingoClient(ctx)
if err != nil {
return errors.Wrap(ctx, err, "get Scalingo client")
}

user := scalingo.DatabaseUpdateUserParam{

Check failure on line 53 in db/users/update.go

View workflow job for this annotation

GitHub Actions / Unit Tests

undefined: scalingo.DatabaseUpdateUserParam
DatabaseID: addonUUID,
Password: password,
PasswordConfirmation: confirmedPassword,
}
databaseUsers, err := c.DatabaseUpdateUser(ctx, app, addonUUID, username, user)

Check failure on line 58 in db/users/update.go

View workflow job for this annotation

GitHub Actions / Unit Tests

c.DatabaseUpdateUser undefined (type *scalingo.Client has no field or method DatabaseUpdateUser)
if err != nil {
return errors.Wrap(ctx, err, "update password of the given database user")
}

if isPasswordGenerated {
fmt.Printf("User \"%s\" updated with password \"%s\".\n", databaseUsers.Name, password)
return nil
}

fmt.Printf("User \"%s\" password updated.\n", databaseUsers.Name)
return nil
}

0 comments on commit e90e9ab

Please sign in to comment.