-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add database user creation
- Loading branch information
1 parent
1a1ee8f
commit d5dd9d1
Showing
9 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package users | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/term" | ||
|
||
"github.com/Scalingo/cli/config" | ||
"github.com/Scalingo/cli/io" | ||
"github.com/Scalingo/go-scalingo/v6" | ||
scErrors "github.com/Scalingo/go-utils/errors/v2" | ||
"github.com/Scalingo/gopassword" | ||
) | ||
|
||
func CreateUser(ctx context.Context, app, addonUUID, username string, readonly bool) error { | ||
isSupported, err := isDatabaseHandlesUserManagement(ctx, app, addonUUID) | ||
if err != nil { | ||
return scErrors.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 scErrors.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 scErrors.Wrap(ctx, err, "get Scalingo client") | ||
} | ||
|
||
user := scalingo.DatabaseCreateUserParam{ | ||
DatabaseID: addonUUID, | ||
Name: username, | ||
Password: password, | ||
PasswordConfirmation: confirmedPassword, | ||
ReadOnly: readonly, | ||
} | ||
databaseUsers, err := c.DatabaseCreateUser(ctx, app, addonUUID, user) | ||
if err != nil { | ||
return scErrors.Wrap(ctx, err, "create the given database user") | ||
} | ||
|
||
if isPasswordGenerated { | ||
fmt.Printf("User \"%s\" created with password \"%s\".\n", databaseUsers.Name, password) | ||
return nil | ||
} | ||
|
||
fmt.Printf("User \"%s\" created.\n", databaseUsers.Name) | ||
return nil | ||
} | ||
|
||
func askForPassword(ctx context.Context) (string, string, error) { | ||
fmt.Printf("Password: (Will be generated if left empty) ") | ||
|
||
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd())) | ||
if err != nil { | ||
return "", "", scErrors.Wrap(ctx, err, "read password") | ||
} | ||
|
||
fmt.Printf("\nPassword Confirmation: ") | ||
byteConfirmedPassword, err := term.ReadPassword(int(os.Stdin.Fd())) | ||
if err != nil { | ||
return "", "", scErrors.Wrap(ctx, err, "read password confirmation") | ||
} | ||
fmt.Printf("\n") | ||
|
||
return string(bytePassword), string(byteConfirmedPassword), nil | ||
} | ||
|
||
func isPasswordValid(password, confirmedPassword string) (string, bool) { | ||
if password != confirmedPassword { | ||
return "Password confirmation don't watch", false | ||
} | ||
if len(password) < 8 || len(password) > 64 { | ||
return "Password must be between 8 and 64 characters", false | ||
} | ||
return "", true | ||
} | ||
|
||
func isUsernameValid(username string) (string, bool) { | ||
if len(username) < 6 || len(username) > 32 { | ||
return "name must be between 6 and 32 characters", false | ||
} | ||
return "", true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters